diff --git a/src/aws/flb_aws_util.c b/src/aws/flb_aws_util.c index a0509c8ce54..ef6db9ec375 100644 --- a/src/aws/flb_aws_util.c +++ b/src/aws/flb_aws_util.c @@ -33,8 +33,10 @@ #include #include -#define AWS_SERVICE_ENDPOINT_FORMAT "%s.%s.amazonaws.com" -#define AWS_SERVICE_ENDPOINT_BASE_LEN 15 +#define AWS_SERVICE_ENDPOINT_FORMAT "%s.%s%s" +#define AWS_SERVICE_ENDPOINT_SUFFIX_COM ".amazonaws.com" +#define AWS_SERVICE_ENDPOINT_SUFFIX_COM_CN ".amazonaws.com.cn" +#define AWS_SERVICE_ENDPOINT_SUFFIX_EU ".amazonaws.eu" #define TAG_PART_DESCRIPTOR "$TAG[%d]" #define TAG_DESCRIPTOR "$TAG" @@ -71,29 +73,30 @@ struct flb_http_client *request_do(struct flb_aws_client *aws_client, size_t dynamic_headers_len); /* - * https://service.region.amazonaws.com(.cn) + * https://service.region.amazonaws.[com(.cn)|eu] */ char *flb_aws_endpoint(char* service, char* region) { char *endpoint = NULL; - size_t len = AWS_SERVICE_ENDPOINT_BASE_LEN; - int is_cn = FLB_FALSE; + const char *domain_suffix = AWS_SERVICE_ENDPOINT_SUFFIX_COM; + size_t len; int bytes; - /* In the China regions, ".cn" is appended to the URL */ - if (strcmp("cn-north-1", region) == 0) { - len += 3; - is_cn = FLB_TRUE; + /* China regions end with amazonaws.com.cn */ + if (strcmp("cn-north-1", region) == 0 || + strcmp("cn-northwest-1", region) == 0) { + domain_suffix = AWS_SERVICE_ENDPOINT_SUFFIX_COM_CN; } - if (strcmp("cn-northwest-1", region) == 0) { - len += 3; - is_cn = FLB_TRUE; + else if (strncmp(region, "eusc-", 5) == 0) { + domain_suffix = AWS_SERVICE_ENDPOINT_SUFFIX_EU; } - len += strlen(service); + len = strlen(service); + len += 1; /* dot between service and region */ len += strlen(region); - len++; /* null byte */ + len += strlen(domain_suffix); + len += 1; /* null byte */ endpoint = flb_calloc(len, sizeof(char)); if (!endpoint) { @@ -101,18 +104,13 @@ char *flb_aws_endpoint(char* service, char* region) return NULL; } - bytes = snprintf(endpoint, len, AWS_SERVICE_ENDPOINT_FORMAT, service, region); - if (bytes < 0) { + bytes = snprintf(endpoint, len, AWS_SERVICE_ENDPOINT_FORMAT, service, region, domain_suffix); + if (bytes < 0 || bytes >= len) { flb_errno(); flb_free(endpoint); return NULL; } - if (is_cn) { - memcpy(endpoint + bytes, ".cn", 3); - endpoint[bytes + 3] = '\0'; - } - return endpoint; } diff --git a/tests/internal/aws_util.c b/tests/internal/aws_util.c index 6f6fbdb1aca..8132a5c6d59 100644 --- a/tests/internal/aws_util.c +++ b/tests/internal/aws_util.c @@ -163,6 +163,13 @@ static void test_flb_aws_endpoint() endpoint) == 0); flb_free(endpoint); + /* EU Sovereign Cloud regions have a different domain */ + endpoint = flb_aws_endpoint("cloudwatch", "eusc-de-east-1"); + + TEST_CHECK(strcmp("cloudwatch.eusc-de-east-1.amazonaws.eu", + endpoint) == 0); + flb_free(endpoint); + } static void test_flb_get_s3_key_multi_tag_exists()