Search in sources :

Example 16 with Security

use of io.helidon.security.Security 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 17 with Security

use of io.helidon.security.Security in project helidon by oracle.

the class ServiceAndMethodLevelSecurityIT method startServer.

@BeforeAll
public static void startServer() throws Exception {
    LogConfig.configureRuntime();
    Config config = Config.create();
    Security security = Security.builder().addProvider(HttpBasicAuthProvider.create(config.get("http-basic-auth"))).build();
    ServiceDescriptor echoService = ServiceDescriptor.builder(new EchoService()).intercept(GrpcSecurity.rolesAllowed("admin")).build();
    ServiceDescriptor stringService = ServiceDescriptor.builder(new StringService()).intercept("Upper", GrpcSecurity.rolesAllowed("admin")).intercept("Split", GrpcSecurity.rolesAllowed("admin")).build();
    // Add the EchoService
    GrpcRouting routing = GrpcRouting.builder().intercept(GrpcSecurity.create(security).securityDefaults(GrpcSecurity.authenticate())).register(echoService).register(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) ServiceDescriptor(io.helidon.grpc.server.ServiceDescriptor) LogConfig(io.helidon.common.LogConfig) Config(io.helidon.config.Config) EchoService(services.EchoService) Channel(io.grpc.Channel) StringService(services.StringService) Security(io.helidon.security.Security) GrpcRouting(io.helidon.grpc.server.GrpcRouting) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 18 with Security

use of io.helidon.security.Security in project helidon by oracle.

the class ClientSecurityFilter method doFilter.

private void doFilter(ClientRequestContext requestContext) {
    // find the context - if not cannot propagate
    Optional<SecurityContext> securityContext = findContext(requestContext);
    if (securityContext.isPresent()) {
        outboundSecurity(requestContext, securityContext.get());
    } else {
        LOGGER.finest("Security context not available, using empty one. You can define it using " + "property \"" + ClientSecurity.PROPERTY_CONTEXT + "\" on request");
        // use current context, or create a new one if we run outside of Helidon context
        Context context = Contexts.context().orElseGet(() -> Context.builder().id("security-" + CONTEXT_COUNTER.incrementAndGet()).build());
        // create a new security context for current request (not authenticated)
        Optional<SecurityContext> newSecurityContext = context.get(Security.class).map(it -> it.createContext(context.id()));
        if (newSecurityContext.isPresent()) {
            // run in the context we obtained above with the new security context
            // we may still propagate security information (such as when we explicitly configure outbound
            // security in outbound target of a provider
            Contexts.runInContext(context, () -> outboundSecurity(requestContext, newSecurityContext.get()));
        } else {
            // we cannot do anything - security is not available in global or current context, cannot propagate
            LOGGER.finest("Security is not available in global or current context, cannot propagate identity.");
        }
    }
}
Also used : Context(io.helidon.common.context.Context) SecurityContext(io.helidon.security.SecurityContext) ClientRequestContext(jakarta.ws.rs.client.ClientRequestContext) SecurityContext(io.helidon.security.SecurityContext) Security(io.helidon.security.Security)

Example 19 with Security

use of io.helidon.security.Security in project helidon by oracle.

the class SecurityFeature method configure.

@Override
public boolean configure(FeatureContext context) {
    RuntimeType runtimeType = context.getConfiguration().getRuntimeType();
    // register server
    if (runtimeType != RuntimeType.SERVER) {
        return false;
    }
    context.register(SecurityPreMatchingFilter.class);
    context.register(SecurityFilter.class);
    // allow injection of security context (our, not Jersey)
    context.register(new AbstractBinder() {

        @Override
        protected void configure() {
            bindFactory(SecurityContextRefFactory.class).to(SecurityContext.class).proxy(true).proxyForSameScope(false).in(RequestScoped.class);
            bindFactory(ReferencingFactory.<SecurityContext>referenceFactory()).to(new GenericType<Ref<SecurityContext>>() {
            }).in(RequestScoped.class);
            bind(security).to(Security.class);
            bind(featureConfig).to(FeatureConfig.class);
        }
    });
    return true;
}
Also used : Ref(org.glassfish.jersey.internal.util.collection.Ref) RuntimeType(jakarta.ws.rs.RuntimeType) AbstractBinder(org.glassfish.jersey.internal.inject.AbstractBinder) SecurityContext(io.helidon.security.SecurityContext) RequestScoped(org.glassfish.jersey.process.internal.RequestScoped) Security(io.helidon.security.Security)

Example 20 with Security

use of io.helidon.security.Security in project helidon by oracle.

the class ExtractQueryParamsTest method initClass.

@BeforeAll
public static void initClass() throws Throwable {
    Config config = Config.create();
    Security security = Security.create(config.get("security"));
    SecurityFeature feature = SecurityFeature.builder(security).config(config.get("security.jersey")).build();
    server = Routing.builder().register(JerseySupport.builder().register(BindingTest.MyResource.class).register(TestResource1.class).register(new TestResource2()).register(feature).register(new ExceptionMapper<Exception>() {

        @Override
        public Response toResponse(Exception exception) {
            exception.printStackTrace();
            return Response.serverError().build();
        }
    }).build()).build().createServer();
    CountDownLatch cdl = new CountDownLatch(1);
    AtomicReference<Throwable> th = new AtomicReference<>();
    server.start().whenComplete((webServer, throwable) -> {
        th.set(throwable);
        cdl.countDown();
    });
    cdl.await();
    if (th.get() != null) {
        throw th.get();
    }
    client = ClientBuilder.newClient();
    baseTarget = client.target(UriBuilder.fromUri("http://localhost/").port(server.port()).build());
}
Also used : ExceptionMapper(jakarta.ws.rs.ext.ExceptionMapper) Config(io.helidon.config.Config) AtomicReference(java.util.concurrent.atomic.AtomicReference) Security(io.helidon.security.Security) CountDownLatch(java.util.concurrent.CountDownLatch) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

Security (io.helidon.security.Security)48 SecurityContext (io.helidon.security.SecurityContext)25 Config (io.helidon.config.Config)22 BeforeAll (org.junit.jupiter.api.BeforeAll)14 Test (org.junit.jupiter.api.Test)14 LogConfig (io.helidon.common.LogConfig)13 Routing (io.helidon.webserver.Routing)12 Optional (java.util.Optional)12 WebSecurity (io.helidon.security.integration.webserver.WebSecurity)10 WebClientSecurity (io.helidon.webclient.security.WebClientSecurity)10 WebServer (io.helidon.webserver.WebServer)10 Channel (io.grpc.Channel)7 GrpcRouting (io.helidon.grpc.server.GrpcRouting)7 GrpcServerConfiguration (io.helidon.grpc.server.GrpcServerConfiguration)7 ServiceDescriptor (io.helidon.grpc.server.ServiceDescriptor)7 Set (java.util.Set)7 CoreMatchers.is (org.hamcrest.CoreMatchers.is)7 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)7 Context (io.helidon.common.context.Context)6 AuthorizationResponse (io.helidon.security.AuthorizationResponse)6