2019-03-19 07:16:21 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe Gitlab::AuthorizedKeys do
|
2019-03-19 07:16:21 -04:00
|
|
|
let(:logger) { double('logger').as_null_object }
|
|
|
|
|
2019-08-29 00:21:59 -04:00
|
|
|
subject(:authorized_keys) { described_class.new(logger) }
|
2019-03-19 07:16:21 -04:00
|
|
|
|
2019-08-27 00:33:48 -04:00
|
|
|
describe '#accessible?' do
|
2019-08-29 00:21:59 -04:00
|
|
|
subject { authorized_keys.accessible? }
|
|
|
|
|
2019-08-27 00:33:48 -04:00
|
|
|
context 'authorized_keys file exists' do
|
|
|
|
before do
|
|
|
|
create_authorized_keys_fixture
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
delete_authorized_keys_file
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'can open file' do
|
2019-08-29 00:21:59 -04:00
|
|
|
it { is_expected.to be_truthy }
|
2019-08-27 00:33:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'cannot open file' do
|
|
|
|
before do
|
|
|
|
allow(File).to receive(:open).and_raise(Errno::EACCES)
|
|
|
|
end
|
|
|
|
|
2019-08-29 00:21:59 -04:00
|
|
|
it { is_expected.to be_falsey }
|
2019-08-27 00:33:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized_keys file does not exist' do
|
2019-08-29 00:21:59 -04:00
|
|
|
it { is_expected.to be_falsey }
|
2019-08-27 00:33:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-08-29 04:28:22 -04:00
|
|
|
describe '#create' do
|
2022-01-31 13:18:10 -05:00
|
|
|
subject { authorized_keys.create } # rubocop:disable Rails/SaveBang
|
2019-08-29 04:28:22 -04:00
|
|
|
|
|
|
|
context 'authorized_keys file exists' do
|
|
|
|
before do
|
|
|
|
create_authorized_keys_fixture
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
delete_authorized_keys_file
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_truthy }
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized_keys file does not exist' do
|
|
|
|
after do
|
|
|
|
delete_authorized_keys_file
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates authorized_keys file' do
|
|
|
|
expect(subject).to be_truthy
|
|
|
|
expect(File.exist?(tmp_authorized_keys_path)).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'cannot create file' do
|
|
|
|
before do
|
|
|
|
allow(File).to receive(:open).and_raise(Errno::EACCES)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.to be_falsey }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-19 07:16:21 -04:00
|
|
|
describe '#add_key' do
|
2019-08-29 00:21:59 -04:00
|
|
|
let(:id) { 'key-741' }
|
|
|
|
|
|
|
|
subject { authorized_keys.add_key(id, key) }
|
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
context 'authorized_keys file exists' do
|
2019-08-29 00:21:59 -04:00
|
|
|
let(:key) { 'ssh-rsa AAAAB3NzaDAxx2E trailing garbage' }
|
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
before do
|
|
|
|
create_authorized_keys_fixture
|
|
|
|
end
|
|
|
|
|
2019-03-21 00:42:28 -04:00
|
|
|
after do
|
|
|
|
delete_authorized_keys_file
|
|
|
|
end
|
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
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')
|
2019-08-29 00:21:59 -04:00
|
|
|
expect(subject).to be_truthy
|
2019-03-19 20:07:22 -04:00
|
|
|
expect(File.read(tmp_authorized_keys_path)).to eq("existing content\n#{auth_line}\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized_keys file does not exist' do
|
2019-08-29 00:21:59 -04:00
|
|
|
let(:key) { 'ssh-rsa AAAAB3NzaDAxx2E' }
|
|
|
|
|
2019-03-21 00:42:28 -04:00
|
|
|
before do
|
|
|
|
delete_authorized_keys_file
|
|
|
|
end
|
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
it 'creates the file' do
|
2019-08-29 00:21:59 -04:00
|
|
|
expect(subject).to be_truthy
|
2019-03-19 20:07:22 -04:00
|
|
|
expect(File.exist?(tmp_authorized_keys_path)).to be_truthy
|
|
|
|
end
|
2019-03-19 07:16:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#batch_add_keys' do
|
|
|
|
let(:keys) do
|
|
|
|
[
|
|
|
|
double(shell_id: 'key-12', key: 'ssh-dsa ASDFASGADG trailing garbage'),
|
|
|
|
double(shell_id: 'key-123', key: 'ssh-rsa GFDGDFSGSDFG')
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2019-08-29 00:21:59 -04:00
|
|
|
subject { authorized_keys.batch_add_keys(keys) }
|
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
context 'authorized_keys file exists' do
|
|
|
|
before do
|
|
|
|
create_authorized_keys_fixture
|
|
|
|
end
|
2019-03-19 07:16:21 -04:00
|
|
|
|
2019-03-21 00:42:28 -04:00
|
|
|
after do
|
|
|
|
delete_authorized_keys_file
|
|
|
|
end
|
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
it "adds lines at the end of the file" do
|
|
|
|
auth_line1 = "command=\"#{Gitlab.config.gitlab_shell.path}/bin/gitlab-shell key-12\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-dsa ASDFASGADG"
|
|
|
|
auth_line2 = "command=\"#{Gitlab.config.gitlab_shell.path}/bin/gitlab-shell key-123\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa GFDGDFSGSDFG"
|
2019-03-19 07:16:21 -04:00
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
expect(logger).to receive(:info).with('Adding key (key-12): ssh-dsa ASDFASGADG')
|
|
|
|
expect(logger).to receive(:info).with('Adding key (key-123): ssh-rsa GFDGDFSGSDFG')
|
2019-08-29 00:21:59 -04:00
|
|
|
expect(subject).to be_truthy
|
2019-03-19 20:07:22 -04:00
|
|
|
expect(File.read(tmp_authorized_keys_path)).to eq("existing content\n#{auth_line1}\n#{auth_line2}\n")
|
|
|
|
end
|
2019-03-19 07:16:21 -04:00
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
context "invalid key" do
|
|
|
|
let(:keys) { [double(shell_id: 'key-123', key: "ssh-rsa A\tSDFA\nSGADG")] }
|
2019-03-19 07:16:21 -04:00
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
it "doesn't add keys" do
|
2019-08-29 00:21:59 -04:00
|
|
|
expect(subject).to be_falsey
|
2019-03-19 20:07:22 -04:00
|
|
|
expect(File.read(tmp_authorized_keys_path)).to eq("existing content\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized_keys file does not exist' do
|
2019-03-21 00:42:28 -04:00
|
|
|
before do
|
|
|
|
delete_authorized_keys_file
|
|
|
|
end
|
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
it 'creates the file' do
|
2019-08-29 00:21:59 -04:00
|
|
|
expect(subject).to be_truthy
|
2019-03-19 20:07:22 -04:00
|
|
|
expect(File.exist?(tmp_authorized_keys_path)).to be_truthy
|
2019-03-19 07:16:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-12 08:09:17 -04:00
|
|
|
describe '#remove_key' do
|
2019-08-29 00:21:59 -04:00
|
|
|
let(:key) { 'key-741' }
|
|
|
|
|
2020-03-12 08:09:17 -04:00
|
|
|
subject { authorized_keys.remove_key(key) }
|
2019-08-29 00:21:59 -04:00
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
context 'authorized_keys file exists' do
|
2019-08-29 00:21:59 -04:00
|
|
|
let(:other_line) { "command=\"#{Gitlab.config.gitlab_shell.path}/bin/gitlab-shell key-742\",options ssh-rsa AAAAB3NzaDAxx2E" }
|
|
|
|
let(:delete_line) { "command=\"#{Gitlab.config.gitlab_shell.path}/bin/gitlab-shell key-741\",options ssh-rsa AAAAB3NzaDAxx2E" }
|
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
before do
|
|
|
|
create_authorized_keys_fixture
|
2019-08-29 00:21:59 -04:00
|
|
|
|
|
|
|
File.open(tmp_authorized_keys_path, 'a') do |auth_file|
|
|
|
|
auth_file.puts delete_line
|
|
|
|
auth_file.puts other_line
|
|
|
|
end
|
2019-03-19 20:07:22 -04:00
|
|
|
end
|
|
|
|
|
2019-03-21 00:42:28 -04:00
|
|
|
after do
|
|
|
|
delete_authorized_keys_file
|
|
|
|
end
|
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
it "removes the right line" do
|
|
|
|
erased_line = delete_line.gsub(/./, '#')
|
|
|
|
|
|
|
|
expect(logger).to receive(:info).with('Removing key (key-741)')
|
2019-08-29 00:21:59 -04:00
|
|
|
expect(subject).to be_truthy
|
2019-03-19 20:07:22 -04:00
|
|
|
expect(File.read(tmp_authorized_keys_path)).to eq("existing content\n#{erased_line}\n#{other_line}\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized_keys file does not exist' do
|
2019-03-21 00:42:28 -04:00
|
|
|
before do
|
|
|
|
delete_authorized_keys_file
|
|
|
|
end
|
|
|
|
|
2019-08-29 00:21:59 -04:00
|
|
|
it { is_expected.to be_falsey }
|
2019-03-19 07:16:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#clear' do
|
2019-08-29 00:21:59 -04:00
|
|
|
subject { authorized_keys.clear }
|
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
context 'authorized_keys file exists' do
|
|
|
|
before do
|
|
|
|
create_authorized_keys_fixture
|
|
|
|
end
|
|
|
|
|
2019-03-21 00:42:28 -04:00
|
|
|
after do
|
|
|
|
delete_authorized_keys_file
|
|
|
|
end
|
|
|
|
|
2019-08-29 00:21:59 -04:00
|
|
|
it { is_expected.to be_truthy }
|
2019-03-19 20:07:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'authorized_keys file does not exist' do
|
2019-03-21 00:42:28 -04:00
|
|
|
before do
|
|
|
|
delete_authorized_keys_file
|
|
|
|
end
|
|
|
|
|
2019-08-29 00:21:59 -04:00
|
|
|
it { is_expected.to be_truthy }
|
2019-03-19 07:16:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#list_key_ids' do
|
2019-08-29 00:21:59 -04:00
|
|
|
subject { authorized_keys.list_key_ids }
|
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
context 'authorized_keys file exists' do
|
|
|
|
before do
|
|
|
|
create_authorized_keys_fixture(
|
|
|
|
existing_content:
|
|
|
|
"key-1\tssh-dsa AAA\nkey-2\tssh-rsa BBB\nkey-3\tssh-rsa CCC\nkey-9000\tssh-rsa DDD\n"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-03-21 00:42:28 -04:00
|
|
|
after do
|
|
|
|
delete_authorized_keys_file
|
|
|
|
end
|
|
|
|
|
2019-08-29 00:21:59 -04:00
|
|
|
it { is_expected.to eq([1, 2, 3, 9000]) }
|
2019-03-19 07:16:21 -04:00
|
|
|
end
|
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
context 'authorized_keys file does not exist' do
|
2019-03-21 00:42:28 -04:00
|
|
|
before do
|
|
|
|
delete_authorized_keys_file
|
|
|
|
end
|
|
|
|
|
2019-08-29 00:21:59 -04:00
|
|
|
it { is_expected.to be_empty }
|
2019-03-19 07:16:21 -04:00
|
|
|
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
|
|
|
|
|
2019-03-19 20:07:22 -04:00
|
|
|
def delete_authorized_keys_file
|
|
|
|
File.delete(tmp_authorized_keys_path) if File.exist?(tmp_authorized_keys_path)
|
|
|
|
end
|
|
|
|
|
2019-03-19 07:16:21 -04:00
|
|
|
def tmp_authorized_keys_path
|
|
|
|
Gitlab.config.gitlab_shell.authorized_keys_file
|
|
|
|
end
|
|
|
|
end
|