feat(iam): read IAM from the generated tables, its paths, signing user and renames from the user guide and API reference (#9252)

IAM is read from iam-request-resources.json now, and ResourceArnBuilder's
IAM branch, the last hand-built namespace, is gone: HAND_BUILT is empty.

What botocore and the Service Reference do not say comes from the IAM
User Guide's identifiers and the IAM API reference, vendored by
fetch_iam_identifiers.py into tools/aws/sources/iam-identifiers.json and
read under PERMISSION_SOURCES.

- A user, group, role, instance profile or server certificate a request
  names by its name is checked on the ARN IAM stores it under, path
  included (IamStoredResourceArns.find), and one IAM does not hold on its
  name under the root path.
- A create is checked on the name its request gives after the Path it
  gives (path), as the identifiers say the API's creates take one.
- UpdateUser, UpdateGroup and UpdateServerCertificate are also checked
  on the resource under its new name and path (renames), as their API
  reference says the caller needs permission on both.
- The twenty operations whose API reference says they act on the user
  signing the request when it leaves out UserName, and ChangePassword,
  are checked on that user (signer), which the new
  StoredResourceArns.signedBy hook gives for the signing key.
- A virtual MFA device is named by its SerialNumber, which the API
  reference gives as its ARN; the access key, SSH public key and
  service-specific credential operations name the user owning what they
  name, and GetServiceLinkedRoleDeletionStatus the role its task
  deleted (IamStoredResourceArns.through).
- MEASURED gives UpdateDelegationRequest, which the Service Reference
  does not list, the request its DelegationRequestId names, citing its
  API reference ("that owner gets a default permission to update the
  delegation request") and the user guide's temporary delegation page.
- CreateServiceLinkedRole is checked on role/*, CreateDelegationRequest
  on delegation-request/* and CreateOpenIDConnectProvider on *, as the
  table gives them. CreateOpenIDConnectProvider refuses an
  IdentityCenterInstanceArn that is not the ARN of an IAM Identity
  Center instance, with InvalidInput.
- Bedrock's ListTrustedAccessPrograms is checked as
  bedrock:CallWithBearerToken, as the Service Reference now authorizes it.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-23 08:23:04 -04:00
1 parent dee5f3c59b
commit a4b2f01713
22 files changed
+1743 -938

No files matched your search

@@ -559,7 +559,8 @@ class RequestResourceMembersTest {
ArnFormat format = table.formats().get(namespace).get(type);
String arn = example(format.format());
String account = resource.account() != null ? "111122223333" : "000000000000";
String expected = resource.arn() != null || resource.through() != null || resource.byDefault() ? arn
String expected = resource.arn() != null || resource.through() != null || resource.byDefault()
|| resource.signer() ? arn
: expected(format.format(), resource, account) + (format.minted() ? "/*"
: format.mintedSuffix() != null && resource.fill().size() == 1
? format.mintedSuffix() : "");
@@ -567,7 +568,8 @@ class RequestResourceMembersTest {
StringBuilder form = new StringBuilder("Action=" + name);
Map<Member, String> values = resource.arn() != null ? Map.of(resource.arn(), arn)
: resource.through() != null ? Map.of(resource.through(), "x")
: Stream.concat(resource.fill().values().stream(), resource.find().stream()).distinct()
: Stream.of(resource.fill().values().stream(), resource.find().stream(),
Stream.ofNullable(resource.renames())).flatMap(members -> members).distinct()
.collect(Collectors.toMap(m -> m, m -> "x"));
Map<Member, String> given = new LinkedHashMap<>(values);
if (resource.account() != null) {
@@ -599,13 +601,15 @@ class RequestResourceMembersTest {
form.append('&').append(parameter).append('=').append(encoded(sent));
});
List<String> named = operation.getValue().stream()
.filter(other -> (other == resource || named(other) || sharesAMember(other, given)
.filter(other -> (other == resource || named(other) && !other.signer()
|| signedFor(other, given) || sharesAMember(other, given)
|| fillsFromTheArn(other, resource, type)
|| !other.find().isEmpty() && other.find().equals(resource.find())
&& other.when().equals(resource.when()) && other.unless().equals(resource.unless()))
&& (other.endpoint() == null || resource.endpoint() == null
|| other.endpoint().equals(resource.endpoint())))
.flatMap(other -> other == resource ? inTheRequestsRegionToo(expected, resource).stream()
: other.signer() ? Stream.of(arn)
: Stream.of(expected(table.formats().get(namespace).get(other.types().getFirst())
.format(), other, "000000000000", given.keySet())))
.distinct().toList();
@@ -714,6 +718,17 @@ class RequestResourceMembersTest {
.map(other -> other.types().getFirst()).findFirst().orElse(resource.types().getFirst());
}
/**
* Whether a request giving these members names the user who signs it: the resource is that user, and every member
* it is named {@code when} is absent from them, as each value list holds absent as {@code ""}.
*/
private static boolean signedFor(Resource resource, Map<Member, String> given) {
Map<String, String> values = given.entrySet().stream()
.collect(Collectors.toMap(entry -> entry.getKey().name(), Map.Entry::getValue, (a, b) -> a));
return resource.signer() && resource.when().entrySet().stream()
.allMatch(member -> member.getValue().contains(values.getOrDefault(member.getKey(), "")));
}
/** Whether every request of its operation names the resource: no member names it and no member's value selects it. */
private static boolean named(Resource resource) {
return resource.arn() == null && resource.fill().isEmpty() && resource.find().isEmpty()
@@ -721,11 +736,17 @@ class RequestResourceMembersTest {
}
/**
* A namespace's stored ARNs that find nothing, and give {@code arn} for whatever a request names a resource through
* and, when {@code defaults}, as the resource of any type the service acts on for a request naming none.
* A namespace's stored ARNs that find nothing, and give {@code arn} for whatever a request names a resource
* through, as the user whose key signs a request and, when {@code defaults}, as the resource of any type the
* service acts on for a request naming none.
*/
private static StoredResourceArns runningOn(String namespace, String arn, boolean defaults) {
return new StoredResourceArns() {
@Override
public Optional<String> signedBy(String accessKeyId) {
return Optional.of(arn);
}
@Override
public Optional<String> findDefault(String type, String region) {
return defaults ? Optional.of(arn) : Optional.empty();
@@ -946,6 +967,8 @@ class RequestResourceMembersTest {
when(request.getMethod()).thenReturn("POST");
when(request.getMediaType()).thenReturn(MediaType.valueOf(mediaType));
when(request.getEntityStream()).thenAnswer(invocation -> new ByteArrayInputStream(bytes));
when(request.getHeaderString("Authorization"))
.thenReturn("AWS4-HMAC-SHA256 Credential=AKIAEXAMPLE/20261123/us-east-1/iam/aws4_request");
return request;
}
}
@@ -7,8 +7,6 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -75,84 +73,6 @@ class IamActionResourcesTest {
formats.get("user"));
}
@Test
void everyCreateIsAuthorizedAgainstExactlyOneResourceType() {
// A create has nothing to read the resource off, so ResourceArnBuilder mints the ARN from
// the type AWS gives the action. One type is what lets it mint without being told which,
// and two would leave it picking. Iterated rather than restated: a create added to the
// resolver that AWS gives no single type falls back to the wildcard, which is the
// permissive answer and a silent one, so this is what says it out loud.
for (String create : ResourceArnBuilder.CREATE_PARAMETERS.keySet()) {
assertEquals(1, IamActionResources.typesOf(create).size(),
create + " is no longer authorized against exactly one resource type, so "
+ "there is no single ARN for it to mint: "
+ IamActionResources.typesOf(create));
}
}
@Test
void anArnSpellsTheResourceTypeInFrontOfTheName() {
// What minting assumes. ResourceArnBuilder builds `:<type>/<name>`, which holds only
// while AWS spells that type's ARN the same way, and the reference publishes the format,
// so the assumption is checked rather than carried.
for (Map.Entry<String, List<String>> entry : IamActionResources.arnFormats().entrySet()) {
for (String arnFormat : entry.getValue()) {
assertTrue(arnFormat.contains(":" + entry.getKey() + "/"),
"AWS spells a " + entry.getKey() + " ARN as " + arnFormat
+ ", which this resolver's minting does not match");
}
}
}
@Test
void everyResourceTypeInUseIsOneTheResolverCoversOrDeclaresUnresolved() {
// Read off the resolver's own tables rather than restated, so a type cannot be waved
// through by adding it to a list in this file. The resolver answers `*` for a type it
// does not know, which is the permissive answer and a silent one.
Set<String> covered = new TreeSet<>(ResourceArnBuilder.NAME_PARAMETERS.keySet());
covered.addAll(ResourceArnBuilder.UNRESOLVED_TYPES.keySet());
for (String action : ResourceArnBuilder.IAM_ARN_PARAMETERS.keySet()) {
covered.addAll(IamActionResources.typesOf(action));
}
for (String action : ResourceArnBuilder.CREATE_PARAMETERS.keySet()) {
covered.addAll(IamActionResources.typesOf(action));
}
for (String action : ResourceArnBuilder.DERIVED_RESOLVERS.keySet()) {
covered.addAll(IamActionResources.typesOf(action));
}
for (Map.Entry<String, List<String>> entry : IamActionResources.all().entrySet()) {
for (String resourceType : entry.getValue()) {
assertTrue(covered.contains(resourceType), "AWS authorizes " + entry.getKey()
+ " against '" + resourceType + "', which ResourceArnBuilder neither "
+ "resolves nor lists as unresolved, so the action falls back to *");
}
}
}
@Test
void theResourceTypesInUseAreTheOnesTheResolverKnowsAbout() {
// The upstream half of the same question: which types AWS uses at all. The test above
// catches a type nothing resolves; this one catches AWS changing the set, including a
// type disappearing, which leaves a dead entry behind rather than a gap.
Set<String> inUse = new TreeSet<>();
IamActionResources.all().values().forEach(inUse::addAll);
assertEquals(Set.of(
// Named by a parameter the resolver reads.
"user", "role", "group", "instance-profile", "server-certificate",
// Named by an ARN the request carries, or minted by a create.
"policy", "mfa", "sms-mfa", "saml-provider", "oidc-provider",
// Named by a parameter that is not an IAM entity name.
"access-report",
// Derived from an id the request carries.
"delegation-request",
// Named by an ARN the request carries, in the aws account rather than the
// caller's.
"role-template"),
inUse,
"AWS has changed which resource types IAM actions are authorized against; "
+ "teach ResourceArnBuilder the new one or list it as unresolved");
}
@Test
void aMapMissingItsActionsIsABuildDefect() {
assertThrows(IllegalStateException.class, () -> parse("{\"resourceArnFormats\":{}}"));
@@ -207,6 +207,33 @@ class IamOpenIDConnectProviderIntegrationTest {
.body("ErrorResponse.Error.Code", equalTo("ValidationError"));
}
/**
* IdentityCenterInstanceArn names an IAM Identity Center instance; the ARN of a provider there is refused, and no
* provider is made for the request's Url.
*/
@Test
@Order(9)
void anIdentityCenterInstanceArnNamingAnythingElseIsRejected() {
given()
.formParam("Action", "CreateOpenIDConnectProvider")
.formParam("Url", "https://oidc.example.com/id/spoofed")
.formParam("IdentityCenterInstanceArn", providerArn)
.header("Authorization", IAM_CREDENTIAL)
.when()
.post("/")
.then()
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidInput"));
given()
.formParam("Action", "GetOpenIDConnectProvider")
.formParam("OpenIDConnectProviderArn", providerArn.replace(OIDC_HOST, "oidc.example.com/id/spoofed"))
.header("Authorization", IAM_CREDENTIAL)
.when()
.post("/")
.then()
.statusCode(404);
}
@Test
@Order(10)
void deleteOpenIDConnectProvider() {
@@ -0,0 +1,104 @@
package io.floci.aws.services.iam;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.SshPublicKeys;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.services.iam.model.IamRole;
import jakarta.enterprise.inject.Instance;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.security.KeyPairGenerator;
import java.security.interfaces.RSAPublicKey;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* What IAM gives the request-resources table of its own resources: the ARN a name names, path included, the user a key
* or a credential belongs to, the role a deletion task deleted and the user who signs a request.
*/
class IamStoredResourceArnsTest {
private static final String ACCOUNT = "000000000000";
private static final String TRUST_POLICY = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
+ "\"Principal\":{\"Service\":\"ec2.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}";
private IamService iam;
private IamStoredResourceArns stored;
@BeforeEach
void setUp() {
iam = new IamService(
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
new InMemoryStorage<>(), new RegionResolver("us-east-1", ACCOUNT), false, null);
@SuppressWarnings("unchecked")
Instance<IamService> service = mock(Instance.class);
when(service.get()).thenReturn(iam);
@SuppressWarnings("unchecked")
Instance<DelegationRequestOwnership> delegations = mock(Instance.class);
when(delegations.isUnsatisfied()).thenReturn(true);
stored = new IamStoredResourceArns(delegations, service, new RegionResolver("us-east-1", ACCOUNT));
}
@Test
void aNameNamesTheArnItIsStoredUnderPathIncluded() {
iam.createUser("bob", "/team/");
iam.createRole("app", "/service-role/", TRUST_POLICY, null, 0, null);
assertEquals(Optional.of("arn:aws:iam::000000000000:user/team/bob"), stored.find("user", List.of("bob"), ""));
assertEquals(Optional.of("arn:aws:iam::000000000000:role/service-role/app"),
stored.find("role", List.of("app"), ""));
assertEquals(Optional.empty(), stored.find("user", List.of("alice"), ""));
assertEquals(Optional.empty(), stored.find("policy", List.of("bob"), ""));
}
@Test
void aKeyOrACredentialNamesTheUserItBelongsTo() {
iam.createUser("bob", "/team/");
String bob = "arn:aws:iam::000000000000:user/team/bob";
String accessKey = iam.createAccessKey("bob").getAccessKeyId();
String sshKey = iam.uploadSshPublicKey("bob", SshPublicKeys.toOpenSsh(generated())).getSshPublicKeyId();
String credential = iam.createServiceSpecificCredential("bob", "codecommit.amazonaws.com", null)
.getServiceSpecificCredentialId();
assertEquals(List.of(bob), stored.through("AccessKeyId", accessKey, ""));
assertEquals(List.of(bob), stored.through("SSHPublicKeyId", sshKey, ""));
assertEquals(List.of(bob), stored.through("ServiceSpecificCredentialId", credential, ""));
assertEquals(List.of(), stored.through("AccessKeyId", "AKIANOTHELD", ""));
assertEquals(List.of(), stored.through("UserName", "bob", ""));
}
@Test
void aDeletionTaskNamesTheRoleItDeleted() {
IamRole role = iam.createServiceLinkedRole("es.amazonaws.com", null, null);
String task = iam.deleteServiceLinkedRole(role.getRoleName());
assertEquals(List.of(role.getArn()), stored.through("DeletionTaskId", task, ""));
assertEquals(List.of(), stored.through("DeletionTaskId", "task/aws-service-role/es.amazonaws.com/x/y", ""));
}
@Test
void aSigningKeyNamesItsUser() {
iam.createUser("bob", "/team/");
String accessKey = iam.createAccessKey("bob").getAccessKeyId();
assertEquals(Optional.of("arn:aws:iam::000000000000:user/team/bob"), stored.signedBy(accessKey));
assertEquals(Optional.empty(), stored.signedBy("AKIANOTHELD"));
}
private static RSAPublicKey generated() {
try {
KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
generator.initialize(2048);
return (RSAPublicKey) generator.generateKeyPair().getPublic();
} catch (Exception e) {
throw new IllegalStateException("could not generate a key", e);
}
}
}
@@ -87,6 +87,8 @@ class ResourceArnBuilderTest {
private UriInfo uriInfo;
private MultivaluedMap<String, String> queryParams;
private Map<String, Object> contextProperties;
/** The form {@link #setFormBody} last sent, whose {@code Action} or {@code Operation} is what IAM dispatches. */
private String sentForm;
@ParameterizedTest
@CsvSource({"aws,us-east-1", "aws-us-gov,us-gov-west-1", "aws-cn,cn-north-1"})
@@ -124,6 +126,7 @@ class ResourceArnBuilderTest {
/** Query-protocol services carry their parameters in a form body, as IAM does. */
private void setFormBody(String form) {
sentForm = form;
contextProperties.clear();
byte[] bytes = form.getBytes(StandardCharsets.UTF_8);
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
@@ -183,18 +186,21 @@ class ResourceArnBuilderTest {
stored.setArn(arn);
IamService service = mock(IamService.class);
when(service.findServerCertificate(name)).thenReturn(Optional.of(stored));
@SuppressWarnings("unchecked")
Instance<IamService> instance = mock(Instance.class);
when(instance.get()).thenReturn(service);
return new ResourceArnBuilder(new ObjectMapper(), instance);
return backedBy(service);
}
/** A builder reading {@code service}, for the actions that name a resource that exists. */
/**
* A builder reading {@code service} through IAM's stored ARNs, as the filter's does, for the actions that name a
* resource that exists.
*/
private static ResourceArnBuilder backedBy(IamService service) {
@SuppressWarnings("unchecked")
Instance<IamService> instance = mock(Instance.class);
when(instance.get()).thenReturn(service);
return new ResourceArnBuilder(new ObjectMapper(), instance);
@SuppressWarnings("unchecked")
Instance<DelegationRequestOwnership> delegations = mock(Instance.class);
when(delegations.isUnsatisfied()).thenReturn(true);
return reading(new IamStoredResourceArns(delegations, instance, new RegionResolver("us-east-1", ACCOUNT)));
}
private static IamService storeHolding(String userName, String path, String arn) {
@@ -210,7 +216,28 @@ class ResourceArnBuilderTest {
/** The ARN the builder gives for the request in {@code form}, with one resource expected. */
private String resourceOf(ResourceArnBuilder resourceArnBuilder, String form) {
setFormBody(form);
return resourceArnBuilder.build("iam", ctx, "us-east-1", ACCOUNT);
return iamResource(resourceArnBuilder, "us-east-1", ACCOUNT);
}
/** The first of {@link #iamResources}, as the filter's single resource. */
private String iamResource(ResourceArnBuilder resourceArnBuilder, String region, String account) {
return iamResources(resourceArnBuilder, region, account).getFirst();
}
/**
* The resources the builder gives the IAM request set up, in the request's partition, for the operation IAM
* dispatches it as: the form's {@code Action}, or its legacy {@code Operation}, or else the query's.
*/
private List<String> iamResources(ResourceArnBuilder resourceArnBuilder, String region, String account) {
String action = queryParams.getFirst("Action");
for (String parameter : sentForm == null ? new String[0] : sentForm.split("&")) {
if (parameter.startsWith("Action=") || parameter.startsWith("Operation=")) {
action = parameter.substring(parameter.indexOf('=') + 1);
break;
}
}
return resourceArnBuilder.buildResources("iam", "iam:" + action, ctx, region, account,
AwsRegions.partitionFor(region));
}
/**
@@ -251,22 +278,19 @@ class ResourceArnBuilderTest {
@Test
void iamNamesTheDelegationRequestABlockOfActionsActsOn() {
String id = "dr-0123456789abcdef01234567";
// The forward is in this block now. AWS's "Managing Permissions for Delegation Requests"
// lists iam:UpdateDelegationRequest among the permissions an administrator grants, as
// "Forward a delegation request to an administrator for approval", so it is authorizable
// and names the request its id names, where this suite used to pin it at the wildcard on
// the belief that AWS published no action for it.
// The forward is one of them: the Service Reference does not list it, and its API reference says "that owner
// gets a default permission to update the delegation request" its DelegationRequestId names.
for (String action : List.of("AssociateDelegationRequest", "AcceptDelegationRequest",
"RejectDelegationRequest", "GetDelegationRequest", "UpdateDelegationRequest")) {
"RejectDelegationRequest", "GetDelegationRequest", "SendDelegationToken", "UpdateDelegationRequest")) {
assertEquals("arn:aws:iam::000000000000:delegation-request/" + id,
resourceOf(builder, "Action=" + action + "&DelegationRequestId=" + id), action);
}
}
@Test
void iamLeavesADelegationRequestItIsAboutToMintAtTheWildcard() {
// IAM assigns the id, so the create names no resource to authorize against.
assertEquals("*", resourceOf(builder,
void iamLeavesTheIdOfADelegationRequestItIsAboutToMintOpen() {
// IAM assigns the id, so the create is checked on a request of any id.
assertEquals("arn:aws:iam::000000000000:delegation-request/*", resourceOf(builder,
"Action=CreateDelegationRequest&Description=d&RequestorWorkflowId=w"));
}
@@ -353,7 +377,7 @@ class ResourceArnBuilderTest {
void iamMintsACreateInTheRequestsPartition() {
setFormBody("Action=CreateUser&UserName=bob");
assertEquals("arn:aws-cn:iam::000000000000:user/bob",
builder.build("iam", ctx, "cn-north-1", ACCOUNT));
iamResource(builder, "cn-north-1", ACCOUNT));
}
/**
@@ -388,17 +412,37 @@ class ResourceArnBuilderTest {
}
/**
* A SAML provider's ARN is its bare name and an OIDC provider's is its URL without the
* scheme, neither of which carries a path. Minting them the way the pathed types are minted
* would name resources that do not exist.
* {@code SimulatePrincipalPolicy} is checked on each user, role or group its request names by ARN, as the table
* reads every member holding an ARN of a type the action acts on: its {@code PolicySourceArn}, and a
* {@code CallerArn} or {@code ResourceArns} entry naming one too. A resource of another service there names nothing
* of IAM's, so a simulation of S3 objects is checked on its source principal alone.
*/
@Test
void iamMintsProviderArnsWithoutAPath() {
void iamChecksASimulationOnEachPrincipalItsRequestNames() {
String alice = "arn:aws:iam::000000000000:user/alice";
String bob = "arn:aws:iam::000000000000:user/bob";
setFormBody("Action=SimulatePrincipalPolicy&ActionNames.member.1=s3%3AGetObject"
+ "&PolicySourceArn=" + URLEncoder.encode(alice, StandardCharsets.UTF_8)
+ "&ResourceArns.member.1=" + URLEncoder.encode(bob, StandardCharsets.UTF_8)
+ "&ResourceArns.member.2=arn%3Aaws%3As3%3A%3A%3Abucket%2Fkey");
assertEquals(List.of(alice, bob), iamResources(builder, "us-east-1", ACCOUNT));
setFormBody("Action=SimulatePrincipalPolicy&ActionNames.member.1=s3%3AGetObject"
+ "&PolicySourceArn=" + URLEncoder.encode(alice, StandardCharsets.UTF_8)
+ "&ResourceArns.member.1=arn%3Aaws%3As3%3A%3A%3Abucket%2Fkey");
assertEquals(List.of(alice), iamResources(builder, "us-east-1", ACCOUNT));
}
/**
* A SAML provider's ARN is its bare name, which carries no path. An OIDC provider's is its URL without the scheme,
* which no source the table reads says of the request's {@code Url}, so that create names nothing and is checked
* on {@code *}.
*/
@Test
void iamNamesASamlProviderByItsNameAndAnOidcCreateByNothing() {
assertEquals("arn:aws:iam::000000000000:saml-provider/corp",
resourceOf(builder, "Action=CreateSAMLProvider&Name=corp"));
assertEquals("arn:aws:iam::000000000000:oidc-provider/example.com/app",
resourceOf(builder, "Action=CreateOpenIDConnectProvider"
+ "&Url=https%3A%2F%2Fexample.com%2Fapp"));
assertEquals("*", resourceOf(builder, "Action=CreateOpenIDConnectProvider"
+ "&Url=https%3A%2F%2Fexample.com%2Fapp"));
}
/**
@@ -411,8 +455,7 @@ class ResourceArnBuilderTest {
String userArn = "arn:aws:iam::000000000000:user/bob";
setFormBody("Action=UpdateUser&UserName=bob&NewUserName=robert");
assertEquals(List.of(userArn, "arn:aws:iam::000000000000:user/robert"),
backedBy(storeHolding("bob", "/", userArn))
.buildResources("iam", ctx, "us-east-1", ACCOUNT));
iamResources(backedBy(storeHolding("bob", "/", userArn)), "us-east-1", ACCOUNT));
String groupArn = "arn:aws:iam::000000000000:group/managers";
IamGroup group = new IamGroup();
@@ -423,31 +466,17 @@ class ResourceArnBuilderTest {
when(store.findGroup(ACCOUNT, "managers")).thenReturn(Optional.of(group));
setFormBody("Action=UpdateGroup&GroupName=managers&NewGroupName=mgrs");
assertEquals(List.of(groupArn, "arn:aws:iam::000000000000:group/mgrs"),
backedBy(store).buildResources("iam", ctx, "us-east-1", ACCOUNT));
iamResources(backedBy(store), "us-east-1", ACCOUNT));
}
/**
* {@code CreateServiceLinkedRole} does not name the role it creates. AWS derives it from
* {@code AWSServiceName}, so the same table the create reads gives the name, and the role
* sits under the service-role path carrying the canonical principal.
* {@code CreateServiceLinkedRole} does not name the role it creates: AWS derives it from {@code AWSServiceName},
* which no source the table reads says, so the create is checked on a role with its path and name open.
*/
@Test
void iamNamesTheRoleAServiceLinkedCreateWouldMint() {
assertEquals("arn:aws:iam::000000000000:role/aws-service-role/autoscaling.amazonaws.com/"
+ "AWSServiceRoleForAutoScaling",
resourceOf(builder, "Action=CreateServiceLinkedRole"
+ "&AWSServiceName=autoscaling.amazonaws.com"));
// A suffix is part of the name AWS mints, so it is part of the resource too.
assertEquals("arn:aws:iam::000000000000:role/aws-service-role/autoscaling.amazonaws.com/"
+ "AWSServiceRoleForAutoScaling_fleet",
resourceOf(builder, "Action=CreateServiceLinkedRole"
+ "&AWSServiceName=autoscaling.amazonaws.com&CustomSuffix=fleet"));
// A principal the table does not carry keeps the derived name, which is what the create
// mints for it: the two read the same table, so the check cannot name a different role.
assertEquals("arn:aws:iam::000000000000:role/aws-service-role/es.amazonaws.com/"
+ "AWSServiceRoleForEs",
resourceOf(builder,
"Action=CreateServiceLinkedRole&AWSServiceName=es.amazonaws.com"));
void iamChecksAServiceLinkedCreateOnAnyRole() {
assertEquals("arn:aws:iam::000000000000:role/*", resourceOf(builder, "Action=CreateServiceLinkedRole"
+ "&AWSServiceName=autoscaling.amazonaws.com&CustomSuffix=fleet"));
}
/**
@@ -487,7 +516,7 @@ class ResourceArnBuilderTest {
iam.createUser("worker", "/");
setFormBody("Action=GetAccessKeyLastUsed&AccessKeyId=AKIALEGACYEXAMPLE");
assertEquals(List.of("*"), backedBy(iam).buildResources("iam", ctx, "us-east-1", stranger));
assertEquals(List.of("*"), iamResources(backedBy(iam), "us-east-1", stranger));
assertTrue(raw.get("AKIALEGACYEXAMPLE").isPresent(), "the lookup must leave the key unmoved");
assertTrue(raw.get(stranger + "/AKIALEGACYEXAMPLE").isEmpty(), "the lookup must not copy the key");
}
@@ -503,9 +532,8 @@ class ResourceArnBuilderTest {
}
/**
* An action whose resource the request does not name stays at {@code *}: the ones the
* reference gives no resource type, and the ones that default to the calling user, whose
* identity is the filter's to supply rather than this builder's.
* An action whose resource the request does not name stays at {@code *}: the ones the reference gives no resource
* type, and the ones acting on the user who signs them, with no stored ARNs to give that user.
*/
@Test
void iamFallsBackToTheWildcardWhenTheRequestNamesNoResource() {
@@ -517,22 +545,53 @@ class ResourceArnBuilderTest {
}
/**
* The deletion task identifier carries the role's path and name, in the format the action's
* reference publishes, which is the only thing left to name: the role itself is already gone
* by the time the status is asked for.
* The role a deletion task deleted, which IAM gives for the task it holds: the role itself is already gone by the
* time the status is asked for. A task IAM does not hold names nothing rather than half a resource.
*/
@Test
void iamNamesTheRoleADeletionTaskIdentifies() {
assertEquals("arn:aws:iam::000000000000:role/aws-service-role/lex.amazonaws.com/"
+ "AWSServiceRoleForLexBots",
resourceOf(builder, "Action=GetServiceLinkedRoleDeletionStatus&DeletionTaskId="
+ "task%2Faws-service-role%2Flex.amazonaws.com"
+ "%2FAWSServiceRoleForLexBots%2F12345678-1234-1234-1234-123456789012"));
// A malformed identifier names nothing rather than half a resource.
for (String taskId : List.of("task%2Faws-service-role%2F", "nonsense", "task%2Fother%2Fx")) {
assertEquals("*", resourceOf(builder,
"Action=GetServiceLinkedRoleDeletionStatus&DeletionTaskId=" + taskId), taskId);
}
String task = "task/aws-service-role/lex.amazonaws.com/AWSServiceRoleForLexBots/"
+ "12345678-1234-1234-1234-123456789012";
String role = "arn:aws:iam::000000000000:role/aws-service-role/lex.amazonaws.com/AWSServiceRoleForLexBots";
IamService store = mock(IamService.class);
when(store.findServiceLinkedRoleDeletionArn(task)).thenReturn(Optional.of(role));
when(store.findServiceLinkedRoleDeletionArn("nonsense")).thenReturn(Optional.empty());
assertEquals(role, resourceOf(backedBy(store), "Action=GetServiceLinkedRoleDeletionStatus&DeletionTaskId="
+ URLEncoder.encode(task, StandardCharsets.UTF_8)));
assertEquals("*", resourceOf(backedBy(store),
"Action=GetServiceLinkedRoleDeletionStatus&DeletionTaskId=nonsense"));
}
/**
* An operation acting on the user who signs it when its request names none, as {@code GetUser} with no
* {@code UserName} and {@code ChangePassword} always do, is checked on that user's stored ARN, path included, or on
* the account root's for a key of the root; a role session's key names no user, and its {@code UserName}, given,
* names the user it gives.
*/
@Test
void iamNamesTheUserWhoSignsARequestNamingNone() {
String bob = "arn:aws:iam::000000000000:user/team/bob";
IamService store = storeHolding("bob", "/team/", bob);
when(store.findUserNameByAccessKeyId("AKIABOB")).thenReturn(Optional.of("bob"));
when(store.resolveCallerArn("AKIAROOT")).thenReturn(Optional.of("arn:aws:iam::000000000000:root"));
when(store.resolveCallerArn("ASIASESSION"))
.thenReturn(Optional.of("arn:aws:sts::000000000000:assumed-role/app/s"));
ResourceArnBuilder stored = backedBy(store);
when(ctx.getHeaderString("Authorization")).thenReturn(signedBy("AKIABOB"));
assertEquals(bob, resourceOf(stored, "Action=GetUser"));
assertEquals(bob, resourceOf(stored, "Action=ChangePassword&OldPassword=a&NewPassword=b"));
assertEquals(bob, resourceOf(stored, "Action=ListAccessKeys"));
assertEquals("arn:aws:iam::000000000000:user/alice", resourceOf(stored, "Action=GetUser&UserName=alice"));
when(ctx.getHeaderString("Authorization")).thenReturn(signedBy("AKIAROOT"));
assertEquals("arn:aws:iam::000000000000:root", resourceOf(stored, "Action=GetUser"));
when(ctx.getHeaderString("Authorization")).thenReturn(signedBy("ASIASESSION"));
assertEquals("*", resourceOf(stored, "Action=GetUser"));
}
/** A SigV4 {@code Authorization} header signed with {@code accessKeyId}. */
private static String signedBy(String accessKeyId) {
return "AWS4-HMAC-SHA256 Credential=" + accessKeyId + "/20261123/us-east-1/iam/aws4_request, "
+ "SignedHeaders=host, Signature=0";
}
/**
@@ -575,7 +634,7 @@ class ResourceArnBuilderTest {
void iamNamesTheServerCertificateBeingActedOn() {
String arn = "arn:aws:iam::000000000000:server-certificate/my-cert";
setFormBody("Action=DeleteServerCertificate&ServerCertificateName=my-cert");
assertEquals(arn, backedBy("my-cert", arn).build("iam", ctx, "us-east-1", "000000000000"));
assertEquals(arn, iamResource(backedBy("my-cert", arn), "us-east-1", "000000000000"));
}
@Test
@@ -586,11 +645,11 @@ class ResourceArnBuilderTest {
"DeleteServerCertificate", "TagServerCertificate", "UntagServerCertificate",
"ListServerCertificateTags")) {
setFormBody("Action=" + action + "&ServerCertificateName=c1");
assertEquals(arn, stored.build("iam", ctx, "us-east-1", "000000000000"), action);
assertEquals(arn, iamResource(stored, "us-east-1", "000000000000"), action);
}
// The upload is the one that mints rather than reads: nothing is stored yet.
setFormBody("Action=UploadServerCertificate&ServerCertificateName=c1");
assertEquals(arn, builder.build("iam", ctx, "us-east-1", "000000000000"));
assertEquals(arn, iamResource(builder, "us-east-1", "000000000000"));
}
/**
@@ -617,7 +676,7 @@ class ResourceArnBuilderTest {
for (String region : List.of("us-east-1", "cn-north-1", "us-gov-west-1")) {
setFormBody(spelling + "=" + action + "&ServerCertificateName=c1");
assertEquals(storedArn,
stored.build("iam", ctx, region, "000000000000"),
iamResource(stored, region, "000000000000"),
action + " named by " + spelling + ", signed in " + region);
}
}
@@ -637,7 +696,7 @@ class ResourceArnBuilderTest {
+ "&NewServerCertificateName=new");
assertEquals(
List.of(stored, "arn:aws:iam::000000000000:server-certificate/new"),
backedBy("old", stored).buildResources("iam", ctx, "us-east-1", "000000000000"));
iamResources(backedBy("old", stored), "us-east-1", "000000000000"));
}
/** A move renames the path, so the destination path is named the same way. */
@@ -647,7 +706,7 @@ class ResourceArnBuilderTest {
setFormBody("Action=UpdateServerCertificate&ServerCertificateName=old&NewPath=/team/");
assertEquals(
List.of(stored, "arn:aws:iam::000000000000:server-certificate/team/old"),
backedBy("old", stored).buildResources("iam", ctx, "us-east-1", "000000000000"));
iamResources(backedBy("old", stored), "us-east-1", "000000000000"));
}
/**
@@ -659,7 +718,7 @@ class ResourceArnBuilderTest {
String stored = "arn:aws:iam::000000000000:server-certificate/old";
setFormBody("Action=UpdateServerCertificate&ServerCertificateName=old");
assertEquals(List.of(stored),
backedBy("old", stored).buildResources("iam", ctx, "us-east-1", "000000000000"));
iamResources(backedBy("old", stored), "us-east-1", "000000000000"));
}
/**
@@ -674,16 +733,16 @@ class ResourceArnBuilderTest {
+ "&NewServerCertificateName=new");
assertEquals(
List.of(stored, "arn:aws-cn:iam::000000000000:server-certificate/new"),
backedBy("old", stored).buildResources("iam", ctx, "us-east-1", "000000000000"));
iamResources(backedBy("old", stored), "us-east-1", "000000000000"));
}
@Test
void missingGetRoleUsesItsNameButCannotAcceptAnInjectedPath() {
setFormBody("Action=GetRole&RoleName=local-storage&Path=/injected/");
assertEquals("arn:aws:iam::000000000000:role/local-storage",
builder.build("iam", ctx, "us-east-1", ACCOUNT));
iamResource(builder, "us-east-1", ACCOUNT));
assertEquals("arn:aws-us-gov:iam::000000000000:role/local-storage",
builder.build("iam", ctx, "us-gov-west-1", ACCOUNT));
iamResource(builder, "us-gov-west-1", ACCOUNT));
}
@Test
@@ -767,14 +826,14 @@ class ResourceArnBuilderTest {
queryParams.add("RoleName", "query-decoy");
setFormBody("Operation=GetRole&RoleName=first&RoleName=duplicate&Path=/injected/");
assertEquals("arn:aws:iam::111122223333:role/first",
builder.build("iam", ctx, "us-east-1", "111122223333"));
iamResource(builder, "us-east-1", "111122223333"));
for (String action : List.of("DeleteRole", "ListRolePolicies", "TagRole", "UpdateRole")) {
setFormBody("Action=" + action + "&RoleName=first&Path=/injected/");
assertEquals("arn:aws:iam::" + ACCOUNT + ":role/first",
builder.build("iam", ctx, "us-east-1", ACCOUNT), action);
iamResource(builder, "us-east-1", ACCOUNT), action);
}
setFormBody("Action=GetRole");
assertEquals("*", builder.build("iam", ctx, "us-east-1", ACCOUNT));
assertEquals("*", iamResource(builder, "us-east-1", ACCOUNT));
}
@Test
@@ -786,7 +845,7 @@ class ResourceArnBuilderTest {
queryParams.add("RoleName", "first");
queryParams.add("RoleName", "duplicate");
queryParams.add("Path", "/injected/");
assertEquals("arn:aws:iam::000000000000:role/first", builder.build("iam", ctx, "us-east-1", ACCOUNT));
assertEquals("arn:aws:iam::000000000000:role/first", iamResource(builder, "us-east-1", ACCOUNT));
queryParams.clear();
queryParams.add("Action", "CreateTags");
queryParams.add("ResourceId.1", "i-first");
@@ -797,15 +856,13 @@ class ResourceArnBuilderTest {
builder.buildResources("ec2", "ec2:CreateTags", ctx, "us-east-1", ACCOUNT, "aws"));
}
/**
* Nothing stored under that name means nothing to name: the operation fails as NoSuchEntity
* regardless, and minting an ARN for an absent resource would hand a policy something to match.
*/
/** A certificate IAM does not hold is named as its name alone names it, under the root path. */
@Test
void iamFallsBackToTheWildcardWhenNoCertificateIsStored() {
void iamNamesACertificateItDoesNotHoldUnderTheRootPath() {
setFormBody("Action=DeleteServerCertificate&ServerCertificateName=absent");
assertEquals("*", backedBy("other", "arn:aws:iam::000000000000:server-certificate/other")
.build("iam", ctx, "us-east-1", "000000000000"));
assertEquals("arn:aws:iam::000000000000:server-certificate/absent",
iamResource(backedBy("other", "arn:aws:iam::000000000000:server-certificate/other"), "us-east-1",
"000000000000"));
}
/**
@@ -817,17 +874,17 @@ class ResourceArnBuilderTest {
void iamTakesTheUploadPathFromTheRequest() {
setFormBody("Action=UploadServerCertificate&ServerCertificateName=c1&Path=/team/");
assertEquals("arn:aws:iam::000000000000:server-certificate/team/c1",
builder.build("iam", ctx, "us-east-1", "000000000000"));
iamResource(builder, "us-east-1", "000000000000"));
// Written without surrounding slashes, it still lands as a path segment.
setFormBody("Action=UploadServerCertificate&ServerCertificateName=c1&Path=team");
assertEquals("arn:aws:iam::000000000000:server-certificate/team/c1",
builder.build("iam", ctx, "us-east-1", "000000000000"));
iamResource(builder, "us-east-1", "000000000000"));
// Omitted, it is the root path.
setFormBody("Action=UploadServerCertificate&ServerCertificateName=c1");
assertEquals("arn:aws:iam::000000000000:server-certificate/c1",
builder.build("iam", ctx, "us-east-1", "000000000000"));
iamResource(builder, "us-east-1", "000000000000"));
}
/**
@@ -838,11 +895,11 @@ class ResourceArnBuilderTest {
void iamMintsAnUploadedCertificateArnInTheRequestPartition() {
setFormBody("Action=UploadServerCertificate&ServerCertificateName=my-cert");
assertEquals("arn:aws-cn:iam::000000000000:server-certificate/my-cert",
builder.build("iam", ctx, "cn-north-1", "000000000000"));
iamResource(builder, "cn-north-1", "000000000000"));
setFormBody("Action=UploadServerCertificate&ServerCertificateName=my-cert");
assertEquals("arn:aws-us-gov:iam::000000000000:server-certificate/my-cert",
builder.build("iam", ctx, "us-gov-west-1", "000000000000"));
iamResource(builder, "us-gov-west-1", "000000000000"));
}
/**
@@ -856,26 +913,20 @@ class ResourceArnBuilderTest {
ResourceArnBuilder stored = backedBy("my-cert", arn);
setFormBody("Action=DeleteServerCertificate&ServerCertificateName=my-cert");
assertEquals(arn, stored.build("iam", ctx, "cn-north-1", "000000000000"));
assertEquals(arn, iamResource(stored, "cn-north-1", "000000000000"));
setFormBody("Action=DeleteServerCertificate&ServerCertificateName=my-cert");
assertEquals(arn, stored.build("iam", ctx, "us-gov-west-1", "000000000000"));
assertEquals(arn, iamResource(stored, "us-gov-west-1", "000000000000"));
}
/**
* ListServerCertificates names no certificate, and every other IAM action is still unmapped,
* so both resolve to the wildcard rather than to a fabricated ARN.
*/
/** ListServerCertificates names no certificate, nor does a delete leaving its name out: both are on {@code *}. */
@Test
void iamFallsBackToTheWildcardWhenNoCertificateIsNamed() {
setFormBody("Action=ListServerCertificates&PathPrefix=/team/");
assertEquals("*", builder.build("iam", ctx, "us-east-1", "000000000000"));
setFormBody("Action=DeleteUser&UserName=bob");
assertEquals("*", builder.build("iam", ctx, "us-east-1", "000000000000"));
assertEquals("*", iamResource(builder, "us-east-1", "000000000000"));
setFormBody("Action=DeleteServerCertificate");
assertEquals("*", builder.build("iam", ctx, "us-east-1", "000000000000"));
assertEquals("*", iamResource(builder, "us-east-1", "000000000000"));
}
private void setJsonBody(String json) {
@@ -1448,7 +1499,7 @@ class ResourceArnBuilderTest {
@SuppressWarnings("unchecked")
Instance<StoredResourceArns> instance = mock(Instance.class);
when(instance.iterator()).thenAnswer(invocation -> List.of(stored).iterator());
return new ResourceArnBuilder(new ObjectMapper(), null, instance);
return new ResourceArnBuilder(new ObjectMapper(), instance);
}
/** The finder of the stored ARNs of a namespace, answering one name of one type with one ARN. */
@@ -1805,7 +1856,7 @@ class ResourceArnBuilderTest {
Instance<RequestPathLabels> labels = mock(Instance.class);
when(labels.isUnsatisfied()).thenReturn(false);
when(labels.iterator()).thenAnswer(invocation -> List.of(s3Keys).iterator());
return new ResourceArnBuilder(new ObjectMapper(), null, null, labels);
return new ResourceArnBuilder(new ObjectMapper(), null, labels);
}
/** A current request whose path is {@code sentPath}, the path the server received. */
@@ -1,104 +0,0 @@
package io.floci.aws.services.iam;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
/**
* Holds {@link ResourceArnBuilder}'s declared-unresolved resource types to the reason each one
* gives. Every entry claims its type belongs only to actions the handler does not dispatch, and
* that is a claim about the handler rather than about the builder, so nothing inside the builder
* can check it.
*
* <p>It matters because the failure is silent and fail-open. Dispatching such an action without
* teaching the resolver its type leaves the action evaluated against {@code *}, so a policy
* statement naming the resource never applies, which is the gap this builder exists to close.
*
* <p>The map is empty since {@code GetRoleTemplateVersion} was dispatched, so the first test
* holds nothing today and the second holds what emptied it: the type that used to be declared is
* resolved from the ARN the request carries. That one is the regression worth a test, since an
* empty map asserts nothing and dropping the parameter would put the action back on {@code *}.
*/
@QuarkusTest
class UnresolvedResourceTypesIntegrationTest {
private static final String IAM_AUTH =
"AWS4-HMAC-SHA256 Credential=test/20261010/us-east-1/iam/aws4_request";
@Test
void everyTypeDeclaredUnresolvedIsCarriedOnlyByActionsNobodyDispatches() {
for (Map.Entry<String, String> declared : ResourceArnBuilder.UNRESOLVED_TYPES.entrySet()) {
Set<String> actions = actionsCarrying(declared.getKey());
assertFalse(actions.isEmpty(), declared.getKey()
+ " is declared unresolved but AWS no longer gives it to any action, so the "
+ "entry is dead and should go");
for (String action : actions) {
// The credential scope resolves the service, so this reaches the IAM handler
// whether or not the action is routable by name, and an action the handler does
// not dispatch falls to its default branch.
Response response = given().header("Authorization", IAM_AUTH)
.formParam("Action", action)
.when().post("/");
// The status is read before the body: a dispatched action may answer with no
// parseable body at all, and the reason for failing is the same either way.
assertEquals(400, response.statusCode(), reason(declared, action));
assertEquals("UnsupportedOperation",
response.xmlPath().getString("ErrorResponse.Error.Code"),
reason(declared, action));
}
}
}
/**
* {@code role-template} was the last declared-unresolved type, and dispatching
* {@code GetRoleTemplateVersion} is what took it out: the action carries the template's ARN,
* so the type resolves like any other the request names. Held over the wire as well as in the
* table, because the action being dispatched is half of the claim.
*/
@Test
void theTypeThatWasDeclaredUnresolvedIsResolvedFromTheArnTheRequestCarries() {
assertEquals("TemplateArn",
ResourceArnBuilder.IAM_ARN_PARAMETERS.get("GetRoleTemplateVersion"),
"GetRoleTemplateVersion no longer resolves its template from the ARN it carries, "
+ "so role-template is back to * unless it is declared unresolved again");
assertEquals(List.of("role-template"),
IamActionResources.typesOf("GetRoleTemplateVersion"),
"AWS has changed which resource type GetRoleTemplateVersion is authorized "
+ "against, so the parameter above may name the wrong thing");
Response response = given().header("Authorization", IAM_AUTH)
.formParam("Action", "GetRoleTemplateVersion")
.formParam("TemplateArn", "arn:aws:iam::aws:role-template/ec2.amazonaws.com/X:1")
.when().post("/");
assertEquals(404, response.statusCode(),
"GetRoleTemplateVersion is not dispatched any more, so role-template belongs in "
+ "UNRESOLVED_TYPES again rather than being resolved here");
assertEquals("NoSuchEntity", response.xmlPath().getString("ErrorResponse.Error.Code"));
}
private static String reason(Map.Entry<String, String> declared, String action) {
return action + " is dispatched now, so '" + declared.getKey() + "' is no longer a type "
+ "only undispatched actions carry (" + declared.getValue() + "). Resolve it in "
+ "ResourceArnBuilder rather than leaving it declared, or the action is evaluated "
+ "against * and a policy naming the resource never applies";
}
private static Set<String> actionsCarrying(String resourceType) {
Set<String> actions = new TreeSet<>();
for (Map.Entry<String, List<String>> entry : IamActionResources.all().entrySet()) {
if (entry.getValue().contains(resourceType)) {
actions.add(entry.getKey());
}
}
return actions;
}
}