Search in sources :

Example 1 with GrpcServer

use of io.helidon.grpc.server.GrpcServer in project helidon by oracle.

the class AbacServer method main.

/**
 * Main entry point.
 *
 * @param args  the program arguments
 */
public static void main(String[] args) {
    LogConfig.configureRuntime();
    Security security = Security.builder().addProvider(// add out custom provider
    AtnProvider.builder().build()).addProvider(// add the ABAC provider
    AbacProvider.builder().build()).build();
    // Create the time validator that will be used by the ABAC security provider
    TimeValidator.TimeConfig validTimes = TimeValidator.TimeConfig.builder().addBetween(LocalTime.of(8, 15), LocalTime.of(12, 0)).addBetween(LocalTime.of(12, 30), LocalTime.of(17, 30)).addDaysOfWeek(DayOfWeek.MONDAY, DayOfWeek.TUESDAY, DayOfWeek.WEDNESDAY, DayOfWeek.THURSDAY, DayOfWeek.FRIDAY).build();
    // Create the policy validator that will be used by the ABAC security provider
    PolicyValidator.PolicyConfig validPolicy = PolicyValidator.PolicyConfig.builder().statement("${env.time.year >= 2017}").build();
    // Create the scope validator that will be used by the ABAC security provider
    ScopeValidator.ScopesConfig validScopes = ScopeValidator.ScopesConfig.create("calendar_read", "calendar_edit");
    // Create the Atn config that will be used by out custom security provider
    AtnProvider.AtnConfig atnConfig = AtnProvider.AtnConfig.builder().addAuth(AtnProvider.Auth.builder("user").type(SubjectType.USER).roles("user_role").scopes("calendar_read", "calendar_edit").build()).addAuth(AtnProvider.Auth.builder("service").type(SubjectType.SERVICE).roles("service_role").scopes("calendar_read", "calendar_edit").build()).build();
    ServiceDescriptor stringService = ServiceDescriptor.builder(new StringService()).intercept("Upper", GrpcSecurity.secure().customObject(atnConfig).customObject(validScopes).customObject(validTimes).customObject(validPolicy)).build();
    GrpcRouting grpcRouting = GrpcRouting.builder().intercept(GrpcSecurity.create(security).securityDefaults(GrpcSecurity.secure())).register(stringService).build();
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.builder().build();
    GrpcServer grpcServer = GrpcServer.create(serverConfig, grpcRouting);
    grpcServer.start().thenAccept(s -> {
        System.out.println("gRPC server is UP! http://localhost:" + s.port());
        s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
}
Also used : GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Security(io.helidon.security.Security) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) SubjectType(io.helidon.security.SubjectType) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) ScopeValidator(io.helidon.security.abac.scope.ScopeValidator) TimeValidator(io.helidon.security.abac.time.TimeValidator) StringService(io.helidon.grpc.examples.common.StringService) PolicyValidator(io.helidon.security.abac.policy.PolicyValidator) DayOfWeek(java.time.DayOfWeek) LocalTime(java.time.LocalTime) GrpcServer(io.helidon.grpc.server.GrpcServer) AbacProvider(io.helidon.security.providers.abac.AbacProvider) LogConfig(io.helidon.common.LogConfig) TimeValidator(io.helidon.security.abac.time.TimeValidator) StringService(io.helidon.grpc.examples.common.StringService) GrpcServer(io.helidon.grpc.server.GrpcServer) Security(io.helidon.security.Security) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) ScopeValidator(io.helidon.security.abac.scope.ScopeValidator) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) PolicyValidator(io.helidon.security.abac.policy.PolicyValidator) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Example 2 with GrpcServer

use of io.helidon.grpc.server.GrpcServer in project helidon by oracle.

the class AbacServerFromConfig method main.

/**
 * Main entry point.
 *
 * @param args  the program arguments
 */
public static void main(String[] args) {
    LogConfig.configureRuntime();
    Config config = Config.create();
    Security security = Security.create(config.get("security"));
    GrpcRouting grpcRouting = GrpcRouting.builder().intercept(GrpcSecurity.create(security, config.get("security"))).register(new StringService()).build();
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.create(config.get("grpc"));
    GrpcServer grpcServer = GrpcServer.create(serverConfig, grpcRouting);
    grpcServer.start().thenAccept(s -> {
        System.out.println("gRPC server is UP! http://localhost:" + s.port());
        s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
}
Also used : StringService(io.helidon.grpc.examples.common.StringService) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Security(io.helidon.security.Security) Config(io.helidon.config.Config) GrpcServer(io.helidon.grpc.server.GrpcServer) LogConfig(io.helidon.common.LogConfig) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) StringService(io.helidon.grpc.examples.common.StringService) GrpcServer(io.helidon.grpc.server.GrpcServer) Security(io.helidon.security.Security) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Example 3 with GrpcServer

use of io.helidon.grpc.server.GrpcServer in project helidon by oracle.

the class Server method main.

/**
 * The main program entry point.
 *
 * @param args  the program arguments
 */
public static void main(String[] args) {
    // By default this will pick up application.yaml from the classpath
    Config config = Config.create();
    // load logging configuration
    LogConfig.configureRuntime();
    // Get gRPC server config from the "grpc" section of application.yaml
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.builder(config.get("grpc")).build();
    GrpcServer grpcServer = GrpcServer.create(serverConfig, createRouting(config));
    // Try to start the server. If successful, print some info and arrange to
    // print a message at shutdown. If unsuccessful, print the exception.
    grpcServer.start().thenAccept(s -> {
        System.out.println("gRPC server is UP! http://localhost:" + s.port());
        s.whenShutdown().thenRun(() -> System.out.println("gRPC server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
    // add support for standard and gRPC health checks
    HealthSupport health = HealthSupport.builder().addLiveness(HealthChecks.healthChecks()).addLiveness(grpcServer.healthChecks()).build();
    // start web server with health endpoint
    Routing routing = Routing.builder().register(health).build();
    WebServer.create(routing, config.get("webserver")).start().thenAccept(s -> {
        System.out.println("HTTP server is UP! http://localhost:" + s.port());
        s.whenShutdown().thenRun(() -> System.out.println("HTTP server is DOWN. Good bye!"));
    }).exceptionally(t -> {
        System.err.println("Startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
}
Also used : StringService(io.helidon.grpc.examples.common.StringService) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) GreetServiceJava(io.helidon.grpc.examples.common.GreetServiceJava) Config(io.helidon.config.Config) WebServer(io.helidon.webserver.WebServer) GrpcServer(io.helidon.grpc.server.GrpcServer) GreetService(io.helidon.grpc.examples.common.GreetService) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) GrpcRouting(io.helidon.grpc.server.GrpcRouting) HealthSupport(io.helidon.health.HealthSupport) HealthChecks(io.helidon.health.checks.HealthChecks) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) HealthSupport(io.helidon.health.HealthSupport) Routing(io.helidon.webserver.Routing) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcServer(io.helidon.grpc.server.GrpcServer)

Example 4 with GrpcServer

use of io.helidon.grpc.server.GrpcServer in project helidon by oracle.

the class MetricsIT method startGrpcServer.

// ----- helper methods -------------------------------------------------
/**
 * Start the gRPC Server listening on an ephemeral port.
 *
 * @throws Exception in case of an error
 */
private static void startGrpcServer() throws Exception {
    // Add the EchoService and enable GrpcMetrics
    GrpcRouting routing = GrpcRouting.builder().intercept(GrpcMetrics.timed()).register(new EchoService(), rules -> rules.intercept(GrpcMetrics.metered()).intercept("Echo", GrpcMetrics.counted())).build();
    // Run the server on port 0 so that it picks a free ephemeral port
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.builder().port(0).build();
    grpcServer = GrpcServer.create(serverConfig, routing).start().toCompletableFuture().get(10, TimeUnit.SECONDS);
    LOGGER.info("Started gRPC server at: localhost:" + grpcServer.port());
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) WebClient(io.helidon.webclient.WebClient) JsonValue(jakarta.json.JsonValue) Channel(io.grpc.Channel) MediaType(io.helidon.common.http.MediaType) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) AfterAll(org.junit.jupiter.api.AfterAll) JsonpSupport(io.helidon.media.jsonp.JsonpSupport) JsonStructure(jakarta.json.JsonStructure) BeforeAll(org.junit.jupiter.api.BeforeAll) MetricsSupport(io.helidon.metrics.MetricsSupport) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) LogConfig(io.helidon.common.LogConfig) EchoServiceGrpc(io.helidon.grpc.server.test.EchoServiceGrpc) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Echo(io.helidon.grpc.server.test.Echo) Logger(java.util.logging.Logger) GrpcRouting(io.helidon.grpc.server.GrpcRouting) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) ManagedChannelBuilder(io.grpc.ManagedChannelBuilder) WebServer(io.helidon.webserver.WebServer) GrpcServer(io.helidon.grpc.server.GrpcServer) Routing(io.helidon.webserver.Routing) EchoService(services.EchoService) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) EchoService(services.EchoService) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Example 5 with GrpcServer

use of io.helidon.grpc.server.GrpcServer in project helidon by oracle.

the class GrpcServerCdiExtension method loadExtensions.

/**
 * Load any instances of {@link GrpcMpExtension} discovered by the
 * {@link ServiceLoader} and allow them to further configure the
 * gRPC server.
 *
 * @param beanManager the {@link BeanManager}
 * @param config the Helidon configuration
 * @param routingBuilder the {@link GrpcRouting.Builder}
 * @param serverConfiguration the {@link GrpcServerConfiguration}
 */
private void loadExtensions(BeanManager beanManager, Config config, GrpcRouting.Builder routingBuilder, GrpcServerConfiguration.Builder serverConfiguration, CompletionStage<GrpcServer> whenStarted, CompletionStage<GrpcServer> whenShutdown) {
    GrpcMpContext context = new GrpcMpContext() {

        @Override
        public Config config() {
            return config;
        }

        @Override
        public GrpcServerConfiguration.Builder grpcServerConfiguration() {
            return serverConfiguration;
        }

        @Override
        public GrpcRouting.Builder routing() {
            return routingBuilder;
        }

        @Override
        public BeanManager beanManager() {
            return beanManager;
        }

        @Override
        public CompletionStage<GrpcServer> whenStarted() {
            return whenStarted;
        }

        @Override
        public CompletionStage<GrpcServer> whenShutdown() {
            return whenShutdown;
        }
    };
    HelidonServiceLoader.create(ServiceLoader.load(GrpcMpExtension.class)).forEach(ext -> ext.configure(context));
    beanManager.createInstance().select(GrpcMpExtension.class).stream().forEach(ext -> ext.configure(context));
}
Also used : GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) GrpcServer(io.helidon.grpc.server.GrpcServer) GrpcMpContext(io.helidon.microprofile.grpc.server.spi.GrpcMpContext) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Aggregations

GrpcServer (io.helidon.grpc.server.GrpcServer)12 GrpcRouting (io.helidon.grpc.server.GrpcRouting)10 GrpcServerConfiguration (io.helidon.grpc.server.GrpcServerConfiguration)10 LogConfig (io.helidon.common.LogConfig)8 Config (io.helidon.config.Config)7 StringService (io.helidon.grpc.examples.common.StringService)7 GreetService (io.helidon.grpc.examples.common.GreetService)4 ServiceDescriptor (io.helidon.grpc.server.ServiceDescriptor)4 Security (io.helidon.security.Security)4 GrpcSecurity (io.helidon.security.integration.grpc.GrpcSecurity)4 Routing (io.helidon.webserver.Routing)4 WebServer (io.helidon.webserver.WebServer)4 Channel (io.grpc.Channel)2 MetricsSupport (io.helidon.metrics.MetricsSupport)2 HttpBasicAuthProvider (io.helidon.security.providers.httpauth.HttpBasicAuthProvider)2 WebClient (io.helidon.webclient.WebClient)2 BeforeAll (org.junit.jupiter.api.BeforeAll)2 Test (org.junit.jupiter.api.Test)2 ManagedChannelBuilder (io.grpc.ManagedChannelBuilder)1 Status (io.grpc.Status)1