Aws Elastic Search Sig4 Подпись Curl Php - PullRequest
0 голосов
/ 22 июня 2019

Я ищу пример кодирования того, как именно подписать эластичный поисковый запрос с помощью php и curl.

Я нашел этот пример о том, как подписать и загрузить файл в корзину S3, и попыталсяприспособить его, но мне не повезло.

Я не могу использовать библиотеку, и огромный класс, который я написал, опирается только на использование raw curl и должен иметь возможность подписывать запрос изнутриКерл класс сам себе.

http://usefulangle.com/post/34/aws-s3-upload-api-php-curl

$aws_access_key_id     = 'ACCESS_ID';
$aws_secret_access_key = 'ACCESS_KEY';

$host = 'HOST';

$aws_region = 'us-east-1';

$content = '{
    "query" : {
        "term" : { "col" : "val" }
    }
}';

// Service name for S3
$aws_service_name = 'es';

// UTC timestamp and date
$timestamp = gmdate('Ymd\THis\Z');
$date      = gmdate('Ymd');

// HTTP request headers as key & value
$request_headers = [];
$request_headers['Content-Type']         = 'application/json';
$request_headers['Content-Length']       = strlen($content);
$request_headers['Date']                 = $timestamp;
$request_headers['Host']                 = $host;
$request_headers['X-Amz-Date']           = $date;
$request_headers['x-amz-content-sha256'] = hash('sha256', $content);

// Sort it in ascending order
ksort($request_headers);

// Canonical headers
$canonical_headers = [];

foreach ($request_headers as $key => $value) 
{
    $canonical_headers[] = strtolower($key) . ":" . $value;
}

$canonical_headers = implode("\n", $canonical_headers);

// Signed headers
$signed_headers = [];

foreach ($request_headers as $key => $value) 
{
    $signed_headers[] = strtolower($key);
}

$signed_headers = implode(";", $signed_headers);

// Cannonical request 
$canonical_request        = [];
$canonical_request[]      = 'GET';
$canonical_request[]      = '';
$canonical_request[]      = $canonical_headers;
$canonical_request[]      = '';
$canonical_request[]      = $signed_headers;
$canonical_request[]      = hash('sha256', $content);
$canonical_request        = implode("\n", $canonical_request);
$hashed_canonical_request = hash('sha256', $canonical_request);

// AWS Scope
$scope = [];
$scope[] = $date;
$scope[] = $aws_region;
$scope[] = $aws_service_name;
$scope[] = 'aws4_request';

// String to sign
$string_to_sign   = [];
$string_to_sign[] = 'AWS4-HMAC-SHA256';
$string_to_sign[] = $timestamp; 
$string_to_sign[] = implode('/', $scope);
$string_to_sign[] = $hashed_canonical_request;
$string_to_sign   = implode("\n", $string_to_sign);

// Signing key
$kSecret  = 'AWS4' . $aws_secret_access_key;
$kDate    = hash_hmac('sha256', $date, $kSecret, true);
$kRegion  = hash_hmac('sha256', $aws_region, $kDate, true);
$kService = hash_hmac('sha256', $aws_service_name, $kRegion, true);
$kSigning = hash_hmac('sha256', 'aws4_request', $kService, true);

// Signature
$signature = hash_hmac('sha256', $string_to_sign, $kSigning);

// Authorization
$authorization = [
    'Credential=' . $aws_access_key_id . '/' . implode('/', $scope),
    'SignedHeaders=' . $signed_headers,
    'Signature=' . $signature
];

$authorization = 'AWS4-HMAC-SHA256' . ' ' . implode( ',', $authorization);

// Curl headers
$curl_headers = [ 'Authorization: ' . $authorization ];

foreach ($request_headers as $key => $value) 
{
    $curl_headers[] = $key . ": " . $value;
}

$url = 'https://' . $host . '/_search';

$ch = curl_init($url);

curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curl_headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_exec($ch);

$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($http_code != 200)
{
    exit('Error : Failed to upload');
}
...