Skip to content

Instantly share code, notes, and snippets.

@shun115
Last active August 29, 2015 13:57
Show Gist options
  • Save shun115/9526768 to your computer and use it in GitHub Desktop.
Save shun115/9526768 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
require 'rubygems'
require 'cgi'
require 'time'
require 'openssl'
require 'base64'
require 'net/http'
require 'net/https'
require 'pp'
# キー情報をコピペしてください。
# アクセスキー
ACCESS_IDENTIFIER = ''
# シークレットキー
SECRET_IDENTIFIER = ''
# RDBのAPIアクセスポイント
ENDPOINT = 'rdb-api.jp-e1.cloudn-service.com'
ADDRESS = "http://#{ENDPOINT}"
if ARGV.size < 1 then
puts "usage :./kick_api.rb Action=... parameter=... parameter=..."; exit 1;
end
# シグネチャ生成用の変数
params = {
'AWSAccessKeyId' => ACCESS_IDENTIFIER,
'SignatureMethod' => 'HmacSHA256',
'SignatureVersion' => 2,
'Version' => '2012-04-23',
'Timestamp' => Time.now.iso8601
}
for i in ARGV
key, value = i.split("=")
params[key] = value
end
canonical_querystring = params.sort.collect { |key, value| [CGI.escape(key.to_s), CGI.escape(value.to_s)].join('=') }.join('&')
puts canonical_querystring
string_to_sign = "GET
#{ENDPOINT}
/
#{canonical_querystring}"
ssl = OpenSSL::HMAC.new(SECRET_IDENTIFIER, 'sha256')
ssl.update(string_to_sign)
signature = Base64.encode64(ssl.digest).chomp # chomp is important! the base64 encoded version will have a newline at the end
params['Signature'] = signature
querystring = params.collect { |key, value| [CGI.escape(key.to_s), CGI.escape(value.to_s)].join('=') }.join('&') # order doesn't matter for the actual request
get = Net::HTTP::Get.new("/?#{querystring}")
https = Net::HTTP.new("#{ENDPOINT}", 443)
https.use_ssl = true
https.start { |w|
puts "====="
response = w.request(get)
p response.header
puts response.body
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment