helidon-io / helidon

Java libraries for writing microservices
https://helidon.io
Apache License 2.0
3.53k stars 565 forks source link

SecurityPreMatchingFilter is overriding the SecurityContext when using webserver security #6414

Closed fssouza closed 1 year ago

fssouza commented 1 year ago

Environment Details


Problem Description

An authenticated SecurityContext is being replaced by an unauthenticated one after all filters are executed

Steps to reproduce

Setup Helidon Security using "web server security". Example:

security:
  providers:
    - spectra-auth:
        idcsTenantCertChain: '<idcsTenantCertChain>'
        authentication:
          authenticate: true
        audience: "<audience>"
        idcsUrl: "<idcsUrl>"
        scope: "<scope>"
  web-server:
    paths:
      # authenticate all paths
      - path: "/myapplication/*"
        authenticate: true
        spectra-auth:
          authentication:
            authenticate: true

Put a break point in the following lines in SecurityPreMatchingFilter:

    public void filter(ContainerRequestContext request) {
        SecurityTracing tracing = SecurityTracing.get(); // BREAK POINT 1
        SecurityContext securityContext = this.security().contextBuilder(Integer.toString(CONTEXT_COUNTER.incrementAndGet(), 36)).tracingSpan((SpanContext)tracing.findParent().orElse((Object)null)).build();
        Contexts.context().ifPresent((ctx) -> {
            ctx.register(securityContext);
        });
        ((Ref)this.injectionManager.getInstance((new GenericType<Ref<SecurityContext>>() {
        }).getType())).set(securityContext);  // BREAK POINT 2
        if (this.featureConfig().shouldUsePrematchingAuthentication()) {
            this.doFilter(request, securityContext);
        }

    }

In debug mode, execute a request using a valid Bearer Token. This is the result when executing expressions in both break points:

In other words this this the block replacing the existing SecurityContext:

        Contexts.context().ifPresent((ctx) -> {
            ctx.register(securityContext);
        });
fssouza commented 1 year ago

@tomas-langer this is the issue we discussed over slack

fssouza commented 1 year ago

I believe I'm running on another issue that has this one as a root cause. The EndpointConfig I'm getting in the AbacProvider is missing the "abac" config in the configMap image This is my web-server configuration: image And this is the security context before being overridden: image