2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-02-10 06:07:46 -05:00
|
|
|
class PagesDomain < ActiveRecord::Base
|
2018-02-06 08:25:46 -05:00
|
|
|
VERIFICATION_KEY = 'gitlab-pages-verification-code'.freeze
|
|
|
|
VERIFICATION_THRESHOLD = 3.days.freeze
|
|
|
|
|
2016-02-10 06:07:46 -05:00
|
|
|
belongs_to :project
|
|
|
|
|
2017-05-19 10:07:38 -04:00
|
|
|
validates :domain, hostname: { allow_numeric_hostname: true }
|
2017-02-21 19:40:04 -05:00
|
|
|
validates :domain, uniqueness: { case_sensitive: false }
|
2018-01-03 03:07:03 -05:00
|
|
|
validates :certificate, presence: { message: 'must be present if HTTPS-only is enabled' }, if: ->(domain) { domain.project&.pages_https_only? }
|
|
|
|
validates :certificate, certificate: true, if: ->(domain) { domain.certificate.present? }
|
|
|
|
validates :key, presence: { message: 'must be present if HTTPS-only is enabled' }, if: ->(domain) { domain.project&.pages_https_only? }
|
|
|
|
validates :key, certificate_key: true, if: ->(domain) { domain.key.present? }
|
2018-02-06 08:25:46 -05:00
|
|
|
validates :verification_code, presence: true, allow_blank: false
|
2016-02-10 06:07:46 -05:00
|
|
|
|
2016-02-12 10:05:17 -05:00
|
|
|
validate :validate_pages_domain
|
|
|
|
validate :validate_matching_key, if: ->(domain) { domain.certificate.present? || domain.key.present? }
|
2016-02-10 09:06:31 -05:00
|
|
|
validate :validate_intermediates, if: ->(domain) { domain.certificate.present? }
|
|
|
|
|
2016-06-03 15:01:54 -04:00
|
|
|
attr_encrypted :key,
|
|
|
|
mode: :per_attribute_iv_and_salt,
|
2016-06-28 04:14:24 -04:00
|
|
|
insecure_mode: true,
|
2018-05-19 09:03:29 -04:00
|
|
|
key: Settings.attr_encrypted_db_key_base,
|
2016-06-03 15:01:54 -04:00
|
|
|
algorithm: 'aes-256-cbc'
|
2016-02-10 06:07:46 -05:00
|
|
|
|
2018-02-06 08:25:46 -05:00
|
|
|
after_initialize :set_verification_code
|
2017-10-20 02:33:52 -04:00
|
|
|
after_create :update_daemon
|
2018-02-06 08:25:46 -05:00
|
|
|
after_update :update_daemon, if: :pages_config_changed?
|
2017-10-20 02:33:52 -04:00
|
|
|
after_destroy :update_daemon
|
2016-02-10 06:07:46 -05:00
|
|
|
|
2018-02-06 08:25:46 -05: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
|
|
|
|
|
|
|
|
def verified?
|
|
|
|
!!verified_at
|
|
|
|
end
|
|
|
|
|
|
|
|
def unverified?
|
|
|
|
!verified?
|
|
|
|
end
|
|
|
|
|
|
|
|
def enabled?
|
|
|
|
!Gitlab::CurrentSettings.pages_domain_verification_enabled? || enabled_until.present?
|
|
|
|
end
|
|
|
|
|
2018-01-03 03:07:03 -05:00
|
|
|
def https?
|
|
|
|
certificate.present?
|
|
|
|
end
|
|
|
|
|
2016-02-10 09:06:31 -05:00
|
|
|
def to_param
|
|
|
|
domain
|
|
|
|
end
|
|
|
|
|
2016-02-10 06:07:46 -05:00
|
|
|
def url
|
|
|
|
return unless domain
|
|
|
|
|
2018-01-07 20:27:19 -05:00
|
|
|
if certificate.present?
|
2016-02-15 09:01:42 -05:00
|
|
|
"https://#{domain}"
|
2016-02-10 06:07:46 -05:00
|
|
|
else
|
2016-02-15 09:01:42 -05:00
|
|
|
"http://#{domain}"
|
2016-02-10 06:07:46 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-10 09:06:31 -05:00
|
|
|
def has_matching_key?
|
2016-02-12 10:05:17 -05:00
|
|
|
return false unless x509
|
|
|
|
return false unless pkey
|
2016-02-10 09:06:31 -05: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 10:05:17 -05:00
|
|
|
# self-signed certificates doesn't have the certificate chain
|
|
|
|
return true if x509.verify(x509.public_key)
|
|
|
|
|
2016-02-10 09:06:31 -05: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 04:02:39 -05:00
|
|
|
|
2016-02-10 09:06:31 -05:00
|
|
|
current = Time.new
|
2016-02-14 13:58:45 -05:00
|
|
|
current < x509.not_before || x509.not_after < current
|
2016-02-10 09:06:31 -05:00
|
|
|
end
|
|
|
|
|
2017-11-13 11:05:44 -05:00
|
|
|
def expiration
|
|
|
|
x509&.not_after
|
|
|
|
end
|
|
|
|
|
2016-02-10 09:06:31 -05:00
|
|
|
def subject
|
|
|
|
return unless x509
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2016-02-14 13:58:45 -05:00
|
|
|
x509.subject.to_s
|
2016-02-10 09:06:31 -05:00
|
|
|
end
|
|
|
|
|
2016-02-12 10:05:17 -05:00
|
|
|
def certificate_text
|
|
|
|
@certificate_text ||= x509.try(:to_text)
|
2016-02-10 09:06:31 -05:00
|
|
|
end
|
|
|
|
|
2018-02-06 08:25:46 -05: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
|
|
|
|
|
2016-02-10 10:45:59 -05:00
|
|
|
private
|
|
|
|
|
2018-02-06 08:25:46 -05:00
|
|
|
def set_verification_code
|
|
|
|
return if self.verification_code.present?
|
|
|
|
|
|
|
|
self.verification_code = SecureRandom.hex(16)
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ServiceClass
|
2017-10-20 02:33:52 -04:00
|
|
|
def update_daemon
|
2016-02-10 09:06:31 -05:00
|
|
|
::Projects::UpdatePagesConfigurationService.new(project).execute
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ServiceClass
|
2016-02-10 09:06:31 -05:00
|
|
|
|
2018-02-06 08:25:46 -05:00
|
|
|
def pages_config_changed?
|
|
|
|
project_id_changed? ||
|
|
|
|
domain_changed? ||
|
|
|
|
certificate_changed? ||
|
|
|
|
key_changed? ||
|
|
|
|
became_enabled? ||
|
|
|
|
became_disabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def became_enabled?
|
|
|
|
enabled_until.present? && !enabled_until_was.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def became_disabled?
|
|
|
|
!enabled_until.present? && enabled_until_was.present?
|
|
|
|
end
|
|
|
|
|
2016-02-10 09:06:31 -05: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 06:07:46 -05:00
|
|
|
end
|
2016-02-12 10:05:17 -05:00
|
|
|
|
|
|
|
def validate_pages_domain
|
|
|
|
return unless domain
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2017-05-19 10:07:38 -04:00
|
|
|
if domain.downcase.ends_with?(Settings.pages.host.downcase)
|
2016-02-12 10:05:17 -05:00
|
|
|
self.errors.add(:domain, "*.#{Settings.pages.host} is restricted")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def x509
|
|
|
|
return unless certificate
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2016-02-12 10:05:17 -05:00
|
|
|
@x509 ||= OpenSSL::X509::Certificate.new(certificate)
|
|
|
|
rescue OpenSSL::X509::CertificateError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def pkey
|
|
|
|
return unless key
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2016-02-12 10:05:17 -05:00
|
|
|
@pkey ||= OpenSSL::PKey::RSA.new(key)
|
|
|
|
rescue OpenSSL::PKey::PKeyError, OpenSSL::Cipher::CipherError
|
|
|
|
nil
|
|
|
|
end
|
2016-02-10 06:07:46 -05:00
|
|
|
end
|