Files
floci/compatibility-tests/sdk-test-awscli/test/iam.bats
T
12154297b5 feat(iam): support account aliases (#2103)
* feat(iam): support account aliases

Implement ListAccountAliases, CreateAccountAlias and DeleteAccountAlias.
An account holds at most one alias: creating a second returns
EntityAlreadyExists, and deleting requires the current alias by name so a
stale value cannot silently clear it.

FLOCI_SERVICES_IAM_ACCOUNT_ALIAS seeds an alias at startup for callers that
expect to read one without creating it first.

Closes #2101

* fix(iam): make account alias create and delete atomic

Concurrent CreateAccountAlias calls could all pass the emptiness check and
write, reporting success to every caller while keeping only one value. Unlike
a named resource, where racing creates carry the same name and either winner
is equivalent, racing alias creates carry different values, so a loser is told
their alias was set when another is stored.

Guard the check-then-write on both create and delete, and cover it with a
concurrency trial asserting exactly one winner. Also track the alias created
by the bats delete case in ACCOUNT_ALIAS so teardown clears it if the test
fails midway.

* fix(iam): match verified AWS account alias semantics and unbreak the test fixture

IamServiceTestHelper resolves the package-private IamService constructor
reflectively against a hardcoded parameter list, so the extra storage backend
broke it at run time with no compile-time signal. Every test using that fixture
errored, and one of them hangs on a socket read rather than failing, which is
what exhausted the CI job. Extend both lookups to the new arity.

Checking the error message against a live AWS account showed the operation
itself was modelled wrongly: CreateAccountAlias does not reject a second alias,
it replaces the current one. EntityAlreadyExists means the requested name is
taken, which for a single-account store is only the alias already held. Create
now replaces and rejects just that case, which also makes the message name the
alias the caller asked for. DeleteAccountAlias applies the same pattern
constraint as create, so a malformed value is a ValidationError.

Concurrent creates therefore all succeed with last-write-wins; the trial now
asserts the store settles on one of the requested values. Seeding logs when it
skips because an alias is already stored, which is otherwise invisible under
persistent storage.

* fix(test): repair iam.bats after the merge fused test bodies

Resolving the merge by keeping both sides concatenated the OIDC and account
alias cases as text, which left the create_oidc_provider helper nested inside an
alias test and dropped a closing brace, so bats could not parse the file and
produced no report at all. Rebuild it from main plus the alias cases, with the
alias tracked in setup and teardown.

* test(iam): pin account alias isolation, and document the two-dash rule

The alias is the only IAM entity keyed by a constant rather than a
caller-supplied name, so every account shares one storage key and the
separation rests entirely on AccountAwareStorageBackend prefixing. Every
integration test here signs as the same account, so that prefixing was never
exercised for aliases. The new case pins it: handing both services the store
unwrapped fails it.

The docs still described the pre-fix constraint. They now carry the
consecutive-hyphen rule and note that the ValidationError message, reproduced
from AWS verbatim, does not itself mention it.

---------

Co-authored-by: Michal Schreiber <michal.p.schreiber@gmail.com>
Co-authored-by: Michal Schreiber <michal.schreiber@benefitsystems.pl>
2026-08-14 12:00:14 -04:00

259 lines
9.4 KiB
Bash

#!/usr/bin/env bats
# IAM tests
setup() {
load 'test_helper/common-setup'
ROLE_NAME="bats-test-role-$(unique_name)"
POLICY_ARN=""
ACCOUNT_ALIAS=""
}
teardown() {
if [ -n "$POLICY_ARN" ]; then
aws_cmd iam detach-role-policy --role-name "$ROLE_NAME" --policy-arn "$POLICY_ARN" >/dev/null 2>&1 || true
aws_cmd iam delete-policy --policy-arn "$POLICY_ARN" >/dev/null 2>&1 || true
fi
aws_cmd iam delete-role --role-name "$ROLE_NAME" >/dev/null 2>&1 || true
# An account holds one alias, so a leaked one would fail every later create.
if [ -n "$ACCOUNT_ALIAS" ]; then
aws_cmd iam delete-account-alias --account-alias "$ACCOUNT_ALIAS" >/dev/null 2>&1 || true
fi
}
@test "IAM: create role" {
local policy_doc='{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}'
run aws_cmd iam create-role \
--role-name "$ROLE_NAME" \
--assume-role-policy-document "$policy_doc"
assert_success
arn=$(json_get "$output" '.Role.Arn')
[ -n "$arn" ]
}
@test "IAM: get role" {
local policy_doc='{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}'
aws_cmd iam create-role --role-name "$ROLE_NAME" --assume-role-policy-document "$policy_doc" >/dev/null
run aws_cmd iam get-role --role-name "$ROLE_NAME"
assert_success
name=$(json_get "$output" '.Role.RoleName')
[ "$name" = "$ROLE_NAME" ]
}
@test "IAM: list roles" {
local policy_doc='{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}'
aws_cmd iam create-role --role-name "$ROLE_NAME" --assume-role-policy-document "$policy_doc" >/dev/null
run aws_cmd iam list-roles
assert_success
found=$(echo "$output" | jq --arg name "$ROLE_NAME" '.Roles | any(.RoleName == $name)')
[ "$found" = "true" ]
}
@test "IAM: create and delete policy" {
local policy_doc='{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"}]}'
run aws_cmd iam create-policy \
--policy-name "bats-test-policy-$(unique_name)" \
--policy-document "$policy_doc"
assert_success
POLICY_ARN=$(json_get "$output" '.Policy.Arn')
[ -n "$POLICY_ARN" ]
run aws_cmd iam delete-policy --policy-arn "$POLICY_ARN"
assert_success
POLICY_ARN=""
}
@test "IAM: attach and detach role policy" {
local role_policy_doc='{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}'
local policy_doc='{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:GetObject","Resource":"*"}]}'
aws_cmd iam create-role --role-name "$ROLE_NAME" --assume-role-policy-document "$role_policy_doc" >/dev/null
out=$(aws_cmd iam create-policy --policy-name "bats-test-policy-$(unique_name)" --policy-document "$policy_doc")
POLICY_ARN=$(json_get "$out" '.Policy.Arn')
run aws_cmd iam attach-role-policy --role-name "$ROLE_NAME" --policy-arn "$POLICY_ARN"
assert_success
run aws_cmd iam detach-role-policy --role-name "$ROLE_NAME" --policy-arn "$POLICY_ARN"
assert_success
}
@test "IAM: delete role" {
local policy_doc='{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Principal":{"Service":"lambda.amazonaws.com"},"Action":"sts:AssumeRole"}]}'
aws_cmd iam create-role --role-name "$ROLE_NAME" --assume-role-policy-document "$policy_doc" >/dev/null
run aws_cmd iam delete-role --role-name "$ROLE_NAME"
assert_success
}
# ─── OIDC identity providers ────────────────────────────────────────────────
# Creates a provider and sets OIDC_ARN. Each test uses a unique URL so the suite
# stays safe when bats runs test files in parallel.
create_oidc_provider() {
local url="https://oidc.bats.example.com/id/$(date +%s)-$$-${1:-0}"
local out
out=$(aws_cmd iam create-open-id-connect-provider \
--url "$url" \
--client-id-list sts.amazonaws.com \
--thumbprint-list 9e99a48a9960b14926bb7f3b02e22da2b0ab7280)
OIDC_ARN=$(json_get "$out" '.OpenIDConnectProviderArn')
OIDC_HOST="${url#https://}"
}
@test "IAM: create and get OIDC provider" {
create_oidc_provider 1
[ -n "$OIDC_ARN" ]
run aws_cmd iam get-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN"
assert_success
# AWS reports the URL without its scheme.
url=$(json_get "$output" '.Url')
[ "$url" = "$OIDC_HOST" ]
client=$(json_get "$output" '.ClientIDList[0]')
[ "$client" = "sts.amazonaws.com" ]
aws_cmd iam delete-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN" >/dev/null
}
@test "IAM: list OIDC providers includes the created provider" {
create_oidc_provider 2
run aws_cmd iam list-open-id-connect-providers
assert_success
found=$(echo "$output" | jq --arg arn "$OIDC_ARN" '.OpenIDConnectProviderList | any(.Arn == $arn)')
[ "$found" = "true" ]
aws_cmd iam delete-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN" >/dev/null
}
@test "IAM: add and remove an OIDC client id" {
create_oidc_provider 3
run aws_cmd iam add-client-id-to-open-id-connect-provider \
--open-id-connect-provider-arn "$OIDC_ARN" --client-id extra.audience
assert_success
run aws_cmd iam get-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN"
assert_success
count=$(json_get "$output" '.ClientIDList | length')
[ "$count" = "2" ]
run aws_cmd iam remove-client-id-from-open-id-connect-provider \
--open-id-connect-provider-arn "$OIDC_ARN" --client-id extra.audience
assert_success
aws_cmd iam delete-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN" >/dev/null
}
@test "IAM: update OIDC provider thumbprint" {
create_oidc_provider 4
run aws_cmd iam update-open-id-connect-provider-thumbprint \
--open-id-connect-provider-arn "$OIDC_ARN" \
--thumbprint-list 1111111111111111111111111111111111111111
assert_success
run aws_cmd iam get-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN"
assert_success
thumb=$(json_get "$output" '.ThumbprintList[0]')
[ "$thumb" = "1111111111111111111111111111111111111111" ]
aws_cmd iam delete-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN" >/dev/null
}
@test "IAM: delete OIDC provider" {
create_oidc_provider 5
run aws_cmd iam delete-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN"
assert_success
run aws_cmd iam get-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN"
assert_failure
assert_output --partial "NoSuchEntity"
}
@test "IAM: OIDC provider url must be https" {
run aws_cmd iam create-open-id-connect-provider \
--url http://oidc.bats.example.com/id/insecure \
--thumbprint-list 9e99a48a9960b14926bb7f3b02e22da2b0ab7280
assert_failure
assert_output --partial "ValidationError"
}
@test "IAM: list account aliases is empty by default" {
run aws_cmd iam list-account-aliases
assert_success
count=$(json_get "$output" '.AccountAliases | length')
[ "$count" = "0" ]
}
@test "IAM: create and list account alias" {
ACCOUNT_ALIAS="$(unique_name bats-alias)"
run aws_cmd iam create-account-alias --account-alias "$ACCOUNT_ALIAS"
assert_success
run aws_cmd iam list-account-aliases
assert_success
alias=$(json_get "$output" '.AccountAliases[0]')
[ "$alias" = "$ACCOUNT_ALIAS" ]
}
@test "IAM: creating another account alias replaces the current one" {
ACCOUNT_ALIAS="$(unique_name bats-alias)"
aws_cmd iam create-account-alias --account-alias "$ACCOUNT_ALIAS" >/dev/null
local replacement
replacement="$(unique_name bats-alias-two)"
run aws_cmd iam create-account-alias --account-alias "$replacement"
assert_success
ACCOUNT_ALIAS="$replacement"
run aws_cmd iam list-account-aliases
assert_success
alias=$(json_get "$output" '.AccountAliases[0]')
[ "$alias" = "$replacement" ]
}
@test "IAM: re-creating the alias the account already holds fails" {
ACCOUNT_ALIAS="$(unique_name bats-alias)"
aws_cmd iam create-account-alias --account-alias "$ACCOUNT_ALIAS" >/dev/null
run aws_cmd iam create-account-alias --account-alias "$ACCOUNT_ALIAS"
assert_failure
assert_output --partial "EntityAlreadyExists"
}
@test "IAM: deleting a mismatched account alias fails" {
ACCOUNT_ALIAS="$(unique_name bats-alias)"
aws_cmd iam create-account-alias --account-alias "$ACCOUNT_ALIAS" >/dev/null
run aws_cmd iam delete-account-alias --account-alias "bats-alias-not-set-$$"
assert_failure
assert_output --partial "NoSuchEntity"
}
@test "IAM: delete account alias" {
ACCOUNT_ALIAS="$(unique_name bats-alias)"
aws_cmd iam create-account-alias --account-alias "$ACCOUNT_ALIAS" >/dev/null
run aws_cmd iam delete-account-alias --account-alias "$ACCOUNT_ALIAS"
assert_success
run aws_cmd iam list-account-aliases
assert_success
count=$(json_get "$output" '.AccountAliases | length')
[ "$count" = "0" ]
}
@test "IAM: malformed account alias is rejected" {
run aws_cmd iam create-account-alias --account-alias "Upper-Case"
assert_failure
assert_output --partial "ValidationError"
}