Permalink
Browse files

Use Kernel#open to append lines to authorized_keys

  • Loading branch information...
jacobvosmaer committed Oct 31, 2013
1 parent d3c3fbc commit 8d6807098ffbe9492dbf1de2a7d9620bdeb348e3
Showing with 7 additions and 5 deletions.
  1. +2 −3 lib/gitlab_keys.rb
  2. +5 −2 spec/gitlab_keys_spec.rb
View
@@ -29,9 +29,8 @@ def exec
def add_key
$logger.info "Adding key #{@key_id} => #{@key.inspect}"
- cmd = "command=\"#{ROOT_PATH}/bin/gitlab-shell #{@key_id}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty #{@key}"
- cmd = "echo \'#{cmd}\' >> #{auth_file}"
- system(cmd)
+ auth_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell #{@key_id}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty #{@key}"
+ open(auth_file, 'a') { |file| file.puts(auth_line) }
end
def rm_key
View
@@ -16,15 +16,18 @@
describe :add_key do
let(:gitlab_keys) { build_gitlab_keys('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E') }
+ let(:file) { mock(:file) }
it "should receive valid cmd" do
- valid_cmd = "echo 'command=\"#{ROOT_PATH}/bin/gitlab-shell key-741\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaDAxx2E' >> #{GitlabConfig.new.auth_file}"
- gitlab_keys.should_receive(:system).with(valid_cmd)
+ auth_line = "command=\"#{ROOT_PATH}/bin/gitlab-shell key-741\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaDAxx2E"
+ gitlab_keys.should_receive(:open).with(GitlabConfig.new.auth_file, 'a').and_yield(file)
+ file.should_receive(:puts).with(auth_line)
gitlab_keys.send :add_key
end
it "should log an add-key event" do
$logger.should_receive(:info).with('Adding key key-741 => "ssh-rsa AAAAB3NzaDAxx2E"')
+ gitlab_keys.stub(:open)
gitlab_keys.send :add_key
end
end

0 comments on commit 8d68070

Please sign in to comment.