summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Kozono <mkozono@gmail.com>2017-06-27 16:47:05 -0700
committerMichael Kozono <mkozono@gmail.com>2017-06-28 07:32:48 -0700
commitf980d8f59b172122d4985923094ea3b8342d6da2 (patch)
tree4a3457c9895f0e58849dd48e87655d5a595301a6
parent7e41e378ab71b326f28d62195faeca5c54aafdb8 (diff)
downloadgitlab-shell-f980d8f59b172122d4985923094ea3b8342d6da2.tar.gz
Add list-key-ids command
-rw-r--r--lib/gitlab_keys.rb13
-rw-r--r--spec/gitlab_keys_spec.rb30
2 files changed, 41 insertions, 2 deletions
diff --git a/lib/gitlab_keys.rb b/lib/gitlab_keys.rb
index 46189ae..975ee3e 100644
--- a/lib/gitlab_keys.rb
+++ b/lib/gitlab_keys.rb
@@ -39,6 +39,8 @@ class GitlabKeys
rm_key
when 'list-keys';
list_keys
+ when 'list-key-ids';
+ list_key_ids
when 'clear';
clear
when 'check-permissions';
@@ -75,6 +77,17 @@ class GitlabKeys
keys
end
+ def list_key_ids
+ $logger.info 'Listing all key IDs'
+ open_auth_file('r') do |f|
+ f.each_line do |line|
+ matchd = line.match(/key-(\d+)/)
+ next unless matchd
+ puts matchd[1]
+ end
+ end
+ end
+
def batch_add_keys
lock(300) do # Allow 300 seconds (5 minutes) for batch_add_keys
open_auth_file('a') do |file|
diff --git a/spec/gitlab_keys_spec.rb b/spec/gitlab_keys_spec.rb
index c1c3854..92d5909 100644
--- a/spec/gitlab_keys_spec.rb
+++ b/spec/gitlab_keys_spec.rb
@@ -80,6 +80,23 @@ describe GitlabKeys do
end
end
+ describe :list_key_ids do
+ let(:gitlab_keys) { build_gitlab_keys('list-key-ids') }
+ 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
+
+ it 'outputs the key IDs, separated by newlines' do
+ output = capture_stdout do
+ gitlab_keys.send(:list_key_ids)
+ end
+ output.should match "1\n2\n3\n9000"
+ end
+ end
+
describe :batch_add_keys do
let(:gitlab_keys) { build_gitlab_keys('batch-add-keys') }
let(:fake_stdin) { StringIO.new("key-12\tssh-dsa ASDFASGADG\nkey-123\tssh-rsa GFDGDFSGSDFG\n", 'r') }
@@ -288,9 +305,9 @@ describe GitlabKeys do
end
end
- def create_authorized_keys_fixture
+ def create_authorized_keys_fixture(existing_content: 'existing content')
FileUtils.mkdir_p(File.dirname(tmp_authorized_keys_path))
- open(tmp_authorized_keys_path, 'w') { |file| file.puts('existing content') }
+ open(tmp_authorized_keys_path, 'w') { |file| file.puts(existing_content) }
gitlab_keys.stub(auth_file: tmp_authorized_keys_path)
end
@@ -301,4 +318,13 @@ describe GitlabKeys do
def tmp_lock_file_path
tmp_authorized_keys_path + '.lock'
end
+
+ def capture_stdout(&blk)
+ old = $stdout
+ $stdout = fake = StringIO.new
+ blk.call
+ fake.string
+ ensure
+ $stdout = old
+ end
end