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

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

at_most, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, underscore, without_order

Class Method Details

.find_by_domain_case_insensitive(domain) ⇒ Object


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

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

Instance Method Details

#certificate=(certificate) ⇒ Object


177
178
179
180
181
182
183
# File 'app/models/pages_domain.rb', line 177

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


159
160
161
# File 'app/models/pages_domain.rb', line 159

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

#clear_auto_ssl_failureObject


219
220
221
# File 'app/models/pages_domain.rb', line 219

def clear_auto_ssl_failure
  self.auto_ssl_failed = false
end

#enabled?Boolean

Returns:

  • (Boolean)

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

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

#expirationObject


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

def expiration
  x509&.not_after
end

#expired?Boolean

Returns:

  • (Boolean)

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

def expired?
  return false unless x509

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

#gitlab_provided_certificate=(certificate) ⇒ Object


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

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

#gitlab_provided_key=(key) ⇒ Object


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

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

#has_intermediates?Boolean

Returns:

  • (Boolean)

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/models/pages_domain.rb', line 121

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

  # 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

#has_matching_key?Boolean

Returns:

  • (Boolean)

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

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)

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

def https?
  certificate.present?
end

#keyed_verification_codeObject


171
172
173
174
175
# File 'app/models/pages_domain.rb', line 171

def keyed_verification_code
  return unless verification_code.present?

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

#pages_virtual_domainObject


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

def pages_virtual_domain
  return unless pages_deployed?

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

#subjectObject


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

def subject
  return unless x509

  x509.subject.to_s
end

#to_paramObject


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

def to_param
  domain
end

#unverified?Boolean

Returns:

  • (Boolean)

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

def unverified?
  !verified?
end

#urlObject


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

def url
  return unless domain

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

#user_provided_certificateObject


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

def user_provided_certificate
  certificate if certificate_user_provided?
end

#user_provided_certificate=(certificate) ⇒ Object


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

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

#user_provided_keyObject


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

def user_provided_key
  key if certificate_user_provided?
end

#user_provided_key=(key) ⇒ Object


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

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

#verification_domainObject

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


165
166
167
168
169
# File 'app/models/pages_domain.rb', line 165

def verification_domain
  return unless domain.present?

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

#verified?Boolean

Returns:

  • (Boolean)

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

def verified?
  !!verified_at
end