use of io.helidon.grpc.server.ServiceDescriptor 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.ServiceDescriptor in project helidon by oracle.
the class GrpcMetricsInterceptorIT method shouldApplyTags.
@Test
public void shouldApplyTags() throws Exception {
ServiceDescriptor descriptor = ServiceDescriptor.builder(createMockService()).unary("barTags", this::dummyUnary).build();
MethodDescriptor methodDescriptor = descriptor.method("barTags");
Map<String, String> tags = Map.of("one", "t1", "two", "t2");
GrpcMetrics metrics = GrpcMetrics.counted().tags(tags);
ServerCall<String, String> call = call(metrics, methodDescriptor);
call.close(Status.OK, new Metadata());
Map<MetricID, Metric> matchingMetrics = appRegistry.get().getMetrics().entrySet().stream().filter(entry -> entry.getKey().getName().equals("Foo.barTags")).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
assertThat(matchingMetrics.size(), not(0));
Map.Entry<MetricID, Metric> match = matchingMetrics.entrySet().stream().findFirst().orElse(null);
assertVendorMetrics();
Map<String, String> expected = new HashMap<>();
expected.put("one", "t1");
expected.put("two", "t2");
/*
* We expect the tags to be on the ID in the version-neutral (and 2.0)
* metrics programming model.
*/
assertThat(match.getKey().getTags(), is(expected));
}
use of io.helidon.grpc.server.ServiceDescriptor in project helidon by oracle.
the class GrpcMetricsInterceptorIT method shouldUseTimerMetric.
@Test
public void shouldUseTimerMetric() throws Exception {
ServiceDescriptor descriptor = ServiceDescriptor.builder(createMockService()).unary("barTimed", this::dummyUnary).build();
MethodDescriptor methodDescriptor = descriptor.method("barTimed");
GrpcMetrics metrics = GrpcMetrics.timed();
ServerCall<String, String> call = call(metrics, methodDescriptor);
call.close(Status.OK, new Metadata());
Timer appTimer = appRegistry.get().timer("Foo.barTimed");
assertVendorMetrics();
assertThat(appTimer.getCount(), is(1L));
}
use of io.helidon.grpc.server.ServiceDescriptor in project helidon by oracle.
the class GrpcMetricsInterceptorIT method shouldUseCountedMetric.
@Test
public void shouldUseCountedMetric() throws Exception {
ServiceDescriptor descriptor = ServiceDescriptor.builder(createMockService()).unary("testCounted", this::dummyUnary).build();
MethodDescriptor methodDescriptor = descriptor.method("testCounted");
GrpcMetrics metrics = GrpcMetrics.counted();
ServerCall<String, String> call = call(metrics, methodDescriptor);
call.close(Status.OK, new Metadata());
Counter appCounter = appRegistry.get().counter("Foo.testCounted");
assertVendorMetrics();
assertThat(appCounter.getCount(), is(1L));
}
use of io.helidon.grpc.server.ServiceDescriptor in project helidon by oracle.
the class GrpcMetricsInterceptorIT method shouldUseConcurrentGaugeMetric.
@Test
public void shouldUseConcurrentGaugeMetric() throws Exception {
ServiceDescriptor descriptor = ServiceDescriptor.builder(createMockService()).unary("barConcurrentGauge", this::dummyUnary).build();
MethodDescriptor methodDescriptor = descriptor.method("barConcurrentGauge");
GrpcMetrics metrics = GrpcMetrics.concurrentGauge();
ServerCall<String, String> call = call(metrics, methodDescriptor);
call.close(Status.OK, new Metadata());
ConcurrentGauge appConcurrentGauge = appRegistry.get().concurrentGauge("Foo.barConcurrentGauge");
assertVendorMetrics();
assertThat(appConcurrentGauge.getCount(), is(1L));
}
Aggregations