Skip to content

Commit

Permalink
re-assign Sys.time to prevent null binding break paginator functions (#…
Browse files Browse the repository at this point in the history
…880)

* re-assign Sys.time to prevent null binding break paginator functions

* prevent .Internal call
  • Loading branch information
DyfanJones authored Jan 30, 2025
1 parent c708359 commit 43c5827
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion paws.common/R/credentials.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ check_if_cred_needs_refresh <- function(creds, window) {
if (is.numeric(expire <- creds$expiration)) {
expire <- expire / 1000
}
return(length(expire) == 1 && is.finite(expire) && Sys.time() > expire - window)
return(length(expire) == 1 && is.finite(expire) && now() > expire - window)
}
8 changes: 4 additions & 4 deletions paws.common/R/custom_rds.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
rds_build_auth_token <- function(endpoint, user, creds = NULL, region=NULL) {
rds_build_auth_token <- function(endpoint, user, creds = NULL, region = NULL) {
if (!startsWith(endpoint, "https://")) endpoint <- paste0("https://", endpoint)
req <- new_http_request("GET", endpoint)
auth_token_params <- list(
Expand All @@ -16,14 +16,14 @@ rds_build_auth_token <- function(endpoint, user, creds = NULL, region=NULL) {
} else {
creds <- populate(creds, tag_annotate(Creds()))
}
v4 <- Signer(credentials =Credentials(creds = creds))
req <- sign_with_body(v4, req, NULL, "rds-db", region, 900, TRUE, Sys.time())
v4 <- Signer(credentials = Credentials(creds = creds))
req <- sign_with_body(v4, req, NULL, "rds-db", region, 900, TRUE, now())

url <- build_url(req$url)
return(substr(url, 9, nchar(url)))
}

rds_build_auth_token_v2 <- function(DBHostname, Port, DBUsername, Region=NULL) {
rds_build_auth_token_v2 <- function(DBHostname, Port, DBUsername, Region = NULL) {
op <- new_operation(
name = "connect", http_method = "GET",
http_path = "/", host_prefix = "", paginator = list(), stream_api = FALSE
Expand Down
2 changes: 1 addition & 1 deletion paws.common/R/handlers_core.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ validate_req_sig_handler <- function(request) {
}

minute <- 60
if (signed_time + 10 * minute > Sys.time()) {
if (signed_time + 10 * minute > now()) {
return(request)
}

Expand Down
2 changes: 1 addition & 1 deletion paws.common/R/logging.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ log_error <- function(...) {

log_msg <- function(lvl, msg) {
log_file <- getOption("paws.log_file")
now <- strftime(Sys.time(), "%Y-%m-%d %H:%M:%OS3")
now <- strftime(now(), "%Y-%m-%d %H:%M:%OS3")
cat(
sprintf("%s [%s]: %s\n", log_color(lvl), now, msg),
file = log_file,
Expand Down
3 changes: 2 additions & 1 deletion paws.common/R/mock_bindings.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# https://testthat.r-lib.org/reference/local_mocked_bindings.html#base-functions
################################################################

Sys.time <- NULL
# re-assign Sys.time for mocking as setting a NULL binding will break the paginator
now <- function() {Sys.time()}

get0 <- NULL
2 changes: 1 addition & 1 deletion paws.common/R/request.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ new_request <- function(client, operation, params, data, dest = NULL) {
client_info = client$client_info,
handlers = client$handlers,
retryer = NULL, # Implement retryers.
time = Sys.time(),
time = now(),
expire_time = 0,
operation = operation,
http_request = http_req,
Expand Down
6 changes: 3 additions & 3 deletions paws.common/R/signer_v4.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Signer <- struct(
disable_header_hoisting = FALSE,
disable_uri_path_escaping = FALSE,
disable_request_body_overwrite = FALSE,
curr_time_fn = Sys.time,
curr_time_fn = now,
unsigned_payload = FALSE
)

Expand Down Expand Up @@ -104,7 +104,7 @@ v4_sign_request_handler <- function(request) {
}

sign_sdk_request_with_curr_time <- function(request,
curr_time_fn = Sys.time,
curr_time_fn = now,
opts = NULL) {
region <- request$client_info$signing_region
if (region == "") {
Expand Down Expand Up @@ -153,7 +153,7 @@ sign_with_body <- function(signer, request, body, service, region,
expire_time, is_presigned, signing_time) {
curr_time_fn <- signer$curr_time_fn
if (is.null(curr_time_fn)) {
curr_time_fn <- Sys.time
curr_time_fn <- now
}

if (is.null(body)) body <- ""
Expand Down
34 changes: 19 additions & 15 deletions paws.common/tests/testthat/test_custom_rds.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
test_that("check rds_build_auth_token", {
local_mocked_bindings(
Sys.time = function() as.POSIXct("2025/01/01 00:00:01 UTC"),
get_config = function() list(credentials = list(creds = list(
access_key_id = "AKIA",
secret_access_key = "SECRET",
session_token = "SESSION"
))),
now = function() as.POSIXct("2025/01/01 00:00:01 UTC"),
get_config = function() {
list(credentials = list(creds = list(
access_key_id = "AKIA",
secret_access_key = "SECRET",
session_token = "SESSION"
)))
},
.package = "paws.common"
)
expected <- "prod-instance.us-east-1.rds.amazonaws.com:3306/?Action=connect&DBUser=mysqlUser&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIA%2F20250101%2Fus-west-2%2Frds-db%2Faws4_request&X-Amz-Date=20250101T000001Z&X-Amz-Expires=900&X-Amz-Security-Token=SESSION&X-Amz-SignedHeaders=host&X-Amz-Signature=4e4ce10c7b3710decae757df60ba2519348dcb4db802f15646dce1bf5e17c3ed"
Expand Down Expand Up @@ -37,12 +39,14 @@ test_that("check rds_build_auth_token", {

test_that("check rds_build_auth_token upper case host", {
local_mocked_bindings(
Sys.time = function() as.POSIXct("2025/01/01 00:00:01 UTC"),
get_config = function() list(credentials = list(creds = list(
access_key_id = "AKIA",
secret_access_key = "SECRET",
session_token = "SESSION"
))),
now = function() as.POSIXct("2025/01/01 00:00:01 UTC"),
get_config = function() {
list(credentials = list(creds = list(
access_key_id = "AKIA",
secret_access_key = "SECRET",
session_token = "SESSION"
)))
},
.package = "paws.common"
)

Expand All @@ -65,10 +69,10 @@ test_that("check rds_build_auth_token upper case host", {
)

actual_v2 <- client$build_auth_token_v2(
DBHostname='XXXXX.US-EAST-2.RDS.AMAZONAWS.COM',
DBHostname = "XXXXX.US-EAST-2.RDS.AMAZONAWS.COM",
Port = 3306,
DBUsername="user1",
Region="us-east-2"
DBUsername = "user1",
Region = "us-east-2"
)
expect_equal(actual_v1, expected)
expect_equal(actual_v2, expected)
Expand Down

0 comments on commit 43c5827

Please sign in to comment.