diff --git a/src/main/java/io/floci/aws/services/iam/IamService.java b/src/main/java/io/floci/aws/services/iam/IamService.java index 7ff510067b..b5bb15c8f4 100644 --- a/src/main/java/io/floci/aws/services/iam/IamService.java +++ b/src/main/java/io/floci/aws/services/iam/IamService.java @@ -4861,12 +4861,24 @@ public class IamService implements SessionAccountLookup, ResourceProvider { public void registerSessionForAccount(String accountId, String sessionAccessKeyId, String secretAccessKey, String sessionToken, String roleArn, Instant expiration, String sessionPolicyDocument) { + registerSessionForAccount(accountId, sessionAccessKeyId, secretAccessKey, sessionToken, roleArn, expiration, + sessionPolicyDocument, null); + } + + /** + * Stores a temporary credential in an explicit account namespace, as a role session named {@code roleSessionName}, + * or with no name of its own when it is null. + */ + public void registerSessionForAccount(String accountId, String sessionAccessKeyId, String secretAccessKey, + String sessionToken, String roleArn, Instant expiration, + String sessionPolicyDocument, String roleSessionName) { if (accountId == null || accountId.isBlank()) { throw new IllegalArgumentException("Session account ID must not be blank"); } SessionCredential session = new SessionCredential( sessionAccessKeyId, secretAccessKey, sessionToken, roleArn, expiration, sessionPolicyDocument, accountId); + session.setRoleSessionName(roleSessionName); putSessionForAccount(accountId, sessionAccessKeyId, session); } diff --git a/src/main/java/io/floci/aws/services/identitystore/IdentityStoreService.java b/src/main/java/io/floci/aws/services/identitystore/IdentityStoreService.java index 71d330560c..d9ef747c04 100644 --- a/src/main/java/io/floci/aws/services/identitystore/IdentityStoreService.java +++ b/src/main/java/io/floci/aws/services/identitystore/IdentityStoreService.java @@ -20,6 +20,7 @@ import java.time.Instant; import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.UUID; import java.util.regex.Pattern; @@ -210,6 +211,14 @@ public class IdentityStoreService implements Resettable { return requireUser(storeId, requireResourceId(required(request, "UserId"), "UserId")); } + /** The user {@code userId} of the identity store {@code storeId}, if the store holds one. */ + public Optional findUser(String storeId, String userId) { + if (storeId == null || userId == null) { + return Optional.empty(); + } + return users.getForAccount(GLOBAL_PARTITION, userKey(storeId, userId)); + } + public synchronized void updateUser(JsonNode request) { String storeId = requireStore(required(request, "IdentityStoreId")); String userId = requireResourceId(required(request, "UserId"), "UserId"); @@ -404,8 +413,7 @@ public class IdentityStoreService implements Resettable { } private User requireUser(String storeId, String userId) { - return users.getForAccount(GLOBAL_PARTITION, userKey(storeId, userId)) - .orElseThrow(() -> notFound("User not found: " + userId)); + return findUser(storeId, userId).orElseThrow(() -> notFound("User not found: " + userId)); } private Membership requireMembership(String storeId, String membershipId) { diff --git a/src/main/java/io/floci/aws/services/ssoadmin/SsoAdminService.java b/src/main/java/io/floci/aws/services/ssoadmin/SsoAdminService.java index 1be5380e20..9236f660a9 100644 --- a/src/main/java/io/floci/aws/services/ssoadmin/SsoAdminService.java +++ b/src/main/java/io/floci/aws/services/ssoadmin/SsoAdminService.java @@ -1809,6 +1809,16 @@ public class SsoAdminService implements Resettable, StoredResourceArns { .sorted(Comparator.comparing(Assignment::principalId)).toList(); } + /** + * The user name IAM Identity Center names a user's role sessions after (the session name in + * {@code assumed-role/AWSReservedSSO_.../}), from the identity store of the instance that holds + * {@code assignment}'s permission set; empty when that store holds no such user. + */ + public Optional portalSessionName(Assignment assignment, String userId) { + SsoInstance instance = instanceForPermissionSetForPortal(assignment.permissionSetArn()); + return identityStoreService.findUser(instance.identityStoreId(), userId).map(user -> user.userName()); + } + public List portalAssignmentsForUser(String userId) { validatePrincipalId(userId); Map> groupsByIdentityStore = new HashMap<>(); diff --git a/src/main/java/io/floci/aws/services/ssoportal/SsoPortalService.java b/src/main/java/io/floci/aws/services/ssoportal/SsoPortalService.java index ff3188fa1f..348eff02de 100644 --- a/src/main/java/io/floci/aws/services/ssoportal/SsoPortalService.java +++ b/src/main/java/io/floci/aws/services/ssoportal/SsoPortalService.java @@ -112,8 +112,10 @@ public class SsoPortalService { String sessionToken = random(SECRET_CHARACTERS, 200); String roleArn = regionResolver.buildGlobalArn("iam", accountId, "role/aws-reserved/" + ServicePrincipals.of("sso") + "/AWSReservedSSO_" + roleName + "_floci"); + // IAM Identity Center names the role session after the user, as GetCallerIdentity and EKS then report it. + String sessionName = ssoAdminService.portalSessionName(assignment, session.principalId()).orElse(null); iamService.registerSessionForAccount(accountId, accessKeyId, secretAccessKey, sessionToken, - roleArn, expiration, null); + roleArn, expiration, null, sessionName); return new PortalRoleCredentials(accessKeyId, expiration.toEpochMilli(), secretAccessKey, sessionToken); } diff --git a/src/test/java/io/floci/aws/services/ssoportal/SsoPortalIntegrationTest.java b/src/test/java/io/floci/aws/services/ssoportal/SsoPortalIntegrationTest.java index 6a4caa98b8..454ab647de 100644 --- a/src/test/java/io/floci/aws/services/ssoportal/SsoPortalIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/ssoportal/SsoPortalIntegrationTest.java @@ -80,7 +80,7 @@ class SsoPortalIntegrationTest { .then().statusCode(400) .body("__type", containsString("InvalidRequestException")); - given() + String roleAccessKeyId = given() .header("x-amz-sso_bearer_token", accessToken) .queryParam("account_id", "111111111111") .queryParam("role_name", "PortalDirect" + suffix) @@ -89,7 +89,20 @@ class SsoPortalIntegrationTest { .body("roleCredentials.accessKeyId", Matchers.startsWith("ASIA")) .body("roleCredentials.secretAccessKey", Matchers.not(Matchers.emptyString())) .body("roleCredentials.sessionToken", Matchers.not(Matchers.emptyString())) - .body("roleCredentials.expiration", Matchers.greaterThan(System.currentTimeMillis())); + .body("roleCredentials.expiration", Matchers.greaterThan(System.currentTimeMillis())) + .extract().path("roleCredentials.accessKeyId"); + + // IAM Identity Center names the role session after the user. + given() + .header("Authorization", "AWS4-HMAC-SHA256 Credential=" + roleAccessKeyId + + "/20260101/us-east-1/sts/aws4_request, SignedHeaders=host, Signature=abc") + .contentType("application/x-www-form-urlencoded") + .formParam("Action", "GetCallerIdentity") + .when().post("/") + .then().statusCode(200) + .body("GetCallerIdentityResponse.GetCallerIdentityResult.Arn", Matchers.equalTo( + "arn:aws:sts::111111111111:assumed-role/AWSReservedSSO_PortalDirect" + suffix + + "_floci/portal-" + suffix + "@example.com")); given() .header("x-amz-sso_bearer_token", accessToken) diff --git a/src/test/java/io/floci/aws/services/ssoportal/SsoPortalServiceTest.java b/src/test/java/io/floci/aws/services/ssoportal/SsoPortalServiceTest.java index 11ea113c0f..a242a5288b 100644 --- a/src/test/java/io/floci/aws/services/ssoportal/SsoPortalServiceTest.java +++ b/src/test/java/io/floci/aws/services/ssoportal/SsoPortalServiceTest.java @@ -70,12 +70,15 @@ class SsoPortalServiceTest { PaginatedResult roles = service.listAccountRoles(accessToken, accountId, null, null); assertEquals("PlatformAdmins", roles.items().getFirst().roleName()); + when(ssoAdminService.portalSessionName(any(), eq(principalId))).thenReturn(Optional.of("alice@example.com")); PortalRoleCredentials credentials = service.getRoleCredentials(accessToken, accountId, "PlatformAdmins"); assertTrue(credentials.accessKeyId().startsWith("ASIA")); assertTrue(credentials.expiration() > System.currentTimeMillis()); ArgumentCaptor roleArn = ArgumentCaptor.forClass(String.class); - verify(iamService).registerSessionForAccount(eq(accountId), any(), any(), any(), roleArn.capture(), any(), eq(null)); + // The role session is named after the Identity Store user, as IAM Identity Center names it. + verify(iamService).registerSessionForAccount(eq(accountId), any(), any(), any(), roleArn.capture(), any(), eq(null), + eq("alice@example.com")); assertEquals("arn:aws-cn:iam::" + accountId + ":role/aws-reserved/sso.amazonaws.com/AWSReservedSSO_PlatformAdmins_floci", roleArn.getValue()); }