mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(iam): finish the SAML provider lifecycle (#4360)
Adds UpdateSAMLProvider, DeleteSAMLProvider, TagSAMLProvider, UntagSAMLProvider and ListSAMLProviderTags, and adds tags to GetSAMLProvider. UpdateSAMLProvider re-parses a new SAMLMetadataDocument the same way CreateSAMLProvider does and replaces the stored entity ID and certificate. The document is optional on the request: omitting it (metadata == null) is a no-op, but an explicitly empty one is invalid input, the same as CreateSAMLProvider already rejects it as; the two are distinguishable at the wire level and were previously treated the same. The update replaces the stored provider with a newly built one rather than mutating the existing, already-published object's fields one at a time: assertion verification (StsQueryHandler) reads a provider via find() / findForAccount() without taking providerLock, so setting entityId and certificate as two separate writes on the live object could let it observe one field updated and the other not. Publishing a fully-built replacement through the same ConcurrentHashMap-backed store means any reader, synchronized or not, sees either the old provider intact or the new one intact, never a mix. DeleteSAMLProvider's existence check now runs inside the same lock as the delete itself, not before it: two concurrent deletes could otherwise both pass the check before either removed the provider, so the second returned success instead of NoSuchEntity, and a delete racing a recreate of the same ARN could remove the newly created provider instead of the deleted one. DeleteSAMLProvider itself does not check or update any role whose trust policy still references the provider's ARN. This corrects an assumption from the original gap-report plan (which expected a delete-blocked-while- referenced check, by analogy with DeletePolicy's attachment guard): AWS's own docs say the opposite, that deletion always succeeds and it is a later AssumeRoleWithSAML against the now-dangling ARN that fails, not this call. GetSAMLProvider and ListSAMLProviderTags both sort their tags by key, matching AWS's own documented behavior for both, which the same wording in ListInstanceProfileTags's docs already established the precedent for. Also completes AwsQueryController's IAM_ACTIONS fallback set for every SAML provider action, including the three pre-existing ones (ListSAMLProviders, CreateSAMLProvider, GetSAMLProvider), none of which were there before, and adds SDK-client coverage for the whole SAML provider lifecycle to compatibility-tests/sdk-test-java/IamTest.java, which (like the three pre-existing SAML actions) had none.
This commit is contained in:
1 parent
61e7f07fb6
commit
3d35eabf51
7 files changed
+709
-20
No files matched your search
@@ -107,7 +107,10 @@ public class AwsQueryController {
|
||||
"GenerateCredentialReport", "GetCredentialReport",
|
||||
"GetAccountSummary", "GetAccountAuthorizationDetails",
|
||||
"SimulatePrincipalPolicy", "SimulateCustomPolicy",
|
||||
"GetContextKeysForCustomPolicy", "GetContextKeysForPrincipalPolicy"
|
||||
"GetContextKeysForCustomPolicy", "GetContextKeysForPrincipalPolicy",
|
||||
"ListSAMLProviders", "CreateSAMLProvider", "GetSAMLProvider",
|
||||
"UpdateSAMLProvider", "DeleteSAMLProvider",
|
||||
"TagSAMLProvider", "UntagSAMLProvider", "ListSAMLProviderTags"
|
||||
);
|
||||
|
||||
private static final Set<String> AUTOSCALING_ACTIONS = Set.of(
|
||||
|
||||
@@ -76,6 +76,11 @@ public class IamQueryHandler {
|
||||
case "ListSAMLProviders" -> handleListSAMLProviders(authorization);
|
||||
case "CreateSAMLProvider" -> handleCreateSAMLProvider(params, authorization);
|
||||
case "GetSAMLProvider" -> handleGetSAMLProvider(params, authorization);
|
||||
case "UpdateSAMLProvider" -> handleUpdateSAMLProvider(params, authorization);
|
||||
case "DeleteSAMLProvider" -> handleDeleteSAMLProvider(params, authorization);
|
||||
case "TagSAMLProvider" -> handleTagSAMLProvider(params, authorization);
|
||||
case "UntagSAMLProvider" -> handleUntagSAMLProvider(params, authorization);
|
||||
case "ListSAMLProviderTags" -> handleListSAMLProviderTags(params, authorization);
|
||||
case "ListOpenIDConnectProviders" -> handleListOpenIDConnectProviders(params);
|
||||
case "CreateOpenIDConnectProvider" -> handleCreateOpenIDConnectProvider(params);
|
||||
case "GetOpenIDConnectProvider" -> handleGetOpenIDConnectProvider(params);
|
||||
@@ -371,10 +376,12 @@ public class IamQueryHandler {
|
||||
}
|
||||
|
||||
private Response handleCreateSAMLProvider(MultivaluedMap<String, String> params, String authorization) {
|
||||
checkSamlTagMembers(params);
|
||||
SAMLProvider provider = samlProviderService.create(accountResolver.resolve(authorization),
|
||||
getParam(params, "Name"), getParam(params, "SAMLMetadataDocument"));
|
||||
getParam(params, "Name"), getParam(params, "SAMLMetadataDocument"), extractTags(params));
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateSAMLProvider", AwsNamespaces.IAM,
|
||||
new XmlBuilder().elem("SAMLProviderArn", provider.getArn()).build())).build();
|
||||
new XmlBuilder().elem("SAMLProviderArn", provider.getArn())
|
||||
.raw(tagsElement(new TreeMap<>(provider.getTags()))).build())).build();
|
||||
}
|
||||
|
||||
private Response handleGetSAMLProvider(MultivaluedMap<String, String> params, String authorization) {
|
||||
@@ -385,7 +392,44 @@ public class IamQueryHandler {
|
||||
+ provider.getCertificate() + "</ds:X509Certificate></ds:X509Data></ds:KeyInfo></md:KeyDescriptor></md:EntityDescriptor>";
|
||||
return Response.ok(AwsQueryResponse.envelope("GetSAMLProvider", AwsNamespaces.IAM,
|
||||
new XmlBuilder().elem("SAMLProviderArn", provider.getArn()).elem("CreateDate", isoDate(provider.getCreateDate()))
|
||||
.elem("ValidUntil", isoDate(provider.getCreateDate().plusSeconds(31536000))).elem("SAMLMetadataDocument", metadata).build())).build();
|
||||
.elem("ValidUntil", isoDate(provider.getCreateDate().plusSeconds(31536000))).elem("SAMLMetadataDocument", metadata)
|
||||
.raw(tagsElement(new TreeMap<>(provider.getTags()))).build())).build();
|
||||
}
|
||||
|
||||
private Response handleUpdateSAMLProvider(MultivaluedMap<String, String> params, String authorization) {
|
||||
String accountId = accountResolver.resolve(authorization);
|
||||
SAMLProvider provider = samlProviderService.update(accountId,
|
||||
getParam(params, "SAMLProviderArn"), getParam(params, "SAMLMetadataDocument"));
|
||||
return Response.ok(AwsQueryResponse.envelope("UpdateSAMLProvider", AwsNamespaces.IAM,
|
||||
new XmlBuilder().elem("SAMLProviderArn", provider.getArn()).build())).build();
|
||||
}
|
||||
|
||||
private Response handleDeleteSAMLProvider(MultivaluedMap<String, String> params, String authorization) {
|
||||
String accountId = accountResolver.resolve(authorization);
|
||||
samlProviderService.delete(accountId, getParam(params, "SAMLProviderArn"));
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("DeleteSAMLProvider", AwsNamespaces.IAM)).build();
|
||||
}
|
||||
|
||||
private Response handleTagSAMLProvider(MultivaluedMap<String, String> params, String authorization) {
|
||||
String accountId = accountResolver.resolve(authorization);
|
||||
checkSamlTagMembers(params);
|
||||
samlProviderService.tag(accountId, getParam(params, "SAMLProviderArn"), extractTags(params));
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("TagSAMLProvider", AwsNamespaces.IAM)).build();
|
||||
}
|
||||
|
||||
private Response handleUntagSAMLProvider(MultivaluedMap<String, String> params, String authorization) {
|
||||
String accountId = accountResolver.resolve(authorization);
|
||||
samlProviderService.untag(accountId, getParam(params, "SAMLProviderArn"), extractTagKeys(params));
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("UntagSAMLProvider", AwsNamespaces.IAM)).build();
|
||||
}
|
||||
|
||||
private Response handleListSAMLProviderTags(MultivaluedMap<String, String> params, String authorization) {
|
||||
String accountId = accountResolver.resolve(authorization);
|
||||
Map<String, String> tags = new TreeMap<>(
|
||||
samlProviderService.listTags(accountId, getParam(params, "SAMLProviderArn")));
|
||||
String result = new XmlBuilder().start("Tags").raw(tagsXml(tags)).end("Tags")
|
||||
.elem("IsTruncated", false).build();
|
||||
return Response.ok(AwsQueryResponse.envelope("ListSAMLProviderTags", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
// ListOpenIDConnectProviders is not paginated and carries only ARNs — the client fetches
|
||||
@@ -1660,6 +1704,23 @@ public class IamQueryHandler {
|
||||
// Parameter parsing helpers
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* Enforces {@code tagListType}'s {@code max: 50} on the members as sent. extractTags collapses
|
||||
* repeated keys into a map, and AWS resolves a repeated key by overwriting rather than
|
||||
* rejecting, so counting the map would let a 51-member list through whenever any key repeats.
|
||||
*/
|
||||
private void checkSamlTagMembers(MultivaluedMap<String, String> params) {
|
||||
int members = 0;
|
||||
while (params.getFirst("Tags.member." + (members + 1) + ".Key") != null) {
|
||||
members++;
|
||||
}
|
||||
if (members > SAMLProviderService.MAX_TAGS_PER_SAML_PROVIDER) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Value at 'tags' failed to satisfy constraint: Member must have length "
|
||||
+ "less than or equal to " + SAMLProviderService.MAX_TAGS_PER_SAML_PROVIDER, 400);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> extractTags(MultivaluedMap<String, String> params) {
|
||||
Map<String, String> tags = new HashMap<>();
|
||||
for (int i = 1; ; i++) {
|
||||
|
||||
@@ -11,7 +11,9 @@ import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -20,6 +22,8 @@ import java.util.regex.Pattern;
|
||||
public class SAMLProviderService {
|
||||
private static final Pattern ARN = Pattern.compile(
|
||||
"^arn:" + AwsArnUtils.PARTITION_REGEX + ":iam::(\\d{12}):saml-provider/[A-Za-z0-9+=,.@_-]{1,128}$");
|
||||
/** {@code tagListType} / {@code tagKeyListType} are both {@code max: 50}, as elsewhere in IAM. */
|
||||
static final int MAX_TAGS_PER_SAML_PROVIDER = 50;
|
||||
private final StorageBackend<String, SAMLProvider> providers;
|
||||
private final Object providerLock = new Object();
|
||||
|
||||
@@ -32,7 +36,7 @@ public class SAMLProviderService {
|
||||
this.providers = providers;
|
||||
}
|
||||
|
||||
public SAMLProvider create(String accountId, String name, String metadata) {
|
||||
public SAMLProvider create(String accountId, String name, String metadata, Map<String, String> tags) {
|
||||
String arn = "arn:aws:iam::" + accountId + ":saml-provider/" + name;
|
||||
if (!ARN.matcher(arn).matches()) {
|
||||
throw new AwsException("InvalidInput", "Invalid SAML provider name.", 400);
|
||||
@@ -40,6 +44,15 @@ public class SAMLProviderService {
|
||||
if (metadata == null || metadata.isBlank()) {
|
||||
throw new AwsException("InvalidInput", "SAML metadata document must not be empty.", 400);
|
||||
}
|
||||
// Checked before anything is stored: AWS documents that if any tag is invalid or the list
|
||||
// exceeds the maximum, "the entire request fails and the resource is not created". The
|
||||
// sent-list length is a wire-shape rule enforced in IamQueryHandler, which can still see
|
||||
// the members before duplicate keys collapse; what is left here is the quota on what the
|
||||
// provider would actually end up holding.
|
||||
if (tags != null && tags.size() > MAX_TAGS_PER_SAML_PROVIDER) {
|
||||
throw new AwsException("LimitExceeded",
|
||||
"Cannot exceed quota for TagsPerSAMLProvider: " + MAX_TAGS_PER_SAML_PROVIDER, 409);
|
||||
}
|
||||
SAMLMetadata.Parsed parsed;
|
||||
try {
|
||||
parsed = SAMLMetadata.parse(metadata);
|
||||
@@ -50,16 +63,13 @@ public class SAMLProviderService {
|
||||
provider.setArn(arn);
|
||||
provider.setEntityId(parsed.entityId());
|
||||
provider.setCertificate(parsed.certificateBase64());
|
||||
provider.setTags(tags);
|
||||
synchronized (providerLock) {
|
||||
if (findForAccount(accountId, arn).isPresent()) {
|
||||
throw new AwsException("EntityAlreadyExists",
|
||||
"SAML provider " + arn + " already exists.", 409);
|
||||
}
|
||||
if (providers instanceof AccountAwareStorageBackend<SAMLProvider> aware) {
|
||||
aware.putForAccount(accountId, arn, provider);
|
||||
} else {
|
||||
providers.put(arn, provider);
|
||||
}
|
||||
putBack(accountId, arn, provider);
|
||||
}
|
||||
return provider;
|
||||
}
|
||||
@@ -95,6 +105,111 @@ public class SAMLProviderService {
|
||||
"The SAML provider with ARN " + arn + " cannot be found.", 404));
|
||||
}
|
||||
|
||||
/**
|
||||
* Replaces the provider's metadata document when one is given. AWS's current
|
||||
* UpdateSAMLProvider also manages an {@code AssertionEncryptionMode} and a private-key
|
||||
* list for decrypting encrypted assertions; Floci's assertion verifier only checks
|
||||
* signatures against a single certificate and does not model encrypted assertions at all,
|
||||
* so those inputs are not modeled here either.
|
||||
*/
|
||||
public SAMLProvider update(String accountId, String arn, String metadata) {
|
||||
if (metadata == null) {
|
||||
return getForAccount(accountId, arn);
|
||||
}
|
||||
// Request shape (parsing) before the read-modify-write, matching tag/untag's order.
|
||||
// An explicitly empty document is invalid input, the same as CreateSAMLProvider already
|
||||
// rejects it as; only an omitted document (metadata == null, above) is a no-op.
|
||||
if (metadata.isBlank()) {
|
||||
throw new AwsException("InvalidInput", "SAML metadata document must not be empty.", 400);
|
||||
}
|
||||
SAMLMetadata.Parsed parsed;
|
||||
try {
|
||||
parsed = SAMLMetadata.parse(metadata);
|
||||
} catch (Exception e) {
|
||||
throw new AwsException("InvalidInput", "The SAML metadata document is invalid.", 400);
|
||||
}
|
||||
synchronized (providerLock) {
|
||||
SAMLProvider existing = getForAccount(accountId, arn);
|
||||
// A new object, not an in-place mutation of the existing one: assertion verification
|
||||
// (StsQueryHandler) reads a provider via find()/findForAccount() without taking this
|
||||
// lock, so setting entityId and certificate as two separate writes on the live,
|
||||
// already-published object could let it observe one field updated and the other not.
|
||||
// Publishing a fully-built replacement through the same ConcurrentHashMap-backed
|
||||
// store means any reader, synchronized or not, sees either the old provider intact
|
||||
// or the new one intact, never a mix.
|
||||
SAMLProvider updated = new SAMLProvider();
|
||||
updated.setArn(existing.getArn());
|
||||
updated.setCreateDate(existing.getCreateDate());
|
||||
updated.setTags(existing.getTags());
|
||||
updated.setEntityId(parsed.entityId());
|
||||
updated.setCertificate(parsed.certificateBase64());
|
||||
putBack(accountId, arn, updated);
|
||||
return updated;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deleting the provider does not check or update any role that references its ARN in a
|
||||
* trust policy: AWS documents that deletion succeeds regardless, and that an AssumeRole
|
||||
* against the now-dangling ARN is what fails afterward, not this call.
|
||||
*/
|
||||
public void delete(String accountId, String arn) {
|
||||
// Existence check under the same lock as the delete: two concurrent deletes must not
|
||||
// both pass the check before either removes the provider, and a delete racing a
|
||||
// recreate of the same ARN must not remove the newly created one.
|
||||
synchronized (providerLock) {
|
||||
getForAccount(accountId, arn);
|
||||
if (providers instanceof AccountAwareStorageBackend<SAMLProvider> aware) {
|
||||
aware.deleteForAccount(accountId, arn);
|
||||
} else {
|
||||
providers.delete(arn);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void tag(String accountId, String arn, Map<String, String> newTags) {
|
||||
synchronized (providerLock) {
|
||||
SAMLProvider provider = getForAccount(accountId, arn);
|
||||
Map<String, String> merged = new LinkedHashMap<>(provider.getTags());
|
||||
merged.putAll(newTags == null ? Map.of() : newTags);
|
||||
if (merged.size() > MAX_TAGS_PER_SAML_PROVIDER) {
|
||||
throw new AwsException("LimitExceeded",
|
||||
"Cannot exceed quota for TagsPerSAMLProvider: " + MAX_TAGS_PER_SAML_PROVIDER, 409);
|
||||
}
|
||||
provider.setTags(merged);
|
||||
putBack(accountId, arn, provider);
|
||||
}
|
||||
}
|
||||
|
||||
public void untag(String accountId, String arn, List<String> tagKeys) {
|
||||
if (tagKeys != null && tagKeys.size() > MAX_TAGS_PER_SAML_PROVIDER) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Value at 'tagKeys' failed to satisfy constraint: Member must have length "
|
||||
+ "less than or equal to " + MAX_TAGS_PER_SAML_PROVIDER, 400);
|
||||
}
|
||||
synchronized (providerLock) {
|
||||
SAMLProvider provider = getForAccount(accountId, arn);
|
||||
Map<String, String> remaining = new LinkedHashMap<>(provider.getTags());
|
||||
if (tagKeys != null) {
|
||||
tagKeys.forEach(remaining::remove);
|
||||
}
|
||||
provider.setTags(remaining);
|
||||
putBack(accountId, arn, provider);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> listTags(String accountId, String arn) {
|
||||
return getForAccount(accountId, arn).getTags();
|
||||
}
|
||||
|
||||
private void putBack(String accountId, String arn, SAMLProvider provider) {
|
||||
if (providers instanceof AccountAwareStorageBackend<SAMLProvider> aware) {
|
||||
aware.putForAccount(accountId, arn, provider);
|
||||
} else {
|
||||
providers.put(arn, provider);
|
||||
}
|
||||
}
|
||||
|
||||
/** Metadata parser shared by provider registration and the assertion verifier. */
|
||||
static final class SAMLMetadata {
|
||||
private SAMLMetadata() {
|
||||
|
||||
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
@@ -12,6 +14,7 @@ public class SAMLProvider {
|
||||
private String entityId;
|
||||
private String certificate;
|
||||
private Instant createDate = Instant.now();
|
||||
private Map<String, String> tags = new ConcurrentHashMap<>();
|
||||
|
||||
public SAMLProvider() {}
|
||||
|
||||
@@ -23,4 +26,8 @@ public class SAMLProvider {
|
||||
public void setCertificate(String certificate) { this.certificate = certificate; }
|
||||
public Instant getCreateDate() { return createDate; }
|
||||
public void setCreateDate(Instant createDate) { this.createDate = createDate; }
|
||||
public Map<String, String> getTags() { return tags; }
|
||||
public void setTags(Map<String, String> tags) {
|
||||
this.tags = tags == null ? new ConcurrentHashMap<>() : new ConcurrentHashMap<>(tags);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user