Search in sources :

Example 6 with GrpcRouting

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

the class ServiceAndMethodLevelSecurityIT method startServer.

@BeforeAll
public static void startServer() throws Exception {
    LogConfig.configureRuntime();
    Config config = Config.create();
    Security security = Security.builder().addProvider(HttpBasicAuthProvider.create(config.get("http-basic-auth"))).build();
    ServiceDescriptor echoService = ServiceDescriptor.builder(new EchoService()).intercept(GrpcSecurity.rolesAllowed("admin")).build();
    ServiceDescriptor stringService = ServiceDescriptor.builder(new StringService()).intercept("Upper", GrpcSecurity.rolesAllowed("admin")).intercept("Split", GrpcSecurity.rolesAllowed("admin")).build();
    // Add the EchoService
    GrpcRouting routing = GrpcRouting.builder().intercept(GrpcSecurity.create(security).securityDefaults(GrpcSecurity.authenticate())).register(echoService).register(stringService).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);
    Channel channel = InProcessChannelBuilder.forName(grpcServer.configuration().name()).build();
    adminEchoStub = EchoServiceGrpc.newBlockingStub(channel).withCallCredentials(adminCreds);
    userEchoStub = EchoServiceGrpc.newBlockingStub(channel).withCallCredentials(userCreds);
    adminStringStub = StringServiceGrpc.newBlockingStub(channel).withCallCredentials(adminCreds);
    userStringStub = StringServiceGrpc.newBlockingStub(channel).withCallCredentials(userCreds);
    noCredsEchoStub = StringServiceGrpc.newBlockingStub(channel);
}
Also used : GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) LogConfig(io.helidon.common.LogConfig) Config(io.helidon.config.Config) EchoService(services.EchoService) Channel(io.grpc.Channel) StringService(services.StringService) Security(io.helidon.security.Security) GrpcRouting(io.helidon.grpc.server.GrpcRouting) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 7 with GrpcRouting

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

the class AnnotatedServiceTest method startServer.

@BeforeAll
public static void startServer() throws Exception {
    LogConfig.configureRuntime();
    GrpcRouting routing = GrpcRouting.builder().register(descriptor(UnaryService.class)).register(descriptor(ServerStreamingService.class)).register(descriptor(ClientStreamingService.class)).register(descriptor(BidiService.class)).build();
    GrpcServerConfiguration serverConfig = GrpcServerConfiguration.builder().port(0).build();
    grpcServer = GrpcServer.create(serverConfig, routing).start().toCompletableFuture().get(10, TimeUnit.SECONDS);
    channel = InProcessChannelBuilder.forName(grpcServer.configuration().name()).build();
}
Also used : GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) GrpcRouting(io.helidon.grpc.server.GrpcRouting) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 8 with GrpcRouting

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

the class SecureServer method createGrpcServer.

/**
 * Create the gRPC server.
 */
private static GrpcServer createGrpcServer(Config config, Security security) {
    GrpcRouting grpcRouting = GrpcRouting.builder().intercept(GrpcSecurity.create(security).securityDefaults(GrpcSecurity.authenticate())).register(new StringService(), GrpcSecurity.rolesAllowed("admin")).register(new GreetService()).build();
    GrpcServer grpcServer = GrpcServer.create(GrpcServerConfiguration.create(config), 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("gRPC server startup failed: " + t.getMessage());
        t.printStackTrace(System.err);
        return null;
    });
    return grpcServer;
}
Also used : Security(io.helidon.security.Security) WebClient(io.helidon.webclient.WebClient) WebClientResponse(io.helidon.webclient.WebClientResponse) Context(io.helidon.common.context.Context) ResponseHelper.complete(io.helidon.grpc.core.ResponseHelper.complete) Channel(io.grpc.Channel) StringService(io.helidon.grpc.examples.common.StringService) StreamObserver(io.grpc.stub.StreamObserver) GrpcHelper(io.helidon.grpc.core.GrpcHelper) ServerResponse(io.helidon.webserver.ServerResponse) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) Service(io.helidon.webserver.Service) Status(io.grpc.Status) LogConfig(io.helidon.common.LogConfig) Http(io.helidon.common.http.Http) InProcessChannelBuilder(io.grpc.inprocess.InProcessChannelBuilder) HttpBasicAuthProvider(io.helidon.security.providers.httpauth.HttpBasicAuthProvider) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Config(io.helidon.config.Config) GrpcClientSecurity(io.helidon.security.integration.grpc.GrpcClientSecurity) WebClientSecurity(io.helidon.webclient.security.WebClientSecurity) SecurityContext(io.helidon.security.SecurityContext) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) Greet(io.helidon.grpc.examples.common.Greet) ServerRequest(io.helidon.webserver.ServerRequest) StatusRuntimeException(io.grpc.StatusRuntimeException) Strings(io.helidon.grpc.examples.common.Strings) GrpcService(io.helidon.grpc.server.GrpcService) StringServiceGrpc(io.helidon.grpc.examples.common.StringServiceGrpc) WebServer(io.helidon.webserver.WebServer) Optional(java.util.Optional) GrpcServer(io.helidon.grpc.server.GrpcServer) Routing(io.helidon.webserver.Routing) StringService(io.helidon.grpc.examples.common.StringService) GrpcServer(io.helidon.grpc.server.GrpcServer) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Example 9 with GrpcRouting

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

the class SecureServer 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.builder().addProvider(HttpBasicAuthProvider.create(config.get("http-basic-auth"))).build();
    ServiceDescriptor greetService1 = ServiceDescriptor.builder(new GreetService(config)).name("GreetService").intercept(GrpcSecurity.rolesAllowed("user")).intercept("SetGreeting", GrpcSecurity.rolesAllowed("admin")).build();
    GrpcRouting grpcRouting = GrpcRouting.builder().intercept(GrpcSecurity.create(security).securityDefaults(GrpcSecurity.authenticate())).register(greetService1).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) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Config(io.helidon.config.Config) GrpcServer(io.helidon.grpc.server.GrpcServer) GreetService(io.helidon.grpc.examples.common.GreetService) LogConfig(io.helidon.common.LogConfig) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcSecurity(io.helidon.security.integration.grpc.GrpcSecurity) HttpBasicAuthProvider(io.helidon.security.providers.httpauth.HttpBasicAuthProvider) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) GreetService(io.helidon.grpc.examples.common.GreetService) 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 10 with GrpcRouting

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

the class GrpcChannelsProviderIT method startGrpcServer.

/**
 * Start a gRPC server listening on the specified port and with ssl enabled (if sslEnabled is true).
 *
 * @param nPort      The server port where the server will listen.
 * @param sslEnabled true if ssl enabled.
 * @param mutual     if true then 2 way (mutual) or just one way ssl.
 * @return A reference to a {@link io.helidon.grpc.server.GrpcServer}.
 */
private static GrpcServer startGrpcServer(int nPort, boolean sslEnabled, boolean mutual) throws Exception {
    Resource tlsCert = Resource.create(SERVER_CERT);
    Resource tlsKey = Resource.create(SERVER_KEY);
    Resource tlsCaCert = Resource.create(CA_CERT);
    GrpcTlsDescriptor sslConfig = null;
    String name = "grpc.server";
    if (!sslEnabled) {
        name = name + 1;
    } else if (mutual) {
        name = name + 2;
        sslConfig = GrpcTlsDescriptor.builder().jdkSSL(false).tlsCert(tlsCert).tlsKey(tlsKey).tlsCaCert(tlsCaCert).build();
    } else {
        name = name + 3;
        sslConfig = GrpcTlsDescriptor.builder().jdkSSL(false).tlsCert(tlsCert).tlsKey(tlsKey).build();
    }
    // Add the EchoService
    GrpcRouting routing = GrpcRouting.builder().register(new TreeMapService()).build();
    GrpcServerConfiguration.Builder bldr = GrpcServerConfiguration.builder().name(name).port(nPort);
    if (sslEnabled) {
        bldr.tlsConfig(sslConfig);
    }
    return GrpcServer.create(bldr.build(), routing).start().toCompletableFuture().get(10, TimeUnit.SECONDS);
}
Also used : GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) TreeMapService(services.TreeMapService) Resource(io.helidon.common.configurable.Resource) GrpcTlsDescriptor(io.helidon.grpc.core.GrpcTlsDescriptor) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Aggregations

GrpcRouting (io.helidon.grpc.server.GrpcRouting)13 GrpcServerConfiguration (io.helidon.grpc.server.GrpcServerConfiguration)13 LogConfig (io.helidon.common.LogConfig)9 Config (io.helidon.config.Config)7 BeforeAll (org.junit.jupiter.api.BeforeAll)7 Channel (io.grpc.Channel)6 GrpcServer (io.helidon.grpc.server.GrpcServer)6 Security (io.helidon.security.Security)6 StringService (io.helidon.grpc.examples.common.StringService)5 ServiceDescriptor (io.helidon.grpc.server.ServiceDescriptor)5 GrpcSecurity (io.helidon.security.integration.grpc.GrpcSecurity)4 Routing (io.helidon.webserver.Routing)4 StringService (services.StringService)4 WebServer (io.helidon.webserver.WebServer)3 EchoService (services.EchoService)3 TreeMapService (services.TreeMapService)3 GreetService (io.helidon.grpc.examples.common.GreetService)2 MetricsSupport (io.helidon.metrics.MetricsSupport)2 WebSecurity (io.helidon.security.integration.webserver.WebSecurity)2 HttpBasicAuthProvider (io.helidon.security.providers.httpauth.HttpBasicAuthProvider)2