Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(cloudwatch): mint the regionless dashboard ARN AWS uses (#5135)

Hector Venturac81290823bed
diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsService.java b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsService.java
index a0f4005623..b41bf4a13b 100644
--- a/src/main/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsService.java
+++ b/src/main/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsService.java
@@ -6,7 +6,9 @@ import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
+import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
+import io.github.hectorvent.floci.core.common.AwsRegions;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
@@ -21,6 +23,7 @@ import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
/**
* CloudWatch dashboards. Dashboards are pure metadata: the DashboardBody is an opaque JSON
@@ -81,9 +84,7 @@ public class CloudWatchDashboardsService {
validateDashboardBody(dashboardBody);
Dashboard existing = dashboardStore.get(key(region, dashboardName)).orElse(null);
- Dashboard dashboard = new Dashboard(dashboardName,
- regionResolver.buildArn("cloudwatch", region, "dashboard/" + dashboardName),
- dashboardBody);
+ Dashboard dashboard = new Dashboard(dashboardName, dashboardArn(region, dashboardName), dashboardBody);
dashboard.setLastModified(Instant.now().getEpochSecond());
if (existing != null) {
dashboard.setTags(new LinkedHashMap<>(existing.getTags()));
@@ -123,12 +124,15 @@ public class CloudWatchDashboardsService {
public Dashboard getDashboard(String dashboardName, String region) {
return dashboardStore.get(key(region, dashboardName))
+ .map(CloudWatchDashboardsService::withRegionlessArn)
.orElseThrow(() -> notFound(dashboardName));
}
public List<Dashboard> listDashboards(String dashboardNamePrefix, String region) {
String keyPrefix = region + "::";
- List<Dashboard> all = dashboardStore.scan(k -> k.startsWith(keyPrefix));
+ List<Dashboard> all = new ArrayList<>(dashboardStore.scan(k -> k.startsWith(keyPrefix)).stream()
+ .map(CloudWatchDashboardsService::withRegionlessArn)
+ .toList());
if (dashboardNamePrefix != null && !dashboardNamePrefix.isBlank()) {
all = new ArrayList<>(all.stream()
.filter(d -> d.getDashboardName().startsWith(dashboardNamePrefix))
@@ -206,38 +210,44 @@ public class CloudWatchDashboardsService {
"Dashboard does not exist: " + resourceArn, 404));
}
- private java.util.Optional<Dashboard> findByArn(String resourceArn, String region) {
+ private Optional<Dashboard> findByArn(String resourceArn, String region) {
return dashboardStore.scan(k -> k.startsWith(region + "::")).stream()
- .filter(d -> namesDashboard(resourceArn, d.getDashboardArn()))
+ .map(CloudWatchDashboardsService::withRegionlessArn)
+ .filter(d -> namesDashboard(resourceArn, d.getDashboardArn(), region))
.findFirst();
}
/**
- * Whether {@code candidate} is an ARN for the dashboard whose own ARN is {@code stored}.
+ * Whether {@code candidate} is an ARN for the dashboard whose regionless ARN is {@code arn}.
*
- * <p>Both the region-ful and the regionless form resolve, deliberately, because the two
- * disagree here and a caller may hold either. Dashboards are a global resource and AWS
- * documents their ARN without a region on both the TagResource and the ListTagsForResource
- * pages, {@code arn:aws:cloudwatch::<account-id>:dashboard/<dashboard-name>}, while Floci
- * mints a region-ful one. That divergence used to be invisible on this path: an ARN
- * matching nothing was a silent success answering an empty tag map. Now that it is an
- * error, a caller sending the documented ARN for a dashboard that exists would be told the
- * dashboard does not, so the lookup accepts both shapes.
- *
- * <p>What Floci mints is left alone here on purpose. Clients may already hold the ARNs it
- * has handed out, so bringing the minted shape into line with AWS is a separate change
- * from making the lookup tolerant, and only the second one is needed to keep this path
- * correct.
- *
- * <p>Only an absent region is accepted in place of the dashboard's own, so an ARN naming
- * some other region is still a miss rather than resolving to a same-named dashboard
- * elsewhere.
+ * <p>AWS's dashboard ARN has no Region ({@code arn:<partition>:cloudwatch::<account>:dashboard/<name>}),
+ * which is what Floci mints. Floci used to mint one with the Region of the request, and callers
+ * may still hold those, so that form resolves too, but only for the Region the dashboard is
+ * stored in: an ARN naming another Region is a miss rather than a same-named dashboard here.
*/
- private static boolean namesDashboard(String candidate, String stored) {
- if (candidate == null || stored == null) {
+ private static boolean namesDashboard(String candidate, String arn, String region) {
+ if (candidate == null || arn == null) {
return false;
}
- return candidate.equals(stored) || candidate.equals(withoutRegion(stored));
+ if (candidate.equals(arn)) {
+ return true;
+ }
+ String[] fields = candidate.split(":", 6);
+ return fields.length == 6 && region.equals(fields[3]) && withoutRegion(candidate).equals(arn);
+ }
+
+ /** The dashboard's ARN: regionless, in the partition of the Region it was put through. */
+ private String dashboardArn(String region, String dashboardName) {
+ return AwsArnUtils.Arn.global(AwsRegions.partitionFor(region), "cloudwatch",
+ regionResolver.getAccountId(), "dashboard/" + dashboardName).toString();
+ }
+
+ /** A dashboard stored before the ARN lost its Region reads back with the regionless one. */
+ private static Dashboard withRegionlessArn(Dashboard dashboard) {
+ if (dashboard.getDashboardArn() != null) {
+ dashboard.setDashboardArn(withoutRegion(dashboard.getDashboardArn()));
+ }
+ return dashboard;
}
/**
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/CloudWatchTagUnknownResourceIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/CloudWatchTagUnknownResourceIntegrationTest.java
index 8963e06460..5369185487 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/CloudWatchTagUnknownResourceIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/CloudWatchTagUnknownResourceIntegrationTest.java
@@ -192,10 +192,10 @@ class CloudWatchTagUnknownResourceIntegrationTest {
}
/**
- * The dashboard ARN AWS documents carries no region, and the one Floci mints does. A
- * caller sending the documented form for a dashboard that exists must not be told the
- * dashboard does not exist, which is what raising on a miss would otherwise have made of
- * a shape that used to fall through to an empty tag map.
+ * The dashboard ARN AWS documents carries no region, and it is the one Floci mints. A
+ * caller sending it for a dashboard that exists must not be told the dashboard does not
+ * exist, which is what raising on a miss would otherwise have made of a shape that used to
+ * fall through to an empty tag map.
*/
@Test
void theRegionlessDashboardArnAwsDocumentsAlsoResolves() {
diff --git a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsServiceTest.java
index fc21abdf30..ad18fccbbf 100644
--- a/src/test/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsServiceTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/cloudwatch/dashboards/CloudWatchDashboardsServiceTest.java
@@ -34,8 +34,32 @@ class CloudWatchDashboardsServiceTest {
Dashboard dashboard = service.getDashboard("ops", REGION);
assertEquals(BODY, dashboard.getDashboardBody());
assertEquals("ops", dashboard.getDashboardName());
- assertEquals("arn:aws:cloudwatch:us-east-1:000000000000:dashboard/ops",
- dashboard.getDashboardArn());
+ assertEquals("arn:aws:cloudwatch::000000000000:dashboard/ops", dashboard.getDashboardArn());
+ }
+
+ // Catches: a dashboard ARN minted with the request's Region; AWS's has none, in every partition.
+ @Test
+ void theDashboardArnIsRegionlessInTheRegionsPartition() {
+ service.putDashboard("ops", BODY, "cn-north-1");
+
+ assertEquals("arn:aws-cn:cloudwatch::000000000000:dashboard/ops",
+ service.getDashboard("ops", "cn-north-1").getDashboardArn());
+ }
+
+ // Catches: a dashboard persisted before the change reading back with its old region-ful ARN.
+ @Test
+ void aDashboardStoredWithTheOldRegionfulArnReadsBackRegionless() {
+ InMemoryStorage<String, Dashboard> store = new InMemoryStorage<>();
+ store.put(REGION + "::legacy", new Dashboard("legacy",
+ "arn:aws:cloudwatch:us-east-1:000000000000:dashboard/legacy", BODY));
+ CloudWatchDashboardsService legacy = new CloudWatchDashboardsService(store,
+ new RegionResolver(REGION, "000000000000"));
+
+ String regionless = "arn:aws:cloudwatch::000000000000:dashboard/legacy";
+ assertEquals(regionless, legacy.getDashboard("legacy", REGION).getDashboardArn());
+ assertEquals(regionless, legacy.listDashboards(null, REGION).get(0).getDashboardArn());
+ legacy.tagResource(regionless, Map.of("env", "dev"), REGION);
+ assertEquals(Map.of("env", "dev"), legacy.listTagsForResource(regionless, REGION));
}
@Test
@@ -151,31 +175,30 @@ class CloudWatchDashboardsServiceTest {
}
/**
- * AWS documents the dashboard ARN without a region and Floci mints one with a region, so
- * both forms have to resolve. Before the tag operations raised on a miss this divergence
- * was invisible here: the documented ARN matched nothing and got an empty tag map back.
+ * Floci mints the regionless ARN AWS uses, and used to mint one with the request's Region.
+ * Callers may still hold the old form, so both resolve to the same dashboard.
*/
@Test
- void bothTheRegionlessAndTheRegionfulDashboardArnResolve() {
+ void bothTheRegionlessAndTheOldRegionfulDashboardArnResolve() {
service.putDashboard("ops", BODY, REGION);
- String regionful = service.getDashboard("ops", REGION).getDashboardArn();
- String regionless = "arn:aws:cloudwatch::000000000000:dashboard/ops";
+ String regionless = service.getDashboard("ops", REGION).getDashboardArn();
+ String regionful = "arn:aws:cloudwatch:us-east-1:000000000000:dashboard/ops";
- assertEquals("arn:aws:cloudwatch:us-east-1:000000000000:dashboard/ops", regionful);
+ assertEquals("arn:aws:cloudwatch::000000000000:dashboard/ops", regionless);
assertTrue(CloudWatchDashboardsService.isDashboardArn(regionless));
- service.tagResource(regionless, Map.of("env", "dev"), REGION);
+ service.tagResource(regionful, Map.of("env", "dev"), REGION);
// Read back through the other form: one dashboard, reachable either way.
- assertEquals(Map.of("env", "dev"), service.listTagsForResource(regionful, REGION));
assertEquals(Map.of("env", "dev"), service.listTagsForResource(regionless, REGION));
+ assertEquals(Map.of("env", "dev"), service.listTagsForResource(regionful, REGION));
- service.untagResource(regionless, List.of("env"), REGION);
- assertEquals(Map.of(), service.listTagsForResource(regionful, REGION));
+ service.untagResource(regionful, List.of("env"), REGION);
+ assertEquals(Map.of(), service.listTagsForResource(regionless, REGION));
}
/**
- * Only an absent region stands in for the dashboard's own. An ARN naming a different
- * region must not resolve to the same-named dashboard in this one.
+ * The old region-ful form resolves only for the Region the dashboard was put through. An
+ * ARN naming a different Region must not resolve to the same-named dashboard in this one.
*/
@Test
void anArnNamingAnotherRegionDoesNotResolve() {