mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(core): serve the shared /_floci control-plane contract (#8431)
Health at /_floci/health, /_localstack/health and /health gains status UP
and cloud aws from one shared body. GET /_floci/tls-cert serves the same
PEM as ca.pem, as application/x-pem-file, and answers the shared 404
{tlsEnabled, error, message} when TLS is off.
A path under /_floci or /_localstack that no route serves answers a JSON
404 from ControlPlaneNotFoundFilter instead of falling through to S3, and
S3VirtualHostFilter leaves those paths alone, so a virtual-hosted request
can no longer address a key under _floci/ or _localstack/. The endpoints
are documented on one page.
This commit is contained in:
1 parent
70924d4afc
commit
7e146f8180
11 files changed
+467
-13
No files matched your search
@@ -0,0 +1,87 @@
|
||||
package io.floci.aws.lifecycle;
|
||||
|
||||
import io.floci.aws.core.common.RoutedResource;
|
||||
import jakarta.annotation.Priority;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Priorities;
|
||||
import jakarta.ws.rs.container.ContainerRequestContext;
|
||||
import jakarta.ws.rs.container.ContainerRequestFilter;
|
||||
import jakarta.ws.rs.container.ResourceInfo;
|
||||
import jakarta.ws.rs.core.Context;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.ext.Provider;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Answers a path under the control-plane prefixes ({@code /_floci}, {@code /_localstack}) that no
|
||||
* control-plane resource serves with a JSON 404, the catch-all every Floci emulator shares. Without
|
||||
* it the request falls through to S3's path-style routes as an object in a bucket named
|
||||
* {@code _floci}, which no S3 bucket can be named, and a client probing a wrong path reads an S3 XML
|
||||
* error. A control-plane resource is one whose route is declared under the prefix: the lifecycle
|
||||
* endpoints, the console, and the ECR, EKS, AppSync and sign-in routes.
|
||||
* <p>
|
||||
* Runs after route matching, so {@link ResourceInfo} names the route, and before the protocol claim
|
||||
* and every signature or IAM check, which would otherwise judge the request as an S3 call.
|
||||
*/
|
||||
@Provider
|
||||
@Priority(Priorities.AUTHENTICATION - 300)
|
||||
public class ControlPlaneNotFoundFilter implements ContainerRequestFilter {
|
||||
|
||||
static final List<String> PREFIXES = List.of("_floci", "_localstack");
|
||||
private static final String PREFIX_TEMPLATE = "{prefix:(_floci|_localstack)}";
|
||||
|
||||
@Context
|
||||
ResourceInfo resourceInfo;
|
||||
|
||||
@Override
|
||||
public void filter(ContainerRequestContext ctx) {
|
||||
String path = ctx.getUriInfo().getPath();
|
||||
if (!isControlPlanePath(path)) {
|
||||
return;
|
||||
}
|
||||
Class<?> resourceClass = RoutedResource.resourceClass(resourceInfo);
|
||||
if (resourceClass != null && isControlPlaneRoute(resourceClass, resourceInfo.getResourceMethod())) {
|
||||
return;
|
||||
}
|
||||
ctx.abortWith(notFound(path));
|
||||
}
|
||||
|
||||
/** True for {@code /_floci}, {@code /_localstack} and any path under them, with or without the leading slash. */
|
||||
public static boolean isControlPlanePath(String path) {
|
||||
String relative = stripLeadingSlash(path);
|
||||
for (String prefix : PREFIXES) {
|
||||
if (relative.equals(prefix) || relative.startsWith(prefix + "/")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static boolean isControlPlaneRoute(Class<?> resourceClass, Method method) {
|
||||
String route = stripLeadingSlash(template(resourceClass.getAnnotation(Path.class)));
|
||||
if (route.isEmpty() && method != null) {
|
||||
route = stripLeadingSlash(template(method.getAnnotation(Path.class)));
|
||||
}
|
||||
return route.startsWith(PREFIX_TEMPLATE) || isControlPlanePath(route);
|
||||
}
|
||||
|
||||
static Response notFound(String path) {
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("error", "NotFound");
|
||||
body.put("path", path.startsWith("/") ? path : "/" + path);
|
||||
return Response.status(Response.Status.NOT_FOUND).type(MediaType.APPLICATION_JSON).entity(body).build();
|
||||
}
|
||||
|
||||
private static String template(Path path) {
|
||||
return path == null ? "" : path.value();
|
||||
}
|
||||
|
||||
private static String stripLeadingSlash(String path) {
|
||||
return path.startsWith("/") ? path.substring(1) : path;
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@ public class EmulatorInfoController {
|
||||
private static final Logger LOG = Logger.getLogger(EmulatorInfoController.class);
|
||||
static final int MANIFEST_VERSION = 1;
|
||||
static final int CONSOLE_CONTRACT = 2;
|
||||
private static final String PEM_FILE = "application/x-pem-file";
|
||||
|
||||
private final ServiceRegistry serviceRegistry;
|
||||
private final InitLifecycleState initLifecycleState;
|
||||
@@ -73,11 +74,7 @@ public class EmulatorInfoController {
|
||||
@GET
|
||||
@Path("/health")
|
||||
public Response health() {
|
||||
return Response.ok(Map.of(
|
||||
"services", serviceRegistry.getServices(),
|
||||
"edition", "community",
|
||||
"original_edition", "floci-always-free",
|
||||
"version", version)).build();
|
||||
return Response.ok(HealthController.body(serviceRegistry.getServices(), version)).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@@ -113,7 +110,7 @@ public class EmulatorInfoController {
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("manifest_version", MANIFEST_VERSION);
|
||||
body.put("console_contract", CONSOLE_CONTRACT);
|
||||
body.put("cloud", "aws");
|
||||
body.put("cloud", HealthController.CLOUD);
|
||||
body.put("emulator", "floci");
|
||||
body.put("version", version);
|
||||
body.put("edition", "community");
|
||||
@@ -171,6 +168,43 @@ public class EmulatorInfoController {
|
||||
@Path("/ca.pem")
|
||||
@Produces(MediaType.TEXT_PLAIN)
|
||||
public String caPem() throws IOException, GeneralSecurityException {
|
||||
return trustBundle();
|
||||
}
|
||||
|
||||
/**
|
||||
* The certificate a client must trust for this emulator's HTTPS endpoint, at the path every Floci
|
||||
* emulator serves it under: the same PEM as {@code ca.pem}, as {@code application/x-pem-file}. With
|
||||
* TLS off there is no HTTPS endpoint to trust, so it answers the 404 the other emulators answer;
|
||||
* {@code ca.pem} keeps serving the local CA, which still signs the certificates Floci issues.
|
||||
*/
|
||||
@GET
|
||||
@Path("/tls-cert")
|
||||
@Produces({PEM_FILE, MediaType.APPLICATION_JSON})
|
||||
public Response tlsCert() {
|
||||
if (!config.tls().enabled()) {
|
||||
return tlsCertUnavailable(false, "TLS is not enabled",
|
||||
"Floci is serving plain HTTP only. Set FLOCI_TLS_ENABLED=true and restart to serve HTTPS "
|
||||
+ "on the same port. GET /_floci/ca.pem serves the local CA either way.");
|
||||
}
|
||||
try {
|
||||
return Response.ok(trustBundle()).type(PEM_FILE).build();
|
||||
} catch (IOException | GeneralSecurityException e) {
|
||||
LOG.warnv(e, "Cannot read the TLS certificate served at /_floci/tls-cert");
|
||||
return tlsCertUnavailable(true, "TLS certificate not available",
|
||||
"TLS is enabled but the certificate cannot be read. Check floci.tls.cert-path and the "
|
||||
+ "startup logs.");
|
||||
}
|
||||
}
|
||||
|
||||
private static Response tlsCertUnavailable(boolean tlsEnabled, String error, String message) {
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("tlsEnabled", tlsEnabled);
|
||||
body.put("error", error);
|
||||
body.put("message", message);
|
||||
return Response.status(Response.Status.NOT_FOUND).type(MediaType.APPLICATION_JSON).entity(body).build();
|
||||
}
|
||||
|
||||
private String trustBundle() throws IOException, GeneralSecurityException {
|
||||
String localCa = certificateAuthority.caPem();
|
||||
Optional<String> userCertificate = userCertificatePath();
|
||||
if (userCertificate.isEmpty()) {
|
||||
|
||||
@@ -9,12 +9,16 @@ import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Path("/health")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public class HealthController {
|
||||
|
||||
/** The {@code cloud} this emulator names itself with in its health and info bodies. */
|
||||
static final String CLOUD = "aws";
|
||||
|
||||
private final ServiceRegistry serviceRegistry;
|
||||
private final String version;
|
||||
|
||||
@@ -26,10 +30,24 @@ public class HealthController {
|
||||
|
||||
@GET
|
||||
public Response health() {
|
||||
return Response.ok(Map.of(
|
||||
"services", serviceRegistry.getServices(),
|
||||
"edition", "community",
|
||||
"original_edition", "floci-always-free",
|
||||
"version", version)).build();
|
||||
return Response.ok(body(serviceRegistry.getServices(), version)).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* The health body of the Floci control-plane contract, shared by bare {@code /health} and
|
||||
* {@code /_floci/health} so the two cannot drift. {@code status}, {@code cloud}, {@code services}
|
||||
* and {@code version} are the fields every Floci emulator serves; {@code edition} and
|
||||
* {@code original_edition} are this emulator's own and stay as they always were. Floci has no
|
||||
* failure signal of its own, so {@code status} is always {@code UP}.
|
||||
*/
|
||||
static Map<String, Object> body(Map<String, String> services, String version) {
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("status", "UP");
|
||||
body.put("cloud", CLOUD);
|
||||
body.put("services", services);
|
||||
body.put("edition", "community");
|
||||
body.put("original_edition", "floci-always-free");
|
||||
body.put("version", version);
|
||||
return body;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import io.floci.aws.core.common.RequestUriRewrite;
|
||||
import io.floci.aws.core.common.ResteasyRequests;
|
||||
import io.floci.aws.core.common.dns.EmbeddedDnsServer;
|
||||
import io.floci.aws.core.common.docker.ContainerDetector;
|
||||
import io.floci.aws.lifecycle.ControlPlaneNotFoundFilter;
|
||||
import io.floci.aws.services.apigateway.ApiGatewayCustomDomainFilter;
|
||||
import io.floci.aws.services.cloudfront.CloudFrontDistributionFilter;
|
||||
import io.floci.aws.services.cognito.CognitoCustomDomainFilter;
|
||||
@@ -169,6 +170,13 @@ public class S3VirtualHostFilter implements ContainerRequestFilter {
|
||||
return;
|
||||
}
|
||||
|
||||
// The Floci control plane is matched before any service routing, so a health probe sent with a
|
||||
// bucket-style Host still reaches it. The cost is that a virtual-hosted request cannot address
|
||||
// an object key under _floci/ or _localstack/; a path-style request still can.
|
||||
if (ControlPlaneNotFoundFilter.isControlPlanePath(path)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// A higher-priority Host filter (API Gateway custom domain, CloudFront, Cognito custom
|
||||
// domain) may have already routed this request. Use its server-side marker rather than
|
||||
// trusting a user-controlled path prefix.
|
||||
|
||||
Reference in new issue
Block a user