refactor: read and check ARNs through AwsArnUtils in RAM, Resource Groups, CloudWatch, Service Catalog and four more (#6904)

Seven startsWith("arn:") probes become AwsArnUtils.looksLikeArn. RAM, the Resource Groups Tagging visibility check, the CloudWatch dashboards cross-account ARN and Service Catalog read ARN fields with AwsArnUtils.parse and regionOrDefault instead of a split; the dashboards ARN without its region is rebuilt as an AwsArnUtils.Arn.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-10-29 07:31:03 -04:00
1 parent 66a5c05037
commit 03da5da997
11 files changed
+25 -35

No files matched your search

@@ -814,7 +814,7 @@ public class BudgetsService implements Resettable {
private ResourceRef parseResourceArn(String arn) {
String marker = ":budgets::";
int service = arn.indexOf(marker);
if (!arn.startsWith("arn:") || service < 0) {
if (!AwsArnUtils.looksLikeArn(arn) || service < 0) {
throw invalid("ResourceARN is not a valid Budgets ARN.");
}
int resourceStart = service + marker.length();
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cloudfront;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.config.LocalStackParity;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.CloudFrontEdgeDomain;
import io.github.hectorvent.floci.core.common.RegionResolver;
@@ -2159,7 +2160,7 @@ public class CloudFrontService implements ServerCertificateReferenceProvider {
}
public RealtimeLogConfig getRealtimeLogConfig(String nameOrArn) {
if (nameOrArn != null && nameOrArn.startsWith("arn:")) {
if (AwsArnUtils.looksLikeArn(nameOrArn)) {
String name = nameOrArn.substring(nameOrArn.lastIndexOf('/') + 1);
return realtimeLogConfigStore.get(name).orElseThrow(() ->
new AwsException("NoSuchRealtimeLogConfig",
@@ -922,7 +922,7 @@ public class CloudTrailService {
private Trail findTrail(String region, String nameOrArn) {
// ARN → cross-region scan is valid (callers use ARN to target another Region)
if (nameOrArn != null && nameOrArn.startsWith("arn:")) {
if (AwsArnUtils.looksLikeArn(nameOrArn)) {
for (String k : store.keys()) {
CloudTrailEntry entry = store.get(k).orElse(null);
if (entry == null) continue;
@@ -257,12 +257,12 @@ public class CloudWatchDashboardsService {
* returned unchanged, since there is no region field to blank.
*/
private static String withoutRegion(String arn) {
String[] fields = arn.split(":", 6);
if (fields.length < 6) {
if (!AwsArnUtils.isArn(arn)) {
return arn;
}
fields[3] = "";
return String.join(":", fields);
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
return new AwsArnUtils.Arn(parsed.partition(), parsed.service(), "", parsed.accountId(), parsed.resource())
.toString();
}
private static String key(String region, String dashboardName) {
@@ -319,7 +319,7 @@ public class CloudWatchLogsCrossAccountService {
}
}
private static void requireArn(String value, String field) {
if (value == null || !value.startsWith("arn:") || value.length() < 10) {
if (!AwsArnUtils.looksLikeArn(value) || value.length() < 10) {
throw invalid(field + " must be a valid ARN.");
}
}
@@ -313,8 +313,7 @@ public class RamService {
/** {@code arn:aws:ram:<region>:<account>:resource-share/<id>} → {@code <region>}. */
private static String extractRegion(String resourceShareArn) {
String[] parts = resourceShareArn.split(":", 6);
return parts.length < 4 ? "" : parts[3];
return AwsArnUtils.regionOrDefault(resourceShareArn, "");
}
private void putInvitation(ResourceShareInvitation invitation) {
@@ -761,12 +760,12 @@ public class RamService {
/** {@code arn:aws:ec2:...:transit-gateway/tgw-1} → {@code ec2:TransitGateway}. */
private static String ramResourceType(String resourceArn) {
String[] parts = resourceArn.split(":", 6);
if (parts.length < 6) {
if (!AwsArnUtils.isArn(resourceArn)) {
return "";
}
String service = parts[2];
String resource = parts[5];
AwsArnUtils.Arn parsed = AwsArnUtils.parse(resourceArn);
String service = parsed.service();
String resource = parsed.resource();
int separator = indexOfTypeSeparator(resource);
String typeSegment = separator >= 0 ? resource.substring(0, separator) : resource;
StringBuilder camel = new StringBuilder();
@@ -90,7 +90,7 @@ public class ResourceGroupsService implements Resettable {
public List<String> capacityReservationMembers(String region, String account, String groupArn) {
// EC2 holds its capacity lock; GroupResources calls EC2 while holding this service's monitor.
// Read the immutable storage snapshot without acquiring that monitor in the reverse order.
if (groupArn == null || !groupArn.startsWith("arn:")) {
if (!AwsArnUtils.looksLikeArn(groupArn)) {
throw bad("A capacity reservation group ARN is required.");
}
return selected(region, account, JsonNodeFactory.instance.objectNode().put("Group", groupArn)).members();
@@ -222,7 +222,7 @@ public class ResourceGroupsService implements Resettable {
}
String selected = text(request, request.has("Group") ? "Group" : "GroupName", true);
String name = selected;
if (selected.startsWith("arn:")) {
if (AwsArnUtils.looksLikeArn(selected)) {
if (!AwsArnUtils.isArn(selected)) {
throw bad("Invalid group ARN.");
}
@@ -437,11 +437,14 @@ public class ResourceGroupsTaggingService implements Resettable {
// Region and account come from the ARN (arn:<partition>:svc:region:acct:resource), and a string
// that is not an ARN, or an ARN without a region, stays visible as it always has.
private static boolean isVisible(String arn, String region, String accountId) {
String[] parts = arn.split(":", 6);
if (parts.length >= 4 && !parts[3].isEmpty() && !parts[3].equals(region)) {
if (!AwsArnUtils.isArn(arn)) {
return true;
}
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
if (!parsed.region().isEmpty() && !parsed.region().equals(region)) {
return false;
}
return accountId == null || parts.length < 5 || parts[4].isEmpty() || parts[4].equals(accountId);
return accountId == null || parsed.accountId().isEmpty() || parsed.accountId().equals(accountId);
}
// A provider resource's region is its ARN's, else the owner's region for it. Null means it has none,
@@ -1326,11 +1326,8 @@ public class ServiceCatalogService implements PassedRoleLookup {
ObjectNode instance = objectMapper.createObjectNode();
instance.put("Account", text(product, "PhysicalId"));
String arn = text(product, "Arn");
if (arn != null) {
String[] parts = arn.split(":");
if (parts.length >= 4) {
instance.put("Region", parts[3]);
}
if (AwsArnUtils.isArn(arn)) {
instance.put("Region", AwsArnUtils.parse(arn).region());
}
instance.put("StackInstanceStatus", "CURRENT");
return List.of(instance);
@@ -1118,7 +1118,7 @@ public class SsmService implements ResourceProvider {
}
private String parameterName(String nameOrArn, String region, boolean parameterRead) {
if (nameOrArn != null && nameOrArn.startsWith("arn:")) {
if (AwsArnUtils.looksLikeArn(nameOrArn)) {
try {
AwsArnUtils.Arn arn = AwsArnUtils.parse(nameOrArn);
if ("ssm".equals(arn.service()) && arn.resource().startsWith("parameter")) {