diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-03-14 12:45:24 +0000 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2014-03-14 12:45:24 +0000 |
commit | 367a672a35d78c26a79c18ba92bfc14a950e3545 (patch) | |
tree | 398d32db7a41036c088e44bcad387692f294f74f | |
parent | 9e076ab204eb3fec2f59581230a69eacfd39f78c (diff) | |
parent | 3a48eae79eafe1d3641b0aa4271b83b348a95ee6 (diff) | |
download | gitlab-shell-367a672a35d78c26a79c18ba92bfc14a950e3545.tar.gz |
Merge branch 'batch_key_import' into 'master'
Batch Key Import
-rw-r--r-- | CHANGELOG | 1 | ||||
-rwxr-xr-x | bin/gitlab-keys | 4 | ||||
-rw-r--r-- | lib/gitlab_keys.rb | 24 | ||||
-rw-r--r-- | spec/gitlab_keys_spec.rb | 42 |
4 files changed, 69 insertions, 2 deletions
@@ -1,5 +1,6 @@ v1.8.4 - Dont do import if repository exists + - Add `gitlab-keys batch-add-keys` subcommand for authorized_keys rebuilds v1.8.3 - Add timeout option for repository import diff --git a/bin/gitlab-keys b/bin/gitlab-keys index d402d1e..2f53a71 100755 --- a/bin/gitlab-keys +++ b/bin/gitlab-keys @@ -8,9 +8,11 @@ require_relative '../lib/gitlab_init' # Ex. # /bin/gitlab-keys add-key key-782 "ssh-rsa AAAAx321..." # +# printf "key-782\tssh-rsa AAAAx321...\n" | /bin/gitlab-keys batch-add-keys +# # /bin/gitlab-keys rm-key key-23 "ssh-rsa AAAAx321..." # -# /bin/gitlab-keys clear" +# /bin/gitlab-keys clear # require File.join(ROOT_PATH, 'lib', 'gitlab_keys') diff --git a/lib/gitlab_keys.rb b/lib/gitlab_keys.rb index 0098578..cade09e 100644 --- a/lib/gitlab_keys.rb +++ b/lib/gitlab_keys.rb @@ -16,6 +16,7 @@ class GitlabKeys def exec case @command when 'add-key'; add_key + when 'batch-add-keys'; batch_add_keys when 'rm-key'; rm_key when 'clear'; clear else @@ -29,11 +30,32 @@ class GitlabKeys def add_key $logger.info "Adding key #{@key_id} => #{@key.inspect}" - auth_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell #{@key_id}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty #{@key}" + auth_line = key_line(@key_id, @key) open(auth_file, 'a') { |file| file.puts(auth_line) } true end + def batch_add_keys + open(auth_file, 'a') do |file| + stdin.each_line do |input| + tokens = input.strip.split("\t") + abort("#{$0}: invalid input #{input.inspect}") unless tokens.count == 2 + key_id, public_key = tokens + $logger.info "Adding key #{key_id} => #{public_key.inspect}" + file.puts(key_line(key_id, public_key)) + end + end + true + end + + def stdin + $stdin + end + + def key_line(key_id, public_key) + auth_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell #{key_id}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty #{public_key}" + end + def rm_key $logger.info "Removing key #{@key_id}" Tempfile.open('authorized_keys') do |temp| diff --git a/spec/gitlab_keys_spec.rb b/spec/gitlab_keys_spec.rb index e86874a..5bf4c04 100644 --- a/spec/gitlab_keys_spec.rb +++ b/spec/gitlab_keys_spec.rb @@ -1,5 +1,6 @@ require_relative 'spec_helper' require_relative '../lib/gitlab_keys' +require 'stringio' describe GitlabKeys do before do @@ -39,6 +40,47 @@ describe GitlabKeys do 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') } + before do + create_authorized_keys_fixture + gitlab_keys.stub(stdin: fake_stdin) + end + + it "adds lines at the end of the file" do + gitlab_keys.send :batch_add_keys + auth_line1 = "command=\"#{ROOT_PATH}/bin/gitlab-shell key-12\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-dsa ASDFASGADG" + auth_line2 = "command=\"#{ROOT_PATH}/bin/gitlab-shell key-123\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa GFDGDFSGSDFG" + File.read(tmp_authorized_keys_path).should == "existing content\n#{auth_line1}\n#{auth_line2}\n" + end + + context "with invalid input" do + let(:fake_stdin) { StringIO.new("key-12\tssh-dsa ASDFASGADG\nkey-123\tssh-rsa GFDGDFSGSDFG\nfoo\tbar\tbaz\n", 'r') } + + it "aborts" do + gitlab_keys.should_receive(:abort) + gitlab_keys.send :batch_add_keys + end + end + + context "without file writing" do + before do + gitlab_keys.should_receive(:open).and_yield(mock(:file, puts: nil)) + end + + it "should log an add-key event" do + $logger.should_receive(:info).with('Adding key key-12 => "ssh-dsa ASDFASGADG"') + $logger.should_receive(:info).with('Adding key key-123 => "ssh-rsa GFDGDFSGSDFG"') + gitlab_keys.send :batch_add_keys + end + + it "should return true" do + gitlab_keys.send(:batch_add_keys).should be_true + end + end + end + describe :rm_key do let(:gitlab_keys) { build_gitlab_keys('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') } |