mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(opensearch): give resource-tag conditions the tags of the domain a request names (#8762)
The generated request resources give OpenSearch's domain type the aws:ResourceTag/ key, but no service answered the es namespace's stored tags, so a condition on a domain's tags saw none. OpenSearchService now answers it: storedTags reads the domain in the request's account, where the tag handlers read, in place under the key the ARN gives or the name a domain was stored under before its key named the Region, and matched on the whole ARN. AddTags and RemoveTags changed the domain's map in place with no lock; they now give it a new map one call at a time, and the tags field is volatile.
This commit is contained in:
1 parent
2517edb471
commit
2b3c22a3a5
6 files changed
+120
-11
No files matched your search
@@ -3203,8 +3203,8 @@ floci populates:
|
||||
permission sets, applications and trusted token issuers, OAM for its sinks and links, RAM for its resource shares,
|
||||
Payment Cryptography for its keys, Resource Explorer for its indexes and views, Application Auto Scaling for its
|
||||
scalable targets, ECS for its clusters, task definitions, services, capacity providers, tasks and container
|
||||
instances, ACM for its certificates, Firehose for its delivery streams, and Cognito for its user pools. Tag keys
|
||||
differing only in case are one condition key holding every value.
|
||||
instances, ACM for its certificates, Firehose for its delivery streams, Cognito for its user pools, and OpenSearch
|
||||
for its domains. Tag keys differing only in case are one condition key holding every value.
|
||||
- EC2 resource keys such as a network interface's `ec2:Vpc`, `ec2:Subnet`, `ec2:AvailabilityZone` and
|
||||
`ec2:NetworkInterfaceID`: for every action on a resource EC2 holds, the members of the resource the
|
||||
key names, an ARN for a key the Service Reference types as one. `resourceKeys` in
|
||||
|
||||
@@ -45,9 +45,9 @@ import java.util.Optional;
|
||||
* key value store a name names and the tags of any resource it tags; ECS, for the revision a task definition
|
||||
* reference resolves to, its default cluster and the tags of a cluster, a task definition, a service, a capacity
|
||||
* provider, a task or a container instance; ACM, for the tags of a certificate; Firehose, for the tags of a delivery
|
||||
* stream; Cognito, for the tags of a user pool); looked up lazily by {@code ResourceArnBuilder} and
|
||||
* {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for the namespace a request or an
|
||||
* ARN is of, so IAM never depends on that service directly.</p>
|
||||
* stream; Cognito, for the tags of a user pool; OpenSearch, for the tags of a domain); looked up lazily by
|
||||
* {@code ResourceArnBuilder} and {@code IamConditionContextResolver} through {@code Instance<StoredResourceArns>} for
|
||||
* the namespace a request or an ARN is of, so IAM never depends on that service directly.</p>
|
||||
*
|
||||
* <p>Each answer is a lookup of what the service holds, asked for by an entry of the generated table ({@code find},
|
||||
* {@code through}, a default, {@code forwarded}, a type's {@code resourceTagKeys}): never a fact about what an operation acts on, which the table alone
|
||||
|
||||
@@ -6,6 +6,7 @@ import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.AwsRegions;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.common.docker.ContainerStorageHelper;
|
||||
import io.floci.aws.core.resource.ExplorerResource;
|
||||
import io.floci.aws.core.resource.ResourceProvider;
|
||||
@@ -33,6 +34,7 @@ import java.security.SecureRandom;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -50,7 +52,7 @@ import java.util.concurrent.TimeUnit;
|
||||
* region from the domain's ARN, which has always carried it.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class OpenSearchService implements ResourceProvider {
|
||||
public class OpenSearchService implements ResourceProvider, StoredResourceArns {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(OpenSearchService.class);
|
||||
|
||||
@@ -352,9 +354,12 @@ public class OpenSearchService implements ResourceProvider {
|
||||
return domain;
|
||||
}
|
||||
|
||||
public void addTags(String arn, Map<String, String> tags) {
|
||||
/** Gives the domain a new map, its tags with {@code tags} added, one tag call at a time. */
|
||||
public synchronized void addTags(String arn, Map<String, String> tags) {
|
||||
Domain domain = findByArn(arn);
|
||||
domain.getTags().putAll(tags);
|
||||
Map<String, String> merged = new HashMap<>(domain.getTags());
|
||||
merged.putAll(tags);
|
||||
domain.setTags(merged);
|
||||
putDomain(domain);
|
||||
}
|
||||
|
||||
@@ -362,12 +367,55 @@ public class OpenSearchService implements ResourceProvider {
|
||||
return findByArn(arn).getTags();
|
||||
}
|
||||
|
||||
public void removeTags(String arn, List<String> tagKeys) {
|
||||
/** Gives the domain a new map, its tags without {@code tagKeys}, one tag call at a time. */
|
||||
public synchronized void removeTags(String arn, List<String> tagKeys) {
|
||||
Domain domain = findByArn(arn);
|
||||
tagKeys.forEach(domain.getTags()::remove);
|
||||
Map<String, String> kept = new HashMap<>(domain.getTags());
|
||||
tagKeys.forEach(kept::remove);
|
||||
domain.setTags(kept);
|
||||
putDomain(domain);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "es";
|
||||
}
|
||||
|
||||
/** A request names a domain by its name, which its ARN carries whole, so there is nothing to find. */
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* The tags of the domain whose ARN is {@code arn}, in the request's account, where the tag handlers read: an ARN of
|
||||
* another account names none. Read in place under the key the ARN gives, or the name a domain was stored under
|
||||
* before its key named the Region, a record from before storage was account-aware included, as {@link #findByArn}
|
||||
* finds it, and matched on the whole ARN. A tag call stores a new map, so the read is of one whole set.
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> storedTags(String arn) {
|
||||
if (!AwsArnUtils.isArnFor(arn, "es")) {
|
||||
return Map.of();
|
||||
}
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
String account = parsed.accountId();
|
||||
if (!account.equals(regionResolver.getAccountId()) || !parsed.resource().startsWith("domain/")) {
|
||||
return Map.of();
|
||||
}
|
||||
String domainName = parsed.resource().substring("domain/".length());
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
AccountAwareStorageBackend.readInAccount(domainStore, account, storageKey(parsed.region(), domainName))
|
||||
.or(() -> AccountAwareStorageBackend.readInAccount(domainStore, account, domainName))
|
||||
.filter(domain -> arn.equals(domain.getArn()))
|
||||
.ifPresent(domain -> domain.getTags().forEach((key, value) -> {
|
||||
if (key != null && value != null) {
|
||||
tags.put(key, value);
|
||||
}
|
||||
}));
|
||||
return tags;
|
||||
}
|
||||
|
||||
public Domain upgradeDomain(String domainName, String targetVersion, String region) {
|
||||
Domain domain = describeDomain(domainName, region);
|
||||
if (targetVersion == null || targetVersion.isBlank()) {
|
||||
|
||||
@@ -81,7 +81,7 @@ public class Domain {
|
||||
private String endpoint = "";
|
||||
|
||||
@JsonProperty("Tags")
|
||||
private Map<String, String> tags = new HashMap<>();
|
||||
private volatile Map<String, String> tags = new HashMap<>();
|
||||
|
||||
@JsonProperty("ContainerId")
|
||||
private String containerId;
|
||||
|
||||
@@ -813,6 +813,35 @@ class IamRestRouteEnforcementIntegrationTest {
|
||||
as(user, "es").get("/2021-01-01/opensearch/domain/other-" + domain).then().statusCode(409);
|
||||
}
|
||||
|
||||
/**
|
||||
* An {@code aws:ResourceTag} condition reads the tags OpenSearch holds on the domain a request names in its path,
|
||||
* as a tag call leaves them, so an allow on one team's tag lets the caller describe that team's domain and not
|
||||
* another's.
|
||||
*/
|
||||
@Test
|
||||
void anOpenSearchResourceTagConditionReadsTheTagsOfTheDomain() {
|
||||
String tagged = openSearchDomain("web");
|
||||
String other = openSearchDomain("web");
|
||||
adminOf("es").body("{\"ARN\":\"arn:aws:es:us-east-1:" + ACCOUNT + ":domain/" + tagged
|
||||
+ "\",\"TagList\":[{\"Key\":\"team\",\"Value\":\"data\"}]}").post("/2021-01-01/tags").then()
|
||||
.statusCode(200);
|
||||
String user = user("route-domain-tags");
|
||||
|
||||
policy(user, onTeam("Allow", "es:DescribeDomain", "data"));
|
||||
request(user, "es", "GET", "/2021-01-01/opensearch/domain/" + tagged, null).statusCode(200);
|
||||
refused(request(user, "es", "GET", "/2021-01-01/opensearch/domain/" + other, null), "es:DescribeDomain");
|
||||
}
|
||||
|
||||
/** The name of an OpenSearch domain an administrator creates tagged with the team, deleted after the test. */
|
||||
private String openSearchDomain(String team) {
|
||||
String name = "route-tags-" + suffix();
|
||||
adminOf("es").body("{\"DomainName\":\"" + name + "\",\"EngineVersion\":\"OpenSearch_2.11\",\"TagList\":"
|
||||
+ "[{\"Key\":\"team\",\"Value\":\"" + team + "\"}]}").post("/2021-01-01/opensearch/domain").then()
|
||||
.statusCode(200);
|
||||
cleanup.register(() -> adminOf("es").delete("/2021-01-01/opensearch/domain/" + name));
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* An Access Analyzer request naming its analyzer, in its path or its body, is checked on that analyzer, the ARN
|
||||
* Access Analyzer gives it: a caller with no grant is refused, a grant scoped to the analyzer lets
|
||||
|
||||
@@ -13,6 +13,7 @@ import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
@@ -215,6 +216,37 @@ class OpenSearchServiceTest {
|
||||
assertEquals(EU_WEST_1, service.describeDomain("dual-region", EU_WEST_1).getRegion());
|
||||
}
|
||||
|
||||
/**
|
||||
* IAM's tag lookup reads the tags of the domain whose ARN it is given, as tag calls leave them, a tag call giving
|
||||
* the domain a new map rather than changing the one a reader holds, and those of one stored under its name from
|
||||
* before keys named the Region, in place, without migrating it; nothing for an ARN of another account or Region, or
|
||||
* of no domain.
|
||||
*/
|
||||
@Test
|
||||
void storedTagsAreTheTagsOfTheDomainTheArnNamesAndTaggingPutsANewMap() {
|
||||
when(domainManager.tryStartDomain(any())).thenReturn(false);
|
||||
String arn = service.createDomain("tagged", "OpenSearch_2.11", null, null, Map.of("env", "dev"), US_EAST_1)
|
||||
.getArn();
|
||||
Map<String, String> held = service.listTags(arn);
|
||||
service.addTags(arn, Map.of("team", "data"));
|
||||
service.removeTags(arn, List.of("env"));
|
||||
String legacyArn = AwsArnUtils.Arn.of("es", US_EAST_1, ACCOUNT, "domain/legacy").toString();
|
||||
Domain legacy = new Domain();
|
||||
legacy.setDomainName("legacy");
|
||||
legacy.setArn(legacyArn);
|
||||
legacy.setTags(Map.of("tier", "web"));
|
||||
domainStore.putForAccount(ACCOUNT, "legacy", legacy);
|
||||
|
||||
assertEquals(Map.of("env", "dev"), held);
|
||||
assertEquals(Map.of("team", "data"), service.storedTags(arn));
|
||||
assertEquals(Map.of("tier", "web"), service.storedTags(legacyArn));
|
||||
assertTrue(domainStore.getForAccount(ACCOUNT, "legacy").isPresent());
|
||||
assertEquals(Map.of(), service.storedTags(arn.replace(":" + ACCOUNT + ":", ":111122223333:")));
|
||||
assertEquals(Map.of(), service.storedTags(arn.replace(":" + US_EAST_1 + ":", ":" + EU_WEST_1 + ":")));
|
||||
assertEquals(Map.of(), service.storedTags(arn + "0"));
|
||||
assertEquals(Map.of(), service.storedTags("arn:aws:s3:::tagged"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sameNameInTheSameRegionStillConflicts() {
|
||||
when(domainManager.tryStartDomain(any())).thenReturn(false);
|
||||
|
||||
Reference in new issue
Block a user