Search in sources :

Example 1 with AuthenticatedJsonRpcProcessor

use of org.hyperledger.besu.ethereum.api.jsonrpc.execution.AuthenticatedJsonRpcProcessor 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 AuthenticatedJsonRpcProcessor

use of org.hyperledger.besu.ethereum.api.jsonrpc.execution.AuthenticatedJsonRpcProcessor in project besu by hyperledger.

the class WebSocketServiceLoginTest method before.

@Before
public void before() throws URISyntaxException {
    vertx = Vertx.vertx();
    final String authTomlPath = Paths.get(ClassLoader.getSystemResource("JsonRpcHttpService/auth.toml").toURI()).toAbsolutePath().toString();
    websocketConfiguration = WebSocketConfiguration.createDefault();
    websocketConfiguration.setPort(0);
    websocketConfiguration.setAuthenticationEnabled(true);
    websocketConfiguration.setAuthenticationCredentialsFile(authTomlPath);
    websocketConfiguration.setHostsAllowlist(Collections.singletonList("*"));
    websocketConfiguration.setRpcApisNoAuth(new ArrayList<>(NO_AUTH_METHODS));
    peerDiscoveryMock = mock(P2PNetwork.class);
    blockchainQueries = mock(BlockchainQueries.class);
    synchronizer = mock(Synchronizer.class);
    final Set<Capability> supportedCapabilities = new HashSet<>();
    supportedCapabilities.add(EthProtocol.ETH62);
    supportedCapabilities.add(EthProtocol.ETH63);
    final StubGenesisConfigOptions genesisConfigOptions = new StubGenesisConfigOptions().constantinopleBlock(0).chainId(CHAIN_ID);
    final Map<String, JsonRpcMethod> websocketMethods = new WebSocketMethodsFactory(new SubscriptionManager(new NoOpMetricsSystem()), new HashMap<>()).methods();
    rpcMethods = spy(new JsonRpcMethodsFactory().methods(CLIENT_VERSION, CHAIN_ID, genesisConfigOptions, peerDiscoveryMock, blockchainQueries, synchronizer, MainnetProtocolSchedule.fromConfig(genesisConfigOptions), mock(ProtocolContext.class), mock(FilterManager.class), mock(TransactionPool.class), mock(PoWMiningCoordinator.class), new NoOpMetricsSystem(), supportedCapabilities, Optional.empty(), Optional.empty(), JSON_RPC_APIS, mock(PrivacyParameters.class), mock(JsonRpcConfiguration.class), mock(WebSocketConfiguration.class), mock(MetricsConfiguration.class), natService, new HashMap<>(), folder.getRoot().toPath(), mock(EthPeers.class)));
    websocketMethods.putAll(rpcMethods);
    webSocketMessageHandlerSpy = spy(new WebSocketMessageHandler(vertx, new JsonRpcExecutor(new AuthenticatedJsonRpcProcessor(new BaseJsonRpcProcessor(), DefaultAuthenticationService.create(vertx, websocketConfiguration).get(), websocketConfiguration.getRpcApisNoAuth()), websocketMethods), mock(EthScheduler.class), TimeoutOptions.defaultOptions().getTimeoutSeconds()));
    websocketService = new WebSocketService(vertx, websocketConfiguration, webSocketMessageHandlerSpy, new NoOpMetricsSystem());
    websocketService.start().join();
    jwtAuth = websocketService.authenticationService.get().getJwtAuthProvider();
    websocketConfiguration.setPort(websocketService.socketAddress().getPort());
    final HttpClientOptions httpClientOptions = new HttpClientOptions().setDefaultHost(websocketConfiguration.getHost()).setDefaultPort(websocketConfiguration.getPort());
    httpClient = vertx.createHttpClient(httpClientOptions);
}
Also used : HashMap(java.util.HashMap) EthPeers(org.hyperledger.besu.ethereum.eth.manager.EthPeers) WebSocketMethodsFactory(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketMethodsFactory) SubscriptionManager(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.SubscriptionManager) HttpClientOptions(io.vertx.core.http.HttpClientOptions) FilterManager(org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager) BaseJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.BaseJsonRpcProcessor) MetricsConfiguration(org.hyperledger.besu.metrics.prometheus.MetricsConfiguration) BlockchainQueries(org.hyperledger.besu.ethereum.api.query.BlockchainQueries) ProtocolContext(org.hyperledger.besu.ethereum.ProtocolContext) JsonRpcExecutor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor) AuthenticatedJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.AuthenticatedJsonRpcProcessor) HashSet(java.util.HashSet) Synchronizer(org.hyperledger.besu.ethereum.core.Synchronizer) TransactionPool(org.hyperledger.besu.ethereum.eth.transactions.TransactionPool) Capability(org.hyperledger.besu.ethereum.p2p.rlpx.wire.Capability) JsonRpcConfiguration(org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration) P2PNetwork(org.hyperledger.besu.ethereum.p2p.network.P2PNetwork) PrivacyParameters(org.hyperledger.besu.ethereum.core.PrivacyParameters) JsonRpcMethodsFactory(org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory) EthScheduler(org.hyperledger.besu.ethereum.eth.manager.EthScheduler) JsonRpcMethod(org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) StubGenesisConfigOptions(org.hyperledger.besu.config.StubGenesisConfigOptions) PoWMiningCoordinator(org.hyperledger.besu.ethereum.blockcreation.PoWMiningCoordinator) Before(org.junit.Before)

Example 3 with AuthenticatedJsonRpcProcessor

use of org.hyperledger.besu.ethereum.api.jsonrpc.execution.AuthenticatedJsonRpcProcessor in project besu by hyperledger.

the class RunnerBuilder method createWebsocketService.

private WebSocketService createWebsocketService(final Vertx vertx, final WebSocketConfiguration configuration, final SubscriptionManager subscriptionManager, final Map<String, JsonRpcMethod> jsonRpcMethods, final PrivacyParameters privacyParameters, final ProtocolSchedule protocolSchedule, final BlockchainQueries blockchainQueries, final Optional<AuthenticationService> authenticationService, final ObservableMetricsSystem metricsSystem) {
    final WebSocketMethodsFactory websocketMethodsFactory = new WebSocketMethodsFactory(subscriptionManager, jsonRpcMethods);
    if (privacyParameters.isEnabled()) {
        final PrivateWebSocketMethodsFactory privateWebSocketMethodsFactory = new PrivateWebSocketMethodsFactory(privacyParameters, subscriptionManager, protocolSchedule, blockchainQueries);
        privateWebSocketMethodsFactory.methods().forEach(websocketMethodsFactory::addMethods);
    }
    rpcEndpointServiceImpl.getPluginMethods(configuration.getRpcApis()).values().forEach(websocketMethodsFactory::addMethods);
    final JsonRpcProcessor jsonRpcProcessor;
    if (authenticationService.isPresent()) {
        jsonRpcProcessor = new AuthenticatedJsonRpcProcessor(new BaseJsonRpcProcessor(), authenticationService.get(), configuration.getRpcApisNoAuth());
    } else {
        jsonRpcProcessor = new BaseJsonRpcProcessor();
    }
    final JsonRpcExecutor jsonRpcExecutor = new JsonRpcExecutor(jsonRpcProcessor, websocketMethodsFactory.methods());
    final WebSocketMessageHandler websocketMessageHandler = new WebSocketMessageHandler(vertx, jsonRpcExecutor, besuController.getProtocolManager().ethContext().getScheduler(), webSocketConfiguration.getTimeoutSec());
    return new WebSocketService(vertx, configuration, websocketMessageHandler, authenticationService, metricsSystem);
}
Also used : BaseJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.BaseJsonRpcProcessor) WebSocketMessageHandler(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketMessageHandler) JsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcProcessor) BaseJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.BaseJsonRpcProcessor) AuthenticatedJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.AuthenticatedJsonRpcProcessor) WebSocketService(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.WebSocketService) WebSocketMethodsFactory(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketMethodsFactory) PrivateWebSocketMethodsFactory(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.PrivateWebSocketMethodsFactory) PrivateWebSocketMethodsFactory(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.PrivateWebSocketMethodsFactory) AuthenticatedJsonRpcProcessor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.AuthenticatedJsonRpcProcessor) JsonRpcExecutor(org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor)

Example 4 with AuthenticatedJsonRpcProcessor

use of org.hyperledger.besu.ethereum.api.jsonrpc.execution.AuthenticatedJsonRpcProcessor 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

AuthenticatedJsonRpcProcessor (org.hyperledger.besu.ethereum.api.jsonrpc.execution.AuthenticatedJsonRpcProcessor)4 BaseJsonRpcProcessor (org.hyperledger.besu.ethereum.api.jsonrpc.execution.BaseJsonRpcProcessor)4 JsonRpcExecutor (org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcExecutor)4 Route (io.vertx.ext.web.Route)2 Router (io.vertx.ext.web.Router)2 TimeoutOptions (org.hyperledger.besu.ethereum.api.handlers.TimeoutOptions)2 DefaultAuthenticationService (org.hyperledger.besu.ethereum.api.jsonrpc.authentication.DefaultAuthenticationService)2 TimedJsonRpcProcessor (org.hyperledger.besu.ethereum.api.jsonrpc.execution.TimedJsonRpcProcessor)2 TracedJsonRpcProcessor (org.hyperledger.besu.ethereum.api.jsonrpc.execution.TracedJsonRpcProcessor)2 WebSocketMethodsFactory (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.methods.WebSocketMethodsFactory)2 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 StubGenesisConfigOptions (org.hyperledger.besu.config.StubGenesisConfigOptions)1 ProtocolContext (org.hyperledger.besu.ethereum.ProtocolContext)1 JsonRpcConfiguration (org.hyperledger.besu.ethereum.api.jsonrpc.JsonRpcConfiguration)1 JsonRpcProcessor (org.hyperledger.besu.ethereum.api.jsonrpc.execution.JsonRpcProcessor)1 FilterManager (org.hyperledger.besu.ethereum.api.jsonrpc.internal.filter.FilterManager)1 JsonRpcMethod (org.hyperledger.besu.ethereum.api.jsonrpc.internal.methods.JsonRpcMethod)1 JsonRpcMethodsFactory (org.hyperledger.besu.ethereum.api.jsonrpc.methods.JsonRpcMethodsFactory)1