Search in sources :

Example 1 with GrpcRouting

use of io.helidon.grpc.server.GrpcRouting 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 GrpcRouting

use of io.helidon.grpc.server.GrpcRouting 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 GrpcRouting

use of io.helidon.grpc.server.GrpcRouting 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 4 with GrpcRouting

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

the class OutboundSecurityIT method startServers.

// ----- test lifecycle methods -----------------------------------------
@BeforeAll
public static void startServers() throws Exception {
    LogConfig.configureRuntime();
    Config config = Config.create();
    Security security = Security.builder().addProvider(HttpBasicAuthProvider.create(config.get("http-basic-auth"))).build();
    // secured web server's Routing
    Routing webRouting = Routing.builder().register(WebSecurity.create(security).securityDefaults(WebSecurity.authenticate())).get("/test", WebSecurity.rolesAllowed("admin"), OutboundSecurityIT::echoWebRequest).get("/propagate", WebSecurity.rolesAllowed("user"), OutboundSecurityIT::propagateCredentialsWebRequest).get("/override", WebSecurity.rolesAllowed("user"), OutboundSecurityIT::overrideCredentialsWebRequest).build();
    webServer = WebServer.create(webRouting).start().toCompletableFuture().get(10, TimeUnit.SECONDS);
    webServerURL = "http://127.0.0.1:" + webServer.port();
    client = ClientBuilder.newBuilder().build().register(HttpAuthenticationFeature.basicBuilder().build());
    ServiceDescriptor echoService = ServiceDescriptor.builder(new SecuredOutboundEchoService(webServerURL)).intercept(GrpcSecurity.rolesAllowed("admin")).build();
    // Add the EchoService
    GrpcRouting grpcRouting = GrpcRouting.builder().intercept(GrpcSecurity.create(security).securityDefaults(GrpcSecurity.authenticate())).register(echoService).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, grpcRouting).start().toCompletableFuture().get(10, TimeUnit.SECONDS);
    Channel channel = InProcessChannelBuilder.forName(grpcServer.configuration().name()).build();
    adminEchoStub = EchoServiceGrpc.newBlockingStub(channel).withCallCredentials(adminCreds);
    noCredsEchoStub = EchoServiceGrpc.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) Channel(io.grpc.Channel) GrpcRouting(io.helidon.grpc.server.GrpcRouting) Routing(io.helidon.webserver.Routing) Security(io.helidon.security.Security) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) SecuredOutboundEchoService(services.SecuredOutboundEchoService) GrpcRouting(io.helidon.grpc.server.GrpcRouting) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 5 with GrpcRouting

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

the class SecurityFromConfigIT method startServer.

@BeforeAll
public static void startServer() throws Exception {
    LogConfig.configureRuntime();
    // load the config containing the gRPC service security settings
    Config config = Config.builder().sources(ConfigSources.classpath("secure-services.conf")).build();
    // Create the gRPC routing configuring the GrpcSecurity interceptor from config
    GrpcRouting routing = GrpcRouting.builder().intercept(GrpcSecurity.create(config.get("security"))).register(new EchoService()).register(new 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) Config(io.helidon.config.Config) LogConfig(io.helidon.common.LogConfig) EchoService(services.EchoService) Channel(io.grpc.Channel) StringService(services.StringService) GrpcRouting(io.helidon.grpc.server.GrpcRouting) BeforeAll(org.junit.jupiter.api.BeforeAll)

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