2018-07-25 09:30:33 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-28 13:17:42 +00:00
|
|
|
class PagesDomain < ApplicationRecord
|
2019-08-31 19:57:00 +00:00
|
|
|
VERIFICATION_KEY = 'gitlab-pages-verification-code'
|
2018-02-06 13:25:46 +00:00
|
|
|
VERIFICATION_THRESHOLD = 3.days.freeze
|
2019-06-24 20:35:12 +00:00
|
|
|
SSL_RENEWAL_THRESHOLD = 30.days.freeze
|
2018-02-06 13:25:46 +00:00
|
|
|
|
2019-06-21 12:06:12 +00:00
|
|
|
enum certificate_source: { user_provided: 0, gitlab_provided: 1 }, _prefix: :certificate
|
2019-12-14 15:07:56 +00:00
|
|
|
enum domain_type: { instance: 0, group: 1, project: 2 }, _prefix: :domain_type
|
2019-06-21 12:06:12 +00:00
|
|
|
|
2016-02-10 11:07:46 +00:00
|
|
|
belongs_to :project
|
2019-06-06 18:55:31 +00:00
|
|
|
has_many :acme_orders, class_name: "PagesDomainAcmeOrder"
|
2016-02-10 11:07:46 +00:00
|
|
|
|
2017-05-19 14:07:38 +00:00
|
|
|
validates :domain, hostname: { allow_numeric_hostname: true }
|
2017-02-22 00:40:04 +00:00
|
|
|
validates :domain, uniqueness: { case_sensitive: false }
|
2019-07-12 14:19:01 +00:00
|
|
|
validates :certificate, presence: { message: 'must be present if HTTPS-only is enabled' },
|
|
|
|
if: :certificate_should_be_present?
|
2018-01-03 08:07:03 +00:00
|
|
|
validates :certificate, certificate: true, if: ->(domain) { domain.certificate.present? }
|
2019-07-12 14:19:01 +00:00
|
|
|
validates :key, presence: { message: 'must be present if HTTPS-only is enabled' },
|
|
|
|
if: :certificate_should_be_present?
|
2019-09-07 00:29:03 +00:00
|
|
|
validates :key, certificate_key: true, named_ecdsa_key: true, if: ->(domain) { domain.key.present? }
|
2018-02-06 13:25:46 +00:00
|
|
|
validates :verification_code, presence: true, allow_blank: false
|
2016-02-10 11:07:46 +00:00
|
|
|
|
2016-02-12 15:05:17 +00:00
|
|
|
validate :validate_pages_domain
|
|
|
|
validate :validate_matching_key, if: ->(domain) { domain.certificate.present? || domain.key.present? }
|
2019-07-22 15:38:08 +00:00
|
|
|
validate :validate_intermediates, if: ->(domain) { domain.certificate.present? && domain.certificate_changed? }
|
2016-02-10 14:06:31 +00:00
|
|
|
|
2019-11-15 21:06:14 +00:00
|
|
|
default_value_for(:auto_ssl_enabled, allow_nil: false) { ::Gitlab::LetsEncrypt.enabled? }
|
2019-12-14 15:07:56 +00:00
|
|
|
default_value_for :domain_type, allow_nil: false, value: :project
|
|
|
|
default_value_for :wildcard, allow_nil: false, value: false
|
2019-11-15 21:06:14 +00:00
|
|
|
|
2016-06-03 19:01:54 +00:00
|
|
|
attr_encrypted :key,
|
|
|
|
mode: :per_attribute_iv_and_salt,
|
2016-06-28 08:14:24 +00:00
|
|
|
insecure_mode: true,
|
2018-05-19 13:03:29 +00:00
|
|
|
key: Settings.attr_encrypted_db_key_base,
|
2016-06-03 19:01:54 +00:00
|
|
|
algorithm: 'aes-256-cbc'
|
2016-02-10 11:07:46 +00:00
|
|
|
|
2018-02-06 13:25:46 +00:00
|
|
|
after_initialize :set_verification_code
|
2017-10-20 06:33:52 +00:00
|
|
|
after_create :update_daemon
|
2019-04-23 09:30:18 +00:00
|
|
|
after_update :update_daemon, if: :saved_change_to_pages_config?
|
2017-10-20 06:33:52 +00:00
|
|
|
after_destroy :update_daemon
|
2016-02-10 11:07:46 +00:00
|
|
|
|
2018-02-06 13:25:46 +00:00
|
|
|
scope :enabled, -> { where('enabled_until >= ?', Time.now ) }
|
|
|
|
scope :needs_verification, -> do
|
|
|
|
verified_at = arel_table[:verified_at]
|
|
|
|
enabled_until = arel_table[:enabled_until]
|
|
|
|
threshold = Time.now + VERIFICATION_THRESHOLD
|
|
|
|
|
|
|
|
where(verified_at.eq(nil).or(enabled_until.eq(nil).or(enabled_until.lt(threshold))))
|
|
|
|
end
|
|
|
|
|
2019-06-24 20:35:12 +00:00
|
|
|
scope :need_auto_ssl_renewal, -> do
|
|
|
|
expiring = where(certificate_valid_not_after: nil).or(
|
|
|
|
where(arel_table[:certificate_valid_not_after].lt(SSL_RENEWAL_THRESHOLD.from_now)))
|
|
|
|
|
|
|
|
user_provided_or_expiring = certificate_user_provided.or(expiring)
|
|
|
|
|
|
|
|
where(auto_ssl_enabled: true).merge(user_provided_or_expiring)
|
|
|
|
end
|
|
|
|
|
2019-04-30 12:05:54 +00:00
|
|
|
scope :for_removal, -> { where("remove_at < ?", Time.now) }
|
|
|
|
|
2018-02-06 13:25:46 +00:00
|
|
|
def verified?
|
|
|
|
!!verified_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def unverified?
|
|
|
|
!verified?
|
|
|
|
end
|
|
|
|
|
|
|
|
def enabled?
|
|
|
|
!Gitlab::CurrentSettings.pages_domain_verification_enabled? || enabled_until.present?
|
|
|
|
end
|
|
|
|
|
2018-01-03 08:07:03 +00:00
|
|
|
def https?
|
|
|
|
certificate.present?
|
|
|
|
end
|
|
|
|
|
2016-02-10 14:06:31 +00:00
|
|
|
def to_param
|
|
|
|
domain
|
|
|
|
end
|
|
|
|
|
2016-02-10 11:07:46 +00:00
|
|
|
def url
|
|
|
|
return unless domain
|
|
|
|
|
2018-01-08 01:27:19 +00:00
|
|
|
if certificate.present?
|
2016-02-15 14:01:42 +00:00
|
|
|
"https://#{domain}"
|
2016-02-10 11:07:46 +00:00
|
|
|
else
|
2016-02-15 14:01:42 +00:00
|
|
|
"http://#{domain}"
|
2016-02-10 11:07:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-10 14:06:31 +00:00
|
|
|
def has_matching_key?
|
2016-02-12 15:05:17 +00:00
|
|
|
return false unless x509
|
|
|
|
return false unless pkey
|
2016-02-10 14:06:31 +00:00
|
|
|
|
|
|
|
# We compare the public key stored in certificate with public key from certificate key
|
|
|
|
x509.check_private_key(pkey)
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_intermediates?
|
|
|
|
return false unless x509
|
|
|
|
|
2016-02-12 15:05:17 +00:00
|
|
|
# self-signed certificates doesn't have the certificate chain
|
|
|
|
return true if x509.verify(x509.public_key)
|
|
|
|
|
2016-02-10 14:06:31 +00:00
|
|
|
store = OpenSSL::X509::Store.new
|
|
|
|
store.set_default_paths
|
|
|
|
|
|
|
|
# This forces to load all intermediate certificates stored in `certificate`
|
|
|
|
Tempfile.open('certificate_chain') do |f|
|
|
|
|
f.write(certificate)
|
|
|
|
f.flush
|
|
|
|
store.add_file(f.path)
|
|
|
|
end
|
|
|
|
|
|
|
|
store.verify(x509)
|
|
|
|
rescue OpenSSL::X509::StoreError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def expired?
|
|
|
|
return false unless x509
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2016-02-10 14:06:31 +00:00
|
|
|
current = Time.new
|
2016-02-14 18:58:45 +00:00
|
|
|
current < x509.not_before || x509.not_after < current
|
2016-02-10 14:06:31 +00:00
|
|
|
end
|
|
|
|
|
2017-11-13 16:05:44 +00:00
|
|
|
def expiration
|
|
|
|
x509&.not_after
|
|
|
|
end
|
|
|
|
|
2016-02-10 14:06:31 +00:00
|
|
|
def subject
|
|
|
|
return unless x509
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2016-02-14 18:58:45 +00:00
|
|
|
x509.subject.to_s
|
2016-02-10 14:06:31 +00:00
|
|
|
end
|
|
|
|
|
2016-02-12 15:05:17 +00:00
|
|
|
def certificate_text
|
|
|
|
@certificate_text ||= x509.try(:to_text)
|
2016-02-10 14:06:31 +00:00
|
|
|
end
|
|
|
|
|
2018-02-06 13:25:46 +00:00
|
|
|
# Verification codes may be TXT records for domain or verification_domain, to
|
|
|
|
# support the use of CNAME records on domain.
|
|
|
|
def verification_domain
|
|
|
|
return unless domain.present?
|
|
|
|
|
|
|
|
"_#{VERIFICATION_KEY}.#{domain}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def keyed_verification_code
|
|
|
|
return unless verification_code.present?
|
|
|
|
|
|
|
|
"#{VERIFICATION_KEY}=#{verification_code}"
|
|
|
|
end
|
|
|
|
|
2019-06-06 19:14:09 +00:00
|
|
|
def certificate=(certificate)
|
|
|
|
super(certificate)
|
|
|
|
|
|
|
|
# set nil, if certificate is nil
|
|
|
|
self.certificate_valid_not_before = x509&.not_before
|
|
|
|
self.certificate_valid_not_after = x509&.not_after
|
|
|
|
end
|
|
|
|
|
2019-06-21 12:06:12 +00:00
|
|
|
def user_provided_key
|
|
|
|
key if certificate_user_provided?
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_provided_key=(key)
|
|
|
|
self.key = key
|
|
|
|
self.certificate_source = 'user_provided' if key_changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_provided_certificate
|
|
|
|
certificate if certificate_user_provided?
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_provided_certificate=(certificate)
|
|
|
|
self.certificate = certificate
|
|
|
|
self.certificate_source = 'user_provided' if certificate_changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitlab_provided_certificate=(certificate)
|
|
|
|
self.certificate = certificate
|
|
|
|
self.certificate_source = 'gitlab_provided' if certificate_changed?
|
|
|
|
end
|
|
|
|
|
|
|
|
def gitlab_provided_key=(key)
|
|
|
|
self.key = key
|
|
|
|
self.certificate_source = 'gitlab_provided' if key_changed?
|
|
|
|
end
|
|
|
|
|
2019-09-06 05:20:05 +00:00
|
|
|
def pages_virtual_domain
|
2019-09-25 09:06:04 +00:00
|
|
|
return unless pages_deployed?
|
|
|
|
|
2019-09-06 05:20:05 +00:00
|
|
|
Pages::VirtualDomain.new([project], domain: self)
|
|
|
|
end
|
|
|
|
|
2016-02-10 15:45:59 +00:00
|
|
|
private
|
|
|
|
|
2019-09-25 09:06:04 +00:00
|
|
|
def pages_deployed?
|
2019-10-01 12:05:59 +00:00
|
|
|
# TODO: remove once `pages_metadatum` is migrated
|
|
|
|
# https://gitlab.com/gitlab-org/gitlab/issues/33106
|
|
|
|
unless project.pages_metadatum
|
|
|
|
Gitlab::BackgroundMigration::MigratePagesMetadata
|
|
|
|
.new
|
|
|
|
.perform_on_relation(Project.where(id: project_id))
|
|
|
|
|
|
|
|
project.reset
|
|
|
|
end
|
|
|
|
|
2019-09-25 09:06:04 +00:00
|
|
|
project.pages_metadatum&.deployed?
|
|
|
|
end
|
|
|
|
|
2018-02-06 13:25:46 +00:00
|
|
|
def set_verification_code
|
|
|
|
return if self.verification_code.present?
|
|
|
|
|
|
|
|
self.verification_code = SecureRandom.hex(16)
|
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2017-10-20 06:33:52 +00:00
|
|
|
def update_daemon
|
2019-12-14 15:07:56 +00:00
|
|
|
return if domain_type_instance?
|
|
|
|
|
2016-02-10 14:06:31 +00:00
|
|
|
::Projects::UpdatePagesConfigurationService.new(project).execute
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2016-02-10 14:06:31 +00:00
|
|
|
|
2019-04-23 09:30:18 +00:00
|
|
|
def saved_change_to_pages_config?
|
2019-01-15 21:05:36 +00:00
|
|
|
saved_change_to_project_id? ||
|
|
|
|
saved_change_to_domain? ||
|
|
|
|
saved_change_to_certificate? ||
|
|
|
|
saved_change_to_key? ||
|
2018-02-06 13:25:46 +00:00
|
|
|
became_enabled? ||
|
|
|
|
became_disabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def became_enabled?
|
2019-01-15 21:05:36 +00:00
|
|
|
enabled_until.present? && !enabled_until_before_last_save.present?
|
2018-02-06 13:25:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def became_disabled?
|
2019-01-15 21:05:36 +00:00
|
|
|
!enabled_until.present? && enabled_until_before_last_save.present?
|
2018-02-06 13:25:46 +00:00
|
|
|
end
|
|
|
|
|
2016-02-10 14:06:31 +00:00
|
|
|
def validate_matching_key
|
|
|
|
unless has_matching_key?
|
|
|
|
self.errors.add(:key, "doesn't match the certificate")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_intermediates
|
|
|
|
unless has_intermediates?
|
|
|
|
self.errors.add(:certificate, 'misses intermediates')
|
|
|
|
end
|
2016-02-10 11:07:46 +00:00
|
|
|
end
|
2016-02-12 15:05:17 +00:00
|
|
|
|
|
|
|
def validate_pages_domain
|
|
|
|
return unless domain
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2017-05-19 14:07:38 +00:00
|
|
|
if domain.downcase.ends_with?(Settings.pages.host.downcase)
|
2016-02-12 15:05:17 +00:00
|
|
|
self.errors.add(:domain, "*.#{Settings.pages.host} is restricted")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def x509
|
2019-06-06 19:14:09 +00:00
|
|
|
return unless certificate.present?
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2016-02-12 15:05:17 +00:00
|
|
|
@x509 ||= OpenSSL::X509::Certificate.new(certificate)
|
|
|
|
rescue OpenSSL::X509::CertificateError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def pkey
|
|
|
|
return unless key
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2019-09-07 00:29:03 +00:00
|
|
|
@pkey ||= OpenSSL::PKey.read(key)
|
2016-02-12 15:05:17 +00:00
|
|
|
rescue OpenSSL::PKey::PKeyError, OpenSSL::Cipher::CipherError
|
|
|
|
nil
|
|
|
|
end
|
2019-07-12 14:19:01 +00:00
|
|
|
|
|
|
|
def certificate_should_be_present?
|
|
|
|
!auto_ssl_enabled? && project&.pages_https_only?
|
|
|
|
end
|
2016-02-10 11:07:46 +00:00
|
|
|
end
|