Skip to content

Commit

Permalink
fix aws-global default region
Browse files Browse the repository at this point in the history
  • Loading branch information
DyfanJones authored Jan 3, 2024
2 parents 7b9278a + a11288c commit 3cd53ff
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 21 deletions.
3 changes: 2 additions & 1 deletion paws.common/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# paws.common 0.7.0
* support sse md5 (#718)
* support sse md5 (#718). Thanks to @odysseu for raising issue.
* add pagination StopOnSameToken option (#721) aligns with aws-sdk-js-v3 implementation (https://github.com/aws/aws-sdk-js-v3/releases/tag/v3.78.0). Thanks to @wlandau for raising error in `paginate`.
* tidy up internal function `jmespath_index`
* fix `aws-global` region when resolving endpoint point (#730). Thanks to @atheriel for identifying the issue.

# paws.common 0.6.4
* ensure xml build structure is correctly flattened (#597)
Expand Down
39 changes: 23 additions & 16 deletions paws.common/R/client.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,27 +84,34 @@ resolver_endpoint <- function(service, region, endpoints, sts_regional_endpoint
match <- matches[order(nchar(matches), decreasing = TRUE)][1]
return(match)
}
signing_region <- NULL
if (service == "sts" & nzchar(sts_regional_endpoint)) {
global <- vapply(endpoints, function(x) x$global, FUN.VALUE = logical(1))
endpoint <- endpoints[global][[1]]$endpoint
endpoints[global][[1]]$endpoint <- set_sts_regional_endpoint(
sts_regional_endpoint,
endpoint
)
signing_region <- set_sts_signing_region(sts_regional_endpoint, region)
# locate global endpoint
global_found <- vapply(
endpoints, function(x) if (is.list(x)) x$global else FALSE,
FUN.VALUE = logical(1)
)
global_region <- (region == "aws-global")
if (!any(global_found) & global_region) {
stop("No region provided and no global region found.")
}


e <- endpoints[[get_region_pattern(region, endpoints)]]
search_region <- (
if (any(global_found) & global_region) names(global_found[global_found][1]) else region
)
e <- endpoints[[get_region_pattern(search_region, endpoints)]]
# TODO: Delete old endpoint format handling once all packages are updated.
if (is.character(e)) {
e <- list(endpoint = e, global = FALSE)
}
endpoint <- gsub("{service}", service, e$endpoint, fixed = TRUE)
endpoint <- gsub("{region}", region, endpoint, fixed = TRUE)
if (service == "sts" & nzchar(sts_regional_endpoint)) {
e$endpoint <- set_sts_regional_endpoint(
sts_regional_endpoint, e
)
region <- set_sts_region(sts_regional_endpoint, region)
}
signing_region <- if (e[["global"]]) "us-east-1" else region
endpoint <- gsub("{service}", service, e[["endpoint"]], fixed = TRUE)
endpoint <- gsub("{region}", signing_region, endpoint, fixed = TRUE)
endpoint <- gsub("^(.+://)?", sprintf("%s://", scheme), endpoint)
signing_region <- signing_region %||% ifelse(e$global, "us-east-1", region)

return(list(
endpoint = endpoint,
signing_region = signing_region
Expand All @@ -119,7 +126,7 @@ set_sts_regional_endpoint <- function(sts_regional_endpoint, endpoint) {
)
}

set_sts_signing_region <- function(sts_regional_endpoint, region) {
set_sts_region <- function(sts_regional_endpoint, region) {
switch(sts_regional_endpoint,
"legacy" = "us-east-1",
"regional" = region
Expand Down
7 changes: 3 additions & 4 deletions paws.common/R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -295,17 +295,16 @@ get_region <- function(profile = "") {
if (region != "") {
return(region)
}

# Check if default region is specified
region <- get_env("AWS_DEFAULT_REGION")
if (region != "") {
return(region)
}

region <- check_config_file_region(profile)

if (is.null(region)) stop("No region provided")

if (is.null(region)) {
region <- "aws-global"
}
return(region)
}

Expand Down
43 changes: 43 additions & 0 deletions paws.common/tests/testthat/test_client.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ test_that("resolver_endpoint", {
r <- resolver_endpoint("service", "us-west-2", endpoints)
expect_equal(r$endpoint, "https://us-west.amazonaws.com")
expect_equal(r$signing_region, "us-east-1")

r <- resolver_endpoint("service", "aws-global", endpoints)
expect_equal(r$endpoint, "https://us-west.amazonaws.com")
expect_equal(r$signing_region, "us-east-1")

endpoints <- list(
"*" = list(endpoint = "https://{service}.amazonaws.com", global = TRUE),
"us-east-*" = list(endpoint = "https://{service}.amazonaws.com", global = FALSE),
"us-west-*" = list(endpoint = "https://us-west.amazonaws.com", global = TRUE)
)

r <- resolver_endpoint("service", "aws-global", endpoints)
expect_equal(r$endpoint, "https://service.amazonaws.com")
expect_equal(r$signing_region, "us-east-1")
})

test_that("resolver_endpoint old endpoint format handling", {
Expand All @@ -30,6 +44,35 @@ test_that("resolver_endpoint old endpoint format handling", {
expect_equal(r$signing_region, "region")
})

test_that("resolver_endpoint no region and no global region found", {
endpoints <- list(
"*" = list(endpoint = "https://{service}.amazonaws.com", global = FALSE),
"us-east-*" = list(endpoint = "https://{service}.amazonaws.com", global = FALSE),
"us-west-*" = list(endpoint = "https://us-west.amazonaws.com", global = FALSE)
)

expect_error(
resolver_endpoint("service", "aws-global", endpoints),
"No region provided and no global region found."
)
})

test_that("resolver_endpoint service sts_regional_endpoint", {
endpoints <- list(
"*" = list(endpoint = "https://{service}.amazonaws.com", global = FALSE),
"us-east-*" = list(endpoint = "https://{service}.amazonaws.com", global = FALSE),
"us-west-*" = list(endpoint = "https://us-west.amazonaws.com", global = TRUE)
)

r <- resolver_endpoint("sts", "aws-global", endpoints, "regional")
expect_equal(r$endpoint, "https://sts.us-east-1.amazonaws.com")
expect_equal(r$signing_region, "us-east-1")

r <- resolver_endpoint("sts", "aws-global", endpoints, "legacy")
expect_equal(r$endpoint, "https://sts.amazonaws.com")
expect_equal(r$signing_region, "us-east-1")
})

test_that("client_config uses custom endpoint", {
Sys.setenv("AWS_REGION" = "region")
cfgs <- Config()
Expand Down

0 comments on commit 3cd53ff

Please sign in to comment.