fix(s3tables): validate legacy bucket ownership before migration (#8534)

S3 Tables now migrates an unprefixed legacy table bucket only when the
caller is the configured default account and the bucket's ARN matches the
stored one, on a lookup and on CreateTableBucket's existence check. A
lookup from another account, or with a mismatched ARN, answers
NotFoundException and leaves the legacy record in place, and another
account creating a bucket of the same name gets its own.
This commit is contained in:
Jackson Price authored and GitHub committed 2026-11-16 07:24:17 -04:00
1 parent 8ec2c184f1
commit 8625f09f4c
2 files changed
+126 -3

No files matched your search

@@ -47,10 +47,11 @@ public class S3TablesService {
Object storageClassConfiguration, Map<String, String> tags, String region) {
validateBucketName(name);
String key = storageKey(region, name);
if (store.get(key).isPresent()) {
String arn = bucketArn(region, name);
if (findTableBucket(key, arn).isPresent()) {
throw conflict("The table bucket " + name + " already exists.");
}
TableBucket bucket = new TableBucket(name, bucketArn(region, name), now(), regionResolver.getAccountId(),
TableBucket bucket = new TableBucket(name, arn, now(), regionResolver.getAccountId(),
encryptionConfiguration, storageClassConfiguration, tags);
store.put(key, bucket);
LOG.infov("Created S3 Tables bucket {0} in region {1}", name, region);
@@ -59,7 +60,8 @@ public class S3TablesService {
public synchronized TableBucket getTableBucket(String tableBucketArn, String region) {
String name = resourceName(tableBucketArn, BUCKET_PREFIX);
TableBucket bucket = store.get(storageKey(region, name))
String key = storageKey(region, name);
TableBucket bucket = findTableBucket(key, tableBucketArn)
.orElseThrow(() -> notFound("The table bucket does not exist."));
if (!bucket.getArn().equals(tableBucketArn)) {
throw notFound("The table bucket does not exist.");
@@ -487,6 +489,15 @@ public class S3TablesService {
return region + "::" + name;
}
private Optional<TableBucket> findTableBucket(String key, String tableBucketArn) {
if (store instanceof AccountAwareStorageBackend<TableBucket> aware) {
String accountId = regionResolver.getAccountId();
return aware.getForAccountMigratingLegacy(accountId, key,
legacy -> accountId.equals(aware.defaultAccountId()) && tableBucketArn.equals(legacy.getArn()));
}
return store.get(key);
}
/** The bucket stored under {@code key} in the account, or the default account's legacy one, read only. */
private Optional<TableBucket> stored(String accountId, String key) {
return store instanceof AccountAwareStorageBackend<TableBucket> aware
@@ -0,0 +1,112 @@
package io.floci.aws.services.s3tables;
import io.floci.aws.core.common.AwsException;
import io.floci.aws.core.common.RegionResolver;
import io.floci.aws.core.common.RequestContext;
import io.floci.aws.core.storage.AccountAwareStorageBackend;
import io.floci.aws.core.storage.InMemoryStorage;
import io.floci.aws.core.storage.StorageFactory;
import io.floci.aws.services.s3.S3Service;
import io.floci.aws.services.s3tables.model.TableBucket;
import jakarta.enterprise.inject.Instance;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Map;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class S3TablesLegacyBucketLookupTest {
private static final String OWNER = "000000000000";
private static final String OTHER = "111111111111";
private static final String REGION = "us-east-1";
private static final String KEY = REGION + "::legacy-bucket";
private static final String ARN = "arn:aws:s3tables:" + REGION + ":" + OWNER + ":bucket/legacy-bucket";
private static final Map<String, String> CONFIGURATION = Map.of("sseAlgorithm", "aws:kms", "kmsKeyArn", "example-key");
private final InMemoryStorage<String, TableBucket> raw = new InMemoryStorage<>();
private final RequestContext context = mock(RequestContext.class);
private S3TablesService service;
private TableBucket bucket;
@BeforeEach
void setUp() {
@SuppressWarnings("unchecked")
Instance<RequestContext> contexts = mock(Instance.class);
when(contexts.get()).thenReturn(context);
when(context.getAccountId()).thenReturn(OWNER);
StorageFactory factory = mock(StorageFactory.class);
when(factory.<TableBucket>create(anyString(), anyString(), any()))
.thenReturn(new AccountAwareStorageBackend<>(raw, contexts, OWNER));
RegionResolver resolver = mock(RegionResolver.class);
when(resolver.getAccountId()).thenAnswer(invocation -> context.getAccountId());
service = new S3TablesService(factory, resolver, mock(S3Service.class));
bucket = new TableBucket("legacy-bucket", ARN, "2026-01-01T00:00:00Z", OWNER,
CONFIGURATION, null, Map.of());
raw.put(KEY, bucket);
}
@Test
void foreignAccountCannotReadOrMigrateTheLegacyBucket() {
when(context.getAccountId()).thenReturn(OTHER);
AwsException failure = assertThrows(AwsException.class, () -> service.getTableBucket(ARN, REGION));
assertEquals("NotFoundException", failure.getErrorCode());
assertEquals(Set.of(KEY), raw.keys());
assertSame(bucket, raw.get(KEY).orElseThrow());
}
@Test
void wrongArnDoesNotMigrateTheOwnersLegacyBucket() {
String wrongArn = ARN.replace(OWNER, OTHER);
AwsException failure = assertThrows(AwsException.class, () -> service.getTableBucket(wrongArn, REGION));
assertEquals("NotFoundException", failure.getErrorCode());
assertEquals(Set.of(KEY), raw.keys());
assertSame(bucket, raw.get(KEY).orElseThrow());
}
@Test
void foreignAccountCreatesItsOwnBucketWithoutMovingLegacyBucket() {
when(context.getAccountId()).thenReturn(OTHER);
TableBucket created = service.createTableBucket("legacy-bucket", null, null, Map.of(), REGION);
assertEquals(ARN.replace(OWNER, OTHER), created.getArn());
assertEquals(Set.of(KEY, OTHER + "/" + KEY), raw.keys());
assertSame(bucket, raw.get(KEY).orElseThrow());
assertSame(created, service.getTableBucket(created.getArn(), REGION));
AwsException failure = assertThrows(AwsException.class, () -> service.getTableBucket(ARN, REGION));
assertEquals("NotFoundException", failure.getErrorCode());
when(context.getAccountId()).thenReturn(OWNER);
assertSame(bucket, service.getTableBucket(ARN, REGION));
assertEquals(Set.of(OWNER + "/" + KEY, OTHER + "/" + KEY), raw.keys());
assertSame(created, raw.get(OTHER + "/" + KEY).orElseThrow());
}
@Test
void ownerCannotCreateDuplicateLegacyBucket() {
AwsException failure = assertThrows(AwsException.class,
() -> service.createTableBucket("legacy-bucket", null, null, Map.of(), REGION));
assertEquals("ConflictException", failure.getErrorCode());
assertEquals(Set.of(OWNER + "/" + KEY), raw.keys());
assertSame(bucket, service.getTableBucket(ARN, REGION));
}
@Test
void ownerReadsAndDeletesTheLegacyBucket() {
assertSame(bucket, service.getTableBucket(ARN, REGION));
assertEquals(Set.of(OWNER + "/" + KEY), raw.keys());
service.deleteTableBucket(ARN, REGION);
assertTrue(raw.isEmpty());
}
}