diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2016-03-29 13:32:17 +0000 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2016-03-29 13:32:17 +0000 |
commit | fda23577b96bc1fcb50e0b6e7c41df729ece68dd (patch) | |
tree | 1aa59a1778b367d406de919d1e20dc5e3eb13b21 /lib/gitlab_keys.rb | |
parent | 91e722550b40bbd8214e54a01b2183b27b95bde1 (diff) | |
parent | 38b8600f93ebe40eb9211051ef64b1013d053132 (diff) | |
download | gitlab-shell-fda23577b96bc1fcb50e0b6e7c41df729ece68dd.tar.gz |
Merge branch 'use-ssh-key-internal-api' into 'master'
Use ssh key internal api to build the authorized-keys command on openssh 6.9
See merge request !42
Diffstat (limited to 'lib/gitlab_keys.rb')
-rw-r--r-- | lib/gitlab_keys.rb | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/lib/gitlab_keys.rb b/lib/gitlab_keys.rb index 3710f96..f17e6b7 100644 --- a/lib/gitlab_keys.rb +++ b/lib/gitlab_keys.rb @@ -11,6 +11,7 @@ class GitlabKeys @key_id = ARGV.shift @key = ARGV.shift @auth_file = GitlabConfig.new.auth_file + @gitlab_key = GitlabKey.new end def exec @@ -32,7 +33,7 @@ class GitlabKeys def add_key lock do $logger.info "Adding key #{@key_id} => #{@key.inspect}" - auth_line = key_line(@key_id, @key) + auth_line = @gitlab_key.key_line(@key_id, @key) open(auth_file, 'a') { |file| file.puts(auth_line) } end true @@ -59,7 +60,7 @@ class GitlabKeys 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)) + file.puts(@gitlab_key.key_line(key_id, public_key)) end end end @@ -70,20 +71,12 @@ class GitlabKeys $stdin end - def key_command(key_id) - "#{ROOT_PATH}/bin/gitlab-shell #{key_id}" - end - - def key_line(key_id, public_key) - auth_line = "command=\"#{key_command(key_id)}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty #{public_key}" - end - def rm_key lock do $logger.info "Removing key #{@key_id}" open(auth_file, 'r+') do |f| while line = f.gets do - next unless line.start_with?("command=\"#{key_command(@key_id)}\"") + next unless line.start_with?("command=\"#{@gitlab_key.command(@key_id)}\"") f.seek(-line.length, IO::SEEK_CUR) # Overwrite the line with #'s. Because the 'line' variable contains # a terminating '\n', we write line.length - 1 '#' characters. @@ -115,3 +108,14 @@ class GitlabKeys @lock_file ||= auth_file + '.lock' end end + + +class GitlabKey + def command(key_id) + "#{ROOT_PATH}/bin/gitlab-shell #{key_id}" + end + + def key_line(key_id, public_key) + "command=\"#{command(key_id)}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty #{public_key}" + end +end |