mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(sts): reject AssumeRole for roles that do not exist (#4463)
* fix(sts): từ chối AssumeRole khi role không tồn tại trong IAM AssumeRole trước đây cấp credential cho mọi role ARN, kể cả role chưa từng được tạo (iam:GetRole trả NoSuchEntity). Giờ trả AccessDenied như AWS, bất kể enforcement có bật hay không; trust policy vẫn chỉ được đánh giá khi enforcement bật. Các test cũ assume role chưa tạo được sửa để tạo role trước. Closes #4392 * fix(sts): so sánh chính xác role ARN và cập nhật compatibility tests - StsQueryHandler: kiểm tra roleArn khớp chính xác role.getArn() để tránh chấp nhận role cùng tên khác path - StsQueryHandlerTest: đổi tham số helper sang IamRole thay vì Optional, bổ sung test case từ chối role khác path - AssumeRoleTrustPolicyIntegrationTest: sửa Javadoc mô tả đúng hành vi từ chối unknown role - Cập nhật compatibility tests (Java, Node, Python, AWS CLI) để tạo role trước khi assume và kiểm tra lỗi 403 khi role không tồn tại * fix(sts): accept partition-correct role ARNs and strictly catch cleanup exceptions - StsQueryHandler: match role ARN structurally using AwsArnUtils to admit valid noncommercial partition ARNs - StsQueryHandler: preserve caller partition when generating assumedRoleArn - StsTest, test_sts.py, sts.test.ts: only ignore NoSuchEntity/EntityAlreadyExists in cleanup, re-raising unexpected errors per AGENTS.md - StsQueryHandlerTest, AssumeRoleTrustPolicyIntegrationTest: add unit and integration coverage for partition-correct role ARNs * fix(sts): preserve role partition in resolved session caller ARN * fix(sts): preserve stored role partition in assumed identity * fix(sts): validate web identity role ARN * fix(sts): require role existence in AssumeRoleWithWebIdentity and reuse path helpers * fix(sts): keep IAM ARN partition behavior unchanged * fix(sts): derive trust caller account from identity ARN * fix(iam): migrate legacy credential reports by partition * fix(iam): serialize legacy credential report migration * test(iam): make credential report race deterministic * fix(sts): address review feedback * fix(sts): reuse IAM role path normalization
This commit is contained in:
1 parent
f798fdc662
commit
436c15ca38
13 files changed
+580
-52
No files matched your search
+11
@@ -39,6 +39,17 @@ class AssumedRoleAccountRoutingIntegrationTest {
|
||||
void assumedRoleCredentialsRouteResourcesToTargetAccount() {
|
||||
String tableName = "routing-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
|
||||
given()
|
||||
.formParam("Action", "CreateRole")
|
||||
.formParam("RoleName", "CrossAccountAccess")
|
||||
.formParam("AssumeRolePolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Principal\":{\"AWS\":\"arn:aws:iam::" + ACCOUNT_A + ":root\"},\"Action\":\"sts:AssumeRole\"}]}")
|
||||
.header("Authorization", auth(ACCOUNT_B, "iam"))
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// 1. Account A assumes a role in account B and receives temporary credentials.
|
||||
String tempAccessKeyId = given()
|
||||
.formParam("Action", "AssumeRole")
|
||||
|
||||
+10
@@ -110,6 +110,16 @@ class CloudFormationAccountOwnershipIntegrationTest {
|
||||
createStack(ACCOUNT_1, US_EAST_1, "cfn-management-stack",
|
||||
"{\"Resources\":{\"ManagementBucket\":{\"Type\":\"AWS::S3::Bucket\"}}}");
|
||||
|
||||
given()
|
||||
.header("Authorization", auth(ACCOUNT_2, US_EAST_1, "iam"))
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateRole")
|
||||
.formParam("RoleName", "cfn-member-deploy-role")
|
||||
.formParam("AssumeRolePolicyDocument", "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Principal\":{\"AWS\":\"arn:aws:iam::" + ACCOUNT_1 + ":root\"},\"Action\":\"sts:AssumeRole\"}]}")
|
||||
.when().post("/").then()
|
||||
.statusCode(200);
|
||||
|
||||
String memberAccessKeyId = given()
|
||||
.header("Authorization", auth(ACCOUNT_1, US_EAST_1, "sts"))
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
|
||||
+78
-9
@@ -14,7 +14,7 @@ import static org.hamcrest.Matchers.startsWith;
|
||||
/**
|
||||
* Verifies that, with {@code iam.enforcement-enabled=true}, STS AssumeRole honors the target role's
|
||||
* trust policy: a caller the trust policy permits succeeds; one it does not is denied; and a role
|
||||
* Floci does not know about stays permissive (backward-compatible).
|
||||
* that does not exist is denied.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestProfile(IamEnforcementProfile.class)
|
||||
@@ -34,12 +34,16 @@ class AssumeRoleTrustPolicyIntegrationTest {
|
||||
}
|
||||
|
||||
private static void createRoleInB(String roleName) {
|
||||
createRole(ACCOUNT_B, roleName, TRUST_ALLOW_A);
|
||||
}
|
||||
|
||||
private static void createRole(String accountId, String roleName, String trustPolicyDocument) {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "CreateRole")
|
||||
.formParam("RoleName", roleName)
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_ALLOW_A)
|
||||
.header("Authorization", auth(ACCOUNT_B, "iam"))
|
||||
.formParam("AssumeRolePolicyDocument", trustPolicyDocument)
|
||||
.header("Authorization", auth(accountId, "iam"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200);
|
||||
}
|
||||
@@ -60,6 +64,70 @@ class AssumeRoleTrustPolicyIntegrationTest {
|
||||
.body("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId", startsWith("ASIA"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void alternatePartitionArnKeepsTheStoredRolePartitionInSessionIdentity() {
|
||||
String role = "trust-partition-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
createRoleInB(role);
|
||||
|
||||
String accessKeyId = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "AssumeRole")
|
||||
.formParam("RoleArn", "arn:aws-cn:iam::" + ACCOUNT_B + ":role/" + role)
|
||||
.formParam("RoleSessionName", "s")
|
||||
.header("Authorization", auth(ACCOUNT_A, "sts"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId", startsWith("ASIA"))
|
||||
.body("AssumeRoleResponse.AssumeRoleResult.AssumedRoleUser.Arn",
|
||||
containsString("arn:aws:sts::" + ACCOUNT_B + ":assumed-role/" + role + "/s"))
|
||||
.extract().path("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId");
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "GetCallerIdentity")
|
||||
.header("Authorization", auth(accessKeyId, "sts"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("GetCallerIdentityResponse.GetCallerIdentityResult.Arn",
|
||||
containsString("arn:aws:sts::" + ACCOUNT_B + ":assumed-role/" + role + "/s"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void requestedPartitionCannotChangeCallerPrincipalForLaterTrustChecks() {
|
||||
String firstRole = "trust-source-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String protectedRole = "trust-target-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
String sessionName = "partition-spoof";
|
||||
createRoleInB(firstRole);
|
||||
|
||||
String forgedPrincipal = "arn:aws-cn:sts::" + ACCOUNT_B + ":assumed-role/" + firstRole + "/" + sessionName;
|
||||
String targetTrust = "{\"Version\":\"2012-10-17\",\"Statement\":[{"
|
||||
+ "\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"" + forgedPrincipal + "\"},"
|
||||
+ "\"Action\":\"sts:AssumeRole\"}]}";
|
||||
createRole(ACCOUNT_C, protectedRole, targetTrust);
|
||||
|
||||
String accessKeyId = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "AssumeRole")
|
||||
.formParam("RoleArn", "arn:aws-cn:iam::" + ACCOUNT_B + ":role/" + firstRole)
|
||||
.formParam("RoleSessionName", sessionName)
|
||||
.header("Authorization", auth(ACCOUNT_A, "sts"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("AssumeRoleResponse.AssumeRoleResult.AssumedRoleUser.Arn",
|
||||
containsString("arn:aws:sts::" + ACCOUNT_B + ":assumed-role/" + firstRole + "/" + sessionName))
|
||||
.extract().path("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId");
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "AssumeRole")
|
||||
.formParam("RoleArn", "arn:aws:iam::" + ACCOUNT_C + ":role/" + protectedRole)
|
||||
.formParam("RoleSessionName", "second-session")
|
||||
.header("Authorization", auth(accessKeyId, "sts"))
|
||||
.when().post("/")
|
||||
.then().statusCode(403)
|
||||
.body(containsString("AccessDenied"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unauthorizedCallerIsDenied() {
|
||||
String role = "trust-deny-" + UUID.randomUUID().toString().substring(0, 8);
|
||||
@@ -81,17 +149,18 @@ class AssumeRoleTrustPolicyIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownRoleStaysPermissive() {
|
||||
// No role created — enforcement must not block roles Floci has never seen.
|
||||
void unknownRoleIsDenied() {
|
||||
String roleArn = "arn:aws:iam::" + ACCOUNT_B + ":role/never-created-"
|
||||
+ UUID.randomUUID().toString().substring(0, 8);
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "AssumeRole")
|
||||
.formParam("RoleArn", "arn:aws:iam::" + ACCOUNT_B + ":role/never-created-"
|
||||
+ UUID.randomUUID().toString().substring(0, 8))
|
||||
.formParam("RoleArn", roleArn)
|
||||
.formParam("RoleSessionName", "s")
|
||||
.header("Authorization", auth(ACCOUNT_C, "sts"))
|
||||
.when().post("/")
|
||||
.then().statusCode(200)
|
||||
.body("AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId", startsWith("ASIA"));
|
||||
.then().statusCode(403)
|
||||
.body(containsString("<Code>AccessDenied</Code>"))
|
||||
.body(containsString("is not authorized to perform: sts:AssumeRole on resource: " + roleArn));
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,19 @@ class IamIntegrationTest {
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Principal\":{\"Service\":\"lambda.amazonaws.com\"},\"Action\":\"sts:AssumeRole\"}]}";
|
||||
|
||||
private static void createRoleForAssume(String accessKeyId, String roleName) {
|
||||
given()
|
||||
.formParam("Action", "CreateRole")
|
||||
.formParam("RoleName", roleName)
|
||||
.formParam("AssumeRolePolicyDocument", TRUST_POLICY)
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=" + accessKeyId + "/20260227/us-east-1/iam/aws4_request")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
private static final String POLICY_DOCUMENT =
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Action\":\"s3:GetObject\",\"Resource\":\"*\"}]}";
|
||||
@@ -77,9 +90,10 @@ class IamIntegrationTest {
|
||||
@Test
|
||||
@Order(3)
|
||||
void stsAssumeRole() {
|
||||
createRoleForAssume("test", "StsAssumeTestRole");
|
||||
given()
|
||||
.formParam("Action", "AssumeRole")
|
||||
.formParam("RoleArn", "arn:aws:iam::000000000000:role/TestRole")
|
||||
.formParam("RoleArn", "arn:aws:iam::000000000000:role/StsAssumeTestRole")
|
||||
.formParam("RoleSessionName", "test-session")
|
||||
.formParam("DurationSeconds", "3600")
|
||||
.header("Authorization",
|
||||
@@ -94,12 +108,13 @@ class IamIntegrationTest {
|
||||
.body("AssumeRoleResponse.AssumeRoleResult.Credentials.SessionToken", notNullValue())
|
||||
.body("AssumeRoleResponse.AssumeRoleResult.Credentials.Expiration", notNullValue())
|
||||
.body("AssumeRoleResponse.AssumeRoleResult.AssumedRoleUser.Arn",
|
||||
containsString("assumed-role/TestRole/test-session"));
|
||||
containsString("assumed-role/StsAssumeTestRole/test-session"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void stsAssumeRoleHonoursTwelveDigitAccessKey() {
|
||||
createRoleForAssume("123456789012", "TestRole");
|
||||
given()
|
||||
.formParam("Action", "AssumeRole")
|
||||
.formParam("RoleArn", "arn:aws:iam::123456789012:role/TestRole")
|
||||
@@ -118,6 +133,7 @@ class IamIntegrationTest {
|
||||
@Test
|
||||
@Order(6)
|
||||
void stsAssumeRoleUsesAccountFromRoleArnForCrossAccount() {
|
||||
createRoleForAssume("222222222222", "CrossAccountRole");
|
||||
given()
|
||||
.formParam("Action", "AssumeRole")
|
||||
.formParam("RoleArn", "arn:aws:iam::222222222222:role/CrossAccountRole")
|
||||
|
||||
@@ -138,6 +138,20 @@ class IamServiceTest {
|
||||
assertEquals(restored.getAssumedRoleId(), service.resolveCallerUserId(accessKeyId).orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolveCallerArnPreservesPartitionFromRoleArn() {
|
||||
String accessKeyId = "ASIACHINASESSION";
|
||||
InMemoryStorage<String, SessionCredential> sessions = new InMemoryStorage<>();
|
||||
IamService service = iamService(false, new InMemoryStorage<>(), sessions);
|
||||
service.registerSession(accessKeyId, "secret", "token",
|
||||
"arn:aws-cn:iam::123456789012:role/ChinaRole", Instant.now().plusSeconds(3600),
|
||||
null, "123456789012", "china-session",
|
||||
"AROAEXAMPLE:china-session");
|
||||
|
||||
assertEquals("arn:aws-cn:sts::123456789012:assumed-role/ChinaRole/china-session",
|
||||
service.resolveCallerArn(accessKeyId).orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSessionsCallerAndPrincipalArnsComeFromOneLookup() {
|
||||
IamService service = iamService(false, new InMemoryStorage<>(), new InMemoryStorage<>());
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -45,10 +46,21 @@ class StsQueryHandlerTest {
|
||||
private static final Pattern ARN = Pattern.compile("<Arn>([^<]+)</Arn>");
|
||||
|
||||
private static StsQueryHandler newHandler() {
|
||||
return newHandler(new RegionResolver(REGION, "000000000000"));
|
||||
IamRole role = new IamRole();
|
||||
role.setRoleName("TestRole");
|
||||
role.setArn("arn:aws:iam::000000000000:role/TestRole");
|
||||
return newHandler(role);
|
||||
}
|
||||
|
||||
private static StsQueryHandler newHandler(RegionResolver regionResolver) {
|
||||
private static StsQueryHandler newHandler(IamRole role) {
|
||||
return newHandler(role, new RegionResolver(REGION, "000000000000"));
|
||||
}
|
||||
|
||||
private static StsQueryHandler newHandlerForRegion(RegionResolver regionResolver) {
|
||||
return newHandler(null, regionResolver);
|
||||
}
|
||||
|
||||
private static StsQueryHandler newHandler(IamRole role, RegionResolver regionResolver) {
|
||||
EmulatorConfig config = mock(EmulatorConfig.class);
|
||||
EmulatorConfig.ServicesConfig services = mock(EmulatorConfig.ServicesConfig.class);
|
||||
EmulatorConfig.IamServiceConfig iam = mock(EmulatorConfig.IamServiceConfig.class);
|
||||
@@ -56,8 +68,11 @@ class StsQueryHandlerTest {
|
||||
when(services.iam()).thenReturn(iam);
|
||||
when(iam.enforcementEnabled()).thenReturn(false);
|
||||
|
||||
IamService iamService = mock(IamService.class);
|
||||
when(iamService.findRole(anyString(), anyString())).thenReturn(Optional.ofNullable(role));
|
||||
|
||||
return new StsQueryHandler(
|
||||
mock(IamService.class),
|
||||
iamService,
|
||||
mock(AccountResolver.class),
|
||||
regionResolver,
|
||||
config,
|
||||
@@ -92,6 +107,96 @@ class StsQueryHandlerTest {
|
||||
assertNotEquals(extract(SESSION_TOKEN, firstBody), extract(SESSION_TOKEN, secondBody));
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleDeniesRoleThatDoesNotExist() {
|
||||
StsQueryHandler handler = newHandler(null);
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleArn", "arn:aws:iam::000000000000:role/definitely-does-not-exist");
|
||||
params.putSingle("RoleSessionName", "test-session");
|
||||
|
||||
Response response = handler.handle("AssumeRole", params);
|
||||
|
||||
assertEquals(403, response.getStatus());
|
||||
assertTrue(((String) response.getEntity()).contains("<Code>AccessDenied</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleDeniesRoleWhenPathDiffers() {
|
||||
IamRole role = new IamRole();
|
||||
role.setRoleName("TestRole");
|
||||
role.setPath("/team/");
|
||||
role.setArn("arn:aws:iam::000000000000:role/team/TestRole");
|
||||
StsQueryHandler handler = newHandler(role);
|
||||
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleArn", "arn:aws:iam::000000000000:role/other/TestRole");
|
||||
params.putSingle("RoleSessionName", "test-session");
|
||||
|
||||
Response response = handler.handle("AssumeRole", params);
|
||||
|
||||
assertEquals(403, response.getStatus());
|
||||
assertTrue(((String) response.getEntity()).contains("<Code>AccessDenied</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleAllowsPartitionCorrectRoleArn() {
|
||||
IamRole role = new IamRole();
|
||||
role.setRoleName("TestRole");
|
||||
role.setPath("/");
|
||||
role.setArn("arn:aws:iam::000000000000:role/TestRole");
|
||||
StsQueryHandler handler = newHandler(role);
|
||||
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleArn", "arn:aws-cn:iam::000000000000:role/TestRole");
|
||||
params.putSingle("RoleSessionName", "test-session");
|
||||
|
||||
Response response = handler.handle("AssumeRole", params);
|
||||
|
||||
assertEquals(200, response.getStatus(), (String) response.getEntity());
|
||||
String body = (String) response.getEntity();
|
||||
assertTrue(body.contains("<Arn>arn:aws:sts::000000000000:assumed-role/TestRole/test-session</Arn>"),
|
||||
"AssumedRoleUser Arn must use the stored role's partition");
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleAllowsGovCloudPartitionRoleArn() {
|
||||
IamRole role = new IamRole();
|
||||
role.setRoleName("TestRole");
|
||||
role.setPath("/");
|
||||
role.setArn("arn:aws:iam::000000000000:role/TestRole");
|
||||
StsQueryHandler handler = newHandler(role);
|
||||
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleArn", "arn:aws-us-gov:iam::000000000000:role/TestRole");
|
||||
params.putSingle("RoleSessionName", "test-session");
|
||||
|
||||
Response response = handler.handle("AssumeRole", params);
|
||||
|
||||
assertEquals(200, response.getStatus(), (String) response.getEntity());
|
||||
String body = (String) response.getEntity();
|
||||
assertTrue(body.contains("<Arn>arn:aws:sts::000000000000:assumed-role/TestRole/test-session</Arn>"),
|
||||
"AssumedRoleUser Arn must use the stored role's partition");
|
||||
}
|
||||
|
||||
@Test
|
||||
void roleArnMatchesValidatesPartitionsAndPaths() {
|
||||
IamRole role = new IamRole();
|
||||
role.setRoleName("TestRole");
|
||||
role.setPath("/team/");
|
||||
role.setArn("arn:aws:iam::000000000000:role/team/TestRole");
|
||||
|
||||
assertTrue(StsQueryHandler.roleArnMatches("arn:aws:iam::000000000000:role/team/TestRole", role));
|
||||
assertTrue(StsQueryHandler.roleArnMatches("arn:aws-cn:iam::000000000000:role/team/TestRole", role));
|
||||
assertTrue(StsQueryHandler.roleArnMatches("arn:aws-us-gov:iam::000000000000:role/team/TestRole", role));
|
||||
|
||||
assertFalse(StsQueryHandler.roleArnMatches("arn:aws:iam::000000000000:role/other/TestRole", role));
|
||||
assertFalse(StsQueryHandler.roleArnMatches("arn:aws:iam::111111111111:role/team/TestRole", role));
|
||||
assertFalse(StsQueryHandler.roleArnMatches("arn:aws:iam:us-east-1:000000000000:role/team/TestRole", role));
|
||||
assertFalse(StsQueryHandler.roleArnMatches("arn:aws:s3:::bucket", role));
|
||||
assertFalse(StsQueryHandler.roleArnMatches("not-an-arn", role));
|
||||
assertFalse(StsQueryHandler.roleArnMatches(null, role));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"900", "43200"})
|
||||
void assumeRoleAcceptsDurationSecondsAtTheLimits(String durationSeconds) {
|
||||
@@ -228,6 +333,64 @@ class StsQueryHandlerTest {
|
||||
assertTrue(body.contains("greater than or equal to 900"), body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleWithWebIdentityRejectsMalformedRoleArnWhenTokenCannotBeVerified() {
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleArn", "not-an-arn");
|
||||
params.putSingle("RoleSessionName", "test-session");
|
||||
params.putSingle("WebIdentityToken", "opaque-token");
|
||||
|
||||
Response response = newHandler(null).handle("AssumeRoleWithWebIdentity", params);
|
||||
|
||||
assertEquals(400, response.getStatus());
|
||||
assertTrue(((String) response.getEntity()).contains("<Code>ValidationError</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleWithWebIdentityRejectsNonexistentRoleWhenTokenCannotBeVerified() {
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleArn", "arn:aws:iam::000000000000:role/NonexistentRole");
|
||||
params.putSingle("RoleSessionName", "test-session");
|
||||
params.putSingle("WebIdentityToken", "opaque-token");
|
||||
|
||||
Response response = newHandler(null).handle("AssumeRoleWithWebIdentity", params);
|
||||
|
||||
assertEquals(403, response.getStatus());
|
||||
String body = (String) response.getEntity();
|
||||
assertTrue(body.contains("<Code>AccessDenied</Code>"), body);
|
||||
assertTrue(body.contains("Not authorized to perform sts:AssumeRoleWithWebIdentity on resource: "
|
||||
+ "arn:aws:iam::000000000000:role/NonexistentRole"), body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleWithWebIdentitySucceedsWhenRoleExistsAndTokenCannotBeVerified() {
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleArn", "arn:aws:iam::000000000000:role/TestRole");
|
||||
params.putSingle("RoleSessionName", "test-session");
|
||||
params.putSingle("WebIdentityToken", "opaque-token");
|
||||
|
||||
Response response = newHandler().handle("AssumeRoleWithWebIdentity", params);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
String body = (String) response.getEntity();
|
||||
assertTrue(body.contains("<AssumeRoleWithWebIdentityResult>"), body);
|
||||
assertTrue(body.contains("<Arn>arn:aws:sts::000000000000:assumed-role/TestRole/test-session</Arn>"), body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleWithWebIdentityRejectsRoleMismatchWhenTokenCannotBeVerified() {
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleArn", "arn:aws:iam::111111111111:role/TestRole");
|
||||
params.putSingle("RoleSessionName", "test-session");
|
||||
params.putSingle("WebIdentityToken", "opaque-token");
|
||||
|
||||
Response response = newHandler().handle("AssumeRoleWithWebIdentity", params);
|
||||
|
||||
assertEquals(403, response.getStatus());
|
||||
String body = (String) response.getEntity();
|
||||
assertTrue(body.contains("<Code>AccessDenied</Code>"), body);
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleWithSAMLRejectsDurationSecondsBelowMinimum() {
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
@@ -248,7 +411,7 @@ class StsQueryHandlerTest {
|
||||
@ParameterizedTest
|
||||
@MethodSource("io.github.hectorvent.floci.testing.PartitionMatrix#cases")
|
||||
void getCallerIdentityMintsTheRootArnInTheDeploymentsPartition(PartitionCase partitionCase) {
|
||||
Response response = newHandler(PartitionMatrix.regionResolver(partitionCase))
|
||||
Response response = newHandlerForRegion(PartitionMatrix.regionResolver(partitionCase))
|
||||
.handle("GetCallerIdentity", new MultivaluedHashMap<>());
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
@@ -263,7 +426,7 @@ class StsQueryHandlerTest {
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("Name", "bob");
|
||||
|
||||
Response response = newHandler(PartitionMatrix.regionResolver(partitionCase))
|
||||
Response response = newHandlerForRegion(PartitionMatrix.regionResolver(partitionCase))
|
||||
.handle("GetFederationToken", params);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
@@ -327,8 +490,11 @@ class StsQueryHandlerTest {
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleArn", "arn:aws:iam::000000000000:role/commercial-role");
|
||||
params.putSingle("RoleSessionName", "s");
|
||||
IamRole role = new IamRole();
|
||||
role.setRoleName("commercial-role");
|
||||
role.setArn("arn:aws:iam::000000000000:role/commercial-role");
|
||||
|
||||
Response response = newHandler(new RegionResolver("cn-north-1", "000000000000"))
|
||||
Response response = newHandler(role, new RegionResolver("cn-north-1", "000000000000"))
|
||||
.handle("AssumeRole", params);
|
||||
|
||||
assertEquals(200, response.getStatus());
|
||||
|
||||
Reference in new issue
Block a user