Commit 983dcba3 authored by Patrick Bajao's avatar Patrick Bajao

Handle cases when authorized_keys doesn't exist

Modify Gitlab::AuthorizedKeys#rm_key and #list_key_ids to
handle cases when an `Errno::ENOENT` is raised.

Also added test cases in spec to ensure each method will
still work when authorized_keys doesn't exist.
parent dd43abec
......@@ -68,6 +68,8 @@ module Gitlab
end
true
rescue Errno::ENOENT
false
end
# Clear the authorized_keys file
......@@ -96,6 +98,8 @@ module Gitlab
end
end
end
rescue Errno::ENOENT
[]
end
private
......
......@@ -7,9 +7,17 @@ describe Gitlab::AuthorizedKeys do
subject { described_class.new(logger) }
after do
delete_authorized_keys_file
end
describe '#add_key' do
it "adds a line at the end of the file and strips trailing garbage" do
context 'authorized_keys file exists' do
before do
create_authorized_keys_fixture
end
it "adds a line at the end of the file and strips trailing garbage" do
auth_line = "command=\"#{Gitlab.config.gitlab_shell.path}/bin/gitlab-shell key-741\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaDAxx2E"
expect(logger).to receive(:info).with('Adding key (key-741): ssh-rsa AAAAB3NzaDAxx2E')
......@@ -19,6 +27,14 @@ describe Gitlab::AuthorizedKeys do
end
end
context 'authorized_keys file does not exist' do
it 'creates the file' do
expect(subject.add_key('key-741', 'ssh-rsa AAAAB3NzaDAxx2E')).to be_truthy
expect(File.exist?(tmp_authorized_keys_path)).to be_truthy
end
end
end
describe '#batch_add_keys' do
let(:keys) do
[
......@@ -27,6 +43,7 @@ describe Gitlab::AuthorizedKeys do
]
end
context 'authorized_keys file exists' do
before do
create_authorized_keys_fixture
end
......@@ -51,9 +68,21 @@ describe Gitlab::AuthorizedKeys do
end
end
context 'authorized_keys file does not exist' do
it 'creates the file' do
expect(subject.batch_add_keys(keys)).to be_truthy
expect(File.exist?(tmp_authorized_keys_path)).to be_truthy
end
end
end
describe '#rm_key' do
it "removes the right line" do
context 'authorized_keys file exists' do
before do
create_authorized_keys_fixture
end
it "removes the right line" do
other_line = "command=\"#{Gitlab.config.gitlab_shell.path}/bin/gitlab-shell key-742\",options ssh-rsa AAAAB3NzaDAxx2E"
delete_line = "command=\"#{Gitlab.config.gitlab_shell.path}/bin/gitlab-shell key-741\",options ssh-rsa AAAAB3NzaDAxx2E"
erased_line = delete_line.gsub(/./, '#')
......@@ -68,13 +97,33 @@ describe Gitlab::AuthorizedKeys do
end
end
context 'authorized_keys file does not exist' do
it 'returns false' do
expect(subject.rm_key('key-741')).to be_falsey
end
end
end
describe '#clear' do
it "should return true" do
context 'authorized_keys file exists' do
before do
create_authorized_keys_fixture
end
it "returns true" do
expect(subject.clear).to be_truthy
end
end
context 'authorized_keys file does not exist' do
it "still returns true" do
expect(subject.clear).to be_truthy
end
end
end
describe '#list_key_ids' do
context 'authorized_keys file exists' do
before do
create_authorized_keys_fixture(
existing_content:
......@@ -87,11 +136,22 @@ describe Gitlab::AuthorizedKeys do
end
end
context 'authorized_keys file does not exist' do
it 'returns an empty array' do
expect(subject.list_key_ids).to be_empty
end
end
end
def create_authorized_keys_fixture(existing_content: 'existing content')
FileUtils.mkdir_p(File.dirname(tmp_authorized_keys_path))
File.open(tmp_authorized_keys_path, 'w') { |file| file.puts(existing_content) }
end
def delete_authorized_keys_file
File.delete(tmp_authorized_keys_path) if File.exist?(tmp_authorized_keys_path)
end
def tmp_authorized_keys_path
Gitlab.config.gitlab_shell.authorized_keys_file
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