mirror of
https://github.com/floci-io/floci.git
synced 2026-11-23 22:11:36 -05:00
feat(iam): implement CreateLoginProfile, UpdateLoginProfile, DeleteLoginProfile (#3865)
* feat(iam): implement CreateLoginProfile, UpdateLoginProfile, DeleteLoginProfile GetLoginProfile previously existed as a permanent stub always returning NoSuchEntity, and the docs claimed the other three actions were supported even though the query handler had no case for them at all. Adds a LoginProfile model and the full lifecycle on IamService: a user holds at most one login profile, Create/Get/Delete default UserName to the caller's resolved identity (matching AWS), Update requires it explicitly, and Update leaves an omitted field (Password or PasswordResetRequired) unchanged rather than resetting it. The password is validated against AWS's wire character-class/length rule and, when the account has one, against the existing account password policy (PasswordPolicyViolation). Verified against the real AWS IAM API reference for all four actions' request/response shapes and error lists. * fix(iam): block DeleteUser on a login profile and carry it through UpdateUser rename Login profiles now persist, so deleteUser left an orphaned profile that a recreated user of the same name would inherit, and a rename dropped the profile. DeleteUser now returns DeleteConflict while a login profile exists, as AWS does, and UpdateUser moves the profile to the new name.
This commit is contained in:
1 parent
052da93515
commit
e190f7d340
7 files changed
+685
-16
No files matched your search
@@ -8,6 +8,7 @@ import io.github.hectorvent.floci.services.iam.model.IamPolicy;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamRole;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamUser;
|
||||
import io.github.hectorvent.floci.services.iam.model.InstanceProfile;
|
||||
import io.github.hectorvent.floci.services.iam.model.LoginProfile;
|
||||
import io.github.hectorvent.floci.services.iam.model.OpenIDConnectProvider;
|
||||
import io.github.hectorvent.floci.services.iam.model.SAMLProvider;
|
||||
import io.github.hectorvent.floci.services.iam.model.PolicyVersion;
|
||||
@@ -65,7 +66,10 @@ public class IamQueryHandler {
|
||||
case "UntagUser" -> handleUntagUser(params);
|
||||
case "ListUserTags" -> handleListUserTags(params);
|
||||
case "ListMFADevices" -> handleListMFADevices(params);
|
||||
case "GetLoginProfile" -> handleGetLoginProfile(params);
|
||||
case "CreateLoginProfile" -> handleCreateLoginProfile(params, authorization);
|
||||
case "GetLoginProfile" -> handleGetLoginProfile(params, authorization);
|
||||
case "UpdateLoginProfile" -> handleUpdateLoginProfile(params);
|
||||
case "DeleteLoginProfile" -> handleDeleteLoginProfile(params, authorization);
|
||||
|
||||
// Identity providers & server certificates
|
||||
case "ListSAMLProviders" -> handleListSAMLProviders(authorization);
|
||||
@@ -305,16 +309,48 @@ public class IamQueryHandler {
|
||||
return Response.ok(AwsQueryResponse.envelope("ListMFADevices", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
private Response handleGetLoginProfile(MultivaluedMap<String, String> params) {
|
||||
// Login profiles (console passwords) are not modeled. Per the IAM API,
|
||||
// GetLoginProfile returns NoSuchEntity (HTTP 404) when a user has no console
|
||||
// password — a documented, expected result that callers branch on. We must
|
||||
// return that exact error, not an empty 200 or an UnsupportedOperation 400,
|
||||
// which clients would treat as a real failure rather than "no profile".
|
||||
private Response handleCreateLoginProfile(MultivaluedMap<String, String> params, String authorization) {
|
||||
String userName = resolveUserName(params, authorization);
|
||||
String password = getParam(params, "Password");
|
||||
if (password == null) {
|
||||
throw new AwsException("ValidationError", "The request must contain the parameter Password.", 400);
|
||||
}
|
||||
boolean passwordResetRequired = getBooleanParam(params, "PasswordResetRequired", false);
|
||||
LoginProfile profile = iamService.createLoginProfile(userName, password, passwordResetRequired);
|
||||
String result = new XmlBuilder().start("LoginProfile").raw(loginProfileXml(profile)).end("LoginProfile").build();
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateLoginProfile", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
private Response handleGetLoginProfile(MultivaluedMap<String, String> params, String authorization) {
|
||||
String userName = resolveUserName(params, authorization);
|
||||
LoginProfile profile = iamService.getLoginProfile(userName);
|
||||
String result = new XmlBuilder().start("LoginProfile").raw(loginProfileXml(profile)).end("LoginProfile").build();
|
||||
return Response.ok(AwsQueryResponse.envelope("GetLoginProfile", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
private Response handleUpdateLoginProfile(MultivaluedMap<String, String> params) {
|
||||
String userName = getParam(params, "UserName");
|
||||
return AwsQueryResponse.error("NoSuchEntity",
|
||||
"Login Profile for User " + (userName != null ? userName : "") + " cannot be found.",
|
||||
AwsNamespaces.IAM, 404);
|
||||
if (userName == null) {
|
||||
throw new AwsException("ValidationError", "The request must contain the parameter UserName.", 400);
|
||||
}
|
||||
String password = getParam(params, "Password");
|
||||
Boolean passwordResetRequired = getOptionalBooleanParam(params, "PasswordResetRequired");
|
||||
iamService.updateLoginProfile(userName, password, passwordResetRequired);
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("UpdateLoginProfile", AwsNamespaces.IAM)).build();
|
||||
}
|
||||
|
||||
private Response handleDeleteLoginProfile(MultivaluedMap<String, String> params, String authorization) {
|
||||
String userName = resolveUserName(params, authorization);
|
||||
iamService.deleteLoginProfile(userName);
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("DeleteLoginProfile", AwsNamespaces.IAM)).build();
|
||||
}
|
||||
|
||||
private String loginProfileXml(LoginProfile profile) {
|
||||
return new XmlBuilder()
|
||||
.elem("PasswordResetRequired", profile.isPasswordResetRequired())
|
||||
.elem("UserName", profile.getUserName())
|
||||
.elem("CreateDate", isoDate(profile.getCreateDate()))
|
||||
.build();
|
||||
}
|
||||
|
||||
private Response handleListSAMLProviders(String authorization) {
|
||||
@@ -565,6 +601,15 @@ public class IamQueryHandler {
|
||||
return parseStrictBoolean(name, value);
|
||||
}
|
||||
|
||||
/** Unlike {@link #getBooleanParam}, absence is meaningful here: it must not collapse to a default. */
|
||||
private Boolean getOptionalBooleanParam(MultivaluedMap<String, String> params, String name) {
|
||||
String value = params.getFirst(name);
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return parseStrictBoolean(name, value);
|
||||
}
|
||||
|
||||
private boolean parseStrictBoolean(String name, String value) {
|
||||
if ("true".equalsIgnoreCase(value)) {
|
||||
return true;
|
||||
|
||||
@@ -19,6 +19,7 @@ import io.github.hectorvent.floci.services.iam.model.IamPolicy;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamRole;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamUser;
|
||||
import io.github.hectorvent.floci.services.iam.model.InstanceProfile;
|
||||
import io.github.hectorvent.floci.services.iam.model.LoginProfile;
|
||||
import io.github.hectorvent.floci.services.iam.model.OpenIDConnectProvider;
|
||||
import io.github.hectorvent.floci.services.iam.model.OrganizationRootFeatures;
|
||||
import io.github.hectorvent.floci.services.iam.model.PolicyVersion;
|
||||
@@ -79,6 +80,12 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
private static final int MAX_PASSWORD_AGE_CEILING = 1095;
|
||||
private static final int PASSWORD_REUSE_PREVENTION_FLOOR = 1;
|
||||
private static final int PASSWORD_REUSE_PREVENTION_CEILING = 24;
|
||||
/**
|
||||
* AWS's documented {@code passwordType} pattern for Create/UpdateLoginProfile: printable
|
||||
* ASCII and Latin-1 Supplement (space through code point 0xFF), plus tab/LF/CR, 1-128 chars.
|
||||
*/
|
||||
private static final Pattern LOGIN_PROFILE_PASSWORD_PATTERN =
|
||||
Pattern.compile("[\\t\\n\\r\\x20-\\xff]{1,128}");
|
||||
|
||||
/** Guards the read-modify-write in the OIDC provider mutators. */
|
||||
private final Object oidcProviderLock = new Object();
|
||||
@@ -131,6 +138,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
* single-value-per-account shape as {@link #accountAliases}.
|
||||
*/
|
||||
private final StorageBackend<String, AccountPasswordPolicy> passwordPolicies;
|
||||
private final StorageBackend<String, LoginProfile> loginProfiles;
|
||||
private final StorageBackend<String, OpenIDConnectProvider> oidcProviders;
|
||||
/** Deletion is synchronous, so an issued task id is a completed one; the value is its role. */
|
||||
private final StorageBackend<String, String> serviceLinkedRoleDeletions;
|
||||
@@ -158,6 +166,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
storageFactory.create("iam", "iam-sessions.json", new TypeReference<>() {}),
|
||||
storageFactory.create("iam", "iam-account-aliases.json", new TypeReference<>() {}),
|
||||
storageFactory.create("iam", "iam-password-policy.json", new TypeReference<>() {}),
|
||||
storageFactory.create("iam", "iam-login-profiles.json", new TypeReference<>() {}),
|
||||
storageFactory.create("iam", "iam-oidc-providers.json", new TypeReference<>() {}),
|
||||
storageFactory.create("iam", "iam-slr-deletions.json", new TypeReference<>() {}),
|
||||
storageFactory.create("iam", "iam-org-root-features.json", new TypeReference<>() {}),
|
||||
@@ -189,7 +198,8 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
boolean seedDeployerPrincipal) {
|
||||
this(users, groups, roles, policies, accessKeys, instanceProfiles, sessions,
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), regionResolver, seedDeployerPrincipal, null);
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
regionResolver, seedDeployerPrincipal, null);
|
||||
}
|
||||
|
||||
// 8-backend constructor (no org-root-features): kept for existing callers/tests;
|
||||
@@ -209,8 +219,9 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
boolean seedDeployerPrincipal,
|
||||
String seededAccountAlias) {
|
||||
this(users, groups, roles, policies, accessKeys, instanceProfiles, sessions,
|
||||
accountAliases, passwordPolicies, oidcProviders, serviceLinkedRoleDeletions,
|
||||
new InMemoryStorage<>(), regionResolver, seedDeployerPrincipal, seededAccountAlias);
|
||||
accountAliases, passwordPolicies, new InMemoryStorage<>(), oidcProviders,
|
||||
serviceLinkedRoleDeletions, new InMemoryStorage<>(), regionResolver,
|
||||
seedDeployerPrincipal, seededAccountAlias);
|
||||
}
|
||||
|
||||
// 9-backend constructor (no alias/OIDC/SLR backends): kept for existing callers/tests;
|
||||
@@ -228,7 +239,8 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
boolean seedDeployerPrincipal) {
|
||||
this(users, groups, roles, policies, accessKeys, instanceProfiles, sessions,
|
||||
new InMemoryStorage<>(), passwordPolicies, new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), orgRootFeatures, regionResolver, seedDeployerPrincipal, null);
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), orgRootFeatures, regionResolver,
|
||||
seedDeployerPrincipal, null);
|
||||
}
|
||||
|
||||
IamService(StorageBackend<String, IamUser> users,
|
||||
@@ -240,6 +252,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
StorageBackend<String, SessionCredential> sessions,
|
||||
StorageBackend<String, String> accountAliases,
|
||||
StorageBackend<String, AccountPasswordPolicy> passwordPolicies,
|
||||
StorageBackend<String, LoginProfile> loginProfiles,
|
||||
StorageBackend<String, OpenIDConnectProvider> oidcProviders,
|
||||
StorageBackend<String, String> serviceLinkedRoleDeletions,
|
||||
StorageBackend<String, OrganizationRootFeatures> orgRootFeatures,
|
||||
@@ -255,6 +268,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
this.sessions = sessions;
|
||||
this.accountAliases = accountAliases;
|
||||
this.passwordPolicies = passwordPolicies;
|
||||
this.loginProfiles = loginProfiles;
|
||||
this.oidcProviders = oidcProviders;
|
||||
this.serviceLinkedRoleDeletions = serviceLinkedRoleDeletions;
|
||||
this.orgRootFeatures = orgRootFeatures;
|
||||
@@ -393,6 +407,10 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
throw new AwsException("DeleteConflict",
|
||||
"Cannot delete entity, must remove from all groups first.", 409);
|
||||
}
|
||||
if (loginProfiles.get(userName).isPresent()) {
|
||||
throw new AwsException("DeleteConflict",
|
||||
"Cannot delete entity, must delete login profile first.", 409);
|
||||
}
|
||||
users.delete(userName);
|
||||
LOG.infov("Deleted IAM user: {0}", userName);
|
||||
}
|
||||
@@ -430,6 +448,11 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
if (newPath != null) user.setPath(normalizePath(newPath));
|
||||
user.setArn(iamArn("user", user.getPath(), newUserName));
|
||||
users.put(newUserName, user);
|
||||
loginProfiles.get(userName).ifPresent(profile -> {
|
||||
loginProfiles.delete(userName);
|
||||
profile.setUserName(newUserName);
|
||||
loginProfiles.put(newUserName, profile);
|
||||
});
|
||||
} else {
|
||||
if (newPath != null) {
|
||||
user.setPath(normalizePath(newPath));
|
||||
@@ -1635,6 +1658,100 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
}
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Login Profiles
|
||||
// =========================================================================
|
||||
|
||||
/** A user holds at most one login profile: creating a second is EntityAlreadyExists. */
|
||||
public LoginProfile createLoginProfile(String userName, String password, boolean passwordResetRequired) {
|
||||
getUser(userName);
|
||||
if (loginProfiles.get(userName).isPresent()) {
|
||||
throw new AwsException("EntityAlreadyExists",
|
||||
"Login Profile for User " + userName + " already exists.", 409);
|
||||
}
|
||||
validateLoginProfilePassword(password);
|
||||
LoginProfile profile = new LoginProfile(userName, password, passwordResetRequired);
|
||||
loginProfiles.put(userName, profile);
|
||||
LOG.infov("Created IAM login profile for user: {0}", userName);
|
||||
return profile;
|
||||
}
|
||||
|
||||
public LoginProfile getLoginProfile(String userName) {
|
||||
getUser(userName);
|
||||
return loginProfiles.get(userName)
|
||||
.orElseThrow(() -> new AwsException("NoSuchEntity",
|
||||
"Login Profile for User " + userName + " cannot be found.", 404));
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the fields the caller supplies; unlike {@link #updateAccountPasswordPolicy}, an
|
||||
* omitted field here carries over its previous value rather than resetting to a default.
|
||||
* {@code Password} and {@code PasswordResetRequired} are independently optional on AWS's
|
||||
* {@code UpdateLoginProfile}, so a caller rotating only the password must not accidentally
|
||||
* clear the reset-required flag, and vice versa.
|
||||
*/
|
||||
public void updateLoginProfile(String userName, String password, Boolean passwordResetRequired) {
|
||||
LoginProfile profile = getLoginProfile(userName);
|
||||
if (password != null) {
|
||||
validateLoginProfilePassword(password);
|
||||
profile.setPassword(password);
|
||||
}
|
||||
if (passwordResetRequired != null) {
|
||||
profile.setPasswordResetRequired(passwordResetRequired);
|
||||
}
|
||||
loginProfiles.put(userName, profile);
|
||||
LOG.infov("Updated IAM login profile for user: {0}", userName);
|
||||
}
|
||||
|
||||
public void deleteLoginProfile(String userName) {
|
||||
getUser(userName);
|
||||
if (loginProfiles.get(userName).isEmpty()) {
|
||||
throw new AwsException("NoSuchEntity",
|
||||
"Login Profile for User " + userName + " cannot be found.", 404);
|
||||
}
|
||||
loginProfiles.delete(userName);
|
||||
LOG.infov("Deleted IAM login profile for user: {0}", userName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Base wire validation shared by Create/UpdateLoginProfile, then, only when the account has
|
||||
* ever set one (matching {@link #getAccountPasswordPolicy}'s own NoSuchEntity-by-default
|
||||
* semantics), the account's password policy.
|
||||
*/
|
||||
private void validateLoginProfilePassword(String password) {
|
||||
if (password == null || !LOGIN_PROFILE_PASSWORD_PATTERN.matcher(password).matches()) {
|
||||
throw new AwsException("ValidationError",
|
||||
"1 validation error detected: Value at 'password' failed to satisfy constraint: "
|
||||
+ "Member must have length less than or equal to 128 and greater than or "
|
||||
+ "equal to 1", 400);
|
||||
}
|
||||
getAccountPasswordPolicy().ifPresent(policy -> checkPasswordAgainstPolicy(password, policy));
|
||||
}
|
||||
|
||||
private void checkPasswordAgainstPolicy(String password, AccountPasswordPolicy policy) {
|
||||
List<String> violations = new ArrayList<>();
|
||||
if (password.length() < policy.getMinimumPasswordLength()) {
|
||||
violations.add("a minimum length of " + policy.getMinimumPasswordLength());
|
||||
}
|
||||
if (policy.isRequireUppercaseCharacters() && password.chars().noneMatch(Character::isUpperCase)) {
|
||||
violations.add("at least one uppercase letter");
|
||||
}
|
||||
if (policy.isRequireLowercaseCharacters() && password.chars().noneMatch(Character::isLowerCase)) {
|
||||
violations.add("at least one lowercase letter");
|
||||
}
|
||||
if (policy.isRequireNumbers() && password.chars().noneMatch(Character::isDigit)) {
|
||||
violations.add("at least one number");
|
||||
}
|
||||
if (policy.isRequireSymbols() && password.chars().allMatch(Character::isLetterOrDigit)) {
|
||||
violations.add("at least one non-alphanumeric character");
|
||||
}
|
||||
if (!violations.isEmpty()) {
|
||||
throw new AwsException("PasswordPolicyViolation",
|
||||
"Password does not conform to the account password policy. It must contain: "
|
||||
+ String.join(", ", violations) + ".", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private void validateIamResourceName(String value, String paramName) {
|
||||
if (value == null || !IAM_RESOURCE_NAME_PATTERN.matcher(value).matches()) {
|
||||
throw new AwsException("ValidationError",
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package io.github.hectorvent.floci.services.iam.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
/**
|
||||
* A user's console (password) login profile. At most one per user, mirroring AWS: creating
|
||||
* one when the user already has one is {@code EntityAlreadyExists}, and a user with no
|
||||
* console password simply has no entry here. The password is stored in plain text, matching
|
||||
* how {@link AccessKey} already stores its secret: this is an emulator, not a credential
|
||||
* store, and AWS itself never echoes the password back on any of these actions.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class LoginProfile {
|
||||
|
||||
private String userName;
|
||||
private String password;
|
||||
private boolean passwordResetRequired;
|
||||
private Instant createDate;
|
||||
|
||||
public LoginProfile() {}
|
||||
|
||||
public LoginProfile(String userName, String password, boolean passwordResetRequired) {
|
||||
this.userName = userName;
|
||||
this.password = password;
|
||||
this.passwordResetRequired = passwordResetRequired;
|
||||
this.createDate = Instant.now();
|
||||
}
|
||||
|
||||
public String getUserName() { return userName; }
|
||||
public void setUserName(String userName) { this.userName = userName; }
|
||||
|
||||
public String getPassword() { return password; }
|
||||
public void setPassword(String password) { this.password = password; }
|
||||
|
||||
public boolean isPasswordResetRequired() { return passwordResetRequired; }
|
||||
public void setPasswordResetRequired(boolean passwordResetRequired) {
|
||||
this.passwordResetRequired = passwordResetRequired;
|
||||
}
|
||||
|
||||
public Instant getCreateDate() { return createDate; }
|
||||
public void setCreateDate(Instant createDate) { this.createDate = createDate; }
|
||||
}
|
||||
Reference in new issue
Block a user