mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudmap): give resource-tag conditions the tags of the namespace or service a request names (#8932)
The generated request resources give Cloud Map's namespace and service types the aws:ResourceTag/ key, but no service answered the servicediscovery namespace's stored tags, so a condition on their tags saw none. CloudMapService now answers it: storedTags reads the namespace or the service by the id its ARN ends in, in the request's account, in place, the id the handlers find it by in every Region, so a request sent to another Region is checked with the tags of the resource it acts on; a tag without a value reads as an empty one. TagResource and UntagResource changed the tag map in place with no lock; they now give the resource a new map one call at a time, and the tags fields are volatile.
This commit is contained in:
1 parent
813f89bd8e
commit
26dc6d79ed
7 files changed
+214
-18
No files matched your search
@@ -0,0 +1,77 @@
|
||||
package io.floci.aws.services.cloudmap;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.storage.InMemoryStorage;
|
||||
import io.floci.aws.services.cloudmap.model.Namespace;
|
||||
import io.floci.aws.services.cloudmap.model.Service;
|
||||
import io.floci.aws.testing.InMemoryStorageFactory;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/** IAM's lookup of the tags Cloud Map holds on a namespace or a service. */
|
||||
class CloudMapStoredTagsTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String ACCOUNT = "000000000000";
|
||||
|
||||
/**
|
||||
* The lookup reads the tags of the namespace or the service the ARN names, as the tag calls leave them, a tag
|
||||
* without a value as an empty one, and those of the one the handlers find by its id for an ARN naming another
|
||||
* Region; nothing for another account's, an unknown resource's or another service's ARN.
|
||||
*/
|
||||
@Test
|
||||
void storedTagsAreTheTagsOfTheNamespaceOrServiceTheArnNames() {
|
||||
CloudMapService service = service(new InMemoryStorageFactory(ACCOUNT));
|
||||
String namespaceId = service.createHttpNamespace("stored-tags", null, null,
|
||||
Map.of("team", "web", "env", "dev"), REGION).getTargets().get("NAMESPACE");
|
||||
String namespaceArn = service.getNamespace(namespaceId).getArn();
|
||||
service.tagResource(namespaceArn, Map.of("team", "data"));
|
||||
service.untagResource(namespaceArn, List.of("env"));
|
||||
Map<String, String> noValue = new HashMap<>();
|
||||
noValue.put("owner", null);
|
||||
Service svc = service.createService("stored-tags", namespaceId, null, null, null, null, null, null,
|
||||
noValue, REGION);
|
||||
service.tagResource(svc.getArn(), Map.of("team", "ops"));
|
||||
|
||||
assertEquals("servicediscovery", service.namespace());
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(namespaceArn));
|
||||
assertEquals(Map.of("owner", "", "team", "ops"), service.storedTags(svc.getArn()));
|
||||
assertTrue(service.storedTags(namespaceArn.replace(ACCOUNT, "111122223333")).isEmpty());
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(namespaceArn.replace(REGION, "us-west-2")));
|
||||
assertTrue(service.storedTags(namespaceArn.replace(namespaceId, "ns-missing")).isEmpty());
|
||||
assertTrue(service.storedTags(svc.getArn().replace("service/", "namespace/")).isEmpty());
|
||||
assertTrue(service.storedTags("arn:aws:ec2:us-east-1:000000000000:instance/i-0123456789abcdef0").isEmpty());
|
||||
}
|
||||
|
||||
/** A namespace stored before storage was account-aware is read in place: the lookup migrates nothing. */
|
||||
@Test
|
||||
void storedTagsReadAPreAccountAwareNamespaceInPlace() {
|
||||
InMemoryStorage<String, Namespace> raw = new InMemoryStorage<>();
|
||||
CloudMapService service =
|
||||
service(new InMemoryStorageFactory(ACCOUNT).withStore("cloudmap-namespaces.json", raw));
|
||||
Namespace legacy = new Namespace();
|
||||
legacy.setId("ns-legacy");
|
||||
legacy.setArn("arn:aws:servicediscovery:us-east-1:000000000000:namespace/ns-legacy");
|
||||
legacy.setTags(Map.of("team", "data"));
|
||||
raw.put("ns-legacy", legacy);
|
||||
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(legacy.getArn()));
|
||||
assertEquals(Set.of("ns-legacy"), raw.keys());
|
||||
}
|
||||
|
||||
private CloudMapService service(InMemoryStorageFactory storage) {
|
||||
return new CloudMapService(storage, mock(EmulatorConfig.class, RETURNS_DEEP_STUBS),
|
||||
new RegionResolver(REGION, ACCOUNT), new ObjectMapper(), null);
|
||||
}
|
||||
}
|
||||
+60
@@ -1116,6 +1116,66 @@ class IamRequestResourceEnforcementIntegrationTest {
|
||||
"servicediscovery:CreateService");
|
||||
}
|
||||
|
||||
/**
|
||||
* An {@code aws:ResourceTag} condition reads the tags Cloud Map holds on the namespace or the service a request
|
||||
* names by its id, as a tag call or the create leaves them, so an allow on one team's tag lets the caller read that
|
||||
* team's namespace and service and not another's.
|
||||
*/
|
||||
@Test
|
||||
void aCloudMapResourceTagConditionReadsTheTagsOfTheNamespaceAndTheService() {
|
||||
String tagged = cloudMapNamespace("res-ns-team-" + suffix());
|
||||
String other = cloudMapNamespace("res-ns-team-" + suffix());
|
||||
adminJson("servicediscovery", "Route53AutoNaming_v20170314.TagResource").body("{\"ResourceARN\":\""
|
||||
+ "arn:aws:servicediscovery:" + REGION + ":" + ACCOUNT + ":namespace/" + tagged + "\",\"Tags\":"
|
||||
+ teamTag("data") + "}").post("/").then().statusCode(200);
|
||||
String taggedService = cloudMapService(tagged, "data");
|
||||
String otherService = cloudMapService(other, "web");
|
||||
|
||||
User user = user("res-cloud-map-team");
|
||||
user.policy(onTeam("servicediscovery:GetNamespace") + "," + onTeam("servicediscovery:GetService"));
|
||||
cloudMap(user, "GetNamespace", tagged).statusCode(200);
|
||||
refusedJson(cloudMap(user, "GetNamespace", other), "servicediscovery:GetNamespace");
|
||||
cloudMap(user, "GetService", taggedService).statusCode(200);
|
||||
refusedJson(cloudMap(user, "GetService", otherService), "servicediscovery:GetService");
|
||||
}
|
||||
|
||||
/**
|
||||
* Cloud Map's handlers find a namespace by its id in every Region, so a request sent to another Region acts on it
|
||||
* and is checked with its tags: a deny on its team's tag holds there too.
|
||||
*/
|
||||
@Test
|
||||
void aCloudMapTagDenyHoldsForARequestSentToAnotherRegion() {
|
||||
String namespaceId = cloudMapNamespace("res-ns-deny-" + suffix());
|
||||
adminJson("servicediscovery", "Route53AutoNaming_v20170314.TagResource").body("{\"ResourceARN\":\""
|
||||
+ "arn:aws:servicediscovery:" + REGION + ":" + ACCOUNT + ":namespace/" + namespaceId + "\",\"Tags\":"
|
||||
+ teamTag("data") + "}").post("/").then().statusCode(200);
|
||||
|
||||
User user = user("res-cloud-map-deny");
|
||||
user.policy(allowOn("servicediscovery:GetNamespace", "*") + ",{\"Effect\":\"Deny\",\"Action\":"
|
||||
+ "\"servicediscovery:GetNamespace\",\"Resource\":\"*\",\"Condition\":{\"StringEquals\":"
|
||||
+ "{\"aws:ResourceTag/team\":\"data\"}}}");
|
||||
refusedJson(given().header("Authorization", auth(user.accessKeyId(), "us-west-2", "servicediscovery"))
|
||||
.header("X-Amz-Target", "Route53AutoNaming_v20170314.GetNamespace")
|
||||
.contentType("application/x-amz-json-1.1").body("{\"Id\":\"" + namespaceId + "\"}").post("/").then(),
|
||||
"servicediscovery:GetNamespace");
|
||||
}
|
||||
|
||||
/** An allow of {@code action} on every resource whose {@code team} tag is {@code data}. */
|
||||
private static String onTeam(String action) {
|
||||
return "{\"Effect\":\"Allow\",\"Action\":\"" + action + "\",\"Resource\":\"*\","
|
||||
+ "\"Condition\":{\"StringEquals\":{\"aws:ResourceTag/team\":\"data\"}}}";
|
||||
}
|
||||
|
||||
/** A Cloud Map service in the namespace, tagged with the team when created and deleted after the test; its id. */
|
||||
private String cloudMapService(String namespaceId, String team) {
|
||||
String id = adminJson("servicediscovery", "Route53AutoNaming_v20170314.CreateService")
|
||||
.body("{\"Name\":\"res-svc-" + suffix() + "\",\"NamespaceId\":\"" + namespaceId + "\",\"Tags\":"
|
||||
+ teamTag(team) + "}").post("/").then().statusCode(200).extract().path("Service.Id");
|
||||
cleanup.register(() -> adminJson("servicediscovery", "Route53AutoNaming_v20170314.DeleteService")
|
||||
.body("{\"Id\":\"" + id + "\"}").post("/").then().statusCode(200));
|
||||
return id;
|
||||
}
|
||||
|
||||
/** A service's tags are checked on the service it creates, not on the namespace it creates it in. */
|
||||
@Test
|
||||
void aServicesTagsAreCheckedOnTheServiceNotItsNamespace() {
|
||||
|
||||
Reference in new issue
Block a user