mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(sts): preserve IAM role IDs in federated sessions (#7839)
This commit is contained in:
1 parent
99be149382
commit
37adf31f6a
4 files changed
+70
-9
No files matched your search
@@ -194,8 +194,7 @@ public class StsQueryHandler {
|
||||
|
||||
String sessionRoleArn = canonicalRoleArn(role, accountId, roleName);
|
||||
String assumedRoleArn = assumedRoleArn(sessionRoleArn, accountId, roleName, sessionName);
|
||||
String roleId = (role != null && role.getRoleId() != null) ? role.getRoleId() : ("AROA" + randomId(16));
|
||||
String assumedRoleId = roleId + ":" + sessionName;
|
||||
String assumedRoleId = assumedRoleId(role, sessionName);
|
||||
|
||||
// Register session so IAM enforcement can resolve the role's policies, RDS/ElastiCache
|
||||
// IAM token validation can find the temporary secret key, and account routing can map
|
||||
@@ -408,7 +407,7 @@ public class StsQueryHandler {
|
||||
|
||||
String sessionRoleArn = canonicalRoleArn(role, accountId, roleName);
|
||||
String assumedRoleArn = assumedRoleArn(sessionRoleArn, accountId, roleName, sessionName);
|
||||
String assumedRoleId = "AROA" + randomId(16) + ":" + sessionName;
|
||||
String assumedRoleId = assumedRoleId(role, sessionName);
|
||||
|
||||
String provider = verified != null ? verified.issuer()
|
||||
: (providerId != null && !providerId.isBlank() ? providerId : "accounts.google.com");
|
||||
@@ -529,6 +528,11 @@ public class StsQueryHandler {
|
||||
new VerifiedWebIdentity(claims.issuer(), claims.subject(), STS_AUDIENCE), role.get());
|
||||
}
|
||||
|
||||
private static String assumedRoleId(IamRole role, String sessionName) {
|
||||
String roleId = (role != null && role.getRoleId() != null) ? role.getRoleId() : ("AROA" + randomId(16));
|
||||
return roleId + ":" + sessionName;
|
||||
}
|
||||
|
||||
/**
|
||||
* A session on a role lives in the role's partition, whatever region AssumeRole is signed
|
||||
* for. Callers pass the stored role's ARN when the role exists, not the RoleArn the request
|
||||
@@ -612,7 +616,7 @@ public class StsQueryHandler {
|
||||
String sessionToken = sessionToken(params);
|
||||
String sessionRoleArn = canonicalRoleArn(role, accountId, roleName);
|
||||
String assumedRoleArn = assumedRoleArn(sessionRoleArn, accountId, roleName, sessionName);
|
||||
String assumedRoleId = "AROA" + randomId(16) + ":" + sessionName;
|
||||
String assumedRoleId = assumedRoleId(role, sessionName);
|
||||
|
||||
iamService.registerSession(accessKeyId, secretKey, sessionToken, sessionRoleArn, expiration, null,
|
||||
callerAccountId, sessionName, assumedRoleId);
|
||||
|
||||
+5
-2
@@ -22,6 +22,7 @@ import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.iam.IamClient;
|
||||
import software.amazon.awssdk.services.iam.model.IamException;
|
||||
import software.amazon.awssdk.services.iam.model.Role;
|
||||
import software.amazon.awssdk.services.sts.StsClient;
|
||||
import software.amazon.awssdk.services.sts.model.AssumeRoleWithSamlResponse;
|
||||
import software.amazon.awssdk.services.sts.model.StsException;
|
||||
@@ -383,8 +384,8 @@ class AssumeRoleWithSamlValidationIntegrationTest {
|
||||
String trust = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\","
|
||||
+ "\"Principal\":{\"Federated\":\"" + providerArn
|
||||
+ "\"},\"Action\":\"sts:AssumeRoleWithSAML\"}]}";
|
||||
roleArn = client.createRole(b -> b.roleName(name).assumeRolePolicyDocument(trust)
|
||||
.maxSessionDuration(maximum)).role().arn();
|
||||
roleArn = client.createRole(b -> b.roleName(name).path("/teams/saml/").assumeRolePolicyDocument(trust)
|
||||
.maxSessionDuration(maximum).tags(tag -> tag.key("team").value("federated"))).role().arn();
|
||||
} catch (RuntimeException | Error failure) {
|
||||
try {
|
||||
close();
|
||||
@@ -402,6 +403,8 @@ class AssumeRoleWithSamlValidationIntegrationTest {
|
||||
.principalArn(providerArn).durationSeconds(duration)
|
||||
.samlAssertion(assertion(roleArn, providerArn, expiry, AUDIENCE, ISSUER, sign)));
|
||||
sessions.add(response.credentials().accessKeyId());
|
||||
Role role = client.getRole(b -> b.roleName(name)).role();
|
||||
FederatedSessionAssertions.assertIdentity(endpoint, iamService, role, "saml-subject", response);
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package io.floci.aws.services.iam;
|
||||
|
||||
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
import software.amazon.awssdk.services.iam.model.Role;
|
||||
import software.amazon.awssdk.services.sts.StsClient;
|
||||
import software.amazon.awssdk.services.sts.model.AssumeRoleWithSamlResponse;
|
||||
import software.amazon.awssdk.services.sts.model.AssumeRoleWithWebIdentityResponse;
|
||||
import software.amazon.awssdk.services.sts.model.AssumedRoleUser;
|
||||
import software.amazon.awssdk.services.sts.model.Credentials;
|
||||
import software.amazon.awssdk.services.sts.model.GetCallerIdentityResponse;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
final class FederatedSessionAssertions {
|
||||
|
||||
private FederatedSessionAssertions() {
|
||||
}
|
||||
|
||||
static void assertIdentity(URI endpoint, IamService iam, Role role, String sessionName,
|
||||
AssumeRoleWithWebIdentityResponse response) {
|
||||
assertIdentity(endpoint, iam, role, sessionName, response.credentials(), response.assumedRoleUser());
|
||||
}
|
||||
|
||||
static void assertIdentity(URI endpoint, IamService iam, Role role, String sessionName,
|
||||
AssumeRoleWithSamlResponse response) {
|
||||
assertIdentity(endpoint, iam, role, sessionName, response.credentials(), response.assumedRoleUser());
|
||||
}
|
||||
|
||||
private static void assertIdentity(URI endpoint, IamService iam, Role role, String sessionName,
|
||||
Credentials credentials, AssumedRoleUser assumedRoleUser) {
|
||||
GetCallerIdentityResponse identity;
|
||||
try (StsClient caller = StsClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsSessionCredentials.create(
|
||||
credentials.accessKeyId(), credentials.secretAccessKey(), credentials.sessionToken())))
|
||||
.build()) {
|
||||
identity = caller.getCallerIdentity();
|
||||
}
|
||||
assertAll(
|
||||
() -> assertEquals(role.roleId() + ":" + sessionName, assumedRoleUser.assumedRoleId()),
|
||||
() -> assertEquals(assumedRoleUser.arn(), identity.arn()),
|
||||
() -> assertEquals(role.roleId() + ":" + sessionName, identity.userId()),
|
||||
() -> assertEquals(Map.of("team", "federated"),
|
||||
iam.resolveCallerArns(credentials.accessKeyId()).orElseThrow().principalTags()));
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ import software.amazon.awssdk.services.iam.IamClient;
|
||||
import software.amazon.awssdk.services.iam.model.Role;
|
||||
import software.amazon.awssdk.services.sts.StsClient;
|
||||
import software.amazon.awssdk.services.sts.model.AssumeRoleWithWebIdentityRequest;
|
||||
import software.amazon.awssdk.services.sts.model.AssumeRoleWithWebIdentityResponse;
|
||||
import software.amazon.awssdk.services.sts.model.Credentials;
|
||||
import software.amazon.awssdk.services.sts.model.StsException;
|
||||
|
||||
@@ -60,7 +61,8 @@ class WebIdentityDurationIntegrationTest {
|
||||
StsClient sts = StsClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
|
||||
.credentialsProvider(AnonymousCredentialsProvider.create()).build()) {
|
||||
Role role = admin.createRole(b -> b.roleName(keyName).path("/teams/web/")
|
||||
.maxSessionDuration(maximum).assumeRolePolicyDocument(trust)).role();
|
||||
.maxSessionDuration(maximum).assumeRolePolicyDocument(trust)
|
||||
.tags(tag -> tag.key("team").value("federated"))).role();
|
||||
try {
|
||||
String token = oidc.mintServiceAccountToken(keyName, issuer, "default",
|
||||
"untrusted".equals(tokenKind) ? "other" : "app", "sts.amazonaws.com", 3600);
|
||||
@@ -72,12 +74,13 @@ class WebIdentityDurationIntegrationTest {
|
||||
}
|
||||
Instant before = Instant.now();
|
||||
if ("allow".equals(expected)) {
|
||||
Credentials credentials = sts.assumeRoleWithWebIdentity(request.build()).credentials();
|
||||
AssumeRoleWithWebIdentityResponse response = sts.assumeRoleWithWebIdentity(request.build());
|
||||
Credentials credentials = response.credentials();
|
||||
sessions.add(credentials);
|
||||
int seconds = requested == 0 ? 3600 : requested;
|
||||
assertFalse(credentials.expiration().isBefore(before.plusSeconds(seconds - 1)));
|
||||
assertFalse(credentials.expiration().isAfter(Instant.now().plusSeconds(seconds)));
|
||||
assertTrue(iam.resolveCallerArns(credentials.accessKeyId()).isPresent());
|
||||
FederatedSessionAssertions.assertIdentity(endpoint, iam, role, "duration", response);
|
||||
} else {
|
||||
StsException error = assertThrows(StsException.class, () ->
|
||||
sessions.add(sts.assumeRoleWithWebIdentity(request.build()).credentials()));
|
||||
|
||||
Reference in new issue
Block a user