Skip to content

Commit

Permalink
support global endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
DyfanJones committed Mar 19, 2024
2 parents d6e9720 + 1c363d1 commit d5c39f8
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 40 deletions.
9 changes: 8 additions & 1 deletion docs/credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,10 @@ Paws supports the following settings in environment variables.
* `AWS_SHARED_CREDENTIALS_FILES` - Specifies the location of the file used to
store access keys. The default path is `~/.aws/credentials`.
* `AWS_ENDPOINT_URL`- Set global endpoint URL.
* `AWS_ENDPOINT_URL_<SERVICE>` - [Service-specific endpoints: List of service-specific identifiers](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-endpoints.html#endpoints-service-specific-table)
---
Expand Down Expand Up @@ -620,4 +624,7 @@ Paws supports the following settings in the AWS config file.
the SDK can use to assume a role that you specified with the `role_arn`
parameter. You cannot specify both `source_profile` and `credential_source`
in the same profile.
* `endpoint_url` - Global endpoint for all AWS Services [Use endpoints](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-endpoints.html#endpoints-service-specific-config)
* `services` - Service-specific endpoint configuration provides the option to use a persistent endpoint of your choosing [set service-specific endpoints](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-endpoints.html#endpoints-service-specific). [Service-specific endpoints: List of service-specific identifiers](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-endpoints.html#endpoints-service-specific-table).
3 changes: 2 additions & 1 deletion paws.common/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# paws.common 0.7.1.9000
* improve performance of `restxml_unmarshal` by x3
* fix `rest_unmarshal_location_elements` only skip header if location is not found (#761)
* fix `rest_unmarshal_location_elements` only skip header if location is not found (#761)
* support global `endpoint_url` in config file and environmental variables (#764), thanks to @James-G-Hill for raising issue

# paws.common 0.7.1
* minor performance enhancements
Expand Down
60 changes: 27 additions & 33 deletions paws.common/R/config.R
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,24 @@ get_credentials_file_path <- function() {
return(NULL)
}

get_env <- function(variable) {
value <- Sys.getenv(variable)
if (value != "") {
return(value)
}

value <- get_os_env(variable)
if (value != "") {
get_env <- function(variable, default_variable = NULL) {
if (is.null(default_variable)) {
if ((value <- Sys.getenv(variable)) != "") {
return(value)
}
return(get_os_env(variable))
} else {
if ((value <- Sys.getenv(variable)) == "") {
value <- Sys.getenv(default_variable)
}
if (value != "") {
return(value)
}
if ((value <- get_os_env(variable)) == "") {
value <- get_os_env(default_variable)
}
return(value)
}

return("")
}

# Get the name of the IAM role from the instance metadata.
Expand Down Expand Up @@ -249,7 +255,6 @@ get_os_env <- function(var) {
} else {
value <- "" # Not implemented on Windows.
}

return(value)
}

Expand All @@ -258,11 +263,7 @@ get_profile_name <- function(profile = "") {
if (!is.null(profile) && profile != "") {
return(profile)
}

profile <- get_env("AWS_PROFILE")

if (profile == "") profile <- "default"

if ((profile <- get_env("AWS_PROFILE")) == "") profile <- "default"
return(profile)
}

Expand Down Expand Up @@ -313,7 +314,10 @@ get_region <- function(profile = "") {
# https://docs.aws.amazon.com/sdkref/latest/guide/feature-ss-endpoints.html#ss-endpoints-envar
get_service_endpoint <- function(profile = "", service_id = "") {
service_id <- gsub(" ", "_", service_id)
endpoint <- get_env(paste0("AWS_ENDPOINT_URL_", toupper(service_id)))
endpoint <- get_env(
paste0("AWS_ENDPOINT_URL_", toupper(service_id)),
"AWS_ENDPOINT_URL"
)
if (endpoint != "") {
return(endpoint)
}
Expand All @@ -327,29 +331,20 @@ check_config_file_endpoint <- function(profile = "", service_id = "") {
if (is.null(config_path)) {
return(NULL)
}

profile_name <- get_profile_name(profile)
if (profile_name != "default") profile_name <- paste("profile", profile_name)

config_values <- read_ini(config_path)

if (is.null(config_values[[profile_name]])) {
if (is.null(profile <- config_values[[profile_name]])) {
return(NULL)
}

profile <- config_values[[profile_name]]

if (!("services" %in% names(profile))) {
return(NULL)
if (is.null(service_name <- profile[["services"]])) {
return(profile[["endpoint_url"]])
}

service_name <- profile[["services"]]
profile_service <- config_values[[paste("services", service_name)]][[service_id]]
if (is.null(profile_service)) {
return(NULL)
return(profile[["endpoint_url"]])
}

endpoint <- profile_service[["endpoint_url"]]
endpoint <- profile_service[["endpoint_url"]] %||% profile[["endpoint_url"]]
return(endpoint)
}

Expand Down Expand Up @@ -399,8 +394,7 @@ get_web_identity_token <- function(web_identity_token_file = "") {

# Check if sts_regional_endpoint is present in config file
check_config_file_sts_regional_endpoint <- function(profile = "") {
config_path <- get_config_file_path()
if (is.null(config_path)) {
if (is.null(config_path <- get_config_file_path())) {
return(NULL)
}

Expand Down
11 changes: 11 additions & 0 deletions paws.common/tests/testthat/data_ini
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,14 @@ services = profileminio
s3 =
endpoint_url = http://localhost:9000
# Empty comment

[profile localstack]
region=us-east-1
output=json
endpoint_url = http://localhost:1234
services = profilelocalstack

[services profilelocalstack]
s3 =
endpoint_url = http://localhost:9000
ec2 =
17 changes: 12 additions & 5 deletions paws.common/tests/testthat/test_service.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,15 @@ test_that("test custom config credentials take priority", {
})

test_that("test service endpoint config file with service present", {
mock_get_config_file_path <- mock2("data_ini")
mock_get_config_file_path <- mock2("data_ini", cycle = T)
mockery::stub(check_config_file_endpoint, "get_config_file_path", mock_get_config_file_path)

endpoint <- check_config_file_endpoint("minio", "s3")
expect_equal(endpoint, "http://localhost:9000")
s3_endpoint <- check_config_file_endpoint("localstack", "s3")
ec2_endpoint <- check_config_file_endpoint("localstack", "ec2")
bob_endpoint <- check_config_file_endpoint("localstack", "bob")
expect_equal(s3_endpoint, "http://localhost:9000")
expect_equal(ec2_endpoint, "http://localhost:1234")
expect_equal(bob_endpoint, "http://localhost:1234")
})

test_that("test service endpoint config file with service not present", {
Expand All @@ -134,14 +138,17 @@ test_that("test service endpoint config file with service not present", {
test_that("test service endpoint environment variables", {
Sys.setenv(
"AWS_ENDPOINT_URL_BAR" = "http://localhost:9000",
"AWS_ENDPOINT_URL_BAZ_CHO" = "http://localhost:9090"
"AWS_ENDPOINT_URL_BAZ_CHO" = "http://localhost:9090",
"AWS_ENDPOINT_URL" = "http://localhost:1234"
)

endpoint1 <- get_service_endpoint("foo", "bar")
endpoint2 <- get_service_endpoint("foo", "baz cho")
endpoint3 <- get_service_endpoint("foo", "zoo")

expect_equal(endpoint1, "http://localhost:9000")
expect_equal(endpoint2, "http://localhost:9090")
expect_equal(endpoint3, "http://localhost:1234")

Sys.unsetenv(c("AWS_ENDPOINT_URL_BAR", "AWS_ENDPOINT_URL_BAZ_CHO"))
Sys.unsetenv(c("AWS_ENDPOINT_URL_BAR", "AWS_ENDPOINT_URL_BAZ_CHO", "AWS_ENDPOINT_URL"))
})

0 comments on commit d5c39f8

Please sign in to comment.