Skip to content

Added list-keys command, with associated spec #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 28, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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
2 changes: 2 additions & 0 deletions bin/gitlab-keys
Original file line number Diff line number Diff line change
Expand Up @@ -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
#

Expand Down
14 changes: 14 additions & 0 deletions lib/gitlab_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def exec
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}."
Expand All @@ -38,6 +39,19 @@ def add_key
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|
Expand Down
13 changes: 13 additions & 0 deletions spec/gitlab_keys_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@
end
end

describe :list_keys do
let(:gitlab_keys) do
build_gitlab_keys('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

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') }
Expand Down