diff options
author | Pablo Carranza <pcarranza@gmail.com> | 2016-03-08 17:49:22 +0000 |
---|---|---|
committer | Pablo Carranza <pcarranza@gmail.com> | 2016-03-24 20:48:27 +0000 |
commit | c9fac154cd99233c9a6f1cbb88316a476fffc3ad (patch) | |
tree | 6d7c6ea65e4c0204ca9596adc3da7e9e29caa51c /lib | |
parent | 1f2bef765d8aa03b76f991178cfa7513833b4c3b (diff) | |
download | gitlab-shell-c9fac154cd99233c9a6f1cbb88316a476fffc3ad.tar.gz |
Add authorized keys bin script to find keys by fingerprint
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab_authorized_keys.rb | 0 | ||||
-rw-r--r-- | lib/gitlab_keys.rb | 26 | ||||
-rw-r--r-- | lib/gitlab_net.rb | 6 |
3 files changed, 19 insertions, 13 deletions
diff --git a/lib/gitlab_authorized_keys.rb b/lib/gitlab_authorized_keys.rb new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/gitlab_authorized_keys.rb 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 diff --git a/lib/gitlab_net.rb b/lib/gitlab_net.rb index ddf2a06..503a267 100644 --- a/lib/gitlab_net.rb +++ b/lib/gitlab_net.rb @@ -56,9 +56,11 @@ class GitlabNet get("#{host}/check", read_timeout: CHECK_TIMEOUT) end - def ssh_key(fingerprint) + def authorized_key(fingerprint) resp = get("#{host}/ssh-key?fingerprint=#{fingerprint}") - JSON.parse(resp.body) rescue nil + JSON.parse(resp.body) if resp.code == "200" + rescue + nil end protected |