feat(iam): read AWS Config from the generated tables (#9296)

Move `config` from NOT_COVERED to NAMESPACES in
regen_iam_request_resources.py, so Config requests are checked on the
resources they name instead of `*`. A config rule's, configuration
recorder's and conformance pack's ARN ends in an id Config mints, which a
request naming one by its name does not carry, so ConfigStoredResourceArns
answers the table's lookups with the ARN Config stores it under; one Config
holds none of is checked on its name with the id open. The tag calls are
read from ResourceArn.

Two generic readings are added to the generator, changing Config's
PutAggregationAuthorization and PutConfigurationRecorder rows and no other
operation's: a format naming an account or a Region of its own beside the
ARN's takes them from the request's one account id member and its one
Region member, and a member filling one of the format's variables is no
longer also the ARN's account; and a type the request names but fills no
variable of takes its one name variable from the request's structure named
after the type (ConfigurationRecorder.name).

PutConfigRule now updates the rule its ConfigRuleArn names, else its
ConfigRuleId, else its ConfigRuleName, as the PutConfigRule reference lets
an update name its rule by any of the three, and refuses an ARN or id that
names no rule with InvalidParameterValueException. Finding the rule by name
alone let a request carrying one rule's id and another rule's name be
checked on the first and change the second.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-23 17:28:33 -04:00
1 parent 0dd02aed44
commit 422c78866d
13 files changed
+648 -10

No files matched your search

@@ -0,0 +1,59 @@
package io.floci.aws.services.configservice;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.configservice.model.ConfigRule;
import io.floci.aws.services.configservice.model.ConfigRuleSource;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* PutConfigRule updates the rule its request names: the PutConfigRule reference lets an update name its rule by
* ConfigRuleName, ConfigRuleId or ConfigRuleArn and requires the name only of a new rule.
*/
class AwsConfigServicePutConfigRuleTest {
private static final String REGION = "us-east-1";
private static final ConfigRuleSource SOURCE = new ConfigRuleSource("AWS", "S3_BUCKET_VERSIONING_ENABLED", null,
null);
private final AwsConfigService config = new AwsConfigService(new RegionResolver(REGION, "000000000000"), null);
@Test
void anUpdateNamingItsRuleByIdOrArnUpdatesThatRuleWhichKeepsItsName() {
ConfigRule first = put("first", null, null, "one");
ConfigRule second = put("second", null, null, "two");
ConfigRule updated = put("second", first.configRuleId(), null, "by id");
assertEquals("first", updated.configRuleName());
assertEquals(first.configRuleArn(), updated.configRuleArn());
assertEquals(List.of("by id", "two"), descriptions());
put(null, null, second.configRuleArn(), "by arn");
assertEquals(List.of("by id", "by arn"), descriptions());
}
@Test
void anIdOrArnNamingNoRuleIsRefused() {
put("first", null, null, "one");
assertEquals("InvalidParameterValueException",
assertThrows(AwsException.class, () -> put("first", "config-rule-missing", null, "x")).getErrorCode());
assertEquals("InvalidParameterValueException", assertThrows(AwsException.class,
() -> put("first", null, "arn:aws:config:us-east-1:000000000000:config-rule/config-rule-missing", "x"))
.getErrorCode());
assertEquals(List.of("one"), descriptions());
}
private ConfigRule put(String name, String id, String arn, String description) {
return config.putConfigRule(REGION, new ConfigRule(name, arn, id, description, null, SOURCE, null, null, null,
null, null));
}
private List<String> descriptions() {
return config.describeConfigRules(REGION, null).stream().map(ConfigRule::description).toList();
}
}
@@ -0,0 +1,33 @@
package io.floci.aws.services.configservice;
import io.floci.aws.services.configservice.model.ConfigRule;
import io.floci.aws.services.configservice.model.ConfigRuleSource;
import io.floci.aws.services.configservice.model.ConfigurationRecorder;
import java.util.List;
/**
* An AWS Config holding the resources whose ARNs carry an id it mints: the config rule {@code versioning}, the
* configuration recorder {@code default} and the conformance pack {@code pack}.
*/
public final class ConfigFixtures {
/** The ARNs Config stored the rule, the recorder and the conformance pack under. */
public record Stored(String rule, String recorder, String pack) {
}
private ConfigFixtures() {
}
/** Puts the rule, the recorder and the conformance pack in {@code region} and returns the ARNs they are under. */
public static Stored stored(AwsConfigService config, String region) {
String rule = config.putConfigRule(region, new ConfigRule("versioning", null, null, null, null,
new ConfigRuleSource("AWS", "S3_BUCKET_VERSIONING_ENABLED", null, null), null, null, null, null, null))
.configRuleArn();
config.putConfigurationRecorder(region, new ConfigurationRecorder(null, "default", "role", null, null),
List.of());
String recorder = config.describeConfigurationRecorders(region, null, null).getFirst().arn();
String pack = config.putConformancePack(region, "pack", "s3://bucket/template.yaml", null).conformancePackArn();
return new Stored(rule, recorder, pack);
}
}
@@ -0,0 +1,42 @@
package io.floci.aws.services.configservice;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.services.configservice.model.ConfigurationRecorder;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* What AWS Config gives the request-resources table of its own resources: the ARN a config rule, a configuration
* recorder or a conformance pack of a name is stored under, the id Config minted for it included.
*/
class ConfigStoredResourceArnsTest {
private static final String REGION = "us-east-1";
private final AwsConfigService config = new AwsConfigService(new RegionResolver(REGION, "000000000000"), null);
private final ConfigStoredResourceArns stored = new ConfigStoredResourceArns(config);
@Test
void aNameFindsTheArnItsResourceIsStoredUnderItsMintedIdIncluded() {
ConfigFixtures.Stored arns = ConfigFixtures.stored(config, REGION);
assertEquals(Optional.of(arns.rule()), stored.find("ConfigRule", List.of("versioning"), REGION));
assertEquals(Optional.of(arns.recorder()), stored.find("ConfigurationRecorder", List.of("default"), REGION));
assertEquals(Optional.of(arns.pack()), stored.find("ConformancePack", List.of("pack"), REGION));
}
@Test
void aNameItHoldsNoneOfFindsNothing() {
config.putConfigurationRecorder(REGION, new ConfigurationRecorder(null, "default", "role", null, null),
List.of());
assertEquals(Optional.empty(), stored.find("ConfigRule", List.of("missing"), REGION));
assertEquals(Optional.empty(), stored.find("ConfigurationRecorder", List.of("other"), REGION));
assertEquals(Optional.empty(), stored.find("ConformancePack", List.of("pack"), REGION));
assertEquals(Optional.empty(), stored.find("ConfigurationRecorder", List.of("default"), "eu-west-1"));
assertEquals(Optional.empty(), stored.find("AggregationAuthorization", List.of("default"), REGION));
}
}
@@ -3309,6 +3309,135 @@ class IamRequestResourceEnforcementIntegrationTest {
.formParam("DashboardName", name).formParam("DashboardBody", "{\"widgets\":[]}").post("/").then();
}
/**
* An AWS Config rule is checked on the ARN Config stores it under, which ends in the id Config minted for it and
* not in its name, so a grant on that ARN lets the caller act on the rule a request names by name, and not on
* another.
*/
@Test
void aConfigRuleIsCheckedOnTheArnConfigStoresItUnder() {
String rule = "res-rule-" + suffix();
config("PutConfigRule", "{\"ConfigRule\":{\"ConfigRuleName\":\"" + rule + "\",\"Source\":{\"Owner\":\"AWS\","
+ "\"SourceIdentifier\":\"S3_BUCKET_VERSIONING_ENABLED\"}}}").statusCode(200);
cleanup.register(() -> config("DeleteConfigRule", "{\"ConfigRuleName\":\"" + rule + "\"}"));
String arn = config("DescribeConfigRules", "{\"ConfigRuleNames\":[\"" + rule + "\"]}").statusCode(200)
.extract().jsonPath().getString("ConfigRules[0].ConfigRuleArn");
assertTrue(arn.startsWith("arn:aws:config:" + REGION + ":" + ACCOUNT + ":config-rule/config-rule-"), arn);
User user = user("res-config-rule");
user.policy(allowOn("config:GetComplianceDetailsByConfigRule", arn));
send(user, "config", "StarlingDoveService.GetComplianceDetailsByConfigRule", "1.1",
"{\"ConfigRuleName\":\"" + rule + "\"}").statusCode(200);
refusedJson(send(user, "config", "StarlingDoveService.GetComplianceDetailsByConfigRule", "1.1",
"{\"ConfigRuleName\":\"" + rule + "-other\"}"), "config:GetComplianceDetailsByConfigRule");
}
/**
* A PutConfigRule naming one rule by its id and another by its name updates the rule its id names, as the
* PutConfigRule reference lets an update name its rule by id, so the rule it is checked on is the one it changes:
* a grant on the first rule does not let the caller change the second.
*/
@Test
void aConfigRulePutUpdatesTheRuleItsIdNames() {
String first = configRule("res-rule-a-" + suffix());
String second = configRule("res-rule-b-" + suffix());
JsonNode firstRule = describeConfigRule(first);
User user = user("res-config-put");
user.policy(allowOn("config:PutConfigRule", firstRule.path("ConfigRuleArn").asText()));
send(user, "config", "StarlingDoveService.PutConfigRule", "1.1", "{\"ConfigRule\":{\"ConfigRuleId\":\""
+ firstRule.path("ConfigRuleId").asText() + "\",\"ConfigRuleName\":\"" + second + "\","
+ "\"Description\":\"changed\",\"Source\":{\"Owner\":\"AWS\","
+ "\"SourceIdentifier\":\"S3_BUCKET_VERSIONING_ENABLED\"}}}").statusCode(200);
assertEquals("changed", describeConfigRule(first).path("Description").asText());
assertTrue(!"changed".equals(describeConfigRule(second).path("Description").asText()));
}
/**
* An AWS Config aggregation authorization is checked on the ARN Config gives it, in the caller's account with the
* account and Region it authorizes in its path, so a grant on that ARN lets the caller put it and not another.
*/
@Test
void aConfigAggregationAuthorizationIsCheckedOnTheArnConfigGivesIt() {
String region = "eu-west-" + (1 + (int) (System.nanoTime() % 3));
cleanup.register(() -> config("DeleteAggregationAuthorization",
"{\"AuthorizedAccountId\":\"111122223333\",\"AuthorizedAwsRegion\":\"" + region + "\"}"));
User user = user("res-config-aggregation");
user.policy(allowOn("config:PutAggregationAuthorization", "arn:aws:config:" + REGION + ":" + ACCOUNT
+ ":aggregation-authorization/111122223333/" + region));
String arn = send(user, "config", "StarlingDoveService.PutAggregationAuthorization", "1.1",
"{\"AuthorizedAccountId\":\"111122223333\",\"AuthorizedAwsRegion\":\"" + region + "\"}")
.statusCode(200).extract().jsonPath().getString("AggregationAuthorization.AggregationAuthorizationArn");
assertEquals("arn:aws:config:" + REGION + ":" + ACCOUNT + ":aggregation-authorization/111122223333/" + region,
arn);
refusedJson(send(user, "config", "StarlingDoveService.PutAggregationAuthorization", "1.1",
"{\"AuthorizedAccountId\":\"444455556666\",\"AuthorizedAwsRegion\":\"" + region + "\"}"),
"config:PutAggregationAuthorization");
}
/**
* An AWS Config recorder put is checked on the recorder its ConfigurationRecorder structure names, the ARN Config
* stores it under, so a grant on that ARN lets the caller update it, and a grant on another refuses it.
*/
@Test
void aConfigRecorderPutIsCheckedOnTheRecorderItsStructureNames() {
String role = "arn:aws:iam::" + ACCOUNT + ":role/res-config-role";
String body = "{\"ConfigurationRecorder\":{\"name\":\"default\",\"roleARN\":\"" + role + "\"}}";
config("PutConfigurationRecorder", body).statusCode(200);
cleanup.register(() -> config("DeleteConfigurationRecorder", "{\"ConfigurationRecorderName\":\"default\"}"));
String arn = config("DescribeConfigurationRecorders", "{}").statusCode(200).extract().jsonPath()
.getString("ConfigurationRecorders[0].arn");
User user = user("res-config-recorder");
user.policy(allowOn("config:PutConfigurationRecorder", arn) + "," + allowOn("iam:PassRole", role));
send(user, "config", "StarlingDoveService.PutConfigurationRecorder", "1.1", body).statusCode(200);
user.policy(allowOn("config:PutConfigurationRecorder", arn + "0") + "," + allowOn("iam:PassRole", role));
refusedJson(send(user, "config", "StarlingDoveService.PutConfigurationRecorder", "1.1", body),
"config:PutConfigurationRecorder");
}
/** A config rule of the name, created as the admin and deleted after the test; its name. */
private String configRule(String name) {
config("PutConfigRule", "{\"ConfigRule\":{\"ConfigRuleName\":\"" + name + "\",\"Source\":{\"Owner\":\"AWS\","
+ "\"SourceIdentifier\":\"S3_BUCKET_VERSIONING_ENABLED\"}}}").statusCode(200);
cleanup.register(() -> config("DeleteConfigRule", "{\"ConfigRuleName\":\"" + name + "\"}"));
return name;
}
private static JsonNode describeConfigRule(String name) {
try {
return new ObjectMapper().readTree(config("DescribeConfigRules", "{\"ConfigRuleNames\":[\"" + name
+ "\"]}").statusCode(200).extract().asString()).path("ConfigRules").path(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* An AWS Config conformance pack is checked on the ARN Config stores it under, its name followed by the id Config
* minted for it, so a grant on that ARN lets the caller update the pack, and a pack Config holds none of is checked
* on its name with the id open.
*/
@Test
void aConfigConformancePackIsCheckedOnTheArnConfigStoresItUnder() {
String pack = "res-pack-" + suffix();
String body = "\",\"TemplateS3Uri\":\"s3://templates/pack.yaml\"}";
config("PutConformancePack", "{\"ConformancePackName\":\"" + pack + body).statusCode(200);
cleanup.register(() -> config("DeleteConformancePack", "{\"ConformancePackName\":\"" + pack + "\"}"));
String arn = config("DescribeConformancePacks", "{\"ConformancePackNames\":[\"" + pack + "\"]}")
.statusCode(200).extract().jsonPath().getString("ConformancePackDetails[0].ConformancePackArn");
User user = user("res-config-pack");
user.policy(allowOn("config:PutConformancePack", arn));
send(user, "config", "StarlingDoveService.PutConformancePack", "1.1",
"{\"ConformancePackName\":\"" + pack + body).statusCode(200);
refusedJson(send(user, "config", "StarlingDoveService.PutConformancePack", "1.1",
"{\"ConformancePackName\":\"" + pack + "-other" + body), "config:PutConformancePack");
}
private static ValidatableResponse config(String operation, String body) {
return adminJson("config", "StarlingDoveService." + operation).body(body).post("/").then();
}
private static ValidatableResponse glue(String operation, String body) {
return adminJson("glue", "AWSGlue." + operation).body(body).post("/").then();
}
@@ -16,6 +16,9 @@ import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.services.cloudwatch.logs.CloudWatchLogsDeliveryService;
import io.floci.aws.services.cloudwatch.logs.CloudWatchLogsStoredResourceArns;
import io.floci.aws.services.cloudwatch.logs.model.Delivery;
import io.floci.aws.services.configservice.AwsConfigService;
import io.floci.aws.services.configservice.ConfigFixtures;
import io.floci.aws.services.configservice.ConfigStoredResourceArns;
import io.floci.aws.services.dynamodb.DynamoDbStoredResourceArns;
import io.floci.aws.services.dynamodb.backend.DynamoDbTableAccess;
import io.floci.aws.services.eks.EksService;
@@ -1876,6 +1879,47 @@ class ResourceArnBuilderTest {
builder.buildResources("cloudwatch", "cloudwatch:PutMetricData", ctx, "us-west-2", ACCOUNT, "aws"));
}
// ── AWS Config ──────────────────────────────────────────────────────────────
/**
* A config rule, a configuration recorder and a conformance pack carry an id Config mints in their ARNs, which a
* request naming them by name does not give, so each is checked on the ARN Config stores it under; one Config holds
* none of is checked on its name with that id open, or on every rule for a rule.
*/
@Test
void configChecksARuleARecorderAndAConformancePackOnTheArnsConfigStoresThemUnder() {
AwsConfigService config = new AwsConfigService(new RegionResolver("us-east-1", ACCOUNT), null);
ConfigFixtures.Stored stored = ConfigFixtures.stored(config, "us-east-1");
ResourceArnBuilder reading = reading(new ConfigStoredResourceArns(config));
setJsonBody("{\"ConfigRuleName\":\"versioning\"}");
assertEquals(List.of(stored.rule()), config(reading, "DeleteEvaluationResults"));
setJsonBody("{\"ConfigurationRecorderName\":\"default\"}");
assertEquals(List.of(stored.recorder()), config(reading, "StopConfigurationRecorder"));
setJsonBody("{\"ConformancePackName\":\"pack\"}");
assertEquals(List.of(stored.pack()), config(reading, "DeleteConformancePack"));
setJsonBody("{\"ConfigRuleName\":\"missing\"}");
assertEquals(List.of("arn:aws:config:us-east-1:000000000000:config-rule/*"),
config(reading, "DeleteEvaluationResults"));
setJsonBody("{\"ConformancePackName\":\"other\"}");
assertEquals(List.of("arn:aws:config:us-east-1:000000000000:conformance-pack/other/*"),
config(reading, "DeleteConformancePack"));
// A recorder put names its recorder in its ConfigurationRecorder structure, the stored one when it exists.
setJsonBody("{\"ConfigurationRecorder\":{\"name\":\"default\",\"roleARN\":\"arn:aws:iam::000000000000:role/r\"}}");
assertEquals(List.of(stored.recorder()), config(reading, "PutConfigurationRecorder"));
// An aggregation authorization is the caller's, its path the account and Region it authorizes.
setJsonBody("{\"AuthorizedAccountId\":\"111122223333\",\"AuthorizedAwsRegion\":\"eu-west-1\"}");
assertEquals(List.of("arn:aws:config:us-east-1:000000000000:aggregation-authorization/111122223333/eu-west-1"),
config(reading, "PutAggregationAuthorization"));
}
/** The resources an AWS Config request is checked on, as the table reads {@code operation}. */
private List<String> config(ResourceArnBuilder builder, String operation) {
return builder.buildResources("config", "config:" + operation, ctx, "us-east-1", ACCOUNT, "aws");
}
// ── S3 and S3 Control ───────────────────────────────────────────────────────
/** The resources an S3 or S3 Control request names, read from the generated table on the route it matches. */