nixos-config/static/add-fudo-ssh-key.rb

49 lines
1.0 KiB
Ruby
Raw Normal View History

2021-01-14 14:43:52 -08:00
#!/usr/bin/env ruby
require 'uri'
require 'net/http'
require 'net/https'
require 'json'
require 'socket'
if ! ENV['FUDO_GIT_TOKEN']
puts "FUDO_GIT_TOKEN must be set first"
exit 1
end
token = ENV['FUDO_GIT_TOKEN']
if ARGV.length != 1
puts "usage: #{$0} <filename>"
exit 1
end
filename = ARGV[0]
if not File::exist?(filename)
puts "file does not exist: #{filename}"
exit 2
end
target_uri = URI.parse("https://git.fudo.org/api/v1/admin/users/fudo/keys")
key = File::open(filename).read.strip
hostname = Socket::gethostname
@payload = {
key: key,
read_only: true,
title: "#{hostname} fudo key"
}
https = Net::HTTP.new(target_uri.host, target_uri.port)
https.use_ssl = true
req = Net::HTTP::Post.new(target_uri.path, initheader = {
'Content-Type' => 'application/json',
'Authorization' => "token #{token}"
})
req.body = @payload.to_json
res = https.request(req)
puts "response #{res.code} #{res.message}: #{res.body}"