File inc/wh/amazon.inc.php changed (mode: 100644) (index 5c4c32c..749260e) |
... |
... |
function rg_amazon_auth($a) |
11 |
11 |
|
|
12 |
12 |
$ret = array('ok' => 0); |
$ret = array('ok' => 0); |
13 |
13 |
while (1) { |
while (1) { |
|
14 |
|
if (!isset($a['service'])) |
|
15 |
|
$service = ''; |
|
16 |
|
else |
|
17 |
|
$service = $a['service']; |
|
18 |
|
|
|
19 |
|
// Must be ordered! |
|
20 |
|
$ret['query_string'] = ''; |
|
21 |
|
if (!isset($a['query_string'])) { |
|
22 |
|
$ret['query_string'] = ''; |
|
23 |
|
} else if (is_array($a['query_string'])) { |
|
24 |
|
ksort($a['query_string']); |
|
25 |
|
$add = ''; |
|
26 |
|
foreach ($a['query_string'] as $k => $v) { |
|
27 |
|
$ret['query_string'] .= $add |
|
28 |
|
. urlencode($k) . '=' |
|
29 |
|
. urlencode($v); |
|
30 |
|
$add = '&'; |
|
31 |
|
} |
|
32 |
|
} else { |
|
33 |
|
$ret['query_string'] = $a['query_string']; |
|
34 |
|
} |
|
35 |
|
|
|
36 |
|
if (strcmp($a['method'], 'GET') == 0) { |
|
37 |
|
$qs = $ret['query_string']; |
|
38 |
|
$ret['query_string'] = ''; |
|
39 |
|
} else { |
|
40 |
|
$qs = ''; |
|
41 |
|
} |
|
42 |
|
|
|
43 |
|
rg_log('DEBUG: ret[query_string]=' . $ret['query_string']); |
|
44 |
|
|
|
45 |
|
if (!empty($a['content'])) { |
|
46 |
|
$a['x-amz-content-sha256'] = hash('sha256', $a['content'], TRUE); |
|
47 |
|
rg_log('DEBUG: a[content] is set to: ' . $a['content']); |
|
48 |
|
$a['iheaders']['x-amz-content-sha256'] = |
|
49 |
|
bin2hex($a['x-amz-content-sha256']); |
|
50 |
|
} else { |
|
51 |
|
$a['x-amz-content-sha256'] = hash('sha256', |
|
52 |
|
$ret['query_string'], TRUE); |
|
53 |
|
rg_log('DEBUG: a[content] is not set'); |
|
54 |
|
} |
|
55 |
|
rg_log_ml('DEBUG: a[iheaders]: ' . print_r($a['iheaders'], TRUE)); |
|
56 |
|
|
14 |
57 |
$iheaders_final = array(); |
$iheaders_final = array(); |
15 |
58 |
$iheaders_list = array(); |
$iheaders_list = array(); |
16 |
59 |
foreach ($a['iheaders'] as $head => $val) { |
foreach ($a['iheaders'] as $head => $val) { |
|
... |
... |
function rg_amazon_auth($a) |
23 |
66 |
$iheaders_list = implode(';', $iheaders_list); |
$iheaders_list = implode(';', $iheaders_list); |
24 |
67 |
$iheaders_final = implode("\n", $iheaders_final) . "\n\n"; |
$iheaders_final = implode("\n", $iheaders_final) . "\n\n"; |
25 |
68 |
|
|
|
69 |
|
$ret['iheaders'] = $a['iheaders']; |
|
70 |
|
|
26 |
71 |
$canonical_request = $a['method'] . "\n" |
$canonical_request = $a['method'] . "\n" |
27 |
|
. '/' . urlencode($a['file']) . "\n" |
|
28 |
|
. "\n" |
|
|
72 |
|
. $a['url'] . urlencode($a['file']) . "\n" |
|
73 |
|
. $qs . "\n" |
29 |
74 |
. $iheaders_final |
. $iheaders_final |
30 |
75 |
. $iheaders_list . "\n" |
. $iheaders_list . "\n" |
31 |
76 |
. bin2hex($a['x-amz-content-sha256']); |
. bin2hex($a['x-amz-content-sha256']); |
32 |
|
rg_log_ml('canonical_request:' . "\n" . $canonical_request |
|
|
77 |
|
rg_log_ml('DEBUG: canonical_request:' . "\n" . $canonical_request |
33 |
78 |
. "\n" . '==='); |
. "\n" . '==='); |
34 |
79 |
|
|
35 |
80 |
$string_to_sign = 'AWS4-HMAC-SHA256' . "\n" |
$string_to_sign = 'AWS4-HMAC-SHA256' . "\n" |
36 |
81 |
. $a['x-amz-date'] . "\n" |
. $a['x-amz-date'] . "\n" |
37 |
82 |
. gmdate('Ymd', $a['ts']) |
. gmdate('Ymd', $a['ts']) |
38 |
83 |
. '/' . $a['region'] |
. '/' . $a['region'] |
39 |
|
. '/' . $a['service'] |
|
|
84 |
|
. '/' . $service |
40 |
85 |
. '/' . 'aws4_request' |
. '/' . 'aws4_request' |
41 |
86 |
. "\n" |
. "\n" |
42 |
87 |
. hash('sha256', $canonical_request); |
. hash('sha256', $canonical_request); |
43 |
88 |
rg_log_ml('string_to_sign:' . "\n" . $string_to_sign |
rg_log_ml('string_to_sign:' . "\n" . $string_to_sign |
44 |
89 |
. "\n" . '==='); |
. "\n" . '==='); |
45 |
90 |
|
|
|
91 |
|
rg_log('DEBUG: secret_access_key=' . $a['secret_access_key'] . '.'); |
46 |
92 |
$date_key = hash_hmac('sha256', gmdate('Ymd', $a['ts']), |
$date_key = hash_hmac('sha256', gmdate('Ymd', $a['ts']), |
47 |
93 |
'AWS4' . $a['secret_access_key'], TRUE); |
'AWS4' . $a['secret_access_key'], TRUE); |
|
94 |
|
rg_log('DEBUG: date_key=' . bin2hex($date_key)); |
48 |
95 |
$date_region_key = hash_hmac('sha256', $a['region'], |
$date_region_key = hash_hmac('sha256', $a['region'], |
49 |
96 |
$date_key, TRUE); |
$date_key, TRUE); |
|
97 |
|
rg_log('DEBUG: date_region_key=' . bin2hex($date_region_key)); |
50 |
98 |
$date_region_service_key = hash_hmac('sha256', |
$date_region_service_key = hash_hmac('sha256', |
51 |
|
$a['service'], $date_region_key, TRUE); |
|
|
99 |
|
$service, $date_region_key, TRUE); |
|
100 |
|
rg_log('DEBUG: service=' . $service . ' date_region_service_key=' . bin2hex($date_region_service_key)); |
52 |
101 |
$signing_key = hash_hmac('sha256', 'aws4_request', |
$signing_key = hash_hmac('sha256', 'aws4_request', |
53 |
102 |
$date_region_service_key, TRUE); |
$date_region_service_key, TRUE); |
|
103 |
|
rg_log('DEBUG: signing_key=' . bin2hex($signing_key)); |
54 |
104 |
$signature = hash_hmac('sha256', $string_to_sign, $signing_key); |
$signature = hash_hmac('sha256', $string_to_sign, $signing_key); |
55 |
105 |
rg_log('DEBUG: signature=' . $signature); |
rg_log('DEBUG: signature=' . $signature); |
56 |
106 |
|
|
57 |
107 |
$cred = $a['access_key_id'] |
$cred = $a['access_key_id'] |
58 |
|
. '/' . gmdate('Ymd') |
|
|
108 |
|
. '/' . gmdate('Ymd', $a['ts']) |
59 |
109 |
. '/' . $a['region'] |
. '/' . $a['region'] |
60 |
|
. '/' . $a['service'] |
|
|
110 |
|
. '/' . $service |
61 |
111 |
. '/aws4_request'; |
. '/aws4_request'; |
62 |
112 |
|
|
63 |
113 |
$ret['data'] = 'AWS4-HMAC-SHA256' |
$ret['data'] = 'AWS4-HMAC-SHA256' |
|
... |
... |
function rg_amazon_auth($a) |
74 |
124 |
} |
} |
75 |
125 |
|
|
76 |
126 |
/* |
/* |
77 |
|
* Make a generic request to the amazon |
|
|
127 |
|
* Generic curl helper |
78 |
128 |
*/ |
*/ |
79 |
|
function rg_amazon_req($a) |
|
|
129 |
|
function rg_amazon_curl($url, $method, $headers, $data) |
80 |
130 |
{ |
{ |
81 |
|
rg_prof_start('amazon_req'); |
|
82 |
|
rg_log_enter('amazon_req'); |
|
83 |
|
rg_log_ml('DEBUG: a:' . print_r($a, TRUE)); |
|
|
131 |
|
rg_log_ml('amazon_curl: data: START' . "\n" |
|
132 |
|
. print_r($data, TRUE) . "\n" . 'END'); |
84 |
133 |
|
|
85 |
134 |
$ret = array('ok' => 0); |
$ret = array('ok' => 0); |
86 |
135 |
while (1) { |
while (1) { |
87 |
|
$url = 'https://' . $a['host'] . '/' . urlencode($a['file']); |
|
|
136 |
|
if (strcmp($method, 'GET') == 0) { |
|
137 |
|
if (!strstr($url, '?')) |
|
138 |
|
$url .= '?'; |
|
139 |
|
$url .= $data; |
|
140 |
|
} |
|
141 |
|
|
88 |
142 |
$c = curl_init($url); |
$c = curl_init($url); |
89 |
143 |
if ($c === FALSE) { |
if ($c === FALSE) { |
90 |
144 |
$ret['error'] = 'cannot init curl'; |
$ret['error'] = 'cannot init curl'; |
91 |
145 |
break; |
break; |
92 |
146 |
} |
} |
93 |
147 |
|
|
94 |
|
$a['region'] = trim(strtolower($a['region'])); |
|
95 |
|
|
|
96 |
|
$a['ts'] = time(); |
|
97 |
|
$a['x-amz-date'] = gmdate('Ymd', $a['ts']) |
|
98 |
|
. 'T' . gmdate('His', $a['ts']) . 'Z'; |
|
99 |
|
$a['x-amz-content-sha256'] = hash('sha256', $a['content'], TRUE); |
|
100 |
|
$a['host'] = trim(strtolower($a['host'])); |
|
101 |
|
$a['service'] = trim(strtolower($a['service'])); |
|
102 |
|
|
|
103 |
|
if (!isset($a['iheaders'])) |
|
104 |
|
$a['iheaders'] = array(); |
|
105 |
|
$a['iheaders']['Host'] = $a['host']; |
|
106 |
|
$a['iheaders']['x-amz-date'] = $a['x-amz-date']; |
|
107 |
|
$a['iheaders']['x-amz-content-sha256'] = bin2hex($a['x-amz-content-sha256']); |
|
108 |
|
|
|
109 |
|
$auth = rg_amazon_auth($a); |
|
110 |
|
if ($auth['ok'] != 1) { |
|
111 |
|
$ret['error'] = $auth['error']; |
|
112 |
|
break; |
|
|
148 |
|
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $method); |
|
149 |
|
if (strcmp($method, 'GET') != 0) { |
|
150 |
|
curl_setopt($c, CURLOPT_POST, 1); |
|
151 |
|
curl_setopt($c, CURLOPT_POSTFIELDS, $data); |
113 |
152 |
} |
} |
114 |
|
|
|
115 |
|
$headers = array(); |
|
116 |
|
if (isset($a['content_type'])) |
|
117 |
|
$headers[] = 'Content-Type: ' . $a['content_type']; |
|
118 |
|
$headers[] = 'Authorization: ' . $auth['data']; |
|
119 |
|
foreach ($a['iheaders'] as $head => $val) |
|
120 |
|
$headers[] = $head . ': ' . $val; |
|
121 |
|
//rg_log_ml('HEADERS:' . print_r($headers, TRUE)); |
|
122 |
|
|
|
123 |
|
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $a['method']); |
|
124 |
|
//curl_setopt($c, CURLOPT_POST, 1); |
|
125 |
|
curl_setopt($c, CURLOPT_POSTFIELDS, $a['content']); |
|
126 |
153 |
curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE); |
curl_setopt($c, CURLOPT_RETURNTRANSFER, TRUE); |
127 |
154 |
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); |
curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1); |
128 |
155 |
//curl_setopt($c, CURLOPT_HEADER, 1); |
//curl_setopt($c, CURLOPT_HEADER, 1); |
|
... |
... |
function rg_amazon_req($a) |
172 |
199 |
} |
} |
173 |
200 |
curl_close($c); |
curl_close($c); |
174 |
201 |
|
|
|
202 |
|
return $ret; |
|
203 |
|
} |
|
204 |
|
|
|
205 |
|
/* |
|
206 |
|
* Make a generic request to the amazon |
|
207 |
|
*/ |
|
208 |
|
function rg_amazon_req($a) |
|
209 |
|
{ |
|
210 |
|
rg_prof_start('amazon_req'); |
|
211 |
|
rg_log_enter('amazon_req'); |
|
212 |
|
rg_log_ml('DEBUG: a:' . print_r($a, TRUE)); |
|
213 |
|
|
|
214 |
|
$ret = array('ok' => 0); |
|
215 |
|
while (1) { |
|
216 |
|
if (!isset($a['url'])) |
|
217 |
|
$a['url'] = '/'; |
|
218 |
|
if (!isset($a['file'])) |
|
219 |
|
$a['file'] = ''; |
|
220 |
|
if (!empty($a['file'])) |
|
221 |
|
$a['file'] = $a['file']; |
|
222 |
|
|
|
223 |
|
$url = 'https://' . $a['host'] . $a['url'] . $a['file']; |
|
224 |
|
|
|
225 |
|
$a['region'] = trim(strtolower($a['region'])); |
|
226 |
|
|
|
227 |
|
if (!isset($a['ts'])) |
|
228 |
|
$a['ts'] = time(); |
|
229 |
|
$a['x-amz-date'] = gmdate('Ymd', $a['ts']) |
|
230 |
|
. 'T' . gmdate('His', $a['ts']) . 'Z'; |
|
231 |
|
$a['host'] = trim(strtolower($a['host'])); |
|
232 |
|
$a['service'] = trim(strtolower($a['service'])); |
|
233 |
|
|
|
234 |
|
if (!isset($a['iheaders'])) |
|
235 |
|
$a['iheaders'] = array(); |
|
236 |
|
$a['iheaders']['Host'] = $a['host']; |
|
237 |
|
$a['iheaders']['x-amz-date'] = $a['x-amz-date']; |
|
238 |
|
|
|
239 |
|
$auth = rg_amazon_auth($a); |
|
240 |
|
if ($auth['ok'] != 1) { |
|
241 |
|
$ret['error'] = $auth['error']; |
|
242 |
|
break; |
|
243 |
|
} |
|
244 |
|
|
|
245 |
|
$headers = array(); |
|
246 |
|
if (isset($a['content_type'])) |
|
247 |
|
$headers[] = 'Content-Type: ' . $a['content_type']; |
|
248 |
|
$headers[] = 'Authorization: ' . $auth['data']; |
|
249 |
|
foreach ($auth['iheaders'] as $head => $val) |
|
250 |
|
$headers[] = $head . ': ' . $val; |
|
251 |
|
rg_log_ml('DEBUG: HEADERS:' . print_r($headers, TRUE)); |
|
252 |
|
|
|
253 |
|
$ret = rg_amazon_curl($url, $a['method'], $headers, |
|
254 |
|
$a['content']); |
|
255 |
|
break; |
|
256 |
|
} |
|
257 |
|
|
175 |
258 |
rg_log_exit(); |
rg_log_exit(); |
176 |
259 |
rg_prof_end('amazon_req'); |
rg_prof_end('amazon_req'); |
177 |
260 |
return $ret; |
return $ret; |
|
... |
... |
function rg_amazon_codedeploy_create($a) |
237 |
320 |
while (1) { |
while (1) { |
238 |
321 |
$a['service'] = 'codedeploy'; |
$a['service'] = 'codedeploy'; |
239 |
322 |
$a['method'] = 'POST'; |
$a['method'] = 'POST'; |
240 |
|
$a['file'] = ''; |
|
241 |
323 |
$a['host'] = $a['service'] . '.' . $a['region'] . '.amazonaws.com'; |
$a['host'] = $a['service'] . '.' . $a['region'] . '.amazonaws.com'; |
242 |
324 |
$a['iheaders'] = array('x-amz-target' => 'CodeDeploy_20141006.CreateDeployment'); |
$a['iheaders'] = array('x-amz-target' => 'CodeDeploy_20141006.CreateDeployment'); |
243 |
325 |
$a['content'] = $json; |
$a['content'] = $json; |
|
... |
... |
function rg_amazon_lambda_invoke($a) |
268 |
350 |
while (1) { |
while (1) { |
269 |
351 |
$a['service'] = 'lambda'; |
$a['service'] = 'lambda'; |
270 |
352 |
$a['method'] = 'POST'; |
$a['method'] = 'POST'; |
271 |
|
$a['file'] = '2015-03-31/functions/' . $a['function'] |
|
|
353 |
|
$a['url'] = '/2015-03-31/functions/' . $a['function'] |
272 |
354 |
. '/invocations'; |
. '/invocations'; |
273 |
355 |
$a['host'] = $a['service'] . '.' . $a['region'] . '.amazonaws.com'; |
$a['host'] = $a['service'] . '.' . $a['region'] . '.amazonaws.com'; |
274 |
356 |
$a['iheaders'] = array( |
$a['iheaders'] = array( |
275 |
357 |
'x-Amz-Client-Context' => '', |
'x-Amz-Client-Context' => '', |
276 |
358 |
'x-Amz-Invocation-Type' => $a['invocation_type'], |
'x-Amz-Invocation-Type' => $a['invocation_type'], |
277 |
359 |
'x-Amz-Log-Type' => 'Tail'); |
'x-Amz-Log-Type' => 'Tail'); |
|
360 |
|
$a['content'] = $a['payload']; |
278 |
361 |
|
|
279 |
362 |
$ret = rg_amazon_req($a); |
$ret = rg_amazon_req($a); |
280 |
363 |
break; |
break; |