Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/main/resources/aws/iam-request-resources.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,8 @@ Stream<DynamicTest> everyResourceOfTheTableIsReadFromAJsonBodyAndAForm() {
.map(resource -> DynamicTest.dynamicTest(operation.getKey() + " " + resource, () -> {
String namespace = operation.getKey().substring(0, operation.getKey().indexOf(':'));
String name = operation.getKey().substring(namespace.length() + 1);
ArnFormat format = table.formats().get(namespace).get(resource.types().getFirst());
String type = exampleType(resource, operation.getValue());
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
Expand Down Expand Up @@ -536,7 +537,7 @@ Stream<DynamicTest> everyResourceOfTheTableIsReadFromAJsonBodyAndAForm() {
});
List<String> named = operation.getValue().stream()
.filter(other -> (other == resource || named(other) || sharesAMember(other, given)
|| fillsFromTheArn(other, resource)
|| 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
Expand Down Expand Up @@ -629,12 +630,25 @@ Stream<DynamicTest> everyResourceACreateMakesIsNamedFromItsParentsArn() {
/**
* 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.
* the type the test gives names it too.
*/
private static boolean fillsFromTheArn(Resource resource, Resource other) {
private static boolean fillsFromTheArn(Resource resource, Resource other, String type) {
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()));
&& type.equals(resource.arnOf().get(other.arn().path()));
}

/**
* The type of the ARN the test gives a resource's member: for an ARN member that also fills a resource of one of
* the types it is read as, that resource's type, as the member holds one of those (OAM's {@code CreateLink} reads
* its {@code SinkIdentifier} as a link or a sink and fills the sink from it); else its first type.
*/
private static String exampleType(Resource resource, List<Resource> resources) {
return resource.arn() == null ? resource.types().getFirst() : resources.stream()
.filter(other -> other.arn() == null && other.fill().containsValue(resource.arn())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: the method comment is accurate but dense. Consider adding a brief inline example like // e.g. CreateLink's SinkIdentifier is read as Link or Sink, and fills the Sink, so the test gives it a Sink ARN to make it easier to follow on a quick read.

&& !other.arnOf().containsKey(resource.arn().path())
&& resource.types().contains(other.types().getFirst()))
.map(other -> other.types().getFirst()).findFirst().orElse(resource.types().getFirst());
}

/** Whether every request of its operation names the resource: no member names it and no member's value selects it. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ void anArnOfATypeAwsOwnsIsCheckedInItsAccountAlone() {
/**
* An ARN naming another account whose service acts on that account's resource for the caller (the sink an OAM
* link is made to, a RAM invitation sent to it) is that resource alone, never the caller's of that name too; one
* the service holds no such resource under is still checked as the caller's own too.
* the service holds no such resource under is still checked as the caller's own too. The link {@code CreateLink}
* makes is the caller's.
*/
@Test
void anArnOfAnotherAccountItsServiceActsOnThereIsCheckedAsGivenAlone() {
Expand All @@ -345,13 +346,15 @@ void anArnOfAnotherAccountItsServiceActsOnThereIsCheckedAsGivenAlone() {
String link = "{\"SinkIdentifier\":\"" + sink + "\",\"LabelTemplate\":\"$AccountName\"}";
String accept = "{\"resourceShareInvitationArn\":\"" + invitation + "\"}";

assertEquals(List.of(sink), RequestResources.of("oam:CreateLink", json(link), "aws", "us-east-1",
String made = "arn:aws:oam:us-east-1:000000000000:link/*";
assertEquals(List.of(sink, made), RequestResources.of("oam:CreateLink", json(link), "aws", "us-east-1",
"000000000000", reaching("oam", sink)));
assertEquals(List.of(invitation), RequestResources.of("ram:AcceptResourceShareInvitation", json(accept), "aws",
"us-east-1", "000000000000", reaching("ram", invitation)));
assertEquals(List.of(sink, "arn:aws:oam:us-east-1:000000000000:sink/abc"), RequestResources.of("oam:CreateLink",
json(link), "aws", "us-east-1", "000000000000", reaching("oam", "arn:aws:oam:us-east-1:111122223333:sink/x")));
assertEquals(List.of(sink, "arn:aws:oam:us-east-1:000000000000:sink/abc"), of("oam:CreateLink", json(link)));
String own = "arn:aws:oam:us-east-1:000000000000:sink/abc";
assertEquals(List.of(sink, own, made), RequestResources.of("oam:CreateLink", json(link), "aws", "us-east-1",
"000000000000", reaching("oam", "arn:aws:oam:us-east-1:111122223333:sink/x")));
assertEquals(List.of(sink, own, made), of("oam:CreateLink", json(link)));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1590,13 +1590,17 @@ void oamCreateLinkIsCheckedOnTheSinkOfTheAccountItsArnNamesAlone() {
String body = "{\"SinkIdentifier\":\"" + sink + "\",\"LabelTemplate\":\"$AccountName\","
+ "\"ResourceTypes\":[\"AWS::CloudWatch::Metric\"]}";

String links = "arn:aws:oam:us-east-1:" + ACCOUNT + ":link/*";

// The sink is the other account's, which the link is made to: a grant on a sink of that name of the caller's
// own is no grant on it.
policy(user, allow(action, sink.replace(PEER, ACCOUNT)));
// own is no grant on it. The link it makes is the caller's, and is checked too.
policy(user, allow(action, sink.replace(PEER, ACCOUNT)) + "," + allow(action, links));
refused(request(user, "oam", "POST", "/CreateLink", body), action);
policy(user, allow(action, "*") + "," + deny(action, sink));
refused(request(user, "oam", "POST", "/CreateLink", body), action);
policy(user, allow(action, sink));
refused(request(user, "oam", "POST", "/CreateLink", body), action);
policy(user, allow(action, sink) + "," + allow(action, links));
String link = request(user, "oam", "POST", "/CreateLink", body).statusCode(200).extract().path("Arn");
cleanup.register(() -> adminOf("oam").body("{\"Identifier\":\"" + link + "\"}").post("/DeleteLink"));
}
Expand Down
Loading