Files
floci/compatibility-tests/sdk-test-awscli/test/iam.bats
T
f17dd1ed81 feat(iam): support OIDC identity providers (#2106)
* feat(iam): support OIDC identity providers

Implement CreateOpenIDConnectProvider, GetOpenIDConnectProvider,
DeleteOpenIDConnectProvider, AddClientIDToOpenIDConnectProvider,
RemoveClientIDFromOpenIDConnectProvider, UpdateOpenIDConnectProviderThumbprint
and the provider tag actions, and back ListOpenIDConnectProviders with real
data instead of a fixed empty list.

A provider is identified by its URL, so the ARN is derived from it and the
scheme is stripped: GetOpenIDConnectProvider reports the URL without https://,
matching AWS. Creating the same URL twice returns EntityAlreadyExists.

Thumbprints are stored and echoed back but never validated against the remote
endpoint, since nothing here performs the TLS handshake they describe.

Closes #2102

* test(iam): cover OIDC provider persistence and cap the provider URL

Add IamServicePersistenceTest: IAM state is persisted through StorageFactory,
so a restart must preserve providers along with their client IDs, thumbprints,
tags and creation time. The model carries an Instant and two lists, so a broken
serialization round trip would surface here first.

Also enforce the documented 255-character limit on the provider URL, which was
otherwise unchecked and would have produced an oversized ARN.

* fix(iam): match verified AWS behaviour for OIDC providers

Checked the emulated surface against a live AWS account and corrected where it
diverged:

AddClientIDToOpenIDConnectProvider and RemoveClientIDFromOpenIDConnectProvider
are idempotent on AWS. Adding a client ID that is already present, and removing
one that was never added, both succeed and change nothing; they previously
raised EntityAlreadyExists and NoSuchEntity.

Exceeding the thumbprint limit reports InvalidInput, not LimitExceeded. The
client ID quota and duplicate-provider messages now use the AWS wording, and
DeleteOpenIDConnectProvider reports its own not-found phrasing.

Confirmed unchanged: the ARN is derived from the scheme-less URL, Get echoes the
URL without its scheme, the URL is capped at 255 characters, the client ID quota
is 100 with LimitExceeded, and URLs are not normalized, so a trailing slash or a
case difference yields a separate provider.

* fix(iam): reject missing required parameters on OIDC mutators

A null ClientID read as "not in the client ID list", so
RemoveClientIDFromOpenIDConnectProvider reported a no-op success for a request
that never named one. DeleteOpenIDConnectProvider had lost its ARN check when
it stopped routing through getOpenIDConnectProvider to report the not-found
wording AWS uses for that action, so a request with no ARN came back as
NoSuchEntity rather than a required-parameter error.

Route every OIDC mutator through shared ARN and client ID checks so a malformed
request fails as ValidationError before any lookup.

* fix(iam): unbreak the reflective test fixture for the new constructor arity

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

* fix(iam): reject empty tag collections on OIDC providers

TagOpenIDConnectProvider with no Tags and UntagOpenIDConnectProvider with no
TagKeys performed a no-op mutation and returned 200. AWS rejects both, as
InvalidInput rather than ValidationError, with distinct messages for the map and
the key list; checked against a live account. Validate the ARN on the tag path
too, so a missing one fails before the lookup like the other mutators.

---------

Co-authored-by: Michal Schreiber <michal.p.schreiber@gmail.com>
Co-authored-by: Michal Schreiber <michal.schreiber@benefitsystems.pl>
2026-08-11 23:07:37 -04:00

182 lines
6.9 KiB
Bash

#!/usr/bin/env bats
# IAM tests
setup() {
load 'test_helper/common-setup'
ROLE_NAME="bats-test-role-$(unique_name)"
POLICY_ARN=""
}
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
}
@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"
}