Commit bef05ab0 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'ee-fix-gitlab-keys-changes' into 'master'

Fix broken EE master

Closes gitlab-ce#59170

See merge request gitlab-org/gitlab-ee!10287
parents 8288bb20 0c891977
...@@ -32,10 +32,8 @@ module Gitlab ...@@ -32,10 +32,8 @@ module Gitlab
def batch_add_keys_in_db_starting_from(start_id) def batch_add_keys_in_db_starting_from(start_id)
Rails.logger.info("Adding all keys starting from ID: #{start_id}") Rails.logger.info("Adding all keys starting from ID: #{start_id}")
gitlab_shell.batch_add_keys do |adder| ::Key.find_in_batches(start: start_id, batch_size: 1000) do |keys|
Key.find_each(start: start_id, batch_size: 1000) do |key| gitlab_shell.batch_add_keys(keys)
adder.add_key(key.shell_id, key.key)
end
end end
end end
end end
......
...@@ -80,15 +80,19 @@ describe Gitlab::BackgroundMigration::UpdateAuthorizedKeysFileSince do ...@@ -80,15 +80,19 @@ describe Gitlab::BackgroundMigration::UpdateAuthorizedKeysFileSince do
background_migration.batch_add_keys_in_db_starting_from(@keys[3].id) background_migration.batch_add_keys_in_db_starting_from(@keys[3].id)
file = File.read(Rails.root.join('tmp/tests/.ssh/authorized_keys')) file = File.read(Rails.root.join(Gitlab.config.gitlab_shell.authorized_keys_file))
expect(file.scan(/ssh-rsa/).count).to eq(7) expect(file.scan(/ssh-rsa/).count).to eq(7)
expect(file).not_to include(Gitlab::Shell.strip_key(@keys[0].key)) expect(file).not_to include(strip_key(@keys[0].key))
expect(file).not_to include(Gitlab::Shell.strip_key(@keys[2].key)) expect(file).not_to include(strip_key(@keys[2].key))
expect(file).to include(Gitlab::Shell.strip_key(@keys[3].key)) expect(file).to include(strip_key(@keys[3].key))
expect(file).to include(Gitlab::Shell.strip_key(@keys[9].key)) expect(file).to include(strip_key(@keys[9].key))
end end
end end
def strip_key(key)
key.split(/[ ]+/)[0, 2].join(' ')
end
end end
end end
# rubocop:enable RSpec/FactoriesInMigrationSpecs # rubocop:enable RSpec/FactoriesInMigrationSpecs
...@@ -3,6 +3,7 @@ require Rails.root.join('ee', 'db', 'migrate', '20170626202753_update_authorized ...@@ -3,6 +3,7 @@ require Rails.root.join('ee', 'db', 'migrate', '20170626202753_update_authorized
describe UpdateAuthorizedKeysFile, :migration do describe UpdateAuthorizedKeysFile, :migration do
let(:migration) { described_class.new } let(:migration) { described_class.new }
let(:authorized_keys_file) { Rails.root.join(Gitlab.config.gitlab_shell.authorized_keys_file) }
describe '#up' do describe '#up' do
context 'when authorized_keys_enabled is nil' do context 'when authorized_keys_enabled is nil' do
...@@ -15,6 +16,8 @@ describe UpdateAuthorizedKeysFile, :migration do ...@@ -15,6 +16,8 @@ describe UpdateAuthorizedKeysFile, :migration do
end end
it 'sets authorized_keys_enabled to true' do it 'sets authorized_keys_enabled to true' do
FileUtils.touch(authorized_keys_file)
migration.up migration.up
expect(described_class::ApplicationSetting.last.authorized_keys_enabled).to be_truthy expect(described_class::ApplicationSetting.last.authorized_keys_enabled).to be_truthy
...@@ -39,13 +42,14 @@ describe UpdateAuthorizedKeysFile, :migration do ...@@ -39,13 +42,14 @@ describe UpdateAuthorizedKeysFile, :migration do
migration.up migration.up
file = File.read(Rails.root.join('tmp/tests/.ssh/authorized_keys')) file = File.read(authorized_keys_file)
expect(file.scan(/ssh-rsa/).count).to eq(2) expect(file.scan(/ssh-rsa/).count).to eq(2)
expect(file).not_to include(Gitlab::Shell.strip_key(@keys[0].key)) expect(file).not_to include(strip_key(@keys[0].key))
expect(file).not_to include(Gitlab::Shell.strip_key(@keys[1].key)) expect(file).not_to include(strip_key(@keys[1].key))
expect(file).to include(Gitlab::Shell.strip_key(@keys[2].key)) expect(file).to include(strip_key(@keys[2].key))
expect(file).to include(Gitlab::Shell.strip_key(@keys[3].key)) expect(file).to include(strip_key(@keys[3].key))
end end
end end
...@@ -57,18 +61,24 @@ describe UpdateAuthorizedKeysFile, :migration do ...@@ -57,18 +61,24 @@ describe UpdateAuthorizedKeysFile, :migration do
end end
it 'deletes the SSH key from authorized_keys' do it 'deletes the SSH key from authorized_keys' do
file = File.read(Rails.root.join('tmp/tests/.ssh/authorized_keys')) file = File.read(authorized_keys_file)
expect(file).to include(Gitlab::Shell.strip_key(@key_to_stay.key))
expect(file).to include(Gitlab::Shell.strip_key(@key_to_delete.key)) expect(file).to include(strip_key(@key_to_stay.key))
expect(file).to include(strip_key(@key_to_delete.key))
migration.up migration.up
file = File.read(Rails.root.join('tmp/tests/.ssh/authorized_keys')) file = File.read(authorized_keys_file)
expect(file).to include(Gitlab::Shell.strip_key(@key_to_stay.key))
expect(file).not_to include(Gitlab::Shell.strip_key(@key_to_delete.key)) expect(file).to include(strip_key(@key_to_stay.key))
expect(file).not_to include(strip_key(@key_to_delete.key))
end end
end end
end end
def strip_key(key)
key.split(/[ ]+/)[0, 2].join(' ')
end
end end
describe '#authorized_keys_file_in_use_and_stale?' do describe '#authorized_keys_file_in_use_and_stale?' do
......
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