Commit c6fa7e5e authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

add/remove keys to authorized_keys

parent b83ab386
#!/usr/bin/env ruby
#
# GitLab Keys shell. Add/remove keys from ~/.ssh/authorized_keys
#
# Ex.
# /bin/gitlab-keys add-key dzaporozhets "ssh-rsa AAAAx321..."
#
#
ROOT_PATH = File.join(File.expand_path(File.dirname(__FILE__)), "..")
require File.join(ROOT_PATH, 'lib', 'gitlab_keys')
GitlabKeys.new.exec
exit
user: git user: git
repos_path: "/home/git/repositories" repos_path: "/home/git/repositories"
auth_file: "/home/git/.ssh/authorized_keys"
require 'open3'
require 'yaml'
class GitlabKeys
attr_accessor :auth_file, :key, :username
def initialize
@command = ARGV.shift
@username = ARGV.shift
@key = ARGV.shift
config = YAML.load_file(File.join(ROOT_PATH, 'config.yml'))
@auth_file = config['auth_file']
end
def exec
case @command
when 'add-key'; add_key
when 'rm-key'; rm_key
when 'rm-user'; rm_user
else
puts 'not allowed'
end
end
protected
def add_key
cmd = "command=\"#{ROOT_PATH}/bin/gitlab-shell #{@username}\",no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty #{@key}"
cmd = "echo \"#{cmd}\" >> #{auth_file}"
system(cmd)
end
def rm_key
cmd = "sed '/#{@key}/d' #{auth_file}"
system(cmd)
end
def rm_user
cmd = "sed -i '/#{@username}/d' #{auth_file}"
puts cmd
system(cmd)
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