mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ssm): keep document and association tags and tag them by resource type (#8132)
CreateDocument and CreateAssociation now keep their Tags, and AddTagsToResource, ListTagsForResource and RemoveTagsFromResource pick a document by name or an association by ID from ResourceType instead of always looking up a parameter. A document's tags are returned in its DocumentDescription and ListDocuments entry. Read the SSM tag calls, CreateDocument and CreateAssociation from the generated request-resource table instead of building their ARNs by hand. A ResourceType enum value now picks its resource type ignoring case and hyphens, ResourceId fills it whatever its kind, and a variable named for leaving out the leading slash drops it, as a parameter ARN does. Closes #7959
This commit is contained in:
1 parent
410ed8e817
commit
236e20003f
12 files changed
+501
-36
No files matched your search
@@ -509,6 +509,7 @@ public final class RequestResources {
|
||||
for (Map.Entry<String, RequestResourceMembers.Member> variable : resource.fill().entrySet()) {
|
||||
List<String> values = read(resource, form, body, variable.getValue()).stream()
|
||||
.map(value -> withoutTypePath(before(value, resource.before()), resource.types().getFirst()))
|
||||
.map(value -> asVariable(variable.getKey(), value))
|
||||
.toList();
|
||||
if (values.isEmpty()) {
|
||||
continue;
|
||||
@@ -555,6 +556,14 @@ public final class RequestResources {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The value as the variable takes it: without the leading {@code /} a variable named for leaving it out drops,
|
||||
* as an SSM parameter's ARN names {@code /team/x} as {@code parameter/team/x}.
|
||||
*/
|
||||
static String asVariable(String variable, String value) {
|
||||
return variable.endsWith("WithoutLeadingSlash") && value.startsWith("/") ? value.substring(1) : value;
|
||||
}
|
||||
|
||||
/** The value without a leading {@code /<type>/}, the path a service may return an id of that type under. */
|
||||
static String withoutTypePath(String value, String type) {
|
||||
String path = "/" + type + "/";
|
||||
|
||||
@@ -657,7 +657,7 @@ public class SsmJsonHandler {
|
||||
String content = requireDocumentContent(request);
|
||||
String documentType = requireDocumentType(request);
|
||||
|
||||
SsmDocument document = ssmService.createDocument(name, content, documentType, region);
|
||||
SsmDocument document = ssmService.createDocument(name, content, documentType, requestTags(request), region);
|
||||
return Response.ok(documentDescriptionResponse(document)).build();
|
||||
}
|
||||
|
||||
@@ -695,6 +695,7 @@ public class SsmJsonHandler {
|
||||
description.put("Status", document.getStatus());
|
||||
description.put("LatestVersion", String.valueOf(document.getDocumentVersion()));
|
||||
description.put("DefaultVersion", String.valueOf(document.getDocumentVersion()));
|
||||
description.set("Tags", tagList(document.getTags()));
|
||||
response.set("DocumentDescription", description);
|
||||
return response;
|
||||
}
|
||||
@@ -794,6 +795,7 @@ public class SsmJsonHandler {
|
||||
if (d.getDocumentFormat() != null) {
|
||||
docNode.put("DocumentFormat", d.getDocumentFormat());
|
||||
}
|
||||
docNode.set("Tags", tagList(d.getTags()));
|
||||
docArray.add(docNode);
|
||||
}
|
||||
response.set("DocumentIdentifiers", docArray);
|
||||
@@ -863,7 +865,7 @@ public class SsmJsonHandler {
|
||||
|
||||
SsmAssociation assoc = ssmService.createAssociation(
|
||||
name, associationName, documentVersion, instanceId, targets, parameters, scheduleExpression,
|
||||
maxErrors, maxConcurrency, complianceSeverity, region);
|
||||
maxErrors, maxConcurrency, complianceSeverity, requestTags(request), region);
|
||||
|
||||
return Response.ok(associationDescriptionResponse(assoc)).build();
|
||||
}
|
||||
@@ -1200,40 +1202,40 @@ public class SsmJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleAddTagsToResource(JsonNode request, String region) {
|
||||
String resourceId = request.path("ResourceId").asText();
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
request.path("Tags").forEach(t ->
|
||||
tags.put(t.path("Key").asText(), t.path("Value").asText()));
|
||||
|
||||
ssmService.addTagsToResource(resourceId, tags, region);
|
||||
ssmService.addTagsToResource(request.path("ResourceType").asText(), request.path("ResourceId").asText(),
|
||||
requestTags(request), region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
private Response handleListTagsForResource(JsonNode request, String region) {
|
||||
String resourceId = request.path("ResourceId").asText();
|
||||
Map<String, String> tags = ssmService.listTagsForResource(resourceId, region);
|
||||
|
||||
Map<String, String> tags = ssmService.listTagsForResource(request.path("ResourceType").asText(),
|
||||
request.path("ResourceId").asText(), region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode tagsArray = objectMapper.createArrayNode();
|
||||
for (Map.Entry<String, String> entry : tags.entrySet()) {
|
||||
ObjectNode tagNode = objectMapper.createObjectNode();
|
||||
tagNode.put("Key", entry.getKey());
|
||||
tagNode.put("Value", entry.getValue());
|
||||
tagsArray.add(tagNode);
|
||||
}
|
||||
response.set("TagList", tagsArray);
|
||||
response.set("TagList", tagList(tags));
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
private Response handleRemoveTagsFromResource(JsonNode request, String region) {
|
||||
String resourceId = request.path("ResourceId").asText();
|
||||
List<String> tagKeys = new ArrayList<>();
|
||||
request.path("TagKeys").forEach(k -> tagKeys.add(k.asText()));
|
||||
|
||||
ssmService.removeTagsFromResource(resourceId, tagKeys, region);
|
||||
ssmService.removeTagsFromResource(request.path("ResourceType").asText(), request.path("ResourceId").asText(),
|
||||
tagKeys, region);
|
||||
return Response.ok(objectMapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
/** A request's {@code Tags} list as a map; empty when it has none. */
|
||||
private static Map<String, String> requestTags(JsonNode request) {
|
||||
Map<String, String> tags = new LinkedHashMap<>();
|
||||
request.path("Tags").forEach(t -> tags.put(t.path("Key").asText(), t.path("Value").asText()));
|
||||
return tags;
|
||||
}
|
||||
|
||||
private ArrayNode tagList(Map<String, String> tags) {
|
||||
ArrayNode list = objectMapper.createArrayNode();
|
||||
tags.forEach((key, value) -> list.addObject().put("Key", key).put("Value", value));
|
||||
return list;
|
||||
}
|
||||
|
||||
private static final int GET_RESOURCE_POLICIES_MAX_RESULTS = 50;
|
||||
private static final String GET_RESOURCE_POLICIES_TOKEN = "GetResourcePolicies:";
|
||||
private static final String POLICY_PATTERN = "^(?!\\s*$).+";
|
||||
@@ -1604,6 +1606,7 @@ public class SsmJsonHandler {
|
||||
if (document.getCreatedDate() != null) {
|
||||
documentNode.put("CreatedDate", EpochSeconds.fromInstant(document.getCreatedDate()));
|
||||
}
|
||||
documentNode.set("Tags", tagList(document.getTags()));
|
||||
response.set("Document", documentNode);
|
||||
return Response.ok(response).build();
|
||||
}
|
||||
|
||||
@@ -46,6 +46,7 @@ import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
@@ -1120,6 +1121,72 @@ public class SsmService implements ResourceProvider {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code AddTagsToResource} on the resource of {@code resourceType}: a document by its name, an
|
||||
* association by its ID, and a parameter, or any type Floci does not tag yet, as before.
|
||||
*/
|
||||
public synchronized void addTagsToResource(String resourceType, String resourceId, Map<String, String> tags,
|
||||
String region) {
|
||||
validateTagKeys(tags);
|
||||
if (!updateTags(resourceType, resourceId, region, current -> current.putAll(tags))) {
|
||||
addTagsToResource(resourceId, tags, region);
|
||||
}
|
||||
}
|
||||
|
||||
/** {@code ListTagsForResource} on the resource of {@code resourceType}. */
|
||||
public Map<String, String> listTagsForResource(String resourceType, String resourceId, String region) {
|
||||
return switch (resourceType) {
|
||||
case "Document" -> taggableDocument(resourceId, region).getTags();
|
||||
case "Association" -> taggableAssociation(resourceId, region).getTags();
|
||||
default -> listTagsForResource(resourceId, region);
|
||||
};
|
||||
}
|
||||
|
||||
/** {@code RemoveTagsFromResource} on the resource of {@code resourceType}; a key it lacks is ignored. */
|
||||
public synchronized void removeTagsFromResource(String resourceType, String resourceId, List<String> tagKeys,
|
||||
String region) {
|
||||
if (!updateTags(resourceType, resourceId, region, current -> tagKeys.forEach(current::remove))) {
|
||||
removeTagsFromResource(resourceId, tagKeys, region);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies {@code change} to the tags of the document or association {@code resourceId} names and stores
|
||||
* it; {@code false}, changing nothing, for any other {@code resourceType}.
|
||||
*/
|
||||
private boolean updateTags(String resourceType, String resourceId, String region,
|
||||
Consumer<Map<String, String>> change) {
|
||||
String storageKey = regionKey(region, resourceId);
|
||||
switch (resourceType) {
|
||||
case "Document" -> {
|
||||
SsmDocument document = taggableDocument(resourceId, region);
|
||||
change.accept(document.getTags());
|
||||
documentStore.put(storageKey, document);
|
||||
}
|
||||
case "Association" -> {
|
||||
SsmAssociation association = taggableAssociation(resourceId, region);
|
||||
change.accept(association.getTags());
|
||||
associationStore.put(storageKey, association);
|
||||
}
|
||||
default -> {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private SsmDocument taggableDocument(String name, String region) {
|
||||
return documentStore.get(regionKey(region, name)).orElseThrow(() -> resourceNotFound(name));
|
||||
}
|
||||
|
||||
private SsmAssociation taggableAssociation(String associationId, String region) {
|
||||
return associationStore.get(regionKey(region, associationId)).orElseThrow(() -> resourceNotFound(associationId));
|
||||
}
|
||||
|
||||
private static AwsException resourceNotFound(String resourceId) {
|
||||
return new AwsException("InvalidResourceId", "Resource " + resourceId + " not found.", 400);
|
||||
}
|
||||
|
||||
public void addTagsToResource(String resourceId, Map<String, String> tags, String region) {
|
||||
validateTagKeys(tags);
|
||||
String storageKey = regionKey(region, parameterTagName(resourceId, region));
|
||||
@@ -1426,6 +1493,12 @@ public class SsmService implements ResourceProvider {
|
||||
}
|
||||
|
||||
public synchronized SsmDocument createDocument(String name, String content, String documentType, String region) {
|
||||
return createDocument(name, content, documentType, Map.of(), region);
|
||||
}
|
||||
|
||||
public synchronized SsmDocument createDocument(String name, String content, String documentType,
|
||||
Map<String, String> tags, String region) {
|
||||
validateTagKeys(tags);
|
||||
String storageKey = regionKey(region, name);
|
||||
if (documentStore.get(storageKey).isPresent()) {
|
||||
throw new AwsException("DocumentAlreadyExists",
|
||||
@@ -1433,6 +1506,7 @@ public class SsmService implements ResourceProvider {
|
||||
}
|
||||
SsmDocument document = new SsmDocument(name, content, documentType);
|
||||
document.setOwner(regionResolver.getAccountId());
|
||||
document.setTags(tags);
|
||||
documentStore.put(storageKey, document);
|
||||
return document;
|
||||
}
|
||||
@@ -1609,6 +1683,24 @@ public class SsmService implements ResourceProvider {
|
||||
String maxConcurrency,
|
||||
String complianceSeverity,
|
||||
String region) {
|
||||
return createAssociation(name, associationName, documentVersion, instanceId, targets, parameters,
|
||||
scheduleExpression, maxErrors, maxConcurrency, complianceSeverity, Map.of(), region);
|
||||
}
|
||||
|
||||
public synchronized SsmAssociation createAssociation(
|
||||
String name,
|
||||
String associationName,
|
||||
String documentVersion,
|
||||
String instanceId,
|
||||
List<SsmAssociation.Target> targets,
|
||||
Map<String, List<String>> parameters,
|
||||
String scheduleExpression,
|
||||
String maxErrors,
|
||||
String maxConcurrency,
|
||||
String complianceSeverity,
|
||||
Map<String, String> tags,
|
||||
String region) {
|
||||
validateTagKeys(tags);
|
||||
SsmDocument document = getDocument(name, region);
|
||||
validateDocumentVersion(document, documentVersion);
|
||||
|
||||
@@ -1644,6 +1736,7 @@ public class SsmService implements ResourceProvider {
|
||||
association.setMaxConcurrency(maxConcurrency);
|
||||
association.setComplianceSeverity(complianceSeverity);
|
||||
association.setAssociationVersion("1");
|
||||
association.setTags(tags);
|
||||
// Real SSM starts an association at "Pending" and transitions it to "Success"/"Failed" once
|
||||
// its execution engine runs it. This emulator has no execution engine, so it reports
|
||||
// "Success" immediately rather than modeling a state that would never change on its own.
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@@ -61,6 +62,9 @@ public class SsmAssociation {
|
||||
@JsonProperty("AssociationVersion")
|
||||
private String associationVersion;
|
||||
|
||||
@JsonProperty("Tags")
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
|
||||
public SsmAssociation() {}
|
||||
|
||||
public SsmAssociation(
|
||||
@@ -224,6 +228,14 @@ public class SsmAssociation {
|
||||
this.associationVersion = associationVersion;
|
||||
}
|
||||
|
||||
public Map<String, String> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags);
|
||||
}
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public static class Target {
|
||||
|
||||
@@ -46,6 +46,9 @@ public class SsmDocument {
|
||||
@JsonProperty("Versions")
|
||||
private Map<String, String> versions = new LinkedHashMap<>();
|
||||
|
||||
@JsonProperty("Tags")
|
||||
private Map<String, String> tags = new LinkedHashMap<>();
|
||||
|
||||
public SsmDocument() {}
|
||||
|
||||
public SsmDocument(String name, String content, String documentType) {
|
||||
@@ -156,4 +159,12 @@ public class SsmDocument {
|
||||
public String getContentForVersion(String version) {
|
||||
return getVersions().get(version);
|
||||
}
|
||||
|
||||
public Map<String, String> getTags() {
|
||||
return tags;
|
||||
}
|
||||
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user