class GithubAuthorizedKeys::CLI

Attributes

config[R]
headers[R]

Public Instance Methods

fetch_keys(login) click to toggle source
# File lib/github_authorized_keys.rb, line 51
def fetch_keys(login)
  github_http_get("/users/#{login}/keys?#{config['oauth_token']}", headers)
end
fetch_members() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_keys() click to toggle source
# 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) click to toggle source
# 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