mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(storage): preserve account namespaces during legacy fallback (#8251)
Signed-off-by: Jackson Price <59149601+jprice8@users.noreply.github.com> Co-authored-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
1 parent
486f0e4604
commit
e201709363
5 files changed
+430
-5
No files matched your search
@@ -0,0 +1,183 @@
|
||||
package io.floci.aws.core.storage;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.clearInvocations;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
class AccountAwareLegacyKeyIsolationTest {
|
||||
private static final String DEFAULT_ACCOUNT = "111111111111";
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"111111111111", "222222222222"})
|
||||
void ambientPrefixAliasCannotReadOrMoveAnAccountScopedRawRecord(String owner) {
|
||||
InMemoryStorage<String, OwnedValue> raw = spy(new InMemoryStorage<>());
|
||||
AccountAwareStorageBackend<OwnedValue> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, DEFAULT_ACCOUNT);
|
||||
String alias = owner + "/resource";
|
||||
OwnedValue value = new OwnedValue(owner, "original");
|
||||
raw.put(alias, value);
|
||||
clearInvocations(raw);
|
||||
|
||||
Optional<OwnedValue> actual = storage.get(alias);
|
||||
|
||||
assertAll(
|
||||
() -> assertTrue(actual.isEmpty()),
|
||||
() -> assertEquals(Set.of(alias), raw.keys()),
|
||||
() -> assertSame(value, raw.scan(alias::equals).stream().findFirst().orElse(null)),
|
||||
() -> verify(raw, never()).get(alias));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"111111111111", "222222222222"})
|
||||
void singleKeyMigrationCannotReadOrMoveAnAccountScopedRawRecord(String owner) {
|
||||
InMemoryStorage<String, OwnedValue> raw = spy(new InMemoryStorage<>());
|
||||
AccountAwareStorageBackend<OwnedValue> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, DEFAULT_ACCOUNT);
|
||||
String alias = owner + "/resource";
|
||||
OwnedValue value = new OwnedValue(owner, "original");
|
||||
raw.put(alias, value);
|
||||
clearInvocations(raw);
|
||||
|
||||
Optional<OwnedValue> actual = storage.getForAccountMigratingLegacy(
|
||||
DEFAULT_ACCOUNT, alias, candidate -> true);
|
||||
|
||||
assertAll(
|
||||
() -> assertTrue(actual.isEmpty()),
|
||||
() -> assertEquals(Set.of(alias), raw.keys()),
|
||||
() -> assertSame(value, raw.scan(alias::equals).stream().findFirst().orElse(null)),
|
||||
() -> verify(raw, never()).get(alias));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"111111111111", "222222222222"})
|
||||
void ambientLookupPreservesExplicitScopedPrefixShapedLogicalKeys(String prefix) {
|
||||
InMemoryStorage<String, OwnedValue> raw = new InMemoryStorage<>();
|
||||
AccountAwareStorageBackend<OwnedValue> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, DEFAULT_ACCOUNT);
|
||||
String logicalKey = prefix + "/resource";
|
||||
OwnedValue scoped = new OwnedValue(DEFAULT_ACCOUNT, "scoped");
|
||||
OwnedValue other = new OwnedValue(prefix, "other raw record");
|
||||
storage.put(logicalKey, scoped);
|
||||
raw.put(logicalKey, other);
|
||||
|
||||
assertSame(scoped, storage.get(logicalKey).orElseThrow());
|
||||
assertSame(other, raw.get(logicalKey).orElseThrow());
|
||||
assertEquals(Set.of(logicalKey, DEFAULT_ACCOUNT + "/" + logicalKey), raw.keys());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"111111111111", "222222222222"})
|
||||
void singleKeyLookupPreservesExplicitScopedPrefixShapedLogicalKeys(String prefix) {
|
||||
InMemoryStorage<String, OwnedValue> raw = new InMemoryStorage<>();
|
||||
AccountAwareStorageBackend<OwnedValue> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, DEFAULT_ACCOUNT);
|
||||
String logicalKey = prefix + "/resource";
|
||||
OwnedValue scoped = new OwnedValue(DEFAULT_ACCOUNT, "scoped");
|
||||
OwnedValue other = new OwnedValue(prefix, "other raw record");
|
||||
storage.putForAccount(DEFAULT_ACCOUNT, logicalKey, scoped);
|
||||
raw.put(logicalKey, other);
|
||||
|
||||
assertSame(scoped, storage.getForAccountMigratingLegacy(
|
||||
DEFAULT_ACCOUNT, logicalKey, candidate -> false).orElseThrow());
|
||||
assertSame(other, raw.get(logicalKey).orElseThrow());
|
||||
assertEquals(Set.of(logicalKey, DEFAULT_ACCOUNT + "/" + logicalKey), raw.keys());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ambientLookupStillMigratesGenuineDefaultAccountLegacyData() {
|
||||
InMemoryStorage<String, OwnedValue> raw = new InMemoryStorage<>();
|
||||
AccountAwareStorageBackend<OwnedValue> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, DEFAULT_ACCOUNT);
|
||||
OwnedValue legacy = new OwnedValue(DEFAULT_ACCOUNT, "legacy");
|
||||
raw.put("legacy/path", legacy);
|
||||
|
||||
assertSame(legacy, storage.get("legacy/path").orElseThrow());
|
||||
assertEquals(Set.of(DEFAULT_ACCOUNT + "/legacy/path"), raw.keys());
|
||||
assertSame(legacy, raw.get(DEFAULT_ACCOUNT + "/legacy/path").orElseThrow());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"111111111111", "222222222222"})
|
||||
void singleKeyMigrationRetainsProvenLegacyOwnersIncludingNondefaultAccounts(String owner) {
|
||||
InMemoryStorage<String, OwnedValue> raw = new InMemoryStorage<>();
|
||||
AccountAwareStorageBackend<OwnedValue> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, DEFAULT_ACCOUNT);
|
||||
OwnedValue legacy = new OwnedValue(owner, "legacy");
|
||||
raw.put("legacy/path", legacy);
|
||||
|
||||
assertSame(legacy, storage.getForAccountMigratingLegacy(
|
||||
owner, "legacy/path", candidate -> owner.equals(candidate.owner())).orElseThrow());
|
||||
assertEquals(Set.of(owner + "/legacy/path"), raw.keys());
|
||||
assertSame(legacy, raw.get(owner + "/legacy/path").orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void singleKeyMigrationPreservesLegacyDataWhenOwnerPredicateRejectsIt() {
|
||||
InMemoryStorage<String, OwnedValue> raw = new InMemoryStorage<>();
|
||||
AccountAwareStorageBackend<OwnedValue> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, DEFAULT_ACCOUNT);
|
||||
OwnedValue legacy = new OwnedValue("222222222222", "legacy");
|
||||
raw.put("resource", legacy);
|
||||
|
||||
assertTrue(storage.getForAccountMigratingLegacy(DEFAULT_ACCOUNT, "resource",
|
||||
candidate -> DEFAULT_ACCOUNT.equals(candidate.owner())).isEmpty());
|
||||
assertEquals(Set.of("resource"), raw.keys());
|
||||
assertSame(legacy, raw.get("resource").orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
void pluralMigrationStillAcceptsAnIntentionalKnownAccountScopedLegacyKey() {
|
||||
InMemoryStorage<String, OwnedValue> raw = new InMemoryStorage<>();
|
||||
AccountAwareStorageBackend<OwnedValue> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, DEFAULT_ACCOUNT);
|
||||
OwnedValue legacy = new OwnedValue(DEFAULT_ACCOUNT, "legacy");
|
||||
raw.put(DEFAULT_ACCOUNT + "/resource", legacy);
|
||||
|
||||
assertSame(legacy, storage.getForAccountMigratingLegacyKeys(
|
||||
DEFAULT_ACCOUNT, "us-west-2::resource", List.of("resource"),
|
||||
candidate -> DEFAULT_ACCOUNT.equals(candidate.owner())).orElseThrow());
|
||||
assertEquals(Set.of(DEFAULT_ACCOUNT + "/us-west-2::resource"), raw.keys());
|
||||
assertSame(legacy, raw.get(DEFAULT_ACCOUNT + "/us-west-2::resource").orElseThrow());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void rejectedMigrationWritePreservesTheGenuineLegacyRecord(boolean explicitAccount) {
|
||||
InMemoryStorage<String, OwnedValue> raw = spy(new InMemoryStorage<>());
|
||||
AccountAwareStorageBackend<OwnedValue> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, DEFAULT_ACCOUNT);
|
||||
OwnedValue legacy = new OwnedValue(DEFAULT_ACCOUNT, "legacy");
|
||||
raw.put("resource", legacy);
|
||||
doThrow(new IllegalStateException("injected pre-write failure"))
|
||||
.when(raw).put(DEFAULT_ACCOUNT + "/resource", legacy);
|
||||
|
||||
assertThrows(IllegalStateException.class, () -> {
|
||||
if (explicitAccount) {
|
||||
storage.getForAccountMigratingLegacy(DEFAULT_ACCOUNT, "resource",
|
||||
candidate -> DEFAULT_ACCOUNT.equals(candidate.owner()));
|
||||
} else {
|
||||
storage.get("resource");
|
||||
}
|
||||
});
|
||||
|
||||
assertEquals(Set.of("resource"), raw.keys());
|
||||
assertSame(legacy, raw.get("resource").orElseThrow());
|
||||
}
|
||||
|
||||
private record OwnedValue(String owner, String marker) {}
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
package io.floci.aws.services.s3;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
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.PersistentStorage;
|
||||
import io.floci.aws.services.s3.model.Bucket;
|
||||
import io.floci.aws.services.s3.model.S3Object;
|
||||
import jakarta.enterprise.inject.Instance;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static java.nio.charset.StandardCharsets.UTF_8;
|
||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class S3LegacyNumericBucketTest {
|
||||
|
||||
private static final String OWNER = "111111111111";
|
||||
private static final String CALLER = "222222222222";
|
||||
private static final String BUCKET = "333333333333";
|
||||
private static final String KEY = "orders/report.csv";
|
||||
private static final String STORE_KEY = BUCKET + "/" + KEY;
|
||||
|
||||
@TempDir
|
||||
Path directory;
|
||||
|
||||
private final AtomicReference<String> caller = new AtomicReference<>(OWNER);
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void persistedLegacyObjectIsReadAndMigratedIntoTheBucketOwnersAccount(boolean global) throws Exception {
|
||||
PersistentStorage<String, S3Object> raw = persistedObject(BUCKET, KEY);
|
||||
S3Service service = service(raw, global);
|
||||
service.createBucket(BUCKET, "us-east-1");
|
||||
Path legacyFile = directory.resolve(BUCKET).resolve(KEY + ".s3data");
|
||||
Files.createDirectories(legacyFile.getParent());
|
||||
Files.writeString(legacyFile, "legacy-body", UTF_8);
|
||||
if (global) {
|
||||
caller.set(CALLER);
|
||||
}
|
||||
|
||||
assertArrayEquals("legacy-body".getBytes(UTF_8), service.getObject(BUCKET, KEY).getData());
|
||||
|
||||
PersistentStorage<String, S3Object> reloaded = reload();
|
||||
assertFalse(reloaded.get(STORE_KEY).isPresent());
|
||||
assertTrue(reloaded.get(OWNER + "/" + STORE_KEY).isPresent());
|
||||
assertFalse(reloaded.get(CALLER + "/" + STORE_KEY).isPresent());
|
||||
assertArrayEquals("legacy-body".getBytes(UTF_8),
|
||||
Files.readAllBytes(directory.resolve(".accounts").resolve(OWNER)
|
||||
.resolve(BUCKET).resolve(KEY + ".s3data")));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void anAccountScopedObjectAtTheSameRawKeyIsNeitherReadNorMoved(boolean global) {
|
||||
PersistentStorage<String, S3Object> raw = persistedObject("orders", "report.csv");
|
||||
S3Service service = service(raw, global);
|
||||
caller.set(BUCKET);
|
||||
service.createBucket("orders", "us-east-1");
|
||||
caller.set(OWNER);
|
||||
service.createBucket(BUCKET, "us-east-1");
|
||||
if (global) {
|
||||
caller.set(CALLER);
|
||||
}
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.getObject(BUCKET, KEY));
|
||||
|
||||
assertEquals("NoSuchKey", failure.getErrorCode());
|
||||
assertEquals("orders", raw.get(STORE_KEY).orElseThrow().getBucketName());
|
||||
assertFalse(raw.get(OWNER + "/" + STORE_KEY).isPresent());
|
||||
assertFalse(raw.get(CALLER + "/" + STORE_KEY).isPresent());
|
||||
caller.set(BUCKET);
|
||||
assertEquals("report.csv", service.headObject("orders", "report.csv").getKey());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void aMismatchedObjectKeyIsNotMigrated(boolean global) {
|
||||
PersistentStorage<String, S3Object> raw = persistedObject(BUCKET, "different-key");
|
||||
S3Service service = service(raw, global);
|
||||
service.createBucket(BUCKET, "us-east-1");
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.getObject(BUCKET, KEY));
|
||||
|
||||
assertEquals("NoSuchKey", failure.getErrorCode());
|
||||
assertEquals("different-key", raw.get(STORE_KEY).orElseThrow().getKey());
|
||||
assertFalse(raw.get(OWNER + "/" + STORE_KEY).isPresent());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void anotherAccountsSameNamedBucketCannotClaimLegacyData(boolean global) {
|
||||
PersistentStorage<String, S3Object> raw = persistedObject(BUCKET, KEY);
|
||||
S3Service service = service(raw, global);
|
||||
caller.set(CALLER);
|
||||
service.createBucket(BUCKET, "us-east-1");
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> service.getObject(BUCKET, KEY));
|
||||
|
||||
assertEquals("NoSuchKey", failure.getErrorCode());
|
||||
assertTrue(raw.get(STORE_KEY).isPresent());
|
||||
assertFalse(raw.get(CALLER + "/" + STORE_KEY).isPresent());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void anExistingScopedObjectWinsWithoutRemovingTheLegacyRecord(boolean global) {
|
||||
PersistentStorage<String, S3Object> raw = persistedObject(BUCKET, KEY);
|
||||
S3Service service = service(raw, global);
|
||||
service.createBucket(BUCKET, "us-east-1");
|
||||
service.putObject(BUCKET, KEY, "new-body".getBytes(UTF_8), "text/plain", Map.of());
|
||||
raw.put(STORE_KEY, new S3Object(BUCKET, KEY, "legacy-body".getBytes(UTF_8), "text/plain"));
|
||||
|
||||
assertArrayEquals("new-body".getBytes(UTF_8), service.getObject(BUCKET, KEY).getData());
|
||||
assertTrue(raw.get(STORE_KEY).isPresent());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void aPersistedLegacyVersionIsMatchedByItsObjectKeyAndVersion(boolean global) {
|
||||
PersistentStorage<String, S3Object> raw = reload();
|
||||
S3Object version = new S3Object(BUCKET, KEY, "old-version".getBytes(UTF_8), "text/plain");
|
||||
version.setVersionId("old-version-id");
|
||||
String versionKey = STORE_KEY + "#v#old-version-id";
|
||||
raw.put(versionKey, version);
|
||||
raw = reload();
|
||||
S3Service service = service(raw, global);
|
||||
service.createBucket(BUCKET, "us-east-1");
|
||||
if (global) {
|
||||
caller.set(CALLER);
|
||||
}
|
||||
|
||||
assertEquals("old-version-id", service.headObject(BUCKET, KEY, "old-version-id").getVersionId());
|
||||
assertFalse(raw.get(versionKey).isPresent());
|
||||
assertTrue(raw.get(OWNER + "/" + versionKey).isPresent());
|
||||
}
|
||||
|
||||
private PersistentStorage<String, S3Object> persistedObject(String bucket, String key) {
|
||||
PersistentStorage<String, S3Object> raw = reload();
|
||||
raw.put(STORE_KEY, new S3Object(bucket, key, "legacy-body".getBytes(UTF_8), "text/plain"));
|
||||
return reload();
|
||||
}
|
||||
|
||||
private PersistentStorage<String, S3Object> reload() {
|
||||
PersistentStorage<String, S3Object> raw = new PersistentStorage<>(
|
||||
directory.resolve("objects.json"), new TypeReference<Map<String, S3Object>>() { });
|
||||
raw.load();
|
||||
return raw;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private S3Service service(PersistentStorage<String, S3Object> raw, boolean global) {
|
||||
RequestContext request = mock(RequestContext.class);
|
||||
when(request.getAccountId()).thenAnswer(ignored -> caller.get());
|
||||
Instance<RequestContext> context = mock(Instance.class);
|
||||
when(context.get()).thenReturn(request);
|
||||
AccountAwareStorageBackend<Bucket> buckets =
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), context, OWNER);
|
||||
AccountAwareStorageBackend<S3Object> objects = new AccountAwareStorageBackend<>(raw, context, OWNER);
|
||||
return new S3Service(buckets, objects, directory, false, global);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user