Search in sources :

Example 1 with TimeoutOptions

use of org.hyperledger.besu.ethereum.api.handlers.TimeoutOptions in project besu by hyperledger.

the class JsonRpcHttpService method buildRouter.

private Router buildRouter() {
    // Handle json rpc requests
    final Router router = Router.router(vertx);
    router.route().handler(this::createSpan);
    // Verify Host header to avoid rebind attack.
    router.route().handler(checkAllowlistHostHeader());
    router.route().handler(CorsHandler.create(buildCorsRegexFromConfig()).allowedHeader("*").allowedHeader("content-type"));
    router.route().handler(BodyHandler.create().setUploadsDirectory(dataDir.resolve("uploads").toString()).setDeleteUploadedFilesOnEnd(true));
    router.route("/").method(HttpMethod.GET).handler(this::handleEmptyRequest);
    router.route(HealthService.LIVENESS_PATH).method(HttpMethod.GET).handler(livenessService::handleRequest);
    router.route(HealthService.READINESS_PATH).method(HttpMethod.GET).handler(readinessService::handleRequest);
    Route mainRoute = router.route("/").method(HttpMethod.POST).produces(APPLICATION_JSON);
    if (authenticationService.isPresent()) {
        mainRoute.handler(HandlerFactory.authentication(authenticationService.get(), config.getNoAuthRpcApis()));
    }
    mainRoute.handler(HandlerFactory.jsonRpcParser()).handler(HandlerFactory.timeout(new TimeoutOptions(config.getHttpTimeoutSec()), rpcMethods));
    if (authenticationService.isPresent()) {
        mainRoute.blockingHandler(HandlerFactory.jsonRpcExecutor(new JsonRpcExecutor(new AuthenticatedJsonRpcProcessor(new TimedJsonRpcProcessor(new TracedJsonRpcProcessor(new BaseJsonRpcProcessor()), requestTimer), authenticationService.get(), config.getNoAuthRpcApis()), rpcMethods), tracer));
    } else {
        mainRoute.blockingHandler(HandlerFactory.jsonRpcExecutor(new JsonRpcExecutor(new TimedJsonRpcProcessor(new TracedJsonRpcProcessor(new BaseJsonRpcProcessor()), requestTimer), rpcMethods), tracer));
    }
    if (authenticationService.isPresent()) {
        router.route("/login").method(HttpMethod.POST).produces(APPLICATION_JSON).handler(authenticationService.get()::handleLogin);
    } else {
        router.route("/login").method(HttpMethod.POST).produces(APPLICATION_JSON).handler(DefaultAuthenticationService::handleDisabledLogin);
    }
    return router;
}
Also used : BaseJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.BaseJsonRpcProcessor) TracedJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.TracedJsonRpcProcessor) Router(io.vertx.ext.web.Router) TimedJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.TimedJsonRpcProcessor) TimeoutOptions(org.hyperledger.besu.ethereum.api.handlers.TimeoutOptions) JsonRpcExecutor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor) AuthenticatedJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.AuthenticatedJsonRpcProcessor) DefaultAuthenticationService(org.hyperledger.besu.ethereum.api.jsonrpc.authentication.DefaultAuthenticationService) Route(io.vertx.ext.web.Route)

Example 2 with TimeoutOptions

use of org.hyperledger.besu.ethereum.api.handlers.TimeoutOptions in project besu by hyperledger.

the class TimeoutHandlerTest method test.

@Test
public void test() {
    final Map<String, TimeoutOptions> options;
    if (timerMustBeSet) {
        options = ImmutableMap.of(method.getMethodName(), new TimeoutOptions(timeoutSec, DEFAULT_OPTS.getErrorCode()));
    } else {
        options = Collections.emptyMap();
    }
    final Handler<RoutingContext> handler = TimeoutHandler.handler(globalOptions, options);
    final RoutingContext ctx = Mockito.spy(RoutingContext.class);
    final Vertx vertx = Mockito.spy(Vertx.class);
    final JsonObject requestBody = Mockito.mock(JsonObject.class);
    when(requestBody.getString("method")).thenReturn(method.getMethodName());
    when(ctx.data()).thenReturn(Map.of(ContextKey.REQUEST_BODY_AS_JSON_OBJECT.name(), requestBody));
    when(ctx.get(ContextKey.REQUEST_BODY_AS_JSON_OBJECT.name())).thenReturn(requestBody);
    when(ctx.vertx()).thenReturn(vertx);
    handler.handle(ctx);
    verify(vertx, times(timerMustBeSet ? 1 : 0)).setTimer(eq(TimeUnit.SECONDS.toMillis(timeoutSec)), any());
    verify(ctx, times(timerMustBeSet ? 1 : 0)).addBodyEndHandler(any());
}
Also used : RoutingContext(io.vertx.ext.web.RoutingContext) JsonObject(io.vertx.core.json.JsonObject) TimeoutOptions(org.hyperledger.besu.ethereum.api.handlers.TimeoutOptions) Vertx(io.vertx.core.Vertx) Test(org.junit.Test)

Example 3 with TimeoutOptions

use of org.hyperledger.besu.ethereum.api.handlers.TimeoutOptions in project besu by hyperledger.

the class JsonRpcService method buildRouter.

private Router buildRouter() {
    // Handle json rpc requests
    final Router router = Router.router(vertx);
    router.route().handler(this::createSpan);
    // Verify Host header to avoid rebind attack.
    router.route().handler(denyRouteToBlockedHost());
    router.route().handler(CorsHandler.create(buildCorsRegexFromConfig()).allowedHeader("*").allowedHeader("content-type"));
    router.route().handler(BodyHandler.create().setUploadsDirectory(dataDir.resolve("uploads").toString()).setDeleteUploadedFilesOnEnd(true));
    router.route("/").method(HttpMethod.GET).handler(this::handleEmptyRequest);
    router.route(HealthService.LIVENESS_PATH).method(HttpMethod.GET).handler(livenessService::handleRequest);
    router.route(HealthService.READINESS_PATH).method(HttpMethod.GET).handler(readinessService::handleRequest);
    Route mainRoute = router.route("/").method(HttpMethod.POST).produces(APPLICATION_JSON);
    if (authenticationService.isPresent()) {
        mainRoute.handler(HandlerFactory.authentication(authenticationService.get(), config.getNoAuthRpcApis()));
    }
    mainRoute.handler(HandlerFactory.jsonRpcParser()).handler(HandlerFactory.timeout(new TimeoutOptions(config.getHttpTimeoutSec()), rpcMethods));
    if (authenticationService.isPresent()) {
        mainRoute.blockingHandler(HandlerFactory.jsonRpcExecutor(new JsonRpcExecutor(new AuthenticatedJsonRpcProcessor(new TimedJsonRpcProcessor(new TracedJsonRpcProcessor(new BaseJsonRpcProcessor()), requestTimer), authenticationService.get(), config.getNoAuthRpcApis()), rpcMethods), tracer));
    } else {
        mainRoute.blockingHandler(HandlerFactory.jsonRpcExecutor(new JsonRpcExecutor(new TimedJsonRpcProcessor(new TracedJsonRpcProcessor(new BaseJsonRpcProcessor()), requestTimer), rpcMethods), tracer));
    }
    if (authenticationService.isPresent()) {
        router.route("/login").method(HttpMethod.POST).produces(APPLICATION_JSON).handler(authenticationService.get()::handleLogin);
    } else {
        router.route("/login").method(HttpMethod.POST).produces(APPLICATION_JSON).handler(DefaultAuthenticationService::handleDisabledLogin);
    }
    return router;
}
Also used : BaseJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.BaseJsonRpcProcessor) TracedJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.TracedJsonRpcProcessor) Router(io.vertx.ext.web.Router) TimedJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.TimedJsonRpcProcessor) TimeoutOptions(org.hyperledger.besu.ethereum.api.handlers.TimeoutOptions) JsonRpcExecutor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor) AuthenticatedJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.AuthenticatedJsonRpcProcessor) DefaultAuthenticationService(org.hyperledger.besu.ethereum.api.jsonrpc.authentication.DefaultAuthenticationService) Route(io.vertx.ext.web.Route)

Aggregations

TimeoutOptions (org.hyperledger.besu.ethereum.api.handlers.TimeoutOptions)3 Route (io.vertx.ext.web.Route)2 Router (io.vertx.ext.web.Router)2 DefaultAuthenticationService (org.hyperledger.besu.ethereum.api.jsonrpc.authentication.DefaultAuthenticationService)2 AuthenticatedJsonRpcProcessor (org.hyperledger.besu.ethereum.api.jsonrpc.execution.AuthenticatedJsonRpcProcessor)2 BaseJsonRpcProcessor (org.hyperledger.besu.ethereum.api.jsonrpc.execution.BaseJsonRpcProcessor)2 JsonRpcExecutor (org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor)2 TimedJsonRpcProcessor (org.hyperledger.besu.ethereum.api.jsonrpc.execution.TimedJsonRpcProcessor)2 TracedJsonRpcProcessor (org.hyperledger.besu.ethereum.api.jsonrpc.execution.TracedJsonRpcProcessor)2 Vertx (io.vertx.core.Vertx)1 JsonObject (io.vertx.core.json.JsonObject)1 RoutingContext (io.vertx.ext.web.RoutingContext)1 Test (org.junit.Test)1