AWS API Dlang, hmac sha256 function.
holo via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Oct 8 21:04:56 PDT 2015
I was fighting with it a little bit and after all i just leave
original function which was in code, removed dependencies from
vibe.d and finally tried to contact api. Here is my present code:
#!/usr/bin/rdmd -L-lcurl
import std.stdio;
import std.string;
import std.file;
import std.datetime;
import std.process;
import std.digest.sha;
import std.net.curl;
import sigv4;
auto zone = "us-east-1";
auto service = "ec2";
void main()
{
auto accKey = environment["AWS_ACCESS_KEY"];
auto secKey = environment["AWS_SECRET_KEY"];
auto currentClock = Clock.currTime;
auto currentDate = cast(Date)currentClock;
auto curDateStr = currentDate.toISOString;
auto currentTime = cast(TimeOfDay)currentClock;
auto curTimeStr = currentTime.toISOString;
auto xamztime = curDateStr ~ "T" ~ curTimeStr ~ "Z";
string[string] empty;
SignableRequest r;
r.dateString = curDateStr;
r.timeStringUTC = curTimeStr;
r.region = zone;
r.service = service;
r.canonicalRequest = CanonicalRequest(
"GET",
"/",
["action" : "DescribeInstances", "version" : "2013-10-15"],
["content-type" : "application/x-www-form-urlencoded;
charset=utf-8",
"host" : service ~ ".amazonaws.com",
"x-amz-date" : xamztime],
cast(ubyte[])"");
auto qParm =
canonicalQueryString(r.canonicalRequest.queryParameters);
auto sigHead = canonicalHeaders(r.canonicalRequest.headers);
auto sigStr = signableString(r);
auto sigKey = signingKey(secKey, curDateStr, zone, service);
auto signature = sign(sigKey,
cast(ubyte[])sigStr).toHexString().toLower();
writeln();
writeln(qParm);
writeln();
writeln(sigHead);
writeln();
writeln(sigStr);
writeln();
writeln(signature);
writeln();
auto client = HTTP();
client.clearRequestHeaders;
client.addRequestHeader("content-type:",
"application/x-www-form-urlencoded; charset=utf-8");
client.addRequestHeader("host:", service ~ ".amazonaws.com");
client.addRequestHeader("x-amz-date:", xamztime);
client.addRequestHeader("Authoryzation:", "AWS4-HMAC-SHA256" ~ "
" ~ "Credential=" ~ accKey ~ "/" ~ xamztime ~ "/" ~ zone ~ "/" ~
service ~ "/" ~ "aws4_request" ~ ", " ~ "SignedHeaders=" ~
"content-type;host;x-amz-date" ~ ", " ~ "Signature=" ~ signature);
auto url = service ~ ".amazonaws.com?" ~
"Action=DescribeInstances&Version=2013-10-15";
writeln(url);
auto content = get(url, client);
writeln(content);
}
Everything is compiling but im getting 400 (Bad Request):
[root at ultraxps aws]# ./header.d
action=DescribeRegions&version=2013-10-15
content-type: application/x-www-form-urlencoded; charset=utf-8
host: ec2.amazonaws.com
x-amz-date: 20151009T053800Z
AWS4-HMAC-SHA256
20151009T053800Z
20151009/us-east-1/ec2/aws4_request
888595748692147ceafafcae3941ec0d83ac42c97641e4d954d7447a00c56270
69b1e4c5212cc6b485569fdfb43f7dde94413b36c50393c55d4810ced47f167b
ec2.amazonaws.com?Action=DescribeRegions&Version=2013-10-15
std.net.curl.CurlException@/usr/include/dlang/dmd/std/net/curl.d(824): HTTP request returned status code 400 (Bad Request)
----------------
/tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(pure @safe bool std.exception.enforce!(std.net.curl.CurlException, bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong)+0x65) [0x5004bd]
/tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(char[] std.net.curl._basicHTTP!(char)._basicHTTP(const(char)[], const(void)[], std.net.curl.HTTP)+0x1a2) [0x4fc0ba]
/tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(char[] std.net.curl.get!(std.net.curl.HTTP, char).get(const(char)[], std.net.curl.HTTP)+0x6a) [0x4fbec2]
/tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(_Dmain+0x840) [0x4f8f98]
/tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x1f) [0x533eab]
/tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x533e06]
/tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll()+0x2b) [0x533e67]
/tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x533e06]
/tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(_d_run_main+0x1d2) [0x533d86]
/tmp/.rdmd-0/rdmd-header.d-54C0A2BD6BD71C27D9AC7319D786A6F3/header(main+0x12) [0x52cb62]
/usr/lib/libc.so.6(__libc_start_main+0xf0) [0x7f9c39f71610]
[root at ultraxps aws]#
What am i doing wrong? What is strange when when i do same with
curl its responding normally of course is not authenticated).
[root at ultraxps aws]# curl
ec2.amazonaws.com?Action=DescribeInstances&Version=2013-10-15
[1] 20390
[root at ultraxps aws]# <?xml version="1.0" encoding="UTF-8"?>
<Response><Errors><Error><Code>MissingParameter</Code><Message>The request must contain the parameter AWSAccessKeyId</Message></Error></Errors><RequestID>e1352781-c2b4-4e74-ade3-80d655efd0ac</RequestID></Response>
More information about the Digitalmars-d-learn
mailing list