mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix: mint ARNs in the request region for Amazon MQ, MSK, EKS Fargate, AppSync and ElastiCache users (#5053)
* fix: mint ARNs in the request region for Amazon MQ, MSK, EKS Fargate, AppSync and ElastiCache users Several create paths minted the new resource's ARN with the emulator's default region instead of the region the request was signed for, so a resource created in eu-west-1 reported a us-east-1 ARN: - Amazon MQ CreateBroker (broker ARN) - MSK CreateConfiguration (configuration ARN) - EKS CreateFargateProfile, which now takes the cluster's own region - AppSync CreateDomainName (ARN and the appsync-api host) and CreateChannelNamespace - ElastiCache user ARNs in CreateUser/DescribeUsers responses, now rendered in the request region like the other user responses RequestRegionArnIntegrationTest signs each request for eu-west-1 and checks the ARN's region; all five cases fail without this change. * fix: keep ElastiCache user and AppSync channel namespace ARNs in their resource's region ElastiCache users are not stored per region, so DescribeUsers signed for another region can read a user; rendering its ARN from each request's region reported the same user under different ARNs. The ARN is now minted once at CreateUser and stored on the user. Users persisted before keep the default-region ARN they were always reported with. A channel namespace now takes its region from its parent API's ARN rather than from the request, since an API is still reachable by id from another region.
This commit is contained in:
1 parent
d400ea6b7e
commit
1b138812fa
8 files changed
+220
-9
No files matched your search
@@ -120,7 +120,7 @@ public class AmazonMqService implements ResourceProvider {
|
||||
|
||||
String brokerId = "b-" + UUID.randomUUID();
|
||||
String accountId = regionResolver.getAccountId();
|
||||
String brokerArn = AwsArnUtils.Arn.of("mq", config.defaultRegion(), accountId,
|
||||
String brokerArn = AwsArnUtils.Arn.of("mq", regionResolver.getRegion(), accountId,
|
||||
"broker:" + name + ":" + brokerId).toString();
|
||||
String engineVersion = (params.engineVersion() == null || params.engineVersion().isBlank())
|
||||
? DEFAULT_ENGINE_VERSION : params.engineVersion();
|
||||
|
||||
@@ -736,9 +736,9 @@ public class AppSyncService {
|
||||
dn.setDescription((String) request.get("description"));
|
||||
dn.setCertificateArn((String) request.get("certificateArn"));
|
||||
String shortId = generateShortId();
|
||||
dn.setAppsyncDomainName(shortId + "." + AwsEndpoints.host("appsync-api", regionResolver.getDefaultRegion()));
|
||||
dn.setAppsyncDomainName(shortId + "." + AwsEndpoints.host("appsync-api", regionResolver.getRegion()));
|
||||
dn.setHostedZoneId("Z" + generateShortId());
|
||||
dn.setDomainNameArn(regionResolver.buildArn("appsync", regionResolver.getDefaultRegion(),
|
||||
dn.setDomainNameArn(regionResolver.buildArn("appsync", regionResolver.getRegion(),
|
||||
"domainnames/" + domainName));
|
||||
|
||||
Object tagsObj = request.get("tags");
|
||||
@@ -814,7 +814,7 @@ public class AppSyncService {
|
||||
|
||||
public ChannelNamespace createChannelNamespace(String apiId, Map<String, Object> request) {
|
||||
assertSchemaNotBusy(apiId);
|
||||
getGraphqlApi(apiId);
|
||||
GraphqlApi api = getGraphqlApi(apiId);
|
||||
String name = (String) request.get("name");
|
||||
if (name == null || name.isBlank()) {
|
||||
throw new AwsException("BadRequestException", "A channel namespace name is required", 400);
|
||||
@@ -828,7 +828,7 @@ public class AppSyncService {
|
||||
ns.setName(name);
|
||||
ns.setApiId(apiId);
|
||||
ns.setDescription((String) request.get("description"));
|
||||
ns.setChannelNamespaceArn(regionResolver.buildArn("appsync", regionResolver.getDefaultRegion(),
|
||||
ns.setChannelNamespaceArn(regionResolver.buildArn("appsync", apiRegion(api),
|
||||
"apis/" + apiId + "/channelNamespaces/" + name));
|
||||
ns.setCodeHandlers((String) request.get("codeHandlers"));
|
||||
ns.setCreated(System.currentTimeMillis());
|
||||
@@ -1153,6 +1153,14 @@ public class AppSyncService {
|
||||
return apiId + "::" + typeName + "::" + fieldName;
|
||||
}
|
||||
|
||||
/** The region of an API's ARN, where its child resources live; the request region if it has none. */
|
||||
private String apiRegion(GraphqlApi api) {
|
||||
if (api.getArn() != null && AwsArnUtils.isArn(api.getArn())) {
|
||||
return AwsArnUtils.parse(api.getArn()).region();
|
||||
}
|
||||
return regionResolver.getRegion();
|
||||
}
|
||||
|
||||
private String buildApiArn(String apiId, String region) {
|
||||
return regionResolver.buildArn("appsync", region, "apis/" + apiId);
|
||||
}
|
||||
|
||||
@@ -961,7 +961,7 @@ public class EksService implements TagHandler, ResourceProvider {
|
||||
}
|
||||
|
||||
public FargateProfile createFargateProfile(String clusterName, CreateFargateProfileRequest request) {
|
||||
describeCluster(clusterName);
|
||||
Cluster cluster = describeCluster(clusterName);
|
||||
|
||||
String fargateProfileName = request.getFargateProfileName();
|
||||
if (fargateProfileName == null || fargateProfileName.isBlank()) {
|
||||
@@ -977,7 +977,7 @@ public class EksService implements TagHandler, ResourceProvider {
|
||||
"Fargate profile already exists: " + fargateProfileName, 409);
|
||||
}
|
||||
|
||||
String region = config.defaultRegion();
|
||||
String region = resolveClusterRegion(cluster);
|
||||
String accountId = regionResolver.getAccountId();
|
||||
String id = UUID.randomUUID().toString();
|
||||
String arn = AwsArnUtils.Arn.of("eks", region, accountId,
|
||||
|
||||
+13
-1
@@ -1049,10 +1049,22 @@ private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String>
|
||||
// MinimumEngineVersion: the only value AWS documents; no valkey-specific one is published.
|
||||
.elem("MinimumEngineVersion", "6.0")
|
||||
.start("UserGroupIds").end("UserGroupIds")
|
||||
.elem("ARN", AwsArnUtils.Arn.of("elasticache", regionResolver.getDefaultRegion(), regionResolver.getAccountId(), "user:" + u.getUserId()).toString())
|
||||
.elem("ARN", userArn(u))
|
||||
.build();
|
||||
}
|
||||
|
||||
/**
|
||||
* The ARN stored at CreateUser. Users persisted before it was stored keep the default-region ARN
|
||||
* they were always reported with, so their identity stays stable.
|
||||
*/
|
||||
private String userArn(ElastiCacheUser u) {
|
||||
if (u.getArn() != null) {
|
||||
return u.getArn();
|
||||
}
|
||||
return AwsArnUtils.Arn.of("elasticache", regionResolver.getDefaultRegion(),
|
||||
regionResolver.getAccountId(), "user:" + u.getUserId()).toString();
|
||||
}
|
||||
|
||||
private record UserAuthentication(AuthMode mode, List<String> passwords) {}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1538,6 +1538,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
passwords != null ? passwords : List.of(),
|
||||
accessString != null ? accessString : "on ~* +@all",
|
||||
normalizedEngine, "active", Instant.now());
|
||||
user.setArn(regionResolver.buildArn("elasticache", regionResolver.getRegion(), "user:" + userId));
|
||||
|
||||
users.put(userId, user);
|
||||
LOG.infov("ElastiCache user {0} created with authMode={1}", userId, authMode);
|
||||
|
||||
+5
@@ -18,6 +18,8 @@ public class ElastiCacheUser {
|
||||
private String engine = "redis";
|
||||
private String status;
|
||||
private Instant createdAt;
|
||||
// Minted once in the region of the CreateUser request; null for users persisted before it was stored.
|
||||
private String arn;
|
||||
|
||||
public ElastiCacheUser() {}
|
||||
|
||||
@@ -58,6 +60,9 @@ public class ElastiCacheUser {
|
||||
public Instant getCreatedAt() { return createdAt; }
|
||||
public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }
|
||||
|
||||
public String getArn() { return arn; }
|
||||
public void setArn(String arn) { this.arn = arn; }
|
||||
|
||||
@JsonIgnore
|
||||
public boolean isEnabled() {
|
||||
return isAccessStringOn(accessString);
|
||||
|
||||
@@ -440,7 +440,7 @@ public class MskService implements ResourceProvider {
|
||||
}
|
||||
|
||||
String accountId = regionResolver.getAccountId();
|
||||
String arn = AwsArnUtils.Arn.of("kafka", config.defaultRegion(), accountId,
|
||||
String arn = AwsArnUtils.Arn.of("kafka", regionResolver.getRegion(), accountId,
|
||||
"configuration/" + name + "/" + UUID.randomUUID()).toString();
|
||||
|
||||
MskConfiguration configuration = new MskConfiguration(arn, name, description, kafkaVersions, serverProperties);
|
||||
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
package io.github.hectorvent.floci.core.common;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
|
||||
/**
|
||||
* Resources created by a request signed for a non-default region carry that region in their ARN,
|
||||
* not the emulator's default region.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class RequestRegionArnIntegrationTest {
|
||||
|
||||
private static final String REGION = "eu-west-1";
|
||||
|
||||
private static final String OTHER_REGION = "us-west-2";
|
||||
|
||||
private static String auth(String service) {
|
||||
return auth(REGION, service);
|
||||
}
|
||||
|
||||
private static String auth(String region, String service) {
|
||||
return "AWS4-HMAC-SHA256 Credential=AKID/20260215/" + region + "/" + service
|
||||
+ "/aws4_request, SignedHeaders=host, Signature=abc";
|
||||
}
|
||||
|
||||
@Test
|
||||
void amazonMqBrokerArnUsesTheRequestRegion() {
|
||||
String brokerId = given()
|
||||
.header("Authorization", auth("mq"))
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{"brokerName": "region-arn-broker", "engineType": "RABBITMQ",
|
||||
"deploymentMode": "SINGLE_INSTANCE", "hostInstanceType": "mq.t3.micro",
|
||||
"publiclyAccessible": false,
|
||||
"users": [{"username": "admin", "password": "AdminPass123", "consoleAccess": true}]}
|
||||
""")
|
||||
.when()
|
||||
.post("/v1/brokers")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("brokerArn", startsWith("arn:aws:mq:" + REGION + ":"))
|
||||
.extract().path("brokerId");
|
||||
|
||||
given().header("Authorization", auth("mq")).delete("/v1/brokers/{id}", brokerId)
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mskConfigurationArnUsesTheRequestRegion() {
|
||||
String properties = Base64.getEncoder()
|
||||
.encodeToString("auto.create.topics.enable=true".getBytes(StandardCharsets.UTF_8));
|
||||
String arn = given()
|
||||
.header("Authorization", auth("kafka"))
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{"name": "region-arn-config", "kafkaVersions": ["3.6.0"], "serverProperties": "%s"}
|
||||
""".formatted(properties))
|
||||
.when()
|
||||
.post("/v1/configurations")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("arn", startsWith("arn:aws:kafka:" + REGION + ":"))
|
||||
.extract().path("arn");
|
||||
|
||||
given().header("Authorization", auth("kafka")).delete("/v1/configurations/{arn}", arn)
|
||||
.then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void eksFargateProfileArnUsesTheClusterRegion() {
|
||||
String cluster = "region-arn-cluster";
|
||||
given()
|
||||
.header("Authorization", auth("eks"))
|
||||
.contentType("application/json")
|
||||
.body("{\"name\":\"" + cluster + "\",\"roleArn\":\"arn:aws:iam::000000000000:role/eks-role\","
|
||||
+ "\"version\":\"1.29\"}")
|
||||
.when()
|
||||
.post("/clusters")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("cluster.arn", startsWith("arn:aws:eks:" + REGION + ":"));
|
||||
|
||||
given()
|
||||
.header("Authorization", auth("eks"))
|
||||
.contentType("application/json")
|
||||
.body("{\"fargateProfileName\":\"fp\",\"podExecutionRoleArn\":\"arn:aws:iam::000000000000:role/fp\"}")
|
||||
.when()
|
||||
.post("/clusters/{name}/fargate-profiles", cluster)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("fargateProfile.fargateProfileArn", startsWith("arn:aws:eks:" + REGION + ":"));
|
||||
|
||||
given().header("Authorization", auth("eks"))
|
||||
.delete("/clusters/{name}/fargate-profiles/{fp}", cluster, "fp").then().statusCode(200);
|
||||
given().header("Authorization", auth("eks"))
|
||||
.delete("/clusters/{name}", cluster).then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
void appSyncDomainNameUsesTheRequestRegionAndChannelNamespaceItsApisRegion() {
|
||||
String domain = "region-arn-" + System.nanoTime() + ".example.com";
|
||||
given()
|
||||
.header("Authorization", auth("appsync"))
|
||||
.contentType("application/json")
|
||||
.body("""
|
||||
{"domainName": "%s", "certificateArn": "arn:aws:acm:%s:000000000000:certificate/1"}
|
||||
""".formatted(domain, REGION))
|
||||
.when()
|
||||
.post("/v1/domainnames")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("domainNameConfig.domainNameArn", startsWith("arn:aws:appsync:" + REGION + ":"))
|
||||
.body("domainNameConfig.appsyncDomainName", containsString(REGION));
|
||||
given().header("Authorization", auth("appsync")).delete("/v1/domainnames/" + domain)
|
||||
.then().statusCode(204);
|
||||
|
||||
String apiId = given()
|
||||
.header("Authorization", auth("appsync"))
|
||||
.contentType("application/json")
|
||||
.body("{\"name\": \"region-arn-api\", \"authenticationType\": \"API_KEY\"}")
|
||||
.when()
|
||||
.post("/v1/apis")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.extract().path("graphqlApi.apiId");
|
||||
// A namespace lives in its API's region, even when the request is signed for another one.
|
||||
given()
|
||||
.header("Authorization", auth(OTHER_REGION, "appsync"))
|
||||
.contentType("application/json")
|
||||
.body("{\"name\": \"region-arn-ns\"}")
|
||||
.when()
|
||||
.post("/v2/apis/" + apiId + "/channelNamespaces")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("channelNamespace.channelNamespaceArn", startsWith("arn:aws:appsync:" + REGION + ":"));
|
||||
given().header("Authorization", auth("appsync"))
|
||||
.delete("/v2/apis/" + apiId + "/channelNamespaces/region-arn-ns").then().statusCode(204);
|
||||
given().header("Authorization", auth("appsync")).delete("/v1/apis/" + apiId).then().statusCode(204);
|
||||
}
|
||||
|
||||
@Test
|
||||
void elastiCacheUserArnIsMintedOnceInTheCreateRegion() {
|
||||
given()
|
||||
.header("Authorization", auth("elasticache"))
|
||||
.formParam("Action", "CreateUser")
|
||||
.formParam("UserId", "region-arn-user")
|
||||
.formParam("UserName", "region-arn-user")
|
||||
.formParam("Engine", "valkey")
|
||||
.formParam("AuthenticationMode.Type", "no-password-required")
|
||||
.formParam("AccessString", "on ~* +@all")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("CreateUserResponse.CreateUserResult.ARN", startsWith("arn:aws:elasticache:" + REGION + ":"));
|
||||
|
||||
// Users are not stored per region, so another region can read this one; its ARN must not change.
|
||||
given()
|
||||
.header("Authorization", auth(OTHER_REGION, "elasticache"))
|
||||
.formParam("Action", "DescribeUsers")
|
||||
.formParam("UserId", "region-arn-user")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("DescribeUsersResponse.DescribeUsersResult.Users.member.ARN",
|
||||
startsWith("arn:aws:elasticache:" + REGION + ":"));
|
||||
|
||||
given()
|
||||
.header("Authorization", auth("elasticache"))
|
||||
.formParam("Action", "DeleteUser")
|
||||
.formParam("UserId", "region-arn-user")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user