mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
feat(iam): support OIDC identity providers (#2106)
* feat(iam): support OIDC identity providers Implement CreateOpenIDConnectProvider, GetOpenIDConnectProvider, DeleteOpenIDConnectProvider, AddClientIDToOpenIDConnectProvider, RemoveClientIDFromOpenIDConnectProvider, UpdateOpenIDConnectProviderThumbprint and the provider tag actions, and back ListOpenIDConnectProviders with real data instead of a fixed empty list. A provider is identified by its URL, so the ARN is derived from it and the scheme is stripped: GetOpenIDConnectProvider reports the URL without https://, matching AWS. Creating the same URL twice returns EntityAlreadyExists. Thumbprints are stored and echoed back but never validated against the remote endpoint, since nothing here performs the TLS handshake they describe. Closes #2102 * test(iam): cover OIDC provider persistence and cap the provider URL Add IamServicePersistenceTest: IAM state is persisted through StorageFactory, so a restart must preserve providers along with their client IDs, thumbprints, tags and creation time. The model carries an Instant and two lists, so a broken serialization round trip would surface here first. Also enforce the documented 255-character limit on the provider URL, which was otherwise unchecked and would have produced an oversized ARN. * fix(iam): match verified AWS behaviour for OIDC providers Checked the emulated surface against a live AWS account and corrected where it diverged: AddClientIDToOpenIDConnectProvider and RemoveClientIDFromOpenIDConnectProvider are idempotent on AWS. Adding a client ID that is already present, and removing one that was never added, both succeed and change nothing; they previously raised EntityAlreadyExists and NoSuchEntity. Exceeding the thumbprint limit reports InvalidInput, not LimitExceeded. The client ID quota and duplicate-provider messages now use the AWS wording, and DeleteOpenIDConnectProvider reports its own not-found phrasing. Confirmed unchanged: the ARN is derived from the scheme-less URL, Get echoes the URL without its scheme, the URL is capped at 255 characters, the client ID quota is 100 with LimitExceeded, and URLs are not normalized, so a trailing slash or a case difference yields a separate provider. * fix(iam): reject missing required parameters on OIDC mutators A null ClientID read as "not in the client ID list", so RemoveClientIDFromOpenIDConnectProvider reported a no-op success for a request that never named one. DeleteOpenIDConnectProvider had lost its ARN check when it stopped routing through getOpenIDConnectProvider to report the not-found wording AWS uses for that action, so a request with no ARN came back as NoSuchEntity rather than a required-parameter error. Route every OIDC mutator through shared ARN and client ID checks so a malformed request fails as ValidationError before any lookup. * fix(iam): unbreak the reflective test fixture for the new constructor arity IamServiceTestHelper resolves the package-private IamService constructor reflectively against a hardcoded parameter list, so the extra storage backend broke it at run time with nothing failing at compile time. Every test using the fixture errored, and one of them blocks on a socket read rather than failing, which exhausted the CI job before it could report. Extend both lookups and both newInstance calls to the new arity. * fix(iam): reject empty tag collections on OIDC providers TagOpenIDConnectProvider with no Tags and UntagOpenIDConnectProvider with no TagKeys performed a no-op mutation and returned 200. AWS rejects both, as InvalidInput rather than ValidationError, with distinct messages for the map and the key list; checked against a live account. Validate the ARN on the tag path too, so a missing one fails before the lookup like the other mutators. --------- Co-authored-by: Michal Schreiber <michal.p.schreiber@gmail.com> Co-authored-by: Michal Schreiber <michal.schreiber@benefitsystems.pl>
This commit is contained in:
9 files changed
+1092
-6
No files matched your search
@@ -84,3 +84,98 @@ teardown() {
|
||||
run aws_cmd iam delete-role --role-name "$ROLE_NAME"
|
||||
assert_success
|
||||
}
|
||||
|
||||
# ─── OIDC identity providers ────────────────────────────────────────────────
|
||||
|
||||
# Creates a provider and sets OIDC_ARN. Each test uses a unique URL so the suite
|
||||
# stays safe when bats runs test files in parallel.
|
||||
create_oidc_provider() {
|
||||
local url="https://oidc.bats.example.com/id/$(date +%s)-$$-${1:-0}"
|
||||
local out
|
||||
out=$(aws_cmd iam create-open-id-connect-provider \
|
||||
--url "$url" \
|
||||
--client-id-list sts.amazonaws.com \
|
||||
--thumbprint-list 9e99a48a9960b14926bb7f3b02e22da2b0ab7280)
|
||||
OIDC_ARN=$(json_get "$out" '.OpenIDConnectProviderArn')
|
||||
OIDC_HOST="${url#https://}"
|
||||
}
|
||||
|
||||
@test "IAM: create and get OIDC provider" {
|
||||
create_oidc_provider 1
|
||||
[ -n "$OIDC_ARN" ]
|
||||
|
||||
run aws_cmd iam get-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN"
|
||||
assert_success
|
||||
# AWS reports the URL without its scheme.
|
||||
url=$(json_get "$output" '.Url')
|
||||
[ "$url" = "$OIDC_HOST" ]
|
||||
client=$(json_get "$output" '.ClientIDList[0]')
|
||||
[ "$client" = "sts.amazonaws.com" ]
|
||||
|
||||
aws_cmd iam delete-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN" >/dev/null
|
||||
}
|
||||
|
||||
@test "IAM: list OIDC providers includes the created provider" {
|
||||
create_oidc_provider 2
|
||||
|
||||
run aws_cmd iam list-open-id-connect-providers
|
||||
assert_success
|
||||
found=$(echo "$output" | jq --arg arn "$OIDC_ARN" '.OpenIDConnectProviderList | any(.Arn == $arn)')
|
||||
[ "$found" = "true" ]
|
||||
|
||||
aws_cmd iam delete-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN" >/dev/null
|
||||
}
|
||||
|
||||
@test "IAM: add and remove an OIDC client id" {
|
||||
create_oidc_provider 3
|
||||
|
||||
run aws_cmd iam add-client-id-to-open-id-connect-provider \
|
||||
--open-id-connect-provider-arn "$OIDC_ARN" --client-id extra.audience
|
||||
assert_success
|
||||
|
||||
run aws_cmd iam get-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN"
|
||||
assert_success
|
||||
count=$(json_get "$output" '.ClientIDList | length')
|
||||
[ "$count" = "2" ]
|
||||
|
||||
run aws_cmd iam remove-client-id-from-open-id-connect-provider \
|
||||
--open-id-connect-provider-arn "$OIDC_ARN" --client-id extra.audience
|
||||
assert_success
|
||||
|
||||
aws_cmd iam delete-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN" >/dev/null
|
||||
}
|
||||
|
||||
@test "IAM: update OIDC provider thumbprint" {
|
||||
create_oidc_provider 4
|
||||
|
||||
run aws_cmd iam update-open-id-connect-provider-thumbprint \
|
||||
--open-id-connect-provider-arn "$OIDC_ARN" \
|
||||
--thumbprint-list 1111111111111111111111111111111111111111
|
||||
assert_success
|
||||
|
||||
run aws_cmd iam get-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN"
|
||||
assert_success
|
||||
thumb=$(json_get "$output" '.ThumbprintList[0]')
|
||||
[ "$thumb" = "1111111111111111111111111111111111111111" ]
|
||||
|
||||
aws_cmd iam delete-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN" >/dev/null
|
||||
}
|
||||
|
||||
@test "IAM: delete OIDC provider" {
|
||||
create_oidc_provider 5
|
||||
|
||||
run aws_cmd iam delete-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN"
|
||||
assert_success
|
||||
|
||||
run aws_cmd iam get-open-id-connect-provider --open-id-connect-provider-arn "$OIDC_ARN"
|
||||
assert_failure
|
||||
assert_output --partial "NoSuchEntity"
|
||||
}
|
||||
|
||||
@test "IAM: OIDC provider url must be https" {
|
||||
run aws_cmd iam create-open-id-connect-provider \
|
||||
--url http://oidc.bats.example.com/id/insecure \
|
||||
--thumbprint-list 9e99a48a9960b14926bb7f3b02e22da2b0ab7280
|
||||
assert_failure
|
||||
assert_output --partial "ValidationError"
|
||||
}
|
||||
@@ -130,6 +130,38 @@
|
||||
| UpdateAccessKey | Updates an access key's status. |
|
||||
| DeleteAccessKey | Deletes an access key from a user. |
|
||||
|
||||
### OIDC Identity Providers
|
||||
|
||||
| Action | Description |
|
||||
|--------|-------------|
|
||||
| CreateOpenIDConnectProvider | Creates an OIDC identity provider from an https URL. |
|
||||
| GetOpenIDConnectProvider | Returns a provider's URL, client IDs, thumbprints and tags. |
|
||||
| ListOpenIDConnectProviders | Lists the ARNs of stored OIDC providers. |
|
||||
| DeleteOpenIDConnectProvider | Deletes an OIDC identity provider. |
|
||||
| AddClientIDToOpenIDConnectProvider | Adds a client ID (audience) to a provider. |
|
||||
| RemoveClientIDFromOpenIDConnectProvider | Removes a client ID from a provider. |
|
||||
| UpdateOpenIDConnectProviderThumbprint | Replaces a provider's thumbprint list. |
|
||||
| TagOpenIDConnectProvider | Adds tags to a provider. |
|
||||
| UntagOpenIDConnectProvider | Removes tags from a provider. |
|
||||
| ListOpenIDConnectProviderTags | Lists tags stored for a provider. |
|
||||
|
||||
A provider is identified by its URL, so the ARN is derived from it rather than from a generated
|
||||
id: `https://oidc.eks.eu-central-1.amazonaws.com/id/EXAMPLE` becomes
|
||||
`arn:aws:iam::<account>:oidc-provider/oidc.eks.eu-central-1.amazonaws.com/id/EXAMPLE`. Creating
|
||||
the same URL twice returns `EntityAlreadyExists`. As on AWS, `GetOpenIDConnectProvider` reports
|
||||
the URL **without** its scheme.
|
||||
|
||||
The URL must begin with `https://` and is at most 255 characters. It is not normalized, matching
|
||||
AWS: a trailing slash or a difference in case produces a separate provider rather than a
|
||||
duplicate.
|
||||
|
||||
A provider holds at most 100 client IDs (`LimitExceeded` beyond that) and 5 thumbprints
|
||||
(`InvalidInput` beyond that). Adding a client ID that is already present, and removing one that
|
||||
was never added, both succeed and change nothing, as they do on AWS.
|
||||
|
||||
Thumbprints are stored and echoed back but never validated against the remote endpoint, since
|
||||
nothing here performs the TLS handshake they describe.
|
||||
|
||||
### Login Profiles
|
||||
|
||||
| Action | Description |
|
||||
|
||||
@@ -7,6 +7,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.OpenIDConnectProvider;
|
||||
import io.github.hectorvent.floci.services.iam.model.PolicyVersion;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -65,6 +66,16 @@ public class IamQueryHandler {
|
||||
// Identity providers & server certificates (read-only, not modeled)
|
||||
case "ListSAMLProviders" -> handleListSAMLProviders(params);
|
||||
case "ListOpenIDConnectProviders" -> handleListOpenIDConnectProviders(params);
|
||||
case "CreateOpenIDConnectProvider" -> handleCreateOpenIDConnectProvider(params);
|
||||
case "GetOpenIDConnectProvider" -> handleGetOpenIDConnectProvider(params);
|
||||
case "DeleteOpenIDConnectProvider" -> handleDeleteOpenIDConnectProvider(params);
|
||||
case "AddClientIDToOpenIDConnectProvider" -> handleAddClientIDToOpenIDConnectProvider(params);
|
||||
case "RemoveClientIDFromOpenIDConnectProvider" ->
|
||||
handleRemoveClientIDFromOpenIDConnectProvider(params);
|
||||
case "UpdateOpenIDConnectProviderThumbprint" -> handleUpdateOpenIDConnectProviderThumbprint(params);
|
||||
case "TagOpenIDConnectProvider" -> handleTagOpenIDConnectProvider(params);
|
||||
case "UntagOpenIDConnectProvider" -> handleUntagOpenIDConnectProvider(params);
|
||||
case "ListOpenIDConnectProviderTags" -> handleListOpenIDConnectProviderTags(params);
|
||||
case "ListServerCertificates" -> handleListServerCertificates(params);
|
||||
|
||||
// Groups
|
||||
@@ -289,13 +300,97 @@ public class IamQueryHandler {
|
||||
return Response.ok(AwsQueryResponse.envelope("ListSAMLProviders", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
// ListOpenIDConnectProviders is not paginated and carries only ARNs — the client fetches
|
||||
// the rest with GetOpenIDConnectProvider.
|
||||
private Response handleListOpenIDConnectProviders(MultivaluedMap<String, String> params) {
|
||||
// OIDC identity providers are not modeled; return the wire-accurate empty
|
||||
// list (ListOpenIDConnectProviders is not paginated).
|
||||
var xml = new XmlBuilder().start("OpenIDConnectProviderList");
|
||||
for (OpenIDConnectProvider provider : iamService.listOpenIDConnectProviders()) {
|
||||
xml.start("member").elem("Arn", provider.getArn()).end("member");
|
||||
}
|
||||
xml.end("OpenIDConnectProviderList");
|
||||
return Response.ok(AwsQueryResponse.envelope("ListOpenIDConnectProviders", AwsNamespaces.IAM, xml.build())).build();
|
||||
}
|
||||
|
||||
private Response handleCreateOpenIDConnectProvider(MultivaluedMap<String, String> params) {
|
||||
OpenIDConnectProvider provider = iamService.createOpenIDConnectProvider(
|
||||
getParam(params, "Url"),
|
||||
getMemberList(params, "ClientIDList"),
|
||||
getMemberList(params, "ThumbprintList"),
|
||||
extractTags(params));
|
||||
var xml = new XmlBuilder().elem("OpenIDConnectProviderArn", provider.getArn());
|
||||
if (!provider.getTags().isEmpty()) {
|
||||
xml.start("Tags").raw(tagsXml(provider.getTags())).end("Tags");
|
||||
}
|
||||
return Response.ok(AwsQueryResponse.envelope("CreateOpenIDConnectProvider", AwsNamespaces.IAM, xml.build())).build();
|
||||
}
|
||||
|
||||
// The response carries no ARN: AWS echoes back the scheme-less Url, and the caller already
|
||||
// knows the ARN it asked for.
|
||||
private Response handleGetOpenIDConnectProvider(MultivaluedMap<String, String> params) {
|
||||
OpenIDConnectProvider provider =
|
||||
iamService.getOpenIDConnectProvider(getParam(params, "OpenIDConnectProviderArn"));
|
||||
var xml = new XmlBuilder().elem("Url", provider.getUrl());
|
||||
xml.start("ClientIDList");
|
||||
for (String clientId : provider.getClientIdList()) {
|
||||
xml.elem("member", clientId);
|
||||
}
|
||||
xml.end("ClientIDList").start("ThumbprintList");
|
||||
for (String thumbprint : provider.getThumbprintList()) {
|
||||
xml.elem("member", thumbprint);
|
||||
}
|
||||
xml.end("ThumbprintList")
|
||||
.elem("CreateDate", isoDate(provider.getCreateDate()));
|
||||
if (!provider.getTags().isEmpty()) {
|
||||
xml.start("Tags").raw(tagsXml(provider.getTags())).end("Tags");
|
||||
}
|
||||
return Response.ok(AwsQueryResponse.envelope("GetOpenIDConnectProvider", AwsNamespaces.IAM, xml.build())).build();
|
||||
}
|
||||
|
||||
private Response handleDeleteOpenIDConnectProvider(MultivaluedMap<String, String> params) {
|
||||
iamService.deleteOpenIDConnectProvider(getParam(params, "OpenIDConnectProviderArn"));
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("DeleteOpenIDConnectProvider", AwsNamespaces.IAM)).build();
|
||||
}
|
||||
|
||||
private Response handleAddClientIDToOpenIDConnectProvider(MultivaluedMap<String, String> params) {
|
||||
iamService.addClientIdToOpenIDConnectProvider(
|
||||
getParam(params, "OpenIDConnectProviderArn"), getParam(params, "ClientID"));
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult(
|
||||
"AddClientIDToOpenIDConnectProvider", AwsNamespaces.IAM)).build();
|
||||
}
|
||||
|
||||
private Response handleRemoveClientIDFromOpenIDConnectProvider(MultivaluedMap<String, String> params) {
|
||||
iamService.removeClientIdFromOpenIDConnectProvider(
|
||||
getParam(params, "OpenIDConnectProviderArn"), getParam(params, "ClientID"));
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult(
|
||||
"RemoveClientIDFromOpenIDConnectProvider", AwsNamespaces.IAM)).build();
|
||||
}
|
||||
|
||||
private Response handleUpdateOpenIDConnectProviderThumbprint(MultivaluedMap<String, String> params) {
|
||||
iamService.updateOpenIDConnectProviderThumbprint(
|
||||
getParam(params, "OpenIDConnectProviderArn"), getMemberList(params, "ThumbprintList"));
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult(
|
||||
"UpdateOpenIDConnectProviderThumbprint", AwsNamespaces.IAM)).build();
|
||||
}
|
||||
|
||||
private Response handleTagOpenIDConnectProvider(MultivaluedMap<String, String> params) {
|
||||
iamService.tagOpenIDConnectProvider(getParam(params, "OpenIDConnectProviderArn"), extractTags(params));
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("TagOpenIDConnectProvider", AwsNamespaces.IAM)).build();
|
||||
}
|
||||
|
||||
private Response handleUntagOpenIDConnectProvider(MultivaluedMap<String, String> params) {
|
||||
iamService.untagOpenIDConnectProvider(
|
||||
getParam(params, "OpenIDConnectProviderArn"), extractTagKeys(params));
|
||||
return Response.ok(AwsQueryResponse.envelopeNoResult("UntagOpenIDConnectProvider", AwsNamespaces.IAM)).build();
|
||||
}
|
||||
|
||||
private Response handleListOpenIDConnectProviderTags(MultivaluedMap<String, String> params) {
|
||||
Map<String, String> providerTags =
|
||||
iamService.listOpenIDConnectProviderTags(getParam(params, "OpenIDConnectProviderArn"));
|
||||
String result = new XmlBuilder()
|
||||
.start("OpenIDConnectProviderList").end("OpenIDConnectProviderList")
|
||||
.start("Tags").raw(tagsXml(providerTags)).end("Tags")
|
||||
.elem("IsTruncated", false)
|
||||
.build();
|
||||
return Response.ok(AwsQueryResponse.envelope("ListOpenIDConnectProviders", AwsNamespaces.IAM, result)).build();
|
||||
return Response.ok(AwsQueryResponse.envelope("ListOpenIDConnectProviderTags", AwsNamespaces.IAM, result)).build();
|
||||
}
|
||||
|
||||
private Response handleListServerCertificates(MultivaluedMap<String, String> params) {
|
||||
@@ -1033,6 +1128,16 @@ public class IamQueryHandler {
|
||||
return tags;
|
||||
}
|
||||
|
||||
private List<String> getMemberList(MultivaluedMap<String, String> params, String name) {
|
||||
List<String> values = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
String value = params.getFirst(name + ".member." + i);
|
||||
if (value == null) break;
|
||||
values.add(value);
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
private List<String> extractTagKeys(MultivaluedMap<String, String> params) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (int i = 1; ; i++) {
|
||||
|
||||
@@ -15,6 +15,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.OpenIDConnectProvider;
|
||||
import io.github.hectorvent.floci.services.iam.model.PolicyVersion;
|
||||
import io.github.hectorvent.floci.services.iam.model.CallerContext;
|
||||
import io.github.hectorvent.floci.services.iam.model.SessionCredential;
|
||||
@@ -55,6 +56,13 @@ public class IamService implements SessionAccountLookup {
|
||||
private static final String DEFAULT_DEPLOYER_USER = "floci-deployer";
|
||||
private static final String DEFAULT_DEPLOYER_ACCESS_KEY_ID = "floci";
|
||||
private static final String DEFAULT_DEPLOYER_SECRET_ACCESS_KEY = "floci";
|
||||
private static final int MAX_OIDC_CLIENT_IDS = 100;
|
||||
private static final int MAX_OIDC_THUMBPRINTS = 5;
|
||||
private static final int MAX_OIDC_URL_LENGTH = 255;
|
||||
|
||||
/** Guards the read-modify-write in the OIDC provider mutators. */
|
||||
private final Object oidcProviderLock = new Object();
|
||||
|
||||
private static final String SERVICE_LINKED_ROLE_PATH = "/aws-service-role/";
|
||||
private static final String SERVICE_LINKED_ROLE_NAME_PREFIX = "AWSServiceRoleFor";
|
||||
private static final String AMAZONAWS_DOMAIN = ".amazonaws.com";
|
||||
@@ -71,6 +79,7 @@ public class IamService implements SessionAccountLookup {
|
||||
private final StorageBackend<String, AccessKey> accessKeys;
|
||||
private final StorageBackend<String, InstanceProfile> instanceProfiles;
|
||||
private final StorageBackend<String, SessionCredential> sessions;
|
||||
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;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -93,6 +102,7 @@ public class IamService implements SessionAccountLookup {
|
||||
storageFactory.create("iam", "iam-access-keys.json", new TypeReference<>() {}),
|
||||
storageFactory.create("iam", "iam-instance-profiles.json", new TypeReference<>() {}),
|
||||
storageFactory.create("iam", "iam-sessions.json", new TypeReference<>() {}),
|
||||
storageFactory.create("iam", "iam-oidc-providers.json", new TypeReference<>() {}),
|
||||
storageFactory.create("iam", "iam-slr-deletions.json", new TypeReference<>() {}),
|
||||
regionResolver,
|
||||
config.services().iam().seedDeployerPrincipal()
|
||||
@@ -120,7 +130,7 @@ public class IamService implements SessionAccountLookup {
|
||||
RegionResolver regionResolver,
|
||||
boolean seedDeployerPrincipal) {
|
||||
this(users, groups, roles, policies, accessKeys, instanceProfiles, sessions,
|
||||
new InMemoryStorage<>(), regionResolver, seedDeployerPrincipal);
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), regionResolver, seedDeployerPrincipal);
|
||||
}
|
||||
|
||||
IamService(StorageBackend<String, IamUser> users,
|
||||
@@ -130,6 +140,7 @@ public class IamService implements SessionAccountLookup {
|
||||
StorageBackend<String, AccessKey> accessKeys,
|
||||
StorageBackend<String, InstanceProfile> instanceProfiles,
|
||||
StorageBackend<String, SessionCredential> sessions,
|
||||
StorageBackend<String, OpenIDConnectProvider> oidcProviders,
|
||||
StorageBackend<String, String> serviceLinkedRoleDeletions,
|
||||
RegionResolver regionResolver,
|
||||
boolean seedDeployerPrincipal) {
|
||||
@@ -140,6 +151,7 @@ public class IamService implements SessionAccountLookup {
|
||||
this.accessKeys = accessKeys;
|
||||
this.instanceProfiles = instanceProfiles;
|
||||
this.sessions = sessions;
|
||||
this.oidcProviders = oidcProviders;
|
||||
this.serviceLinkedRoleDeletions = serviceLinkedRoleDeletions;
|
||||
this.regionResolver = regionResolver;
|
||||
this.seedDeployerPrincipal = seedDeployerPrincipal;
|
||||
@@ -1158,6 +1170,194 @@ public class IamService implements SessionAccountLookup {
|
||||
.toList();
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// OIDC Identity Providers
|
||||
// =========================================================================
|
||||
|
||||
/**
|
||||
* AWS identifies a provider by URL, so the ARN is derived from it rather than from a random
|
||||
* id, and the scheme is stripped: {@code https://host/path} becomes
|
||||
* {@code arn:aws:iam::<account>:oidc-provider/host/path}. Creating the same URL twice is
|
||||
* therefore a duplicate resource, not a second provider.
|
||||
*/
|
||||
public OpenIDConnectProvider createOpenIDConnectProvider(String url, List<String> clientIdList,
|
||||
List<String> thumbprintList,
|
||||
Map<String, String> providerTags) {
|
||||
if (url == null || url.isBlank()) {
|
||||
throw new AwsException("ValidationError", "The request must contain the parameter Url.", 400);
|
||||
}
|
||||
if (!url.startsWith("https://")) {
|
||||
throw new AwsException("ValidationError",
|
||||
"The OpenID Connect provider URL must begin with https://.", 400);
|
||||
}
|
||||
if (url.length() > MAX_OIDC_URL_LENGTH) {
|
||||
throw new AwsException("ValidationError",
|
||||
"The OpenID Connect provider URL must be at most "
|
||||
+ MAX_OIDC_URL_LENGTH + " characters.", 400);
|
||||
}
|
||||
String normalizedUrl = url.substring("https://".length());
|
||||
if (normalizedUrl.isBlank()) {
|
||||
throw new AwsException("ValidationError", "The OpenID Connect provider URL is not valid.", 400);
|
||||
}
|
||||
if (thumbprintList != null && thumbprintList.size() > MAX_OIDC_THUMBPRINTS) {
|
||||
throw new AwsException("InvalidInput",
|
||||
"Thumbprint list must contain fewer than " + MAX_OIDC_THUMBPRINTS + " entries.", 400);
|
||||
}
|
||||
if (clientIdList != null && clientIdList.size() > MAX_OIDC_CLIENT_IDS) {
|
||||
throw new AwsException("LimitExceeded",
|
||||
"Cannot exceed quota for ClientIdsPerOpenIdConnectProvider: " + MAX_OIDC_CLIENT_IDS, 409);
|
||||
}
|
||||
|
||||
String arn = iamArn("oidc-provider", "/", normalizedUrl);
|
||||
OpenIDConnectProvider provider = new OpenIDConnectProvider();
|
||||
provider.setArn(arn);
|
||||
provider.setUrl(normalizedUrl);
|
||||
provider.setClientIdList(clientIdList == null ? new ArrayList<>() : new ArrayList<>(clientIdList));
|
||||
provider.setThumbprintList(thumbprintList == null ? new ArrayList<>() : new ArrayList<>(thumbprintList));
|
||||
provider.setCreateDate(Instant.now());
|
||||
if (providerTags != null && !providerTags.isEmpty()) {
|
||||
provider.setTags(providerTags);
|
||||
}
|
||||
// Racing creates collide only on the same URL, but they can carry different client IDs,
|
||||
// thumbprints and tags, so an unguarded check-then-write would report success to every
|
||||
// caller while storing one arbitrary payload.
|
||||
synchronized (oidcProviderLock) {
|
||||
if (oidcProviders.get(arn).isPresent()) {
|
||||
throw new AwsException("EntityAlreadyExists",
|
||||
"Provider with url " + url + " already exists.", 409);
|
||||
}
|
||||
oidcProviders.put(arn, provider);
|
||||
}
|
||||
LOG.infov("Created OIDC provider: {0}", arn);
|
||||
return provider;
|
||||
}
|
||||
|
||||
public OpenIDConnectProvider getOpenIDConnectProvider(String arn) {
|
||||
requireProviderArn(arn);
|
||||
return oidcProviders.get(arn)
|
||||
.orElseThrow(() -> new AwsException("NoSuchEntity",
|
||||
"OpenIDConnect Provider not found for arn " + arn, 404));
|
||||
}
|
||||
|
||||
public List<OpenIDConnectProvider> listOpenIDConnectProviders() {
|
||||
return oidcProviders.scan(k -> true);
|
||||
}
|
||||
|
||||
public void deleteOpenIDConnectProvider(String arn) {
|
||||
requireProviderArn(arn);
|
||||
synchronized (oidcProviderLock) {
|
||||
if (oidcProviders.get(arn).isEmpty()) {
|
||||
throw new AwsException("NoSuchEntity",
|
||||
"OpenId connect Provider " + arn + " cannot be found.", 404);
|
||||
}
|
||||
oidcProviders.delete(arn);
|
||||
}
|
||||
LOG.infov("Deleted OIDC provider: {0}", arn);
|
||||
}
|
||||
|
||||
public void addClientIdToOpenIDConnectProvider(String arn, String clientId) {
|
||||
requireProviderArn(arn);
|
||||
requireClientId(clientId);
|
||||
synchronized (oidcProviderLock) {
|
||||
OpenIDConnectProvider provider = getOpenIDConnectProvider(arn);
|
||||
// AWS treats adding a client ID that is already present as a no-op success, so this
|
||||
// returns rather than reporting a conflict.
|
||||
if (provider.getClientIdList().contains(clientId)) {
|
||||
return;
|
||||
}
|
||||
if (provider.getClientIdList().size() >= MAX_OIDC_CLIENT_IDS) {
|
||||
throw new AwsException("LimitExceeded",
|
||||
"Cannot exceed quota for ClientIdsPerOpenIdConnectProvider: " + MAX_OIDC_CLIENT_IDS, 409);
|
||||
}
|
||||
List<String> updated = new ArrayList<>(provider.getClientIdList());
|
||||
updated.add(clientId);
|
||||
provider.setClientIdList(updated);
|
||||
oidcProviders.put(arn, provider);
|
||||
}
|
||||
}
|
||||
|
||||
public void removeClientIdFromOpenIDConnectProvider(String arn, String clientId) {
|
||||
requireProviderArn(arn);
|
||||
requireClientId(clientId);
|
||||
synchronized (oidcProviderLock) {
|
||||
OpenIDConnectProvider provider = getOpenIDConnectProvider(arn);
|
||||
// Removing a client ID that is not present is a no-op success on AWS, not an error.
|
||||
if (!provider.getClientIdList().contains(clientId)) {
|
||||
return;
|
||||
}
|
||||
List<String> updated = new ArrayList<>(provider.getClientIdList());
|
||||
updated.remove(clientId);
|
||||
provider.setClientIdList(updated);
|
||||
oidcProviders.put(arn, provider);
|
||||
}
|
||||
}
|
||||
|
||||
private void requireProviderArn(String arn) {
|
||||
if (arn == null || arn.isBlank()) {
|
||||
throw new AwsException("ValidationError",
|
||||
"The request must contain the parameter OpenIDConnectProviderArn.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private void requireClientId(String clientId) {
|
||||
if (clientId == null || clientId.isBlank()) {
|
||||
throw new AwsException("ValidationError",
|
||||
"The request must contain the parameter ClientID.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateOpenIDConnectProviderThumbprint(String arn, List<String> thumbprintList) {
|
||||
requireProviderArn(arn);
|
||||
if (thumbprintList == null || thumbprintList.isEmpty()) {
|
||||
throw new AwsException("ValidationError",
|
||||
"The request must contain the parameter ThumbprintList.", 400);
|
||||
}
|
||||
if (thumbprintList.size() > MAX_OIDC_THUMBPRINTS) {
|
||||
throw new AwsException("InvalidInput",
|
||||
"Thumbprint list must contain fewer than " + MAX_OIDC_THUMBPRINTS + " entries.", 400);
|
||||
}
|
||||
synchronized (oidcProviderLock) {
|
||||
OpenIDConnectProvider provider = getOpenIDConnectProvider(arn);
|
||||
provider.setThumbprintList(new ArrayList<>(thumbprintList));
|
||||
oidcProviders.put(arn, provider);
|
||||
}
|
||||
}
|
||||
|
||||
// AWS rejects an empty tag map rather than treating it as a no-op, and reports InvalidInput
|
||||
// rather than ValidationError. Both verified against a live account.
|
||||
public void tagOpenIDConnectProvider(String arn, Map<String, String> newTags) {
|
||||
requireProviderArn(arn);
|
||||
if (newTags == null || newTags.isEmpty()) {
|
||||
throw new AwsException("InvalidInput", "The provided tag map must not be null/empty.", 400);
|
||||
}
|
||||
synchronized (oidcProviderLock) {
|
||||
OpenIDConnectProvider provider = getOpenIDConnectProvider(arn);
|
||||
Map<String, String> merged = new LinkedHashMap<>(provider.getTags());
|
||||
merged.putAll(newTags);
|
||||
provider.setTags(merged);
|
||||
oidcProviders.put(arn, provider);
|
||||
}
|
||||
}
|
||||
|
||||
public void untagOpenIDConnectProvider(String arn, List<String> tagKeys) {
|
||||
requireProviderArn(arn);
|
||||
if (tagKeys == null || tagKeys.isEmpty()) {
|
||||
throw new AwsException("InvalidInput", "The provided tag keys must not be null/empty.", 400);
|
||||
}
|
||||
synchronized (oidcProviderLock) {
|
||||
OpenIDConnectProvider provider = getOpenIDConnectProvider(arn);
|
||||
Map<String, String> remaining = new LinkedHashMap<>(provider.getTags());
|
||||
tagKeys.forEach(remaining::remove);
|
||||
provider.setTags(remaining);
|
||||
oidcProviders.put(arn, provider);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, String> listOpenIDConnectProviderTags(String arn) {
|
||||
requireProviderArn(arn);
|
||||
return getOpenIDConnectProvider(arn).getTags();
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// Internal helpers
|
||||
// =========================================================================
|
||||
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package io.github.hectorvent.floci.services.iam.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* An IAM OIDC identity provider.
|
||||
*
|
||||
* <p>{@link #url} is stored without its scheme, which is how AWS reports it: a provider created
|
||||
* from {@code https://oidc.eks.eu-central-1.amazonaws.com/id/EXAMPLE} reads back as
|
||||
* {@code oidc.eks.eu-central-1.amazonaws.com/id/EXAMPLE}, and the ARN is built from that same
|
||||
* scheme-less form.
|
||||
*/
|
||||
@RegisterForReflection
|
||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||
public class OpenIDConnectProvider {
|
||||
|
||||
private String arn;
|
||||
private String url;
|
||||
private List<String> clientIdList = new ArrayList<>();
|
||||
private List<String> thumbprintList = new ArrayList<>();
|
||||
private Instant createDate = Instant.now();
|
||||
private Map<String, String> tags = new ConcurrentHashMap<>();
|
||||
|
||||
public OpenIDConnectProvider() {}
|
||||
|
||||
public String getArn() { return arn; }
|
||||
public void setArn(String arn) { this.arn = arn; }
|
||||
|
||||
public String getUrl() { return url; }
|
||||
public void setUrl(String url) { this.url = url; }
|
||||
|
||||
public List<String> getClientIdList() { return clientIdList; }
|
||||
public void setClientIdList(List<String> clientIdList) { this.clientIdList = clientIdList; }
|
||||
|
||||
public List<String> getThumbprintList() { return thumbprintList; }
|
||||
public void setThumbprintList(List<String> thumbprintList) { this.thumbprintList = thumbprintList; }
|
||||
|
||||
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 = new ConcurrentHashMap<>(tags); }
|
||||
}
|
||||
@@ -37,7 +37,8 @@ class IamConcurrencyTest {
|
||||
return new IamService(
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>(),
|
||||
new InMemoryStorage<>(), new RegionResolver("eu-central-1", "000000000000"), false);
|
||||
new InMemoryStorage<>(),
|
||||
new RegionResolver("eu-central-1", "000000000000"), false);
|
||||
}
|
||||
|
||||
/** Sets up one entity on the given service and returns the i-th concurrent mutation. */
|
||||
@@ -246,6 +247,55 @@ class IamConcurrencyTest {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Only one caller may create a provider for a given URL. Racing creates carry different
|
||||
* client IDs, so an unguarded check-then-write would report success to several callers while
|
||||
* storing one arbitrary payload.
|
||||
*/
|
||||
@Test
|
||||
void concurrentOidcProviderCreatesLeaveExactlyOneWinner() throws Exception {
|
||||
ExecutorService pool = Executors.newFixedThreadPool(THREADS);
|
||||
try {
|
||||
for (int trial = 0; trial < TRIALS; trial++) {
|
||||
IamService iam = newIamService();
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
CountDownLatch done = new CountDownLatch(N);
|
||||
List<String> winners = new CopyOnWriteArrayList<>();
|
||||
List<String> unexpected = new CopyOnWriteArrayList<>();
|
||||
|
||||
for (int i = 0; i < N; i++) {
|
||||
String clientId = "client-" + i;
|
||||
pool.execute(() -> {
|
||||
try {
|
||||
start.await(10, TimeUnit.SECONDS);
|
||||
iam.createOpenIDConnectProvider("https://oidc.example.com/id/RACE",
|
||||
List.of(clientId), List.of("thumb"), java.util.Map.of());
|
||||
winners.add(clientId);
|
||||
} catch (io.github.hectorvent.floci.core.common.AwsException e) {
|
||||
if (!"EntityAlreadyExists".equals(e.getErrorCode())) {
|
||||
unexpected.add(e.getErrorCode());
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
done.countDown();
|
||||
}
|
||||
});
|
||||
}
|
||||
start.countDown();
|
||||
assertTrue(done.await(30, TimeUnit.SECONDS), "oidc workers stalled");
|
||||
|
||||
List<String> storedClientIds = iam.listOpenIDConnectProviders().getFirst().getClientIdList();
|
||||
if (winners.size() != 1 || !storedClientIds.equals(winners) || !unexpected.isEmpty()) {
|
||||
fail("oidcProvider trial " + trial + ": winners=" + winners
|
||||
+ " stored=" + storedClientIds + " unexpectedErrors=" + unexpected);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
private static String[] createPolicies(IamService iam, String prefix) {
|
||||
String[] arns = new String[N];
|
||||
for (int i = 0; i < N; i++) {
|
||||
|
||||
+232
@@ -0,0 +1,232 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsQueryController;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
/**
|
||||
* Integration tests for IAM OIDC identity providers via the Query Protocol, covering the full
|
||||
* HTTP stack through {@link AwsQueryController} → {@link IamQueryHandler}.
|
||||
*
|
||||
* <p>Ordered because the cases drive one provider through its lifecycle.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class IamOpenIDConnectProviderIntegrationTest {
|
||||
|
||||
private static final String IAM_CREDENTIAL =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request";
|
||||
|
||||
private static final String OIDC_URL =
|
||||
"https://oidc.eks.eu-central-1.amazonaws.com/id/INTEGRATION0EXAMPLE";
|
||||
private static final String OIDC_HOST =
|
||||
"oidc.eks.eu-central-1.amazonaws.com/id/INTEGRATION0EXAMPLE";
|
||||
private static final String THUMBPRINT = "9e99a48a9960b14926bb7f3b02e22da2b0ab7280";
|
||||
|
||||
private static String providerArn;
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createOpenIDConnectProvider() {
|
||||
providerArn = given()
|
||||
.formParam("Action", "CreateOpenIDConnectProvider")
|
||||
.formParam("Url", OIDC_URL)
|
||||
.formParam("ClientIDList.member.1", "sts.amazonaws.com")
|
||||
.formParam("ThumbprintList.member.1", THUMBPRINT)
|
||||
.formParam("Tags.member.1.Key", "env")
|
||||
.formParam("Tags.member.1.Value", "test")
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.contentType("application/xml")
|
||||
.body("CreateOpenIDConnectProviderResponse.CreateOpenIDConnectProviderResult"
|
||||
+ ".OpenIDConnectProviderArn", containsString(":oidc-provider/" + OIDC_HOST))
|
||||
.extract().path("CreateOpenIDConnectProviderResponse.CreateOpenIDConnectProviderResult"
|
||||
+ ".OpenIDConnectProviderArn");
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void getReturnsTheUrlWithoutItsScheme() {
|
||||
given()
|
||||
.formParam("Action", "GetOpenIDConnectProvider")
|
||||
.formParam("OpenIDConnectProviderArn", providerArn)
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("GetOpenIDConnectProviderResponse.GetOpenIDConnectProviderResult.Url", equalTo(OIDC_HOST))
|
||||
.body("GetOpenIDConnectProviderResponse.GetOpenIDConnectProviderResult.ClientIDList.member",
|
||||
equalTo("sts.amazonaws.com"))
|
||||
.body("GetOpenIDConnectProviderResponse.GetOpenIDConnectProviderResult.ThumbprintList.member",
|
||||
equalTo(THUMBPRINT))
|
||||
.body("GetOpenIDConnectProviderResponse.GetOpenIDConnectProviderResult.CreateDate",
|
||||
not(emptyOrNullString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void listReturnsTheCreatedProvider() {
|
||||
given()
|
||||
.formParam("Action", "ListOpenIDConnectProviders")
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ListOpenIDConnectProvidersResponse.ListOpenIDConnectProvidersResult"
|
||||
+ ".OpenIDConnectProviderList.member.Arn", equalTo(providerArn));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void creatingTheSameUrlTwiceIsRejected() {
|
||||
given()
|
||||
.formParam("Action", "CreateOpenIDConnectProvider")
|
||||
.formParam("Url", OIDC_URL)
|
||||
.formParam("ThumbprintList.member.1", THUMBPRINT)
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(409)
|
||||
.body("ErrorResponse.Error.Code", equalTo("EntityAlreadyExists"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void addAndRemoveClientId() {
|
||||
given()
|
||||
.formParam("Action", "AddClientIDToOpenIDConnectProvider")
|
||||
.formParam("OpenIDConnectProviderArn", providerArn)
|
||||
.formParam("ClientID", "extra.audience")
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "GetOpenIDConnectProvider")
|
||||
.formParam("OpenIDConnectProviderArn", providerArn)
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("GetOpenIDConnectProviderResponse.GetOpenIDConnectProviderResult.ClientIDList.member",
|
||||
hasItems("sts.amazonaws.com", "extra.audience"));
|
||||
|
||||
given()
|
||||
.formParam("Action", "RemoveClientIDFromOpenIDConnectProvider")
|
||||
.formParam("OpenIDConnectProviderArn", providerArn)
|
||||
.formParam("ClientID", "extra.audience")
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void updateThumbprint() {
|
||||
given()
|
||||
.formParam("Action", "UpdateOpenIDConnectProviderThumbprint")
|
||||
.formParam("OpenIDConnectProviderArn", providerArn)
|
||||
.formParam("ThumbprintList.member.1", "1111111111111111111111111111111111111111")
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "GetOpenIDConnectProvider")
|
||||
.formParam("OpenIDConnectProviderArn", providerArn)
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("GetOpenIDConnectProviderResponse.GetOpenIDConnectProviderResult.ThumbprintList.member",
|
||||
equalTo("1111111111111111111111111111111111111111"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void listTags() {
|
||||
given()
|
||||
.formParam("Action", "ListOpenIDConnectProviderTags")
|
||||
.formParam("OpenIDConnectProviderArn", providerArn)
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ListOpenIDConnectProviderTagsResponse.ListOpenIDConnectProviderTagsResult"
|
||||
+ ".Tags.member.Key", equalTo("env"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void getUnknownArnIsRejected() {
|
||||
given()
|
||||
.formParam("Action", "GetOpenIDConnectProvider")
|
||||
.formParam("OpenIDConnectProviderArn",
|
||||
"arn:aws:iam::000000000000:oidc-provider/nope.example.com/id/X")
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void nonHttpsUrlIsRejected() {
|
||||
given()
|
||||
.formParam("Action", "CreateOpenIDConnectProvider")
|
||||
.formParam("Url", "http://oidc.example.com/id/insecure")
|
||||
.formParam("ThumbprintList.member.1", THUMBPRINT)
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("ErrorResponse.Error.Code", equalTo("ValidationError"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void deleteOpenIDConnectProvider() {
|
||||
given()
|
||||
.formParam("Action", "DeleteOpenIDConnectProvider")
|
||||
.formParam("OpenIDConnectProviderArn", providerArn)
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.formParam("Action", "GetOpenIDConnectProvider")
|
||||
.formParam("OpenIDConnectProviderArn", providerArn)
|
||||
.header("Authorization", IAM_CREDENTIAL)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package io.github.hectorvent.floci.services.iam;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.PersistentStorage;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.services.iam.model.AccessKey;
|
||||
import io.github.hectorvent.floci.services.iam.model.IamGroup;
|
||||
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.OpenIDConnectProvider;
|
||||
import io.github.hectorvent.floci.services.iam.model.SessionCredential;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
/**
|
||||
* IAM state is persisted through StorageFactory, so it must survive a restart. This builds an
|
||||
* IamService over PersistentStorage in a temp dir, then builds a SECOND IamService over the SAME
|
||||
* files (simulating a process restart) and asserts the resources are still intact.
|
||||
*/
|
||||
class IamServicePersistenceTest {
|
||||
|
||||
private static final String OIDC_URL =
|
||||
"https://oidc.eks.eu-central-1.amazonaws.com/id/PERSISTED0EXAMPLE";
|
||||
private static final String THUMBPRINT = "9e99a48a9960b14926bb7f3b02e22da2b0ab7280";
|
||||
|
||||
@Test
|
||||
void openIdConnectProviderSurvivesRestart(@TempDir Path dir) {
|
||||
IamService first = newService(dir);
|
||||
OpenIDConnectProvider created = first.createOpenIDConnectProvider(
|
||||
OIDC_URL, List.of("sts.amazonaws.com"), List.of(THUMBPRINT), Map.of("env", "prod"));
|
||||
|
||||
// A fresh service over the same persistent files = a restart with the same data dir.
|
||||
IamService restarted = newService(dir);
|
||||
|
||||
OpenIDConnectProvider reloaded = restarted.getOpenIDConnectProvider(created.getArn());
|
||||
assertEquals(created.getArn(), reloaded.getArn());
|
||||
assertEquals("oidc.eks.eu-central-1.amazonaws.com/id/PERSISTED0EXAMPLE", reloaded.getUrl());
|
||||
assertEquals(List.of("sts.amazonaws.com"), reloaded.getClientIdList());
|
||||
assertEquals(List.of(THUMBPRINT), reloaded.getThumbprintList());
|
||||
assertEquals("prod", reloaded.getTags().get("env"));
|
||||
// createDate is an Instant, so a broken time round trip would surface here.
|
||||
assertNotNull(reloaded.getCreateDate());
|
||||
assertEquals(created.getCreateDate(), reloaded.getCreateDate());
|
||||
|
||||
assertEquals(1, restarted.listOpenIDConnectProviders().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void openIdConnectProviderMutationsSurviveRestart(@TempDir Path dir) {
|
||||
IamService first = newService(dir);
|
||||
OpenIDConnectProvider created = first.createOpenIDConnectProvider(
|
||||
OIDC_URL, List.of("sts.amazonaws.com"), List.of(THUMBPRINT), Map.of());
|
||||
first.addClientIdToOpenIDConnectProvider(created.getArn(), "extra.audience");
|
||||
first.updateOpenIDConnectProviderThumbprint(created.getArn(), List.of("aaaa", "bbbb"));
|
||||
|
||||
IamService restarted = newService(dir);
|
||||
|
||||
OpenIDConnectProvider reloaded = restarted.getOpenIDConnectProvider(created.getArn());
|
||||
assertEquals(List.of("sts.amazonaws.com", "extra.audience"), reloaded.getClientIdList());
|
||||
assertEquals(List.of("aaaa", "bbbb"), reloaded.getThumbprintList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void userAndRoleSurviveRestart(@TempDir Path dir) {
|
||||
IamService first = newService(dir);
|
||||
first.createUser("alice", "/");
|
||||
first.createRole("LambdaExec", "/", "{\"Version\":\"2012-10-17\",\"Statement\":[]}",
|
||||
"Lambda role", 3600, null);
|
||||
|
||||
IamService restarted = newService(dir);
|
||||
|
||||
assertEquals("alice", restarted.getUser("alice").getUserName());
|
||||
assertEquals("LambdaExec", restarted.getRole("LambdaExec").getRoleName());
|
||||
}
|
||||
|
||||
private IamService newService(Path dir) {
|
||||
return new IamService(
|
||||
load(dir, "iam-users.json", new TypeReference<Map<String, IamUser>>() {}),
|
||||
load(dir, "iam-groups.json", new TypeReference<Map<String, IamGroup>>() {}),
|
||||
load(dir, "iam-roles.json", new TypeReference<Map<String, IamRole>>() {}),
|
||||
load(dir, "iam-policies.json", new TypeReference<Map<String, IamPolicy>>() {}),
|
||||
load(dir, "iam-access-keys.json", new TypeReference<Map<String, AccessKey>>() {}),
|
||||
load(dir, "iam-instance-profiles.json", new TypeReference<Map<String, InstanceProfile>>() {}),
|
||||
load(dir, "iam-sessions.json", new TypeReference<Map<String, SessionCredential>>() {}),
|
||||
load(dir, "iam-oidc-providers.json", new TypeReference<Map<String, OpenIDConnectProvider>>() {}),
|
||||
load(dir, "iam-slr-deletions.json", new TypeReference<Map<String, String>>() {}),
|
||||
new RegionResolver("us-east-1", "000000000000"),
|
||||
false);
|
||||
}
|
||||
|
||||
private <V> StorageBackend<String, V> load(Path dir, String file, TypeReference<Map<String, V>> type) {
|
||||
PersistentStorage<String, V> backend = new PersistentStorage<>(dir.resolve(file), type);
|
||||
backend.load();
|
||||
return backend;
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import io.github.hectorvent.floci.services.iam.model.IamGroup;
|
||||
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.OpenIDConnectProvider;
|
||||
import io.github.hectorvent.floci.services.iam.model.InstanceProfile;
|
||||
import io.github.hectorvent.floci.services.iam.model.PolicyVersion;
|
||||
import io.github.hectorvent.floci.services.iam.model.SessionCredential;
|
||||
@@ -857,4 +858,219 @@ class IamServiceTest {
|
||||
List<IamPolicy> all = iamService.listPolicies(null, "/");
|
||||
assertEquals(awsOnly.size() + localOnly.size(), all.size());
|
||||
}
|
||||
|
||||
// =========================================================================
|
||||
// OIDC Identity Providers
|
||||
// =========================================================================
|
||||
|
||||
private static final String OIDC_URL =
|
||||
"https://oidc.eks.eu-central-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B716D3041E";
|
||||
private static final String OIDC_HOST =
|
||||
"oidc.eks.eu-central-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B716D3041E";
|
||||
private static final String THUMBPRINT = "9e99a48a9960b14926bb7f3b02e22da2b0ab7280";
|
||||
|
||||
@Test
|
||||
void createOpenIDConnectProviderStripsTheSchemeFromUrlAndArn() {
|
||||
OpenIDConnectProvider provider = iamService.createOpenIDConnectProvider(
|
||||
OIDC_URL, List.of("sts.amazonaws.com"), List.of(THUMBPRINT), Map.of());
|
||||
|
||||
assertEquals(OIDC_HOST, provider.getUrl());
|
||||
assertEquals("arn:aws:iam::000000000000:oidc-provider/" + OIDC_HOST, provider.getArn());
|
||||
assertEquals(List.of("sts.amazonaws.com"), provider.getClientIdList());
|
||||
assertEquals(List.of(THUMBPRINT), provider.getThumbprintList());
|
||||
assertNotNull(provider.getCreateDate());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createOpenIDConnectProviderRequiresHttps() {
|
||||
AwsException error = assertThrows(AwsException.class, () -> iamService.createOpenIDConnectProvider(
|
||||
"http://oidc.example.com/id/1", List.of(), List.of(), Map.of()));
|
||||
assertEquals("ValidationError", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createDuplicateOpenIDConnectProviderFails() {
|
||||
iamService.createOpenIDConnectProvider(OIDC_URL, List.of(), List.of(THUMBPRINT), Map.of());
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> iamService.createOpenIDConnectProvider(
|
||||
OIDC_URL, List.of(), List.of(THUMBPRINT), Map.of()));
|
||||
assertEquals("EntityAlreadyExists", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getOpenIDConnectProviderNotFoundThrows() {
|
||||
AwsException error = assertThrows(AwsException.class, () -> iamService.getOpenIDConnectProvider(
|
||||
"arn:aws:iam::000000000000:oidc-provider/missing.example.com"));
|
||||
assertEquals("NoSuchEntity", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void listOpenIDConnectProviders() {
|
||||
assertTrue(iamService.listOpenIDConnectProviders().isEmpty());
|
||||
OpenIDConnectProvider provider =
|
||||
iamService.createOpenIDConnectProvider(OIDC_URL, List.of(), List.of(THUMBPRINT), Map.of());
|
||||
|
||||
List<OpenIDConnectProvider> all = iamService.listOpenIDConnectProviders();
|
||||
assertEquals(1, all.size());
|
||||
assertEquals(provider.getArn(), all.getFirst().getArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteOpenIDConnectProvider() {
|
||||
OpenIDConnectProvider provider =
|
||||
iamService.createOpenIDConnectProvider(OIDC_URL, List.of(), List.of(THUMBPRINT), Map.of());
|
||||
|
||||
iamService.deleteOpenIDConnectProvider(provider.getArn());
|
||||
|
||||
assertTrue(iamService.listOpenIDConnectProviders().isEmpty());
|
||||
assertThrows(AwsException.class, () -> iamService.getOpenIDConnectProvider(provider.getArn()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addAndRemoveClientId() {
|
||||
OpenIDConnectProvider provider = iamService.createOpenIDConnectProvider(
|
||||
OIDC_URL, List.of("sts.amazonaws.com"), List.of(THUMBPRINT), Map.of());
|
||||
|
||||
iamService.addClientIdToOpenIDConnectProvider(provider.getArn(), "extra.audience");
|
||||
assertEquals(List.of("sts.amazonaws.com", "extra.audience"),
|
||||
iamService.getOpenIDConnectProvider(provider.getArn()).getClientIdList());
|
||||
|
||||
iamService.removeClientIdFromOpenIDConnectProvider(provider.getArn(), "extra.audience");
|
||||
assertEquals(List.of("sts.amazonaws.com"),
|
||||
iamService.getOpenIDConnectProvider(provider.getArn()).getClientIdList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verified against a live AWS account: adding a client ID that is already present and removing
|
||||
* one that was never added both succeed and change nothing.
|
||||
*/
|
||||
@Test
|
||||
void clientIdAddAndRemoveAreIdempotent() {
|
||||
OpenIDConnectProvider provider = iamService.createOpenIDConnectProvider(
|
||||
OIDC_URL, List.of("sts.amazonaws.com"), List.of(THUMBPRINT), Map.of());
|
||||
|
||||
iamService.addClientIdToOpenIDConnectProvider(provider.getArn(), "sts.amazonaws.com");
|
||||
assertEquals(List.of("sts.amazonaws.com"),
|
||||
iamService.getOpenIDConnectProvider(provider.getArn()).getClientIdList());
|
||||
|
||||
iamService.removeClientIdFromOpenIDConnectProvider(provider.getArn(), "never.added");
|
||||
assertEquals(List.of("sts.amazonaws.com"),
|
||||
iamService.getOpenIDConnectProvider(provider.getArn()).getClientIdList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateThumbprintReplacesTheList() {
|
||||
OpenIDConnectProvider provider =
|
||||
iamService.createOpenIDConnectProvider(OIDC_URL, List.of(), List.of(THUMBPRINT), Map.of());
|
||||
|
||||
iamService.updateOpenIDConnectProviderThumbprint(provider.getArn(), List.of("aaaa", "bbbb"));
|
||||
|
||||
assertEquals(List.of("aaaa", "bbbb"),
|
||||
iamService.getOpenIDConnectProvider(provider.getArn()).getThumbprintList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void openIdConnectProviderUrlIsCappedAt255Characters() {
|
||||
String longUrl = "https://oidc.example.com/id/" + "a".repeat(255);
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () ->
|
||||
iamService.createOpenIDConnectProvider(longUrl, List.of(), List.of(THUMBPRINT), Map.of()));
|
||||
assertEquals("ValidationError", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void thumbprintListIsCappedAtFive() {
|
||||
// Five is accepted; six is not, and AWS reports InvalidInput rather than LimitExceeded.
|
||||
iamService.createOpenIDConnectProvider(OIDC_URL, List.of(),
|
||||
List.of("a", "b", "c", "d", "e"), Map.of());
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> iamService.createOpenIDConnectProvider(
|
||||
"https://oidc.example.com/id/six", List.of(), List.of("a", "b", "c", "d", "e", "f"), Map.of()));
|
||||
assertEquals("InvalidInput", error.getErrorCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* A missing required parameter must fail as a ValidationError before any lookup. A null
|
||||
* ClientID would otherwise read as "not in the list" and report a no-op success, and a null
|
||||
* ARN as a missing provider.
|
||||
*/
|
||||
@Test
|
||||
void oidcMutatorsRejectMissingRequiredParameters() {
|
||||
OpenIDConnectProvider provider = iamService.createOpenIDConnectProvider(
|
||||
OIDC_URL, List.of("sts.amazonaws.com"), List.of(THUMBPRINT), Map.of());
|
||||
|
||||
assertEquals("ValidationError", assertThrows(AwsException.class, () ->
|
||||
iamService.removeClientIdFromOpenIDConnectProvider(provider.getArn(), null)).getErrorCode());
|
||||
assertEquals("ValidationError", assertThrows(AwsException.class, () ->
|
||||
iamService.addClientIdToOpenIDConnectProvider(provider.getArn(), " ")).getErrorCode());
|
||||
assertEquals("ValidationError", assertThrows(AwsException.class, () ->
|
||||
iamService.deleteOpenIDConnectProvider(null)).getErrorCode());
|
||||
assertEquals("ValidationError", assertThrows(AwsException.class, () ->
|
||||
iamService.updateOpenIDConnectProviderThumbprint(null, List.of("aaaa"))).getErrorCode());
|
||||
|
||||
// The provider and its client IDs are untouched by any of the rejected calls.
|
||||
assertEquals(List.of("sts.amazonaws.com"),
|
||||
iamService.getOpenIDConnectProvider(provider.getArn()).getClientIdList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void clientIdListIsCappedAtOneHundred() {
|
||||
List<String> tooMany = java.util.stream.IntStream.range(0, 101)
|
||||
.mapToObj(i -> "client-" + i).toList();
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () ->
|
||||
iamService.createOpenIDConnectProvider(OIDC_URL, tooMany, List.of(THUMBPRINT), Map.of()));
|
||||
assertEquals("LimitExceeded", error.getErrorCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verified against a live AWS account: AWS does not normalize the URL, so a trailing slash or
|
||||
* a case difference yields a separate provider rather than a duplicate.
|
||||
*/
|
||||
@Test
|
||||
void providerUrlsAreNotNormalized() {
|
||||
iamService.createOpenIDConnectProvider(OIDC_URL, List.of(), List.of(THUMBPRINT), Map.of());
|
||||
iamService.createOpenIDConnectProvider(OIDC_URL + "/", List.of(), List.of(THUMBPRINT), Map.of());
|
||||
iamService.createOpenIDConnectProvider(
|
||||
"https://OIDC.eks.eu-central-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B716D3041E",
|
||||
List.of(), List.of(THUMBPRINT), Map.of());
|
||||
|
||||
assertEquals(3, iamService.listOpenIDConnectProviders().size());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verified against a live AWS account: an empty tag map or key list is rejected as InvalidInput
|
||||
* rather than accepted as a no-op.
|
||||
*/
|
||||
@Test
|
||||
void oidcTagMutatorsRejectEmptyCollections() {
|
||||
OpenIDConnectProvider provider = iamService.createOpenIDConnectProvider(
|
||||
OIDC_URL, List.of(), List.of(THUMBPRINT), Map.of("env", "prod"));
|
||||
|
||||
assertEquals("InvalidInput", assertThrows(AwsException.class, () ->
|
||||
iamService.tagOpenIDConnectProvider(provider.getArn(), Map.of())).getErrorCode());
|
||||
assertEquals("InvalidInput", assertThrows(AwsException.class, () ->
|
||||
iamService.untagOpenIDConnectProvider(provider.getArn(), List.of())).getErrorCode());
|
||||
assertEquals("ValidationError", assertThrows(AwsException.class, () ->
|
||||
iamService.tagOpenIDConnectProvider(null, Map.of("k", "v"))).getErrorCode());
|
||||
|
||||
// The rejected calls leave the existing tags alone.
|
||||
assertEquals(Map.of("env", "prod"), iamService.listOpenIDConnectProviderTags(provider.getArn()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagAndUntagOpenIDConnectProvider() {
|
||||
OpenIDConnectProvider provider = iamService.createOpenIDConnectProvider(
|
||||
OIDC_URL, List.of(), List.of(THUMBPRINT), Map.of("env", "prod"));
|
||||
|
||||
assertEquals("prod", iamService.listOpenIDConnectProviderTags(provider.getArn()).get("env"));
|
||||
|
||||
iamService.tagOpenIDConnectProvider(provider.getArn(), Map.of("team", "platform"));
|
||||
assertEquals(2, iamService.listOpenIDConnectProviderTags(provider.getArn()).size());
|
||||
|
||||
iamService.untagOpenIDConnectProvider(provider.getArn(), List.of("env"));
|
||||
Map<String, String> remaining = iamService.listOpenIDConnectProviderTags(provider.getArn());
|
||||
assertEquals(1, remaining.size());
|
||||
assertEquals("platform", remaining.get("team"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user