Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions R/lambda-deploy.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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
Expand Down
45 changes: 40 additions & 5 deletions R/lambda-util.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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. 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
Expand All @@ -570,11 +578,38 @@ 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}.")
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)
}
)

logger::log_debug("[create_lambda_exec_role] Done.")
invisible(role_meta)
Expand Down
105 changes: 99 additions & 6 deletions tests/testthat/test-lambda-deploy.R
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,85 @@ 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("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")
)
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(
Expand All @@ -157,14 +236,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"
)
})
Expand All @@ -182,7 +271,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")
Expand Down Expand Up @@ -227,7 +320,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")
})
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-lambda-util.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading