summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-10-28 19:20:26 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-10-28 19:20:26 +0200
commit3a16acf364aa85e283ef10bbbaf6a5f558e5897e (patch)
treed1074ae9cc68f9c4cf63f95f8eacd556ae1501bc
parent8f9c11af3c14c0036e7038c33262367288a09ac8 (diff)
parent89d8af466ba283b31d7800fe604defa0ce189902 (diff)
downloadgitlab-shell-3a16acf364aa85e283ef10bbbaf6a5f558e5897e.tar.gz
Merge pull request #177 from feedhenry/keys_list
Added list-keys command, with associated spec
-rw-r--r--README.md5
-rwxr-xr-xbin/gitlab-keys2
-rw-r--r--lib/gitlab_keys.rb14
-rw-r--r--spec/gitlab_keys_spec.rb13
4 files changed, 34 insertions, 0 deletions
diff --git a/README.md b/README.md
index 5f3c988..3f04de9 100644
--- a/README.md
+++ b/README.md
@@ -78,6 +78,11 @@ Remove key:
./bin/gitlab-keys rm-key key-23 "ssh-rsa AAAAx321..."
+List all keys:
+
+ ./bin/gitlab-keys list-keys
+
+
Remove all keys from authorized_keys file:
./bin/gitlab-keys clear
diff --git a/bin/gitlab-keys b/bin/gitlab-keys
index 2f53a71..9eb1950 100755
--- a/bin/gitlab-keys
+++ b/bin/gitlab-keys
@@ -12,6 +12,8 @@ require_relative '../lib/gitlab_init'
#
# /bin/gitlab-keys rm-key key-23 "ssh-rsa AAAAx321..."
#
+# /bin/gitlab-keys list-keys
+#
# /bin/gitlab-keys clear
#
diff --git a/lib/gitlab_keys.rb b/lib/gitlab_keys.rb
index 3bdf6c6..100e164 100644
--- a/lib/gitlab_keys.rb
+++ b/lib/gitlab_keys.rb
@@ -19,6 +19,7 @@ class GitlabKeys
when 'add-key'; add_key
when 'batch-add-keys'; batch_add_keys
when 'rm-key'; rm_key
+ when 'list-keys'; puts list_keys
when 'clear'; clear
else
$logger.warn "Attempt to execute invalid gitlab-keys command #{@command.inspect}."
@@ -38,6 +39,19 @@ class GitlabKeys
true
end
+ def list_keys
+ $logger.info 'Listing all keys'
+ keys = ''
+ File.readlines(auth_file).each do |line|
+ # key_id & public_key
+ # command=".../bin/gitlab-shell key-741" ... ssh-rsa AAAAB3NzaDAxx2E\n
+ # ^^^^^^^ ^^^^^^^^^^^^^^^
+ matches = /^command=\".+?\s+(.+?)\".+?ssh-rsa\s(.+)\s*.*\n*$/.match(line)
+ keys << "#{matches[1]} #{matches[2]}\n" unless matches.nil?
+ end
+ keys
+ end
+
def batch_add_keys
lock do
open(auth_file, 'a') do |file|
diff --git a/spec/gitlab_keys_spec.rb b/spec/gitlab_keys_spec.rb
index aaaee15..bcce628 100644
--- a/spec/gitlab_keys_spec.rb
+++ b/spec/gitlab_keys_spec.rb
@@ -41,6 +41,19 @@ describe GitlabKeys do
end
end
+ describe :list_keys do
+ let(:gitlab_keys) do
+ build_gitlab_keys('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E')
+ end
+
+ it 'adds a key and lists it' do
+ create_authorized_keys_fixture
+ gitlab_keys.send :add_key
+ auth_line1 = 'key-741 AAAAB3NzaDAxx2E'
+ gitlab_keys.send(:list_keys).should == "#{auth_line1}\n"
+ 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') }