mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(sts): preserve assumed role session identity (#4403)
* fix(sts): preserve assumed role session identity * test(sts): cover federated caller identity round trips
This commit is contained in:
1 parent
4b15ca8950
commit
cd9843f5bd
8 files changed
+186
-24
No files matched your search
+18
-3
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.eks;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.Response;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -134,11 +135,10 @@ class EksIrsaEndToEndIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void mintedTokenAssumesRoleAndReturnsRealClaims() {
|
||||
void mintedTokenAssumesRoleAndPreservesCallerIdentity() {
|
||||
String token = mintToken(NAMESPACE, SERVICE_ACCOUNT, null);
|
||||
|
||||
assumeRole(token)
|
||||
.then()
|
||||
ValidatableResponse assumed = assumeRole(token).then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<AccessKeyId>ASIA"))
|
||||
.body(containsString("<SecretAccessKey>"))
|
||||
@@ -148,6 +148,21 @@ class EksIrsaEndToEndIntegrationTest {
|
||||
+ NAMESPACE + ":" + SERVICE_ACCOUNT + "</SubjectFromWebIdentityToken>"))
|
||||
.body(containsString("<Provider>" + issuer + "</Provider>"))
|
||||
.body(containsString("<Audience>sts.amazonaws.com</Audience>"));
|
||||
|
||||
String prefix = "AssumeRoleWithWebIdentityResponse.AssumeRoleWithWebIdentityResult.";
|
||||
String accessKeyId = assumed.extract().path(prefix + "Credentials.AccessKeyId");
|
||||
String sessionToken = assumed.extract().path(prefix + "Credentials.SessionToken");
|
||||
String assumedRoleArn = assumed.extract().path(prefix + "AssumedRoleUser.Arn");
|
||||
String assumedRoleId = assumed.extract().path(prefix + "AssumedRoleUser.AssumedRoleId");
|
||||
given()
|
||||
.contentType(FORM)
|
||||
.formParam("Action", "GetCallerIdentity")
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=" + accessKeyId
|
||||
+ "/20260926/us-east-1/sts/aws4_request")
|
||||
.header("X-Amz-Security-Token", sessionToken)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("GetCallerIdentityResponse.GetCallerIdentityResult.Arn", equalTo(assumedRoleArn))
|
||||
.body("GetCallerIdentityResponse.GetCallerIdentityResult.UserId", equalTo(assumedRoleId));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+19
-3
@@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.bouncycastle.asn1.x500.X500Name;
|
||||
import org.bouncycastle.cert.X509CertificateHolder;
|
||||
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
|
||||
@@ -224,14 +225,29 @@ class AssumeRoleWithSamlValidationIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void validSignedAssertionReturnsCredentialsAndSamlFields() {
|
||||
void validSignedAssertionPreservesCallerIdentity() {
|
||||
String role = createRole(true);
|
||||
assume(role, assertion(role, PROVIDER, Instant.now().plusSeconds(300), AUDIENCE, ISSUER, true), PROVIDER)
|
||||
ValidatableResponse assumed = assume(role,
|
||||
assertion(role, PROVIDER, Instant.now().plusSeconds(300), AUDIENCE, ISSUER, true), PROVIDER)
|
||||
.statusCode(200)
|
||||
.body("AssumeRoleWithSAMLResponse.AssumeRoleWithSAMLResult.Credentials.AccessKeyId", startsWith("ASIA"))
|
||||
.body("AssumeRoleWithSAMLResponse.AssumeRoleWithSAMLResult.Issuer", containsString(ISSUER))
|
||||
.body("AssumeRoleWithSAMLResponse.AssumeRoleWithSAMLResult.Audience", containsString(AUDIENCE))
|
||||
.body("AssumeRoleWithSAMLResponse.AssumeRoleWithSAMLResult.Subject", containsString("saml-subject"));
|
||||
|
||||
String prefix = "AssumeRoleWithSAMLResponse.AssumeRoleWithSAMLResult.";
|
||||
String accessKeyId = assumed.extract().path(prefix + "Credentials.AccessKeyId");
|
||||
String sessionToken = assumed.extract().path(prefix + "Credentials.SessionToken");
|
||||
String assumedRoleArn = assumed.extract().path(prefix + "AssumedRoleUser.Arn");
|
||||
String assumedRoleId = assumed.extract().path(prefix + "AssumedRoleUser.AssumedRoleId");
|
||||
given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "GetCallerIdentity")
|
||||
.header("Authorization", "AWS4-HMAC-SHA256 Credential=" + accessKeyId
|
||||
+ "/20260926/us-east-1/sts/aws4_request")
|
||||
.header("X-Amz-Security-Token", sessionToken)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("GetCallerIdentityResponse.GetCallerIdentityResult.Arn", equalTo(assumedRoleArn))
|
||||
.body("GetCallerIdentityResponse.GetCallerIdentityResult.UserId", equalTo(assumedRoleId));
|
||||
}
|
||||
|
||||
private static void assertSignatureInvalid(Function<XMLSignatureFactory, List<Transform>> transforms) {
|
||||
@@ -255,7 +271,7 @@ class AssumeRoleWithSamlValidationIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static io.restassured.response.ValidatableResponse assume(String role, String assertion, String provider) {
|
||||
private static ValidatableResponse assume(String role, String assertion, String provider) {
|
||||
return given().contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "AssumeRoleWithSAML")
|
||||
.formParam("RoleArn", role)
|
||||
|
||||
@@ -88,6 +88,27 @@ class IamServiceTest {
|
||||
assertTrue(iamService.findSecretKey("ASIAOTHER", "other-token").isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumedRoleSessionIdentitySurvivesPersistence() throws Exception {
|
||||
String accessKeyId = "ASIAASSUMEDROLESESSION";
|
||||
InMemoryStorage<String, SessionCredential> sessions = new InMemoryStorage<>();
|
||||
IamService service = iamService(false, new InMemoryStorage<>(), sessions);
|
||||
service.registerSession(accessKeyId, "secret", "token",
|
||||
"arn:aws:iam::123456789012:role/TestRole", Instant.now().plusSeconds(3600),
|
||||
null, "123456789012", "my-custom-session-name",
|
||||
"AROATESTROLEID:my-custom-session-name");
|
||||
|
||||
SessionCredential stored = sessions.get(accessKeyId).orElseThrow();
|
||||
ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
|
||||
SessionCredential restored = mapper.readValue(mapper.writeValueAsBytes(stored), SessionCredential.class);
|
||||
assertEquals("my-custom-session-name", restored.getRoleSessionName());
|
||||
assertEquals("AROATESTROLEID:my-custom-session-name", restored.getAssumedRoleId());
|
||||
sessions.put(accessKeyId, restored);
|
||||
assertEquals("arn:aws:sts::123456789012:assumed-role/TestRole/my-custom-session-name",
|
||||
service.resolveCallerArn(accessKeyId).orElseThrow());
|
||||
assertEquals(restored.getAssumedRoleId(), service.resolveCallerUserId(accessKeyId).orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ec2SessionMarkerSurvivesPersistenceAndExpiredCredentialsCannotAuthenticate() throws Exception {
|
||||
SessionCredential expired = new SessionCredential("ASIAEXPIREDEC2", "secret", "token",
|
||||
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
@QuarkusTest
|
||||
class StsSessionIdentityIntegrationTest {
|
||||
|
||||
private static final String ROLE_NAME = "sts-session-identity-role";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/" + ROLE_NAME;
|
||||
private static final String SESSION_NAME = "my-custom-session-name";
|
||||
private static final String AUTHORIZATION =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260925/us-east-1/iam/aws4_request";
|
||||
private static final String STS_AUTHORIZATION =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260925/us-east-1/sts/aws4_request";
|
||||
|
||||
@AfterEach
|
||||
void deleteRole() {
|
||||
given()
|
||||
.formParam("Action", "DeleteRole")
|
||||
.formParam("RoleName", ROLE_NAME)
|
||||
.header("Authorization", AUTHORIZATION)
|
||||
.when().post("/");
|
||||
}
|
||||
|
||||
@Test
|
||||
void callerIdentityMatchesTheAssumedRoleSession() {
|
||||
given()
|
||||
.formParam("Action", "CreateRole")
|
||||
.formParam("RoleName", ROLE_NAME)
|
||||
.formParam("AssumeRolePolicyDocument",
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Principal\":{\"AWS\":\"arn:aws:iam::000000000000:root\"},"
|
||||
+ "\"Action\":\"sts:AssumeRole\"}]}")
|
||||
.header("Authorization", AUTHORIZATION)
|
||||
.when().post("/").then().statusCode(200);
|
||||
|
||||
ValidatableResponse assumed = given()
|
||||
.formParam("Action", "AssumeRole")
|
||||
.formParam("RoleArn", ROLE_ARN)
|
||||
.formParam("RoleSessionName", SESSION_NAME)
|
||||
.header("Authorization", STS_AUTHORIZATION)
|
||||
.when().post("/").then().statusCode(200);
|
||||
String accessKeyId = assumed.extract().path(
|
||||
"AssumeRoleResponse.AssumeRoleResult.Credentials.AccessKeyId");
|
||||
String sessionToken = assumed.extract().path(
|
||||
"AssumeRoleResponse.AssumeRoleResult.Credentials.SessionToken");
|
||||
String assumedRoleArn = assumed.extract().path(
|
||||
"AssumeRoleResponse.AssumeRoleResult.AssumedRoleUser.Arn");
|
||||
String assumedRoleId = assumed.extract().path(
|
||||
"AssumeRoleResponse.AssumeRoleResult.AssumedRoleUser.AssumedRoleId");
|
||||
|
||||
given()
|
||||
.formParam("Action", "GetCallerIdentity")
|
||||
.header("Authorization",
|
||||
"AWS4-HMAC-SHA256 Credential=" + accessKeyId
|
||||
+ "/20260925/us-east-1/sts/aws4_request")
|
||||
.header("X-Amz-Security-Token", sessionToken)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body("GetCallerIdentityResponse.GetCallerIdentityResult.Arn", equalTo(assumedRoleArn))
|
||||
.body("GetCallerIdentityResponse.GetCallerIdentityResult.UserId", equalTo(assumedRoleId));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user