mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ssm): generate the IAM request resources for SSM (#8203)
* feat(ssm): generate the IAM request resources for SSM * test(ssm): enforce IAM on parameter reads, deletes and tagging * refactor(ssm): derive the SSM request resources without corrections * fix(iam): read a Parameter Store name as its parameter's ARN holds it * fix(ssm): refuse tagging a resource type other than a parameter * test(ssm): read an unslashed parameter by its ARN under a grant on it * test(iam): expect ssm parameter in the vendored unqualified types * fix(ssm): keep tagging documents and associations * refactor(iam): drop the SSM operations read in the hand-built namespaces * chore(iam): resync Route 53 record set protection resource types
This commit is contained in:
1 parent
68a5f2a7f0
commit
39c29de635
14 files changed
+1021
-93
No files matched your search
@@ -44,6 +44,7 @@ public final class RequestResources {
|
||||
|
||||
private static final Pattern VARIABLE = Pattern.compile("\\$\\{(\\w+)}");
|
||||
private static final Pattern ACCOUNT_ID = Pattern.compile("\\d{12}");
|
||||
private static final String WITHOUT_LEADING_SLASH = "WithoutLeadingSlash";
|
||||
|
||||
/** Values for a type's variables, and the ARN whose account and Region the resource they name is in, if any. */
|
||||
private record Filling(Map<String, String> variables, AwsArnUtils.Arn from) {
|
||||
@@ -490,7 +491,9 @@ public final class RequestResources {
|
||||
* structure gives, the request's for a name (a {@code PutEvents} entry, on the bus it names). Each value is read
|
||||
* up to the resource's {@code before} character, when it has one (a CodeBuild build id's project), and without the
|
||||
* type's own path when it begins with one, as Route 53 returns its ids ({@code /hostedzone/Z1} names hosted zone
|
||||
* {@code Z1}). A type
|
||||
* {@code Z1}). A value filling a {@code ...WithoutLeadingSlash} variable is a Parameter Store name instead, read
|
||||
* as {@link #asVariable} reads it. A value rendered past a type's format, when its service checks the
|
||||
* type without a qualifier, is cut where the format ends, as an ARN given is. A type
|
||||
* whose service mints an id after its name is the caller's resource {@code stored} finds under that name, or
|
||||
* else has that id open.
|
||||
*/
|
||||
@@ -508,8 +511,10 @@ public final class RequestResources {
|
||||
boolean given = false;
|
||||
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))
|
||||
.map(value -> before(value, resource.before()))
|
||||
.map(value -> variable.getKey().endsWith(WITHOUT_LEADING_SLASH)
|
||||
? asVariable(variable.getKey(), value)
|
||||
: withoutTypePath(value, resource.types().getFirst()))
|
||||
.toList();
|
||||
if (values.isEmpty()) {
|
||||
continue;
|
||||
@@ -543,13 +548,14 @@ public final class RequestResources {
|
||||
for (Filling filling : fillings) {
|
||||
AwsArnUtils.Arn from = filling.from();
|
||||
if (from == null) {
|
||||
arns.add(withId(render(format.format(), filling.variables(), partition, region, account), format,
|
||||
arns.add(withId(format.checked(render(format.format(), filling.variables(), partition, region,
|
||||
account)), format,
|
||||
resource.types().getFirst(), filling.variables(), account.equals(accountId) ? stored : null,
|
||||
region));
|
||||
} else {
|
||||
addNamed(arns, withId(render(format.format(), filling.variables(), from.partition(),
|
||||
addNamed(arns, withId(format.checked(render(format.format(), filling.variables(), from.partition(),
|
||||
from.region().isEmpty() ? region : from.region(),
|
||||
from.accountId().isEmpty() ? accountId : from.accountId()), format,
|
||||
from.accountId().isEmpty() ? accountId : from.accountId())), format,
|
||||
resource.types().getFirst(), filling.variables(), null, region), format.awsOwned(), partition,
|
||||
region, accountId, null);
|
||||
}
|
||||
@@ -557,11 +563,17 @@ 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}.
|
||||
* The value as the variable takes it: a variable named for leaving out the leading {@code /} takes a Parameter
|
||||
* Store name without the whitespace around it, which a put drops, and without one leading {@code /}, as an SSM
|
||||
* parameter's ARN names {@code /team/x} as {@code parameter/team/x}. A name of two leading slashes keeps the
|
||||
* second, and a name beginning {@code /parameter/} keeps that, being no type path.
|
||||
*/
|
||||
static String asVariable(String variable, String value) {
|
||||
return variable.endsWith("WithoutLeadingSlash") && value.startsWith("/") ? value.substring(1) : value;
|
||||
if (!variable.endsWith(WITHOUT_LEADING_SLASH)) {
|
||||
return value;
|
||||
}
|
||||
String name = value.strip();
|
||||
return name.startsWith("/") ? name.substring(1) : name;
|
||||
}
|
||||
|
||||
/** The value without a leading {@code /<type>/}, the path a service may return an id of that type under. */
|
||||
|
||||
@@ -89,10 +89,6 @@ public class ResourceArnBuilder {
|
||||
/** The route of Bedrock's resource-policy actions, which the ARN of the resource they act on may follow. */
|
||||
private static final String BEDROCK_RESOURCE_POLICY_ROUTE = "/resource-policy";
|
||||
|
||||
/** The SSM actions whose {@code ResourceArn} names the parameter they act on, in place of {@code Name}. */
|
||||
private static final Set<String> SSM_RESOURCE_POLICY_ACTIONS =
|
||||
Set.of("PutResourcePolicy", "GetResourcePolicies", "DeleteResourcePolicy");
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
/**
|
||||
* Looked up lazily through a provider: this builder is constructed by the enforcement filter,
|
||||
@@ -232,7 +228,6 @@ public class ResourceArnBuilder {
|
||||
case "sns" -> List.of(buildSnsArn(ctx, region, accountId));
|
||||
case "dynamodb" -> buildDynamoDbArns(ctx, region, accountId);
|
||||
case "kinesis" -> List.of(buildKinesisArn(ctx, region, accountId));
|
||||
case "ssm" -> List.of(buildSsmArn(ctx, region, accountId));
|
||||
case "iam" -> buildIamArns(ctx, region, accountId);
|
||||
case "resource-groups" -> List.of(buildResourceGroupArn(ctx, region, accountId));
|
||||
case "scheduler" -> List.of(buildSchedulerArn(path, ctx, region, accountId, partition));
|
||||
@@ -1890,32 +1885,6 @@ public class ResourceArnBuilder {
|
||||
return new AwsArnUtils.Arn(partition, "secretsmanager", region, accountId, "secret:" + name).toString();
|
||||
}
|
||||
|
||||
// ── SSM ──────────────────────────────────────────────────────────────────────
|
||||
private String buildSsmArn(ContainerRequestContext ctx, String region, String accountId) {
|
||||
JsonNode json = readJsonBody(ctx);
|
||||
if (json != null && json.isObject()) {
|
||||
String operation = IamActionRegistry.targetOperation(ctx);
|
||||
if (operation != null && SSM_RESOURCE_POLICY_ACTIONS.contains(operation)
|
||||
&& json.hasNonNull("ResourceArn")) {
|
||||
String resourceArn = json.get("ResourceArn").asText().trim();
|
||||
if (AwsArnUtils.isArnFor(resourceArn, "ssm")) {
|
||||
return resourceArn;
|
||||
}
|
||||
}
|
||||
if (json.hasNonNull("Name")) {
|
||||
String name = json.get("Name").asText().trim();
|
||||
if (!name.isEmpty()) {
|
||||
if (AwsArnUtils.isArnFor(name, "ssm")) {
|
||||
return name;
|
||||
}
|
||||
String paramResource = name.startsWith("/") ? "parameter" + name : "parameter/" + name;
|
||||
return AwsArnUtils.Arn.of("ssm", region, accountId, paramResource).toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
return AwsArnUtils.Arn.of("ssm", region, accountId, "parameter/*").toString();
|
||||
}
|
||||
|
||||
// ── KMS ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
/**
|
||||
|
||||
@@ -236,6 +236,9 @@ public class SsmJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleGetParametersByPath(JsonNode request, String region) {
|
||||
if (!request.path("Path").isTextual()) {
|
||||
throw SmithyValidationErrors.validationException(SmithyValidationErrors.notNull("path"));
|
||||
}
|
||||
PageRequest pageRequest = pageRequest(request, GET_PARAMETERS_BY_PATH_MAX_RESULTS);
|
||||
String path = request.path("Path").asText();
|
||||
boolean recursive = request.path("Recursive").asBoolean(false);
|
||||
@@ -1202,12 +1205,14 @@ public class SsmJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleAddTagsToResource(JsonNode request, String region) {
|
||||
requireTaggedResourceType(request);
|
||||
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) {
|
||||
requireTaggedResourceType(request);
|
||||
Map<String, String> tags = ssmService.listTagsForResource(request.path("ResourceType").asText(),
|
||||
request.path("ResourceId").asText(), region);
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
@@ -1216,6 +1221,7 @@ public class SsmJsonHandler {
|
||||
}
|
||||
|
||||
private Response handleRemoveTagsFromResource(JsonNode request, String region) {
|
||||
requireTaggedResourceType(request);
|
||||
List<String> tagKeys = new ArrayList<>();
|
||||
request.path("TagKeys").forEach(k -> tagKeys.add(k.asText()));
|
||||
ssmService.removeTagsFromResource(request.path("ResourceType").asText(), request.path("ResourceId").asText(),
|
||||
@@ -1236,6 +1242,29 @@ public class SsmJsonHandler {
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refuses a tagging request whose {@code ResourceType} is missing, outside the model's enum, or a type whose tags
|
||||
* this service does not keep, so the request acts on the type IAM checked.
|
||||
*/
|
||||
private static void requireTaggedResourceType(JsonNode request) {
|
||||
JsonNode resourceType = request.path("ResourceType");
|
||||
if (!resourceType.isTextual()) {
|
||||
throw SmithyValidationErrors.validationException(SmithyValidationErrors.notNull("resourceType"));
|
||||
}
|
||||
if (!RESOURCE_TYPES_FOR_TAGGING.contains(resourceType.asText())) {
|
||||
throw SmithyValidationErrors.validationException(SmithyValidationErrors.notInEnum(resourceType.asText(),
|
||||
"resourceType", RESOURCE_TYPES_FOR_TAGGING));
|
||||
}
|
||||
if (!TAGGED_RESOURCE_TYPES.contains(resourceType.asText())) {
|
||||
throw new AwsException("InvalidResourceType", "The resource type isn't valid.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static final List<String> RESOURCE_TYPES_FOR_TAGGING = List.of("Document", "ManagedInstance",
|
||||
"MaintenanceWindow", "Parameter", "PatchBaseline", "OpsItem", "OpsMetadata", "Automation", "Association",
|
||||
"CloudConnector");
|
||||
private static final Set<String> TAGGED_RESOURCE_TYPES = Set.of("Parameter", "Document", "Association");
|
||||
|
||||
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*$).+";
|
||||
|
||||
@@ -15,6 +15,7 @@ import io.floci.aws.core.common.DeadlineCharSequence;
|
||||
import io.floci.aws.core.common.Digests;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.resource.ExplorerResource;
|
||||
import io.floci.aws.core.resource.ResourceProvider;
|
||||
import io.floci.aws.core.resource.SupportedResourceType;
|
||||
@@ -51,7 +52,7 @@ import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
@ApplicationScoped
|
||||
public class SsmService implements ResourceProvider {
|
||||
public class SsmService implements ResourceProvider, StoredResourceArns {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(SsmService.class);
|
||||
|
||||
@@ -1123,7 +1124,7 @@ public class SsmService implements ResourceProvider {
|
||||
|
||||
/**
|
||||
* {@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.
|
||||
* association by its ID, and a parameter by its name or ARN.
|
||||
*/
|
||||
public synchronized void addTagsToResource(String resourceType, String resourceId, Map<String, String> tags,
|
||||
String region) {
|
||||
@@ -2023,4 +2024,25 @@ public class SsmService implements ResourceProvider {
|
||||
public Set<SupportedResourceType> getSupportedResourceTypes() {
|
||||
return Set.of(new SupportedResourceType("ssm:parameter", "ssm", true));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String namespace() {
|
||||
return "ssm";
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@code parameter/<name>} ARN a legacy {@code parameter<name>} ARN of the caller's partition, Region and
|
||||
* account names, which the tagging operations act on as {@link #parameterTagName} reads it; empty for any other
|
||||
* value, whose ARN is the one its name renders.
|
||||
*/
|
||||
@Override
|
||||
public Optional<String> find(String type, List<String> names, String region) {
|
||||
if (!"parameter".equals(type) || names.size() != 1) {
|
||||
return Optional.empty();
|
||||
}
|
||||
String name = names.getFirst();
|
||||
String arn = withLegacyParameterSlash(name, regionResolver.partitionForRegion(region), region,
|
||||
regionResolver.getAccountId());
|
||||
return arn == null || arn.equals(name) ? Optional.empty() : Optional.of(arn);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user