Search in sources :

Example 46 with Security

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

the class SecurityFilterTest method testAtzAbortWith.

@Test
void testAtzAbortWith() {
    SecurityFeature feature = SecurityFeature.builder(security).build();
    SecurityContext securityContext = security.createContext("testAbortWith");
    SecurityFilter sf = new SecurityFilter(feature.featureConfig(), security, serverConfig, securityContext);
    ContainerRequest request = mock(ContainerRequest.class);
    SecurityFilter.FilterContext filterContext = new SecurityFilter.FilterContext();
    filterContext.setJerseyRequest(request);
    SecurityClientBuilder<AuthorizationResponse> clientBuilder = mock(SecurityClientBuilder.class);
    when(clientBuilder.buildAndGet()).thenReturn(AuthorizationResponse.builder().description("Unit-test").status(SecurityResponse.SecurityStatus.FAILURE).build());
    sf.processAuthorization(filterContext, clientBuilder);
    assertThat(filterContext.isShouldFinish(), is(true));
    verify(request).abortWith(argThat(response -> response.getStatus() == 403));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Security(io.helidon.security.Security) ServerConfig(org.glassfish.jersey.server.ServerConfig) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Set(java.util.Set) SecurityContext(io.helidon.security.SecurityContext) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) Mockito.when(org.mockito.Mockito.when) AuthenticationResponse(io.helidon.security.AuthenticationResponse) WebApplicationException(jakarta.ws.rs.WebApplicationException) Mockito.verify(org.mockito.Mockito.verify) SecurityResponse(io.helidon.security.SecurityResponse) Test(org.junit.jupiter.api.Test) Response(jakarta.ws.rs.core.Response) SecurityClientBuilder(io.helidon.security.SecurityClientBuilder) BeforeAll(org.junit.jupiter.api.BeforeAll) ResourceConfig(org.glassfish.jersey.server.ResourceConfig) Assertions(org.junit.jupiter.api.Assertions) Application(jakarta.ws.rs.core.Application) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) SecurityTracing(io.helidon.security.integration.common.SecurityTracing) AuthorizationResponse(io.helidon.security.AuthorizationResponse) Mockito.mock(org.mockito.Mockito.mock) SecurityContext(io.helidon.security.SecurityContext) ContainerRequest(org.glassfish.jersey.server.ContainerRequest) AuthorizationResponse(io.helidon.security.AuthorizationResponse) Test(org.junit.jupiter.api.Test)

Example 47 with Security

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

the class SecurityCdiExtension method registerSecurity.

// security must have priority higher than metrics, openapi and healt
// so we can protect these endpoints
private void registerSecurity(@Observes @Priority(LIBRARY_BEFORE) @Initialized(ApplicationScoped.class) Object adv, BeanManager bm) {
    if (securityBuilder.noProvider(AuthenticationProvider.class)) {
        LOGGER.info("Authentication provider is missing from security configuration, but security extension for microprofile " + "is enabled (requires providers configuration at key security.providers). " + "Security will not have any valid authentication provider");
        securityBuilder.addAuthenticationProvider(this::failingAtnProvider);
    }
    if (securityBuilder.noProvider(AuthorizationProvider.class)) {
        LOGGER.info("Authorization provider is missing from security configuration, but security extension for microprofile " + "is enabled (requires providers configuration at key security.providers). " + "ABAC provider is configured for authorization.");
        securityBuilder.addAuthorizationProvider(AbacProvider.create());
    }
    Security tmpSecurity = securityBuilder.build();
    // free it and make sure we fail if somebody wants to update security afterwards
    securityBuilder = null;
    if (!tmpSecurity.enabled()) {
        // security is disabled, we need to set up some basic stuff - injection, security context etc.
        LOGGER.info("Security is disabled.");
        tmpSecurity = Security.builder().enabled(false).build();
    }
    // we need an effectively final instance to use in lambda
    Security security = tmpSecurity;
    // security is available in global
    Contexts.globalContext().register(security);
    JaxRsCdiExtension jaxrs = bm.getExtension(JaxRsCdiExtension.class);
    ServerCdiExtension server = bm.getExtension(ServerCdiExtension.class);
    Contexts.context().ifPresent(ctx -> ctx.register(security));
    Config jerseyConfig = config.get("security.jersey");
    if (jerseyConfig.get("enabled").asBoolean().orElse(true)) {
        SecurityFeature feature = SecurityFeature.builder(security).config(jerseyConfig).build();
        jaxrs.applicationsToRun().forEach(app -> app.resourceConfig().register(feature));
    }
    Config webServerConfig = config.get("security.web-server");
    if (webServerConfig.exists() && webServerConfig.get("enabled").asBoolean().orElse(true)) {
        server.serverRoutingBuilder().register(WebSecurity.create(security, config.get("security")));
    }
    this.security.set(security);
}
Also used : SecurityFeature(io.helidon.security.integration.jersey.SecurityFeature) Config(io.helidon.config.Config) JaxRsCdiExtension(io.helidon.microprofile.server.JaxRsCdiExtension) Security(io.helidon.security.Security) WebSecurity(io.helidon.security.integration.webserver.WebSecurity) ServerCdiExtension(io.helidon.microprofile.server.ServerCdiExtension)

Example 48 with Security

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

the class TestProviderOverrides method testOverride.

@Test
void testOverride() {
    Map<String, String> map = Map.of("security.providers.1.type", "header-atn", "security.providers.1.header-atn.authenticate", "false");
    Config config = Config.builder().addSource(ConfigSources.create(map)).addSource(ConfigSources.classpath("application.yaml")).disableEnvironmentVariablesSource().disableSystemPropertiesSource().build();
    Security security = Security.create(config.get("security"));
}
Also used : Config(io.helidon.config.Config) Security(io.helidon.security.Security) Test(org.junit.jupiter.api.Test)

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