mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user