-
Notifications
You must be signed in to change notification settings - Fork 983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #13870 - encrypts specific settings values in db #3230
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
module EncryptValue | ||
ENCRYPTION_PREFIX = "encrypted-" | ||
def matches_prefix?(str) | ||
ENCRYPTION_PREFIX == str.to_s[0..(ENCRYPTION_PREFIX.length - 1)] | ||
end | ||
|
||
def encryption_key | ||
return ENV['ENCRYPTION_KEY'] if ENV['ENCRYPTION_KEY'].present? | ||
return EncryptionKey::ENCRYPTION_KEY if defined? EncryptionKey::ENCRYPTION_KEY | ||
nil | ||
end | ||
|
||
def is_encryptable?(str) | ||
if !encryption_key.present? | ||
puts_and_logs "Missing ENCRYPTION_KEY configuration, so #{self.class.name} #{name} could not be encrypted", Logger::WARN | ||
false | ||
elsif str.blank? | ||
puts_and_logs "String is blank', so #{self.class.name} #{name} was not encrypted", Logger::DEBUG | ||
false | ||
elsif matches_prefix?(str) | ||
puts_and_logs "String starts with the prefix '#{ENCRYPTION_PREFIX}', so #{self.class.name} #{name} was not encrypted again", Logger::DEBUG | ||
false | ||
else | ||
true | ||
end | ||
end | ||
|
||
def is_decryptable?(str) | ||
if !matches_prefix?(str) | ||
puts_and_logs "String does not start with the prefix '#{ENCRYPTION_PREFIX}', so #{self.class.name} #{name} was not decrypted", Logger::DEBUG | ||
false | ||
elsif !encryption_key.present? | ||
puts_and_logs "Missing ENCRYPTION_KEY configuration, so #{self.class.name} #{name} could not be decrypted", Logger::WARN | ||
false | ||
else | ||
true | ||
end | ||
end | ||
|
||
def encrypt_field(str) | ||
return str unless is_encryptable?(str) | ||
encryptor = ActiveSupport::MessageEncryptor.new(encryption_key) | ||
begin | ||
# add prefix to encrypted string | ||
str_encrypted = "#{ENCRYPTION_PREFIX}#{encryptor.encrypt_and_sign(str)}" | ||
puts_and_logs "Successfully encrypted field for #{self.class.name} #{name}" | ||
str = str_encrypted | ||
rescue | ||
puts_and_logs "WARNING: Encryption failed for string. Please check that the ENCRYPTION_KEY has not changed.", Logger::WARN | ||
end | ||
str | ||
end | ||
|
||
def decrypt_field(str) | ||
return str unless is_decryptable?(str) | ||
encryptor = ActiveSupport::MessageEncryptor.new(encryption_key) | ||
begin | ||
# remove prefix before decrypting string | ||
str_no_prefix = str.gsub(/^#{ENCRYPTION_PREFIX}/, "") | ||
str_decrypted = encryptor.decrypt_and_verify(str_no_prefix) | ||
puts_and_logs "Successfully decrypted field for #{self.class.name} #{name}" | ||
str = str_decrypted | ||
rescue ActiveSupport::MessageVerifier::InvalidSignature | ||
puts_and_logs "WARNING: Decryption failed for string. Please check that the ENCRYPTION_KEY has not changed.", Logger::WARN | ||
end | ||
str | ||
end | ||
|
||
private | ||
|
||
def puts_and_logs(msg, level = Logger::INFO) | ||
logger.add level, msg | ||
puts msg if Foreman.in_rake? && !Rails.env.test? && level >= Logger::INFO | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ class Setting < ActiveRecord::Base | |
extend FriendlyId | ||
friendly_id :name | ||
include ActiveModel::Validations | ||
include EncryptValue | ||
self.inheritance_column = 'category' | ||
|
||
TYPES= %w{ integer boolean hash array string } | ||
|
@@ -25,7 +26,7 @@ def validate(record) | |
|
||
validates_lengths_from_database | ||
# audit the changes to this model | ||
audited :except => [:name, :description, :category, :settings_type, :full_name], :on => [:update] | ||
audited :except => [:name, :description, :category, :settings_type, :full_name, :encrypted], :on => [:update] | ||
|
||
validates :name, :presence => true, :uniqueness => true | ||
validates :description, :presence => true | ||
|
@@ -107,11 +108,14 @@ def self.method_missing(method, *args) | |
|
||
def value=(v) | ||
v = v.to_yaml unless v.nil? | ||
# the has_attribute is for enabling DB migrations on older versions | ||
v = encrypt_field(v) if has_attribute?(:encrypted) && encrypted | ||
write_attribute :value, v | ||
end | ||
|
||
def value | ||
v = read_attribute(:value) | ||
v = decrypt_field(v) | ||
v.nil? ? default : YAML.load(v) | ||
end | ||
alias_method :value_before_type_cast, :value | ||
|
@@ -201,12 +205,19 @@ def self.convert_array_to_regexp(array) | |
|
||
def self.create_existing(s, opts) | ||
bypass_readonly(s) do | ||
attrs = column_check([:default, :description, :full_name]) | ||
attrs = column_check([:default, :description, :full_name, :encrypted]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're not mass-assigning :encrypted on line 215 then it perhaps shouldn't be listed here. |
||
to_update = Hash[opts.select { |k,v| attrs.include? k }] | ||
to_update.merge!(:value => SETTINGS[opts[:name].to_sym]) if SETTINGS.key?(opts[:name].to_sym) | ||
s.update_attributes(to_update) | ||
s.update_column :category, opts[:category] if s.category != opts[:category] | ||
s.update_column :full_name, opts[:full_name] if !column_check([:full_name]).empty? | ||
raw_value = s.read_attribute(:value) | ||
if s.is_encryptable?(raw_value) && attrs.include?(:encrypted) && opts[:encrypted] | ||
s.update_column :value, s.encrypt_field(raw_value) | ||
end | ||
if s.is_decryptable?(raw_value) && attrs.include?(:encrypted) && !opts[:encrypted] | ||
s.update_column :value, s.decrypt_field(raw_value) | ||
end | ||
end | ||
s | ||
end | ||
|
@@ -237,7 +248,8 @@ def self.set(name, description, default, full_name = nil, value = nil, options = | |
define_method("#{name}_collection".to_sym){ options[:collection].call } | ||
end | ||
end | ||
{:name => name, :value => value, :description => description, :default => default, :full_name => full_name} | ||
options[:encrypted] ||= false | ||
{:name => name, :value => value, :description => description, :default => default, :full_name => full_name, :encrypted => options[:encrypted]} | ||
end | ||
|
||
def self.model_name | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddEncryptedToSettings < ActiveRecord::Migration | ||
def change | ||
add_column :settings, :encrypted, :boolean, :null => false, :default => false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does
[encrypted]
get translated in the UI? My last comment here suggested doing it in a UI helper, but I can't see that in this PR. (See #3230 (comment))There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just forgot to commit the changes in
audits_helper
.