From ee082cbc61d1de770a2e28231751f3946bd72dbf Mon Sep 17 00:00:00 2001 From: novica Date: Sun, 1 Nov 2026 20:05:25 +0100 Subject: [PATCH 1/2] fix: check deploy_lambda() inputs before pushing the image Malformed role or policy ARNs failed only after the slow ECR push, and a failed policy attach left the new role behind. Assisted-by: Claude Opus 5.5 --- R/lambda-deploy.R | 19 +++++++- R/lambda-util.R | 35 ++++++++++++-- tests/testthat/test-lambda-deploy.R | 75 ++++++++++++++++++++++++++--- tests/testthat/test-lambda-util.R | 12 +++++ 4 files changed, 128 insertions(+), 13 deletions(-) diff --git a/R/lambda-deploy.R b/R/lambda-deploy.R index 5328bf1..6a3450d 100644 --- a/R/lambda-deploy.R +++ b/R/lambda-deploy.R @@ -228,7 +228,18 @@ test_lambda <- function(tag, payload) { deploy_lambda <- function(tag, policy_arns = NULL, role_arn = NULL, ...) { error_call <- rlang::current_env() - checkmate::assert_string(role_arn, min.chars = 1, null.ok = TRUE) + # Check the ARNs here: AWS would reject them only after the slow push. + checkmate::assert_string( + role_arn, + pattern = "^arn:aws[a-z-]*:iam::[0-9]{12}:role/", + null.ok = TRUE + ) + checkmate::assert_character( + policy_arns, + pattern = "^arn:aws[a-z-]*:iam::(aws|[0-9]{12}):policy/", + any.missing = FALSE, + null.ok = TRUE + ) if (!is.null(role_arn) && !is.null(policy_arns)) { cli::cli_abort(c( "Use either {.arg role_arn} or {.arg policy_arns}, not both.", @@ -289,8 +300,12 @@ deploy_lambda <- ) role_arn <- iam_lambda_role$Role$Arn + policies <- glue_collapse( + unique(c(lambda_basic_policy, policy_arns)), + ", " + ) logger::log_warn( - "[deploy_lambda] Created AWS role with basic lambda execution permissions." # nolint + "[deploy_lambda] Created AWS role {role_arn} with policies {policies}." ) logger::log_warn( "[deploy_lambda] Use `paws::iam()`, the AWS CLI, or the AWS console to manage your roles, and permissions." # nolint diff --git a/R/lambda-util.R b/R/lambda-util.R index 245c4ca..5618e93 100644 --- a/R/lambda-util.R +++ b/R/lambda-util.R @@ -242,6 +242,7 @@ create_lambda_dockerfile <- checkmate::assert_character( x = system_deps, pattern = "^[A-Za-z0-9][A-Za-z0-9._+-]*$", + min.len = 1, any.missing = FALSE, null.ok = TRUE ) @@ -543,7 +544,14 @@ push_lambda_image <- function(repo_uri) { invisible(repo_uri) } +#' AWS managed policy every execution role gets, for CloudWatch logging +#' @noRd +lambda_basic_policy <- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" # nolint + #' create_lambda_exec_role +#' +#' If a policy can't be attached, the new role is deleted before the error is +#' raised, so a failed deploy leaves no role behind. #' @param tag the tag of an existing local image #' @param policy_arns ARNs of IAM policies to attach besides the basic #' execution policy @@ -570,11 +578,28 @@ create_lambda_exec_role <- function(tag, policy_arns = NULL) { AssumeRolePolicyDocument = role_string ) - basic_policy <- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" # nolint - for (policy_arn in unique(c(basic_policy, policy_arns))) { - logger::log_debug("[create_lambda_exec_role] Attaching {policy_arn}.") - iam_service$attach_role_policy(RoleName = role_name, PolicyArn = policy_arn) - } + attached <- character() + tryCatch( + for (policy_arn in unique(c(lambda_basic_policy, policy_arns))) { + logger::log_debug("[create_lambda_exec_role] Attaching {policy_arn}.") + iam_service$attach_role_policy( + RoleName = role_name, + PolicyArn = policy_arn + ) + attached <- c(attached, policy_arn) + }, + error = function(e) { + logger::log_debug("[create_lambda_exec_role] Deleting role {role_name}.") + for (policy_arn in attached) { + iam_service$detach_role_policy( + RoleName = role_name, + PolicyArn = policy_arn + ) + } + iam_service$delete_role(RoleName = role_name) + stop(e) + } + ) logger::log_debug("[create_lambda_exec_role] Done.") invisible(role_meta) diff --git a/tests/testthat/test-lambda-deploy.R b/tests/testthat/test-lambda-deploy.R index e7e8741..6175cfd 100644 --- a/tests/testthat/test-lambda-deploy.R +++ b/tests/testthat/test-lambda-deploy.R @@ -140,6 +140,55 @@ test_that("create_lambda_exec_role attaches the basic and extra policies", { expect_error(create_lambda_exec_role("f", policy_arns = "")) }) +test_that("create_lambda_exec_role deletes the role when a policy fails", { + calls <- character() + local_mocked_bindings( + aws_region = function() "eu-west-1", + aws_connect = function(service) { + list( + create_role = function(Path, RoleName, AssumeRolePolicyDocument) { + list(Role = list(Arn = "arn:role")) + }, + attach_role_policy = function(RoleName, PolicyArn) { + if (grepl("Missing", PolicyArn)) { + stop("NoSuchEntity (HTTP 404). Policy does not exist.") + } + calls <<- c(calls, "attach") + }, + detach_role_policy = function(RoleName, PolicyArn) { + calls <<- c(calls, "detach") + }, + delete_role = function(RoleName) { + calls <<- c(calls, "delete_role") + } + ) + } + ) + expect_error( + create_lambda_exec_role( + "f", + policy_arns = "arn:aws:iam::aws:policy/Missing" + ), + "NoSuchEntity" + ) + expect_equal(calls, c("attach", "detach", "delete_role")) +}) + +test_that("deploy_lambda checks the ARNs before touching AWS", { + local_mocked_bindings( + fetch_ecr_repo = function(...) stop("should not look up the repository") + ) + expect_error(deploy_lambda("f", role_arn = "my-role"), "role_arn") + expect_error( + deploy_lambda("f", policy_arns = "AmazonS3FullAccess"), + "policy_arns" + ) + expect_error( + deploy_lambda("f", policy_arns = c("arn:aws:iam::aws:policy/A", NA)), + "policy_arns" + ) +}) + test_that("deploy_lambda uses an existing role without creating one", { used_role <- NULL local_mocked_bindings( @@ -157,14 +206,24 @@ test_that("deploy_lambda uses an existing role without creating one", { list(FunctionArn = "arn:fn") } ) - result <- deploy_lambda("f", role_arn = "arn:aws:iam::1:role/existing") - expect_equal(used_role, "arn:aws:iam::1:role/existing") - expect_equal(result$IAM_lambda_role_arn, "arn:aws:iam::1:role/existing") + result <- deploy_lambda( + "f", + role_arn = "arn:aws:iam::123456789012:role/existing" + ) + expect_equal(used_role, "arn:aws:iam::123456789012:role/existing") + expect_equal( + result$IAM_lambda_role_arn, + "arn:aws:iam::123456789012:role/existing" + ) }) test_that("deploy_lambda rejects role_arn together with policy_arns", { expect_error( - deploy_lambda("f", policy_arns = "arn:p", role_arn = "arn:r"), + deploy_lambda( + "f", + policy_arns = "arn:aws:iam::aws:policy/ReadOnlyAccess", + role_arn = "arn:aws:iam::123456789012:role/existing" + ), "not both" ) }) @@ -182,7 +241,11 @@ test_that("deploy_lambda creates the function for the image architecture", { }) } ) - deploy_lambda("f", role_arn = "arn:aws:iam::1:role/existing", Timeout = 60) + deploy_lambda( + "f", + role_arn = "arn:aws:iam::123456789012:role/existing", + Timeout = 60 + ) expect_equal(args$Architectures, list("arm64")) expect_equal(args$Timeout, 60) expect_equal(args$Code$ImageUri, "reg/f:latest") @@ -227,7 +290,7 @@ test_that("deploy_lambda inspects and pushes the same image", { }, create_lambda_function = function(...) list(FunctionArn = "arn:fn") ) - deploy_lambda("f", role_arn = "arn:aws:iam::1:role/existing") + deploy_lambda("f", role_arn = "arn:aws:iam::123456789012:role/existing") expect_equal(inspected, "123.dkr.ecr.eu-west-1.amazonaws.com/f:latest") expect_equal(pushed, "123.dkr.ecr.eu-west-1.amazonaws.com/f") }) diff --git a/tests/testthat/test-lambda-util.R b/tests/testthat/test-lambda-util.R index 0f22eb6..caf33e6 100644 --- a/tests/testthat/test-lambda-util.R +++ b/tests/testthat/test-lambda-util.R @@ -256,6 +256,18 @@ test_that("create_lambda_dockerfile rejects unsafe system package names", { ) }) +test_that("create_lambda_dockerfile rejects an empty system_deps", { + expect_error( + create_lambda_dockerfile( + folder = withr::local_tempfile(), + runtime_function = "parity", + runtime_path = system.file("parity.R", package = "r2lambda"), + system_deps = character() + ), + "system_deps" + ) +}) + test_that("create_lambda_dockerfile copies support folders with their contents", { src <- withr::local_tempdir() dir.create(file.path(src, "R", "sub"), recursive = TRUE) From cfe17345c807e278e80ab5542f45f04d3e0714d4 Mon Sep 17 00:00:00 2001 From: novica Date: Sun, 1 Nov 2026 20:06:21 +0100 Subject: [PATCH 2/2] fix: keep the attach error when role cleanup fails Assisted-by: Claude Opus 5.5 --- R/lambda-util.R | 26 +++++++++++++++++-------- tests/testthat/test-lambda-deploy.R | 30 +++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/R/lambda-util.R b/R/lambda-util.R index 5618e93..be40306 100644 --- a/R/lambda-util.R +++ b/R/lambda-util.R @@ -551,7 +551,7 @@ lambda_basic_policy <- "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecu #' create_lambda_exec_role #' #' If a policy can't be attached, the new role is deleted before the error is -#' raised, so a failed deploy leaves no role behind. +#' raised. If that deletion fails too, a warning names the role left behind. #' @param tag the tag of an existing local image #' @param policy_arns ARNs of IAM policies to attach besides the basic #' execution policy @@ -590,13 +590,23 @@ create_lambda_exec_role <- function(tag, policy_arns = NULL) { }, error = function(e) { logger::log_debug("[create_lambda_exec_role] Deleting role {role_name}.") - for (policy_arn in attached) { - iam_service$detach_role_policy( - RoleName = role_name, - PolicyArn = policy_arn - ) - } - iam_service$delete_role(RoleName = role_name) + tryCatch( + { + for (policy_arn in attached) { + iam_service$detach_role_policy( + RoleName = role_name, + PolicyArn = policy_arn + ) + } + iam_service$delete_role(RoleName = role_name) + }, + error = function(cleanup_error) { + cli::cli_warn( + "Failed to delete the IAM role {.val {role_name}}; delete it by hand.", + parent = cleanup_error + ) + } + ) stop(e) } ) diff --git a/tests/testthat/test-lambda-deploy.R b/tests/testthat/test-lambda-deploy.R index 6175cfd..59f1996 100644 --- a/tests/testthat/test-lambda-deploy.R +++ b/tests/testthat/test-lambda-deploy.R @@ -174,6 +174,36 @@ test_that("create_lambda_exec_role deletes the role when a policy fails", { expect_equal(calls, c("attach", "detach", "delete_role")) }) +test_that("create_lambda_exec_role keeps the attach error when cleanup fails", { + local_mocked_bindings( + aws_region = function() "eu-west-1", + aws_connect = function(service) { + list( + create_role = function(Path, RoleName, AssumeRolePolicyDocument) { + list(Role = list(Arn = "arn:role")) + }, + attach_role_policy = function(RoleName, PolicyArn) { + if (grepl("Missing", PolicyArn)) { + stop("NoSuchEntity (HTTP 404). Policy does not exist.") + } + }, + detach_role_policy = function(RoleName, PolicyArn) NULL, + delete_role = function(RoleName) stop("AccessDenied") + ) + } + ) + expect_warning( + expect_error( + create_lambda_exec_role( + "f", + policy_arns = "arn:aws:iam::aws:policy/Missing" + ), + "NoSuchEntity" + ), + "delete it by hand" + ) +}) + test_that("deploy_lambda checks the ARNs before touching AWS", { local_mocked_bindings( fetch_ecr_repo = function(...) stop("should not look up the repository")