fix(iam): check four more services on the resources their requests name (#7365)

Under IAM enforcement, DataSync, EMR, Rekognition and IAM Identity
Center requests were checked on `*`, so a grant scoped to a DataSync
agent, an EMR cluster, an Identity Center permission set or a
Rekognition custom model was refused, and a deny scoped to one never
applied.

The generated table of the resources requests name now reads them, 276
operations, from AWS's Service Reference and botocore as it reads the
earlier batches: their namespaces move from the generator's
NOT_COVERED to NAMESPACES and the table is regenerated, nothing in it
edited by hand; 1385 operations over 33 namespaces. Each resource
type Floci serves has the ARN format it mints: DataSync's
`agent/agent-<id>`, `location/loc-<id>` and `task/task-<id>`, EMR's
`cluster/<id>`, and IAM Identity Center's instances and permission
sets, with no Region or account. Of Rekognition, Floci serves the image
analyses alone, which act on no resource but a custom model.

Corrections, each with its reason:

- EMR requests naming a cluster by its job flow id, JobFlowId or
  JobFlowIds (AddInstanceGroups, AddJobFlowSteps, DescribeJobFlows,
  TerminateJobFlows and the four Set operations), are checked on that
  cluster: a job flow's id is the cluster's.
- EMR's AddTags and RemoveTags are checked on the cluster their
  ResourceId names, the one Floci tags, not on the one their ClusterId
  gives, which only scopes a session-scoped resource's tags. Read as
  generated, a ClusterId the grant covers would have let a request tag
  another cluster.
- EMR's ModifyInstanceGroups may leave out its ClusterId ("Required:
  No") and name each instance group by its id alone, which EMR then
  finds the cluster of. Such a request is checked on the cluster each
  group is in, where it was checked on `*`; a null ClusterId is read as
  none, as the handler reads it. An InstanceGroups that is not a list
  names no group to the check, and the handler now refuses it as naming
  none, where it modified the groups an object's values named.
- Rekognition's stream processor operations are checked on
  `streamprocessor/<Name>`: the Developer Guide's example gives a
  processor named streamProcessorForCam the ARN
  `...:streamprocessor/streamProcessorForCam`.
- IAM Identity Center's CreateAccountAssignment,
  DeleteAccountAssignment and ProvisionPermissionSet are also checked on
  the account their TargetId names, `account/<id>`, which the action
  acts on beside the instance and the permission set. A
  ProvisionPermissionSet for ALL_PROVISIONED_ACCOUNTS names no TargetId
  and provisions the permission set to every account it is provisioned
  to (the User Guide: up to 3,500 accounts that way, one with
  AWS_ACCOUNT), so it is checked on each of them, where a grant on the
  instance and the permission set alone let it through. Its TargetId
  names the account only for AWS_ACCOUNT, so a null one with
  ALL_PROVISIONED_ACCOUNTS names none.

The two lookups are the existing `through` entries, which named the
cluster or workgroup a Redshift Data session runs on: one may now read
each value of a list's field, and apply only `when` the request names
its resource no other way. StoredResourceArns.through gives a list of
ARNs, which EmrService and SsoAdminService now answer from what they
store, EmrService under the lock its handlers change clusters under.

Fixes the new formats needed:

- DataSync's ARN formats, and IAM Identity Center's applications' and
  trusted token issuers', name their account `${AccountId}` where every
  other format has `${Account}`. The generator now reads that slot as
  `${Account}`, so the caller's account fills it; a new Identity Center
  application was otherwise checked on `arn:aws:sso::*:application/*/*`,
  which no account-scoped grant matches.
- Identity Center's application providers are AWS's own,
  `arn:aws:sso::aws:applicationProvider/<id>`. An ARN of a type whose
  format fixes the account as `aws` is now checked in that account
  alone, not also as the caller's own copy, which does not exist and
  which a grant on the provider did not cover. An ARN of any other type
  giving the account `aws` is still checked as the caller's too.
- A permission set, an application or a trusted token issuer an
  Identity Center create makes is in the instance its InstanceArn
  names, whose id its ARN carries, `permissionSet/<instance id>/<id>`.
  It was checked with both ids open, which no grant scoped to an
  instance's permission sets matched. The generator now fills a variable
  no member fills from the ARN of another type the action acts on whose
  format is that variable alone, given by the ARN member named after it.

Left as generated, on `*`: Rekognition's CreateStreamProcessor and
StartMediaAnalysisJob, which name their collection or model two
structures deep, where the table reads none, and which Floci does not
serve.

Elastic Beanstalk and Redshift Serverless, which have as few resource
types, stay unread, each with its reason: an environment's ARN carries
an application name its requests do not give, and Redshift Serverless'
ARNs end in ids its requests name by name, so both need the stored
ARNs found first.

iam.md lists the four services, and AWS Payment Cryptography, which
the table already read, and gives the corrections, the rule for an
AWS-owned resource's ARN, and the cluster, accounts and instance the
lookups and the creates are checked on.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-03 17:14:32 -06:00
1 parent 96fafbd4dc
commit 5c303afac6
14 files changed
+987 -103

No files matched your search

@@ -137,10 +137,17 @@ class RequestResourceMembersTest {
assertEquals(new Member("SessionId", false), parseUnchecked(document(formats,
"{\"types\":[\"cluster\",\"group\"],\"through\":\"SessionId\",\"endpoint\":\"a\"}"))
.operations().get("a:Op").getFirst().through());
// No types, a type with no format, no member of one value, or beside members reading it otherwise.
// A list's structures' field names one through each of its values, when another member is absent.
Resource each = parseUnchecked(document(formats, "{\"types\":[\"group\"],\"through\":\"Parts[].PartId\","
+ "\"when\":{\"GroupId\":[null]}}")).operations().get("a:Op").getFirst();
assertEquals(new Member("Parts", true, new Member("PartId", false)), each.through());
assertEquals(Map.of("GroupId", List.of("")), each.when());
// No types, a type with no format, no member, or beside members reading it otherwise.
for (String bad : List.of("\"types\":[],\"through\":\"SessionId\"",
"\"types\":[\"pool\"],\"through\":\"SessionId\"", "\"types\":[\"group\"],\"through\":\"Ids[]\"",
"\"types\":[\"group\"],\"through\":\"Session.Id\"",
"\"types\":[\"pool\"],\"through\":\"SessionId\"", "\"types\":[\"group\"],\"through\":\"Ids[][]\"",
"\"types\":[\"group\"],\"through\":\"Session.Id.Part\"",
"\"types\":[\"group\"],\"through\":\"SessionId\",\"unless\":{\"GroupId\":[null]}",
"\"types\":[\"group\"],\"through\":\"SessionId\",\"when\":{\"Ids[]\":[\"a\"]}",
"\"types\":[\"group\"],\"through\":\"SessionId\",\"arn\":\"GroupArn\"",
"\"types\":[\"group\"],\"through\":\"SessionId\",\"before\":\"#\"",
"\"type\":\"group\",\"fill\":{},\"through\":\"SessionId\"")) {
@@ -269,9 +276,10 @@ class RequestResourceMembersTest {
/**
* Each resource alone, from a request naming it and nothing else, gives exactly its ARN, in the account its
* account member names when it has one, with those sharing its explicit member selector and those the operation
* always names, such as a create's new resource (Cloud Map's {@code CreateService}); one named through another
* resource of its service's own, the ARN its service gives for that.
* account member names when it has one, with those sharing its explicit member selector, those the operation
* always names, such as a create's new resource (Cloud Map's {@code CreateService}), and those filled from the ARN
* it is read from (a permission set made in an instance); one named through another resource of its service's
* own, the ARN its service gives for that.
*/
@TestFactory
Stream<DynamicTest> everyResourceOfTheTableIsReadFromAJsonBodyAndAForm() {
@@ -305,7 +313,7 @@ class RequestResourceMembersTest {
form.append('&').append(parameter(member)).append('=').append(encoded(value));
});
List<String> named = operation.getValue().stream()
.filter(other -> (other == resource || named(other)
.filter(other -> (other == resource || named(other) || fillsFromTheArn(other, resource)
|| !other.find().isEmpty() && other.find().equals(resource.find())
&& other.when().equals(resource.when()) && other.unless().equals(resource.unless()))
&& (other.endpoint() == null || resource.endpoint() == null
@@ -350,6 +358,17 @@ class RequestResourceMembersTest {
})));
}
/**
* Whether a resource fills its variables from the ARN the other's member holds, as that ARN's type's variable alone
* (a permission set made in the instance an {@code InstanceArn} names), so a request naming the other by an ARN of
* its first type, which the test gives, names it too.
*/
private static boolean fillsFromTheArn(Resource resource, Resource other) {
return other.arn() != null && !resource.fill().isEmpty()
&& resource.fill().values().stream().allMatch(other.arn()::equals)
&& other.types().getFirst().equals(resource.arnOf().get(other.arn().path()));
}
/** 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()
@@ -370,8 +389,8 @@ class RequestResourceMembersTest {
}
@Override
public Optional<String> through(String member, String value, String region) {
return Optional.of(arn);
public List<String> through(String member, String value, String region) {
return List.of(arn);
}
};
}
@@ -70,6 +70,21 @@ class RequestResourcesTest {
json("{\"resourceArn\":\"arn:aws:ecr:eu-west-1:111122223333:repository/web\",\"tags\":[]}")));
}
/**
* A resource of a type AWS owns, whose format's account is {@code aws} (an IAM Identity Center application
* provider), has no copy in the caller's account, so its ARN is checked as given alone; an ARN of any other type
* giving that account is still checked as the caller's own too.
*/
@Test
void anArnOfATypeAwsOwnsIsCheckedInItsAccountAlone() {
String provider = "arn:aws:sso::aws:applicationProvider/custom";
assertEquals(List.of(provider), of("sso:DescribeApplicationProvider",
json("{\"ApplicationProviderArn\":\"" + provider + "\"}")));
String machine = "arn:aws:states:us-east-1:aws:stateMachine:orders";
assertEquals(List.of(machine, MACHINE), of("states:DescribeStateMachine",
json("{\"stateMachineArn\":\"" + machine + "\"}")));
}
@Test
void aTypesVariablesAreFilledFromTheRequestAndAListGivesAResourceForEachValue() {
assertEquals(List.of("arn:aws:ecr:us-east-1:000000000000:repository/web",
@@ -577,9 +592,9 @@ class RequestResourcesTest {
}
@Override
public Optional<String> through(String member, String value, String region) {
public List<String> through(String member, String value, String region) {
return "SessionId".equals(member) && "us-east-1".equals(region)
? Optional.ofNullable(sessions.get(value)) : Optional.empty();
? Optional.ofNullable(sessions.get(value)).stream().toList() : List.of();
}
};
String secret = ",\"SecretArn\":\"arn:aws:secretsmanager:us-east-1:000000000000:secret:db-AbCdEf\"";
@@ -613,6 +628,101 @@ class RequestResourcesTest {
"eu-west-1", "000000000000", stored));
}
/**
* An EMR request modifying instance groups without its {@code ClusterId} is on the cluster each group is in, as its
* service finds it, and none for a group in no cluster; with one, on that cluster alone, and with a null one as
* with none. A permission set provisioned to all the accounts it is provisioned to is on each of them, a null
* {@code TargetId} naming none, and one provisioned to an account on that account alone.
*/
@Test
void aResourceIsNamedThroughEachValueOfAListAndOnlyWhenTheRequestNamesItNoOtherWay() {
String clusters = "arn:aws:elasticmapreduce:us-east-1:000000000000:cluster/";
Map<String, String> groups = Map.of("ig-A", clusters + "j-A", "ig-B", clusters + "j-B");
StoredResourceArns emr = new StoredResourceArns() {
@Override
public String namespace() {
return "elasticmapreduce";
}
@Override
public Optional<String> find(String type, List<String> names, String region) {
return Optional.empty();
}
@Override
public List<String> through(String member, String value, String region) {
return "InstanceGroups[].InstanceGroupId".equals(member)
? Optional.ofNullable(groups.get(value)).stream().toList() : List.of();
}
};
String modified = "\"InstanceGroups\":[{\"InstanceGroupId\":\"ig-A\",\"InstanceCount\":2},"
+ "{\"InstanceGroupId\":\"ig-B\",\"InstanceCount\":2},{\"InstanceGroupId\":\"ig-none\"}]";
assertEquals(List.of(clusters + "j-A", clusters + "j-B"), RequestResources.of(
"elasticmapreduce:ModifyInstanceGroups", json("{" + modified + "}"), "aws", "us-east-1",
"000000000000", emr));
assertEquals(List.of(clusters + "j-A"), RequestResources.of("elasticmapreduce:ModifyInstanceGroups",
json("{\"ClusterId\":\"j-A\"," + modified + "}"), "aws", "us-east-1", "000000000000", emr));
// A null ClusterId names no cluster of its own, as the handler reads it; an InstanceGroups that is not a
// list names no group, which the handler refuses.
assertEquals(List.of(clusters + "j-A", clusters + "j-B"), RequestResources.of(
"elasticmapreduce:ModifyInstanceGroups", json("{\"ClusterId\":null," + modified + "}"), "aws",
"us-east-1", "000000000000", emr));
assertEquals(List.of(), RequestResources.of("elasticmapreduce:ModifyInstanceGroups",
json("{\"InstanceGroups\":{\"first\":{\"InstanceGroupId\":\"ig-A\"}}}"), "aws", "us-east-1",
"000000000000", emr));
String instance = "arn:aws:sso:::instance/ssoins-1111111111111111";
String permissionSet = "arn:aws:sso:::permissionSet/ssoins-1111111111111111/ps-2222222222222222";
StoredResourceArns sso = new StoredResourceArns() {
@Override
public String namespace() {
return "sso";
}
@Override
public Optional<String> find(String type, List<String> names, String region) {
return Optional.empty();
}
@Override
public List<String> through(String member, String value, String region) {
return "PermissionSetArn".equals(member) && permissionSet.equals(value)
? List.of("arn:aws:sso:::account/111111111111", "arn:aws:sso:::account/222222222222")
: List.of();
}
};
String provisioned = "{\"InstanceArn\":\"" + instance + "\",\"PermissionSetArn\":\"" + permissionSet + "\",";
assertEquals(List.of(instance, permissionSet, "arn:aws:sso:::account/111111111111",
"arn:aws:sso:::account/222222222222"), RequestResources.of("sso:ProvisionPermissionSet",
json(provisioned + "\"TargetType\":\"ALL_PROVISIONED_ACCOUNTS\"}"), "aws", "us-east-1",
"000000000000", sso));
assertEquals(List.of(instance, permissionSet, "arn:aws:sso:::account/111111111111",
"arn:aws:sso:::account/222222222222"), RequestResources.of("sso:ProvisionPermissionSet",
json(provisioned + "\"TargetType\":\"ALL_PROVISIONED_ACCOUNTS\",\"TargetId\":null}"), "aws",
"us-east-1", "000000000000", sso));
assertEquals(List.of(instance, permissionSet, "arn:aws:sso:::account/333333333333"), RequestResources.of(
"sso:ProvisionPermissionSet", json(provisioned + "\"TargetType\":\"AWS_ACCOUNT\","
+ "\"TargetId\":\"333333333333\"}"), "aws", "us-east-1", "000000000000", sso));
}
/**
* A resource an IAM Identity Center create makes in an instance has the id of the instance its request names by
* its ARN, and its own id open, so a grant on the instance's permission sets covers it.
*/
@Test
void aResourceMadeInAnInstanceHasTheInstancesId() {
String instance = "arn:aws:sso:::instance/ssoins-1111111111111111";
assertEquals(List.of(instance, "arn:aws:sso:::permissionSet/ssoins-1111111111111111/*"),
of("sso:CreatePermissionSet", json("{\"InstanceArn\":\"" + instance + "\",\"Name\":\"admins\"}")));
assertEquals(List.of("arn:aws:sso:::permissionSet/ssoins-1111111111111111/*"),
RequestResources.created("sso:CreatePermissionSet",
json("{\"InstanceArn\":\"" + instance + "\",\"Name\":\"admins\"}"), "aws", "us-east-1",
"000000000000"));
assertEquals(List.of(instance, "arn:aws:sso::000000000000:trustedTokenIssuer/ssoins-1111111111111111/*"),
of("sso:CreateTrustedTokenIssuer", json("{\"InstanceArn\":\"" + instance + "\",\"Name\":\"idp\","
+ "\"TrustedTokenIssuerType\":\"OIDC_JWT\"}")));
}
/** A request value is a name, so a {@code ${...}} in it is kept, not read as a variable of the format. */
@Test
void aValueIsTakenLiterally() {
@@ -1383,6 +1383,248 @@ class IamRequestResourceEnforcementIntegrationTest {
.body("{\"Database\":\"res-none\"," + members + "}").post("/").then();
}
/** A DataSync agent is checked on its ARN, which ends in the id DataSync gave it, to describe it or to tag it. */
@Test
void aDataSyncAgentIsCheckedOnItsArn() {
String arn = adminJson("datasync", "FmrsService.CreateAgent").body("{\"ActivationKey\":\"RES-ACTIVATION\"}")
.post("/").then().statusCode(200).extract().path("AgentArn");
cleanup.register(() -> adminJson("datasync", "FmrsService.DeleteAgent")
.body("{\"AgentArn\":\"" + arn + "\"}").post("/"));
String agents = "arn:aws:datasync:" + REGION + ":" + ACCOUNT + ":agent/";
assertTrue(arn.startsWith(agents + "agent-"), arn);
String other = agents + "agent-0123456789abcdef0";
User user = user("res-agent");
user.policy(allowOn("datasync:DescribeAgent", arn) + "," + allowOn("datasync:TagResource", arn));
dataSync(user, "DescribeAgent", "{\"AgentArn\":\"" + arn + "\"}").statusCode(200);
refusedJson(dataSync(user, "DescribeAgent", "{\"AgentArn\":\"" + other + "\"}"), "datasync:DescribeAgent");
String tags = "\",\"Tags\":[{\"Key\":\"a\",\"Value\":\"b\"}]}";
dataSync(user, "TagResource", "{\"ResourceArn\":\"" + arn + tags).statusCode(200);
refusedJson(dataSync(user, "TagResource", "{\"ResourceArn\":\"" + other + tags), "datasync:TagResource");
}
/**
* An EMR cluster is checked on {@code cluster/<id>}, the ARN EMR gives it, whether a request names it by its
* {@code ClusterId} or among its {@code JobFlowIds}, and a tag request on the cluster its {@code ResourceId}
* names, the one Floci tags, not on the one its {@code ClusterId} gives.
*/
@Test
void anEmrClusterIsCheckedOnTheArnItsIdGives() {
String arn = emrCluster("res-emr");
String id = arn.substring(arn.lastIndexOf('/') + 1);
User user = user("res-emr");
user.policy(allowOn("elasticmapreduce:DescribeCluster", arn) + ","
+ allowOn("elasticmapreduce:SetTerminationProtection", arn) + ","
+ allowOn("elasticmapreduce:AddTags", arn));
emr(user, "DescribeCluster", "{\"ClusterId\":\"" + id + "\"}").statusCode(200);
refusedJson(emr(user, "DescribeCluster", "{\"ClusterId\":\"j-RESOTHER\"}"), "elasticmapreduce:DescribeCluster");
String unprotected = "],\"TerminationProtected\":false}";
emr(user, "SetTerminationProtection", "{\"JobFlowIds\":[\"" + id + "\"" + unprotected).statusCode(200);
refusedJson(emr(user, "SetTerminationProtection", "{\"JobFlowIds\":[\"" + id + "\",\"j-RESOTHER\""
+ unprotected), "elasticmapreduce:SetTerminationProtection");
String tags = "\",\"Tags\":[{\"Key\":\"a\",\"Value\":\"b\"}]}";
emr(user, "AddTags", "{\"ResourceId\":\"" + id + tags).statusCode(200);
refusedJson(emr(user, "AddTags", "{\"ClusterId\":\"" + id + "\",\"ResourceId\":\"j-RESOTHER" + tags),
"elasticmapreduce:AddTags");
}
/**
* A Rekognition stream processor is checked on the ARN its {@code Name} gives, and a moderation analysis on the
* custom model whose ARN its {@code ProjectVersion} gives. Floci serves no stream processor and no custom model,
* so an allowed request is answered by the handler, never by IAM.
*/
@Test
void aStreamProcessorIsCheckedOnTheArnItsNameGives() {
String processor = "arn:aws:rekognition:" + REGION + ":" + ACCOUNT + ":streamprocessor/res-denied";
String version = "arn:aws:rekognition:" + REGION + ":" + ACCOUNT + ":project/res/version/res-denied/1";
User user = user("res-rekognition");
user.policy(allowOn("rekognition:*", "*") + ",{\"Effect\":\"Deny\",\"Action\":\"rekognition:*\","
+ "\"Resource\":[\"" + processor + "\",\"" + version + "\"]}");
refusedJson(rekognition(user, "StartStreamProcessor", "{\"Name\":\"res-denied\"}"),
"rekognition:StartStreamProcessor");
rekognition(user, "StartStreamProcessor", "{\"Name\":\"res-other\"}")
.body(not(containsString("not authorized")));
String image = "{\"Image\":{\"Bytes\":\"AA==\"}";
refusedJson(rekognition(user, "DetectModerationLabels", image + ",\"ProjectVersion\":\"" + version + "\"}"),
"rekognition:DetectModerationLabels");
rekognition(user, "DetectModerationLabels", image + "}").body(not(containsString("not authorized")));
}
/**
* An EMR {@code ModifyInstanceGroups} naming its instance groups by their ids alone, with no {@code ClusterId} or
* a null one, is checked on the cluster each group is in, the one EMR finds and modifies it in, so a grant on
* another cluster does not reach it and a deny on its own holds; an {@code InstanceGroups} that is no list is
* refused, not read as its values.
*/
@Test
void anInstanceGroupNamedWithoutItsClusterIsCheckedOnTheClusterItIsIn() {
String arn = emrCluster("res-emr-groups");
String group = adminJson("elasticmapreduce", "ElasticMapReduce.ListInstanceGroups")
.body("{\"ClusterId\":\"" + arn.substring(arn.lastIndexOf('/') + 1) + "\"}").post("/").then()
.statusCode(200).extract().path("InstanceGroups[0].Id");
String modify = "{\"InstanceGroups\":[{\"InstanceGroupId\":\"" + group + "\",\"InstanceCount\":1}]}";
User user = user("res-emr-groups");
user.policy(allowOn("elasticmapreduce:ModifyInstanceGroups", arn));
emr(user, "ModifyInstanceGroups", modify).statusCode(200);
emr(user, "ModifyInstanceGroups", "{\"ClusterId\":null," + modify.substring(1)).statusCode(200);
user.policy(allowOn("elasticmapreduce:ModifyInstanceGroups", arn.substring(0, arn.lastIndexOf('/') + 1)
+ "j-RESOTHER"));
refusedJson(emr(user, "ModifyInstanceGroups", modify), "elasticmapreduce:ModifyInstanceGroups");
user.policy(allowOn("elasticmapreduce:ModifyInstanceGroups", "*") + ",{\"Effect\":\"Deny\","
+ "\"Action\":\"elasticmapreduce:ModifyInstanceGroups\",\"Resource\":\"" + arn + "\"}");
refusedJson(emr(user, "ModifyInstanceGroups", modify), "elasticmapreduce:ModifyInstanceGroups");
// An InstanceGroups that is not a list names no group to the check, and EMR changes none.
emr(user, "ModifyInstanceGroups", "{\"InstanceGroups\":{\"first\":{\"InstanceGroupId\":\"" + group
+ "\",\"InstanceCount\":1}}}").statusCode(400)
.body(containsString("InstanceGroups must contain at least one instance group."));
}
/**
* An IAM Identity Center account assignment is checked on the account its {@code TargetId} names beside the
* instance and the permission set, so a grant scoped to one account does not reach another, and a deny on an
* account holds.
*/
@Test
void anAccountAssignmentIsCheckedOnTheAccountItsTargetIdNames() {
String instance = ssoInstance();
String permissionSet = permissionSet(instance);
String assigned = assignment(instance, permissionSet, "111111111111");
String other = assignment(instance, permissionSet, "222222222222");
User user = user("res-sso");
user.policy(allowOn("sso:CreateAccountAssignment", instance) + ","
+ allowOn("sso:CreateAccountAssignment", permissionSet) + ","
+ allowOn("sso:CreateAccountAssignment", "arn:aws:sso:::account/111111111111"));
ssoAdmin(user, "CreateAccountAssignment", assigned).statusCode(200);
cleanup.register(() -> adminJson("sso", "SWBExternalService.DeleteAccountAssignment").body(assigned)
.post("/"));
refusedJson(ssoAdmin(user, "CreateAccountAssignment", other), "sso:CreateAccountAssignment");
user.policy(allowOn("sso:CreateAccountAssignment", "*") + ",{\"Effect\":\"Deny\","
+ "\"Action\":\"sso:CreateAccountAssignment\",\"Resource\":\"arn:aws:sso:::account/222222222222\"}");
refusedJson(ssoAdmin(user, "CreateAccountAssignment", other), "sso:CreateAccountAssignment");
}
/** An assignment of the permission set to a group, in the account. */
/**
* An IAM Identity Center permission set provisioned to all the accounts it is provisioned to, with no
* {@code TargetId}, or a null one, is checked on each of those accounts, so a grant on one does not reach the
* others, a deny on one holds, and a grant on all of them lets it through.
*/
@Test
void aPermissionSetProvisionedToAllItsAccountsIsCheckedOnEachOfThem() {
String instance = ssoInstance();
String permissionSet = permissionSet(instance);
for (String account : List.of("111111111111", "222222222222")) {
String assigned = assignment(instance, permissionSet, account);
adminJson("sso", "SWBExternalService.CreateAccountAssignment").body(assigned).post("/").then()
.statusCode(200);
cleanup.register(() -> adminJson("sso", "SWBExternalService.DeleteAccountAssignment").body(assigned)
.post("/"));
}
String provision = "{\"InstanceArn\":\"" + instance + "\",\"PermissionSetArn\":\"" + permissionSet + "\",";
String all = provision + "\"TargetType\":\"ALL_PROVISIONED_ACCOUNTS\"}";
String one = provision + "\"TargetType\":\"AWS_ACCOUNT\",\"TargetId\":\"111111111111\"}";
User user = user("res-sso-provision");
user.policy(allowOn("sso:ProvisionPermissionSet", instance) + ","
+ allowOn("sso:ProvisionPermissionSet", permissionSet) + ","
+ allowOn("sso:ProvisionPermissionSet", "arn:aws:sso:::account/111111111111"));
ssoAdmin(user, "ProvisionPermissionSet", one).statusCode(200);
refusedJson(ssoAdmin(user, "ProvisionPermissionSet", all), "sso:ProvisionPermissionSet");
user.policy(allowOn("sso:ProvisionPermissionSet", "*") + ",{\"Effect\":\"Deny\","
+ "\"Action\":\"sso:ProvisionPermissionSet\",\"Resource\":\"arn:aws:sso:::account/222222222222\"}");
refusedJson(ssoAdmin(user, "ProvisionPermissionSet", all), "sso:ProvisionPermissionSet");
ssoAdmin(user, "ProvisionPermissionSet", one).statusCode(200);
// A grant on both accounts covers it, a null TargetId naming no account of its own.
user.policy(allowOn("sso:ProvisionPermissionSet", instance) + ","
+ allowOn("sso:ProvisionPermissionSet", permissionSet) + ","
+ allowOn("sso:ProvisionPermissionSet", "arn:aws:sso:::account/111111111111") + ","
+ allowOn("sso:ProvisionPermissionSet", "arn:aws:sso:::account/222222222222"));
ssoAdmin(user, "ProvisionPermissionSet", provision + "\"TargetType\":\"ALL_PROVISIONED_ACCOUNTS\","
+ "\"TargetId\":null}").statusCode(200);
}
/**
* An IAM Identity Center permission set a request creates is checked on the permission sets of the instance its
* {@code InstanceArn} names, {@code permissionSet/<instance id>/*}, so a grant on those lets it through and one on
* another instance's does not.
*/
@Test
void aPermissionSetIsCreatedUnderTheInstanceItsRequestNames() {
String instance = ssoInstance();
String permissionSets = "arn:aws:sso:::permissionSet/";
String create = "{\"InstanceArn\":\"" + instance + "\",\"Name\":\"res-" + suffix() + "\"}";
User user = user("res-sso-create");
user.policy(allowOn("sso:CreatePermissionSet", instance) + ","
+ allowOn("sso:CreatePermissionSet", permissionSets + "ssoins-0000000000000000/*"));
refusedJson(ssoAdmin(user, "CreatePermissionSet", create), "sso:CreatePermissionSet");
user.policy(allowOn("sso:CreatePermissionSet", instance) + ","
+ allowOn("sso:CreatePermissionSet", permissionSets + instance.substring(instance.lastIndexOf('/') + 1)
+ "/*"));
String permissionSet = ssoAdmin(user, "CreatePermissionSet", create).statusCode(200).extract()
.path("PermissionSet.PermissionSetArn");
cleanup.register(() -> adminJson("sso", "SWBExternalService.DeletePermissionSet")
.body("{\"InstanceArn\":\"" + instance + "\",\"PermissionSetArn\":\"" + permissionSet + "\"}")
.post("/"));
}
/** A cluster an administrator runs, kept alive with no steps, terminated after the test; its ARN. */
private String emrCluster(String name) {
String arn = adminJson("elasticmapreduce", "ElasticMapReduce.RunJobFlow")
.body("{\"Name\":\"" + name + "\",\"ReleaseLabel\":\"emr-7.5.0\",\"Instances\":{"
+ "\"KeepJobFlowAliveWhenNoSteps\":true,\"InstanceGroups\":[{\"Name\":\"master\","
+ "\"InstanceRole\":\"MASTER\",\"InstanceType\":\"m5.xlarge\",\"InstanceCount\":1}]}}")
.post("/").then().statusCode(200).extract().path("ClusterArn");
String clusters = "arn:aws:elasticmapreduce:" + REGION + ":" + ACCOUNT + ":cluster/";
assertTrue(arn.startsWith(clusters + "j-"), arn);
String id = arn.substring(clusters.length());
cleanup.register(() -> adminJson("elasticmapreduce", "ElasticMapReduce.TerminateJobFlows")
.body("{\"JobFlowIds\":[\"" + id + "\"]}").post("/"));
return arn;
}
/** The IAM Identity Center instance ListInstances gives. */
private static String ssoInstance() {
return adminJson("sso", "SWBExternalService.ListInstances").body("{}").post("/").then()
.statusCode(200).extract().path("Instances[0].InstanceArn");
}
/** A permission set an administrator creates in the instance, deleted after the test. */
private String permissionSet(String instance) {
String permissionSet = adminJson("sso", "SWBExternalService.CreatePermissionSet")
.body("{\"InstanceArn\":\"" + instance + "\",\"Name\":\"res-" + suffix() + "\"}").post("/").then()
.statusCode(200).extract().path("PermissionSet.PermissionSetArn");
cleanup.register(() -> adminJson("sso", "SWBExternalService.DeletePermissionSet")
.body("{\"InstanceArn\":\"" + instance + "\",\"PermissionSetArn\":\"" + permissionSet + "\"}")
.post("/"));
return permissionSet;
}
private static String assignment(String instance, String permissionSet, String account) {
return "{\"InstanceArn\":\"" + instance + "\",\"TargetId\":\"" + account + "\",\"TargetType\":\"AWS_ACCOUNT\","
+ "\"PermissionSetArn\":\"" + permissionSet + "\",\"PrincipalType\":\"GROUP\","
+ "\"PrincipalId\":\"11111111-2222-3333-4444-555555555555\"}";
}
private static ValidatableResponse dataSync(User user, String operation, String body) {
return send(user, "datasync", "FmrsService." + operation, "1.1", body);
}
private static ValidatableResponse emr(User user, String operation, String body) {
return send(user, "elasticmapreduce", "ElasticMapReduce." + operation, "1.1", body);
}
private static ValidatableResponse rekognition(User user, String operation, String body) {
return send(user, "rekognition", "RekognitionService." + operation, "1.1", body);
}
private static ValidatableResponse ssoAdmin(User user, String operation, String body) {
return send(user, "sso", "SWBExternalService." + operation, "1.1", body);
}
/** A monthly cost budget of the name, with a billing view ARN when one is given. */
private static String budget(String name, String billingViewArn) {
return "{\"BudgetName\":\"" + name + "\",\"BudgetType\":\"COST\",\"TimeUnit\":\"MONTHLY\",\"BudgetLimit\":"