File tree Expand file tree Collapse file tree 4 files changed +34
-0
lines changed Expand file tree Collapse file tree 4 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -74,6 +74,11 @@ Remove key:
74
74
75
75
./bin/gitlab-keys rm-key key-23 "ssh-rsa AAAAx321..."
76
76
77
+ List all keys:
78
+
79
+ ./bin/gitlab-keys list-keys
80
+
81
+
77
82
Remove all keys from authorized_keys file:
78
83
79
84
./bin/gitlab-keys clear
Original file line number Diff line number Diff line change @@ -12,6 +12,8 @@ require_relative '../lib/gitlab_init'
12
12
#
13
13
# /bin/gitlab-keys rm-key key-23 "ssh-rsa AAAAx321..."
14
14
#
15
+ # /bin/gitlab-keys list-keys
16
+ #
15
17
# /bin/gitlab-keys clear
16
18
#
17
19
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ def exec
19
19
when 'add-key' ; add_key
20
20
when 'batch-add-keys' ; batch_add_keys
21
21
when 'rm-key' ; rm_key
22
+ when 'list-keys' ; puts list_keys
22
23
when 'clear' ; clear
23
24
else
24
25
$logger. warn "Attempt to execute invalid gitlab-keys command #{ @command . inspect } ."
@@ -38,6 +39,19 @@ def add_key
38
39
true
39
40
end
40
41
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
+
41
55
def batch_add_keys
42
56
lock do
43
57
open ( auth_file , 'a' ) do |file |
Original file line number Diff line number Diff line change 41
41
end
42
42
end
43
43
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
+
44
57
describe :batch_add_keys do
45
58
let ( :gitlab_keys ) { build_gitlab_keys ( 'batch-add-keys' ) }
46
59
let ( :fake_stdin ) { StringIO . new ( "key-12\t ssh-dsa ASDFASGADG\n key-123\t ssh-rsa GFDGDFSGSDFG\n " , 'r' ) }
You can’t perform that action at this time.
0 commit comments