1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
let hex_sha256 s =
Digestif.SHA256.digest_string s |> Digestif.SHA256.to_hex
let hmac_sha256 ~key msg =
Digestif.SHA256.hmac_string ~key msg
|> Digestif.SHA256.to_raw_string
let hex_of_string s =
let buf = Buffer.create (String.length s * 2) in
String.iter
(fun c -> Buffer.add_string buf (Printf.sprintf "%02x" (Char.code c)))
s;
Buffer.contents buf
let derive_signing_key ~secret_access_key ~date ~region ~service =
let k_date = hmac_sha256 ~key:("AWS4" ^ secret_access_key) date in
let k_region = hmac_sha256 ~key:k_date region in
let k_service = hmac_sha256 ~key:k_region service in
hmac_sha256 ~key:k_service "aws4_request"
let percent_encode ~reserved s =
let buf = Buffer.create (String.length s) in
String.iter
(fun c ->
let code = Char.code c in
let unreserved =
(code >= 0x30 && code <= 0x39)
|| (code >= 0x41 && code <= 0x5a)
|| (code >= 0x61 && code <= 0x7a)
|| c = '-' || c = '.' || c = '_' || c = '~'
in
let keep_as_path_sep = reserved && c = '/' in
if unreserved || keep_as_path_sep then Buffer.add_char buf c
else Buffer.add_string buf (Printf.sprintf "%%%02X" code))
s;
Buffer.contents buf
let compare_pair (k1, v1) (k2, v2) =
match String.compare k1 k2 with
| 0 -> String.compare v1 v2
| c -> c
let canonical_query_string params =
let encoded =
List.map
(fun (k, v) ->
percent_encode ~reserved:false k,
percent_encode ~reserved:false v)
params
in
let sorted = List.sort compare_pair encoded in
String.concat "&"
(List.map (fun (k, v) -> k ^ "=" ^ v) sorted)
let amz_timestamps now =
let tm = Unix.gmtime now in
let date =
Printf.sprintf "%04d%02d%02d"
(tm.tm_year + 1900) (tm.tm_mon + 1) tm.tm_mday
in
let timestamp =
Printf.sprintf "%sT%02d%02d%02dZ"
date tm.tm_hour tm.tm_min tm.tm_sec
in
date, timestamp
type presign_steps = {
canonical_query : string;
canonical_request : string;
string_to_sign : string;
signature : string;
token : string;
}
let presigned_elasticache_token_with_steps
~credentials ~region ~cluster_id ~user_id ~now =
let cluster_id = String.lowercase_ascii cluster_id in
let service = "elasticache" in
let date, timestamp = amz_timestamps now in
let scope =
Printf.sprintf "%s/%s/%s/aws4_request" date region service
in
let credential =
Printf.sprintf "%s/%s" credentials.Iam_credentials.access_key_id scope
in
let base_params =
[ "Action", "connect";
"User", user_id;
"X-Amz-Algorithm", "AWS4-HMAC-SHA256";
"X-Amz-Credential", credential;
"X-Amz-Date", timestamp;
"X-Amz-Expires", "900";
"X-Amz-SignedHeaders", "host";
]
in
let params =
match credentials.session_token with
| None -> base_params
| Some t -> ("X-Amz-Security-Token", t) :: base_params
in
let canonical_query = canonical_query_string params in
let empty_hash = hex_sha256 "" in
let canonical_request =
String.concat "\n"
[ "GET";
"/";
canonical_query;
"host:" ^ cluster_id;
"";
"host";
empty_hash;
]
in
let string_to_sign =
String.concat "\n"
[ "AWS4-HMAC-SHA256";
timestamp;
scope;
hex_sha256 canonical_request;
]
in
let signing_key =
derive_signing_key
~secret_access_key:credentials.Iam_credentials.secret_access_key
~date ~region ~service
in
let signature =
hex_of_string (hmac_sha256 ~key:signing_key string_to_sign)
in
let token =
Printf.sprintf "%s/?%s&X-Amz-Signature=%s"
cluster_id canonical_query signature
in
{ canonical_query; canonical_request; string_to_sign;
signature; token }
let presigned_elasticache_token
~credentials ~region ~cluster_id ~user_id ~now =
(presigned_elasticache_token_with_steps
~credentials ~region ~cluster_id ~user_id ~now).token