diff --git a/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java b/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java index d2429c09d1..496507fa13 100644 --- a/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java +++ b/src/main/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackend.java @@ -177,6 +177,11 @@ public class AccountAwareStorageBackend implements StorageBackend return delegate.scan(k -> true); } + /** Whether no account holds a value. */ + public boolean isEmptyInAllAccounts() { + return delegate.isEmpty(); + } + /** Removes matching values using their actual stored keys, including unprefixed legacy keys. */ public int deleteAllAccountsMatching(Predicate valueFilter) { int removed = 0; diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontDistributionFilter.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontDistributionFilter.java index 4275295f35..04491c76b4 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontDistributionFilter.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontDistributionFilter.java @@ -55,7 +55,12 @@ public class CloudFrontDistributionFilter implements ContainerRequestFilter { return; } - Distribution dist = service.get().findByHost(host); + CloudFrontService cloudFront = service.get(); + if (!cloudFront.hasDistributions()) { + return; + } + + Distribution dist = cloudFront.findByHost(host); if (dist == null) { return; } diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java index 0380f47d16..d6b0459f11 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java @@ -10,6 +10,7 @@ import io.github.hectorvent.floci.core.common.CloudFrontEdgeDomain; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.common.SmithyValidationErrors; import io.github.hectorvent.floci.core.common.dns.EmbeddedDnsServer; +import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.acm.AcmCertificateReferenceProvider.CertificateReference; @@ -119,7 +120,7 @@ public class CloudFrontService implements ServerCertificateReferenceProvider { private static final Map MANAGED_CACHE_POLICIES = managedCachePolicies(); private static final String MANAGED_POLICY_ETAG = "E23ZP02F085DFQ"; - private final StorageBackend distStore; + private final AccountAwareStorageBackend distStore; private final StorageBackend> invalidationStore; private final StorageBackend cachePolicyStore; private final StorageBackend orpStore; @@ -560,6 +561,10 @@ public class CloudFrontService implements ServerCertificateReferenceProvider { distStore.put(targetDistributionId, dist); } + public boolean hasDistributions() { + return !distStore.isEmptyInAllAccounts(); + } + /** * Finds the distribution whose data-plane requests should be served for the given {@code Host} * header. A distribution matches when the host equals its assigned CloudFront domain name diff --git a/src/test/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackendTest.java b/src/test/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackendTest.java index 30d01a21d1..92b5bc46bc 100644 --- a/src/test/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackendTest.java +++ b/src/test/java/io/github/hectorvent/floci/core/storage/AccountAwareStorageBackendTest.java @@ -52,6 +52,22 @@ class AccountAwareStorageBackendTest { assertTrue(second.isEmpty()); } + @Test + void isEmptyInAllAccountsSeesEveryAccount() { + InMemoryStorage raw = new InMemoryStorage<>(); + AccountAwareStorageBackend first = + new AccountAwareStorageBackend<>(raw, null, "111111111111"); + AccountAwareStorageBackend second = + new AccountAwareStorageBackend<>(raw, null, "222222222222"); + + assertTrue(second.isEmptyInAllAccounts()); + + first.put("resource", "value"); + + assertTrue(second.isEmpty()); + assertFalse(second.isEmptyInAllAccounts()); + } + @Test void putAllPrefixesEveryEntryWithTheRequestScope() { InMemoryStorage raw = new InMemoryStorage<>();