Commit 1ea542c0 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Refactor 'GitlabKey' class away

It is not nice to have both 'GitlabKeys' and 'GitlabKey'. We also do
not need GitlabKey to be a class when it has no state.
parent fb311531
...@@ -21,5 +21,5 @@ authorized_key = GitlabNet.new.authorized_key(key) ...@@ -21,5 +21,5 @@ authorized_key = GitlabNet.new.authorized_key(key)
if authorized_key.nil? if authorized_key.nil?
puts "# No key was found for #{key}" puts "# No key was found for #{key}"
else else
puts GitlabKey.new.key_line("key-#{authorized_key['id']}", authorized_key["key"]) puts GitlabKeys.key_line("key-#{authorized_key['id']}", authorized_key["key"])
end end
...@@ -6,12 +6,19 @@ require_relative 'gitlab_logger' ...@@ -6,12 +6,19 @@ require_relative 'gitlab_logger'
class GitlabKeys class GitlabKeys
attr_accessor :auth_file, :key attr_accessor :auth_file, :key
def self.command(key_id)
"#{ROOT_PATH}/bin/gitlab-shell #{key_id}"
end
def self.key_line(key_id, public_key)
"command=\"#{command(key_id)}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty #{public_key}"
end
def initialize def initialize
@command = ARGV.shift @command = ARGV.shift
@key_id = ARGV.shift @key_id = ARGV.shift
@key = ARGV.shift @key = ARGV.shift
@auth_file = GitlabConfig.new.auth_file @auth_file = GitlabConfig.new.auth_file
@gitlab_key = GitlabKey.new
end end
def exec def exec
...@@ -34,7 +41,7 @@ class GitlabKeys ...@@ -34,7 +41,7 @@ class GitlabKeys
def add_key def add_key
lock do lock do
$logger.info "Adding key #{@key_id} => #{@key.inspect}" $logger.info "Adding key #{@key_id} => #{@key.inspect}"
auth_line = @gitlab_key.key_line(@key_id, @key) auth_line = self.class.key_line(@key_id, @key)
open_auth_file('a') { |file| file.puts(auth_line) } open_auth_file('a') { |file| file.puts(auth_line) }
end end
true true
...@@ -61,7 +68,7 @@ class GitlabKeys ...@@ -61,7 +68,7 @@ class GitlabKeys
abort("#{$0}: invalid input #{input.inspect}") unless tokens.count == 2 abort("#{$0}: invalid input #{input.inspect}") unless tokens.count == 2
key_id, public_key = tokens key_id, public_key = tokens
$logger.info "Adding key #{key_id} => #{public_key.inspect}" $logger.info "Adding key #{key_id} => #{public_key.inspect}"
file.puts(@gitlab_key.key_line(key_id, public_key)) file.puts(self.class.key_line(key_id, public_key))
end end
end end
end end
...@@ -77,7 +84,7 @@ class GitlabKeys ...@@ -77,7 +84,7 @@ class GitlabKeys
$logger.info "Removing key #{@key_id}" $logger.info "Removing key #{@key_id}"
open_auth_file('r+') do |f| open_auth_file('r+') do |f|
while line = f.gets do while line = f.gets do
next unless line.start_with?("command=\"#{@gitlab_key.command(@key_id)}\"") next unless line.start_with?("command=\"#{self.class.command(@key_id)}\"")
f.seek(-line.length, IO::SEEK_CUR) f.seek(-line.length, IO::SEEK_CUR)
# Overwrite the line with #'s. Because the 'line' variable contains # Overwrite the line with #'s. Because the 'line' variable contains
# a terminating '\n', we write line.length - 1 '#' characters. # a terminating '\n', we write line.length - 1 '#' characters.
...@@ -128,14 +135,3 @@ class GitlabKeys ...@@ -128,14 +135,3 @@ class GitlabKeys
end end
end end
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
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