From 50862d780e867b105548db3cf07736a10c8b6d8c Mon Sep 17 00:00:00 2001 From: mikera Date: Fri, 23 Aug 2024 12:54:18 +0100 Subject: [PATCH] Add peer admin REST API --- .../main/java/convex/restapi/RESTServer.java | 6 ++ .../java/convex/restapi/api/PeerAdminAPI.java | 74 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 convex-restapi/src/main/java/convex/restapi/api/PeerAdminAPI.java diff --git a/convex-restapi/src/main/java/convex/restapi/RESTServer.java b/convex-restapi/src/main/java/convex/restapi/RESTServer.java index e64486ba8..454218164 100644 --- a/convex-restapi/src/main/java/convex/restapi/RESTServer.java +++ b/convex-restapi/src/main/java/convex/restapi/RESTServer.java @@ -16,6 +16,7 @@ import convex.restapi.api.ChainAPI; import convex.restapi.api.DLAPI; import convex.restapi.api.DepAPI; +import convex.restapi.api.PeerAdminAPI; import io.javalin.Javalin; import io.javalin.community.ssl.SslPlugin; import io.javalin.config.JavalinConfig; @@ -129,6 +130,7 @@ protected void addOpenApiPlugins(JavalinConfig config) { protected ChainAPI chainAPI; protected DepAPI depAPI; protected DLAPI dlAPI; + protected PeerAdminAPI peerAPI; private void addAPIRoutes(Javalin app) { chainAPI = new ChainAPI(this); @@ -136,6 +138,10 @@ private void addAPIRoutes(Javalin app) { depAPI = new DepAPI(this); depAPI.addRoutes(app); + + peerAPI = new PeerAdminAPI(this); + peerAPI.addRoutes(app); + dlAPI = new DLAPI(this); dlAPI.addRoutes(app); diff --git a/convex-restapi/src/main/java/convex/restapi/api/PeerAdminAPI.java b/convex-restapi/src/main/java/convex/restapi/api/PeerAdminAPI.java new file mode 100644 index 000000000..bc329bc5a --- /dev/null +++ b/convex-restapi/src/main/java/convex/restapi/api/PeerAdminAPI.java @@ -0,0 +1,74 @@ +package convex.restapi.api; + +import java.util.Arrays; +import java.util.HashSet; + +import convex.peer.Server; +import convex.restapi.RESTServer; +import convex.restapi.model.CreateAccountResponse; +import io.javalin.Javalin; +import io.javalin.http.Context; +import io.javalin.http.InternalServerErrorResponse; +import io.javalin.openapi.HttpMethod; +import io.javalin.openapi.OpenApi; +import io.javalin.openapi.OpenApiContent; +import io.javalin.openapi.OpenApiResponse; + +public class PeerAdminAPI extends ABaseAPI { + + public Server server; + + public PeerAdminAPI(RESTServer restServer) { + super(restServer); + server = restServer.getServer(); + } + + private static final String ROUTE = "/api/v1/"; + + @Override + public void addRoutes(Javalin app) { + String prefix = ROUTE; + + app.post(prefix + "peer/shutdown", this::shutDown); + + } + + @OpenApi(path = ROUTE + "peer/shutdown", + methods = HttpMethod.POST, + operationId = "shutdownPeer", + tags = { "Admin"}, + summary = "Shut down the current peer", + responses = { + @OpenApiResponse( + status = "200", + description = "Peer shutdown initiated", + content = { + @OpenApiContent( + type = "application/json", + from = CreateAccountResponse.class) }) + }) + public void shutDown(Context ctx) { + ensureLocalAdmin(ctx); + try { + server.shutdown(); + } catch (Exception e) { + e.printStackTrace(); + throw new InternalServerErrorResponse("Failed to initiate sutdown: "+e.getMessage()); + } + ctx.result("Shutdown initiated."); + } + + private HashSet authorisedIPs = new HashSet(Arrays.asList("127.0.0.1","::1","[0:0:0:0:0:0:0:1]")); + + private void ensureLocalAdmin(Context ctx) { + String ip=ctx.ip(); + if (authorisedIPs.contains(ip)) { + return; + } else { + throw new io.javalin.http.UnauthorizedResponse("Can't performa admin actions from IP: "+ip); + } + } + + + +}