fix(iam): check five more services on the resources their requests name (#6810)

Under IAM enforcement, Amazon Data Firehose, Cognito identity pools,
Cloud Map, ACM and CodePipeline requests were checked on `*`, so a
grant scoped to a delivery stream, an identity pool, a namespace or a
service, a certificate, a pipeline or a webhook was refused, and a deny
scoped to one never applied.

The generated table of the resources requests name now reads them, 149
operations, from AWS's Service Reference and botocore as it reads the
first batch: their namespaces move from the generator's NOT_COVERED to
NAMESPACES, and the table is regenerated, nothing in it edited by hand.
Each resource type's ARN format is the one Floci's handler mints, which
the tests check against the ARN each create returns:

- `deliverystream/<name>`, `identitypool/<id>`, `namespace/<id>`,
  `service/<id>`, `certificate/<id>` and a pipeline's bare name.
- A CodePipeline webhook: AWS's is `webhook:<name>` (the Service
  Reference's format, and the CodePipeline User Guide's
  "arn:aws:codepipeline:eu-central-1:ACCOUNT_ID:webhook:my-webhook"),
  where Floci minted `webhook/<name>`; it now mints AWS's.

Corrections, each with its reason:

- StartPipelineExecution and GetPipelineState name their pipeline by a
  bare `name` ("The name of the pipeline to start"), which the rule
  reads only for an operation named after the type, as Step Functions'
  StartExecution names its execution, not its state machine, by `name`.
- CreatePipeline and UpdatePipeline name it by `pipeline.name`, and
  PutWebhook the webhook by `webhook.name` and the pipeline it connects
  to by `webhook.targetPipeline`. `pipeline.roleArn` holds the role
  CodePipeline assumes, an IAM role's ARN by botocore's pattern, and
  names no pipeline: read as one, a pipeline-shaped value there was the
  only resource an UpdatePipeline was checked on, so a grant on one
  pipeline let a request change another.
- CreateCustomActionType and DeleteCustomActionType are checked on
  `actiontype:Custom/<category>/<provider>/<version>`: a custom action
  type's owner is Custom, which the request does not give, and AWS's
  CreateCustomActionType example answers "owner": "Custom".
- Cloud Map's CreateService names its namespace by NamespaceId or,
  when it gives none or a JSON null, by DnsConfig.NamespaceId, which
  botocore keeps ("Use NamespaceId in Service instead") and the handler
  reads then; read by NamespaceId alone, a deny on the namespace did
  not hold for a request giving it in DnsConfig. A member given a JSON
  null now counts as absent in a resource's `when` and `unless`, as a
  handler reading it with asText(null) takes it, unless the table reads
  it as a value through `absent` (EventBridge's bus, `default`).

For those, the table may fill a variable from a structure's field
(`pipeline.name`), and give a variable no member gives the one value
its handler uses (`fixed`), in the generator, the loader and the
reader.

A create that names another resource beside the one it makes has its
tags checked on the one it makes alone, which the table marks `made`:
the resource of the type, or with an ARN format's leading resource
word, related to the operation's name past its verb. PutWebhook's tags
go on the webhook, not the pipeline it connects to, and CreateService's
on `service/*`, not the namespace; no other operation in the table is
marked.

Application Auto Scaling stays unread, its reason in NOT_COVERED: a
scalable target's ARN ends in an id the service mints, while requests
name the target by ServiceNamespace, ResourceId and ScalableDimension,
and read as a member ResourceId would fill that id.

RequestResourceMembersTest's test of every resource in the table now
expects, with the resource a request names, the resources of its
operation that every request names: Cloud Map's CreateService names
the service it creates with every variable open.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-28 17:18:11 -05:00
1 parent 3d4fdfe7ca
commit 096d336fab
12 files changed
+746 -65

No files matched your search

@@ -100,9 +100,9 @@ class RequestResourceMembersTest {
+ "{\"resources\":[],\"creates\":[{\"type\":\"part\",\"parent\":\"thing\",\"arn\":\"ThingArn\"}]}}}"),
part);
}
// A member past one structure, or filling a variable from a lone structure's member or a list in a list.
// A member past one structure, or filling a variable from a list in a list.
assertThrows(IllegalStateException.class, () -> parse(document(formats, "{\"types\":[\"thing\"],\"arn\":\"A[].B.ThingArn\"}")));
for (String bad : List.of("A.Name", "A[].Names[]", "A[].B.Name")) {
for (String bad : List.of("A.Names[]", "A[].Names[]", "A[].B.Name")) {
assertThrows(IllegalStateException.class, () -> parse(document(formats,
"{\"type\":\"thing\",\"fill\":{\"ThingName\":\"" + bad + "\"}}")), bad);
}
@@ -110,7 +110,7 @@ class RequestResourceMembersTest {
// only a member filling a variable is.
Member each = new Member("A", true, new Member("Name", false));
assertEquals(List.of(new Resource(List.of("thing"), null, Map.of("ThingName", each), Map.of(), Map.of(),
Map.of("A[].Name", "default"), Map.of("A[].Name", "thing"), null, null)),
Map.of("A[].Name", "default"), Map.of("A[].Name", "thing"), null, null, Map.of(), false)),
parseUnchecked(document(formats, "{\"type\":\"thing\",\"fill\":{\"ThingName\":\"A[].Name\"},"
+ "\"absent\":{\"A[].Name\":\"default\"},\"arnOf\":{\"A[].Name\":\"thing\"}}"))
.operations().get("a:Op"));
@@ -119,8 +119,36 @@ class RequestResourceMembersTest {
assertThrows(IllegalStateException.class, () -> parse(document(formats,
"{\"type\":\"thing\",\"fill\":{\"ThingName\":\"Name\"}," + bad + "}")), bad);
}
// A field of a structure fills a variable, as one value.
Member field = new Member("A", false, new Member("Name", false));
assertEquals(List.of(new Resource(List.of("thing"), null, Map.of("ThingName", field), Map.of(), Map.of(),
Map.of("A.Name", "default"), Map.of(), null, null, Map.of(), false)),
parseUnchecked(document(formats, "{\"type\":\"thing\",\"fill\":{\"ThingName\":\"A.Name\"},"
+ "\"absent\":{\"A.Name\":\"default\"}}")).operations().get("a:Op"));
assertEquals("A.Name", field.path());
// A variable fixed to the one value its handler uses, and one fixed that is no variable, that a member fills,
// or to no value.
String owned = "\"arnFormats\":{\"a\":{\"thing\":\"arn:${Partition}:a:${Region}:${Account}:thing/${Owner}/${ThingName}\"}}";
assertEquals(List.of(new Resource(List.of("thing"), null, Map.of("ThingName", new Member("Name", false)), Map.of(),
Map.of(), Map.of(), Map.of(), null, null, Map.of("Owner", "Custom"), false)),
parseUnchecked(document(owned, "{\"type\":\"thing\",\"fill\":{\"ThingName\":\"Name\"},"
+ "\"fixed\":{\"Owner\":\"Custom\"}}")).operations().get("a:Op"));
for (String bad : List.of("{\"Other\":\"Custom\"}", "{\"ThingName\":\"Custom\"}", "{\"Owner\":\"\"}",
"{\"Owner\":1}")) {
assertThrows(IllegalStateException.class, () -> parse(document(owned,
"{\"type\":\"thing\",\"fill\":{\"ThingName\":\"Name\"},\"fixed\":" + bad + "}")), bad);
}
// The resource a create makes, beside others it names, marked made, and a mark that is not true.
assertTrue(parseUnchecked(document(formats, "{\"type\":\"thing\",\"fill\":{\"ThingName\":\"Name\"},"
+ "\"made\":true}")).operations().get("a:Op").getFirst().made());
assertTrue(parseUnchecked(document(formats, "{\"types\":[\"thing\"],\"arn\":\"ThingArn\",\"made\":true}"))
.operations().get("a:Op").getFirst().made());
for (String bad : List.of("false", "\"true\"", "1")) {
assertThrows(IllegalStateException.class, () -> parse(document(formats,
"{\"type\":\"thing\",\"fill\":{\"ThingName\":\"Name\"},\"made\":" + bad + "}")), bad);
}
assertEquals(List.of(new Resource(List.of("thing"), new Member("A", true, new Member("ThingArns", true)), Map.of(),
Map.of(), Map.of(), Map.of(), Map.of(), null, null)), parseUnchecked(document(formats, "{\"types\":[\"thing\"],\"arn\":\"A[].ThingArns[]\"}"))
Map.of(), Map.of(), Map.of(), Map.of(), null, null, Map.of(), false)), parseUnchecked(document(formats, "{\"types\":[\"thing\"],\"arn\":\"A[].ThingArns[]\"}"))
.operations().get("a:Op"));
// An ARN read before more than one character.
assertThrows(IllegalStateException.class, () -> parse(document(formats,
@@ -154,19 +182,21 @@ class RequestResourceMembersTest {
"{\"type\":\"thing\",\"fill\":{\"ThingName\":\"Name\"}," + bad + "}")), bad);
}
assertEquals(List.of(new Resource(List.of("thing"), null, Map.of("ThingName", new Member("Name", false)),
Map.of(), Map.of("Bus", List.of("default")), Map.of("Bus", "default"), Map.of("Bus", "thing"), null, null)),
Map.of(), Map.of("Bus", List.of("default")), Map.of("Bus", "default"), Map.of("Bus", "thing"), null, null,
Map.of(), false)),
parseUnchecked(document(formats, "{\"type\":\"thing\",\"fill\":{\"ThingName\":\"Name\"},"
+ "\"unless\":{\"Bus\":[\"default\"]},\"absent\":{\"Bus\":\"default\"},"
+ "\"arnOf\":{\"Bus\":\"thing\"}}")).operations().get("a:Op"));
assertEquals(List.of(new Resource(List.of("thing"), null, Map.of("ThingName", new Member("Names", true)), Map.of(),
Map.of(), Map.of(), Map.of(), new Member("registryId", false), null)),
Map.of(), Map.of(), Map.of(), new Member("registryId", false), null, Map.of(), false)),
parseUnchecked(document(formats, "{\"type\":\"thing\",\"fill\":{\"ThingName\":\"Names[]\"},"
+ "\"account\":\"registryId\"}")).operations().get("a:Op"));
}
/**
* 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.
* account member names when it has one, with those of the operation's resources every request names, a create's
* new resource no member names (Cloud Map's {@code CreateService}).
*/
@TestFactory
Stream<DynamicTest> everyResourceOfTheTableIsReadFromAJsonBodyAndAForm() {
@@ -197,9 +227,15 @@ class RequestResourceMembersTest {
put(body, member, value);
form.append('&').append(parameter(member)).append('=').append(encoded(value));
});
assertEquals(List.of(expected), RequestResources.of(operation.getKey(), json(body.toString()),
List<String> named = operation.getValue().stream()
.filter(other -> other == resource || named(other))
.map(other -> other == resource ? expected : expected(
table.formats().get(namespace).get(other.types().getFirst()).format(), other,
"000000000000"))
.distinct().toList();
assertEquals(named, RequestResources.of(operation.getKey(), json(body.toString()),
"aws", "us-east-1", "000000000000"), body.toString());
assertEquals(List.of(expected), RequestResources.of(operation.getKey(), form(form.toString()),
assertEquals(named, RequestResources.of(operation.getKey(), form(form.toString()),
"aws", "us-east-1", "000000000000"), form.toString());
})));
}
@@ -230,6 +266,12 @@ class RequestResourceMembersTest {
})));
}
/** 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.when().isEmpty()
&& resource.unless().isEmpty();
}
/** The value at the member's path in a body: in its list's first element, and in its field's structure. */
private static void put(ObjectNode body, Member member, String value) {
if (member.field() == null) {
@@ -262,6 +304,9 @@ class RequestResourceMembersTest {
for (String variable : resource.fill().keySet()) {
arn = arn.replace("${" + variable + "}", "x");
}
for (Map.Entry<String, String> variable : resource.fixed().entrySet()) {
arn = arn.replace("${" + variable.getKey() + "}", variable.getValue());
}
return arn.replaceAll("\\$\\{\\w+}", "*");
}
@@ -364,6 +364,60 @@ class RequestResourcesTest {
"{\"repositoryNames\":[\"${Account}\"]}")));
}
/**
* A pipeline is the one its request's {@code pipeline.name} names; its {@code roleArn}, the role CodePipeline
* assumes, names none, even when it holds a pipeline's ARN. A webhook is the one {@code webhook.name} names, on
* the pipeline {@code webhook.targetPipeline} names, and its tags go on the webhook alone.
*/
@Test
void aPipelineIsTheOneItsStructuresNameNames() {
String codePipeline = "arn:aws:codepipeline:us-east-1:000000000000:";
assertEquals(List.of(codePipeline + "b"), of("codepipeline:UpdatePipeline",
json("{\"pipeline\":{\"name\":\"b\",\"roleArn\":\"" + codePipeline + "a\"}}")));
assertEquals(List.of(codePipeline + "b"), of("codepipeline:CreatePipeline", form("Action=CreatePipeline"
+ "&pipeline.name=b&pipeline.roleArn=" + codePipeline + "a")));
assertEquals(List.of(), of("codepipeline:UpdatePipeline", json("{\"pipeline\":{\"roleArn\":\"" + codePipeline
+ "a\"}}")));
assertEquals(List.of(codePipeline + "webhook:w", codePipeline + "p"), of("codepipeline:PutWebhook",
json("{\"webhook\":{\"name\":\"w\",\"targetPipeline\":\"p\"}}")));
// A webhook's tags go on the webhook, not on the pipeline it connects to.
assertEquals(List.of(codePipeline + "webhook:w"), created("codepipeline:PutWebhook",
json("{\"webhook\":{\"name\":\"w\",\"targetPipeline\":\"p\"}}")));
}
/** A custom action type is {@code Custom}'s, the owner CodePipeline gives every one, which its request does not. */
@Test
void aCustomActionTypeIsCustoms() {
assertEquals(List.of("arn:aws:codepipeline:us-east-1:000000000000:actiontype:Custom/Build/MyProvider/1"),
of("codepipeline:CreateCustomActionType",
json("{\"category\":\"Build\",\"provider\":\"MyProvider\",\"version\":\"1\"}")));
assertEquals(List.of("arn:aws:codepipeline:us-east-1:000000000000:actiontype:Custom/Build/MyProvider/1"),
of("codepipeline:DeleteCustomActionType",
json("{\"category\":\"Build\",\"provider\":\"MyProvider\",\"version\":\"1\"}")));
assertEquals(List.of(), of("codepipeline:DeleteCustomActionType", json("{}")));
}
/**
* A Cloud Map service is created in the namespace {@code NamespaceId} names, or {@code DnsConfig.NamespaceId}
* when it names none or gives a JSON null, as the handler reads them, and tags go on the service it creates,
* not on the namespace.
*/
@Test
void aServiceIsCreatedInTheNamespaceItsHandlerReads() {
String cloudMap = "arn:aws:servicediscovery:us-east-1:000000000000:";
assertEquals(List.of(cloudMap + "namespace/ns-a", cloudMap + "service/*"), of("servicediscovery:CreateService",
json("{\"Name\":\"s\",\"NamespaceId\":\"ns-a\"}")));
assertEquals(List.of(cloudMap + "namespace/ns-b", cloudMap + "service/*"), of("servicediscovery:CreateService",
json("{\"Name\":\"s\",\"DnsConfig\":{\"NamespaceId\":\"ns-b\"}}")));
assertEquals(List.of(cloudMap + "namespace/ns-a", cloudMap + "service/*"), of("servicediscovery:CreateService",
json("{\"Name\":\"s\",\"NamespaceId\":\"ns-a\",\"DnsConfig\":{\"NamespaceId\":\"ns-b\"}}")));
// A JSON null is no namespace, as the handler reads it, which then takes DnsConfig's.
assertEquals(List.of(cloudMap + "namespace/ns-b", cloudMap + "service/*"), of("servicediscovery:CreateService",
json("{\"Name\":\"s\",\"NamespaceId\":null,\"DnsConfig\":{\"NamespaceId\":\"ns-b\"}}")));
assertEquals(List.of(cloudMap + "service/*"), created("servicediscovery:CreateService",
json("{\"Name\":\"s\",\"NamespaceId\":\"ns-a\"}")));
}
@Test
void aRequestNamingNoResourceOrOfANamespaceTheTableDoesNotReadIsCheckedOnStar() {
assertEquals(List.of(), of("ecr:DescribeRepositories", json("{}")));
@@ -18,7 +18,9 @@ import org.junit.jupiter.api.extension.RegisterExtension;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
import static io.restassured.RestAssured.given;
@@ -489,6 +491,277 @@ class IamRequestResourceEnforcementIntegrationTest {
+ "\",\"tags\":[{\"Key\":\"a\",\"Value\":\"b\"}]}").post("/").then(), "ecr:TagResource");
}
@Test
void aDeliveryStreamIsCheckedOnTheArnItsNameGives() {
String name = "res-stream-" + suffix();
String arn = adminJson("firehose", "Firehose_20150804.CreateDeliveryStream")
.body("{\"DeliveryStreamName\":\"" + name + "\"}").post("/").then().statusCode(200)
.extract().path("DeliveryStreamARN");
cleanup.register(() -> adminJson("firehose", "Firehose_20150804.DeleteDeliveryStream")
.body("{\"DeliveryStreamName\":\"" + name + "\"}").post("/"));
assertEquals("arn:aws:firehose:" + REGION + ":" + ACCOUNT + ":deliverystream/" + name, arn);
User user = user("res-stream");
user.policy(allowOn("firehose:DescribeDeliveryStream", arn));
describeDeliveryStream(user, name).statusCode(200);
refusedJson(describeDeliveryStream(user, name + "-other"), "firehose:DescribeDeliveryStream");
}
/** An identity pool is checked on the ARN its id gives, the one Cognito tags it under. */
@Test
void anIdentityPoolIsCheckedOnTheArnItsIdGives() {
String id = adminJson("cognito-identity", "AWSCognitoIdentityService.CreateIdentityPool")
.body("{\"IdentityPoolName\":\"res pool " + suffix() + "\",\"AllowUnauthenticatedIdentities\":false}")
.post("/").then().statusCode(200).extract().path("IdentityPoolId");
cleanup.register(() -> adminJson("cognito-identity", "AWSCognitoIdentityService.DeleteIdentityPool")
.body("{\"IdentityPoolId\":\"" + id + "\"}").post("/"));
String arn = "arn:aws:cognito-identity:" + REGION + ":" + ACCOUNT + ":identitypool/" + id;
adminJson("cognito-identity", "AWSCognitoIdentityService.TagResource")
.body("{\"ResourceArn\":\"" + arn + "\",\"Tags\":{\"a\":\"b\"}}").post("/").then().statusCode(200);
User user = user("res-identity-pool");
user.policy(allowOn("cognito-identity:DescribeIdentityPool", arn));
describeIdentityPool(user, id).statusCode(200);
refusedJson(describeIdentityPool(user, REGION + ":" + UUID.randomUUID()), "cognito-identity:DescribeIdentityPool");
}
@Test
void aCloudMapNamespaceAndServiceAreCheckedOnTheArnsTheirIdsGive() {
String namespaceId = cloudMapNamespace("res-ns-" + suffix());
String namespaceArn = adminJson("servicediscovery", "Route53AutoNaming_v20170314.GetNamespace")
.body("{\"Id\":\"" + namespaceId + "\"}").post("/").then().statusCode(200).extract().path("Namespace.Arn");
assertEquals("arn:aws:servicediscovery:" + REGION + ":" + ACCOUNT + ":namespace/" + namespaceId, namespaceArn);
Map<String, String> service = adminJson("servicediscovery", "Route53AutoNaming_v20170314.CreateService")
.body("{\"Name\":\"res-svc\",\"NamespaceId\":\"" + namespaceId + "\"}").post("/").then().statusCode(200)
.extract().path("Service");
cleanup.register(() -> adminJson("servicediscovery", "Route53AutoNaming_v20170314.DeleteService")
.body("{\"Id\":\"" + service.get("Id") + "\"}").post("/"));
assertEquals("arn:aws:servicediscovery:" + REGION + ":" + ACCOUNT + ":service/" + service.get("Id"),
service.get("Arn"));
User user = user("res-cloud-map");
user.policy(allowOn("servicediscovery:GetNamespace", namespaceArn) + ","
+ allowOn("servicediscovery:GetService", service.get("Arn")));
cloudMap(user, "GetNamespace", namespaceId).statusCode(200);
refusedJson(cloudMap(user, "GetNamespace", "ns-" + suffix()), "servicediscovery:GetNamespace");
cloudMap(user, "GetService", service.get("Id")).statusCode(200);
refusedJson(cloudMap(user, "GetService", "srv-" + suffix()), "servicediscovery:GetService");
}
@Test
void aCertificateIsCheckedOnItsArn() {
String arn = adminJson("acm", "CertificateManager.RequestCertificate")
.body("{\"DomainName\":\"res-" + suffix() + ".example.com\"}").post("/").then().statusCode(200)
.extract().path("CertificateArn");
cleanup.register(() -> adminJson("acm", "CertificateManager.DeleteCertificate")
.body("{\"CertificateArn\":\"" + arn + "\"}").post("/"));
assertTrue(arn.startsWith("arn:aws:acm:" + REGION + ":" + ACCOUNT + ":certificate/"), arn);
User user = user("res-certificate");
user.policy(allowOn("acm:DescribeCertificate", arn));
describeCertificate(user, arn).statusCode(200);
refusedJson(describeCertificate(user, "arn:aws:acm:" + REGION + ":" + ACCOUNT + ":certificate/" + UUID.randomUUID()),
"acm:DescribeCertificate");
}
/**
* A pipeline is checked on the ARN its name gives, a request naming it by a bare {@code name} too, and a webhook
* on {@code webhook:<name>}, the ARN CodePipeline gives it.
*/
@Test
void aPipelineAndAWebhookAreCheckedOnTheArnsTheirNamesGive() {
String name = "res-pipeline-" + suffix();
adminJson("codepipeline", "CodePipeline_20150709.CreatePipeline").body(pipeline(name)).post("/")
.then().statusCode(200);
cleanup.register(() -> adminJson("codepipeline", "CodePipeline_20150709.DeletePipeline")
.body("{\"name\":\"" + name + "\"}").post("/"));
String pipelineArn = adminJson("codepipeline", "CodePipeline_20150709.GetPipeline")
.body("{\"name\":\"" + name + "\"}").post("/").then().statusCode(200).extract().path("metadata.pipelineArn");
assertEquals("arn:aws:codepipeline:" + REGION + ":" + ACCOUNT + ":" + name, pipelineArn);
User user = user("res-pipeline");
user.policy(allowOn("codepipeline:GetPipeline", pipelineArn) + ","
+ allowOn("codepipeline:GetPipelineState", pipelineArn));
codePipeline(user, "GetPipeline", "{\"name\":\"" + name + "\"}").statusCode(200);
codePipeline(user, "GetPipelineState", "{\"name\":\"" + name + "\"}").statusCode(200);
refusedJson(codePipeline(user, "GetPipelineState", "{\"name\":\"" + name + "-other\"}"),
"codepipeline:GetPipelineState");
String hook = "res-hook-" + suffix();
String hookArn = adminJson("codepipeline", "CodePipeline_20150709.PutWebhook").body("{\"webhook\":{\"name\":\""
+ hook + "\",\"targetPipeline\":\"" + name + "\",\"targetAction\":\"SourceAction\",\"filters\":"
+ "[{\"jsonPath\":\"$.ref\",\"matchEquals\":\"refs/heads/main\"}],\"authentication\":\"UNAUTHENTICATED\"}}")
.post("/").then().statusCode(200).extract().path("webhook.arn");
assertEquals("arn:aws:codepipeline:" + REGION + ":" + ACCOUNT + ":webhook:" + hook, hookArn);
user.policy(allowOn("codepipeline:DeleteWebhook", hookArn));
refusedJson(codePipeline(user, "DeleteWebhook", "{\"name\":\"" + hook + "-other\"}"), "codepipeline:DeleteWebhook");
codePipeline(user, "DeleteWebhook", "{\"name\":\"" + hook + "\"}").statusCode(200);
}
/** {@code StartPipelineExecution} names its pipeline by a bare {@code name}, on which it is checked. */
@Test
void aPipelineExecutionIsCheckedOnThePipelineItsNameGives() {
String name = "res-pipeline-" + suffix();
User user = user("res-pipeline-start");
user.policy(allowOn("codepipeline:StartPipelineExecution",
"arn:aws:codepipeline:" + REGION + ":" + ACCOUNT + ":" + name));
codePipeline(user, "StartPipelineExecution", "{\"name\":\"" + name + "\"}").statusCode(400)
.body(containsString("PipelineNotFoundException"));
refusedJson(codePipeline(user, "StartPipelineExecution", "{\"name\":\"" + name + "-other\"}"),
"codepipeline:StartPipelineExecution");
}
/**
* An update is checked on the pipeline its {@code pipeline.name} names, which it changes, not on one its
* {@code roleArn} holds: a grant on one pipeline does not let a request naming another through by giving the
* first's ARN as its role.
*/
@Test
void anUpdateIsCheckedOnThePipelineItNamesNotOneItsRoleHolds() {
String allowed = "res-pipeline-" + suffix();
String other = allowed + "-other";
for (String name : List.of(allowed, other)) {
adminJson("codepipeline", "CodePipeline_20150709.CreatePipeline").body(pipeline(name)).post("/")
.then().statusCode(200);
cleanup.register(() -> adminJson("codepipeline", "CodePipeline_20150709.DeletePipeline")
.body("{\"name\":\"" + name + "\"}").post("/"));
}
String allowedArn = "arn:aws:codepipeline:" + REGION + ":" + ACCOUNT + ":" + allowed;
User user = user("res-pipeline-update");
user.policy(allowOn("codepipeline:UpdatePipeline", allowedArn) + ","
+ allowOn("iam:PassRole", "arn:aws:iam::" + ACCOUNT + ":role/codepipeline-role"));
refusedJson(codePipeline(user, "UpdatePipeline", pipeline(other).replace(
"arn:aws:iam::" + ACCOUNT + ":role/codepipeline-role", allowedArn)), "codepipeline:UpdatePipeline");
codePipeline(user, "UpdatePipeline", pipeline(allowed)).statusCode(200);
}
/** A webhook's tags are checked on the webhook, not on the pipeline it connects to, which they do not go on. */
@Test
void aWebhooksTagsAreCheckedOnTheWebhookNotItsPipeline() {
String name = "res-pipeline-" + suffix();
adminJson("codepipeline", "CodePipeline_20150709.CreatePipeline").body(pipeline(name)).post("/")
.then().statusCode(200);
cleanup.register(() -> adminJson("codepipeline", "CodePipeline_20150709.DeletePipeline")
.body("{\"name\":\"" + name + "\"}").post("/"));
String hook = "res-hook-" + suffix();
String codePipeline = "arn:aws:codepipeline:" + REGION + ":" + ACCOUNT + ":";
User user = user("res-webhook-tags");
user.policy(allowOn("codepipeline:PutWebhook", codePipeline + "webhook:" + hook) + ","
+ allowOn("codepipeline:PutWebhook", codePipeline + name) + ","
+ allowOn("codepipeline:TagResource", codePipeline + "webhook:*"));
codePipeline(user, "PutWebhook", "{\"webhook\":{\"name\":\"" + hook + "\",\"targetPipeline\":\"" + name
+ "\",\"targetAction\":\"SourceAction\",\"filters\":[{\"jsonPath\":\"$.ref\",\"matchEquals\":"
+ "\"refs/heads/main\"}],\"authentication\":\"UNAUTHENTICATED\"},\"tags\":[{\"key\":\"a\",\"value\":\"b\"}]}")
.statusCode(200);
cleanup.register(() -> adminJson("codepipeline", "CodePipeline_20150709.DeleteWebhook")
.body("{\"name\":\"" + hook + "\"}").post("/"));
}
/**
* A service is created in the namespace its {@code DnsConfig.NamespaceId} names when the request gives no
* {@code NamespaceId}, or a JSON null for it, as Cloud Map reads it, so a deny on that namespace holds.
*/
@Test
void aServiceIsCheckedOnTheNamespaceItsDnsConfigNames() {
String namespaceId = cloudMapNamespace("res-ns-dns-" + suffix());
User user = user("res-cloud-map-dns");
user.policy(allowOn("servicediscovery:CreateService", "*") + ",{\"Effect\":\"Deny\",\"Action\":"
+ "\"servicediscovery:CreateService\",\"Resource\":\"arn:aws:servicediscovery:" + REGION + ":" + ACCOUNT
+ ":namespace/" + namespaceId + "\"}");
String dnsConfig = "\"DnsConfig\":{\"NamespaceId\":\"" + namespaceId + "\",\"DnsRecords\":"
+ "[{\"Type\":\"A\",\"TTL\":60}]}";
refusedJson(user.json("servicediscovery", "Route53AutoNaming_v20170314.CreateService", "1.1")
.body("{\"Name\":\"res-svc\"," + dnsConfig + "}").post("/").then(), "servicediscovery:CreateService");
refusedJson(user.json("servicediscovery", "Route53AutoNaming_v20170314.CreateService", "1.1")
.body("{\"Name\":\"res-svc\",\"NamespaceId\":null," + dnsConfig + "}").post("/").then(),
"servicediscovery:CreateService");
}
/** A service's tags are checked on the service it creates, not on the namespace it creates it in. */
@Test
void aServicesTagsAreCheckedOnTheServiceNotItsNamespace() {
String namespaceId = cloudMapNamespace("res-ns-tags-" + suffix());
String cloudMap = "arn:aws:servicediscovery:" + REGION + ":" + ACCOUNT + ":";
User user = user("res-cloud-map-tags");
user.policy(allowOn("servicediscovery:CreateService", cloudMap + "namespace/" + namespaceId) + ","
+ allowOn("servicediscovery:CreateService", cloudMap + "service/*") + ","
+ allowOn("servicediscovery:TagResource", cloudMap + "service/*"));
String serviceId = user.json("servicediscovery", "Route53AutoNaming_v20170314.CreateService", "1.1")
.body("{\"Name\":\"res-svc\",\"NamespaceId\":\"" + namespaceId + "\",\"Tags\":[{\"Key\":\"a\",\"Value\":\"b\"}]}")
.post("/").then().statusCode(200).extract().path("Service.Id");
cleanup.register(() -> adminJson("servicediscovery", "Route53AutoNaming_v20170314.DeleteService")
.body("{\"Id\":\"" + serviceId + "\"}").post("/"));
}
/**
* A custom action type is checked on {@code actiontype:Custom/...}, its owner being {@code Custom}, which the
* request does not give, so a grant on the ARN CodePipeline gives it allows creating and deleting it.
*/
@Test
void aCustomActionTypeIsCheckedAsCustoms() {
String provider = "Res" + suffix();
String arn = "arn:aws:codepipeline:" + REGION + ":" + ACCOUNT + ":actiontype:Custom/Build/" + provider + "/1";
User user = user("res-action-type");
user.policy(allowOn("codepipeline:CreateCustomActionType", arn) + ","
+ allowOn("codepipeline:DeleteCustomActionType", arn));
String type = "\"category\":\"Build\",\"provider\":\"" + provider + "\",\"version\":\"1\"";
codePipeline(user, "CreateCustomActionType", "{" + type + ",\"inputArtifactDetails\":{\"minimumCount\":0,"
+ "\"maximumCount\":1},\"outputArtifactDetails\":{\"minimumCount\":0,\"maximumCount\":1}}")
.statusCode(200).body("actionType.id.owner", equalTo("Custom"));
refusedJson(codePipeline(user, "DeleteCustomActionType", "{" + type.replace(provider, provider + "x") + "}"),
"codepipeline:DeleteCustomActionType");
codePipeline(user, "DeleteCustomActionType", "{" + type + "}").statusCode(200);
}
/** A Cloud Map HTTP namespace, created for the test and deleted after it; its id. */
private String cloudMapNamespace(String name) {
adminJson("servicediscovery", "Route53AutoNaming_v20170314.CreateHttpNamespace")
.body("{\"Name\":\"" + name + "\"}").post("/").then().statusCode(200);
String id = adminJson("servicediscovery", "Route53AutoNaming_v20170314.ListNamespaces").body("{}")
.post("/").then().statusCode(200).extract().path("Namespaces.find { it.Name == '" + name + "' }.Id");
cleanup.register(() -> adminJson("servicediscovery", "Route53AutoNaming_v20170314.DeleteNamespace")
.body("{\"Id\":\"" + id + "\"}").post("/"));
return id;
}
private static RequestSpecification adminJson(String service, String target) {
return admin(service).header("X-Amz-Target", target).contentType("application/x-amz-json-1.1");
}
private static ValidatableResponse describeDeliveryStream(User user, String name) {
return user.json("firehose", "Firehose_20150804.DescribeDeliveryStream", "1.1")
.body("{\"DeliveryStreamName\":\"" + name + "\"}").post("/").then();
}
private static ValidatableResponse describeIdentityPool(User user, String id) {
return user.json("cognito-identity", "AWSCognitoIdentityService.DescribeIdentityPool", "1.1")
.body("{\"IdentityPoolId\":\"" + id + "\"}").post("/").then();
}
private static ValidatableResponse cloudMap(User user, String operation, String id) {
return user.json("servicediscovery", "Route53AutoNaming_v20170314." + operation, "1.1")
.body("{\"Id\":\"" + id + "\"}").post("/").then();
}
private static ValidatableResponse describeCertificate(User user, String arn) {
return user.json("acm", "CertificateManager.DescribeCertificate", "1.1")
.body("{\"CertificateArn\":\"" + arn + "\"}").post("/").then();
}
private static ValidatableResponse codePipeline(User user, String operation, String body) {
return user.json("codepipeline", "CodePipeline_20150709." + operation, "1.1").body(body).post("/").then();
}
/** A pipeline with a source and a build stage, the two stages CodePipeline requires. */
private static String pipeline(String name) {
return "{\"pipeline\":{\"name\":\"" + name + "\",\"roleArn\":\"arn:aws:iam::" + ACCOUNT + ":role/codepipeline-role\","
+ "\"artifactStore\":{\"type\":\"S3\",\"location\":\"codepipeline-artifacts\"},\"stages\":["
+ "{\"name\":\"Source\",\"actions\":[{\"name\":\"SourceAction\",\"actionTypeId\":{\"category\":\"Source\","
+ "\"owner\":\"AWS\",\"provider\":\"S3\",\"version\":\"1\"},\"runOrder\":1}]},"
+ "{\"name\":\"Build\",\"actions\":[{\"name\":\"BuildAction\",\"actionTypeId\":{\"category\":\"Build\","
+ "\"owner\":\"AWS\",\"provider\":\"CodeBuild\",\"version\":\"1\"},\"runOrder\":1}]}]}}";
}
private static RequestSpecification tagged(RequestSpecification request) {
return request.formParam("Tags.member.1.Key", "elbv2.k8s.aws/cluster").formParam("Tags.member.1.Value", "c");
}