mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 06:00:46 -05:00
* feat(lambdaweb): check the execution role as AWS does
AWS refuses a revision whose execution role is in another account with
403 AccessDeniedException. It refuses a role that does not exist or does
not trust lambda.amazonaws.com with 400 ValidationException, and that
check gives no aws:SourceAccount or aws:SourceArn. An environment
assumes the role with the function as its source, and a request gets
502 Platform.CannotAssumeRole when Lambda can no longer assume it.
* fix(iam): match a wildcard principal and the service keys in service trust
{"AWS": "*"} lets Lambda assume a role, and a Deny on it refuses the role.
Lambda sets aws:PrincipalIsAWSService to true and aws:PrincipalServiceName
to its principal when it checks the role and when an environment assumes it.
* fix(lambdaweb): answer CannotAssumeRole when the role goes during a start
A role deleted after the trust check gave the container the account's
placeholder credentials. AWS answers 502 Platform.CannotAssumeRole for a
deleted role.
193 lines
8.8 KiB
Bash
193 lines
8.8 KiB
Bash
#!/usr/bin/env bats
|
|
# Lambda Web Functions integration tests
|
|
#
|
|
# The `lambda-web` service model signs as lambda and is served under the lambda
|
|
# service. Code comes from an S3 object, so each test seeds one first.
|
|
|
|
setup() {
|
|
load 'test_helper/common-setup'
|
|
FUNCTION_NAME=""
|
|
BUCKET="cli-lambda-web-code-$$"
|
|
ROLE_NAME="cli-lambda-web-$$"
|
|
ROLE_ARN="arn:aws:iam::000000000000:role/$ROLE_NAME"
|
|
aws_cmd iam create-role --role-name "$ROLE_NAME" --assume-role-policy-document \
|
|
'{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}' \
|
|
>/dev/null 2>&1 || true
|
|
aws_cmd s3api create-bucket --bucket "$BUCKET" >/dev/null 2>&1 || true
|
|
# A revision whose code is not a zip fails. python3 ships in the CLI image.
|
|
python3 -c 'import sys, zipfile
|
|
with zipfile.ZipFile(sys.argv[1], "w") as z:
|
|
z.writestr("index.js", "")' "$BATS_TEST_TMPDIR/app.zip"
|
|
aws_cmd s3api put-object --bucket "$BUCKET" --key app.zip --body "$BATS_TEST_TMPDIR/app.zip" >/dev/null
|
|
}
|
|
|
|
teardown() {
|
|
if [ -n "$FUNCTION_NAME" ]; then
|
|
aws_cmd lambda-web delete-web-function --function-name "$FUNCTION_NAME" >/dev/null 2>&1 || true
|
|
aws_cmd iam delete-role-policy --role-name "awscli-lambdaweb-$FUNCTION_NAME" --policy-name awscli-lambdaweb-logs >/dev/null 2>&1 || true
|
|
aws_cmd iam delete-role --role-name "awscli-lambdaweb-$FUNCTION_NAME" >/dev/null 2>&1 || true
|
|
fi
|
|
aws_cmd s3 rb "s3://$BUCKET" --force >/dev/null 2>&1 || true
|
|
aws_cmd iam delete-role --role-name "$ROLE_NAME" >/dev/null 2>&1 || true
|
|
}
|
|
|
|
build_config() {
|
|
printf '{"codeConfig":{"s3Object":{"bucket":"%s","key":"app.zip"}},"runtimeConfig":{"runtime":"nodejs24.x"}}' "$BUCKET"
|
|
}
|
|
|
|
create_function() {
|
|
local name="$1"
|
|
aws_cmd lambda-web create-web-function \
|
|
--function-name "$name" \
|
|
--revision-config "{\"buildConfig\":$(build_config),\"serviceConfig\":{\"executionRoleArn\":\"$ROLE_ARN\"}}" \
|
|
--endpoint-config '{"endpointName":"dev","endpointType":"HomeRegion","authType":"ApplicationManaged","autoDeploymentMode":"LatestRevision"}'
|
|
}
|
|
|
|
@test "Lambda Web: create a function with its first revision and endpoint" {
|
|
FUNCTION_NAME=$(unique_name "cli-web")
|
|
run create_function "$FUNCTION_NAME"
|
|
assert_success
|
|
state=$(json_get "$output" '.state')
|
|
revision_state=$(json_get "$output" '.revision.state')
|
|
endpoint_state=$(json_get "$output" '.endpoint.state')
|
|
domain=$(json_get "$output" '.endpoint.domainName')
|
|
[ "$state" = "Active" ]
|
|
[ "$revision_state" = "Pending" ]
|
|
[ "$endpoint_state" = "Pending" ]
|
|
[[ "$domain" =~ \.lambda-web\.us-east-1\. ]]
|
|
|
|
run aws_cmd lambda-web wait web-function-endpoint-active --function-name "$FUNCTION_NAME" --endpoint-name dev
|
|
assert_success
|
|
}
|
|
|
|
@test "Lambda Web: a new revision moves a LatestRevision endpoint" {
|
|
FUNCTION_NAME=$(unique_name "cli-web")
|
|
create_function "$FUNCTION_NAME" >/dev/null
|
|
|
|
run aws_cmd lambda-web create-web-function-revision \
|
|
--function-name "$FUNCTION_NAME" \
|
|
--build-config "$(build_config)" \
|
|
--service-config "{\"executionRoleArn\":\"$ROLE_ARN\",\"timeoutSeconds\":60}"
|
|
assert_success
|
|
revision_id=$(json_get "$output" '.revisionId')
|
|
[[ "$revision_id" =~ ^rev-[a-z0-9]{5}$ ]]
|
|
|
|
run aws_cmd lambda-web wait web-function-revision-active --function-name "$FUNCTION_NAME" --revision-id "$revision_id"
|
|
assert_success
|
|
|
|
run aws_cmd lambda-web get-web-function-endpoint --function-name "$FUNCTION_NAME" --endpoint-name dev
|
|
assert_success
|
|
routed=$(json_get "$output" '.revisionWeights[0].revisionId')
|
|
[ "$routed" = "$revision_id" ]
|
|
}
|
|
|
|
@test "Lambda Web: weighted routing and the in-use revision guard" {
|
|
FUNCTION_NAME=$(unique_name "cli-web")
|
|
create_function "$FUNCTION_NAME" >/dev/null
|
|
first=$(aws_cmd lambda-web list-web-function-revisions --function-name "$FUNCTION_NAME" | jq -r '.revisions[0].revisionId')
|
|
|
|
run aws_cmd lambda-web update-web-function-endpoint \
|
|
--function-name "$FUNCTION_NAME" --endpoint-name dev \
|
|
--auto-deployment-mode Disabled \
|
|
--revision-weights "[{\"revisionId\":\"$first\",\"weight\":100}]"
|
|
assert_success
|
|
status=$(json_get "$output" '.updateStatus')
|
|
[ "$status" = "InProgress" ]
|
|
|
|
run aws_cmd lambda-web wait web-function-endpoint-updated --function-name "$FUNCTION_NAME" --endpoint-name dev
|
|
assert_success
|
|
|
|
run aws_cmd lambda-web delete-web-function-revision --function-name "$FUNCTION_NAME" --revision-id "$first"
|
|
assert_failure
|
|
assert_output --partial "ConflictException"
|
|
assert_output --partial "the endpoints [dev] are using it"
|
|
}
|
|
|
|
@test "Lambda Web: tags and the resource policy" {
|
|
FUNCTION_NAME=$(unique_name "cli-web")
|
|
create_function "$FUNCTION_NAME" >/dev/null
|
|
arn="arn:aws:lambda:us-east-1:000000000000:web-function/$FUNCTION_NAME"
|
|
|
|
run aws_cmd lambda-web tag-resource --resource "$arn" --tags env=test
|
|
assert_success
|
|
run aws_cmd lambda-web list-tags --resource "$arn"
|
|
assert_success
|
|
env_tag=$(json_get "$output" '.tags.env')
|
|
[ "$env_tag" = "test" ]
|
|
|
|
run aws_cmd lambda-web put-resource-policy --resource-arn "$arn" \
|
|
--policy "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"arn:aws:iam::000000000000:root\"},\"Action\":\"lambda:InvokeWebFunctionEndpoint\",\"Resource\":\"$arn/endpoint/*\"}]}"
|
|
assert_success
|
|
revision_id=$(json_get "$output" '.revisionId')
|
|
[ -n "$revision_id" ]
|
|
|
|
run aws_cmd lambda-web delete-resource-policy --resource-arn "$arn" --revision-id "$revision_id"
|
|
assert_success
|
|
run aws_cmd lambda-web get-resource-policy --resource-arn "$arn"
|
|
assert_failure
|
|
assert_output --partial "does not have a policy"
|
|
}
|
|
|
|
@test "Lambda Web: invalid names and missing functions" {
|
|
run aws_cmd lambda-web get-web-function --function-name cli-no-such-web-function
|
|
assert_failure
|
|
assert_output --partial "ResourceNotFoundException"
|
|
|
|
# The = form keeps the CLI from reading a name that starts with a dash as an option.
|
|
run aws_cmd lambda-web create-web-function --function-name=-bad \
|
|
--revision-config "{\"buildConfig\":$(build_config),\"serviceConfig\":{\"executionRoleArn\":\"$ROLE_ARN\"}}" \
|
|
--endpoint-config '{"endpointName":"dev","endpointType":"HomeRegion","authType":"ApplicationManaged"}'
|
|
assert_failure
|
|
assert_output --partial "Invalid function name or ARN '-bad'"
|
|
}
|
|
|
|
@test "Lambda Web: an execution role Lambda cannot assume is refused" {
|
|
FUNCTION_NAME=$(unique_name "cli-web")
|
|
create_function "$FUNCTION_NAME" >/dev/null
|
|
|
|
run aws_cmd lambda-web create-web-function-revision --function-name "$FUNCTION_NAME" \
|
|
--build-config "$(build_config)" --service-config "{\"executionRoleArn\":\"$ROLE_ARN-missing\"}"
|
|
assert_failure
|
|
assert_output --partial "ValidationException"
|
|
assert_output --partial "The execution role $ROLE_ARN-missing cannot be assumed by lambda.amazonaws.com."
|
|
|
|
run aws_cmd lambda-web create-web-function-revision --function-name "$FUNCTION_NAME" \
|
|
--build-config "$(build_config)" \
|
|
--service-config "{\"executionRoleArn\":\"arn:aws:iam::111122223333:role/$ROLE_NAME\"}"
|
|
assert_failure
|
|
assert_output --partial "AccessDeniedException"
|
|
assert_output --partial "Cross-account pass role is not allowed."
|
|
}
|
|
|
|
@test "Lambda Web: deploy the hello world app and invoke its endpoint" {
|
|
FUNCTION_NAME=$(unique_name "cli-web")
|
|
# deploy passes --endpoint-url only to its Lambda Web client, so the environment variable routes the rest.
|
|
run bash -c "cd '$BATS_TEST_TMPDIR' && AWS_ENDPOINT_URL='$FLOCI_ENDPOINT' aws lambda-web deploy --name '$FUNCTION_NAME' --hello-world --create"
|
|
assert_success
|
|
domain=$(aws_cmd lambda-web get-web-function-endpoint --function-name "$FUNCTION_NAME" --endpoint-name dev | jq -r '.domainName')
|
|
|
|
run curl -sS -i -H "Host: $domain" "$FLOCI_ENDPOINT/hello?from=bats"
|
|
assert_success
|
|
assert_output --partial "HTTP/1.1 200"
|
|
assert_output --partial "lambda-web-request-id: "
|
|
assert_output --partial "lambda-web-logstream: "
|
|
assert_output --partial "Hello from Lambda Web!"
|
|
|
|
# A domain Floci never minted, in the shape it mints.
|
|
run curl -sS -i -H "Host: nopenopenopenope.001.lambda-web.us-east-1.localhost:4566" "$FLOCI_ENDPOINT/"
|
|
assert_success
|
|
assert_output --partial "HTTP/1.1 404"
|
|
assert_output --partial "lambda-web-error-type: Platform.InvalidEndpoint"
|
|
}
|
|
|
|
@test "Lambda Web: delete removes the function and the waiter settles" {
|
|
FUNCTION_NAME=$(unique_name "cli-web")
|
|
create_function "$FUNCTION_NAME" >/dev/null
|
|
|
|
run aws_cmd lambda-web delete-web-function --function-name "$FUNCTION_NAME"
|
|
assert_success
|
|
run aws_cmd lambda-web wait web-function-deleted --function-name "$FUNCTION_NAME"
|
|
assert_success
|
|
FUNCTION_NAME=""
|
|
}
|