use of io.helidon.grpc.server.GrpcServerConfiguration 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.GrpcServerConfiguration 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.GrpcServerConfiguration 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;
});
}
use of io.helidon.grpc.server.GrpcServerConfiguration 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.GrpcServerConfiguration 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));
}
Aggregations