mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -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
+6
@@ -78,6 +78,12 @@ public class LakeFormationController {
|
||||
return handleResponse(service.registerResource(regionResolver.resolveRegion(headers), mapper.treeToValue(parse(body), RegisterResourceRequest.class)));
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/UpdateResource")
|
||||
public Response updateResource(@Context HttpHeaders headers, String body) throws Exception {
|
||||
return handleResponse(service.updateResource(regionResolver.resolveRegion(headers), mapper.treeToValue(parse(body), UpdateResourceRequest.class)));
|
||||
}
|
||||
|
||||
@POST
|
||||
@Path("/DeregisterResource")
|
||||
public Response deregisterResource(@Context HttpHeaders headers, String body) throws Exception {
|
||||
|
||||
+51
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.lakeformation;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.services.lakeformation.model.*;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -55,6 +56,56 @@ public class LakeFormationService {
|
||||
return new RegisterResourceResponse();
|
||||
}
|
||||
|
||||
public UpdateResourceResponse updateResource(String region, UpdateResourceRequest request) {
|
||||
validateResourceArn(request.getResourceArn());
|
||||
validateRoleArn(request.getRoleArn());
|
||||
|
||||
storage.updateResource(
|
||||
region,
|
||||
request.getResourceArn(),
|
||||
request.getRoleArn(),
|
||||
request.getExpectedResourceOwnerAccount(),
|
||||
request.getHybridAccessEnabled(),
|
||||
request.getWithFederation()
|
||||
);
|
||||
return new UpdateResourceResponse();
|
||||
}
|
||||
|
||||
private static void validateResourceArn(String resourceArn) {
|
||||
if (resourceArn == null) {
|
||||
throw new AwsException("InvalidInputException", "ResourceArn is required", 400);
|
||||
}
|
||||
try {
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(resourceArn);
|
||||
if (parsed.partition().isBlank() || parsed.service().isBlank() || parsed.resource().isBlank()
|
||||
|| resourceArn.chars().anyMatch(Character::isWhitespace)) {
|
||||
throw new IllegalArgumentException("Invalid resource ARN");
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("InvalidInputException", "ResourceArn must be a valid ARN", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateRoleArn(String roleArn) {
|
||||
if (roleArn == null) {
|
||||
throw new AwsException("InvalidInputException", "RoleArn is required", 400);
|
||||
}
|
||||
try {
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(roleArn);
|
||||
if (parsed.partition().isBlank()
|
||||
|| !"iam".equals(parsed.service())
|
||||
|| !parsed.region().isEmpty()
|
||||
|| parsed.accountId().isBlank()
|
||||
|| !parsed.resource().startsWith("role/")
|
||||
|| parsed.resource().length() == "role/".length()
|
||||
|| roleArn.chars().anyMatch(Character::isWhitespace)) {
|
||||
throw new IllegalArgumentException("Invalid IAM role ARN");
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("InvalidInputException", "RoleArn must be a valid IAM role ARN", 400);
|
||||
}
|
||||
}
|
||||
|
||||
public DeregisterResourceResponse deregisterResource(String region, DeregisterResourceRequest request) {
|
||||
if (request.getResourceArn() == null) {
|
||||
throw new AwsException("InvalidInputException", "ResourceArn is required", 400);
|
||||
|
||||
+2
@@ -10,6 +10,8 @@ public interface LakeFormationStorage {
|
||||
Optional<DataLakeSettings> getDataLakeSettings(String region, String catalogId);
|
||||
|
||||
void registerResource(String region, String resourceArn, String roleArn, boolean useServiceLinkedRole, Boolean withFederation);
|
||||
void updateResource(String region, String resourceArn, String roleArn, String expectedResourceOwnerAccount,
|
||||
Boolean hybridAccessEnabled, Boolean withFederation);
|
||||
void deregisterResource(String region, String resourceArn);
|
||||
List<ResourceInfo> listResources(String region, List<FilterCondition> filterConditions, Integer maxResults, String nextToken);
|
||||
Optional<ResourceInfo> describeResource(String region, String resourceArn);
|
||||
|
||||
+39
-2
@@ -42,7 +42,7 @@ public class MemoryLakeFormationStorage implements LakeFormationStorage {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerResource(String region, String resourceArn, String roleArn, boolean useServiceLinkedRole, Boolean withFederation) {
|
||||
public synchronized void registerResource(String region, String resourceArn, String roleArn, boolean useServiceLinkedRole, Boolean withFederation) {
|
||||
ResourceInfo info = new ResourceInfo();
|
||||
info.setResourceArn(resourceArn);
|
||||
info.setRoleArn(roleArn);
|
||||
@@ -52,7 +52,44 @@ public class MemoryLakeFormationStorage implements LakeFormationStorage {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deregisterResource(String region, String resourceArn) {
|
||||
public synchronized void updateResource(String region, String resourceArn, String roleArn, String expectedResourceOwnerAccount,
|
||||
Boolean hybridAccessEnabled, Boolean withFederation) {
|
||||
String resourceKey = region + ":" + resourceArn;
|
||||
ResourceInfo existing = resourcesStorage.get(resourceKey)
|
||||
.orElseThrow(() -> new io.github.hectorvent.floci.core.common.AwsException(
|
||||
"EntityNotFoundException", "Resource not found", 400));
|
||||
// Work on a defensive copy so concurrent readers (DescribeResource, ListResources)
|
||||
// never observe a partially-mutated ResourceInfo: they see either the old complete
|
||||
// state or the new complete state, never a mix of field values.
|
||||
ResourceInfo updated = copyOf(existing);
|
||||
updated.setRoleArn(roleArn);
|
||||
if (expectedResourceOwnerAccount != null) {
|
||||
updated.setExpectedResourceOwnerAccount(expectedResourceOwnerAccount);
|
||||
}
|
||||
if (hybridAccessEnabled != null) {
|
||||
updated.setHybridAccessEnabled(hybridAccessEnabled);
|
||||
}
|
||||
if (withFederation != null) {
|
||||
updated.setWithFederation(withFederation);
|
||||
}
|
||||
resourcesStorage.put(resourceKey, updated);
|
||||
}
|
||||
|
||||
private static ResourceInfo copyOf(ResourceInfo source) {
|
||||
ResourceInfo copy = new ResourceInfo();
|
||||
copy.setExpectedResourceOwnerAccount(source.getExpectedResourceOwnerAccount());
|
||||
copy.setHybridAccessEnabled(source.getHybridAccessEnabled());
|
||||
copy.setLastModified(source.getLastModified());
|
||||
copy.setResourceArn(source.getResourceArn());
|
||||
copy.setRoleArn(source.getRoleArn());
|
||||
copy.setVerificationStatus(source.getVerificationStatus());
|
||||
copy.setWithFederation(source.getWithFederation());
|
||||
copy.setWithPrivilegedAccess(source.getWithPrivilegedAccess());
|
||||
return copy;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void deregisterResource(String region, String resourceArn) {
|
||||
resourcesStorage.delete(region + ":" + resourceArn);
|
||||
}
|
||||
|
||||
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package io.github.hectorvent.floci.services.lakeformation.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class UpdateResourceRequest {
|
||||
|
||||
private String expectedResourceOwnerAccount;
|
||||
private Boolean hybridAccessEnabled;
|
||||
private String resourceArn;
|
||||
private String roleArn;
|
||||
private Boolean withFederation;
|
||||
|
||||
public String getExpectedResourceOwnerAccount() {
|
||||
return expectedResourceOwnerAccount;
|
||||
}
|
||||
|
||||
public void setExpectedResourceOwnerAccount(String expectedResourceOwnerAccount) {
|
||||
this.expectedResourceOwnerAccount = expectedResourceOwnerAccount;
|
||||
}
|
||||
|
||||
public Boolean getHybridAccessEnabled() {
|
||||
return hybridAccessEnabled;
|
||||
}
|
||||
|
||||
public void setHybridAccessEnabled(Boolean hybridAccessEnabled) {
|
||||
this.hybridAccessEnabled = hybridAccessEnabled;
|
||||
}
|
||||
|
||||
public String getResourceArn() {
|
||||
return resourceArn;
|
||||
}
|
||||
|
||||
public void setResourceArn(String resourceArn) {
|
||||
this.resourceArn = resourceArn;
|
||||
}
|
||||
|
||||
public String getRoleArn() {
|
||||
return roleArn;
|
||||
}
|
||||
|
||||
public void setRoleArn(String roleArn) {
|
||||
this.roleArn = roleArn;
|
||||
}
|
||||
|
||||
public Boolean getWithFederation() {
|
||||
return withFederation;
|
||||
}
|
||||
|
||||
public void setWithFederation(Boolean withFederation) {
|
||||
this.withFederation = withFederation;
|
||||
}
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
package io.github.hectorvent.floci.services.lakeformation.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
|
||||
import com.fasterxml.jackson.databind.annotation.JsonNaming;
|
||||
import io.quarkus.runtime.annotations.RegisterForReflection;
|
||||
|
||||
@RegisterForReflection
|
||||
@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class UpdateResourceResponse {
|
||||
}
|
||||
Reference in new issue
Block a user