Search in sources :

Example 1 with JaxRsCdiExtension

use of io.helidon.microprofile.server.JaxRsCdiExtension in project helidon by oracle.

the class TracingCdiExtension method prepareTracer.

private void prepareTracer(@Observes @Priority(PLATFORM_BEFORE + 11) @Initialized(ApplicationScoped.class) Object event, BeanManager bm) {
    JaxRsCdiExtension jaxrs = bm.getExtension(JaxRsCdiExtension.class);
    ServerCdiExtension server = bm.getExtension(ServerCdiExtension.class);
    Config config = ((Config) ConfigProvider.getConfig()).get("tracing");
    // we need that `tracing.service` is a required configuration, yet we do not want to just fail
    // if not present. Let's make a "guess" about the service name
    List<JaxRsApplication> jaxRsApps = jaxrs.applicationsToRun();
    String serviceName = jaxrs.serviceName();
    Tracer tracer = TracerBuilder.create(serviceName).config(config).build();
    // tracer is available in global
    Contexts.globalContext().register(tracer);
    server.serverBuilder().tracer(tracer);
    Contexts.context().ifPresent(ctx -> ctx.register(tracer));
    if (tracer.getClass().getName().startsWith("io.opentracing.noop")) {
        Logger.getLogger(TracingCdiExtension.class.getName()).warning("helidon-microprofile-tracing is on the classpath, yet there is no tracer implementation " + "library. Tracing uses a no-op tracer. As a result, no tracing will be configured" + " for WebServer and JAX-RS");
        // no need to register all of this
        return;
    }
    server.serverRoutingBuilder().register(WebTracingConfig.create(config));
    jaxRsApps.forEach(app -> app.resourceConfig().register(MpTracingFilter.class));
}
Also used : Config(io.helidon.config.Config) WebTracingConfig(io.helidon.webserver.WebTracingConfig) Tracer(io.opentracing.Tracer) JaxRsApplication(io.helidon.microprofile.server.JaxRsApplication) JaxRsCdiExtension(io.helidon.microprofile.server.JaxRsCdiExtension) ServerCdiExtension(io.helidon.microprofile.server.ServerCdiExtension)

Example 2 with JaxRsCdiExtension

use of io.helidon.microprofile.server.JaxRsCdiExtension 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 3 with JaxRsCdiExtension

use of io.helidon.microprofile.server.JaxRsCdiExtension in project helidon by oracle.

the class JwtAuthCdiExtension method registerProvider.

void registerProvider(@Observes @Initialized(ApplicationScoped.class) @Priority(PLATFORM_BEFORE + 5) Object event, BeanManager bm) {
    // Security extension to update and check builder
    SecurityCdiExtension security = bm.getExtension(SecurityCdiExtension.class);
    if (security.securityBuilder().hasProvider(JwtAuthProviderService.PROVIDER_NAME)) {
        return;
    }
    // JAX-RS extension to get to applications to see if we are needed
    JaxRsCdiExtension jaxrs = bm.getExtension(JaxRsCdiExtension.class);
    boolean notNeeded = jaxrs.applicationsToRun().stream().map(JaxRsApplication::applicationClass).flatMap(Optional::stream).map(clazz -> clazz.getAnnotation(LoginConfig.class)).filter(Objects::nonNull).map(LoginConfig::authMethod).noneMatch("MP-JWT"::equals);
    if (notNeeded) {
        return;
    }
    security.securityBuilder().addProvider(JwtAuthProvider.create(config), JwtAuthProviderService.PROVIDER_NAME);
}
Also used : Dependent(jakarta.enterprise.context.Dependent) Arrays(java.util.Arrays) JsonValue(jakarta.json.JsonValue) JaxRsCdiExtension(io.helidon.microprofile.server.JaxRsCdiExtension) AnnotationLiteral(jakarta.enterprise.util.AnnotationLiteral) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) AfterDeploymentValidation(jakarta.enterprise.inject.spi.AfterDeploymentValidation) RuntimeStart(io.helidon.microprofile.cdi.RuntimeStart) AnnotatedParameter(jakarta.enterprise.inject.spi.AnnotatedParameter) BeforeBeanDiscovery(jakarta.enterprise.inject.spi.BeforeBeanDiscovery) Map(java.util.Map) DeploymentException(jakarta.enterprise.inject.spi.DeploymentException) JsonObject(jakarta.json.JsonObject) Method(java.lang.reflect.Method) Initialized(jakarta.enterprise.context.Initialized) Claims(org.eclipse.microprofile.jwt.Claims) PLATFORM_BEFORE(jakarta.interceptor.Interceptor.Priority.PLATFORM_BEFORE) BeanManager(jakarta.enterprise.inject.spi.BeanManager) TYPE(java.lang.annotation.ElementType.TYPE) FIELD(java.lang.annotation.ElementType.FIELD) Member(java.lang.reflect.Member) Set(java.util.Set) Objects(java.util.Objects) List(java.util.List) JaxRsApplication(io.helidon.microprofile.server.JaxRsApplication) Type(java.lang.reflect.Type) Optional(java.util.Optional) JsonNumber(jakarta.json.JsonNumber) JsonArray(jakarta.json.JsonArray) SecurityCdiExtension(io.helidon.microprofile.security.SecurityCdiExtension) Observes(jakarta.enterprise.event.Observes) ClaimValue(org.eclipse.microprofile.jwt.ClaimValue) PARAMETER(java.lang.annotation.ElementType.PARAMETER) ProcessInjectionPoint(jakarta.enterprise.inject.spi.ProcessInjectionPoint) Constructor(java.lang.reflect.Constructor) Instance(jakarta.enterprise.inject.Instance) Retention(java.lang.annotation.Retention) LoginConfig(org.eclipse.microprofile.auth.LoginConfig) AnnotatedField(jakarta.enterprise.inject.spi.AnnotatedField) Extension(jakarta.enterprise.inject.spi.Extension) Priority(jakarta.annotation.Priority) LinkedList(java.util.LinkedList) Config(io.helidon.config.Config) InjectionPoint(jakarta.enterprise.inject.spi.InjectionPoint) Claim(org.eclipse.microprofile.jwt.Claim) Target(java.lang.annotation.Target) Nonbinding(jakarta.enterprise.util.Nonbinding) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) METHOD(java.lang.annotation.ElementType.METHOD) ParameterizedType(java.lang.reflect.ParameterizedType) AfterBeanDiscovery(jakarta.enterprise.inject.spi.AfterBeanDiscovery) Annotated(jakarta.enterprise.inject.spi.Annotated) Provider(jakarta.inject.Provider) Qualifier(jakarta.inject.Qualifier) RequestScoped(jakarta.enterprise.context.RequestScoped) JsonString(jakarta.json.JsonString) Optional(java.util.Optional) SecurityCdiExtension(io.helidon.microprofile.security.SecurityCdiExtension) Objects(java.util.Objects) JaxRsCdiExtension(io.helidon.microprofile.server.JaxRsCdiExtension)

Aggregations

Config (io.helidon.config.Config)3 JaxRsCdiExtension (io.helidon.microprofile.server.JaxRsCdiExtension)3 JaxRsApplication (io.helidon.microprofile.server.JaxRsApplication)2 ServerCdiExtension (io.helidon.microprofile.server.ServerCdiExtension)2 RuntimeStart (io.helidon.microprofile.cdi.RuntimeStart)1 SecurityCdiExtension (io.helidon.microprofile.security.SecurityCdiExtension)1 Security (io.helidon.security.Security)1 SecurityFeature (io.helidon.security.integration.jersey.SecurityFeature)1 WebSecurity (io.helidon.security.integration.webserver.WebSecurity)1 WebTracingConfig (io.helidon.webserver.WebTracingConfig)1 Tracer (io.opentracing.Tracer)1 Priority (jakarta.annotation.Priority)1 ApplicationScoped (jakarta.enterprise.context.ApplicationScoped)1 Dependent (jakarta.enterprise.context.Dependent)1 Initialized (jakarta.enterprise.context.Initialized)1 RequestScoped (jakarta.enterprise.context.RequestScoped)1 Observes (jakarta.enterprise.event.Observes)1 Instance (jakarta.enterprise.inject.Instance)1 AfterBeanDiscovery (jakarta.enterprise.inject.spi.AfterBeanDiscovery)1 AfterDeploymentValidation (jakarta.enterprise.inject.spi.AfterDeploymentValidation)1