mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(cloudformation): provision Transfer Family servers (#4809)
* feat(cloudformation): provision Transfer Family servers * fix(cloudformation): clear Transfer update snapshot after success * test(cloudformation): cover Transfer tag rollback * fix(cloudformation): match Transfer server return values
This commit is contained in:
1 parent
2bdfe45207
commit
bcb50b765f
13 files changed
+944
-19
No files matched your search
+14
-7
@@ -738,7 +738,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
String templateBody = cs.getTemplateBody();
|
||||
Map<String, String> params = cs.getParameters() != null ? cs.getParameters() : Map.of();
|
||||
|
||||
return submitOperation(() -> runUnderAccount(accountId, () -> {
|
||||
return submitOperation(() -> runUnderScope(accountId, region, () -> {
|
||||
executeTemplate(stack, templateBody, params, isCreate, region, accountId);
|
||||
String status = stack.getStatus();
|
||||
cs.setExecutionStatus(status != null && (status.contains("ROLLBACK") || status.endsWith("_FAILED"))
|
||||
@@ -748,11 +748,10 @@ public class CloudFormationService implements ResourceProvider {
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs {@code body} under a synthetic CDI request scope whose account is {@code accountId}, so
|
||||
* that account-aware storage in the downstream services namespaces provisioned resources under
|
||||
* the intended account. Mirrors the pattern used by other background workers.
|
||||
* Runs {@code body} under the stack's account and region, so downstream services resolve the
|
||||
* same scope as the CloudFormation request that submitted this background operation.
|
||||
*/
|
||||
private void runUnderAccount(String accountId, Runnable body) {
|
||||
private void runUnderScope(String accountId, String region, Runnable body) {
|
||||
ManagedContext requestContext = Arc.container().requestContext();
|
||||
boolean alreadyActive = requestContext.isActive();
|
||||
if (!alreadyActive) {
|
||||
@@ -760,19 +759,27 @@ public class CloudFormationService implements ResourceProvider {
|
||||
}
|
||||
// Background workers normally have no active scope, so a fresh one is activated and
|
||||
// terminated below. But if we ran inside an already-active scope, restore its previous
|
||||
// account afterwards so we never leave the overridden account ID behind on a reused thread.
|
||||
// scope afterwards so we never leave the overridden values behind on a reused thread.
|
||||
RequestContext ctx = Arc.container().instance(RequestContext.class).get();
|
||||
String previousAccountId = alreadyActive ? ctx.getAccountId() : null;
|
||||
String previousRegion = alreadyActive ? ctx.getRegion() : null;
|
||||
String previousPartition = alreadyActive ? ctx.getPartition() : null;
|
||||
try {
|
||||
if (accountId != null) {
|
||||
ctx.setAccountId(accountId);
|
||||
}
|
||||
if (region != null) {
|
||||
ctx.setRegion(region);
|
||||
ctx.setPartition(regionResolver.partitionForRegion(region));
|
||||
}
|
||||
body.run();
|
||||
} finally {
|
||||
if (!alreadyActive) {
|
||||
requestContext.terminate();
|
||||
} else {
|
||||
ctx.setAccountId(previousAccountId);
|
||||
ctx.setRegion(previousRegion);
|
||||
ctx.setPartition(previousPartition);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -836,7 +843,7 @@ public class CloudFormationService implements ResourceProvider {
|
||||
"AWS::CloudFormation::Stack", "DELETE_IN_PROGRESS", null);
|
||||
|
||||
try {
|
||||
return submitOperation(() -> runUnderAccount(accountId,
|
||||
return submitOperation(() -> runUnderScope(accountId, region,
|
||||
() -> deleteStackResources(stack, region, accountId)));
|
||||
} catch (AwsException e) {
|
||||
if ("LimitExceededException".equals(e.getErrorCode())) {
|
||||
|
||||
+231
@@ -0,0 +1,231 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.transfer.TransferService;
|
||||
import io.github.hectorvent.floci.services.transfer.model.Server;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
import jakarta.inject.Inject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/** CloudFormation lifecycle for a Transfer Family server. */
|
||||
@ApplicationScoped
|
||||
public class TransferCfnProvisioner implements CfnResourceProvisioner {
|
||||
|
||||
private static final String TYPE = "AWS::Transfer::Server";
|
||||
private static final String SECURITY_POLICY_DEFAULT = "TransferSecurityPolicy-2020-06";
|
||||
private static final Set<String> SUPPORTED_PROPERTIES = Set.of("Domain", "Protocols", "EndpointType",
|
||||
"EndpointDetails", "IdentityProviderType", "IdentityProviderDetails", "LoggingRole",
|
||||
"SecurityPolicyName", "Tags");
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
private static final String UPDATE_SNAPSHOT = "__FlociTransferServerUpdateSnapshot";
|
||||
private static final String TEMPLATE_TAG_KEYS = "__FlociTransferServerTemplateTagKeys";
|
||||
|
||||
private final TransferService transferService;
|
||||
|
||||
@Inject
|
||||
public TransferCfnProvisioner(TransferService transferService) {
|
||||
this.transferService = transferService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> resourceTypes() {
|
||||
return Set.of(TYPE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void provision(StackResource resource, JsonNode props, ProvisionContext ctx) {
|
||||
resource.getAttributes().remove(UPDATE_SNAPSHOT);
|
||||
validateProperties(props);
|
||||
String domain = ctx.resolveOrDefault(props, "Domain", "S3");
|
||||
JsonNode protocolNode = props != null ? props.get("Protocols") : null;
|
||||
JsonNode resolvedProtocols = protocolNode != null ? ctx.engine().resolveNode(protocolNode) : null;
|
||||
boolean protocolsOmitted = resolvedProtocols == null || resolvedProtocols.isNull()
|
||||
|| (resolvedProtocols.isTextual() && resolvedProtocols.asText().isBlank());
|
||||
List<String> protocols = protocolsOmitted ? List.of("SFTP") : ctx.resolveStringList(props, "Protocols");
|
||||
if (protocols.isEmpty()) {
|
||||
throw new AwsException("ValidationError", "AWS::Transfer::Server Protocols must not be empty", 400);
|
||||
}
|
||||
String endpointType = ctx.resolveOrDefault(props, "EndpointType", "PUBLIC");
|
||||
Map<String, Object> endpointDetails = resolvedMap(props, "EndpointDetails", ctx,
|
||||
new TypeReference<Map<String, Object>>() {});
|
||||
String identityProviderType = ctx.resolveOrDefault(props, "IdentityProviderType", "SERVICE_MANAGED");
|
||||
Map<String, String> identityProviderDetails = resolvedMap(props, "IdentityProviderDetails", ctx,
|
||||
new TypeReference<Map<String, String>>() {});
|
||||
String loggingRole = ctx.resolveOptional(props, "LoggingRole");
|
||||
if (loggingRole != null && loggingRole.isBlank()) {
|
||||
loggingRole = null;
|
||||
}
|
||||
String securityPolicyName = ctx.resolveOrDefault(props, "SecurityPolicyName", SECURITY_POLICY_DEFAULT);
|
||||
Map<String, String> tags = ctx.resolveTags(props, "Tags");
|
||||
|
||||
Server server;
|
||||
if (ctx.isUpdate()) {
|
||||
Server existing = transferService.getServer(serverId(ctx.priorPhysicalId()));
|
||||
rejectUnsupportedChange("Domain", existing.getDomain(), domain);
|
||||
rejectUnsupportedChange("IdentityProviderType", existing.getIdentityProviderType(), identityProviderType);
|
||||
Map<String, String> previousTags = transferService.listTagsForResource(existing.getArn());
|
||||
String previousTemplateTagKeys = resource.getAttributes().get(TEMPLATE_TAG_KEYS);
|
||||
ConfigurationSnapshot previous = new ConfigurationSnapshot(existing.getServerId(),
|
||||
existing.getProtocols(), existing.getEndpointType(), existing.getEndpointDetails(),
|
||||
existing.getIdentityProviderDetails(), existing.getLoggingRole(),
|
||||
existing.getSecurityPolicyName(), previousTags, new ArrayList<>(tags.keySet()),
|
||||
previousTemplateTagKeys);
|
||||
resource.getAttributes().put(UPDATE_SNAPSHOT, MAPPER.valueToTree(previous).toString());
|
||||
try {
|
||||
server = transferService.replaceServerConfiguration(existing.getServerId(), protocols, endpointType,
|
||||
endpointDetails, identityProviderDetails, loggingRole, securityPolicyName);
|
||||
reconcileTags(server.getArn(), managedTagKeys(previousTemplateTagKeys), tags);
|
||||
} catch (RuntimeException failure) {
|
||||
try {
|
||||
rollbackUpdate(resource);
|
||||
resource.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_RESTORED_ATTR, "true");
|
||||
} catch (RuntimeException restoreFailure) {
|
||||
resource.getAttributes().put(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR,
|
||||
"Could not restore Transfer server " + existing.getServerId() + ": "
|
||||
+ restoreFailure.getMessage());
|
||||
failure.addSuppressed(restoreFailure);
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
} else {
|
||||
server = transferService.createServer(ctx.region(), domain, protocols, endpointType, endpointDetails,
|
||||
identityProviderType, identityProviderDetails, loggingRole, securityPolicyName, tags);
|
||||
}
|
||||
recordServer(resource, server);
|
||||
resource.getAttributes().put(TEMPLATE_TAG_KEYS, MAPPER.valueToTree(tags.keySet()).toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(String resourceType, String physicalId, String region) {
|
||||
CfnDeletes.safeDelete("Transfer server", physicalId,
|
||||
() -> transferService.deleteServer(serverId(physicalId)), "ResourceNotFoundException");
|
||||
}
|
||||
|
||||
@Override
|
||||
public UpdateCleanupResult completeUpdate(StackResource resource) {
|
||||
if ("UPDATE_COMPLETE".equals(resource.getStatus())) {
|
||||
resource.getAttributes().remove(UPDATE_SNAPSHOT);
|
||||
}
|
||||
return UpdateCleanupResult.notApplicable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean rollbackUpdate(StackResource resource) {
|
||||
String saved = resource.getAttributes().get(UPDATE_SNAPSHOT);
|
||||
if (saved == null) {
|
||||
return true;
|
||||
}
|
||||
ConfigurationSnapshot previous;
|
||||
try {
|
||||
previous = MAPPER.readValue(saved, ConfigurationSnapshot.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalStateException("Could not read Transfer server update snapshot", e);
|
||||
}
|
||||
Server server = transferService.replaceServerConfiguration(previous.serverId(), previous.protocols(),
|
||||
previous.endpointType(), previous.endpointDetails(), previous.identityProviderDetails(),
|
||||
previous.loggingRole(), previous.securityPolicyName());
|
||||
reconcileTags(server.getArn(), previous.newTagKeys(), previous.tags());
|
||||
recordServer(resource, server);
|
||||
if (previous.previousTemplateTagKeys() == null) {
|
||||
resource.getAttributes().remove(TEMPLATE_TAG_KEYS);
|
||||
} else {
|
||||
resource.getAttributes().put(TEMPLATE_TAG_KEYS, previous.previousTemplateTagKeys());
|
||||
}
|
||||
resource.getAttributes().remove(UPDATE_SNAPSHOT);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void recordServer(StackResource resource, Server server) {
|
||||
resource.setPhysicalId(server.getArn());
|
||||
resource.getAttributes().put("ServerId", server.getServerId());
|
||||
resource.getAttributes().put("Arn", server.getArn());
|
||||
resource.getAttributes().put("State", server.getState());
|
||||
}
|
||||
|
||||
private static String serverId(String physicalId) {
|
||||
if (!AwsArnUtils.isArn(physicalId)) {
|
||||
return physicalId;
|
||||
}
|
||||
AwsArnUtils.Arn arn = AwsArnUtils.parse(physicalId);
|
||||
if (!"transfer".equals(arn.service()) || !arn.resource().startsWith("server/")) {
|
||||
throw new AwsException("ValidationError", "Invalid Transfer server ARN: " + physicalId, 400);
|
||||
}
|
||||
return arn.resource().substring("server/".length());
|
||||
}
|
||||
|
||||
private void reconcileTags(String arn, List<String> managedKeys, Map<String, String> desired) {
|
||||
List<String> staleTags = managedKeys.stream().filter(key -> !desired.containsKey(key)).toList();
|
||||
if (!staleTags.isEmpty()) {
|
||||
transferService.untagResource(arn, staleTags);
|
||||
}
|
||||
if (!desired.isEmpty()) {
|
||||
transferService.tagResource(arn, desired);
|
||||
}
|
||||
}
|
||||
|
||||
private record ConfigurationSnapshot(String serverId, List<String> protocols, String endpointType,
|
||||
Map<String, Object> endpointDetails,
|
||||
Map<String, String> identityProviderDetails, String loggingRole,
|
||||
String securityPolicyName, Map<String, String> tags,
|
||||
List<String> newTagKeys, String previousTemplateTagKeys) {
|
||||
}
|
||||
|
||||
private static List<String> managedTagKeys(String stored) {
|
||||
if (stored == null) {
|
||||
return List.of();
|
||||
}
|
||||
try {
|
||||
return MAPPER.readValue(stored, new TypeReference<List<String>>() {});
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new IllegalStateException("Could not read Transfer server template tag keys", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateProperties(JsonNode props) {
|
||||
if (props == null || props.isNull()) {
|
||||
return;
|
||||
}
|
||||
if (!props.isObject()) {
|
||||
throw new AwsException("ValidationError", "AWS::Transfer::Server Properties must be an object", 400);
|
||||
}
|
||||
props.fieldNames().forEachRemaining(name -> {
|
||||
if (!SUPPORTED_PROPERTIES.contains(name)) {
|
||||
throw new AwsException("ValidationError",
|
||||
"AWS::Transfer::Server property " + name + " is not supported by Floci", 400);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static <T> T resolvedMap(JsonNode props, String name, ProvisionContext ctx, TypeReference<T> type) {
|
||||
if (props == null || !props.has(name) || props.get(name).isNull()) {
|
||||
return null;
|
||||
}
|
||||
JsonNode resolved = ctx.engine().resolveNode(props.get(name));
|
||||
if (resolved == null || resolved.isNull()
|
||||
|| (resolved.isTextual() && resolved.asText().isBlank())) {
|
||||
return null;
|
||||
}
|
||||
if (!resolved.isObject()) {
|
||||
throw new AwsException("ValidationError", "AWS::Transfer::Server " + name + " must be an object", 400);
|
||||
}
|
||||
return MAPPER.convertValue(resolved, type);
|
||||
}
|
||||
|
||||
private static void rejectUnsupportedChange(String name, String current, String requested) {
|
||||
if (!Objects.equals(current, requested)) {
|
||||
throw new AwsException("ValidationError",
|
||||
"Updating " + name + " is not supported by Floci.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,10 +75,18 @@ public class TransferService {
|
||||
server.setTags(tags != null ? tags : new HashMap<>());
|
||||
server.setCreationTime(Instant.now());
|
||||
|
||||
putServer(server);
|
||||
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
putTags("server/" + serverId, tags);
|
||||
try {
|
||||
putServer(server);
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
putTags("server/" + serverId, tags);
|
||||
}
|
||||
} catch (RuntimeException failure) {
|
||||
try {
|
||||
deleteServerRecord(server);
|
||||
} catch (RuntimeException cleanupFailure) {
|
||||
failure.addSuppressed(cleanupFailure);
|
||||
}
|
||||
throw failure;
|
||||
}
|
||||
|
||||
return server;
|
||||
@@ -175,6 +183,25 @@ public class TransferService {
|
||||
return server;
|
||||
}
|
||||
|
||||
/** Reconciles mutable server properties, including ones removed from a CloudFormation template. */
|
||||
public Server replaceServerConfiguration(String serverId,
|
||||
List<String> protocols,
|
||||
String endpointType,
|
||||
Map<String, Object> endpointDetails,
|
||||
Map<String, String> identityProviderDetails,
|
||||
String loggingRole,
|
||||
String securityPolicyName) {
|
||||
Server server = getServer(serverId);
|
||||
server.setProtocols(protocols);
|
||||
server.setEndpointType(endpointType);
|
||||
server.setEndpointDetails(endpointDetails);
|
||||
server.setIdentityProviderDetails(identityProviderDetails);
|
||||
server.setLoggingRole(loggingRole);
|
||||
server.setSecurityPolicyName(securityPolicyName);
|
||||
putServer(server);
|
||||
return server;
|
||||
}
|
||||
|
||||
// ── Users ─────────────────────────────────────────────────────────────────
|
||||
|
||||
public User createUser(String serverId, String region, String userName, String role,
|
||||
|
||||
Reference in new issue
Block a user