From 5fbc51f53799f6fb704ab9d33f2bbe9b3ea19958 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Sun, 25 Oct 2026 15:34:42 -0500 Subject: [PATCH] refactor(eks): build and read ARNs through AwsArnUtils (#6484) EKS minted its access entry, addon and pod identity association ARNs and the STS assumed-role username by concatenating the parts of a split cluster or principal ARN, and read account and region from the cluster ARN by splitting it. Those now go through AwsArnUtils: parse for the fields, Arn.global for the assumed-role ARN, and one childArn helper that keeps the cluster's partition, region and account. The role ARN checks keep their conditions and errors. No behaviour change. EKS no longer has any arn-concat or arn-split site, so the partition baseline drops by 15. Signed-off-by: Hector Ventura --- .../services/eks/EksAccessEntryService.java | 32 ++++++++++--------- .../floci/services/eks/EksAddonService.java | 26 ++++++++------- .../floci/services/eks/EksClusterManager.java | 18 ++++------- .../eks/EksPodIdentityAssociationService.java | 18 +++++------ .../services/eks/EksVpcRouteProgramming.java | 10 ++---- tools/partition/baseline.tsv | 7 ---- 6 files changed, 50 insertions(+), 61 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksAccessEntryService.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksAccessEntryService.java index cfdca7406c..8be55fff9a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksAccessEntryService.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksAccessEntryService.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.eks; import com.fasterxml.jackson.core.type.TypeReference; +import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.Base64Url; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; @@ -61,21 +62,21 @@ public class EksAccessEntryService { throw invalid("EC2_LINUX entries cannot specify username or kubernetesGroups"); } String principal = request.principalArn(); - String[] arn = principal.split(":", 6); - String[] clusterArn = cluster.getArn().split(":", 6); - if (arn.length != 6 || !"arn".equals(arn[0]) || !arn[1].equals(clusterArn[1]) - || !"iam".equals(arn[2]) || !arn[3].isEmpty() || !arn[4].matches("[0-9]{12}") - || !(arn[5].startsWith("role/") || (!node && arn[5].startsWith("user/"))) - || arn[5].startsWith("role/aws-service-role/") - || principal.endsWith("/") || (node && !arn[4].equals(clusterArn[4]))) { + AwsArnUtils.Arn arn = AwsArnUtils.isArn(principal) ? AwsArnUtils.parse(principal) : null; + AwsArnUtils.Arn clusterArn = AwsArnUtils.parse(cluster.getArn()); + if (arn == null || !arn.partition().equals(clusterArn.partition()) + || !"iam".equals(arn.service()) || !arn.region().isEmpty() || !arn.accountId().matches("[0-9]{12}") + || !(arn.resource().startsWith("role/") || (!node && arn.resource().startsWith("user/"))) + || arn.resource().startsWith("role/aws-service-role/") + || principal.endsWith("/") || (node && !arn.accountId().equals(clusterArn.accountId()))) { throw invalid("principalArn must identify an IAM principal; EC2_LINUX requires a role in the cluster account"); } String name = principal.substring(principal.lastIndexOf('/') + 1); - boolean role = arn[5].startsWith("role/"); + boolean role = arn.resource().startsWith("role/"); String principalId = role - ? iam.findRole(arn[4], name).filter(value -> principal.equals(value.getArn())) + ? iam.findRole(arn.accountId(), name).filter(value -> principal.equals(value.getArn())) .map(IamRole::getRoleId).orElseThrow(() -> invalid("IAM role does not exist")) - : iam.findUser(arn[4], name).filter(value -> principal.equals(value.getArn())) + : iam.findUser(arn.accountId(), name).filter(value -> principal.equals(value.getArn())) .map(IamUser::getUserId).orElseThrow(() -> invalid("IAM user does not exist")); String username = node ? "system:node:{{EC2PrivateDNSName}}" : request.username(); if (username != null && !node && (username.isBlank() @@ -91,8 +92,8 @@ public class EksAccessEntryService { } } if (username == null) { - username = role ? "arn:" + arn[1] + ":sts::" + arn[4] + ":assumed-role/" + name + "/{{SessionName}}" - : principal; + username = role ? AwsArnUtils.Arn.global(arn.partition(), "sts", arn.accountId(), + "assumed-role/" + name + "/{{SessionName}}").toString() : principal; } List groups = node ? List.of("system:nodes") : request.kubernetesGroups() == null ? List.of() : request.kubernetesGroups(); @@ -125,9 +126,10 @@ public class EksAccessEntryService { throw new AwsException("ResourceInUseException", "Access entry already exists", 409); } double now = Instant.now().toEpochMilli() / 1000.0; - AccessEntry entry = new AccessEntry("arn:" + clusterArn[1] + ":eks:" + clusterArn[3] + ":" - + clusterArn[4] + ":access-entry/" + cluster.getName() + "/" + (role ? "role" : "user") - + "/" + arn[4] + "/" + name + "/" + UUID.randomUUID(), cluster.getName(), principal, + String entryArn = new AwsArnUtils.Arn(clusterArn.partition(), "eks", clusterArn.region(), clusterArn.accountId(), + "access-entry/" + cluster.getName() + "/" + (role ? "role" : "user") + "/" + arn.accountId() + "/" + name + + "/" + UUID.randomUUID()).toString(); + AccessEntry entry = new AccessEntry(entryArn, cluster.getName(), principal, type, username, List.copyOf(groups), Map.copyOf(tags), now, now); entries.put(key, new StoredEntry(entry, principalId, token)); return entry; diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksAddonService.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksAddonService.java index 29198071c7..bd5c0f6b82 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksAddonService.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksAddonService.java @@ -436,11 +436,8 @@ public class EksAddonService { PodIdentityAssociation created = podIdentityAssociations.create(cluster, req); arns.add(created.associationArn()); } else { - String[] clusterArn = cluster.getArn().split(":", 6); String assocId = "a-" + UUID.randomUUID().toString().replace("-", "").substring(0, 17); - String arn = "arn:" + clusterArn[1] + ":eks:" + clusterArn[3] + ":" - + clusterArn[4] + ":podidentityassociation/" + cluster.getName() + "/" + assocId; - arns.add(arn); + arns.add(childArn(cluster, "podidentityassociation/" + cluster.getName() + "/" + assocId)); } } return arns; @@ -467,9 +464,14 @@ public class EksAddonService { } private static String generateAddonArn(Cluster cluster, String addonName) { - String[] clusterArn = cluster.getArn().split(":", 6); - return "arn:" + clusterArn[1] + ":eks:" + clusterArn[3] + ":" - + clusterArn[4] + ":addon/" + cluster.getName() + "/" + addonName + "/" + UUID.randomUUID(); + return childArn(cluster, "addon/" + cluster.getName() + "/" + addonName + "/" + UUID.randomUUID()); + } + + /** An EKS resource in the cluster's partition, region and account. */ + private static String childArn(Cluster cluster, String resource) { + AwsArnUtils.Arn clusterArn = AwsArnUtils.parse(cluster.getArn()); + return new AwsArnUtils.Arn(clusterArn.partition(), "eks", clusterArn.region(), clusterArn.accountId(), resource) + .toString(); } private static void requireActiveCluster(Cluster cluster) { @@ -486,16 +488,16 @@ public class EksAddonService { if (roleArn == null || roleArn.isBlank()) { throw new AwsException("InvalidParameterException", fieldName + " is required", 400); } - String[] arn = roleArn.split(":", 6); - if (arn.length != 6 || !"arn".equals(arn[0]) || !arn[1].matches(AwsArnUtils.PARTITION_REGEX) - || !"iam".equals(arn[2]) || !arn[3].isEmpty() || !arn[4].matches("[0-9]{12}") - || !arn[5].startsWith("role/") || arn[5].startsWith("role/aws-service-role/") + AwsArnUtils.Arn arn = AwsArnUtils.isArn(roleArn) ? AwsArnUtils.parse(roleArn) : null; + if (arn == null || !arn.partition().matches(AwsArnUtils.PARTITION_REGEX) + || !"iam".equals(arn.service()) || !arn.region().isEmpty() || !arn.accountId().matches("[0-9]{12}") + || !arn.resource().startsWith("role/") || arn.resource().startsWith("role/aws-service-role/") || roleArn.endsWith("/")) { throw new AwsException("InvalidParameterException", fieldName + " must identify an IAM role", 400); } if (iam != null) { String roleName = roleArn.substring(roleArn.lastIndexOf('/') + 1); - iam.findRole(arn[4], roleName) + iam.findRole(arn.accountId(), roleName) .filter(role -> IamService.roleArnMatches(roleArn, role)) .orElseThrow(() -> new AwsException("InvalidParameterException", "Role not found: " + roleArn, 400)); } diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java index 271a520d28..a60d4bc651 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java @@ -1950,11 +1950,9 @@ public class EksClusterManager if (cluster.getAccountId() != null && !cluster.getAccountId().isBlank()) { return cluster.getAccountId(); } - if (cluster.getArn() != null) { - String[] parts = cluster.getArn().split(":"); - if (parts.length > 4 && !parts[4].isBlank()) { - return parts[4]; - } + String accountId = AwsArnUtils.accountOrDefault(cluster.getArn(), null); + if (accountId != null && !accountId.isBlank()) { + return accountId; } } if (regionResolver != null && regionResolver.getAccountId() != null && !regionResolver.getAccountId().isBlank()) { @@ -2349,7 +2347,7 @@ public class EksClusterManager // client-go replaces a server URL query when constructing its TokenReview request. String accountId = cluster.getAccountId() != null && !cluster.getAccountId().isBlank() ? cluster.getAccountId() - : (cluster.getArn() != null && cluster.getArn().split(":", 6).length > 4 ? cluster.getArn().split(":", 6)[4] : "000000000000"); + : AwsArnUtils.accountOrDefault(cluster.getArn(), "000000000000"); String region = AwsArnUtils.regionOrDefault(cluster.getArn(), defaultRegion); Instant createdAt = cluster.getCreatedAt() != null ? cluster.getCreatedAt() : Instant.EPOCH; return webhookPath(cluster.getName()) + "/scope/" + accountId @@ -2646,11 +2644,9 @@ public class EksClusterManager } String clusterRegion(Cluster cluster) { - if (cluster != null && cluster.getArn() != null) { - String[] parts = cluster.getArn().split(":"); - if (parts.length > 3 && !parts[3].isBlank()) { - return parts[3]; - } + String region = cluster == null ? null : AwsArnUtils.regionOrDefault(cluster.getArn(), null); + if (region != null && !region.isBlank()) { + return region; } if (regionResolver != null && regionResolver.getDefaultRegion() != null && !regionResolver.getDefaultRegion().isBlank()) { return regionResolver.getDefaultRegion(); diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAssociationService.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAssociationService.java index 8af2fb8355..7d31d27680 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAssociationService.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAssociationService.java @@ -317,24 +317,24 @@ public class EksPodIdentityAssociationService { * accepts the same role ARNs through {@link IamService#roleArnMatches}. */ private void validateRoleArn(String roleArn, String fieldName) { - String[] arn = roleArn.split(":", 6); - if (arn.length != 6 || !"arn".equals(arn[0]) || !arn[1].matches(AwsArnUtils.PARTITION_REGEX) - || !"iam".equals(arn[2]) || !arn[3].isEmpty() || !arn[4].matches("[0-9]{12}") - || !arn[5].startsWith("role/") || arn[5].startsWith("role/aws-service-role/") + AwsArnUtils.Arn arn = AwsArnUtils.isArn(roleArn) ? AwsArnUtils.parse(roleArn) : null; + if (arn == null || !arn.partition().matches(AwsArnUtils.PARTITION_REGEX) + || !"iam".equals(arn.service()) || !arn.region().isEmpty() || !arn.accountId().matches("[0-9]{12}") + || !arn.resource().startsWith("role/") || arn.resource().startsWith("role/aws-service-role/") || roleArn.endsWith("/")) { throw invalid(fieldName + " must identify an IAM role"); } String roleName = roleArn.substring(roleArn.lastIndexOf('/') + 1); - iam.findRole(arn[4], roleName) + iam.findRole(arn.accountId(), roleName) .filter(role -> IamService.roleArnMatches(roleArn, role)) .orElseThrow(() -> invalid("IAM role does not exist")); } private static void validateTargetRoleArn(String targetRoleArn) { - String[] arn = targetRoleArn.split(":", 6); - if (arn.length != 6 || !"arn".equals(arn[0]) || !arn[1].matches(AwsArnUtils.PARTITION_REGEX) - || !"iam".equals(arn[2]) || !arn[3].isEmpty() || !arn[4].matches("[0-9]{12}") - || !arn[5].startsWith("role/") || targetRoleArn.endsWith("/")) { + AwsArnUtils.Arn arn = AwsArnUtils.isArn(targetRoleArn) ? AwsArnUtils.parse(targetRoleArn) : null; + if (arn == null || !arn.partition().matches(AwsArnUtils.PARTITION_REGEX) + || !"iam".equals(arn.service()) || !arn.region().isEmpty() || !arn.accountId().matches("[0-9]{12}") + || !arn.resource().startsWith("role/") || targetRoleArn.endsWith("/")) { throw invalid("targetRoleArn must identify an IAM role"); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgramming.java b/src/main/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgramming.java index ad69e43175..a6bd741951 100644 --- a/src/main/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgramming.java +++ b/src/main/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgramming.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.eks; +import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.services.ec2.Ec2Service; import io.github.hectorvent.floci.services.ec2.model.Instance; @@ -269,13 +270,8 @@ public final class EksVpcRouteProgramming { if (isSet(cluster.getAccountId())) { return cluster.getAccountId(); } - if (isSet(cluster.getArn())) { - String[] parts = cluster.getArn().split(":"); - if (parts.length > 4 && isSet(parts[4])) { - return parts[4]; - } - } - return null; + String accountId = AwsArnUtils.accountOrDefault(cluster.getArn(), null); + return isSet(accountId) ? accountId : null; } diff --git a/tools/partition/baseline.tsv b/tools/partition/baseline.tsv index 61b0b75334..80552c13a7 100644 --- a/tools/partition/baseline.tsv +++ b/tools/partition/baseline.tsv @@ -4,8 +4,6 @@ arn-concat src/main/java/io/github/hectorvent/floci/services/appsync/graphql/aut arn-concat src/main/java/io/github/hectorvent/floci/services/controlcatalog/ControlCatalogService.java 1 arn-concat src/main/java/io/github/hectorvent/floci/services/dsql/DsqlClusterPolicies.java 1 arn-concat src/main/java/io/github/hectorvent/floci/services/ec2/Ec2InstanceCredentials.java 1 -arn-concat src/main/java/io/github/hectorvent/floci/services/eks/EksAccessEntryService.java 2 -arn-concat src/main/java/io/github/hectorvent/floci/services/eks/EksAddonService.java 2 arn-concat src/main/java/io/github/hectorvent/floci/services/iam/AwsManagedPolicies.java 2 arn-concat src/main/java/io/github/hectorvent/floci/services/route53resolver/Route53ResolverService.java 2 arn-concat src/main/java/io/github/hectorvent/floci/services/ses/SesService.java 1 @@ -64,11 +62,6 @@ arn-split src/main/java/io/github/hectorvent/floci/services/bedrockagentcorecont arn-split src/main/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsService.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/datasync/DataSyncService.java 3 arn-split src/main/java/io/github/hectorvent/floci/services/ec2/Ec2InstanceCredentials.java 3 -arn-split src/main/java/io/github/hectorvent/floci/services/eks/EksAccessEntryService.java 1 -arn-split src/main/java/io/github/hectorvent/floci/services/eks/EksAddonService.java 3 -arn-split src/main/java/io/github/hectorvent/floci/services/eks/EksClusterManager.java 4 -arn-split src/main/java/io/github/hectorvent/floci/services/eks/EksPodIdentityAssociationService.java 2 -arn-split src/main/java/io/github/hectorvent/floci/services/eks/EksVpcRouteProgramming.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/glue/GlueService.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/iam/PublicAccessConditions.java 1 arn-split src/main/java/io/github/hectorvent/floci/services/kms/KmsJsonHandler.java 1