summaryrefslogtreecommitdiff
path: root/lib/gitlab_keys.rb
blob: 3612bcafaf2fef94199f9b11d71ce8b81c22d9d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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 '/gitlab-shell #{@username},/d' #{auth_file}"
    system(cmd)
  end
end