Created
December 7, 2018 22:47
-
-
Save xcsrz/0f3be9fadaa54e5b9fbca7fbdb54f17e to your computer and use it in GitHub Desktop.
Sendgrid's UI does not give you full control over API keys. This script does. Set two environment variables and you can view your keys, their scopes and add/remove scopes from any key. Simple utility but saves a lot of time and headache.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'base64' | |
require 'rest-client' | |
require 'json' | |
# https://stackoverflow.com/questions/38613941/sendgrid-error-access-forbidden-when-trying-to-get-user-profile-api | |
$base_endpiont = "https://api.sendgrid.com/v3/api_keys" | |
def usage | |
exe = File.basename $0 | |
puts | |
puts "Usage:" | |
puts "\t#{exe}" | |
puts "\t - list keys" | |
puts "\t#{exe} [ KEY ID ]" | |
puts "\t - view a key's details" | |
puts "\t#{exe} [ KEY ID ] add [ SCOPES ... ]" | |
puts "\t - add one or more scopes to a key" | |
puts "\t#{exe} [ KEY ID ] del [ SCOPES ... ]" | |
puts "\t - remove one or more scopes from a key" | |
puts | |
puts "A list of available scopes can be found here:" | |
puts "https://sendgrid.com/docs/API_Reference/Web_API_v3/API_Keys/api_key_permissions_list.html" | |
puts | |
end | |
def basicAuth | |
"Basic #{ Base64.encode64("#{ENV['SENDGRID_USERNAME']}:#{ENV['SENDGRID_PASSWORD']}") }" | |
end | |
def get_key(key=nil) | |
url = $base_endpiont | |
if key | |
url += "/#{key}" | |
end | |
begin | |
resp = RestClient.get(url, headers={Authorization: basicAuth}) | |
JSON.parse(resp.body) | |
rescue Exception => e | |
puts e | |
puts e.response | |
exit | |
end | |
end | |
def save_key(id, key) | |
if not key or key['errors'] | |
puts "Key not found" | |
exit | |
end | |
key.delete('api_key_id') | |
key['scopes'].uniq! | |
begin | |
resp = RestClient.put("#{$base_endpiont}/#{id}", key.to_json, headers={Authorization: basicAuth, 'Content-Type' => 'application/json'}) | |
JSON.parse(resp.body) | |
rescue Exception => e | |
puts e | |
puts e.response | |
exit | |
end | |
end | |
def add_scope(id, *scopes) | |
key = (get_key id) | |
key['scopes'].concat scopes | |
res = save_key(id, key) | |
print_key res | |
end | |
def del_scope(id, *scopes) | |
key = (get_key id) | |
key['scopes'] = key['scopes'] - scopes | |
res = save_key(id, key) | |
print_key res | |
end | |
def print_key(key) | |
if not key or key['errors'] | |
puts "Key not found" | |
exit | |
end | |
puts "Name: #{key['name']}" | |
puts "Key ID: #{key['api_key_id']}" | |
puts "Scopes:" | |
key['scopes'].each { |scope| | |
puts " - #{scope}" | |
} | |
end | |
case ARGV.size | |
when 0 then | |
res = get_key | |
res['result'].each { |key| | |
puts "#{key['name']}: #{key['api_key_id']}" | |
} | |
when 1 then | |
print_key(get_key ARGV[0]) | |
when 3..100 | |
case ARGV[1].downcase | |
when "add" then | |
add_scope ARGV[0], *ARGV.drop(2) | |
when "del" then | |
del_scope ARGV[0], *ARGV.drop(2) | |
else | |
puts ">> #{ARGV[1].downcase}" | |
usage | |
end | |
else | |
usage | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment