Configure gateway limits and encrypted storage
This commit is contained in:
1 parent
43987bbadb
commit
510e519bd1
23 files changed
+480
-17
No files matched your search
@@ -15,6 +15,7 @@ public final class ClusterGc {
|
||||
if (!"cluster".equals(env.get("STORE_MODE")) || !"true".equals(env.get("CLUSTER_LOCAL_DEV")))
|
||||
throw new IllegalArgumentException("Cluster garbage collection is only enabled in local cluster mode");
|
||||
boolean testDomains = "true".equals(env.get("CLUSTER_TEST_NODE_DOMAINS"));
|
||||
ObjectStorage.Limits limits = StorageLimits.fromEnvironment(env);
|
||||
boolean disposableTest = testDomains && "true".equals(env.get("CLUSTER_GC_TEST_MODE"));
|
||||
long age = Long.parseLong(env.getOrDefault("CLUSTER_GC_MIN_AGE_SECONDS", "1209600"));
|
||||
long backupRetention = Long.parseLong(env.getOrDefault("CLUSTER_BACKUP_RETENTION_SECONDS", "0"));
|
||||
@@ -25,7 +26,8 @@ public final class ClusterGc {
|
||||
try (ClusterStore store = new ClusterStore(env.get("POSTGRES_JDBC_URL"), env.get("POSTGRES_USER"),
|
||||
env.get("POSTGRES_PASSWORD"), env.get("S3_BUCKET"),
|
||||
Arrays.stream(env.get("CLUSTER_NODES").split(",")).map(URI::create).toList(),
|
||||
env.get("CLUSTER_TOKEN"), env.get("CLUSTER_REPAIR_TOKEN"), 134217728, 2147483648L,
|
||||
env.get("CLUSTER_TOKEN"), env.get("CLUSTER_REPAIR_TOKEN"),
|
||||
limits.maxObjectBytes(), limits.maxTotalBytes(),
|
||||
testDomains)) {
|
||||
if (apply) {
|
||||
var repair = store.repairOnce();
|
||||
|
||||
@@ -12,6 +12,8 @@ public final class ClusterJoin {
|
||||
if (args.length != 2)
|
||||
throw new IllegalArgumentException("Usage: objectstore cluster-join node-url expected-host-uuid");
|
||||
Map<String, String> env = System.getenv();
|
||||
EncryptedVolume.requireConfigured(env,
|
||||
java.nio.file.Path.of(System.getProperty("java.io.tmpdir")));
|
||||
if (!"cluster".equals(env.get("STORE_MODE")) || !"true".equals(env.get("CLUSTER_LOCAL_DEV")))
|
||||
throw new IllegalArgumentException("Node registration is only enabled in local cluster mode");
|
||||
URI url = URI.create(args[0]);
|
||||
|
||||
@@ -24,6 +24,8 @@ public final class ClusterMigrate {
|
||||
(args.length != 2 || !args[0].equals("--apply")))
|
||||
throw new IllegalArgumentException("Usage: objectstore cluster-migrate --check | --apply node-id-0,node-id-1,node-id-2");
|
||||
Map<String, String> env = System.getenv();
|
||||
EncryptedVolume.requireConfigured(env,
|
||||
java.nio.file.Path.of(System.getProperty("java.io.tmpdir")));
|
||||
if (!"cluster".equals(env.get("STORE_MODE")) || !"true".equals(env.get("CLUSTER_LOCAL_DEV")))
|
||||
throw new IllegalArgumentException("Migration is enabled only in local cluster mode");
|
||||
List<URI> urls = Arrays.stream(env.get("CLUSTER_NODES").split(",", -1)).map(URI::create).toList();
|
||||
|
||||
@@ -32,6 +32,7 @@ public final class ClusterNode implements AutoCloseable {
|
||||
private final FileLock lock;
|
||||
|
||||
ClusterNode(Path root, String token, String repairToken, UUID hostId) throws IOException {
|
||||
EncryptedVolume.requireConfigured(System.getenv(), root);
|
||||
if (token == null || token.length() < 32) throw new IllegalArgumentException("Cluster token must have at least 32 characters");
|
||||
if (repairToken == null || repairToken.length() < 32 || repairToken.equals(token))
|
||||
throw new IllegalArgumentException("A separate repair token of at least 32 characters is required");
|
||||
|
||||
@@ -12,6 +12,7 @@ public final class ClusterRepair {
|
||||
if (!"cluster".equals(env.get("STORE_MODE")) || !"true".equals(env.get("CLUSTER_LOCAL_DEV")))
|
||||
throw new IllegalArgumentException("Cluster repair is only enabled in local cluster mode");
|
||||
boolean loop = args.length == 1;
|
||||
ObjectStorage.Limits limits = StorageLimits.fromEnvironment(env);
|
||||
long seconds = Long.parseLong(env.getOrDefault("CLUSTER_MAINTENANCE_INTERVAL_SECONDS", "60"));
|
||||
if (seconds < 1 || seconds > 3600) throw new IllegalArgumentException("Invalid maintenance interval");
|
||||
boolean gcEnabled = loop && "true".equals(env.get("CLUSTER_GC_ENABLED"));
|
||||
@@ -23,7 +24,8 @@ public final class ClusterRepair {
|
||||
try (ClusterStore store = new ClusterStore(env.get("POSTGRES_JDBC_URL"), env.get("POSTGRES_USER"),
|
||||
env.get("POSTGRES_PASSWORD"), env.get("S3_BUCKET"),
|
||||
Arrays.stream(env.get("CLUSTER_NODES").split(",")).map(URI::create).toList(),
|
||||
env.get("CLUSTER_TOKEN"), env.get("CLUSTER_REPAIR_TOKEN"), 134217728, 2147483648L,
|
||||
env.get("CLUSTER_TOKEN"), env.get("CLUSTER_REPAIR_TOKEN"),
|
||||
limits.maxObjectBytes(), limits.maxTotalBytes(),
|
||||
"true".equals(env.get("CLUSTER_TEST_NODE_DOMAINS")))) {
|
||||
var report = store.repairOnce();
|
||||
System.out.println("segments_scanned=" + report.scanned());
|
||||
|
||||
@@ -43,6 +43,8 @@ final class ClusterStore implements ObjectStorage, MultipartStorage {
|
||||
ClusterStore(String jdbcUrl, String user, String password, String bucket,
|
||||
List<URI> nodeUrls, String token, String repairToken, long maxObject, long maxTotal,
|
||||
boolean testNodeDomains) throws IOException {
|
||||
EncryptedVolume.requireConfigured(System.getenv(),
|
||||
java.nio.file.Path.of(System.getProperty("java.io.tmpdir")));
|
||||
if (jdbcUrl == null || !jdbcUrl.startsWith("jdbc:postgresql://") || user == null || password == null)
|
||||
throw new IllegalArgumentException("Invalid metadata database configuration");
|
||||
this.jdbcUrl = jdbcUrl;
|
||||
|
||||
@@ -48,6 +48,7 @@ final class DiskStore implements ObjectStorage {
|
||||
private record VersionRecord(String id, String storageId, boolean marker, long modified) {}
|
||||
|
||||
DiskStore(Path root, long maxObject, long maxTotal) throws IOException {
|
||||
EncryptedVolume.requireConfigured(System.getenv(), root);
|
||||
this.root = root;
|
||||
objects = root.resolve("objects");
|
||||
temporary = root.resolve("pending");
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package cloud.lunarsky.store;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
|
||||
final class EncryptedVolume {
|
||||
private EncryptedVolume() {}
|
||||
|
||||
static void requireConfigured(Map<String, String> environment) throws IOException {
|
||||
requireConfigured(environment, null);
|
||||
}
|
||||
|
||||
static void requireConfigured(Map<String, String> environment, Path dataDirectory) throws IOException {
|
||||
String marker = environment.getOrDefault("ENCRYPTED_VOLUME_MARKER_FILE", "");
|
||||
String expected = environment.getOrDefault("ENCRYPTED_VOLUME_ID", "");
|
||||
if (marker.isEmpty() && expected.isEmpty()) return;
|
||||
if (!expected.matches("[a-f0-9]{32}") || marker.isEmpty())
|
||||
throw new IOException("Encrypted storage marker configuration is incomplete");
|
||||
Path path = Path.of(marker);
|
||||
if (!path.isAbsolute() || dataDirectory != null &&
|
||||
!path.normalize().equals(dataDirectory.toAbsolutePath().normalize().resolve(".objectstore-encrypted")) ||
|
||||
Files.isSymbolicLink(path) || !Files.isRegularFile(path) ||
|
||||
Files.size(path) > 33 || !Files.readString(path, StandardCharsets.US_ASCII).trim().equals(expected))
|
||||
throw new IOException("Encrypted storage volume is unavailable or does not match its marker");
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,8 @@ public final class Main {
|
||||
private final String bucket;
|
||||
private final MultipartStorage multipart;
|
||||
private final ClientLimits clientLimits;
|
||||
private final Semaphore slots = new Semaphore(16);
|
||||
private final Semaphore slots;
|
||||
private final String virtualHostSuffix;
|
||||
|
||||
Main(DiskStore store, SigV4 authentication, String bucket) throws IOException {
|
||||
this(store, new MultipartStore(store), authentication, bucket);
|
||||
@@ -48,12 +49,21 @@ public final class Main {
|
||||
|
||||
Main(ObjectStorage store, MultipartStorage multipart, SigV4 authentication, String bucket,
|
||||
ClientLimits clientLimits) throws IOException {
|
||||
this(store, multipart, authentication, bucket, clientLimits, 16, "");
|
||||
}
|
||||
|
||||
Main(ObjectStorage store, MultipartStorage multipart, SigV4 authentication, String bucket,
|
||||
ClientLimits clientLimits, int maxInFlight, String virtualHostSuffix) throws IOException {
|
||||
if (maxInFlight < 1 || maxInFlight > 1024)
|
||||
throw new IllegalArgumentException("MAX_IN_FLIGHT_REQUESTS must be between 1 and 1024");
|
||||
this.store = store;
|
||||
this.multipart = multipart;
|
||||
this.authentication = authentication;
|
||||
this.owner = authentication.root();
|
||||
this.bucket = bucket;
|
||||
this.clientLimits = clientLimits;
|
||||
this.slots = new Semaphore(maxInFlight);
|
||||
this.virtualHostSuffix = validateVirtualHostSuffix(virtualHostSuffix);
|
||||
store.ensureBucket(bucket);
|
||||
}
|
||||
|
||||
@@ -102,7 +112,8 @@ public final class Main {
|
||||
capabilities(exchange);
|
||||
return;
|
||||
}
|
||||
if (path.equals("/")) {
|
||||
String hostBucket = virtualHostBucket(exchange);
|
||||
if (path.equals("/") && hostBucket == null) {
|
||||
requireOwner(verified.principal());
|
||||
if (!exchange.getRequestMethod().equals("GET") ||
|
||||
!(query.isEmpty() || query.size() == 1 && "ListBuckets".equals(query.get("x-id"))))
|
||||
@@ -112,16 +123,41 @@ public final class Main {
|
||||
return;
|
||||
}
|
||||
int slash = path.indexOf('/', 1);
|
||||
String requestedBucket = slash < 0 ? path.substring(1) : path.substring(1, slash);
|
||||
String requestedBucket = hostBucket == null ?
|
||||
(slash < 0 ? path.substring(1) : path.substring(1, slash)) : hostBucket;
|
||||
if (requestedBucket.isEmpty()) throw new StoreException(404, "NoSuchBucket", "Bucket not found");
|
||||
if (slash < 0 || slash == path.length() - 1) {
|
||||
if (hostBucket != null ? path.equals("/") : slash < 0 || slash == path.length() - 1) {
|
||||
handleBucket(exchange, query, verified.payload(), requestedBucket, verified.principal());
|
||||
} else {
|
||||
store.bucket(requestedBucket);
|
||||
handleObject(exchange, path.substring(slash + 1), query, verified, requestedBucket);
|
||||
handleObject(exchange, hostBucket == null ? path.substring(slash + 1) : path.substring(1),
|
||||
query, verified, requestedBucket);
|
||||
}
|
||||
}
|
||||
|
||||
private static String validateVirtualHostSuffix(String value) {
|
||||
if (value == null || value.isBlank()) return "";
|
||||
String suffix = value.toLowerCase(Locale.ROOT);
|
||||
if (suffix.length() > 253 || !suffix.matches("[a-z0-9-]+(?:\\.[a-z0-9-]+)*") ||
|
||||
java.util.Arrays.stream(suffix.split("\\.")).anyMatch(
|
||||
label -> label.length() > 63 || label.startsWith("-") || label.endsWith("-")))
|
||||
throw new IllegalArgumentException("Invalid S3_VIRTUAL_HOST_SUFFIX");
|
||||
return suffix;
|
||||
}
|
||||
|
||||
private String virtualHostBucket(HttpExchange exchange) {
|
||||
if (virtualHostSuffix.isEmpty()) return null;
|
||||
String host = exchange.getRequestHeaders().getFirst("Host");
|
||||
if (host == null) throw new StoreException(400, "InvalidRequest", "Missing Host header");
|
||||
host = host.toLowerCase(Locale.ROOT).replaceFirst(":\\d+$", "");
|
||||
String ending = "." + virtualHostSuffix;
|
||||
if (!host.endsWith(ending)) return null;
|
||||
String bucketName = host.substring(0, host.length() - ending.length());
|
||||
if (!bucketName.matches("[a-z0-9][a-z0-9-]{1,61}[a-z0-9]"))
|
||||
throw new StoreException(400, "InvalidRequest", "Invalid bucket hostname");
|
||||
return bucketName;
|
||||
}
|
||||
|
||||
private void sendStoreError(HttpExchange exchange, StoreException error, String requestId) throws IOException {
|
||||
if (error.status == 503 && error.code.equals("SlowDown"))
|
||||
exchange.getResponseHeaders().set("Retry-After", "1");
|
||||
@@ -1197,10 +1233,8 @@ public final class Main {
|
||||
if (!access.matches("[A-Za-z0-9]{16,128}") || secret.length() < 32 ||
|
||||
!bucket.matches("[a-z0-9][a-z0-9-]{1,61}[a-z0-9]"))
|
||||
throw new IllegalArgumentException("Invalid storage credentials/bucket configuration");
|
||||
long maxObject = Long.parseLong(env.getOrDefault("MAX_OBJECT_BYTES", "134217728"));
|
||||
long maxTotal = Long.parseLong(env.getOrDefault("MAX_TOTAL_BYTES", "2147483648"));
|
||||
if (maxObject < 1 || maxObject > 1073741824L || maxTotal < maxObject)
|
||||
throw new IllegalArgumentException("Invalid size limits");
|
||||
ObjectStorage.Limits limits = StorageLimits.fromEnvironment(env);
|
||||
long maxObject = limits.maxObjectBytes(), maxTotal = limits.maxTotalBytes();
|
||||
String mode = env.getOrDefault("STORE_MODE", "disk");
|
||||
ObjectStorage store;
|
||||
MultipartStorage multipart;
|
||||
@@ -1220,11 +1254,13 @@ public final class Main {
|
||||
multipart = new MultipartStore(disk);
|
||||
} else throw new IllegalArgumentException("Invalid STORE_MODE");
|
||||
var app = new Main(store, multipart, new SigV4(identities(env, access, secret),
|
||||
access, region, Clock.systemUTC()), bucket, ClientLimits.fromEnvironment(env));
|
||||
access, region, Clock.systemUTC()), bucket, ClientLimits.fromEnvironment(env),
|
||||
positiveInt(env, "MAX_IN_FLIGHT_REQUESTS", 16, 1024),
|
||||
env.getOrDefault("S3_VIRTUAL_HOST_SUFFIX", ""));
|
||||
int port = Integer.parseInt(env.getOrDefault("PORT", "9000"));
|
||||
var server = HttpServer.create(mode.equals("cluster")
|
||||
? new InetSocketAddress(env.getOrDefault("BIND_ADDRESS", "127.0.0.1"), port)
|
||||
: new InetSocketAddress(port), 64);
|
||||
: new InetSocketAddress(port), positiveInt(env, "HTTP_BACKLOG", 64, 4096));
|
||||
var executor = Executors.newVirtualThreadPerTaskExecutor();
|
||||
server.setExecutor(executor);
|
||||
server.createContext("/", app::handle);
|
||||
@@ -1291,4 +1327,16 @@ public final class Main {
|
||||
if (value == null || value.isBlank()) throw new IllegalArgumentException("Missing " + key);
|
||||
return value;
|
||||
}
|
||||
|
||||
private static int positiveInt(Map<String, String> env, String key, int fallback, int maximum) {
|
||||
String configured = env.get(key);
|
||||
if (configured == null || configured.isBlank()) return fallback;
|
||||
try {
|
||||
int value = Integer.parseInt(configured);
|
||||
if (value < 1 || value > maximum) throw new NumberFormatException();
|
||||
return value;
|
||||
} catch (NumberFormatException error) {
|
||||
throw new IllegalArgumentException(key + " must be between 1 and " + maximum, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package cloud.lunarsky.store;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
final class StorageLimits {
|
||||
private StorageLimits() {}
|
||||
|
||||
static ObjectStorage.Limits fromEnvironment(Map<String, String> environment) {
|
||||
try {
|
||||
long maxObject = Long.parseLong(environment.getOrDefault("MAX_OBJECT_BYTES", "134217728"));
|
||||
long maxTotal = Long.parseLong(environment.getOrDefault("MAX_TOTAL_BYTES", "2147483648"));
|
||||
if (maxObject < 1 || maxObject > 1073741824L || maxTotal < maxObject)
|
||||
throw new NumberFormatException();
|
||||
return new ObjectStorage.Limits(maxObject, maxTotal);
|
||||
} catch (NumberFormatException error) {
|
||||
throw new IllegalArgumentException("Invalid storage size limits", error);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user