-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for user tracking in spring security
- Loading branch information
1 parent
e46eaa2
commit a7cf8c6
Showing
30 changed files
with
1,326 additions
and
317 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
403 changes: 221 additions & 182 deletions
403
dd-java-agent/instrumentation/spring-security-5/gradle.lockfile
Large diffs are not rendered by default.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
...-5/src/main/java/datadog/trace/instrumentation/springsecurity5/AppSecDeferredContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package datadog.trace.instrumentation.springsecurity5; | ||
|
||
import java.util.function.Supplier; | ||
import org.springframework.security.core.context.SecurityContext; | ||
|
||
public class AppSecDeferredContext implements Supplier<SecurityContext> { | ||
|
||
private final Supplier<SecurityContext> delegate; | ||
|
||
public AppSecDeferredContext(final Supplier<SecurityContext> delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public SecurityContext get() { | ||
SecurityContext context = delegate.get(); | ||
if (context != null) { | ||
SpringSecurityUserEventDecorator.DECORATE.onUser(context.getAuthentication()); | ||
} | ||
return context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...a/datadog/trace/instrumentation/springsecurity5/SecurityContextHolderInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package datadog.trace.instrumentation.springsecurity5; | ||
|
||
import static datadog.trace.agent.tooling.bytebuddy.matcher.HierarchyMatchers.implementsInterface; | ||
import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.agent.tooling.InstrumenterModule; | ||
import datadog.trace.bootstrap.ActiveSubsystems; | ||
import java.util.function.Supplier; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
import org.springframework.security.core.context.SecurityContext; | ||
|
||
@AutoService(InstrumenterModule.class) | ||
public class SecurityContextHolderInstrumentation extends InstrumenterModule.AppSec | ||
implements Instrumenter.ForTypeHierarchy { | ||
|
||
public SecurityContextHolderInstrumentation() { | ||
super("spring-security"); | ||
} | ||
|
||
@Override | ||
public String hierarchyMarkerType() { | ||
return "org.springframework.security.core.context.SecurityContextHolderStrategy"; | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> hierarchyMatcher() { | ||
return implementsInterface(named(hierarchyMarkerType())); | ||
} | ||
|
||
@Override | ||
public String[] helperClassNames() { | ||
return new String[] { | ||
"datadog.trace.instrumentation.springsecurity5.SpringSecurityUserEventDecorator", | ||
"datadog.trace.instrumentation.springsecurity5.AppSecDeferredContext" | ||
}; | ||
} | ||
|
||
@Override | ||
public void methodAdvice(MethodTransformer transformer) { | ||
transformer.applyAdvice( | ||
isMethod() | ||
.and(named("setContext")) | ||
.and(takesArguments(1)) | ||
.and( | ||
takesArgument( | ||
0, named("org.springframework.security.core.context.SecurityContext"))) | ||
.and(isPublic()), | ||
getClass().getName() + "$SetSecurityContextAdvice"); | ||
transformer.applyAdvice( | ||
isMethod().and(named("setDeferredContext")).and(takesArguments(1)).and(isPublic()), | ||
getClass().getName() + "$SetDeferredSecurityContextAdvice"); | ||
} | ||
|
||
public static class SetSecurityContextAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter(@Advice.Argument(0) final SecurityContext context) { | ||
if (context == null) { | ||
return; | ||
} | ||
if (!ActiveSubsystems.APPSEC_ACTIVE) { | ||
return; | ||
} | ||
SpringSecurityUserEventDecorator.DECORATE.onUser(context.getAuthentication()); | ||
} | ||
} | ||
|
||
public static class SetDeferredSecurityContextAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static void onEnter( | ||
@Advice.Argument(value = 0, readOnly = false) Supplier<SecurityContext> deferred) { | ||
if (deferred == null) { | ||
return; | ||
} | ||
if (!ActiveSubsystems.APPSEC_ACTIVE) { | ||
return; | ||
} | ||
deferred = new AppSecDeferredContext(deferred); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 0 additions & 19 deletions
19
...in/java17/datadog/trace/instrumentation/springsecurity5/AuthenticationProviderAdvice.java
This file was deleted.
Oops, something went wrong.
17 changes: 0 additions & 17 deletions
17
...c/main/java17/datadog/trace/instrumentation/springsecurity5/UserDetailsManagerAdvice.java
This file was deleted.
Oops, something went wrong.
11 changes: 0 additions & 11 deletions
11
...java17/datadog/trace/instrumentation/springsecurity5/UsernameNotFoundExceptionAdvice.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.