mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(ssoportal): name IAM Identity Center role sessions after the user (#8162)
* fix(ssoportal): name IAM Identity Center role sessions after the user * refactor(identitystore): read a user through findUser in one place
This commit is contained in:
1 parent
25811dd4c6
commit
5c08fff5d7
6 files changed
+54
-6
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<User> 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) {
|
||||
|
||||
@@ -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_.../<user name>}), from the identity store of the instance that holds
|
||||
* {@code assignment}'s permission set; empty when that store holds no such user.
|
||||
*/
|
||||
public Optional<String> portalSessionName(Assignment assignment, String userId) {
|
||||
SsoInstance instance = instanceForPermissionSetForPortal(assignment.permissionSetArn());
|
||||
return identityStoreService.findUser(instance.identityStoreId(), userId).map(user -> user.userName());
|
||||
}
|
||||
|
||||
public List<Assignment> portalAssignmentsForUser(String userId) {
|
||||
validatePrincipalId(userId);
|
||||
Map<String, Set<String>> groupsByIdentityStore = new HashMap<>();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -70,12 +70,15 @@ class SsoPortalServiceTest {
|
||||
PaginatedResult<PortalRoleInfo> 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<String> 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());
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user