feat(transfer): add AWS Transfer Family management-plane API (#732)

Implements Phase 1 of the Transfer Family JSON 1.1 handler covering
17 operations across server lifecycle, user management, SSH key
management, and tagging.
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-05-07 01:30:43 -05:00
1 parent 2cc3a1a3ba
commit 8e617c6add
15 files changed
+1057 -17

No files matched your search

@@ -286,6 +286,11 @@ public interface EmulatorConfig {
TransferServiceConfig transfer();
}
interface TransferServiceConfig {
@WithDefault("true")
boolean enabled();
}
interface BackupServiceConfig {
@WithDefault("true")
boolean enabled();
@@ -311,11 +316,6 @@ public interface EmulatorConfig {
String defaultNameserver4();
}
interface TransferServiceConfig {
@WithDefault("true")
boolean enabled();
}
interface AutoScalingServiceConfig {
@WithDefault("true")
boolean enabled();
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.services.athena.AthenaJsonHandler;
import io.github.hectorvent.floci.services.codebuild.CodeBuildJsonHandler;
import io.github.hectorvent.floci.services.codedeploy.CodeDeployJsonHandler;
import io.github.hectorvent.floci.services.ecr.EcrJsonHandler;
import io.github.hectorvent.floci.services.transfer.TransferHandler;
import io.github.hectorvent.floci.services.ecs.EcsJsonHandler;
import io.github.hectorvent.floci.services.firehose.FirehoseJsonHandler;
import io.github.hectorvent.floci.services.glue.GlueJsonHandler;
@@ -62,6 +63,7 @@ public class AwsJson11Controller {
private final CodeBuildJsonHandler codeBuildJsonHandler;
private final CodeDeployJsonHandler codeDeployJsonHandler;
private final Ec2MessagesJsonHandler ec2MessagesJsonHandler;
private final TransferHandler transferHandler;
@Inject
public AwsJson11Controller(ObjectMapper objectMapper, ResolvedServiceCatalog catalog,
@@ -79,7 +81,8 @@ public class AwsJson11Controller {
ResourceGroupsTaggingJsonHandler resourceGroupsTaggingJsonHandler,
CodeBuildJsonHandler codeBuildJsonHandler,
CodeDeployJsonHandler codeDeployJsonHandler,
Ec2MessagesJsonHandler ec2MessagesJsonHandler) {
Ec2MessagesJsonHandler ec2MessagesJsonHandler,
TransferHandler transferHandler) {
this.objectMapper = objectMapper;
this.catalog = catalog;
this.regionResolver = regionResolver;
@@ -101,6 +104,7 @@ public class AwsJson11Controller {
this.codeBuildJsonHandler = codeBuildJsonHandler;
this.codeDeployJsonHandler = codeDeployJsonHandler;
this.ec2MessagesJsonHandler = ec2MessagesJsonHandler;
this.transferHandler = transferHandler;
}
@POST
@@ -147,6 +151,7 @@ public class AwsJson11Controller {
case "codebuild" -> codeBuildJsonHandler.handle(action, request, region, regionResolver.getAccountId());
case "codedeploy" -> codeDeployJsonHandler.handle(action, request, region);
case "ec2messages" -> ec2MessagesJsonHandler.handle(action, request, region);
case "transfer" -> transferHandler.handle(action, request, region);
default -> null;
};
// catalog.matchTarget is protocol-agnostic: a JSON 1.0 target
@@ -232,14 +232,14 @@ public class ResolvedServiceCatalog {
null, null, 5000L, null, ServiceProtocol.JSON,
protocols(ServiceProtocol.JSON),
Set.of("AmazonSSMMessageDeliveryService."), Set.of("ec2messages"), Set.of(), Set.of()),
descriptor("route53", "route53", config.services().route53().enabled(), true,
"route53", config.storage().mode(), 5000L, null, ServiceProtocol.REST_XML,
protocols(ServiceProtocol.REST_XML),
Set.of(), Set.of("route53"), Set.of(), Set.of(Route53Controller.class)),
descriptor("transfer", "transfer", config.services().transfer().enabled(), true,
"transfer", config.storage().mode(), 5000L, null, ServiceProtocol.JSON,
protocols(ServiceProtocol.JSON),
Set.of("TransferService."), Set.of("transfer"), Set.of(), Set.of())
Set.of("TransferService."), Set.of("transfer"), Set.of(), Set.of()),
descriptor("route53", "route53", config.services().route53().enabled(), true,
"route53", config.storage().mode(), 5000L, null, ServiceProtocol.REST_XML,
protocols(ServiceProtocol.REST_XML),
Set.of(), Set.of("route53"), Set.of(), Set.of(Route53Controller.class))
));
}
@@ -0,0 +1,426 @@
package io.github.hectorvent.floci.services.transfer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.JsonErrorResponseUtils;
import io.github.hectorvent.floci.services.transfer.model.HomeDirectoryMapping;
import io.github.hectorvent.floci.services.transfer.model.Server;
import io.github.hectorvent.floci.services.transfer.model.SshPublicKey;
import io.github.hectorvent.floci.services.transfer.model.User;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ApplicationScoped
public class TransferHandler {
private final TransferService service;
private final ObjectMapper objectMapper;
@Inject
public TransferHandler(TransferService service, ObjectMapper objectMapper) {
this.service = service;
this.objectMapper = objectMapper;
}
public Response handle(String action, JsonNode request, String region) {
try {
return switch (action) {
case "CreateServer" -> createServer(request, region);
case "DescribeServer" -> describeServer(request);
case "DeleteServer" -> deleteServer(request);
case "ListServers" -> listServers(request);
case "StartServer" -> startServer(request);
case "StopServer" -> stopServer(request);
case "UpdateServer" -> updateServer(request);
case "CreateUser" -> createUser(request, region);
case "DescribeUser" -> describeUser(request);
case "DeleteUser" -> deleteUser(request);
case "ListUsers" -> listUsers(request);
case "UpdateUser" -> updateUser(request);
case "ImportSshPublicKey" -> importSshPublicKey(request);
case "DeleteSshPublicKey" -> deleteSshPublicKey(request);
case "TagResource" -> tagResource(request);
case "UntagResource" -> untagResource(request);
case "ListTagsForResource" -> listTagsForResource(request);
default -> JsonErrorResponseUtils.createUnknownOperationErrorResponse("AmazonTransfer." + action);
};
} catch (AwsException e) {
return JsonErrorResponseUtils.createErrorResponse(e);
} catch (Exception e) {
return JsonErrorResponseUtils.createErrorResponse(e);
}
}
// ── Server handlers ───────────────────────────────────────────────────────
private Response createServer(JsonNode req, String region) {
List<String> protocols = jsonStringList(req.path("Protocols"));
String endpointType = textOrNull(req, "EndpointType");
Map<String, Object> endpointDetails = jsonObjectMap(req.path("EndpointDetails"));
String identityProviderType = textOrNull(req, "IdentityProviderType");
Map<String, String> identityProviderDetails = jsonStringMap(req.path("IdentityProviderDetails"));
String loggingRole = textOrNull(req, "LoggingRole");
String securityPolicyName = textOrNull(req, "SecurityPolicyName");
Map<String, String> tags = parseTags(req.path("Tags"));
Server server = service.createServer(region, protocols, endpointType, endpointDetails,
identityProviderType, identityProviderDetails, loggingRole, securityPolicyName, tags);
ObjectNode resp = objectMapper.createObjectNode();
resp.put("ServerId", server.getServerId());
return Response.ok(resp).build();
}
private Response describeServer(JsonNode req) {
String serverId = req.path("ServerId").asText();
Server server = service.getServer(serverId);
ObjectNode resp = objectMapper.createObjectNode();
resp.set("Server", buildServerNode(server));
return Response.ok(resp).build();
}
private Response deleteServer(JsonNode req) {
service.deleteServer(req.path("ServerId").asText());
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response listServers(JsonNode req) {
String nextToken = textOrNull(req, "NextToken");
int maxResults = req.path("MaxResults").asInt(100);
List<Server> servers = service.listServers(nextToken, maxResults);
ObjectNode resp = objectMapper.createObjectNode();
ArrayNode arr = resp.putArray("Servers");
for (Server s : servers) {
arr.add(buildServerListEntry(s));
}
if (servers.size() == maxResults) {
resp.put("NextToken", servers.get(servers.size() - 1).getServerId());
}
return Response.ok(resp).build();
}
private Response startServer(JsonNode req) {
service.startServer(req.path("ServerId").asText());
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response stopServer(JsonNode req) {
service.stopServer(req.path("ServerId").asText());
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response updateServer(JsonNode req) {
String serverId = req.path("ServerId").asText();
List<String> protocols = jsonStringList(req.path("Protocols"));
String endpointType = textOrNull(req, "EndpointType");
Map<String, Object> endpointDetails = jsonObjectMap(req.path("EndpointDetails"));
String identityProviderDetails = textOrNull(req, "IdentityProviderDetails");
String loggingRole = textOrNull(req, "LoggingRole");
String securityPolicyName = textOrNull(req, "SecurityPolicyName");
Server server = service.updateServer(serverId, protocols, endpointType, endpointDetails,
identityProviderDetails, loggingRole, securityPolicyName);
ObjectNode resp = objectMapper.createObjectNode();
resp.put("ServerId", server.getServerId());
return Response.ok(resp).build();
}
// ── User handlers ─────────────────────────────────────────────────────────
private Response createUser(JsonNode req, String region) {
String serverId = req.path("ServerId").asText();
String userName = req.path("UserName").asText();
String role = textOrNull(req, "Role");
String homeDirectory = textOrNull(req, "HomeDirectory");
String homeDirectoryType = textOrNull(req, "HomeDirectoryType");
List<HomeDirectoryMapping> mappings = parseHomeDirectoryMappings(req.path("HomeDirectoryMappings"));
Map<String, String> tags = parseTags(req.path("Tags"));
if (userName == null || userName.isEmpty()) {
throw new AwsException("InvalidRequestException", "UserName is required.", 400);
}
if (role == null || role.isEmpty()) {
throw new AwsException("InvalidRequestException", "Role is required.", 400);
}
User user = service.createUser(serverId, region, userName, role, homeDirectory,
homeDirectoryType, mappings, tags);
ObjectNode resp = objectMapper.createObjectNode();
resp.put("ServerId", serverId);
resp.put("UserName", user.getUserName());
return Response.ok(resp).build();
}
private Response describeUser(JsonNode req) {
String serverId = req.path("ServerId").asText();
String userName = req.path("UserName").asText();
User user = service.getUser(serverId, userName);
ObjectNode resp = objectMapper.createObjectNode();
resp.put("ServerId", serverId);
resp.set("User", buildUserNode(user));
return Response.ok(resp).build();
}
private Response deleteUser(JsonNode req) {
service.deleteUser(req.path("ServerId").asText(), req.path("UserName").asText());
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response listUsers(JsonNode req) {
String serverId = req.path("ServerId").asText();
String nextToken = textOrNull(req, "NextToken");
int maxResults = req.path("MaxResults").asInt(100);
List<User> users = service.listUsers(serverId, nextToken, maxResults);
ObjectNode resp = objectMapper.createObjectNode();
resp.put("ServerId", serverId);
ArrayNode arr = resp.putArray("Users");
for (User u : users) {
arr.add(buildUserListEntry(u));
}
if (users.size() == maxResults) {
resp.put("NextToken", users.get(users.size() - 1).getUserName());
}
return Response.ok(resp).build();
}
private Response updateUser(JsonNode req) {
String serverId = req.path("ServerId").asText();
String userName = req.path("UserName").asText();
String role = textOrNull(req, "Role");
String homeDirectory = textOrNull(req, "HomeDirectory");
String homeDirectoryType = textOrNull(req, "HomeDirectoryType");
List<HomeDirectoryMapping> mappings = parseHomeDirectoryMappings(req.path("HomeDirectoryMappings"));
User user = service.updateUser(serverId, userName, role, homeDirectory, homeDirectoryType,
mappings.isEmpty() ? null : mappings);
ObjectNode resp = objectMapper.createObjectNode();
resp.put("ServerId", serverId);
resp.put("UserName", user.getUserName());
return Response.ok(resp).build();
}
// ── SSH key handlers ──────────────────────────────────────────────────────
private Response importSshPublicKey(JsonNode req) {
String serverId = req.path("ServerId").asText();
String userName = req.path("UserName").asText();
String body = req.path("SshPublicKeyBody").asText();
SshPublicKey key = service.importSshPublicKey(serverId, userName, body);
ObjectNode resp = objectMapper.createObjectNode();
resp.put("ServerId", serverId);
resp.put("SshPublicKeyId", key.getSshPublicKeyId());
resp.put("UserName", userName);
return Response.ok(resp).build();
}
private Response deleteSshPublicKey(JsonNode req) {
service.deleteSshPublicKey(
req.path("ServerId").asText(),
req.path("UserName").asText(),
req.path("SshPublicKeyId").asText());
return Response.ok(objectMapper.createObjectNode()).build();
}
// ── Tag handlers ──────────────────────────────────────────────────────────
private Response tagResource(JsonNode req) {
String arn = req.path("Arn").asText();
Map<String, String> tags = parseTags(req.path("Tags"));
service.tagResource(arn, tags);
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response untagResource(JsonNode req) {
String arn = req.path("Arn").asText();
List<String> keys = new ArrayList<>();
req.path("TagKeys").forEach(n -> keys.add(n.asText()));
service.untagResource(arn, keys);
return Response.ok(objectMapper.createObjectNode()).build();
}
private Response listTagsForResource(JsonNode req) {
String arn = req.path("Arn").asText();
Map<String, String> tags = service.listTagsForResource(arn);
ObjectNode resp = objectMapper.createObjectNode();
resp.put("Arn", arn);
ArrayNode arr = resp.putArray("Tags");
tags.forEach((k, v) -> {
ObjectNode tag = objectMapper.createObjectNode();
tag.put("Key", k);
tag.put("Value", v);
arr.add(tag);
});
return Response.ok(resp).build();
}
// ── JSON builders ─────────────────────────────────────────────────────────
private ObjectNode buildServerNode(Server s) {
ObjectNode node = objectMapper.createObjectNode();
node.put("ServerId", s.getServerId());
node.put("Arn", s.getArn());
node.put("State", s.getState());
node.put("EndpointType", s.getEndpointType());
node.put("IdentityProviderType", s.getIdentityProviderType());
node.put("SecurityPolicyName", s.getSecurityPolicyName());
node.put("HostKeyFingerprint", s.getHostKeyFingerprint());
node.put("UserCount", service.countUsers(s.getServerId()));
if (s.getLoggingRole() != null) {
node.put("LoggingRole", s.getLoggingRole());
}
if (s.getProtocols() != null) {
ArrayNode protocols = node.putArray("Protocols");
s.getProtocols().forEach(protocols::add);
}
if (s.getTags() != null && !s.getTags().isEmpty()) {
ArrayNode tags = node.putArray("Tags");
s.getTags().forEach((k, v) -> {
ObjectNode tag = objectMapper.createObjectNode();
tag.put("Key", k);
tag.put("Value", v);
tags.add(tag);
});
}
return node;
}
private ObjectNode buildServerListEntry(Server s) {
ObjectNode node = objectMapper.createObjectNode();
node.put("Arn", s.getArn());
node.put("EndpointType", s.getEndpointType());
node.put("IdentityProviderType", s.getIdentityProviderType());
node.put("ServerId", s.getServerId());
node.put("State", s.getState());
node.put("UserCount", service.countUsers(s.getServerId()));
if (s.getLoggingRole() != null) {
node.put("LoggingRole", s.getLoggingRole());
}
return node;
}
private ObjectNode buildUserNode(User u) {
ObjectNode node = objectMapper.createObjectNode();
node.put("UserName", u.getUserName());
node.put("Arn", u.getArn());
node.put("HomeDirectory", u.getHomeDirectory());
node.put("HomeDirectoryType", u.getHomeDirectoryType());
if (u.getRole() != null) node.put("Role", u.getRole());
if (u.getHomeDirectoryMappings() != null && !u.getHomeDirectoryMappings().isEmpty()) {
ArrayNode arr = node.putArray("HomeDirectoryMappings");
for (HomeDirectoryMapping m : u.getHomeDirectoryMappings()) {
ObjectNode entry = objectMapper.createObjectNode();
entry.put("Entry", m.getEntry());
entry.put("Target", m.getTarget());
arr.add(entry);
}
}
ArrayNode keys = node.putArray("SshPublicKeys");
if (u.getSshPublicKeys() != null) {
for (SshPublicKey k : u.getSshPublicKeys()) {
ObjectNode kNode = objectMapper.createObjectNode();
kNode.put("SshPublicKeyId", k.getSshPublicKeyId());
kNode.put("SshPublicKeyBody", k.getSshPublicKeyBody());
if (k.getDateImported() != null) {
kNode.put("DateImported", k.getDateImported().toString());
}
keys.add(kNode);
}
}
if (u.getTags() != null && !u.getTags().isEmpty()) {
ArrayNode tags = node.putArray("Tags");
u.getTags().forEach((k, v) -> {
ObjectNode tag = objectMapper.createObjectNode();
tag.put("Key", k);
tag.put("Value", v);
tags.add(tag);
});
}
return node;
}
private ObjectNode buildUserListEntry(User u) {
ObjectNode node = objectMapper.createObjectNode();
node.put("UserName", u.getUserName());
node.put("Arn", u.getArn());
node.put("HomeDirectory", u.getHomeDirectory());
node.put("HomeDirectoryType", u.getHomeDirectoryType());
if (u.getRole() != null) node.put("Role", u.getRole());
node.put("SshPublicKeyCount", u.getSshPublicKeys() != null ? u.getSshPublicKeys().size() : 0);
return node;
}
// ── Parsing helpers ───────────────────────────────────────────────────────
private String textOrNull(JsonNode node, String field) {
JsonNode child = node.path(field);
return child.isMissingNode() || child.isNull() ? null : child.asText();
}
private List<String> jsonStringList(JsonNode node) {
List<String> list = new ArrayList<>();
if (node != null && node.isArray()) {
node.forEach(n -> list.add(n.asText()));
}
return list;
}
private Map<String, String> jsonStringMap(JsonNode node) {
Map<String, String> map = new HashMap<>();
if (node != null && node.isObject()) {
node.fields().forEachRemaining(e -> map.put(e.getKey(), e.getValue().asText()));
}
return map;
}
private Map<String, Object> jsonObjectMap(JsonNode node) {
if (node == null || node.isMissingNode() || node.isNull()) {
return null;
}
Map<String, Object> map = new HashMap<>();
node.fields().forEachRemaining(e -> map.put(e.getKey(), e.getValue().asText()));
return map.isEmpty() ? null : map;
}
private Map<String, String> parseTags(JsonNode node) {
Map<String, String> tags = new HashMap<>();
if (node != null && node.isArray()) {
node.forEach(t -> {
String key = t.path("Key").asText(null);
String value = t.path("Value").asText("");
if (key != null) tags.put(key, value);
});
}
return tags;
}
private List<HomeDirectoryMapping> parseHomeDirectoryMappings(JsonNode node) {
List<HomeDirectoryMapping> list = new ArrayList<>();
if (node != null && node.isArray()) {
node.forEach(m -> {
String entry = m.path("Entry").asText(null);
String target = m.path("Target").asText(null);
if (entry != null && target != null) {
list.add(new HomeDirectoryMapping(entry, target));
}
});
}
return list;
}
}
@@ -0,0 +1,334 @@
package io.github.hectorvent.floci.services.transfer;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.transfer.model.HomeDirectoryMapping;
import io.github.hectorvent.floci.services.transfer.model.Server;
import io.github.hectorvent.floci.services.transfer.model.SshPublicKey;
import io.github.hectorvent.floci.services.transfer.model.User;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ApplicationScoped
public class TransferService {
private static final String CHARS = "abcdefghijklmnopqrstuvwxyz0123456789";
private final StorageBackend<String, Server> serverStore;
private final StorageBackend<String, User> userStore;
private final StorageBackend<String, Map<String, String>> tagStore;
private final String accountId;
@Inject
public TransferService(StorageFactory factory, EmulatorConfig config) {
this.serverStore = factory.create("transfer", "transfer-servers.json",
new TypeReference<Map<String, Server>>() {});
this.userStore = factory.create("transfer", "transfer-users.json",
new TypeReference<Map<String, User>>() {});
this.tagStore = factory.create("transfer", "transfer-tags.json",
new TypeReference<Map<String, Map<String, String>>>() {});
this.accountId = config.defaultAccountId();
}
// ── Servers ───────────────────────────────────────────────────────────────
public Server createServer(String region,
List<String> protocols,
String endpointType,
Map<String, Object> endpointDetails,
String identityProviderType,
Map<String, String> identityProviderDetails,
String loggingRole,
String securityPolicyName,
Map<String, String> tags) {
String serverId = generateServerId();
String arn = "arn:aws:transfer:" + region + ":" + accountId + ":server/" + serverId;
Server server = new Server();
server.setServerId(serverId);
server.setArn(arn);
server.setState("ONLINE");
server.setProtocols(protocols != null && !protocols.isEmpty() ? protocols : List.of("SFTP"));
server.setEndpointType(endpointType != null ? endpointType : "PUBLIC");
server.setEndpointDetails(endpointDetails);
server.setIdentityProviderType(identityProviderType != null ? identityProviderType : "SERVICE_MANAGED");
server.setIdentityProviderDetails(identityProviderDetails);
server.setLoggingRole(loggingRole);
server.setSecurityPolicyName(securityPolicyName != null ? securityPolicyName : "TransferSecurityPolicy-2020-06");
server.setHostKeyFingerprint("SHA256:AAAAflociemulatedkey" + serverId.substring(2, 10));
server.setTags(tags != null ? tags : new HashMap<>());
server.setCreationTime(Instant.now());
serverStore.put(serverId, server);
if (tags != null && !tags.isEmpty()) {
tagStore.put("server/" + serverId, new HashMap<>(tags));
}
return server;
}
public Server getServer(String serverId) {
return serverStore.get(serverId).orElseThrow(() ->
new AwsException("ResourceNotFoundException",
"Server " + serverId + " does not exist.", 404));
}
public synchronized void deleteServer(String serverId) {
Server server = getServer(serverId);
if (!"OFFLINE".equals(server.getState())) {
throw new AwsException("ConflictException",
"Server must be in OFFLINE state to be deleted.", 409);
}
serverStore.delete(serverId);
tagStore.delete("server/" + serverId);
for (User user : userStore.scan(k -> k.startsWith(serverId + "/"))) {
userStore.delete(serverId + "/" + user.getUserName());
tagStore.delete("user/" + serverId + "/" + user.getUserName());
}
}
public List<Server> listServers(String nextToken, int maxResults) {
List<Server> all = new ArrayList<>(serverStore.scan(k -> true));
all.sort((a, b) -> a.getServerId().compareTo(b.getServerId()));
if (nextToken != null && !nextToken.isEmpty()) {
int idx = 0;
for (int i = 0; i < all.size(); i++) {
if (all.get(i).getServerId().equals(nextToken)) {
idx = i + 1;
break;
}
}
all = all.subList(idx, all.size());
}
if (maxResults > 0 && all.size() > maxResults) {
return all.subList(0, maxResults);
}
return all;
}
public Server startServer(String serverId) {
Server server = getServer(serverId);
if (!"OFFLINE".equals(server.getState())) {
throw new AwsException("ConflictException",
"Server is not in OFFLINE state.", 409);
}
server.setState("ONLINE");
serverStore.put(serverId, server);
return server;
}
public Server stopServer(String serverId) {
Server server = getServer(serverId);
if (!"ONLINE".equals(server.getState())) {
throw new AwsException("ConflictException",
"Server is not in ONLINE state.", 409);
}
server.setState("OFFLINE");
serverStore.put(serverId, server);
return server;
}
public Server updateServer(String serverId,
List<String> protocols,
String endpointType,
Map<String, Object> endpointDetails,
String identityProviderDetails,
String loggingRole,
String securityPolicyName) {
Server server = getServer(serverId);
if (protocols != null && !protocols.isEmpty()) {
server.setProtocols(protocols);
}
if (endpointType != null) {
server.setEndpointType(endpointType);
}
if (endpointDetails != null) {
server.setEndpointDetails(endpointDetails);
}
if (loggingRole != null) {
server.setLoggingRole(loggingRole);
}
if (securityPolicyName != null) {
server.setSecurityPolicyName(securityPolicyName);
}
serverStore.put(serverId, server);
return server;
}
// ── Users ─────────────────────────────────────────────────────────────────
public User createUser(String serverId, String region, String userName, String role,
String homeDirectory, String homeDirectoryType,
List<HomeDirectoryMapping> homeDirectoryMappings,
Map<String, String> tags) {
getServer(serverId);
String key = serverId + "/" + userName;
if (userStore.get(key).isPresent()) {
throw new AwsException("ResourceExistsException",
"User " + userName + " already exists on server " + serverId + ".", 400);
}
String arn = "arn:aws:transfer:" + region + ":" + accountId + ":user/" + serverId + "/" + userName;
User user = new User();
user.setUserName(userName);
user.setArn(arn);
user.setRole(role);
user.setHomeDirectory(homeDirectory != null ? homeDirectory : "/");
user.setHomeDirectoryType(homeDirectoryType != null ? homeDirectoryType : "PATH");
user.setHomeDirectoryMappings(homeDirectoryMappings != null ? homeDirectoryMappings : List.of());
user.setSshPublicKeys(new ArrayList<>());
user.setTags(tags != null ? tags : new HashMap<>());
userStore.put(key, user);
if (tags != null && !tags.isEmpty()) {
tagStore.put("user/" + key, new HashMap<>(tags));
}
return user;
}
public User getUser(String serverId, String userName) {
getServer(serverId);
return userStore.get(serverId + "/" + userName).orElseThrow(() ->
new AwsException("ResourceNotFoundException",
"User " + userName + " does not exist on server " + serverId + ".", 404));
}
public void deleteUser(String serverId, String userName) {
getUser(serverId, userName);
String key = serverId + "/" + userName;
userStore.delete(key);
tagStore.delete("user/" + key);
}
public List<User> listUsers(String serverId, String nextToken, int maxResults) {
getServer(serverId);
List<User> all = new ArrayList<>(userStore.scan(k -> k.startsWith(serverId + "/")));
all.sort((a, b) -> a.getUserName().compareTo(b.getUserName()));
if (nextToken != null && !nextToken.isEmpty()) {
int idx = 0;
for (int i = 0; i < all.size(); i++) {
if (all.get(i).getUserName().equals(nextToken)) {
idx = i + 1;
break;
}
}
all = all.subList(idx, all.size());
}
if (maxResults > 0 && all.size() > maxResults) {
return all.subList(0, maxResults);
}
return all;
}
public User updateUser(String serverId, String userName, String role,
String homeDirectory, String homeDirectoryType,
List<HomeDirectoryMapping> homeDirectoryMappings) {
User user = getUser(serverId, userName);
if (role != null) user.setRole(role);
if (homeDirectory != null) user.setHomeDirectory(homeDirectory);
if (homeDirectoryType != null) user.setHomeDirectoryType(homeDirectoryType);
if (homeDirectoryMappings != null) user.setHomeDirectoryMappings(homeDirectoryMappings);
userStore.put(serverId + "/" + userName, user);
return user;
}
// ── SSH Keys ──────────────────────────────────────────────────────────────
public SshPublicKey importSshPublicKey(String serverId, String userName, String sshPublicKeyBody) {
User user = getUser(serverId, userName);
String keyId = "key-" + UUID.randomUUID().toString().replace("-", "").substring(0, 17);
SshPublicKey key = new SshPublicKey(keyId, sshPublicKeyBody, Instant.now());
List<SshPublicKey> keys = new ArrayList<>(user.getSshPublicKeys() != null ? user.getSshPublicKeys() : List.of());
keys.add(key);
user.setSshPublicKeys(keys);
userStore.put(serverId + "/" + userName, user);
return key;
}
public void deleteSshPublicKey(String serverId, String userName, String sshPublicKeyId) {
User user = getUser(serverId, userName);
List<SshPublicKey> keys = new ArrayList<>(user.getSshPublicKeys() != null ? user.getSshPublicKeys() : List.of());
boolean removed = keys.removeIf(k -> k.getSshPublicKeyId().equals(sshPublicKeyId));
if (!removed) {
throw new AwsException("ResourceNotFoundException",
"SSH public key " + sshPublicKeyId + " does not exist.", 404);
}
user.setSshPublicKeys(keys);
userStore.put(serverId + "/" + userName, user);
}
// ── Tags ──────────────────────────────────────────────────────────────────
public Map<String, String> listTagsForResource(String arn) {
String key = arnToTagKey(arn);
return tagStore.get(key).orElse(new HashMap<>());
}
public void tagResource(String arn, Map<String, String> tags) {
String key = arnToTagKey(arn);
Map<String, String> existing = new HashMap<>(tagStore.get(key).orElse(new HashMap<>()));
existing.putAll(tags);
tagStore.put(key, existing);
// Also sync tags into the resource object
syncTagsToResource(arn, existing);
}
public void untagResource(String arn, List<String> tagKeys) {
String key = arnToTagKey(arn);
Map<String, String> existing = new HashMap<>(tagStore.get(key).orElse(new HashMap<>()));
tagKeys.forEach(existing::remove);
tagStore.put(key, existing);
syncTagsToResource(arn, existing);
}
// ── Helpers ───────────────────────────────────────────────────────────────
private String generateServerId() {
StringBuilder sb = new StringBuilder("s-");
String uuid = UUID.randomUUID().toString().replace("-", "");
sb.append(uuid, 0, 17);
return sb.toString();
}
private String arnToTagKey(String arn) {
// arn:aws:transfer:region:account:server/s-xxx → server/s-xxx
// arn:aws:transfer:region:account:user/s-xxx/alice → user/s-xxx/alice
int idx = arn.lastIndexOf(':');
return idx >= 0 ? arn.substring(idx + 1) : arn;
}
private void syncTagsToResource(String arn, Map<String, String> tags) {
String key = arnToTagKey(arn);
if (key.startsWith("server/")) {
String serverId = key.substring("server/".length());
serverStore.get(serverId).ifPresent(s -> {
s.setTags(tags);
serverStore.put(serverId, s);
});
} else if (key.startsWith("user/")) {
String userKey = key.substring("user/".length());
userStore.get(userKey).ifPresent(u -> {
u.setTags(tags);
userStore.put(userKey, u);
});
}
}
public int countUsers(String serverId) {
return (int) userStore.scan(k -> k.startsWith(serverId + "/")).stream().count();
}
}
@@ -0,0 +1,20 @@
package io.github.hectorvent.floci.services.transfer.model;
public class HomeDirectoryMapping {
private String entry;
private String target;
public HomeDirectoryMapping() {}
public HomeDirectoryMapping(String entry, String target) {
this.entry = entry;
this.target = target;
}
public String getEntry() { return entry; }
public void setEntry(String entry) { this.entry = entry; }
public String getTarget() { return target; }
public void setTarget(String target) { this.target = target; }
}
@@ -0,0 +1,63 @@
package io.github.hectorvent.floci.services.transfer.model;
import java.time.Instant;
import java.util.List;
import java.util.Map;
public class Server {
private String serverId;
private String arn;
private String state;
private List<String> protocols;
private String endpointType;
private Map<String, Object> endpointDetails;
private String identityProviderType;
private Map<String, String> identityProviderDetails;
private String loggingRole;
private String securityPolicyName;
private String hostKeyFingerprint;
private Map<String, String> tags;
private Instant creationTime;
public Server() {}
public String getServerId() { return serverId; }
public void setServerId(String serverId) { this.serverId = serverId; }
public String getArn() { return arn; }
public void setArn(String arn) { this.arn = arn; }
public String getState() { return state; }
public void setState(String state) { this.state = state; }
public List<String> getProtocols() { return protocols; }
public void setProtocols(List<String> protocols) { this.protocols = protocols; }
public String getEndpointType() { return endpointType; }
public void setEndpointType(String endpointType) { this.endpointType = endpointType; }
public Map<String, Object> getEndpointDetails() { return endpointDetails; }
public void setEndpointDetails(Map<String, Object> endpointDetails) { this.endpointDetails = endpointDetails; }
public String getIdentityProviderType() { return identityProviderType; }
public void setIdentityProviderType(String identityProviderType) { this.identityProviderType = identityProviderType; }
public Map<String, String> getIdentityProviderDetails() { return identityProviderDetails; }
public void setIdentityProviderDetails(Map<String, String> identityProviderDetails) { this.identityProviderDetails = identityProviderDetails; }
public String getLoggingRole() { return loggingRole; }
public void setLoggingRole(String loggingRole) { this.loggingRole = loggingRole; }
public String getSecurityPolicyName() { return securityPolicyName; }
public void setSecurityPolicyName(String securityPolicyName) { this.securityPolicyName = securityPolicyName; }
public String getHostKeyFingerprint() { return hostKeyFingerprint; }
public void setHostKeyFingerprint(String hostKeyFingerprint) { this.hostKeyFingerprint = hostKeyFingerprint; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
public Instant getCreationTime() { return creationTime; }
public void setCreationTime(Instant creationTime) { this.creationTime = creationTime; }
}
@@ -0,0 +1,27 @@
package io.github.hectorvent.floci.services.transfer.model;
import java.time.Instant;
public class SshPublicKey {
private String sshPublicKeyId;
private String sshPublicKeyBody;
private Instant dateImported;
public SshPublicKey() {}
public SshPublicKey(String sshPublicKeyId, String sshPublicKeyBody, Instant dateImported) {
this.sshPublicKeyId = sshPublicKeyId;
this.sshPublicKeyBody = sshPublicKeyBody;
this.dateImported = dateImported;
}
public String getSshPublicKeyId() { return sshPublicKeyId; }
public void setSshPublicKeyId(String sshPublicKeyId) { this.sshPublicKeyId = sshPublicKeyId; }
public String getSshPublicKeyBody() { return sshPublicKeyBody; }
public void setSshPublicKeyBody(String sshPublicKeyBody) { this.sshPublicKeyBody = sshPublicKeyBody; }
public Instant getDateImported() { return dateImported; }
public void setDateImported(Instant dateImported) { this.dateImported = dateImported; }
}
@@ -0,0 +1,42 @@
package io.github.hectorvent.floci.services.transfer.model;
import java.util.List;
import java.util.Map;
public class User {
private String userName;
private String arn;
private String homeDirectory;
private String homeDirectoryType;
private List<HomeDirectoryMapping> homeDirectoryMappings;
private String role;
private List<SshPublicKey> sshPublicKeys;
private Map<String, String> tags;
public User() {}
public String getUserName() { return userName; }
public void setUserName(String userName) { this.userName = userName; }
public String getArn() { return arn; }
public void setArn(String arn) { this.arn = arn; }
public String getHomeDirectory() { return homeDirectory; }
public void setHomeDirectory(String homeDirectory) { this.homeDirectory = homeDirectory; }
public String getHomeDirectoryType() { return homeDirectoryType; }
public void setHomeDirectoryType(String homeDirectoryType) { this.homeDirectoryType = homeDirectoryType; }
public List<HomeDirectoryMapping> getHomeDirectoryMappings() { return homeDirectoryMappings; }
public void setHomeDirectoryMappings(List<HomeDirectoryMapping> homeDirectoryMappings) { this.homeDirectoryMappings = homeDirectoryMappings; }
public String getRole() { return role; }
public void setRole(String role) { this.role = role; }
public List<SshPublicKey> getSshPublicKeys() { return sshPublicKeys; }
public void setSshPublicKeys(List<SshPublicKey> sshPublicKeys) { this.sshPublicKeys = sshPublicKeys; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags; }
}