mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(sts): enforce web identity role duration limit (#7119)
This commit is contained in:
1 parent
224c35830c
commit
f74744dc5f
5 files changed
+154
No files matched your search
@@ -323,3 +323,7 @@ When `FLOCI_AUTH_VALIDATE_SIGNATURES=true`, the shared signature verifier checks
|
||||
credentials for STS operations, including `GetCallerIdentity`, `GetSessionToken`, `GetFederationToken`
|
||||
and `AssumeRole`. It checks the request signature, credential scope, expiry and session token before
|
||||
execution. Nonblank Authorization headers retain priority. See [signature checks](../configuration/multi-account.md#signature-validation).
|
||||
|
||||
AssumeRoleWithWebIdentity rejects a requested duration above the target role's
|
||||
`MaxSessionDuration` before issuing credentials. The default remains 3,600 seconds;
|
||||
existing token and trust-policy errors take precedence over this role-specific check.
|
||||
@@ -330,6 +330,18 @@
|
||||
</dependency>
|
||||
|
||||
<!-- Testing -->
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>sts</artifactId>
|
||||
<version>${aws.sdk.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>iam</artifactId>
|
||||
<version>${aws.sdk.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>software.amazon.awssdk</groupId>
|
||||
<artifactId>apigatewayv2</artifactId>
|
||||
|
||||
@@ -373,6 +373,12 @@ public class StsQueryHandler {
|
||||
}
|
||||
}
|
||||
|
||||
if (durationSeconds > role.getMaxSessionDuration()) {
|
||||
return AwsQueryResponse.error("ValidationError",
|
||||
"The requested DurationSeconds exceeds the MaxSessionDuration set for this role.",
|
||||
AwsNamespaces.STS, 400);
|
||||
}
|
||||
|
||||
String accessKeyId = "ASIA" + randomId(16);
|
||||
String secretKey = randomSecret(40);
|
||||
String sessionToken = randomSecret(200);
|
||||
|
||||
@@ -551,6 +551,33 @@ class StsQueryHandlerTest {
|
||||
assertTrue(body.contains("less than or equal to 129600"), body);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"3600,3601,400", "7200,7201,400", "3600,3600,200", "7200,7200,200",
|
||||
"3600,900,200", "43200,43200,200", "3600,0,200"})
|
||||
void webIdentityDurationUsesTheRoleLimitBeforeRegistering(int maximum, int requested, int status) {
|
||||
IamRole role = new IamRole();
|
||||
role.setRoleName("TestRole");
|
||||
role.setArn("arn:aws:iam::000000000000:role/TestRole");
|
||||
role.setMaxSessionDuration(maximum);
|
||||
IamService service = mock(IamService.class);
|
||||
when(service.findRole(anyString(), anyString())).thenReturn(Optional.of(role));
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
params.putSingle("RoleArn", role.getArn());
|
||||
params.putSingle("RoleSessionName", "duration");
|
||||
params.putSingle("WebIdentityToken", "opaque-token");
|
||||
if (requested != 0) {
|
||||
params.putSingle("DurationSeconds", Integer.toString(requested));
|
||||
}
|
||||
Response response = newHandler(service, new RegionResolver(REGION, "000000000000"))
|
||||
.handle("AssumeRoleWithWebIdentity", params);
|
||||
assertEquals(status, response.getStatus(), (String) response.getEntity());
|
||||
if (status == 400) {
|
||||
assertTrue(((String) response.getEntity()).contains("ValidationError"));
|
||||
verify(service, never()).registerSession(anyString(), anyString(), anyString(), anyString(),
|
||||
any(), any(), anyString(), anyString(), anyString());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void assumeRoleWithWebIdentityRejectsDurationSecondsBelowMinimum() {
|
||||
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
|
||||
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.services.eks.EksOidcService;
|
||||
import io.github.hectorvent.floci.testing.SignedIamEnforcementProfile;
|
||||
import io.github.hectorvent.floci.testing.SignedIamUsers;
|
||||
import io.quarkus.test.common.http.TestHTTPResource;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.TestProfile;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import software.amazon.awssdk.auth.credentials.AnonymousCredentialsProvider;
|
||||
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
|
||||
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
|
||||
import software.amazon.awssdk.regions.Region;
|
||||
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.Credentials;
|
||||
import software.amazon.awssdk.services.sts.model.StsException;
|
||||
|
||||
import java.net.URI;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@QuarkusTest
|
||||
@TestProfile(SignedIamEnforcementProfile.class)
|
||||
class WebIdentityDurationIntegrationTest {
|
||||
@TestHTTPResource("/")
|
||||
URI endpoint;
|
||||
@Inject
|
||||
IamService iam;
|
||||
@Inject
|
||||
EksOidcService oidc;
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"3600,3601,valid,ValidationError", "7200,7201,valid,ValidationError",
|
||||
"3600,3600,valid,allow", "7200,7200,valid,allow", "3600,900,valid,allow",
|
||||
"3600,0,valid,allow", "3600,3601,untrusted,AccessDenied",
|
||||
"3600,3601,invalid,InvalidIdentityToken"})
|
||||
void verifiedWebIdentityHonorsTheRoleDuration(int maximum, int requested, String tokenKind,
|
||||
String expected) throws Exception {
|
||||
String keyName = "web-duration-" + UUID.randomUUID();
|
||||
String issuer = oidc.newIssuerUrl("us-east-1");
|
||||
String providerArn = "arn:aws:iam::000000000000:oidc-provider/" + issuer.substring(8);
|
||||
String trust = "{\"Statement\":{\"Effect\":\"Allow\",\"Principal\":{\"Federated\":\""
|
||||
+ providerArn + "\"},\"Action\":\"sts:AssumeRoleWithWebIdentity\",\"Condition\":{\"StringEquals\":{\""
|
||||
+ issuer.substring(8) + ":sub\":\"system:serviceaccount:default:app\"}}}}";
|
||||
List<Credentials> sessions = new ArrayList<>();
|
||||
try (SignedIamUsers.User user = SignedIamUsers.createUser(endpoint, "web-duration-", "admin",
|
||||
"{\"Statement\":{\"Effect\":\"Allow\",\"Action\":\"*\",\"Resource\":\"*\"}}");
|
||||
IamClient admin = IamClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
|
||||
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(
|
||||
user.accessKeyId(), user.secretAccessKey()))).build();
|
||||
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();
|
||||
try {
|
||||
String token = oidc.mintServiceAccountToken(keyName, issuer, "default",
|
||||
"untrusted".equals(tokenKind) ? "other" : "app", "sts.amazonaws.com", 3600);
|
||||
AssumeRoleWithWebIdentityRequest.Builder request = AssumeRoleWithWebIdentityRequest.builder()
|
||||
.roleArn(role.arn()).roleSessionName("duration")
|
||||
.webIdentityToken("invalid".equals(tokenKind) ? "opaque-token" : token);
|
||||
if (requested != 0) {
|
||||
request.durationSeconds(requested);
|
||||
}
|
||||
Instant before = Instant.now();
|
||||
if ("allow".equals(expected)) {
|
||||
Credentials credentials = sts.assumeRoleWithWebIdentity(request.build()).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());
|
||||
} else {
|
||||
StsException error = assertThrows(StsException.class, () ->
|
||||
sessions.add(sts.assumeRoleWithWebIdentity(request.build()).credentials()));
|
||||
assertEquals(expected, error.awsErrorDetails().errorCode());
|
||||
assertEquals("AccessDenied".equals(expected) ? 403 : 400, error.statusCode());
|
||||
}
|
||||
assertEquals(maximum, admin.getRole(b -> b.roleName(keyName)).role().maxSessionDuration());
|
||||
} finally {
|
||||
try {
|
||||
for (Credentials session : sessions.reversed()) {
|
||||
iam.unregisterSession("000000000000", session.accessKeyId());
|
||||
assertTrue(iam.resolveCallerArns(session.accessKeyId()).isEmpty());
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
oidc.deleteKey(keyName);
|
||||
assertTrue(oidc.findKeyByCluster(keyName).isEmpty());
|
||||
} finally {
|
||||
admin.deleteRole(b -> b.roleName(keyName));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user