mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(config): give the configuration recorder an ARN and keep its tags (#8117)
This commit is contained in:
1 parent
bb689885a3
commit
acdbde836a
8 files changed
+302
-45
No files matched your search
@@ -684,11 +684,31 @@ public class AwsConfigService {
|
||||
|
||||
// --- Configuration Recorder ---
|
||||
|
||||
public void putConfigurationRecorder(String region, ConfigurationRecorder recorder) {
|
||||
/** Put is an upsert, and request tags are applied at creation only, as for config rules: the
|
||||
* PutConfigurationRecorder model says a later Put with different Tags leaves them unchanged.
|
||||
* A Put under another name replaces the recorder, so it gets a new ARN and the old one's tags
|
||||
* go. A recorder saved before recorders had an ARN gets one on its next Put. */
|
||||
public void putConfigurationRecorder(String region, ConfigurationRecorder recorder,
|
||||
List<Map<String, String>> tagList) {
|
||||
String name = (recorder.name() == null || recorder.name().isEmpty()) ? "default" : recorder.name();
|
||||
ConfigurationRecorder stored = new ConfigurationRecorder(name, recorder.roleARN(), recorder.recordingGroup(),
|
||||
recorder.recordingMode());
|
||||
configurationRecorders.put(region, stored);
|
||||
synchronized (lockFor(recorderLockKey(region))) {
|
||||
ConfigurationRecorder existing = configurationRecorders.get(region);
|
||||
boolean created = existing == null || !existing.name().equals(name);
|
||||
String arn = created || existing.arn() == null
|
||||
? AwsArnUtils.Arn.of("config", region, regionResolver.getAccountId(),
|
||||
"configuration-recorder/" + name + "/" + shortId()).toString()
|
||||
: existing.arn();
|
||||
// Tags before the recorder, as storeConfigRule does: a retry after a failed tag write
|
||||
// finds no recorder and creates both.
|
||||
if (created && tagList != null && !tagList.isEmpty()) {
|
||||
applyTags(arn, tagList);
|
||||
}
|
||||
configurationRecorders.put(region, new ConfigurationRecorder(arn, name, recorder.roleARN(),
|
||||
recorder.recordingGroup(), recorder.recordingMode()));
|
||||
if (created && existing != null) {
|
||||
tags.remove(existing.arn());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public List<ConfigurationRecorder> describeConfigurationRecorders(String region, List<String> names) {
|
||||
@@ -712,15 +732,22 @@ public class AwsConfigService {
|
||||
}
|
||||
|
||||
public void deleteConfigurationRecorder(String region, String name) {
|
||||
ConfigurationRecorder recorder = configurationRecorders.get(region);
|
||||
if (recorder == null || !recorder.name().equals(name)) {
|
||||
throw new AwsException("NoSuchConfigurationRecorderException",
|
||||
"Cannot find configuration recorder with the specified name.", 400);
|
||||
synchronized (lockFor(recorderLockKey(region))) {
|
||||
ConfigurationRecorder recorder = configurationRecorders.get(region);
|
||||
if (recorder == null || !recorder.name().equals(name)) {
|
||||
throw new AwsException("NoSuchConfigurationRecorderException",
|
||||
"Cannot find configuration recorder with the specified name.", 400);
|
||||
}
|
||||
configurationRecorders.remove(region);
|
||||
tags.remove(recorder.arn());
|
||||
recorderRunning.remove(runtimeKey(region));
|
||||
recorderLastStartTime.remove(runtimeKey(region));
|
||||
recorderLastStopTime.remove(runtimeKey(region));
|
||||
}
|
||||
configurationRecorders.remove(region);
|
||||
recorderRunning.remove(runtimeKey(region));
|
||||
recorderLastStartTime.remove(runtimeKey(region));
|
||||
recorderLastStopTime.remove(runtimeKey(region));
|
||||
}
|
||||
|
||||
private String recorderLockKey(String region) {
|
||||
return "configuration-recorder|" + runtimeKey(region);
|
||||
}
|
||||
|
||||
public void startConfigurationRecorder(String region, String name) {
|
||||
@@ -1023,12 +1050,13 @@ public class AwsConfigService {
|
||||
// --- Tagging ---
|
||||
|
||||
/** TagResource, UntagResource and ListTagsForResource each model ResourceNotFoundException.
|
||||
* For the kinds Floci stores (config rules, conformance packs, aggregation authorizations)
|
||||
* the ARN must name a resource that exists in the caller's account and the request Region,
|
||||
* so a deleted resource, or one in another account, Region or partition, is not found. The
|
||||
* check and the tag write run under the lock that resource's delete takes, so a delete cannot
|
||||
* land between them and leave an orphan tag entry for a resource that is gone. ARNs of kinds
|
||||
* Floci does not model are passed through unchecked, as before. */
|
||||
* For the kinds Floci stores (config rules, conformance packs, aggregation authorizations,
|
||||
* configuration recorders) the ARN must name a resource that exists in the caller's account
|
||||
* and the request Region, so a deleted resource, or one in another account, Region or
|
||||
* partition, is not found. The check and the tag write run under the lock that resource's
|
||||
* delete takes, so a delete cannot land between them and leave an orphan tag entry for a
|
||||
* resource that is gone. ARNs of kinds Floci does not model are passed through unchecked,
|
||||
* as before. */
|
||||
public void tagResource(String region, String arn, List<Map<String, String>> tagList) {
|
||||
onTaggableResource(region, arn, () -> {
|
||||
applyTags(arn, tagList);
|
||||
@@ -1104,6 +1132,13 @@ public class AwsConfigService {
|
||||
return rule != null && arn.equals(rule.configRuleArn());
|
||||
};
|
||||
}
|
||||
case "configuration-recorder" -> {
|
||||
lockKey = recorderLockKey(region);
|
||||
exists = () -> {
|
||||
ConfigurationRecorder recorder = configurationRecorders.get(region);
|
||||
return recorder != null && arn.equals(recorder.arn());
|
||||
};
|
||||
}
|
||||
default -> {
|
||||
return action.get();
|
||||
}
|
||||
|
||||
@@ -86,7 +86,7 @@ public class ConfigServiceJsonHandler {
|
||||
private Response putConfigRule(JsonNode req, String region) throws Exception {
|
||||
JsonNode ruleNode = req.path("ConfigRule");
|
||||
ConfigRule rule = ruleNode.isObject() ? mapper.treeToValue(ruleNode, ConfigRule.class) : null;
|
||||
service.putConfigRule(region, rule, extractCreationTags(req));
|
||||
service.putConfigRule(region, rule, extractCreationTags(req, "InvalidParameterValueException"));
|
||||
return Response.ok(mapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@@ -196,7 +196,7 @@ public class ConfigServiceJsonHandler {
|
||||
|
||||
private Response putConfigurationRecorder(JsonNode req, String region) throws Exception {
|
||||
ConfigurationRecorder recorder = mapper.treeToValue(req.path("ConfigurationRecorder"), ConfigurationRecorder.class);
|
||||
service.putConfigurationRecorder(region, recorder);
|
||||
service.putConfigurationRecorder(region, recorder, extractCreationTags(req, "ValidationException"));
|
||||
return Response.ok(mapper.createObjectNode()).build();
|
||||
}
|
||||
|
||||
@@ -315,7 +315,7 @@ public class ConfigServiceJsonHandler {
|
||||
AggregationAuthorization authorization = service.putAggregationAuthorization(region,
|
||||
req.path("AuthorizedAccountId").asText(null),
|
||||
req.path("AuthorizedAwsRegion").asText(null),
|
||||
extractCreationTags(req));
|
||||
extractCreationTags(req, "InvalidParameterValueException"));
|
||||
ObjectNode resp = mapper.createObjectNode();
|
||||
resp.set("AggregationAuthorization", mapper.valueToTree(authorization));
|
||||
return Response.ok(resp).build();
|
||||
@@ -368,22 +368,22 @@ public class ConfigServiceJsonHandler {
|
||||
return tagList;
|
||||
}
|
||||
|
||||
/** PutConfigRule and PutAggregationAuthorization take a {@code TagsList} (config/2014-11-12:
|
||||
* at most 50 {@code Tag}s, Key 1-128 characters, optional Value of at most 256) and model
|
||||
* {@code InvalidParameterValueException} as their only parameter error. 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. */
|
||||
private List<Map<String, String>> extractCreationTags(JsonNode req) {
|
||||
/** 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. */
|
||||
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("InvalidParameterValueException", "Tags must be a list.", 400);
|
||||
throw new AwsException(errorCode, "Tags must be a list.", 400);
|
||||
}
|
||||
if (tags.size() > 50) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
"Tags must contain at most 50 tags.", 400);
|
||||
throw new AwsException(errorCode, "Tags must contain at most 50 tags.", 400);
|
||||
}
|
||||
for (JsonNode tag : tags) {
|
||||
JsonNode key = tag.path("Key");
|
||||
@@ -391,7 +391,7 @@ public class ConfigServiceJsonHandler {
|
||||
boolean valueOk = value.isMissingNode() || value.isNull()
|
||||
|| (value.isTextual() && value.asText().length() <= 256);
|
||||
if (!key.isTextual() || key.asText().isEmpty() || key.asText().length() > 128 || !valueOk) {
|
||||
throw new AwsException("InvalidParameterValueException",
|
||||
throw new AwsException(errorCode,
|
||||
"Each tag needs a Key of 1-128 characters and a Value of at most 256 characters.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public record ConfigurationRecorder(
|
||||
@JsonProperty("arn") String arn,
|
||||
@JsonProperty("name") String name,
|
||||
@JsonProperty("roleARN") String roleARN,
|
||||
@JsonProperty("recordingGroup") RecordingGroup recordingGroup,
|
||||
|
||||
Reference in new issue
Block a user