Commit 2241d767 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

add/remove keys by id

parent a8b9cb85
...@@ -6,9 +6,9 @@ require_relative '../lib/gitlab_init' ...@@ -6,9 +6,9 @@ require_relative '../lib/gitlab_init'
# GitLab Keys shell. Add/remove keys from ~/.ssh/authorized_keys # GitLab Keys shell. Add/remove keys from ~/.ssh/authorized_keys
# #
# Ex. # Ex.
# /bin/gitlab-keys add-key dzaporozhets "ssh-rsa AAAAx321..." # /bin/gitlab-keys add-key key-782 "ssh-rsa AAAAx321..."
# #
# /bin/gitlab-keys rm-key dzaporozhets "ssh-rsa AAAAx321..." # /bin/gitlab-keys rm-key key-23 "ssh-rsa AAAAx321..."
# #
require File.join(ROOT_PATH, 'lib', 'gitlab_keys') require File.join(ROOT_PATH, 'lib', 'gitlab_keys')
......
...@@ -2,11 +2,11 @@ require 'open3' ...@@ -2,11 +2,11 @@ require 'open3'
require_relative 'gitlab_config' require_relative 'gitlab_config'
class GitlabKeys class GitlabKeys
attr_accessor :auth_file, :key, :username attr_accessor :auth_file, :key
def initialize def initialize
@command = ARGV.shift @command = ARGV.shift
@username = ARGV.shift @key_id = ARGV.shift
@key = ARGV.shift @key = ARGV.shift
@auth_file = GitlabConfig.new.auth_file @auth_file = GitlabConfig.new.auth_file
end end
...@@ -23,13 +23,13 @@ class GitlabKeys ...@@ -23,13 +23,13 @@ class GitlabKeys
protected protected
def add_key def add_key
cmd = "command=\"#{ROOT_PATH}/bin/gitlab-shell #{@username}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty #{@key}" 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}" cmd = "echo \'#{cmd}\' >> #{auth_file}"
system(cmd) system(cmd)
end end
def rm_key def rm_key
cmd = "sed '/#{@key}/d' #{auth_file}" cmd = "sed '/#{@key_id}/d' #{auth_file}"
system(cmd) system(cmd)
end end
end end
...@@ -4,23 +4,23 @@ require_relative '../lib/gitlab_keys' ...@@ -4,23 +4,23 @@ require_relative '../lib/gitlab_keys'
describe GitlabKeys do describe GitlabKeys do
describe :initialize do describe :initialize do
before do before do
argv('add-key', 'dzaporozhets', 'ssh-rsa AAAAB3NzaDAxx2E') argv('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E')
@gl_keys = GitlabKeys.new @gl_keys = GitlabKeys.new
end end
it { @gl_keys.username.should == 'dzaporozhets' }
it { @gl_keys.key.should == 'ssh-rsa AAAAB3NzaDAxx2E' } it { @gl_keys.key.should == 'ssh-rsa AAAAB3NzaDAxx2E' }
it { @gl_keys.instance_variable_get(:@command).should == 'add-key' } it { @gl_keys.instance_variable_get(:@command).should == 'add-key' }
it { @gl_keys.instance_variable_get(:@key_id).should == 'key-741' }
end end
describe :add_key do describe :add_key do
before do before do
argv('add-key', 'dzaporozhets', 'ssh-rsa AAAAB3NzaDAxx2E') argv('add-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E')
@gl_keys = GitlabKeys.new @gl_keys = GitlabKeys.new
end end
it "should receive valid cmd" do it "should receive valid cmd" do
valid_cmd = "echo 'command=\"#{ROOT_PATH}/bin/gitlab-shell dzaporozhets\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty ssh-rsa AAAAB3NzaDAxx2E' >> /home/git/.ssh/authorized_keys" 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' >> /home/git/.ssh/authorized_keys"
@gl_keys.should_receive(:system).with(valid_cmd) @gl_keys.should_receive(:system).with(valid_cmd)
@gl_keys.send :add_key @gl_keys.send :add_key
end end
...@@ -28,12 +28,12 @@ describe GitlabKeys do ...@@ -28,12 +28,12 @@ describe GitlabKeys do
describe :rm_key do describe :rm_key do
before do before do
argv('rm-key', 'dzaporozhets', 'ssh-rsa AAAAB3NzaDAxx2E') argv('rm-key', 'key-741', 'ssh-rsa AAAAB3NzaDAxx2E')
@gl_keys = GitlabKeys.new @gl_keys = GitlabKeys.new
end end
it "should receive valid cmd" do it "should receive valid cmd" do
valid_cmd = "sed '/ssh-rsa AAAAB3NzaDAxx2E/d' /home/git/.ssh/authorized_keys" valid_cmd = "sed '/key-741/d' /home/git/.ssh/authorized_keys"
@gl_keys.should_receive(:system).with(valid_cmd) @gl_keys.should_receive(:system).with(valid_cmd)
@gl_keys.send :rm_key @gl_keys.send :rm_key
end end
......
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