Class: PagesDomain

Inherits:
ApplicationRecord show all
Includes:
AfterCommitQueue, FromUnion, Presentable
Defined in:
app/models/pages_domain.rb

Constant Summary collapse

VERIFICATION_KEY =
'gitlab-pages-verification-code'
VERIFICATION_THRESHOLD =
3.days.freeze
SSL_RENEWAL_THRESHOLD =
30.days.freeze

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AfterCommitQueue

#run_after_commit, #run_after_commit_or_now

Methods included from Presentable

#present

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.find_by_domain_case_insensitive(domain) ⇒ Object


78
79
80
# File 'app/models/pages_domain.rb', line 78

def self.find_by_domain_case_insensitive(domain)
  find_by("LOWER(domain) = LOWER(?)", domain)
end

Instance Method Details

#certificate=(certificate) ⇒ Object


173
174
175
176
177
178
179
# File 'app/models/pages_domain.rb', line 173

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

#certificate_textObject


155
156
157
# File 'app/models/pages_domain.rb', line 155

def certificate_text
  @certificate_text ||= x509.try(:to_text)
end

#clear_auto_ssl_failureObject


215
216
217
# File 'app/models/pages_domain.rb', line 215

def clear_auto_ssl_failure
  self.auto_ssl_failed = false
end

#enabled?Boolean

Returns:

  • (Boolean)

90
91
92
# File 'app/models/pages_domain.rb', line 90

def enabled?
  !Gitlab::CurrentSettings.pages_domain_verification_enabled? || enabled_until.present?
end

#expirationObject


145
146
147
# File 'app/models/pages_domain.rb', line 145

def expiration
  x509&.not_after
end

#expired?Boolean

Returns:

  • (Boolean)

138
139
140
141
142
143
# File 'app/models/pages_domain.rb', line 138

def expired?
  return false unless x509

  current = Time.current
  current < x509.not_before || x509.not_after < current
end

#gitlab_provided_certificate=(certificate) ⇒ Object


199
200
201
202
# File 'app/models/pages_domain.rb', line 199

def gitlab_provided_certificate=(certificate)
  self.certificate = certificate
  self.certificate_source = 'gitlab_provided' if certificate_changed?
end

#gitlab_provided_key=(key) ⇒ Object


204
205
206
207
# File 'app/models/pages_domain.rb', line 204

def gitlab_provided_key=(key)
  self.key = key
  self.certificate_source = 'gitlab_provided' if attribute_changed?(:key)
end

#has_intermediates?Boolean

Returns:

  • (Boolean)

120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/pages_domain.rb', line 120

def has_intermediates?
  return false unless x509

  # self-signed certificates doesn't have the certificate chain
  return true if x509.verify(x509.public_key)

  store = OpenSSL::X509::Store.new
  store.set_default_paths

  store.verify(x509, untrusted_ca_certs_bundle)
rescue OpenSSL::X509::StoreError
  false
end

#has_matching_key?Boolean

Returns:

  • (Boolean)

112
113
114
115
116
117
118
# File 'app/models/pages_domain.rb', line 112

def has_matching_key?
  return false unless x509
  return false unless pkey

  # We compare the public key stored in certificate with public key from certificate key
  x509.check_private_key(pkey)
end

#https?Boolean

Returns:

  • (Boolean)

94
95
96
# File 'app/models/pages_domain.rb', line 94

def https?
  certificate.present?
end

#keyed_verification_codeObject


167
168
169
170
171
# File 'app/models/pages_domain.rb', line 167

def keyed_verification_code
  return unless verification_code.present?

  "#{VERIFICATION_KEY}=#{verification_code}"
end

#pages_virtual_domainObject


209
210
211
212
213
# File 'app/models/pages_domain.rb', line 209

def pages_virtual_domain
  return unless pages_deployed?

  Pages::VirtualDomain.new([project], domain: self)
end

#subjectObject


149
150
151
152
153
# File 'app/models/pages_domain.rb', line 149

def subject
  return unless x509

  x509.subject.to_s
end

#to_paramObject


98
99
100
# File 'app/models/pages_domain.rb', line 98

def to_param
  domain
end

#untrusted_ca_certs_bundleObject


134
135
136
# File 'app/models/pages_domain.rb', line 134

def untrusted_ca_certs_bundle
  ::Gitlab::X509::Certificate.load_ca_certs_bundle(certificate)
end

#unverified?Boolean

Returns:

  • (Boolean)

86
87
88
# File 'app/models/pages_domain.rb', line 86

def unverified?
  !verified?
end

#urlObject


102
103
104
105
106
107
108
109
110
# File 'app/models/pages_domain.rb', line 102

def url
  return unless domain

  if certificate.present?
    "https://#{domain}"
  else
    "http://#{domain}"
  end
end

#user_provided_certificateObject


190
191
192
# File 'app/models/pages_domain.rb', line 190

def user_provided_certificate
  certificate if certificate_user_provided?
end

#user_provided_certificate=(certificate) ⇒ Object


194
195
196
197
# File 'app/models/pages_domain.rb', line 194

def user_provided_certificate=(certificate)
  self.certificate = certificate
  self.certificate_source = 'user_provided' if certificate_changed?
end

#user_provided_keyObject


181
182
183
# File 'app/models/pages_domain.rb', line 181

def user_provided_key
  key if certificate_user_provided?
end

#user_provided_key=(key) ⇒ Object


185
186
187
188
# File 'app/models/pages_domain.rb', line 185

def user_provided_key=(key)
  self.key = key
  self.certificate_source = 'user_provided' if attribute_changed?(:key)
end

#verification_domainObject

Verification codes may be TXT records for domain or verification_domain, to support the use of CNAME records on domain.


161
162
163
164
165
# File 'app/models/pages_domain.rb', line 161

def verification_domain
  return unless domain.present?

  "_#{VERIFICATION_KEY}.#{domain}"
end

#verified?Boolean

Returns:

  • (Boolean)

82
83
84
# File 'app/models/pages_domain.rb', line 82

def verified?
  !!verified_at
end