Class: GithubAuthorizedKeys::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/github_authorized_keys.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/github_authorized_keys.rb', line 7

def config
  @config
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/github_authorized_keys.rb', line 7

def headers
  @headers
end

Instance Method Details

#fetch_keys(login) ⇒ Object



51
52
53
# File 'lib/github_authorized_keys.rb', line 51

def fetch_keys()
  github_http_get("/users/#{}/keys?#{config['oauth_token']}", headers)
end

#fetch_membersObject



47
48
49
# File 'lib/github_authorized_keys.rb', line 47

def fetch_members
  github_http_get("/orgs/#{config['organization']}/members?#{config['oauth_token']}", headers)
end

#github_http_get(url, headers) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/github_authorized_keys.rb', line 37

def github_http_get(url, headers)
  unless @github_http
    @github_http = Net::HTTP.new('api.github.com', 443)
    @github_http.use_ssl = true
    @github_http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end

  JSON.parse(@github_http.request_get(url, headers).body)
end

#load_config(config_file) ⇒ Object



55
56
57
58
# File 'lib/github_authorized_keys.rb', line 55

def load_config(config_file)
  config_file ||= "#{ENV['HOME']}/.github_authorized_keys.yml"
  @config = YAML.load_file(config_file)
end

#read_original_authorized_keysObject



60
61
62
63
64
65
66
67
68
# File 'lib/github_authorized_keys.rb', line 60

def read_original_authorized_keys
  File.open("#{ENV['HOME']}/.ssh/authorized_keys") do |file|
    while(line = file.gets)
      puts line
    end
  end
rescue
  '' # file does not exist
end

#run(config_file) ⇒ Object



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
# File 'lib/github_authorized_keys.rb', line 9

def run(config_file)
  begin
    load_config(config_file)
    @headers = {'User-Agent' => "#{config['organization']} authorized_keys generator"}

    authorized_keys = [
      '### THIS FILE IS AUTOMATICALLY GENERATED',
    ]
    if config.include?('additional_keys')
      authorized_keys.concat(config['additional_keys'])
    end

    fetch_members.each do |member|
      authorized_keys << "# #{member['login']}"
      fetch_keys(member['login']).each do |ssh_key|
        authorized_keys << ssh_key['key']
      end
    end

    authorized_keys.join("\n")
  rescue Errno::ENOENT
    $stderr.puts "Unable to read configuration file: '#{config_file}'" unless $testing
    read_original_authorized_keys
  rescue
    read_original_authorized_keys
  end
end