diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-07 03:09:12 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-02-07 03:09:12 +0300 |
commit | 6168721025dd8e98caeb2bf6844273e6690eaf69 (patch) | |
tree | 8c4fb20d793669e488a739bc9951dab8b363eed4 /app/models/concerns/x509_serial_number_attribute.rb | |
parent | a89cb5cbdd832d4d9e80517973aceda6bc0a3856 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/models/concerns/x509_serial_number_attribute.rb')
-rw-r--r-- | app/models/concerns/x509_serial_number_attribute.rb | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/app/models/concerns/x509_serial_number_attribute.rb b/app/models/concerns/x509_serial_number_attribute.rb new file mode 100644 index 00000000000..d2a5c736604 --- /dev/null +++ b/app/models/concerns/x509_serial_number_attribute.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +module X509SerialNumberAttribute + extend ActiveSupport::Concern + + class_methods do + def x509_serial_number_attribute(name) + return if ENV['STATIC_VERIFICATION'] + + validate_binary_column_exists!(name) unless Rails.env.production? + + attribute(name, Gitlab::Database::X509SerialNumberAttribute.new) + end + + # This only gets executed in non-production environments as an additional check to ensure + # the column is the correct type. In production it should behave like any other attribute. + # See https://gitlab.com/gitlab-org/gitlab/merge_requests/5502 for more discussion + def validate_binary_column_exists!(name) + return unless database_exists? + + unless table_exists? + warn "WARNING: x509_serial_number_attribute #{name.inspect} is invalid since the table doesn't exist - you may need to run database migrations" + return + end + + column = columns.find { |c| c.name == name.to_s } + + unless column + warn "WARNING: x509_serial_number_attribute #{name.inspect} is invalid since the column doesn't exist - you may need to run database migrations" + return + end + + unless column.type == :binary + raise ArgumentError.new("x509_serial_number_attribute #{name.inspect} is invalid since the column type is not :binary") + end + rescue => error + Gitlab::AppLogger.error "X509SerialNumberAttribute initialization: #{error.message}" + raise + end + + def database_exists? + Gitlab::Database.exists? + end + end +end |