Search in sources :

Example 1 with ServiceDescriptor

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;
    });
}
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 ServiceDescriptor

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));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Histogram(org.eclipse.microprofile.metrics.Histogram) Context(io.grpc.Context) ServerCallHandler(io.grpc.ServerCallHandler) Metric(org.eclipse.microprofile.metrics.Metric) CoreMatchers.not(org.hamcrest.CoreMatchers.not) HashMap(java.util.HashMap) Meter(org.eclipse.microprofile.metrics.Meter) CoreMatchers.notNullValue(org.hamcrest.CoreMatchers.notNullValue) ConcurrentGauge(org.eclipse.microprofile.metrics.ConcurrentGauge) SimpleTimer(org.eclipse.microprofile.metrics.SimpleTimer) StreamObserver(io.grpc.stub.StreamObserver) ArgumentCaptor(org.mockito.ArgumentCaptor) Counter(org.eclipse.microprofile.metrics.Counter) LazyValue(io.helidon.common.LazyValue) BeforeAll(org.junit.jupiter.api.BeforeAll) Map(java.util.Map) MetricsSupport(io.helidon.metrics.MetricsSupport) Status(io.grpc.Status) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) CoreMatchers.sameInstance(org.hamcrest.CoreMatchers.sameInstance) MethodDescriptor(io.helidon.grpc.server.MethodDescriptor) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MetricUnits(org.eclipse.microprofile.metrics.MetricUnits) Mockito.when(org.mockito.Mockito.when) MetricID(org.eclipse.microprofile.metrics.MetricID) Collectors(java.util.stream.Collectors) Timer(org.eclipse.microprofile.metrics.Timer) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) GrpcService(io.helidon.grpc.server.GrpcService) ServerCall(io.grpc.ServerCall) Metadata(io.grpc.Metadata) MetricRegistry(org.eclipse.microprofile.metrics.MetricRegistry) Routing(io.helidon.webserver.Routing) ArgumentMatchers.same(org.mockito.ArgumentMatchers.same) Mockito.mock(org.mockito.Mockito.mock) HashMap(java.util.HashMap) Metadata(io.grpc.Metadata) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MethodDescriptor(io.helidon.grpc.server.MethodDescriptor) MetricID(org.eclipse.microprofile.metrics.MetricID) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Metric(org.eclipse.microprofile.metrics.Metric) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 3 with ServiceDescriptor

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));
}
Also used : SimpleTimer(org.eclipse.microprofile.metrics.SimpleTimer) Timer(org.eclipse.microprofile.metrics.Timer) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Metadata(io.grpc.Metadata) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MethodDescriptor(io.helidon.grpc.server.MethodDescriptor) Test(org.junit.jupiter.api.Test)

Example 4 with ServiceDescriptor

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));
}
Also used : Counter(org.eclipse.microprofile.metrics.Counter) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) Metadata(io.grpc.Metadata) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MethodDescriptor(io.helidon.grpc.server.MethodDescriptor) Test(org.junit.jupiter.api.Test)

Example 5 with ServiceDescriptor

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));
}
Also used : ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) ConcurrentGauge(org.eclipse.microprofile.metrics.ConcurrentGauge) Metadata(io.grpc.Metadata) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) MethodDescriptor(io.helidon.grpc.server.MethodDescriptor) Test(org.junit.jupiter.api.Test)

Aggregations

ServiceDescriptor (io.helidon.grpc.server.ServiceDescriptor)45 Test (org.junit.jupiter.api.Test)37 ServerInterceptor (io.grpc.ServerInterceptor)17 MethodDescriptor (io.helidon.grpc.server.MethodDescriptor)14 Metadata (io.grpc.Metadata)12 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)11 BeanManager (jakarta.enterprise.inject.spi.BeanManager)8 MetricID (org.eclipse.microprofile.metrics.MetricID)7 Counter (org.eclipse.microprofile.metrics.Counter)6 LogConfig (io.helidon.common.LogConfig)4 GrpcRouting (io.helidon.grpc.server.GrpcRouting)4 GrpcServerConfiguration (io.helidon.grpc.server.GrpcServerConfiguration)4 Security (io.helidon.security.Security)4 Config (io.helidon.config.Config)3 GrpcServer (io.helidon.grpc.server.GrpcServer)3 SimpleTimer (org.eclipse.microprofile.metrics.SimpleTimer)3 BeforeAll (org.junit.jupiter.api.BeforeAll)3 Channel (io.grpc.Channel)2 ServerCall (io.grpc.ServerCall)2 ServerCallHandler (io.grpc.ServerCallHandler)2