Serve admin console only on dedicated port

This commit is contained in:
admin committed 2026-10-26 07:11:59 +01:00
1 parent 240c278621
commit fbec6694df
8 files changed
+140 -20

No files matched your search

+36 -8
View File
@@ -193,17 +193,27 @@ final class AdminConsole {
byte[] hash = SigV4.hash(trimmed);
java.util.Arrays.fill(trimmed, (byte) 0);
java.util.Arrays.fill(password, (byte) 0);
String configured = env.get("ADMIN_UI_ORIGIN");
String privateOrigin = env.get("ADMIN_UI_PRIVATE_ORIGIN");
String publicOrigin = env.get("ADMIN_UI_ORIGIN");
if (privateOrigin != null && !privateOrigin.isBlank() &&
publicOrigin != null && !publicOrigin.isBlank())
throw new IllegalArgumentException("Configure only one admin UI origin");
String configured = privateOrigin != null && !privateOrigin.isBlank() ? privateOrigin : publicOrigin;
URI origin = null;
if (configured != null && !configured.isBlank()) {
try { origin = URI.create(configured); }
catch (IllegalArgumentException error) {
throw new IllegalArgumentException("Invalid ADMIN_UI_ORIGIN", error);
}
if (!"https".equals(origin.getScheme()) || origin.getHost() == null ||
boolean privateHttp = privateOrigin != null && !privateOrigin.isBlank();
if (!(privateHttp ? "http".equals(origin.getScheme()) &&
privateIpv4(origin.getHost()) && origin.getPort() > 0 && origin.getPort() <= 65535 :
"https".equals(origin.getScheme()) && origin.getHost() != null) ||
origin.getUserInfo() != null || origin.getRawQuery() != null || origin.getRawFragment() != null ||
!(origin.getRawPath().isEmpty() || origin.getRawPath().equals("/")))
throw new IllegalArgumentException("ADMIN_UI_ORIGIN must be an HTTPS origin");
throw new IllegalArgumentException(privateHttp ?
"ADMIN_UI_PRIVATE_ORIGIN must be an HTTP origin with a private IPv4 address and port" :
"ADMIN_UI_ORIGIN must be an HTTPS origin");
origin = URI.create(origin.toString().replaceFirst("/$", ""));
}
return new AdminConsole(store, hash, origin,
@@ -218,7 +228,7 @@ final class AdminConsole {
if (origin == null) {
send(exchange, 403, page("<h1>Admin console unavailable</h1>" +
"<p>Open the local console at <code>http://127.0.0.1:&lt;port&gt;" + PATH +
"/login</code>, or use the configured HTTPS origin.</p>"), "text/html; charset=utf-8");
"/login</code>, or use the configured origin.</p>"), "text/html; charset=utf-8");
return;
}
String path = exchange.getRequestURI().getRawPath();
@@ -506,7 +516,7 @@ final class AdminConsole {
private String sessionCookie(HttpExchange exchange) {
String header = exchange.getRequestHeaders().getFirst("Cookie");
if (header == null || header.length() > 8192) return "";
String name = publicOrigin == null ? "objectstore_admin=" : "__Host-objectstore_admin=";
String name = secureOrigin() ? "__Host-objectstore_admin=" : "objectstore_admin=";
for (String field : header.split(";")) {
String value = field.trim();
if (value.startsWith(name) && value.substring(name.length()).matches("[A-Za-z0-9_-]{43}"))
@@ -516,10 +526,28 @@ final class AdminConsole {
}
private String cookie(String token, int seconds) {
String name = publicOrigin == null ? "objectstore_admin" : "__Host-objectstore_admin";
String path = publicOrigin == null ? PATH : "/";
String name = secureOrigin() ? "__Host-objectstore_admin" : "objectstore_admin";
String path = secureOrigin() ? "/" : PATH;
return name + "=" + token + "; Path=" + path + "; HttpOnly; SameSite=Strict; Max-Age=" + seconds +
(publicOrigin == null ? "" : "; Secure");
(secureOrigin() ? "; Secure" : "");
}
private boolean secureOrigin() {
return publicOrigin != null && "https".equals(publicOrigin.getScheme());
}
private static boolean privateIpv4(String host) {
if (host == null || !host.matches("[0-9]+(?:\\.[0-9]+){3}")) return false;
String[] fields = host.split("\\.");
int[] octets = new int[4];
for (int i = 0; i < octets.length; i++) {
if (fields[i].length() > 3 || (fields[i].length() > 1 && fields[i].startsWith("0"))) return false;
octets[i] = Integer.parseInt(fields[i]);
if (octets[i] > 255) return false;
}
return octets[0] == 10 ||
(octets[0] == 172 && octets[1] >= 16 && octets[1] <= 31) ||
(octets[0] == 192 && octets[1] == 168);
}
private String requestOrigin(HttpExchange exchange) {
+28 -7
View File
@@ -21,6 +21,7 @@ public final class Main {
private final ObjectHandler objectHandler;
private final ClientLimits clientLimits;
private final Semaphore slots;
private final Semaphore adminSlots = new Semaphore(8);
private final String virtualHostSuffix;
private final BrowserCors cors;
private final KeyScopes keyScopes;
@@ -123,13 +124,7 @@ public final class Main {
dataStarted = true;
}
if (path.equals(AdminConsole.PATH) || path.startsWith(AdminConsole.PATH + "/")) {
admitted = slots.tryAcquire();
if (!admitted) throw new StoreException(503, "SlowDown", "Too many concurrent requests");
if (adminConsole == null) {
exchange.sendResponseHeaders(404, -1);
} else {
adminConsole.handle(exchange);
}
exchange.sendResponseHeaders(404, -1);
return;
}
cors.decorate(exchange);
@@ -158,6 +153,32 @@ public final class Main {
}
}
void handleAdmin(HttpExchange exchange) throws IOException {
boolean admitted = false;
try {
String path = exchange.getRequestURI().getRawPath();
if (adminConsole == null ||
!(path.equals(AdminConsole.PATH) || path.startsWith(AdminConsole.PATH + "/"))) {
exchange.sendResponseHeaders(404, -1);
return;
}
admitted = adminSlots.tryAcquire();
if (!admitted) {
exchange.sendResponseHeaders(503, -1);
return;
}
adminConsole.handle(exchange);
} catch (StoreException error) {
exchange.sendResponseHeaders(error.status, -1);
} catch (Exception error) {
System.err.println("ObjectStore admin request failed: " + error.getClass().getSimpleName());
exchange.sendResponseHeaders(500, -1);
} finally {
exchange.close();
if (admitted) adminSlots.release();
}
}
private void dispatch(HttpExchange exchange) throws IOException {
SigV4.Verified verified = anonymousRead(exchange)
? new SigV4.Verified("UNSIGNED-PAYLOAD", exchange.getRequestURI().getRawQuery(),
@@ -55,23 +55,36 @@ final class ObjectStoreServer {
new RequestMetrics(env.get("METRICS_BEARER_TOKEN")),
IdentityPolicies.fromEnvironment(env, access, allIdentities),
publicEndpoint(env));
app.setAdminConsole(AdminConsole.fromEnvironment(env, store));
AdminConsole adminConsole = AdminConsole.fromEnvironment(env, store);
app.setAdminConsole(adminConsole);
int port = Integer.parseInt(env.getOrDefault("PORT", "9000"));
int adminPort = adminConsole == null ? 0 : positiveInt(env, "ADMIN_UI_PORT", 9001, 65535);
if (adminConsole != null && port == adminPort)
throw new IllegalArgumentException("ADMIN_UI_PORT must differ from PORT");
var server = HttpServer.create(mode.equals("cluster")
? new InetSocketAddress(env.getOrDefault("BIND_ADDRESS", "127.0.0.1"), port)
: new InetSocketAddress(port), positiveInt(env, "HTTP_BACKLOG", 64, 4096));
HttpServer adminServer = adminConsole == null ? null : HttpServer.create(
new InetSocketAddress(env.getOrDefault("ADMIN_UI_BIND_ADDRESS", "127.0.0.1"), adminPort), 16);
var executor = Executors.newVirtualThreadPerTaskExecutor();
server.setExecutor(executor);
server.createContext("/", app::handle);
if (adminServer != null) {
adminServer.setExecutor(executor);
adminServer.createContext("/", app::handleAdmin);
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
server.stop(5);
if (adminServer != null) adminServer.stop(5);
executor.close();
try { store.close(); }
catch (IOException error) { System.err.println("Could not release ObjectStore data lock: " + error.getMessage()); }
}));
server.start();
if (adminServer != null) adminServer.start();
if (store instanceof DiskStore disk) printStartup(disk, multipart, bucket, region, port);
else System.out.println("ObjectStore local cluster prototype v" + Version.VALUE + " listening on :" + port);
if (adminServer != null) System.out.println("ObjectStore admin listening on :" + adminPort);
}
static Map<String, String> identities(Map<String, String> env, String access, String secret)