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;
});
}
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;
});
}
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());
}
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);
}
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);
}
Aggregations