mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(transfer): isolate servers and users by account and region (#3403)
* fix(transfer): isolate servers and users by account and region * ci: retrigger transfer checks * fix(transfer): migrate legacy resources by scope * test(transfer): cover unscoped legacy tags
This commit is contained in:
1 parent
901b83cb8e
commit
43eace4e02
2 files changed
+477
-44
No files matched your search
@@ -2,9 +2,10 @@ 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.AwsArnUtils;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.storage.StorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
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;
|
||||
@@ -18,6 +19,7 @@ import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
@ApplicationScoped
|
||||
@@ -25,9 +27,9 @@ 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 AccountAwareStorageBackend<Server> serverStore;
|
||||
private final AccountAwareStorageBackend<User> userStore;
|
||||
private final AccountAwareStorageBackend<Map<String, String>> tagStore;
|
||||
private final RegionResolver regionResolver;
|
||||
|
||||
@Inject
|
||||
@@ -54,6 +56,7 @@ public class TransferService {
|
||||
String securityPolicyName,
|
||||
Map<String, String> tags) {
|
||||
String serverId = generateServerId();
|
||||
region = currentRegion();
|
||||
String arn = regionResolver.buildArn("transfer", region, "server/" + serverId);
|
||||
|
||||
Server server = new Server();
|
||||
@@ -72,17 +75,17 @@ public class TransferService {
|
||||
server.setTags(tags != null ? tags : new HashMap<>());
|
||||
server.setCreationTime(Instant.now());
|
||||
|
||||
serverStore.put(serverId, server);
|
||||
putServer(server);
|
||||
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
tagStore.put("server/" + serverId, new HashMap<>(tags));
|
||||
putTags("server/" + serverId, tags);
|
||||
}
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
public Server getServer(String serverId) {
|
||||
return serverStore.get(serverId).orElseThrow(() ->
|
||||
return findServer(serverId).orElseThrow(() ->
|
||||
new AwsException("ResourceNotFoundException",
|
||||
"Server " + serverId + " does not exist.", 404));
|
||||
}
|
||||
@@ -90,17 +93,19 @@ public class TransferService {
|
||||
public synchronized void deleteServer(String serverId) {
|
||||
// AWS deletes a server in any state: the DeleteServer API defines no
|
||||
// state precondition (and no ConflictException at all).
|
||||
getServer(serverId);
|
||||
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());
|
||||
Server server = getServer(serverId);
|
||||
deleteServerRecord(server);
|
||||
migrateLegacyUsers(currentAccount(), currentRegion(), serverId);
|
||||
for (User user : userStore.scanForAccount(currentAccount(),
|
||||
k -> k.startsWith(scopedKey(currentRegion(), serverId) + "/"))) {
|
||||
deleteUserRecord(user);
|
||||
}
|
||||
}
|
||||
|
||||
public List<Server> listServers(String nextToken, int maxResults) {
|
||||
List<Server> all = new ArrayList<>(serverStore.scan(k -> true));
|
||||
migrateLegacyServers(currentAccount(), currentRegion());
|
||||
List<Server> all = new ArrayList<>(serverStore.scanForAccount(currentAccount(),
|
||||
k -> k.startsWith(currentRegion() + "/")));
|
||||
all.sort((a, b) -> a.getServerId().compareTo(b.getServerId()));
|
||||
if (nextToken != null && !nextToken.isEmpty()) {
|
||||
int idx = 0;
|
||||
@@ -125,7 +130,7 @@ public class TransferService {
|
||||
"Server is not in OFFLINE state.", 409);
|
||||
}
|
||||
server.setState("ONLINE");
|
||||
serverStore.put(serverId, server);
|
||||
putServer(server);
|
||||
return server;
|
||||
}
|
||||
|
||||
@@ -136,7 +141,7 @@ public class TransferService {
|
||||
"Server is not in ONLINE state.", 409);
|
||||
}
|
||||
server.setState("OFFLINE");
|
||||
serverStore.put(serverId, server);
|
||||
putServer(server);
|
||||
return server;
|
||||
}
|
||||
|
||||
@@ -163,7 +168,7 @@ public class TransferService {
|
||||
if (securityPolicyName != null) {
|
||||
server.setSecurityPolicyName(securityPolicyName);
|
||||
}
|
||||
serverStore.put(serverId, server);
|
||||
putServer(server);
|
||||
return server;
|
||||
}
|
||||
|
||||
@@ -174,12 +179,13 @@ public class TransferService {
|
||||
List<HomeDirectoryMapping> homeDirectoryMappings,
|
||||
Map<String, String> tags) {
|
||||
getServer(serverId);
|
||||
String key = serverId + "/" + userName;
|
||||
if (userStore.get(key).isPresent()) {
|
||||
String key = userKey(serverId, userName);
|
||||
if (findUser(serverId, userName).isPresent()) {
|
||||
throw new AwsException("ResourceExistsException",
|
||||
"User " + userName + " already exists on server " + serverId + ".", 400);
|
||||
}
|
||||
|
||||
region = currentRegion();
|
||||
String arn = regionResolver.buildArn("transfer", region, "user/" + serverId + "/" + userName);
|
||||
User user = new User();
|
||||
user.setUserName(userName);
|
||||
@@ -191,10 +197,10 @@ public class TransferService {
|
||||
user.setSshPublicKeys(new ArrayList<>());
|
||||
user.setTags(tags != null ? tags : new HashMap<>());
|
||||
|
||||
userStore.put(key, user);
|
||||
putUser(user);
|
||||
|
||||
if (tags != null && !tags.isEmpty()) {
|
||||
tagStore.put("user/" + key, new HashMap<>(tags));
|
||||
putTags("user/" + key, tags);
|
||||
}
|
||||
|
||||
return user;
|
||||
@@ -202,21 +208,20 @@ public class TransferService {
|
||||
|
||||
public User getUser(String serverId, String userName) {
|
||||
getServer(serverId);
|
||||
return userStore.get(serverId + "/" + userName).orElseThrow(() ->
|
||||
return findUser(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);
|
||||
deleteUserRecord(getUser(serverId, userName));
|
||||
}
|
||||
|
||||
public List<User> listUsers(String serverId, String nextToken, int maxResults) {
|
||||
getServer(serverId);
|
||||
List<User> all = new ArrayList<>(userStore.scan(k -> k.startsWith(serverId + "/")));
|
||||
migrateLegacyUsers(currentAccount(), currentRegion(), serverId);
|
||||
List<User> all = new ArrayList<>(userStore.scanForAccount(currentAccount(),
|
||||
k -> k.startsWith(scopedKey(currentRegion(), serverId) + "/")));
|
||||
all.sort((a, b) -> a.getUserName().compareTo(b.getUserName()));
|
||||
if (nextToken != null && !nextToken.isEmpty()) {
|
||||
int idx = 0;
|
||||
@@ -242,7 +247,7 @@ public class TransferService {
|
||||
if (homeDirectory != null) user.setHomeDirectory(homeDirectory);
|
||||
if (homeDirectoryType != null) user.setHomeDirectoryType(homeDirectoryType);
|
||||
if (homeDirectoryMappings != null) user.setHomeDirectoryMappings(homeDirectoryMappings);
|
||||
userStore.put(serverId + "/" + userName, user);
|
||||
putUser(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
@@ -255,7 +260,7 @@ public class TransferService {
|
||||
List<SshPublicKey> keys = new ArrayList<>(user.getSshPublicKeys() != null ? user.getSshPublicKeys() : List.of());
|
||||
keys.add(key);
|
||||
user.setSshPublicKeys(keys);
|
||||
userStore.put(serverId + "/" + userName, user);
|
||||
putUser(user);
|
||||
return key;
|
||||
}
|
||||
|
||||
@@ -268,21 +273,22 @@ public class TransferService {
|
||||
"SSH public key " + sshPublicKeyId + " does not exist.", 404);
|
||||
}
|
||||
user.setSshPublicKeys(keys);
|
||||
userStore.put(serverId + "/" + userName, user);
|
||||
putUser(user);
|
||||
}
|
||||
|
||||
// ── Tags ──────────────────────────────────────────────────────────────────
|
||||
|
||||
public Map<String, String> listTagsForResource(String arn) {
|
||||
String key = arnToTagKey(arn);
|
||||
return tagStore.get(key).orElse(new HashMap<>());
|
||||
return storedTags(key);
|
||||
}
|
||||
|
||||
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<>()));
|
||||
Map<String, String> existing = new HashMap<>(storedTags(key));
|
||||
resourceTags(arn).ifPresent(resource -> existing.putAll(resource));
|
||||
existing.putAll(tags);
|
||||
tagStore.put(key, existing);
|
||||
putTags(key, existing);
|
||||
|
||||
// Also sync tags into the resource object
|
||||
syncTagsToResource(arn, existing);
|
||||
@@ -290,9 +296,9 @@ public class TransferService {
|
||||
|
||||
public void untagResource(String arn, List<String> tagKeys) {
|
||||
String key = arnToTagKey(arn);
|
||||
Map<String, String> existing = new HashMap<>(tagStore.get(key).orElse(new HashMap<>()));
|
||||
Map<String, String> existing = new HashMap<>(storedTags(key));
|
||||
tagKeys.forEach(existing::remove);
|
||||
tagStore.put(key, existing);
|
||||
putTags(key, existing);
|
||||
syncTagsToResource(arn, existing);
|
||||
}
|
||||
|
||||
@@ -306,30 +312,235 @@ public class TransferService {
|
||||
}
|
||||
|
||||
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;
|
||||
AwsArnUtils.Arn parsed;
|
||||
try {
|
||||
parsed = AwsArnUtils.parse(arn);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("ResourceNotFoundException", "Resource " + arn + " does not exist.", 404);
|
||||
}
|
||||
if (!"transfer".equals(parsed.service())
|
||||
|| !currentAccount().equals(parsed.accountId())
|
||||
|| !currentRegion().equals(parsed.region())) {
|
||||
throw new AwsException("ResourceNotFoundException", "Resource " + arn + " does not exist.", 404);
|
||||
}
|
||||
String resource = parsed.resource();
|
||||
if (resource.startsWith("server/")) {
|
||||
getServer(resource.substring("server/".length()));
|
||||
} else if (resource.startsWith("user/")) {
|
||||
String[] parts = resource.substring("user/".length()).split("/", 2);
|
||||
if (parts.length != 2) {
|
||||
throw new AwsException("ResourceNotFoundException", "Resource " + arn + " does not exist.", 404);
|
||||
}
|
||||
getUser(parts[0], parts[1]);
|
||||
} else {
|
||||
throw new AwsException("ResourceNotFoundException", "Resource " + arn + " does not exist.", 404);
|
||||
}
|
||||
return resource;
|
||||
}
|
||||
|
||||
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 -> {
|
||||
serverStore.getForAccount(currentAccount(), scopedKey(currentRegion(), serverId)).ifPresent(s -> {
|
||||
s.setTags(tags);
|
||||
serverStore.put(serverId, s);
|
||||
putServer(s);
|
||||
});
|
||||
} else if (key.startsWith("user/")) {
|
||||
String userKey = key.substring("user/".length());
|
||||
userStore.get(userKey).ifPresent(u -> {
|
||||
userStore.getForAccount(currentAccount(), scopedKey(currentRegion(), userKey)).ifPresent(u -> {
|
||||
u.setTags(tags);
|
||||
userStore.put(userKey, u);
|
||||
putUser(u);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public int countUsers(String serverId) {
|
||||
return (int) userStore.scan(k -> k.startsWith(serverId + "/")).stream().count();
|
||||
migrateLegacyUsers(currentAccount(), currentRegion(), serverId);
|
||||
return (int) userStore.scanForAccount(currentAccount(),
|
||||
k -> k.startsWith(scopedKey(currentRegion(), serverId) + "/")).stream().count();
|
||||
}
|
||||
|
||||
private Optional<Server> findServer(String serverId) {
|
||||
String accountId = currentAccount();
|
||||
String region = currentRegion();
|
||||
return serverStore.getForAccountMigratingLegacyKeys(accountId, scopedKey(region, serverId),
|
||||
List.of(serverId), server -> owns(server, accountId, region));
|
||||
}
|
||||
|
||||
private Optional<User> findUser(String serverId, String userName) {
|
||||
String accountId = currentAccount();
|
||||
String region = currentRegion();
|
||||
String key = userKey(serverId, userName);
|
||||
return userStore.getForAccountMigratingLegacyKeys(accountId, scopedKey(region, key),
|
||||
List.of(key), user -> owns(user, accountId, region));
|
||||
}
|
||||
|
||||
private void migrateLegacyServers(String account, String region) {
|
||||
for (Server server : serverStore.scanUnscopedLegacy(value -> owns(value, account, region))) {
|
||||
migrateServer(server, account, region);
|
||||
}
|
||||
for (String key : serverStore.keysForAccount(account)) {
|
||||
if (key.contains("/")) {
|
||||
continue;
|
||||
}
|
||||
serverStore.getForAccount(account, key)
|
||||
.filter(value -> owns(value, account, region))
|
||||
.ifPresent(value -> migrateServer(value, account, region));
|
||||
}
|
||||
}
|
||||
|
||||
private void migrateServer(Server server, String account, String region) {
|
||||
String key = server.getServerId();
|
||||
serverStore.getForAccountMigratingLegacyKeys(account, scopedKey(region, key), List.of(key),
|
||||
value -> owns(value, account, region)).ifPresent(value ->
|
||||
serverStore.putForAccount(account, scopedKey(region, key), value));
|
||||
}
|
||||
|
||||
private void migrateLegacyUsers(String account, String region, String serverId) {
|
||||
for (User user : userStore.scanUnscopedLegacy(value -> owns(value, account, region)
|
||||
&& userMatchesServer(value, serverId))) {
|
||||
migrateUser(user, account, region, serverId);
|
||||
}
|
||||
for (String key : userStore.keysForAccount(account)) {
|
||||
if (key.contains("/") && key.startsWith(region + "/")) {
|
||||
continue;
|
||||
}
|
||||
userStore.getForAccount(account, key)
|
||||
.filter(value -> owns(value, account, region) && userMatchesServer(value, serverId))
|
||||
.ifPresent(value -> migrateUser(value, account, region, serverId));
|
||||
}
|
||||
}
|
||||
|
||||
private void migrateUser(User user, String account, String region, String serverId) {
|
||||
String key = userKey(serverId, user.getUserName());
|
||||
userStore.getForAccountMigratingLegacyKeys(account, scopedKey(region, key), List.of(key),
|
||||
value -> owns(value, account, region)).ifPresent(value ->
|
||||
userStore.putForAccount(account, scopedKey(region, key), value));
|
||||
}
|
||||
|
||||
private static boolean userMatchesServer(User user, String serverId) {
|
||||
try {
|
||||
return AwsArnUtils.parse(user.getArn()).resource().equals("user/" + serverId + "/" + user.getUserName());
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, String> storedTags(String key) {
|
||||
String account = currentAccount();
|
||||
String scoped = scopedKey(currentRegion(), key);
|
||||
Map<String, String> merged = new HashMap<>(tagStore.getForAccount(account, scoped)
|
||||
.orElseGet(HashMap::new));
|
||||
tagStore.getForAccount(account, key).ifPresent(merged::putAll);
|
||||
if (!merged.isEmpty()) {
|
||||
tagStore.putForAccount(account, scoped, merged);
|
||||
}
|
||||
return tagStore.getForAccountMigratingLegacyKeys(account, scoped, List.of(key), ignored -> true)
|
||||
.orElseGet(HashMap::new);
|
||||
}
|
||||
|
||||
private Optional<Map<String, String>> resourceTags(String arn) {
|
||||
String resource = AwsArnUtils.parse(arn).resource();
|
||||
if (resource.startsWith("server/")) {
|
||||
return Optional.ofNullable(getServer(resource.substring("server/".length())).getTags());
|
||||
}
|
||||
String[] parts = resource.substring("user/".length()).split("/", 2);
|
||||
return Optional.ofNullable(getUser(parts[0], parts[1]).getTags());
|
||||
}
|
||||
|
||||
private void putServer(Server server) {
|
||||
serverStore.putForAccount(currentAccount(), scopedKey(regionOf(server), server.getServerId()), server);
|
||||
}
|
||||
|
||||
private void deleteServerRecord(Server server) {
|
||||
String serverId = server.getServerId();
|
||||
serverStore.deleteForAccount(currentAccount(), scopedKey(regionOf(server), serverId));
|
||||
tagStore.deleteForAccount(currentAccount(), scopedKey(regionOf(server), "server/" + serverId));
|
||||
tagStore.deleteForAccount(currentAccount(), "server/" + serverId);
|
||||
}
|
||||
|
||||
private void putUser(User user) {
|
||||
String[] resource = userResourceParts(user);
|
||||
userStore.putForAccount(currentAccount(), scopedKey(regionOf(user), userKey(resource[0], resource[1])), user);
|
||||
}
|
||||
|
||||
private void deleteUserRecord(User user) {
|
||||
String[] resource = userResourceParts(user);
|
||||
String key = userKey(resource[0], resource[1]);
|
||||
userStore.deleteForAccount(currentAccount(), scopedKey(regionOf(user), key));
|
||||
tagStore.deleteForAccount(currentAccount(), scopedKey(regionOf(user), "user/" + key));
|
||||
userStore.deleteForAccount(currentAccount(), key);
|
||||
tagStore.deleteForAccount(currentAccount(), "user/" + key);
|
||||
}
|
||||
|
||||
private void putTags(String key, Map<String, String> tags) {
|
||||
tagStore.putForAccount(currentAccount(), scopedKey(currentRegion(), key), new HashMap<>(tags));
|
||||
}
|
||||
|
||||
private String currentAccount() {
|
||||
return regionResolver.getAccountId();
|
||||
}
|
||||
|
||||
private String currentRegion() {
|
||||
return regionResolver.getRegion();
|
||||
}
|
||||
|
||||
private static String scopedKey(String region, String key) {
|
||||
return region + "/" + key;
|
||||
}
|
||||
|
||||
private static String userKey(String serverId, String userName) {
|
||||
return serverId + "/" + userName;
|
||||
}
|
||||
|
||||
private static boolean owns(Server server, String accountId, String region) {
|
||||
return ownsArn(server.getArn(), accountId, region);
|
||||
}
|
||||
|
||||
private static boolean owns(User user, String accountId, String region) {
|
||||
return ownsArn(user.getArn(), accountId, region);
|
||||
}
|
||||
|
||||
private static boolean ownsArn(String arn, String accountId, String region) {
|
||||
try {
|
||||
AwsArnUtils.Arn parsed = AwsArnUtils.parse(arn);
|
||||
return "transfer".equals(parsed.service())
|
||||
&& accountId.equals(parsed.accountId())
|
||||
&& region.equals(parsed.region());
|
||||
} catch (IllegalArgumentException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static String regionOf(Server server) {
|
||||
return parseRegion(server.getArn());
|
||||
}
|
||||
|
||||
private static String regionOf(User user) {
|
||||
return parseRegion(user.getArn());
|
||||
}
|
||||
|
||||
private static String parseRegion(String arn) {
|
||||
try {
|
||||
return AwsArnUtils.parse(arn).region();
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("ResourceNotFoundException", "Persisted Transfer resource has an invalid ARN.", 404);
|
||||
}
|
||||
}
|
||||
|
||||
private static String[] userResourceParts(User user) {
|
||||
try {
|
||||
String resource = AwsArnUtils.parse(user.getArn()).resource();
|
||||
String[] parts = resource.startsWith("user/")
|
||||
? resource.substring("user/".length()).split("/", 2)
|
||||
: new String[0];
|
||||
if (parts.length == 2) {
|
||||
return parts;
|
||||
}
|
||||
} catch (IllegalArgumentException ignored) {
|
||||
// Convert corrupt persisted records into the AWS resource error below.
|
||||
}
|
||||
throw new AwsException("ResourceNotFoundException", "Persisted Transfer user has an invalid ARN.", 404);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user