Skip to content

Commit

Permalink
export locate_credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
DyfanJones authored Feb 21, 2024
2 parents 1f20488 + 3e16079 commit 3b8a0c6
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion paws.common/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Suggests:
testthat (>= 3.0.0)
SystemRequirements: pandoc (>= 1.12.3) - http://pandoc.org
Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace", "collate"))
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Collate:
'RcppExports.R'
'cache.R'
Expand Down
2 changes: 2 additions & 0 deletions paws.common/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export(get_config)
export(is_empty)
export(is_empty_xml)
export(list_paginators)
export(locate_credentials)
export(merge_config)
export(new_handlers)
export(new_operation)
Expand All @@ -46,4 +47,5 @@ importFrom(httr,with_config)
importFrom(stats,runif)
importFrom(utils,flush.console)
importFrom(utils,modifyList)
useDynLib(paws.common,"_paws_common_char_sort")
useDynLib(paws.common,"_paws_common_paws_url_encoder")
2 changes: 2 additions & 0 deletions paws.common/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# paws.common 0.7.0.9000
* minor performance enhancements
* fix MIME type for AWS BedrockRuntime Client (#749), thanks to @alex23lemm for raising issue.
* export locate_credentials (#750), thanks to @tyner for raising request.

# paws.common 0.7.0
* support sse md5 (#718). Thanks to @odysseu for raising issue.
Expand Down
2 changes: 1 addition & 1 deletion paws.common/R/credential_providers.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ env_provider <- function() {
secret_access_key <- get_env("AWS_SECRET_ACCESS_KEY")
session_token <- get_env("AWS_SESSION_TOKEN")
expiration <- as_timestamp(get_env("AWS_CREDENTIAL_EXPIRATION"), "iso8601")
if (length(expiration) == 0) expiration <- Inf
if (is.na(expiration)) expiration <- Inf

if (access_key_id != "" && secret_access_key != "") {
creds <- Creds(
Expand Down
13 changes: 13 additions & 0 deletions paws.common/R/credentials.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#' @include struct.R
#' @include credential_providers.R
#' @include config.R
NULL

Credentials <- struct(
Expand Down Expand Up @@ -33,6 +34,18 @@ get_credentials <- function(credentials) {
return(credentials)
}

#' @title Locate AWS credentials
#' @param profile The name of a profile to use. If not given, then the default profile is used.
#' @param anonymous Set anonymous credentials.
#' @return list containing AWS credentials
#' @export
locate_credentials <- function(profile = "", anonymous = FALSE) {
credentials <- Credentials(profile = profile, anonymous = anonymous)
result <- as.list(get_credentials(credentials)$creds)
result$region <- get_region(profile)
return(result[names(result) != "provider_name"])
}

# Return whether a creds object has at least the minimum data needed to
# authenticate.
is_credentials_provided <- function(creds, window = 5 * 60) {
Expand Down
19 changes: 19 additions & 0 deletions paws.common/man/locate_credentials.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions paws.common/tests/testthat/test_credentials.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,24 @@ test_that("credentials refresh when expired", {

expect_true(expiration1 != expiration3)
})

test_that("check locate_credentials", {
env <- list(
"AWS_ACCESS_KEY_ID" = "foo",
"AWS_SECRET_ACCESS_KEY" = "bar",
"AWS_REGION" = "zoo"
)
do.call(Sys.setenv, env)

actual <- locate_credentials()
expect_equal(actual, list(
access_key_id = "foo",
secret_access_key = "bar",
session_token = "",
access_token = "",
expiration = Inf,
region = "zoo"
))

Sys.unsetenv(names(env))
})

0 comments on commit 3b8a0c6

Please sign in to comment.