mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(config): check requests against the modeled constraints (#8363)
The Config handler read each member with asText, asInt or path and checked only some of them by hand, so an empty or over-long Arn on the two recorder Describe calls answered NoSuchConfigurationRecorderException, and an empty ResourceArn on the three tag operations was accepted, as was an over-long one that isn't a Config resource's ARN. tools/aws/regen_config_shapes.py now vendors every Config operation's request constraints from botocore, kept in sync by make aws-data-check, and RequestConstraintCheck checks each served operation's request before it runs. A violation is a ValidationException, which the model documents for missing required fields and input that fails validation, and a Limit out of range answers the InvalidLimitException its operation models. The hand-written checks the table covers are gone, putConfigRule's included: ConfigCfnProvisioner now checks the rule it builds as a PutConfigRule request before storing it, so a template property outside the constraints fails the resource. A rule without a ConfigRuleName keeps its InvalidParameterValueException, since the model doesn't require the member. The loop that drops blob and timestamp members moves into request_shapes as drop_blob_and_timestamp_members, used by both the Config and Kinesis generators. Both vendored files are unchanged.
This commit is contained in:
1 parent
08b2bae267
commit
6de963848b
21 files changed
+3506
-286
No files matched your search
+8
-2
@@ -1,9 +1,11 @@
|
||||
package io.floci.aws.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.services.cloudformation.model.StackResource;
|
||||
import io.floci.aws.services.configservice.AwsConfigService;
|
||||
import io.floci.aws.services.configservice.ConfigRequestShapes;
|
||||
import io.floci.aws.services.configservice.model.ConfigRule;
|
||||
import io.floci.aws.services.configservice.model.ConfigRuleSource;
|
||||
import io.floci.aws.services.configservice.model.CustomPolicyDetails;
|
||||
@@ -22,6 +24,7 @@ import java.util.Set;
|
||||
public class ConfigCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(ConfigCfnProvisioner.class);
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final String CONFIG_RULE = "AWS::Config::ConfigRule";
|
||||
|
||||
private final AwsConfigService configService;
|
||||
@@ -49,11 +52,14 @@ public class ConfigCfnProvisioner implements CfnResourceProvisioner {
|
||||
? ctx.generatePhysicalName(resource.getLogicalId(), 128, false)
|
||||
: previousName;
|
||||
}
|
||||
ConfigRule rule = configService.putConfigRule(ctx.region(), new ConfigRule(
|
||||
ConfigRule desired = new ConfigRule(
|
||||
name, null, null, text(resolved, "Description"), scope(resolved.path("Scope")),
|
||||
source(resolved.path("Source")), inputParameters(resolved.path("InputParameters")),
|
||||
text(resolved, "MaximumExecutionFrequency"), null, null,
|
||||
evaluationModes(resolved.path("EvaluationModes"))));
|
||||
evaluationModes(resolved.path("EvaluationModes")));
|
||||
ConfigRequestShapes.check("PutConfigRule",
|
||||
MAPPER.createObjectNode().set("ConfigRule", MAPPER.valueToTree(desired)));
|
||||
ConfigRule rule = configService.putConfigRule(ctx.region(), desired);
|
||||
resource.setPhysicalId(rule.configRuleName());
|
||||
resource.getAttributes().put("Arn", rule.configRuleArn());
|
||||
resource.getAttributes().put("ConfigRuleId", rule.configRuleId());
|
||||
|
||||
@@ -26,7 +26,6 @@ import io.floci.aws.services.configservice.model.EvaluationResult;
|
||||
import io.floci.aws.services.configservice.model.EvaluationResultIdentifier;
|
||||
import io.floci.aws.services.configservice.model.EvaluationResultQualifier;
|
||||
import io.floci.aws.services.configservice.model.RetentionConfiguration;
|
||||
import io.floci.aws.services.configservice.model.SourceDetail;
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
@@ -38,46 +37,20 @@ import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ApplicationScoped
|
||||
public class AwsConfigService {
|
||||
|
||||
private static final Set<String> VALID_COMPLIANCE_TYPES =
|
||||
Set.of("COMPLIANT", "NON_COMPLIANT", "NOT_APPLICABLE", "INSUFFICIENT_DATA");
|
||||
private static final String NO_SUCH_CONFIG_RULE_MESSAGE =
|
||||
"The ConfigRule provided in the request is invalid. Please check the configRule name.";
|
||||
private static final String INVALID_NEXT_TOKEN_MESSAGE =
|
||||
"The specified next token is not valid. Specify the nextToken string that was returned "
|
||||
+ "in the previous response to get the next page of results.";
|
||||
/** Enum shapes from the Config model (config/2014-11-12/service-2.json). */
|
||||
private static final Set<String> VALID_RULE_OWNERS =
|
||||
Set.of("CUSTOM_LAMBDA", "AWS", "CUSTOM_POLICY");
|
||||
private static final Set<String> VALID_EXECUTION_FREQUENCIES =
|
||||
Set.of("One_Hour", "Three_Hours", "Six_Hours", "Twelve_Hours", "TwentyFour_Hours");
|
||||
private static final Set<String> VALID_RULE_STATES =
|
||||
Set.of("ACTIVE", "DELETING", "DELETING_RESULTS", "EVALUATING");
|
||||
private static final Set<String> VALID_EVALUATION_MODES = Set.of("DETECTIVE", "PROACTIVE");
|
||||
private static final Set<String> VALID_EVENT_SOURCES = Set.of("aws.config");
|
||||
private static final Set<String> VALID_MESSAGE_TYPES = Set.of(
|
||||
"ConfigurationItemChangeNotification", "ConfigurationSnapshotDeliveryCompleted",
|
||||
"ScheduledNotification", "OversizedConfigurationItemChangeNotification");
|
||||
/** {@code ComplianceTypes} is a list of ComplianceType with {@code max: 3}. */
|
||||
private static final int MAX_COMPLIANCE_TYPE_FILTERS = 3;
|
||||
/** {@code Scope.ComplianceResourceTypes} is {@code max: 100}. */
|
||||
private static final int MAX_COMPLIANCE_RESOURCE_TYPES = 100;
|
||||
/** {@code DescribeConfigRulesRequest.ConfigRuleNames} is {@code max: 25}. */
|
||||
private static final int MAX_DESCRIBE_CONFIG_RULE_NAMES = 25;
|
||||
/** {@code DescribeAggregationAuthorizationsRequest.Limit} is {@code max: 100}. */
|
||||
private static final int MAX_AGGREGATION_AUTHORIZATIONS_LIMIT = 100;
|
||||
/** {@code AuthorizedAccountId} is modeled with {@code pattern: \d{12}}. */
|
||||
private static final Pattern AUTHORIZED_ACCOUNT_ID_PATTERN = Pattern.compile("\\d{12}");
|
||||
private static final int RULE_CONTRIBUTOR_CAP = 25;
|
||||
private static final int RESOURCE_CONTRIBUTOR_CAP = 100;
|
||||
|
||||
@@ -204,44 +177,11 @@ public class AwsConfigService {
|
||||
* the tags alone, which TagResource/UntagResource change instead. The get-then-put runs under
|
||||
* the lock delete takes, so a delete cannot land between them and orphan the creation tags. */
|
||||
public ConfigRule putConfigRule(String region, ConfigRule requested, List<Map<String, String>> tagList) {
|
||||
if (requested == null || isBlank(requested.configRuleName())) {
|
||||
String ruleName = requested.configRuleName();
|
||||
if (ruleName == null) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"ConfigRuleName must be specified.", 400);
|
||||
}
|
||||
if (requested.source() == null || isBlank(requested.source().owner())) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Source with Owner must be specified.", 400);
|
||||
}
|
||||
// The whole modeled ConfigRule is stored and echoed back, so every enum-typed member
|
||||
// it keeps is checked here rather than accepted verbatim.
|
||||
requireEnum(requested.source().owner(), VALID_RULE_OWNERS, "Source.Owner");
|
||||
requireEnum(requested.maximumExecutionFrequency(), VALID_EXECUTION_FREQUENCIES,
|
||||
"MaximumExecutionFrequency");
|
||||
requireEnum(requested.configRuleState(), VALID_RULE_STATES, "ConfigRuleState");
|
||||
if (requested.source().sourceDetails() != null) {
|
||||
for (SourceDetail detail : requested.source().sourceDetails()) {
|
||||
if (detail == null) {
|
||||
continue;
|
||||
}
|
||||
requireEnum(detail.eventSource(), VALID_EVENT_SOURCES, "SourceDetails.EventSource");
|
||||
requireEnum(detail.messageType(), VALID_MESSAGE_TYPES, "SourceDetails.MessageType");
|
||||
requireEnum(detail.maximumExecutionFrequency(), VALID_EXECUTION_FREQUENCIES,
|
||||
"SourceDetails.MaximumExecutionFrequency");
|
||||
}
|
||||
}
|
||||
if (requested.scope() != null && requested.scope().complianceResourceTypes() != null
|
||||
&& requested.scope().complianceResourceTypes().size() > MAX_COMPLIANCE_RESOURCE_TYPES) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Scope.ComplianceResourceTypes accepts at most "
|
||||
+ MAX_COMPLIANCE_RESOURCE_TYPES + " values.", 400);
|
||||
}
|
||||
if (requested.evaluationModes() != null) {
|
||||
for (EvaluationModeConfiguration mode : requested.evaluationModes()) {
|
||||
requireEnum(mode == null ? null : mode.mode(), VALID_EVALUATION_MODES,
|
||||
"EvaluationModes.Mode");
|
||||
}
|
||||
}
|
||||
String ruleName = requested.configRuleName();
|
||||
synchronized (lockFor(ruleKey(region, ruleName))) {
|
||||
return storeConfigRule(region, requested, ruleName, tagList);
|
||||
}
|
||||
@@ -305,10 +245,6 @@ public class AwsConfigService {
|
||||
}
|
||||
|
||||
public List<ConfigRule> describeConfigRules(String region, List<String> ruleNames) {
|
||||
if (ruleNames != null && ruleNames.size() > MAX_DESCRIBE_CONFIG_RULE_NAMES) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"ConfigRuleNames accepts at most " + MAX_DESCRIBE_CONFIG_RULE_NAMES + " values.", 400);
|
||||
}
|
||||
Map<String, ConfigRule> store = rulesFor(region);
|
||||
List<ConfigRule> result = new ArrayList<>();
|
||||
if (ruleNames == null || ruleNames.isEmpty()) {
|
||||
@@ -327,8 +263,7 @@ public class AwsConfigService {
|
||||
}
|
||||
|
||||
public Paged<ConfigRule> describeConfigRulesPaged(String region, List<String> ruleNames, String nextToken) {
|
||||
return paginate(describeConfigRules(region, ruleNames), null, nextToken,
|
||||
25, 25, "InvalidParameterValueException");
|
||||
return paginate(describeConfigRules(region, ruleNames), null, nextToken, 25);
|
||||
}
|
||||
|
||||
public Paged<ConfigRuleEvaluationStatus> describeConfigRuleEvaluationStatus(String region,
|
||||
@@ -349,7 +284,7 @@ public class AwsConfigService {
|
||||
evaluationStarted, ruleFirstActivatedTime.get(key),
|
||||
lastEvaluationTime, lastEvaluationTime));
|
||||
}
|
||||
return paginate(statuses, limit, nextToken, 150, 150, "InvalidParameterValueException");
|
||||
return paginate(statuses, limit, nextToken, 150);
|
||||
}
|
||||
|
||||
public void startConfigRulesEvaluation(String region, List<String> ruleNames) {
|
||||
@@ -372,10 +307,6 @@ public class AwsConfigService {
|
||||
throw new AwsException("InvalidResultTokenException",
|
||||
"The specified ResultToken is not valid.", 400);
|
||||
}
|
||||
if (newEvaluations.size() > 100) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"The Evaluations list cannot contain more than 100 items.", 400);
|
||||
}
|
||||
newEvaluations.forEach(this::validateEvaluation);
|
||||
if (testMode) {
|
||||
return;
|
||||
@@ -414,11 +345,6 @@ public class AwsConfigService {
|
||||
"Each evaluation must specify ComplianceResourceType, ComplianceResourceId, "
|
||||
+ "ComplianceType and OrderingTimestamp.", 400);
|
||||
}
|
||||
if (!VALID_COMPLIANCE_TYPES.contains(evaluation.complianceType())) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"ComplianceType must be one of COMPLIANT, NON_COMPLIANT, NOT_APPLICABLE, "
|
||||
+ "INSUFFICIENT_DATA.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private void storeEvaluations(String region, String ruleName, List<ConfigEvaluation> newEvaluations) {
|
||||
@@ -470,7 +396,6 @@ public class AwsConfigService {
|
||||
|
||||
public Paged<ComplianceByConfigRule> describeComplianceByConfigRule(String region, List<String> ruleNames,
|
||||
List<String> complianceTypes, String nextToken) {
|
||||
validateComplianceTypeFilter(complianceTypes);
|
||||
List<ComplianceByConfigRule> entries = new ArrayList<>();
|
||||
for (ConfigRule rule : describeConfigRules(region, ruleNames)) {
|
||||
Compliance compliance = complianceForRule(region, rule.configRuleName());
|
||||
@@ -478,12 +403,11 @@ public class AwsConfigService {
|
||||
entries.add(new ComplianceByConfigRule(rule.configRuleName(), compliance));
|
||||
}
|
||||
}
|
||||
return paginate(entries, null, nextToken, 25, 25, "InvalidParameterValueException");
|
||||
return paginate(entries, null, nextToken, 25);
|
||||
}
|
||||
|
||||
public Paged<ComplianceByResource> describeComplianceByResource(String region, String resourceType,
|
||||
String resourceId, List<String> complianceTypes, Integer limit, String nextToken) {
|
||||
validateComplianceTypeFilter(complianceTypes);
|
||||
if (!isBlank(resourceId) && isBlank(resourceType)) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"ResourceType must be specified when ResourceId is provided.", 400);
|
||||
@@ -500,12 +424,11 @@ public class AwsConfigService {
|
||||
}
|
||||
entries.sort(Comparator.comparing(ComplianceByResource::resourceType)
|
||||
.thenComparing(ComplianceByResource::resourceId));
|
||||
return paginate(entries, limit, nextToken, 10, 100, "InvalidParameterValueException");
|
||||
return paginate(entries, limit, nextToken, 10);
|
||||
}
|
||||
|
||||
public Paged<EvaluationResult> getComplianceDetailsByConfigRule(String region, String ruleName,
|
||||
List<String> complianceTypes, Integer limit, String nextToken) {
|
||||
validateComplianceTypeFilter(complianceTypes);
|
||||
requireRule(region, ruleName);
|
||||
List<EvaluationResult> results = evaluationsFor(region)
|
||||
.getOrDefault(ruleName, Map.of()).values().stream()
|
||||
@@ -514,12 +437,11 @@ public class AwsConfigService {
|
||||
.thenComparing(ConfigEvaluation::complianceResourceId))
|
||||
.map(e -> toEvaluationResult(ruleName, e))
|
||||
.collect(Collectors.toList());
|
||||
return paginate(results, limit, nextToken, 10, 100, "InvalidParameterValueException");
|
||||
return paginate(results, limit, nextToken, 10);
|
||||
}
|
||||
|
||||
public Paged<EvaluationResult> getComplianceDetailsByResource(String region, String resourceType,
|
||||
String resourceId, List<String> complianceTypes, String nextToken) {
|
||||
validateComplianceTypeFilter(complianceTypes);
|
||||
if (isBlank(resourceType) || isBlank(resourceId)) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"ResourceType and ResourceId must be specified.", 400);
|
||||
@@ -534,7 +456,7 @@ public class AwsConfigService {
|
||||
});
|
||||
results.sort(Comparator.comparing(r ->
|
||||
r.evaluationResultIdentifier().evaluationResultQualifier().configRuleName()));
|
||||
return paginate(results, null, nextToken, 10, 10, "InvalidParameterValueException");
|
||||
return paginate(results, null, nextToken, 10);
|
||||
}
|
||||
|
||||
public ComplianceSummary getComplianceSummaryByConfigRule(String region) {
|
||||
@@ -554,10 +476,6 @@ public class AwsConfigService {
|
||||
|
||||
public List<ComplianceSummaryByResourceType> getComplianceSummaryByResourceType(String region,
|
||||
List<String> resourceTypes) {
|
||||
if (resourceTypes != null && resourceTypes.size() > 20) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"ResourceTypes cannot contain more than 20 items.", 400);
|
||||
}
|
||||
Map<String, long[]> countsByType = new HashMap<>();
|
||||
for (List<ConfigEvaluation> resourceEvaluations
|
||||
: groupEvaluationsByResource(region, null, null).values()) {
|
||||
@@ -640,36 +558,6 @@ public class AwsConfigService {
|
||||
return complianceTypes == null || complianceTypes.isEmpty() || complianceTypes.contains(complianceType);
|
||||
}
|
||||
|
||||
/**
|
||||
* A {@code ComplianceTypes} filter outside the modeled enum (or longer than the list's
|
||||
* {@code max: 3}) would otherwise match nothing and read to the caller as "no results"
|
||||
* instead of the rejection AWS answers.
|
||||
*/
|
||||
private static void validateComplianceTypeFilter(List<String> complianceTypes) {
|
||||
if (complianceTypes == null || complianceTypes.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (complianceTypes.size() > MAX_COMPLIANCE_TYPE_FILTERS) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"ComplianceTypes accepts at most " + MAX_COMPLIANCE_TYPE_FILTERS + " values.", 400);
|
||||
}
|
||||
for (String complianceType : complianceTypes) {
|
||||
requireEnum(complianceType, VALID_COMPLIANCE_TYPES, "ComplianceTypes");
|
||||
}
|
||||
}
|
||||
|
||||
/** Rejects a non-blank value that is not a member of the shape's enum; a null/blank value is left alone. */
|
||||
private static void requireEnum(String value, Set<String> allowed, String memberName) {
|
||||
if (isBlank(value) || allowed.contains(value)) {
|
||||
return;
|
||||
}
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Value '" + value + "' at '" + memberName + "' failed to satisfy constraint: "
|
||||
+ "Member must satisfy enum value set: ["
|
||||
+ allowed.stream().sorted().collect(Collectors.joining(", ")) + "]",
|
||||
400);
|
||||
}
|
||||
|
||||
private ComplianceContributorCount contributorCount(long count, int cap) {
|
||||
return new ComplianceContributorCount((int) Math.min(count, cap), count > cap);
|
||||
}
|
||||
@@ -866,10 +754,6 @@ public class AwsConfigService {
|
||||
// --- Retention Configuration ---
|
||||
|
||||
public RetentionConfiguration putRetentionConfiguration(String region, Integer retentionPeriodInDays) {
|
||||
if (retentionPeriodInDays == null || retentionPeriodInDays < 30 || retentionPeriodInDays > 2557) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"RetentionPeriodInDays must be between 30 and 2557.", 400);
|
||||
}
|
||||
RetentionConfiguration configuration = new RetentionConfiguration("default", retentionPeriodInDays);
|
||||
retentionConfigurations.put(region, configuration);
|
||||
return configuration;
|
||||
@@ -889,10 +773,6 @@ public class AwsConfigService {
|
||||
}
|
||||
|
||||
public void deleteRetentionConfiguration(String region, String name) {
|
||||
if (isBlank(name)) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"RetentionConfigurationName must be specified.", 400);
|
||||
}
|
||||
RetentionConfiguration configuration = retentionConfigurations.get(region);
|
||||
if (configuration == null || !configuration.name().equals(name)) {
|
||||
throw new AwsException("NoSuchRetentionConfigurationException",
|
||||
@@ -967,8 +847,7 @@ public class AwsConfigService {
|
||||
|
||||
public Paged<ConformancePack> describeConformancePacksPaged(String region, List<String> names,
|
||||
Integer limit, String nextToken) {
|
||||
return paginate(describeConformancePacks(region, names), limit, nextToken,
|
||||
20, 20, "InvalidLimitException");
|
||||
return paginate(describeConformancePacks(region, names), limit, nextToken, 20);
|
||||
}
|
||||
|
||||
public Paged<ConformancePackStatusDetail> describeConformancePackStatus(String region, List<String> names,
|
||||
@@ -982,7 +861,7 @@ public class AwsConfigService {
|
||||
"CREATE_SUCCESSFUL",
|
||||
now()));
|
||||
}
|
||||
return paginate(result, limit, nextToken, 20, 20, "InvalidLimitException");
|
||||
return paginate(result, limit, nextToken, 20);
|
||||
}
|
||||
|
||||
// --- Aggregation Authorizations ---
|
||||
@@ -997,7 +876,7 @@ public class AwsConfigService {
|
||||
* CreationTime and re-apply creation tags over a set TagResource may already have changed. */
|
||||
public AggregationAuthorization putAggregationAuthorization(String region, String authorizedAccountId,
|
||||
String authorizedAwsRegion, List<Map<String, String>> tagList) {
|
||||
requireAuthorizationKey(authorizedAccountId, authorizedAwsRegion);
|
||||
requireAuthorizedAwsRegion(authorizedAwsRegion);
|
||||
synchronized (lockFor(authorizationLockKey(region, authorizedAccountId, authorizedAwsRegion))) {
|
||||
Map<String, AggregationAuthorization> store = authorizationsFor(region);
|
||||
String key = authorizationKey(authorizedAccountId, authorizedAwsRegion);
|
||||
@@ -1024,8 +903,7 @@ public class AwsConfigService {
|
||||
.thenComparing(AggregationAuthorization::authorizedAwsRegion);
|
||||
List<AggregationAuthorization> result = new ArrayList<>(authorizationsFor(region).values());
|
||||
result.sort(byAuthorizedTarget);
|
||||
return paginate(result, limit, nextToken, MAX_AGGREGATION_AUTHORIZATIONS_LIMIT,
|
||||
MAX_AGGREGATION_AUTHORIZATIONS_LIMIT, "InvalidLimitException");
|
||||
return paginate(result, limit, nextToken, 100);
|
||||
}
|
||||
|
||||
/** The Config model declares no "not found" error for this operation, so deleting an
|
||||
@@ -1038,7 +916,7 @@ public class AwsConfigService {
|
||||
* authorization already gone. */
|
||||
public void deleteAggregationAuthorization(String region, String authorizedAccountId,
|
||||
String authorizedAwsRegion) {
|
||||
requireAuthorizationKey(authorizedAccountId, authorizedAwsRegion);
|
||||
requireAuthorizedAwsRegion(authorizedAwsRegion);
|
||||
synchronized (lockFor(authorizationLockKey(region, authorizedAccountId, authorizedAwsRegion))) {
|
||||
Map<String, AggregationAuthorization> store = authorizationsFor(region);
|
||||
AggregationAuthorization removed =
|
||||
@@ -1056,11 +934,7 @@ public class AwsConfigService {
|
||||
"aggregation-authorization/" + authorizedAccountId + "/" + authorizedAwsRegion).toString();
|
||||
}
|
||||
|
||||
private void requireAuthorizationKey(String authorizedAccountId, String authorizedAwsRegion) {
|
||||
if (isBlank(authorizedAccountId) || !AUTHORIZED_ACCOUNT_ID_PATTERN.matcher(authorizedAccountId).matches()) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"AuthorizedAccountId must be a 12 digit AWS account ID.", 400);
|
||||
}
|
||||
private static void requireAuthorizedAwsRegion(String authorizedAwsRegion) {
|
||||
if (isBlank(authorizedAwsRegion)) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"AuthorizedAwsRegion must be specified.", 400);
|
||||
@@ -1177,17 +1051,9 @@ public class AwsConfigService {
|
||||
public record Paged<T>(List<T> items, String nextToken) {}
|
||||
|
||||
/** Offset-as-NextToken pagination over a pre-sorted list. A null or zero limit falls back to
|
||||
* the default page size; limits outside [1, maxLimit] fail with the op-specific error code. */
|
||||
private <T> Paged<T> paginate(List<T> items, Integer limit, String nextToken,
|
||||
int defaultPageSize, int maxLimit, String limitErrorCode) {
|
||||
int pageSize = defaultPageSize;
|
||||
if (limit != null && limit != 0) {
|
||||
if (limit < 0 || limit > maxLimit) {
|
||||
throw new AwsException(limitErrorCode,
|
||||
"Limit must be between 0 and " + maxLimit + ".", 400);
|
||||
}
|
||||
pageSize = limit;
|
||||
}
|
||||
* the default page size; ConfigRequestShapes has already checked the limit's modeled range. */
|
||||
private <T> Paged<T> paginate(List<T> items, Integer limit, String nextToken, int defaultPageSize) {
|
||||
int pageSize = limit == null || limit == 0 ? defaultPageSize : limit;
|
||||
int offset = 0;
|
||||
if (nextToken != null && !nextToken.isEmpty()) {
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package io.floci.aws.services.configservice;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RequestConstraintCheck;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Checks AWS Config requests against the constraints generated by {@code tools/aws/regen_config_shapes.py}.
|
||||
* A violation is a {@code ValidationException}, which the model documents for missing required fields and
|
||||
* input that fails validation. A {@code Limit} out of range answers {@code InvalidLimitException} instead
|
||||
* where the operation models it.
|
||||
*/
|
||||
public final class ConfigRequestShapes {
|
||||
|
||||
private static final Set<String> MODELED_LIMITS = Set.of(
|
||||
"DescribeAggregationAuthorizations.Limit", "DescribeConformancePacks.Limit",
|
||||
"DescribeConformancePackStatus.Limit", "ListTagsForResource.Limit");
|
||||
private static final RequestConstraintCheck.Messages VALIDATION =
|
||||
RequestConstraintCheck.standardMessages("ValidationException");
|
||||
private static final RequestConstraintCheck.Messages INVALID_LIMIT =
|
||||
RequestConstraintCheck.standardMessages("InvalidLimitException");
|
||||
|
||||
private static final RequestConstraintCheck CHECK = RequestConstraintCheck.load(
|
||||
"aws/config-request-shapes.json", "AWS Config request shapes",
|
||||
new RequestConstraintCheck.Messages() {
|
||||
@Override
|
||||
public AwsException type(String path, String type) {
|
||||
return VALIDATION.type(path, type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException bounds(String path, String type, Long min, Long max, Number size) {
|
||||
return (MODELED_LIMITS.contains(path) ? INVALID_LIMIT : VALIDATION).bounds(path, type, min, max, size);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException pattern(String path, String pattern) {
|
||||
return VALIDATION.pattern(path, pattern);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException enumValue(String path, List<String> values) {
|
||||
return VALIDATION.enumValue(path, values);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AwsException required(String path) {
|
||||
return VALIDATION.required(path);
|
||||
}
|
||||
});
|
||||
|
||||
private ConfigRequestShapes() {
|
||||
}
|
||||
|
||||
public static void check(String operation, JsonNode request) {
|
||||
if (CHECK.hasOperation(operation)) {
|
||||
CHECK.check(operation, request);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
@ApplicationScoped
|
||||
public class ConfigServiceJsonHandler {
|
||||
@@ -39,46 +40,48 @@ public class ConfigServiceJsonHandler {
|
||||
}
|
||||
|
||||
public Response handle(String action, JsonNode request, String region) throws Exception {
|
||||
return switch (action) {
|
||||
case "PutConfigRule" -> putConfigRule(request, region);
|
||||
case "DeleteConfigRule" -> deleteConfigRule(request, region);
|
||||
case "DescribeConfigRules" -> describeConfigRules(request, region);
|
||||
case "DescribeComplianceByConfigRule" -> describeComplianceByConfigRule(request, region);
|
||||
case "DescribeComplianceByResource" -> describeComplianceByResource(request, region);
|
||||
case "DescribeConfigRuleEvaluationStatus" -> describeConfigRuleEvaluationStatus(request, region);
|
||||
case "StartConfigRulesEvaluation" -> startConfigRulesEvaluation(request, region);
|
||||
case "PutEvaluations" -> putEvaluations(request, region);
|
||||
case "PutExternalEvaluation" -> putExternalEvaluation(request, region);
|
||||
case "DeleteEvaluationResults" -> deleteEvaluationResults(request, region);
|
||||
case "GetComplianceDetailsByConfigRule" -> getComplianceDetailsByConfigRule(request, region);
|
||||
case "GetComplianceDetailsByResource" -> getComplianceDetailsByResource(request, region);
|
||||
case "GetComplianceSummaryByConfigRule" -> getComplianceSummaryByConfigRule(region);
|
||||
case "GetComplianceSummaryByResourceType" -> getComplianceSummaryByResourceType(request, region);
|
||||
case "PutConformancePack" -> putConformancePack(request, region);
|
||||
case "DeleteConformancePack" -> deleteConformancePack(request, region);
|
||||
case "DescribeConformancePacks" -> describeConformancePacks(request, region);
|
||||
case "DescribeConformancePackStatus" -> describeConformancePackStatus(request, region);
|
||||
case "PutConfigurationRecorder" -> putConfigurationRecorder(request, region);
|
||||
case "DescribeConfigurationRecorders" -> describeConfigurationRecorders(request, region);
|
||||
case "DeleteConfigurationRecorder" -> deleteConfigurationRecorder(request, region);
|
||||
case "StartConfigurationRecorder" -> startConfigurationRecorder(request, region);
|
||||
case "StopConfigurationRecorder" -> stopConfigurationRecorder(request, region);
|
||||
case "DescribeConfigurationRecorderStatus" -> describeConfigurationRecorderStatus(request, region);
|
||||
case "PutDeliveryChannel" -> putDeliveryChannel(request, region);
|
||||
case "DescribeDeliveryChannels" -> describeDeliveryChannels(request, region);
|
||||
case "DeleteDeliveryChannel" -> deleteDeliveryChannel(request, region);
|
||||
case "PutRetentionConfiguration" -> putRetentionConfiguration(request, region);
|
||||
case "DescribeRetentionConfigurations" -> describeRetentionConfigurations(request, region);
|
||||
case "DeleteRetentionConfiguration" -> deleteRetentionConfiguration(request, region);
|
||||
case "PutAggregationAuthorization" -> putAggregationAuthorization(request, region);
|
||||
case "DescribeAggregationAuthorizations" -> describeAggregationAuthorizations(request, region);
|
||||
case "DeleteAggregationAuthorization" -> deleteAggregationAuthorization(request, region);
|
||||
case "TagResource" -> tagResource(request, region);
|
||||
case "UntagResource" -> untagResource(request, region);
|
||||
case "ListTagsForResource" -> listTagsForResource(request, region);
|
||||
Callable<Response> operation = switch (action) {
|
||||
case "PutConfigRule" -> () -> putConfigRule(request, region);
|
||||
case "DeleteConfigRule" -> () -> deleteConfigRule(request, region);
|
||||
case "DescribeConfigRules" -> () -> describeConfigRules(request, region);
|
||||
case "DescribeComplianceByConfigRule" -> () -> describeComplianceByConfigRule(request, region);
|
||||
case "DescribeComplianceByResource" -> () -> describeComplianceByResource(request, region);
|
||||
case "DescribeConfigRuleEvaluationStatus" -> () -> describeConfigRuleEvaluationStatus(request, region);
|
||||
case "StartConfigRulesEvaluation" -> () -> startConfigRulesEvaluation(request, region);
|
||||
case "PutEvaluations" -> () -> putEvaluations(request, region);
|
||||
case "PutExternalEvaluation" -> () -> putExternalEvaluation(request, region);
|
||||
case "DeleteEvaluationResults" -> () -> deleteEvaluationResults(request, region);
|
||||
case "GetComplianceDetailsByConfigRule" -> () -> getComplianceDetailsByConfigRule(request, region);
|
||||
case "GetComplianceDetailsByResource" -> () -> getComplianceDetailsByResource(request, region);
|
||||
case "GetComplianceSummaryByConfigRule" -> () -> getComplianceSummaryByConfigRule(region);
|
||||
case "GetComplianceSummaryByResourceType" -> () -> getComplianceSummaryByResourceType(request, region);
|
||||
case "PutConformancePack" -> () -> putConformancePack(request, region);
|
||||
case "DeleteConformancePack" -> () -> deleteConformancePack(request, region);
|
||||
case "DescribeConformancePacks" -> () -> describeConformancePacks(request, region);
|
||||
case "DescribeConformancePackStatus" -> () -> describeConformancePackStatus(request, region);
|
||||
case "PutConfigurationRecorder" -> () -> putConfigurationRecorder(request, region);
|
||||
case "DescribeConfigurationRecorders" -> () -> describeConfigurationRecorders(request, region);
|
||||
case "DeleteConfigurationRecorder" -> () -> deleteConfigurationRecorder(request, region);
|
||||
case "StartConfigurationRecorder" -> () -> startConfigurationRecorder(request, region);
|
||||
case "StopConfigurationRecorder" -> () -> stopConfigurationRecorder(request, region);
|
||||
case "DescribeConfigurationRecorderStatus" -> () -> describeConfigurationRecorderStatus(request, region);
|
||||
case "PutDeliveryChannel" -> () -> putDeliveryChannel(request, region);
|
||||
case "DescribeDeliveryChannels" -> () -> describeDeliveryChannels(request, region);
|
||||
case "DeleteDeliveryChannel" -> () -> deleteDeliveryChannel(request, region);
|
||||
case "PutRetentionConfiguration" -> () -> putRetentionConfiguration(request, region);
|
||||
case "DescribeRetentionConfigurations" -> () -> describeRetentionConfigurations(request, region);
|
||||
case "DeleteRetentionConfiguration" -> () -> deleteRetentionConfiguration(request, region);
|
||||
case "PutAggregationAuthorization" -> () -> putAggregationAuthorization(request, region);
|
||||
case "DescribeAggregationAuthorizations" -> () -> describeAggregationAuthorizations(request, region);
|
||||
case "DeleteAggregationAuthorization" -> () -> deleteAggregationAuthorization(request, region);
|
||||
case "TagResource" -> () -> tagResource(request, region);
|
||||
case "UntagResource" -> () -> untagResource(request, region);
|
||||
case "ListTagsForResource" -> () -> listTagsForResource(request, region);
|
||||
default -> throw new AwsException(
|
||||
"InvalidAction", "Could not find operation " + action, 400);
|
||||
};
|
||||
ConfigRequestShapes.check(action, request);
|
||||
return operation.call();
|
||||
}
|
||||
|
||||
// --- Config Rules ---
|
||||
@@ -371,28 +374,13 @@ public class ConfigServiceJsonHandler {
|
||||
}
|
||||
|
||||
/** PutConfigRule, PutAggregationAuthorization and PutConfigurationRecorder take a
|
||||
* {@code TagsList} (config/2014-11-12: at most 50 {@code Tag}s, Key 1-128 characters,
|
||||
* optional Value of at most 256). The first two model {@code InvalidParameterValueException}
|
||||
* as their only parameter error; PutConfigurationRecorder models {@code ValidationException}.
|
||||
* Checked before the service stores anything, so a rejected request leaves no resource
|
||||
* behind. An absent Value is optional in the model and is stored as the empty string. */
|
||||
* {@code TagsList}, whose count and Key and Value lengths ConfigRequestShapes has checked.
|
||||
* The model leaves a tag's Key optional, so a tag without one is refused here, with the
|
||||
* operation's modelled parameter error, before the service stores anything. An absent Value
|
||||
* is optional in the model and is stored as the empty string. */
|
||||
private List<Map<String, String>> extractCreationTags(JsonNode req, String errorCode) {
|
||||
JsonNode tags = req.path("Tags");
|
||||
if (tags.isMissingNode() || tags.isNull()) {
|
||||
return List.of();
|
||||
}
|
||||
if (!tags.isArray()) {
|
||||
throw new AwsException(errorCode, "Tags must be a list.", 400);
|
||||
}
|
||||
if (tags.size() > 50) {
|
||||
throw new AwsException(errorCode, "Tags must contain at most 50 tags.", 400);
|
||||
}
|
||||
for (JsonNode tag : tags) {
|
||||
JsonNode key = tag.path("Key");
|
||||
JsonNode value = tag.path("Value");
|
||||
boolean valueOk = value.isMissingNode() || value.isNull()
|
||||
|| (value.isTextual() && value.asText().length() <= 256);
|
||||
if (!key.isTextual() || key.asText().isEmpty() || key.asText().length() > 128 || !valueOk) {
|
||||
for (JsonNode tag : req.path("Tags")) {
|
||||
if (!tag.path("Key").isTextual()) {
|
||||
throw new AwsException(errorCode,
|
||||
"Each tag needs a Key of 1-128 characters and a Value of at most 256 characters.", 400);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user