Commit 367a672a authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'batch_key_import' into 'master'

Batch Key Import
parents 9e076ab2 3a48eae7
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
......
......@@ -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')
......
......@@ -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|
......
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') }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment