-
Notifications
You must be signed in to change notification settings - Fork 292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add session tracking support to vert.x 3/4 #7953
Open
manuel-alvarez-alvarez
wants to merge
2
commits into
master
Choose a base branch
from
malvarez/appsec-session-tracking-vertx
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,9 @@ import io.vertx.reactivex.core.MultiMap | |
import io.vertx.reactivex.ext.web.Router | ||
import io.vertx.reactivex.ext.web.RoutingContext | ||
import io.vertx.reactivex.ext.web.handler.BodyHandler | ||
import io.vertx.reactivex.ext.web.handler.CookieHandler | ||
import io.vertx.reactivex.ext.web.handler.SessionHandler | ||
import io.vertx.reactivex.ext.web.sstore.LocalSessionStore | ||
|
||
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.BODY_MULTIPART | ||
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.BODY_URLENCODED | ||
|
@@ -22,6 +25,7 @@ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.QUERY_ | |
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.QUERY_ENCODED_QUERY | ||
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM | ||
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.REDIRECT | ||
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.SESSION_ID | ||
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.SUCCESS | ||
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.USER_BLOCK | ||
import static server.VertxTestServer.CONFIG_HTTP_SERVER_PORT | ||
|
@@ -233,6 +237,26 @@ class VertxRxCircuitBreakerHttpServerForkedTest extends VertxHttpServerForkedTes | |
}) | ||
} | ||
|
||
router.route(SESSION_ID.getPath()).handler(CookieHandler.create()) | ||
final LocalSessionStore sessionStorage = LocalSessionStore.create(super.@vertx) | ||
router | ||
.route(SESSION_ID.getPath()) | ||
.handler(SessionHandler.create(sessionStorage).setCookieSecureFlag(true).setCookieHttpOnlyFlag(true)) | ||
router | ||
.route(SESSION_ID.getPath()) | ||
.handler { ctx -> | ||
controller(SESSION_ID) { | ||
final session = ctx.session() | ||
if (!session) { | ||
ctx.response() | ||
.setStatusCode(500) | ||
.end('Cookie/Session handlers not present') | ||
} else { | ||
ctx.response().setStatusCode(SESSION_ID.getStatus()).end(session.id()) | ||
} | ||
} | ||
} | ||
|
||
[email protected](new HttpServerOptions().setHandle100ContinueAutomatically(true)) | ||
.requestHandler { router.accept(it) } | ||
.listen(port) { startFuture.complete() } | ||
|
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 |
---|---|---|
|
@@ -4,6 +4,9 @@ import io.vertx.core.AbstractVerticle | |
import io.vertx.core.Future | ||
import io.vertx.core.http.HttpServerOptions | ||
import io.vertx.ext.web.Router | ||
import io.vertx.ext.web.handler.CookieHandler | ||
import io.vertx.ext.web.handler.SessionHandler | ||
import io.vertx.ext.web.sstore.LocalSessionStore | ||
import spock.lang.Ignore | ||
|
||
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.ERROR | ||
|
@@ -12,6 +15,7 @@ import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.FORWAR | |
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.PATH_PARAM | ||
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.QUERY_PARAM | ||
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.REDIRECT | ||
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.SESSION_ID | ||
import static datadog.trace.agent.test.base.HttpServerTest.ServerEndpoint.SUCCESS | ||
import static server.VertxTestServer.CONFIG_HTTP_SERVER_PORT | ||
|
||
|
@@ -72,6 +76,24 @@ class VertxRxHttpServerForkedTest extends VertxHttpServerForkedTest { | |
} | ||
} | ||
|
||
router.route(SESSION_ID.getPath()).handler(CookieHandler.create()) | ||
final LocalSessionStore sessionStorage = LocalSessionStore.create(vertx) | ||
router | ||
.route(SESSION_ID.getPath()) | ||
.handler(SessionHandler.create(sessionStorage).setCookieSecureFlag(true).setCookieHttpOnlyFlag(true)) | ||
router.route(SESSION_ID.getPath()).handler { ctx -> | ||
controller(SESSION_ID) { | ||
final session = ctx.session() | ||
if (session == null) { | ||
ctx.response() | ||
.setStatusCode(500) | ||
.end('Cookie/Session handlers not present') | ||
} else { | ||
ctx.response().setStatusCode(SESSION_ID.getStatus()).end(session.id()) | ||
} | ||
} | ||
} | ||
|
||
[email protected](new HttpServerOptions().setHandle100ContinueAutomatically(true)) | ||
.requestHandler { router.accept(it) } | ||
.listen(port) { startFuture.complete() } | ||
|
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
54 changes: 54 additions & 0 deletions
54
...main/java/datadog/trace/instrumentation/vertx_3_4/server/RoutingContextSessionAdvice.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,54 @@ | ||
package datadog.trace.instrumentation.vertx_3_4.server; | ||
|
||
import static datadog.trace.api.gateway.Events.EVENTS; | ||
|
||
import datadog.appsec.api.blocking.BlockingException; | ||
import datadog.trace.advice.ActiveRequestContext; | ||
import datadog.trace.advice.RequiresRequestContext; | ||
import datadog.trace.api.gateway.BlockResponseFunction; | ||
import datadog.trace.api.gateway.CallbackProvider; | ||
import datadog.trace.api.gateway.Flow; | ||
import datadog.trace.api.gateway.RequestContext; | ||
import datadog.trace.api.gateway.RequestContextSlot; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentTracer; | ||
import io.vertx.ext.web.Session; | ||
import java.util.function.BiFunction; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
@RequiresRequestContext(RequestContextSlot.APPSEC) | ||
class RoutingContextSessionAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
static void after( | ||
@Advice.Argument(0) final Session session, | ||
@ActiveRequestContext RequestContext reqCtx, | ||
@Advice.Thrown(readOnly = false) Throwable throwable) { | ||
if (session == null || session.id() == null) { | ||
return; | ||
} | ||
|
||
final CallbackProvider cbp = AgentTracer.get().getCallbackProvider(RequestContextSlot.APPSEC); | ||
BiFunction<RequestContext, String, Flow<Void>> callback = | ||
cbp.getCallback(EVENTS.requestSession()); | ||
if (callback == null) { | ||
return; | ||
} | ||
|
||
Flow<Void> flow = callback.apply(reqCtx, session.id()); | ||
Flow.Action action = flow.getAction(); | ||
if (action instanceof Flow.Action.RequestBlockingAction) { | ||
BlockResponseFunction blockResponseFunction = reqCtx.getBlockResponseFunction(); | ||
if (blockResponseFunction == null) { | ||
return; | ||
} | ||
Flow.Action.RequestBlockingAction rba = (Flow.Action.RequestBlockingAction) action; | ||
blockResponseFunction.tryCommitBlockingResponse( | ||
reqCtx.getTraceSegment(), | ||
rba.getStatusCode(), | ||
rba.getBlockingContentType(), | ||
rba.getExtraHeaders()); | ||
if (throwable == null) { | ||
throwable = new BlockingException("Blocked request (for sessionId)"); | ||
} | ||
Comment on lines
+29
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not related with this PR but I have the feeling that we are repeating this pice of code every time we need to block something 😅 |
||
} | ||
} | ||
} |
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
54 changes: 54 additions & 0 deletions
54
...main/java/datadog/trace/instrumentation/vertx_4_0/server/RoutingContextSessionAdvice.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,54 @@ | ||
package datadog.trace.instrumentation.vertx_4_0.server; | ||
|
||
import static datadog.trace.api.gateway.Events.EVENTS; | ||
|
||
import datadog.appsec.api.blocking.BlockingException; | ||
import datadog.trace.advice.ActiveRequestContext; | ||
import datadog.trace.advice.RequiresRequestContext; | ||
import datadog.trace.api.gateway.BlockResponseFunction; | ||
import datadog.trace.api.gateway.CallbackProvider; | ||
import datadog.trace.api.gateway.Flow; | ||
import datadog.trace.api.gateway.RequestContext; | ||
import datadog.trace.api.gateway.RequestContextSlot; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentTracer; | ||
import io.vertx.ext.web.Session; | ||
import java.util.function.BiFunction; | ||
import net.bytebuddy.asm.Advice; | ||
|
||
@RequiresRequestContext(RequestContextSlot.APPSEC) | ||
class RoutingContextSessionAdvice { | ||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
static void after( | ||
@Advice.Argument(0) final Session session, | ||
@ActiveRequestContext RequestContext reqCtx, | ||
@Advice.Thrown(readOnly = false) Throwable throwable) { | ||
if (session == null || session.id() == null) { | ||
return; | ||
} | ||
|
||
final CallbackProvider cbp = AgentTracer.get().getCallbackProvider(RequestContextSlot.APPSEC); | ||
BiFunction<RequestContext, String, Flow<Void>> callback = | ||
cbp.getCallback(EVENTS.requestSession()); | ||
if (callback == null) { | ||
return; | ||
} | ||
|
||
Flow<Void> flow = callback.apply(reqCtx, session.id()); | ||
Flow.Action action = flow.getAction(); | ||
if (action instanceof Flow.Action.RequestBlockingAction) { | ||
BlockResponseFunction blockResponseFunction = reqCtx.getBlockResponseFunction(); | ||
if (blockResponseFunction == null) { | ||
return; | ||
} | ||
Flow.Action.RequestBlockingAction rba = (Flow.Action.RequestBlockingAction) action; | ||
blockResponseFunction.tryCommitBlockingResponse( | ||
reqCtx.getTraceSegment(), | ||
rba.getStatusCode(), | ||
rba.getBlockingContentType(), | ||
rba.getExtraHeaders()); | ||
if (throwable == null) { | ||
throwable = new BlockingException("Blocked request (for sessionId)"); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should you need to change the throwable in this case or just not apply the advice at all?