Commit 1025d6ac authored by DJ Mountney's avatar DJ Mountney

Apply review feedback

- Autogenerate the encrypted key but only when requested
- Generate the new key as part of the secret token initializer
- Add additional config check during ldap secrets edit
- Ensure that the provided input is valid yaml, and is a hash
parent 187618c5
...@@ -8,6 +8,7 @@ module Gitlab ...@@ -8,6 +8,7 @@ module Gitlab
encrypted = Gitlab::Auth::Ldap::Config.encrypted_secrets encrypted = Gitlab::Auth::Ldap::Config.encrypted_secrets
return unless validate_config(encrypted) return unless validate_config(encrypted)
validate_contents(contents)
encrypted.write(contents) encrypted.write(contents)
puts "File encrypted and saved." puts "File encrypted and saved."
...@@ -27,8 +28,10 @@ module Gitlab ...@@ -27,8 +28,10 @@ module Gitlab
encrypted.change do |contents| encrypted.change do |contents|
contents = encrypted_file_template unless File.exist?(encrypted.content_path) contents = encrypted_file_template unless File.exist?(encrypted.content_path)
File.write(temp_file.path, contents) File.write(temp_file.path, contents)
system("#{editor} #{temp_file.path}") system(editor, temp_file.path)
File.read(temp_file.path) changes = File.read(temp_file.path)
validate_contents(changes)
changes
end end
puts "File encrypted and saved." puts "File encrypted and saved."
...@@ -67,6 +70,12 @@ module Gitlab ...@@ -67,6 +70,12 @@ module Gitlab
true true
end end
def validate_contents(contents)
config = YAML.safe_load(contents, permitted_classes: [Symbol])
puts "WARNING: Content was not a valid LDAP secret yml file." if config.nil? || !config.is_a?(Hash)
contents
end
def encrypted_file_template def encrypted_file_template
<<~YAML <<~YAML
# main: # main:
......
...@@ -77,6 +77,13 @@ RSpec.describe 'gitlab:ldap:secret rake tasks' do ...@@ -77,6 +77,13 @@ RSpec.describe 'gitlab:ldap:secret rake tasks' do
FileUtils.rm_rf(Rails.root.join('tmp/tests/ldapenc')) FileUtils.rm_rf(Rails.root.join('tmp/tests/ldapenc'))
expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/Directory .* does not exist./).to_stdout expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/Directory .* does not exist./).to_stdout
end end
it 'shows a warning when content is invalid' do
Settings.encrypted(ldap_secret_file).write('somevalue')
expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/WARNING: Content was not a valid LDAP secret yml file/).to_stdout
value = Settings.encrypted(ldap_secret_file)
expect(value.read).to match(/somevalue/)
end
end end
describe 'write' do describe 'write' do
...@@ -101,5 +108,12 @@ RSpec.describe 'gitlab:ldap:secret rake tasks' do ...@@ -101,5 +108,12 @@ RSpec.describe 'gitlab:ldap:secret rake tasks' do
FileUtils.rm_rf('tmp/tests/ldapenc/') FileUtils.rm_rf('tmp/tests/ldapenc/')
expect { run_rake_task('gitlab:ldap:secret:write') }.to output(/Directory .* does not exist./).to_stdout expect { run_rake_task('gitlab:ldap:secret:write') }.to output(/Directory .* does not exist./).to_stdout
end end
it 'shows a warning when content is invalid' do
Settings.encrypted(ldap_secret_file).write('somevalue')
expect { run_rake_task('gitlab:ldap:secret:edit') }.to output(/WARNING: Content was not a valid LDAP secret yml file/).to_stdout
value = Settings.encrypted(ldap_secret_file)
expect(value.read).to match(/somevalue/)
end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment