Search in sources :

Example 1 with StringService

use of io.helidon.grpc.examples.common.StringService 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 StringService

use of io.helidon.grpc.examples.common.StringService 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 StringService

use of io.helidon.grpc.examples.common.StringService 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 4 with StringService

use of io.helidon.grpc.examples.common.StringService 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 5 with StringService

use of io.helidon.grpc.examples.common.StringService in project helidon by oracle.

the class Server method main.

/**
 * The main program entry point.
 *
 * @param args  the program arguments
 *
 * @throws Exception  if an error occurs
 */
public static void main(String[] args) throws Exception {
    // 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();
    GrpcRouting grpcRouting = GrpcRouting.builder().intercept(// global metrics - all service methods counted
    GrpcMetrics.counted()).register(// GreetService uses global metrics so all methods are counted
    new GreetService(config)).register(new StringService(), rules -> {
        // service level metrics - StringService overrides global so that its methods are timed
        rules.intercept(GrpcMetrics.timed()).intercept("Upper", GrpcMetrics.histogram());
    }).build();
    GrpcServer grpcServer = GrpcServer.create(serverConfig, grpcRouting);
    // 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;
    });
    // start web server with the metrics endpoints
    Routing routing = Routing.builder().register(MetricsSupport.create()).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) Config(io.helidon.config.Config) WebServer(io.helidon.webserver.WebServer) GrpcServer(io.helidon.grpc.server.GrpcServer) GreetService(io.helidon.grpc.examples.common.GreetService) MetricsSupport(io.helidon.metrics.MetricsSupport) LogConfig(io.helidon.common.LogConfig) Routing(io.helidon.webserver.Routing) GrpcMetrics(io.helidon.grpc.metrics.GrpcMetrics) GrpcRouting(io.helidon.grpc.server.GrpcRouting) GrpcServerConfiguration(io.helidon.grpc.server.GrpcServerConfiguration) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) GreetService(io.helidon.grpc.examples.common.GreetService) Routing(io.helidon.webserver.Routing) GrpcRouting(io.helidon.grpc.server.GrpcRouting) StringService(io.helidon.grpc.examples.common.StringService) GrpcServer(io.helidon.grpc.server.GrpcServer) GrpcRouting(io.helidon.grpc.server.GrpcRouting)

Aggregations

StringService (io.helidon.grpc.examples.common.StringService)6 LogConfig (io.helidon.common.LogConfig)5 GrpcRouting (io.helidon.grpc.server.GrpcRouting)5 GrpcServer (io.helidon.grpc.server.GrpcServer)5 GrpcServerConfiguration (io.helidon.grpc.server.GrpcServerConfiguration)5 Config (io.helidon.config.Config)4 Security (io.helidon.security.Security)4 GrpcSecurity (io.helidon.security.integration.grpc.GrpcSecurity)4 GreetService (io.helidon.grpc.examples.common.GreetService)3 ServiceDescriptor (io.helidon.grpc.server.ServiceDescriptor)3 HttpBasicAuthProvider (io.helidon.security.providers.httpauth.HttpBasicAuthProvider)2 Routing (io.helidon.webserver.Routing)2 WebServer (io.helidon.webserver.WebServer)2 Channel (io.grpc.Channel)1 Status (io.grpc.Status)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1 InProcessChannelBuilder (io.grpc.inprocess.InProcessChannelBuilder)1 StreamObserver (io.grpc.stub.StreamObserver)1 Context (io.helidon.common.context.Context)1 Http (io.helidon.common.http.Http)1