mirror of
https://github.com/floci-io/floci.git
synced 2026-11-28 16:11: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,6 +11,8 @@ import software.amazon.awssdk.services.iam.IamClient;
|
||||
import software.amazon.awssdk.services.iam.model.CreateRoleRequest;
|
||||
import software.amazon.awssdk.services.iam.model.CreateSamlProviderRequest;
|
||||
import software.amazon.awssdk.services.iam.model.CreateSamlProviderResponse;
|
||||
import software.amazon.awssdk.services.iam.model.DeleteRoleRequest;
|
||||
import software.amazon.awssdk.services.iam.model.NoSuchEntityException;
|
||||
import software.amazon.awssdk.services.sts.StsClient;
|
||||
import software.amazon.awssdk.services.sts.model.*;
|
||||
import org.w3c.dom.Document;
|
||||
@@ -59,12 +61,31 @@ class StsTest {
|
||||
private static String allowedRoleArn;
|
||||
private static String allowedRoleName;
|
||||
|
||||
private static final String OPEN_TRUST_POLICY = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":\"*\"},\"Action\":\"sts:AssumeRole\"}]}";
|
||||
|
||||
@BeforeAll
|
||||
static void setup() throws Exception {
|
||||
sts = TestFixtures.stsClient();
|
||||
iam = TestFixtures.iamClient();
|
||||
signingKeys = createSigningKeys();
|
||||
|
||||
iam.createRole(CreateRoleRequest.builder()
|
||||
.roleName("sdk-test-assumed-role")
|
||||
.assumeRolePolicyDocument(OPEN_TRUST_POLICY)
|
||||
.build());
|
||||
iam.createRole(CreateRoleRequest.builder()
|
||||
.roleName("my-role")
|
||||
.assumeRolePolicyDocument(OPEN_TRUST_POLICY)
|
||||
.build());
|
||||
iam.createRole(CreateRoleRequest.builder()
|
||||
.roleName("short-lived-role")
|
||||
.assumeRolePolicyDocument(OPEN_TRUST_POLICY)
|
||||
.build());
|
||||
iam.createRole(CreateRoleRequest.builder()
|
||||
.roleName("web-identity-role")
|
||||
.assumeRolePolicyDocument(OPEN_TRUST_POLICY)
|
||||
.build());
|
||||
|
||||
String issuer = "https://sdk-test.example.test/saml";
|
||||
String providerName = TestFixtures.uniqueName("sdk-saml");
|
||||
String certificate = createCertificate(signingKeys);
|
||||
@@ -88,6 +109,11 @@ class StsTest {
|
||||
|
||||
@AfterAll
|
||||
static void cleanup() {
|
||||
safeDeleteRole("sdk-test-assumed-role");
|
||||
safeDeleteRole("my-role");
|
||||
safeDeleteRole("short-lived-role");
|
||||
safeDeleteRole("web-identity-role");
|
||||
safeDeleteRole(allowedRoleName);
|
||||
if (iam != null) {
|
||||
iam.close();
|
||||
}
|
||||
@@ -96,6 +122,16 @@ class StsTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static void safeDeleteRole(String roleName) {
|
||||
if (iam != null && roleName != null) {
|
||||
try {
|
||||
iam.deleteRole(DeleteRoleRequest.builder().roleName(roleName).build());
|
||||
} catch (NoSuchEntityException ignored) {
|
||||
// Safe to ignore if the role was already cleaned up or never created.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void getCallerIdentity() {
|
||||
GetCallerIdentityResponse response = sts.getCallerIdentity(
|
||||
@@ -211,6 +247,31 @@ class StsTest {
|
||||
.isEqualTo(400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleNonExistentRoleThrows403() {
|
||||
assertThatThrownBy(() -> sts.assumeRole(AssumeRoleRequest.builder()
|
||||
.roleArn("arn:aws:iam::000000000000:role/non-existent-role-" + UUID.randomUUID())
|
||||
.roleSessionName("sdk-test-session")
|
||||
.build()))
|
||||
.isInstanceOf(StsException.class)
|
||||
.extracting(e -> ((StsException) e).statusCode())
|
||||
.isEqualTo(403);
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleWithWebIdentityNonExistentRoleThrows403() {
|
||||
assertThatThrownBy(() -> sts.assumeRoleWithWebIdentity(
|
||||
AssumeRoleWithWebIdentityRequest.builder()
|
||||
.roleArn("arn:aws:iam::000000000000:role/non-existent-role-" + UUID.randomUUID())
|
||||
.roleSessionName("sdk-test-session")
|
||||
.webIdentityToken("eyJhbGciOiJSUzI1NiJ9.test-token")
|
||||
.durationSeconds(3600)
|
||||
.build()))
|
||||
.isInstanceOf(StsException.class)
|
||||
.extracting(e -> ((StsException) e).statusCode())
|
||||
.isEqualTo(403);
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleWithSaml() {
|
||||
AssumeRoleWithSamlResponse response = validAssumeRoleWithSaml();
|
||||
|
||||
Reference in new issue
Block a user