Skip to content

Commit 4e044b0

Browse files
committed
Added list-keys command and spec
Removed puts and tidied up regex Address the hound Address the hound, again Use single quotes
1 parent 27fe2ea commit 4e044b0

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ Remove key:
7474

7575
./bin/gitlab-keys rm-key key-23 "ssh-rsa AAAAx321..."
7676

77+
List all keys:
78+
79+
./bin/gitlab-keys list-keys
80+
81+
7782
Remove all keys from authorized_keys file:
7883

7984
./bin/gitlab-keys clear

bin/gitlab-keys

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ require_relative '../lib/gitlab_init'
1212
#
1313
# /bin/gitlab-keys rm-key key-23 "ssh-rsa AAAAx321..."
1414
#
15+
# /bin/gitlab-keys list-keys
16+
#
1517
# /bin/gitlab-keys clear
1618
#
1719

lib/gitlab_keys.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def exec
1919
when 'add-key'; add_key
2020
when 'batch-add-keys'; batch_add_keys
2121
when 'rm-key'; rm_key
22+
when 'list-keys'; puts list_keys
2223
when 'clear'; clear
2324
else
2425
$logger.warn "Attempt to execute invalid gitlab-keys command #{@command.inspect}."
@@ -38,6 +39,19 @@ def add_key
3839
true
3940
end
4041

42+
def list_keys
43+
$logger.info "Listing all keys"
44+
keys = ""
45+
File.readlines(auth_file).each do |line|
46+
# key_id & public_key
47+
# command=".../bin/gitlab-shell key-741" ... ssh-rsa AAAAB3NzaDAxx2E\n
48+
# ^^^^^^^ ^^^^^^^^^^^^^^^
49+
matches = /^command=\".+?\s+(.+?)\".+?ssh-rsa\s(.+)\s*.*\n*$/.match(line)
50+
keys << "#{matches[1]} #{matches[2]}\n" unless matches.nil?
51+
end
52+
keys
53+
end
54+
4155
def batch_add_keys
4256
lock do
4357
open(auth_file, 'a') do |file|

spec/gitlab_keys_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@
4141
end
4242
end
4343

44+
describe :list_keys do
45+
let(:gitlab_keys) do
46+
build_gitlab_keys('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E')
47+
end
48+
49+
it "adds a key and lists it" do
50+
create_authorized_keys_fixture
51+
gitlab_keys.send :add_key
52+
auth_line1 = 'key-741 AAAAB3NzaDAxx2E'
53+
gitlab_keys.send(:list_keys).should == "#{auth_line1}\n"
54+
end
55+
end
56+
4457
describe :batch_add_keys do
4558
let(:gitlab_keys) { build_gitlab_keys('batch-add-keys') }
4659
let(:fake_stdin) { StringIO.new("key-12\tssh-dsa ASDFASGADG\nkey-123\tssh-rsa GFDGDFSGSDFG\n", 'r') }

0 commit comments

Comments
 (0)