From 63d81b32ed1a6ce9567b4121f2b56ebf13e51336 Mon Sep 17 00:00:00 2001 From: "Thomas J. Leeper" Date: Tue, 3 Apr 2018 10:16:08 +0100 Subject: [PATCH] update tests for CRAN release --- DESCRIPTION | 2 +- NEWS.md | 1 + R/locate_credentials.R | 23 +++++++++++++++++-- tests/testthat/tests-credentials_file.R | 30 ++++++++++++++++++++++--- 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 26a372a..b4d8a64 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -2,7 +2,7 @@ Package: aws.signature Type: Package Title: Amazon Web Services Request Signatures Version: 0.4.0 -Date: 2018-04-02 +Date: 2018-04-03 Authors@R: c(person("Thomas J.", "Leeper", role = c("aut", "cre"), email = "thosjleeper@gmail.com", diff --git a/NEWS.md b/NEWS.md index d31451e..7c36035 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,7 @@ # aws.signature 0.4.0 * `read_credentials()` now trims excess whitespace from profile names. (#22, h/t Paul Ingles) +* `locate_credentials()` returns `region = default_region` even when no other credentials are found. # aws.signature 0.3.7 diff --git a/R/locate_credentials.R b/R/locate_credentials.R index fed0150..f5bb53d 100644 --- a/R/locate_credentials.R +++ b/R/locate_credentials.R @@ -58,7 +58,6 @@ function(key = NULL, } } - # check for user-supplied values if (isTRUE(verbose)) { message("Checking for credentials in user-supplied values") @@ -81,6 +80,7 @@ function(key = NULL, } # now find region, with fail safes if (!is.null(region) && region != "") { + region <- region if (isTRUE(verbose)) { message(sprintf("Using user-supplied value for AWS Region ('%s')", region)) } @@ -184,6 +184,7 @@ function(key = NULL, } # now find region, with fail safes if (!is.null(region) && region != "") { + region <- region if (isTRUE(verbose)) { message(sprintf("Using user-supplied value for AWS Region ('%s')", region)) } @@ -229,8 +230,25 @@ function(key = NULL, if (isTRUE(verbose)) { message("No instance metadata, environment variables, or credentials file found!") } + # now find region, with fail safes + if (!is.null(region) && region != "") { + region <- region + if (isTRUE(verbose)) { + message(sprintf("Using user-supplied value for AWS Region ('%s')", region)) + } + } else if (!is.null(env$region) && env$region != "") { + region <- env$region + if (isTRUE(verbose)) { + message(sprintf("Using Environment Variable 'AWS_DEFAULT_REGION' for AWS Region ('%s')", region)) + } + } else { + region <- default_region + if (isTRUE(verbose)) { + message(sprintf("Using default value for AWS Region ('%s')", region)) + } + } # return early with list of empty values! - return(list(key = NULL, secret = NULL, session_token = NULL, region = NULL)) + return(list(key = NULL, secret = NULL, session_token = NULL, region = region)) } if (!is.null(cred[["AWS_ACCESS_KEY_ID"]])) { key <- cred[["AWS_ACCESS_KEY_ID"]] @@ -252,6 +270,7 @@ function(key = NULL, } # now find region, with fail safes if (!is.null(region) && region != "") { + region <- region if (isTRUE(verbose)) { message(sprintf("Using user-supplied value for AWS Region ('%s')", region)) } diff --git a/tests/testthat/tests-credentials_file.R b/tests/testthat/tests-credentials_file.R index 66abb15..0943aaa 100644 --- a/tests/testthat/tests-credentials_file.R +++ b/tests/testthat/tests-credentials_file.R @@ -14,8 +14,18 @@ test_that("default_credentials_file() works", { context("Tests locate_credentials()") -test_that("locate_credentials() returns non-default region if requested", { - expect_true(locate_credentials(region = "foo")[["region"]] == "foo") +test_that("locate_credentials() returns non-default values if requested", { + e <- Sys.getenv(c("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_DEFAULT_REGION")) + Sys.unsetenv("AWS_ACCESS_KEY_ID") + Sys.unsetenv("AWS_SECRET_ACCESS_KEY") + Sys.unsetenv("AWS_SESSION_TOKEN") + Sys.unsetenv("AWS_DEFAULT_REGION") + expect_true(locate_credentials(key = "foo")[["key"]] == "foo", label = "locate_credentials(key = 'foo')") + expect_true(locate_credentials(secret = "foo")[["secret"]] == "foo", label = "locate_credentials(secret = 'foo')") + # session token cannot be set if key and secret are missing + expect_true(is.null(locate_credentials(session_token = "foo")[["session_token"]]), label = "locate_credentials(session_token = 'foo') returns NULL") + expect_true(locate_credentials(region = "foo")[["region"]] == "foo", label = "locate_credentials(region = 'foo')") + do.call("Sys.setenv", as.list(e)) }) test_that("locate_credentials() returns NULLs when environment variables missing", { @@ -28,7 +38,21 @@ test_that("locate_credentials() returns NULLs when environment variables missing expect_true(is.null(cred[["key"]])) expect_true(is.null(cred[["secret"]])) expect_true(is.null(cred[["session_token"]])) - expect_true(is.null(cred[["region"]])) + expect_true(cred[["region"]] == "us-east-1") + do.call("Sys.setenv", as.list(e)) +}) + +test_that("locate_credentials() returns NULLs when environment variables present", { + e <- Sys.getenv(c("AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY", "AWS_SESSION_TOKEN", "AWS_DEFAULT_REGION")) + Sys.setenv("AWS_ACCESS_KEY_ID" = "foo-key") + Sys.setenv("AWS_SECRET_ACCESS_KEY" = "foo-secret") + Sys.setenv("AWS_SESSION_TOKEN" = "foo-token") + Sys.setenv("AWS_DEFAULT_REGION" = "foo-region") + cred <- locate_credentials() + expect_true(cred[["key"]] == "foo-key") + expect_true(cred[["secret"]] == "foo-secret") + expect_true(cred[["session_token"]] == "foo-token") + expect_true(cred[["region"]] == "foo-region") do.call("Sys.setenv", as.list(e)) })