Go to the documentation of this file.
25 #include "absl/memory/memory.h"
26 #include "absl/status/status.h"
27 #include "absl/status/statusor.h"
28 #include "absl/strings/str_cat.h"
29 #include "absl/strings/str_format.h"
30 #include "absl/strings/str_replace.h"
31 #include "absl/strings/string_view.h"
51 const char* kExpectedEnvironmentId =
"aws1";
53 const char* kRegionEnvVar =
"AWS_REGION";
54 const char* kDefaultRegionEnvVar =
"AWS_DEFAULT_REGION";
55 const char* kAccessKeyIdEnvVar =
"AWS_ACCESS_KEY_ID";
56 const char* kSecretAccessKeyEnvVar =
"AWS_SECRET_ACCESS_KEY";
57 const char* kSessionTokenEnvVar =
"AWS_SESSION_TOKEN";
60 const char*
hex =
"0123456789ABCDEF";
64 if ((c >=
'0' && c <=
'9') || (c >=
'A' && c <=
'Z') ||
65 (c >=
'a' && c <=
'z') || c ==
'-' || c ==
'_' || c ==
'!' ||
66 c ==
'\'' || c ==
'(' || c ==
')' || c ==
'*' || c ==
'~' || c ==
'.') {
70 result.push_back(hex[
static_cast<unsigned char>(c) >> 4]);
71 result.push_back(hex[
static_cast<unsigned char>(c) & 15]);
79 RefCountedPtr<AwsExternalAccountCredentials>
81 std::vector<std::string> scopes,
83 auto creds = MakeRefCounted<AwsExternalAccountCredentials>(
96 auto it =
options.credential_source.object_value().find(
"environment_id");
97 if (
it ==
options.credential_source.object_value().end()) {
99 "environment_id field not present.");
104 "environment_id field must be a string.");
107 if (
it->second.string_value() != kExpectedEnvironmentId) {
112 it =
options.credential_source.object_value().find(
"region_url");
113 if (
it ==
options.credential_source.object_value().end()) {
120 "region_url field must be a string.");
124 it =
options.credential_source.object_value().find(
"url");
125 if (
it !=
options.credential_source.object_value().end() &&
127 url_ =
it->second.string_value();
129 it =
options.credential_source.object_value().find(
130 "regional_cred_verification_url");
131 if (
it ==
options.credential_source.object_value().end()) {
133 "regional_cred_verification_url field not present.");
138 "regional_cred_verification_url field must be a string.");
143 options.credential_source.object_value().find(
"imdsv2_session_token_url");
144 if (
it !=
options.credential_source.object_value().end() &&
153 if (
ctx ==
nullptr) {
157 "Missing HTTPRequestContext to start subject token retrieval."));
164 }
else if (
signer_ !=
nullptr) {
178 headers[0].
key =
gpr_strdup(
"x-aws-ec2-metadata-token-ttl-seconds");
189 if (uri->scheme() ==
"http") {
241 if (region_from_env ==
nullptr) {
244 if (region_from_env !=
nullptr) {
257 "Invalid region url. %s", uri.
status().ToString())));
267 if (uri->scheme() ==
"http") {
321 if (uri->scheme() ==
"http") {
357 if (access_key_id_from_env !=
nullptr &&
358 secret_access_key_from_env !=
nullptr && token_from_env !=
nullptr) {
368 "Missing role name when retrieving signing keys."));
376 "Invalid url with role name: %s.", uri.
status().ToString())));
387 if (uri->scheme() ==
"http") {
420 "Invalid retrieve signing keys response.", &
error, 1));
431 "Missing or invalid AccessKeyId in %s.", response_body)));
441 "Missing or invalid SecretAccessKey in %s.", response_body)));
451 "Missing or invalid Token in %s.", response_body)));
462 signer_ = absl::make_unique<AwsRequestSigner>(
465 std::map<std::string, std::string>(), &
error);
469 "Creating aws request signer failed.", &
error, 1));
474 auto signed_headers =
signer_->GetSignedRequestHeaders();
478 "Invalid getting signed request"
486 headers.push_back(
Json(
487 {{
"key",
"Authorization"}, {
"value", signed_headers[
"Authorization"]}}));
488 headers.push_back(
Json({{
"key",
"host"}, {
"value", signed_headers[
"host"]}}));
490 Json({{
"key",
"x-amz-date"}, {
"value", signed_headers[
"x-amz-date"]}}));
491 headers.push_back(
Json({{
"key",
"x-amz-security-token"},
492 {
"value", signed_headers[
"x-amz-security-token"]}}));
494 Json({{
"key",
"x-goog-cloud-target-resource"}, {
"value",
audience_}}));
496 {
"method",
Json(
"POST")},
497 {
"headers",
Json(headers)}};
498 Json subject_token_json(
object);
std::vector< Json > Array
#define GRPC_CLOSURE_INIT(closure, cb, cb_arg, scheduler)
OrphanablePtr< HttpRequest > http_request_
std::unique_ptr< AwsRequestSigner > signer_
HTTPRequestContext * ctx_
std::string StrCat(const AlphaNum &a, const AlphaNum &b)
return memset(p, 0, total)
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
void RetrieveSubjectToken(HTTPRequestContext *ctx, const Options &options, std::function< void(std::string, grpc_error_handle)> cb) override
grpc_polling_entity * pollent
void OnRetrieveImdsV2SessionTokenInternal(grpc_error_handle error)
const Object & object_value() const
GPRAPI void * gpr_malloc(size_t size)
#define GRPC_ERROR_CREATE_REFERENCING_FROM_STATIC_STRING(desc, errs, count)
static struct test_ctx ctx
void RetrieveImdsV2SessionToken()
RefCountedPtr< grpc_channel_credentials > CreateHttpRequestSSLCredentials()
static void OnRetrieveRegion(void *arg, grpc_error_handle error)
static absl::StatusOr< URI > Parse(absl::string_view uri_text)
void OnRetrieveRoleNameInternal(grpc_error_handle error)
void RetrieveSigningKeys()
static RefCountedPtr< AwsExternalAccountCredentials > Create(Options options, std::vector< std::string > scopes, grpc_error_handle *error)
void grpc_http_request_destroy(grpc_http_request *request)
std::string regional_cred_verification_url_
char * gpr_getenv(const char *name)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
constexpr size_type size() const noexcept
JSON (JavaScript Object Notation).
static void OnRetrieveRoleName(void *arg, grpc_error_handle error)
GRPCAPI grpc_channel_credentials * grpc_insecure_credentials_create()
void grpc_http_response_destroy(grpc_http_response *response)
void OnRetrieveSigningKeysInternal(grpc_error_handle error)
void FinishRetrieveSubjectToken(std::string subject_token, grpc_error_handle error)
void OnRetrieveRegionInternal(grpc_error_handle error)
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
#define GRPC_ERROR_CREATE_FROM_STATIC_STRING(desc)
static Json Parse(absl::string_view json_str, grpc_error_handle *error)
std::string imdsv2_session_token_
grpc_http_response response
std::map< std::string, Json > Object
ABSL_MUST_USE_RESULT bool ok() const
static void OnRetrieveImdsV2SessionToken(void *arg, grpc_error_handle error)
#define GRPC_ERROR_REF(err)
static OrphanablePtr< HttpRequest > Get(URI uri, const grpc_channel_args *args, grpc_polling_entity *pollent, const grpc_http_request *request, Timestamp deadline, grpc_closure *on_done, grpc_http_response *response, RefCountedPtr< grpc_channel_credentials > channel_creds) GRPC_MUST_USE_RESULT
std::string imdsv2_session_token_url_
std::function< void(std::string, grpc_error_handle)> cb_
AwsExternalAccountCredentials(Options options, std::vector< std::string > scopes, grpc_error_handle *error)
#define GRPC_ERROR_CREATE_FROM_CPP_STRING(desc)
std::string StrReplaceAll(absl::string_view s, strings_internal::FixedMapping replacements)
#define GRPC_ERROR_UNREF(err)
std::string secret_access_key_
static void OnRetrieveSigningKeys(void *arg, grpc_error_handle error)
GPRAPI char * gpr_strdup(const char *src)
void AddMetadataRequestHeaders(grpc_http_request *request)
std::string cred_verification_url_
static OrphanablePtr< HttpRequest > Put(URI uri, const grpc_channel_args *args, grpc_polling_entity *pollent, const grpc_http_request *request, Timestamp deadline, grpc_closure *on_done, grpc_http_response *response, RefCountedPtr< grpc_channel_credentials > channel_creds) GRPC_MUST_USE_RESULT
constexpr string_view substr(size_type pos=0, size_type n=npos) const
std::string access_key_id_
std::string Dump(int indent=0) const
OPENSSL_EXPORT pem_password_cb * cb
const Status & status() const &
#define GRPC_ERROR_IS_NONE(err)
grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:45