mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(lakeformation): add UpdateResource action (#2944)
* feat(lakeformation): add UpdateResource action * fix(lakeformation): validate resource updates safely * fix(lakeformation): synchronize registerResource and pin the register/update race * fix(lakeformation): publish UpdateResource atomically to concurrent readers UpdateResource mutated the stored ResourceInfo field by field while DescribeResource and ListResources read the same object reference without synchronization, so a concurrent reader could observe a mixed state (e.g. new role ARN with old federation value). Apply copy-on-write: build a defensive copy, mutate the copy, then atomically put it back. Readers now see either the old complete state or the new complete state, never a partial one. Add a regression test that races DescribeResource against UpdateResource and asserts every observed snapshot is internally consistent. * fix(lakeformation): remove em-dash
This commit is contained in:
1 parent
9162b990ec
commit
ee24c77d02
10 files changed
+446
-2
No files matched your search
+57
@@ -279,6 +279,63 @@ class LakeFormationIntegrationTest {
|
||||
.body("__type", equalTo("EntityNotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateResourcePreservesOmittedValuesAndIsRegionScoped() {
|
||||
String arn = "arn:aws:s3:::update-resource-bucket";
|
||||
String initialRole = "arn:aws:iam::111122223333:role/initial";
|
||||
String updatedRole = "arn:aws:iam::111122223333:role/updated";
|
||||
String secondRegionHeader = "AWS4-HMAC-SHA256 Credential=AKIAIOSFODNN7EXAMPLE/20201022/us-west-2/lakeformation/aws4_request, SignedHeaders=host;x-amz-date, Signature=dummy";
|
||||
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("{\"ResourceArn\":\"" + arn + "\",\"RoleArn\":\"" + initialRole + "\",\"WithFederation\":true}")
|
||||
.when()
|
||||
.post("/RegisterResource")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("Authorization", secondRegionHeader)
|
||||
.body("{\"ResourceArn\":\"" + arn + "\",\"RoleArn\":\"" + initialRole + "\",\"WithFederation\":false}")
|
||||
.when()
|
||||
.post("/RegisterResource")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("{\"ResourceArn\":\"" + arn + "\",\"RoleArn\":\"" + updatedRole + "\"}")
|
||||
.when()
|
||||
.post("/UpdateResource")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("{\"ResourceArn\":\"" + arn + "\"}")
|
||||
.when()
|
||||
.post("/DescribeResource")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ResourceInfo.RoleArn", equalTo(updatedRole))
|
||||
.body("ResourceInfo.WithFederation", equalTo(true));
|
||||
|
||||
given()
|
||||
.contentType(CONTENT_TYPE)
|
||||
.header("Authorization", secondRegionHeader)
|
||||
.body("{\"ResourceArn\":\"" + arn + "\"}")
|
||||
.when()
|
||||
.post("/DescribeResource")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ResourceInfo.RoleArn", equalTo(initialRole))
|
||||
.body("ResourceInfo.WithFederation", equalTo(false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listAndDeleteLFTag() {
|
||||
// Create tag
|
||||
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
package io.github.hectorvent.floci.services.lakeformation;
|
||||
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.lakeformation.model.ResourceInfo;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* A register racing an update for the same resource must not be silently reverted.
|
||||
*
|
||||
* <p>The update is a read-modify-write under the storage monitor: read the record, mutate it,
|
||||
* write it back. A register landing between the read and the write would be overwritten by that
|
||||
* write, losing the fresh registration, so the register takes the same monitor the update and
|
||||
* deregister already hold.
|
||||
*/
|
||||
class LakeFormationResourceRaceTest {
|
||||
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String RESOURCE_ARN = "arn:aws:s3:::race-bucket";
|
||||
private static final String REGISTER_ROLE = "arn:aws:iam::000000000000:role/RegisterRole";
|
||||
private static final String UPDATE_ROLE = "arn:aws:iam::000000000000:role/UpdateRole";
|
||||
|
||||
private MemoryLakeFormationStorage newStorage() {
|
||||
StorageFactory storageFactory = Mockito.mock(StorageFactory.class);
|
||||
when(storageFactory.create(anyString(), anyString(), any()))
|
||||
.thenReturn(AccountAwareStorageBackend.inMemory("000000000000"));
|
||||
return new MemoryLakeFormationStorage(storageFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRegisterRacingAnUpdateIsNeverReverted() throws Exception {
|
||||
for (int attempt = 0; attempt < 50; attempt++) {
|
||||
MemoryLakeFormationStorage storage = newStorage();
|
||||
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
AtomicReference<Throwable> unexpected = new AtomicReference<>();
|
||||
|
||||
Thread registerer = new Thread(() -> {
|
||||
await(start);
|
||||
try {
|
||||
storage.registerResource(REGION, RESOURCE_ARN, REGISTER_ROLE, false, null);
|
||||
} catch (Throwable t) {
|
||||
unexpected.set(t);
|
||||
}
|
||||
});
|
||||
Thread updater = new Thread(() -> {
|
||||
await(start);
|
||||
try {
|
||||
storage.updateResource(REGION, RESOURCE_ARN, UPDATE_ROLE, null, null, null);
|
||||
} catch (io.github.hectorvent.floci.core.common.AwsException expected) {
|
||||
if (!"EntityNotFoundException".equals(expected.getErrorCode())) {
|
||||
unexpected.set(expected);
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
unexpected.set(t);
|
||||
}
|
||||
});
|
||||
|
||||
registerer.start();
|
||||
updater.start();
|
||||
start.countDown();
|
||||
registerer.join(TimeUnit.SECONDS.toMillis(10));
|
||||
updater.join(TimeUnit.SECONDS.toMillis(10));
|
||||
assertFalse(registerer.isAlive() || updater.isAlive(), "a thread never finished");
|
||||
assertNull(unexpected.get(), () -> "unexpected failure: " + unexpected.get());
|
||||
|
||||
// The register always lands, so the resource is there. The role names the last writer:
|
||||
// the register, or the update if it ran after. Anything else means a stale write
|
||||
// reverted the registration.
|
||||
Optional<ResourceInfo> stored = storage.describeResource(REGION, RESOURCE_ARN);
|
||||
assertTrue(stored.isPresent(), "the registration was lost (attempt " + attempt + ")");
|
||||
assertTrue(Set.of(REGISTER_ROLE, UPDATE_ROLE).contains(stored.get().getRoleArn()),
|
||||
"unexpected role " + stored.get().getRoleArn() + " (attempt " + attempt + ")");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void aConcurrentDescribeNeverObservesAPartialUpdate() throws Exception {
|
||||
String initialRole = "arn:aws:iam::000000000000:role/Initial";
|
||||
String updatedRole = "arn:aws:iam::000000000000:role/Updated";
|
||||
|
||||
for (int attempt = 0; attempt < 50; attempt++) {
|
||||
MemoryLakeFormationStorage storage = newStorage();
|
||||
storage.registerResource(REGION, RESOURCE_ARN, initialRole, false, true);
|
||||
|
||||
final int attemptNo = attempt;
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
AtomicReference<Throwable> unexpected = new AtomicReference<>();
|
||||
AtomicInteger observations = new AtomicInteger(0);
|
||||
|
||||
Thread updater = new Thread(() -> {
|
||||
await(start);
|
||||
try {
|
||||
storage.updateResource(REGION, RESOURCE_ARN, updatedRole, null, null, false);
|
||||
} catch (Throwable t) {
|
||||
unexpected.set(t);
|
||||
}
|
||||
});
|
||||
|
||||
Thread reader = new Thread(() -> {
|
||||
await(start);
|
||||
try {
|
||||
for (int i = 0; i < 200; i++) {
|
||||
Optional<ResourceInfo> info = storage.describeResource(REGION, RESOURCE_ARN);
|
||||
if (info.isPresent()) {
|
||||
ResourceInfo r = info.get();
|
||||
// Every observation must be a fully consistent snapshot:
|
||||
// either (initialRole, federation=true) or (updatedRole, federation=false).
|
||||
// A mixed state (e.g. updatedRole with federation=true) means the
|
||||
// reader observed a partially-mutated object.
|
||||
boolean isInitialState = updatedRole.equals(r.getRoleArn()) == false
|
||||
&& Boolean.TRUE.equals(r.getWithFederation());
|
||||
boolean isUpdatedState = updatedRole.equals(r.getRoleArn())
|
||||
&& Boolean.FALSE.equals(r.getWithFederation());
|
||||
assertTrue(isInitialState || isUpdatedState,
|
||||
"partial update observed: role=" + r.getRoleArn()
|
||||
+ ", withFederation=" + r.getWithFederation()
|
||||
+ " (attempt " + attemptNo + ")");
|
||||
observations.incrementAndGet();
|
||||
}
|
||||
}
|
||||
} catch (Throwable t) {
|
||||
unexpected.set(t);
|
||||
}
|
||||
});
|
||||
|
||||
updater.start();
|
||||
reader.start();
|
||||
start.countDown();
|
||||
updater.join(TimeUnit.SECONDS.toMillis(10));
|
||||
reader.join(TimeUnit.SECONDS.toMillis(10));
|
||||
assertFalse(updater.isAlive() || reader.isAlive(), "a thread never finished");
|
||||
assertNull(unexpected.get(), () -> "unexpected failure: " + unexpected.get());
|
||||
assertTrue(observations.get() > 0, "reader never observed the resource");
|
||||
|
||||
// After the update completes, the final state must be fully consistent.
|
||||
Optional<ResourceInfo> stored = storage.describeResource(REGION, RESOURCE_ARN);
|
||||
assertTrue(stored.isPresent());
|
||||
assertEquals(updatedRole, stored.get().getRoleArn());
|
||||
assertEquals(false, stored.get().getWithFederation());
|
||||
}
|
||||
}
|
||||
|
||||
private static void await(CountDownLatch latch) {
|
||||
try {
|
||||
latch.await();
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
+52
@@ -79,6 +79,58 @@ class LakeFormationServiceTest {
|
||||
assertEquals("InvalidInputException", ex.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateResource() {
|
||||
UpdateResourceRequest req = new UpdateResourceRequest();
|
||||
req.setResourceArn("arn:aws:s3:::my-bucket");
|
||||
req.setRoleArn("arn:aws-us-gov:iam::123:role/UpdatedRole");
|
||||
req.setHybridAccessEnabled(true);
|
||||
|
||||
UpdateResourceResponse res = service.updateResource("us-east-1", req);
|
||||
|
||||
assertNotNull(res);
|
||||
verify(storage).updateResource(
|
||||
"us-east-1", "arn:aws:s3:::my-bucket", "arn:aws-us-gov:iam::123:role/UpdatedRole", null, true, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateResourceNotFound() {
|
||||
UpdateResourceRequest req = new UpdateResourceRequest();
|
||||
req.setResourceArn("arn:aws:s3:::my-bucket");
|
||||
req.setRoleArn("arn:aws:iam::123:role/UpdatedRole");
|
||||
doThrow(new AwsException("EntityNotFoundException", "Resource not found", 400))
|
||||
.when(storage).updateResource(anyString(), anyString(), anyString(), any(), any(), any());
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () -> service.updateResource("us-east-1", req));
|
||||
|
||||
assertEquals("EntityNotFoundException", ex.getErrorCode());
|
||||
verify(storage).updateResource(anyString(), anyString(), anyString(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateResourceRejectsInvalidRequiredArn() {
|
||||
UpdateResourceRequest req = new UpdateResourceRequest();
|
||||
req.setResourceArn("not-an-arn");
|
||||
req.setRoleArn("not-an-arn");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () -> service.updateResource("us-east-1", req));
|
||||
|
||||
assertEquals("InvalidInputException", ex.getErrorCode());
|
||||
verifyNoInteractions(storage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testUpdateResourceRejectsMalformedResourceArn() {
|
||||
UpdateResourceRequest req = new UpdateResourceRequest();
|
||||
req.setResourceArn("arn:aws:s3");
|
||||
req.setRoleArn("arn:aws:iam::123:role/UpdatedRole");
|
||||
|
||||
AwsException ex = assertThrows(AwsException.class, () -> service.updateResource("us-east-1", req));
|
||||
|
||||
assertEquals("InvalidInputException", ex.getErrorCode());
|
||||
verifyNoInteractions(storage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDescribeResource() {
|
||||
DescribeResourceRequest req = new DescribeResourceRequest();
|
||||
|
||||
Reference in new issue
Block a user