Skip to content

Commit

Permalink
Remove hard servlet dependency from SystemSecurityContext (eclipse-ha…
Browse files Browse the repository at this point in the history
…wkbit#1812)

Signed-off-by: Marinov Avgustin <[email protected]>
  • Loading branch information
avgustinmm authored Aug 11, 2024
1 parent e874cf5 commit d851fa4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ protected SecurityFilterChain filterChainDDI(final HttpSecurity http) throws Exc
.sessionManagement(configurer -> configurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
}

MDCHandler.getInstance().addLoggingFilter(http);
MDCHandler.Filter.addLoggingFilter(http);

return http.build();
}
Expand Down Expand Up @@ -323,7 +323,7 @@ protected SecurityFilterChain filterChainDDIDL(final HttpSecurity http) throws E
.sessionManagement(configurer -> configurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS));
}

MDCHandler.getInstance().addLoggingFilter(http);
MDCHandler.Filter.addLoggingFilter(http);

return http.build();
}
Expand Down Expand Up @@ -387,7 +387,7 @@ protected SecurityFilterChain filterChainDLID(
.addFilterBefore(downloadIdAuthenticationFilter, AuthorizationFilter.class)
.sessionManagement(configurer -> configurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS));

MDCHandler.getInstance().addLoggingFilter(http);
MDCHandler.Filter.addLoggingFilter(http);

return http.build();
}
Expand Down Expand Up @@ -491,7 +491,7 @@ SecurityFilterChain filterChainREST(
httpSecurityCustomizer.customize(http);
}

MDCHandler.getInstance().addLoggingFilter(http);
MDCHandler.Filter.addLoggingFilter(http);

return http.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package org.eclipse.hawkbit.security;

import ch.qos.logback.classic.turbo.MDCFilter;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -76,36 +77,6 @@ public <T> T withLogging(final Callable<T> callable) throws Exception {
}
}

public void addLoggingFilter(final HttpSecurity httpSecurity) {
httpSecurity.addFilterBefore(new OncePerRequestFilter() {
@Override
protected void doFilterInternal(
final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain)
throws ServletException, IOException {
try {
withLogging(() -> {
filterChain.doFilter(request, response);
return null;
});
} catch (final RuntimeException re) {
throw re;
} catch (final WrappedException we) {
final Throwable cause = we.getCause();
if (cause instanceof ServletException se) {
throw se;
} else if (cause instanceof IOException ioe) {
throw ioe;
} else {
throw we.toRuntimeException();
}
} catch (final Exception e) {
// should never be here - if mdc is handler is enabled non-runtime exceptions are always wrapped
throw new RuntimeException(e);
}
}
}, AuthorizationFilter.class);
}

private <T> T putUserAndCall(final Callable<T> callable) throws WrappedException {
final String user = springSecurityAuditorAware
.getCurrentAuditor()
Expand Down Expand Up @@ -151,4 +122,40 @@ public RuntimeException toRuntimeException() {
return new RuntimeException(getCause() == null ? this : getCause());
}
}

public static class Filter {

public static void addLoggingFilter(final HttpSecurity httpSecurity) {
httpSecurity.addFilterBefore(new OncePerRequestFilter() {

private final MDCHandler mdcFilter = MDCHandler.getInstance();

@Override
protected void doFilterInternal(
final HttpServletRequest request, final HttpServletResponse response, final FilterChain filterChain)
throws ServletException, IOException {
try {
mdcFilter.withLogging(() -> {
filterChain.doFilter(request, response);
return null;
});
} catch (final RuntimeException re) {
throw re;
} catch (final WrappedException we) {
final Throwable cause = we.getCause();
if (cause instanceof ServletException se) {
throw se;
} else if (cause instanceof IOException ioe) {
throw ioe;
} else {
throw we.toRuntimeException();
}
} catch (final Exception e) {
// should never be here - if mdc is handler is enabled non-runtime exceptions are always wrapped
throw new RuntimeException(e);
}
}
}, AuthorizationFilter.class);
}
}
}

0 comments on commit d851fa4

Please sign in to comment.