Search in sources :

Example 1 with OpenApiSpecsExtractor

use of tech.pegasys.web3signer.core.util.OpenApiSpecsExtractor in project web3signer by ConsenSys.

the class SwaggerUIRouteTest method contentsAreLoaded.

@Test
void contentsAreLoaded(Vertx vertx) throws IOException {
    final Router router = Router.router(vertx);
    final SwaggerUIRoute swaggerUIRoute = new SwaggerUIRoute(router);
    final OpenApiSpecsExtractor openApiSpecsExtractor = new OpenApiSpecsExtractor.OpenApiSpecsExtractorBuilder().withConvertRelativeRefToAbsoluteRef(false).withForceDeleteOnJvmExit(true).build();
    final Map<Path, String> swaggerUIWebRoot = swaggerUIRoute.loadSwaggerUIStaticContent(openApiSpecsExtractor);
    assertThat(swaggerUIWebRoot).containsKey(Path.of("/swagger-ui"));
    assertThat(swaggerUIWebRoot).containsKey(Path.of("/swagger-ui/"));
    assertThat(swaggerUIWebRoot).containsKey(Path.of("/swagger-ui/eth2/web3signer.yaml"));
    assertThat(swaggerUIWebRoot).containsKey(Path.of("/swagger-ui/filecoin/web3signer.yaml"));
    assertThat(swaggerUIWebRoot).containsKey(Path.of("/swagger-ui/eth1/web3signer.yaml"));
}
Also used : Path(java.nio.file.Path) OpenApiSpecsExtractor(tech.pegasys.web3signer.core.util.OpenApiSpecsExtractor) Router(io.vertx.ext.web.Router) Test(org.junit.jupiter.api.Test)

Example 2 with OpenApiSpecsExtractor

use of tech.pegasys.web3signer.core.util.OpenApiSpecsExtractor in project web3signer by ConsenSys.

the class Runner method run.

@Override
public void run() {
    if (config.getLogLevel() != null) {
        System.out.println("Setting logging level to " + config.getLogLevel().name());
        Configurator.setRootLevel(config.getLogLevel());
    }
    final MetricsEndpoint metricsEndpoint = new MetricsEndpoint(config.isMetricsEnabled(), config.getMetricsPort(), config.getMetricsNetworkInterface(), config.getMetricCategories(), config.getMetricsHostAllowList());
    final MetricsSystem metricsSystem = metricsEndpoint.getMetricsSystem();
    final Vertx vertx = Vertx.vertx(createVertxOptions(metricsSystem));
    final LogErrorHandler errorHandler = new LogErrorHandler();
    final ArtifactSignerProvider artifactSignerProvider = createArtifactSignerProvider(vertx, metricsSystem);
    try {
        metricsEndpoint.start(vertx);
        try {
            // wait for signers to get loaded ...
            artifactSignerProvider.load().get();
        } catch (final InterruptedException | ExecutionException e) {
            LOG.error("Error loading signers", e);
        }
        incSignerLoadCount(metricsSystem, artifactSignerProvider.availableIdentifiers().size());
        final OpenApiSpecsExtractor openApiSpecsExtractor = new OpenApiSpecsExtractor.OpenApiSpecsExtractorBuilder().withConvertRelativeRefToAbsoluteRef(true).withForceDeleteOnJvmExit(true).build();
        final Path openApiSpec = openApiSpecsExtractor.getSpecFilePathAtDestination(getOpenApiSpecResource()).orElseThrow(() -> new RuntimeException("Unable to load OpenApi spec " + getOpenApiSpecResource()));
        // vertx needs a scheme present (file://) to determine this is an absolute path
        final URI openApiSpecUri = openApiSpec.toUri();
        final RouterBuilder routerBuilder = getRouterBuilder(vertx, openApiSpecUri.toString());
        // register access log handler first
        if (config.isAccessLogsEnabled()) {
            routerBuilder.rootHandler(LoggerHandler.create(LoggerFormat.DEFAULT));
        }
        routerBuilder.rootHandler(CorsHandler.create(buildCorsRegexFromConfig()).allowedHeader("*").allowedMethod(HttpMethod.GET).allowedMethod(HttpMethod.POST).allowedMethod(HttpMethod.DELETE).allowedMethod(HttpMethod.OPTIONS));
        registerUpcheckRoute(routerBuilder, errorHandler);
        registerHttpHostAllowListHandler(routerBuilder);
        final Context context = new Context(routerBuilder, metricsSystem, errorHandler, vertx, artifactSignerProvider);
        final Router router = populateRouter(context);
        if (config.isSwaggerUIEnabled()) {
            new SwaggerUIRoute(router).register();
        }
        final HttpServer httpServer = createServerAndWait(vertx, router);
        final String tlsStatus = config.getTlsOptions().isPresent() ? "enabled" : "disabled";
        LOG.info("Web3Signer has started with TLS {}, and ready to handle signing requests on {}:{}", tlsStatus, config.getHttpListenHost(), httpServer.actualPort());
        persistPortInformation(httpServer.actualPort(), metricsEndpoint.getPort());
    } catch (final InitializationException e) {
        throw e;
    } catch (final Throwable e) {
        if (artifactSignerProvider != null) {
            artifactSignerProvider.close();
        }
        vertx.close();
        metricsEndpoint.stop();
        LOG.error("Failed to initialise application", e);
    }
}
Also used : Path(java.nio.file.Path) OpenApiSpecsExtractor(tech.pegasys.web3signer.core.util.OpenApiSpecsExtractor) Router(io.vertx.ext.web.Router) Vertx(io.vertx.core.Vertx) URI(java.net.URI) LogErrorHandler(tech.pegasys.web3signer.core.service.http.handlers.LogErrorHandler) MetricsEndpoint(tech.pegasys.web3signer.core.metrics.MetricsEndpoint) HttpServer(io.vertx.core.http.HttpServer) MetricsSystem(org.hyperledger.besu.plugin.services.MetricsSystem) SwaggerUIRoute(tech.pegasys.web3signer.core.service.http.SwaggerUIRoute) ExecutionException(java.util.concurrent.ExecutionException) RouterBuilder(io.vertx.ext.web.openapi.RouterBuilder) ArtifactSignerProvider(tech.pegasys.web3signer.signing.ArtifactSignerProvider)

Example 3 with OpenApiSpecsExtractor

use of tech.pegasys.web3signer.core.util.OpenApiSpecsExtractor in project web3signer by ConsenSys.

the class SwaggerUIRoute method register.

public void register() throws IOException {
    LOG.info("Registering /swagger-ui routes ...");
    final OpenApiSpecsExtractor openApiSpecsExtractor = new OpenApiSpecsExtractor.OpenApiSpecsExtractorBuilder().withConvertRelativeRefToAbsoluteRef(false).withForceDeleteOnJvmExit(true).build();
    final Map<Path, String> swaggerUIWebRoot;
    swaggerUIWebRoot = loadSwaggerUIStaticContent(openApiSpecsExtractor);
    LOG.debug("/swagger-ui paths: {}", swaggerUIWebRoot.keySet());
    // serve /swagger-ui/* from static content map
    router.route(HttpMethod.GET, SWAGGER_ENDPOINT + "/*").handler(ctx -> swaggerUIHandler(swaggerUIWebRoot, ctx));
    // Vertx 3.x doesn't handle paths without trailing / (such as /swagger-ui) , so handle it
    // directly. The following code may be removed once upgrade to Vertx 4.x
    router.route(HttpMethod.GET, SWAGGER_ENDPOINT).handler(ctx -> swaggerUIHandler(swaggerUIWebRoot, ctx));
}
Also used : Path(java.nio.file.Path) OpenApiSpecsExtractor(tech.pegasys.web3signer.core.util.OpenApiSpecsExtractor)

Aggregations

Path (java.nio.file.Path)3 OpenApiSpecsExtractor (tech.pegasys.web3signer.core.util.OpenApiSpecsExtractor)3 Router (io.vertx.ext.web.Router)2 Vertx (io.vertx.core.Vertx)1 HttpServer (io.vertx.core.http.HttpServer)1 RouterBuilder (io.vertx.ext.web.openapi.RouterBuilder)1 URI (java.net.URI)1 ExecutionException (java.util.concurrent.ExecutionException)1 MetricsSystem (org.hyperledger.besu.plugin.services.MetricsSystem)1 Test (org.junit.jupiter.api.Test)1 MetricsEndpoint (tech.pegasys.web3signer.core.metrics.MetricsEndpoint)1 SwaggerUIRoute (tech.pegasys.web3signer.core.service.http.SwaggerUIRoute)1 LogErrorHandler (tech.pegasys.web3signer.core.service.http.handlers.LogErrorHandler)1 ArtifactSignerProvider (tech.pegasys.web3signer.signing.ArtifactSignerProvider)1