Skip to content
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

[Draft; don't merge] v1 as frontend of v2 #2607

Draft
wants to merge 189 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
189 commits
Select commit Hold shift + click to select a range
f406a87
wip
darccio Feb 14, 2024
2e2be33
ddtrace/tracer: use all exported functions as frontend for their v2 c…
darccio Feb 15, 2024
7a97334
ddtrace/tracer: remove unused code after conversion
darccio Feb 15, 2024
6119ca9
Merge remote-tracking branch 'origin/main' into vits
darccio Feb 15, 2024
f6dc6b7
go.mod: upgrade dd-trace-go & fix tests
darccio Feb 15, 2024
26eebd4
go.mod: upgrade dd-trace-go
darccio Feb 16, 2024
0a4f483
ddtrace/opentelemetry: use all exported functions as frontend for the…
darccio Feb 16, 2024
a96ebd3
ddtrace/opentelemetry: remove unused code after conversion
darccio Feb 16, 2024
4454b85
datastreams: use all exported functions as frontend for their v2 coun…
darccio Feb 16, 2024
c26938d
ddtrace/tracer: fix failing tests (to be squashed)
darccio Feb 16, 2024
f2b2c7d
profiler: use all exported functions as frontend for their v2 counter…
darccio Feb 19, 2024
655a99a
appsec: use all exported functions as frontend for their v2 counterparts
darccio Feb 19, 2024
acb90a8
ddtrace/tracer: fix failing tests (to be squashed)
darccio Feb 19, 2024
9ac02ee
contrib: use all exported functions and types as frontend for their v…
darccio Feb 19, 2024
ddd3268
contrib: remove unused code after conversion
darccio Feb 19, 2024
e453783
internal: remove unused code after conversion
darccio Feb 19, 2024
112d10a
profiler/internal: remove unused code after conversion
darccio Feb 19, 2024
688e227
internal/traceprof: remove unused code after conversion
darccio Feb 19, 2024
55ca241
contrib: fix failing tests (pending to squash)
darccio Feb 19, 2024
e31e95e
ddtrace/tracer: remove failing tests (pending to squash)
darccio Feb 19, 2024
5a15bca
internal/log: pass logger to v2
darccio Feb 19, 2024
7cbe4ab
internal/version: v1.999.0-rc.1
darccio Feb 20, 2024
9aabf03
internal/apps/setup-smoke-test: recover
darccio Feb 20, 2024
5710cef
profiler/upload: fix copyright header
darccio Feb 20, 2024
8bfcb1d
ddtrace/mocktracer: add MockspanV2Adapter to allow transparent operat…
darccio Feb 20, 2024
7eb723f
ddtrace/tracer: gracefully handle nil Span in ContextWithSpan
darccio Feb 20, 2024
de3213d
ddtrace/tracer: revert ContextWithSpan and SpanFromContext to be self…
darccio Feb 20, 2024
6a6f979
contrib/gorilla/mux: fix tests and mock v1 mockspan behaviour
darccio Feb 21, 2024
a0bbf07
internal/version: v1.999.0-rc.3
darccio Feb 21, 2024
c2a6279
ddtrace: support v1 behaviour for mock spans
darccio Feb 22, 2024
edfa0cc
go.mod: update dd-trace-go
darccio Feb 22, 2024
20d2279
internal/version: v1.999.0-rc.5
darccio Feb 22, 2024
3128d4c
ddtrace/tracer: remove extra ContextWithSpan call in StartSpanFromCon…
darccio Feb 22, 2024
007d72a
go.mod: update dd-trace-go
darccio Feb 22, 2024
5c94f45
internal/version: v1.999.0-rc.7
darccio Feb 23, 2024
f71a46a
ddtrace/mocktracer: fix OpenSpans
darccio Feb 23, 2024
e2610f8
go.mod: upgrade dd-trace-go/v2
darccio Feb 23, 2024
92b954c
internal/version: v1.999.0-rc.8
darccio Feb 23, 2024
27358ff
Merge remote-tracking branch 'origin/main' into vits
darccio Mar 4, 2024
285d5a9
Merge remote-tracking branch 'origin/main' into vits
darccio Mar 7, 2024
55323ef
go.mod: update to latests dd-trace-go/v2
darccio Mar 7, 2024
254f26c
ci: trigger
darccio Mar 7, 2024
a3ecf4a
ddtrace/mocktracer: small fixes for adapting to real tracer behaviour
darccio Mar 8, 2024
d843d2f
go.mod: upgrade dd-trace-go/v2
darccio Mar 8, 2024
f0418fb
contrib/aws/aws-sdk-go/aws: migrate contrib/aws/aws-sdk-go/aws to v2
darccio Mar 8, 2024
4e41abc
contrib/aws/aws-sdk-go-v2/aws: migrate contrib/aws/aws-sdk-go-v2/aws …
darccio Mar 8, 2024
c10f0c2
go.mod: upgrade dd-trace-go/v2 dependencies
darccio Mar 8, 2024
19c57ff
internal/version: v1.999.0-rc.9
darccio Mar 8, 2024
477e794
contrib/IBM/sarama.v1: migrate to v2
darccio Mar 8, 2024
e48294f
contrib/bradfitz/gomemcache: migrate to v2
darccio Mar 8, 2024
b44dad8
contrib/cloud.google.com/go/pubsub.v1: migrate to v2
darccio Mar 8, 2024
b631087
contrib/confluentinc/confluent-kafka-go: migrate to v2
darccio Mar 8, 2024
b6317ce
contrib/confluentinc/confluent-kafka-go.v2: migrate to v2
darccio Mar 10, 2024
0756777
contrib/database/sql/internal: remove unused code
darccio Mar 10, 2024
2f62e96
contrib/dimfeld/httptreemux.v5: migrate to v2
darccio Mar 10, 2024
e0eb1b2
contrib/elastic/go-elasticsearch.v6: migrate to v2
darccio Mar 10, 2024
42d7188
contrib/globalsign/mgo: migrate to v2
darccio Mar 10, 2024
544cde9
contrib/go-pg/pg.v10: migrate to v2
darccio Mar 10, 2024
c8cc4ef
contrib/go-redis/redis: migrate to v2
darccio Mar 10, 2024
8701484
contrib/go-redis/redis.v7: migrate to v2
darccio Mar 10, 2024
06cbb20
contrib/confluentinc/confluent-kafka-go: fix integration check
darccio Mar 10, 2024
65d19e3
contrib/go-redis/redis.v8: migrate to v2
darccio Mar 10, 2024
a4c3ecd
contrib/go.mongodb.org/mongo-driver: migrate to v2
darccio Mar 10, 2024
cda2976
contrib/internal: remove extranuous v2.go file
darccio Mar 10, 2024
124b409
contrib/gocql/gocql: migrate to v2
darccio Mar 10, 2024
3cb04b2
contrib/go.mongodb.org/mongo-driver: fix integration tests
darccio Mar 10, 2024
5edb819
contrib/gofiber/fiber.v2: migrate to v2
darccio Mar 10, 2024
e5529fc
contrib/gomodule/redigo: migrate to v2
darccio Mar 10, 2024
95e067a
contrib/google.golang.org/api/internal/tree: remove unused code
darccio Mar 10, 2024
a99c2f1
contrib/gorm.io/gorm.v1: migrate to v2
darccio Mar 10, 2024
2342d99
contrib/hashicorp/consul: migrate to v2
darccio Mar 10, 2024
751f4e2
contrib/jackc/pgx.v5: migrate to v2
darccio Mar 10, 2024
3f0d8a3
contrib/jmoiron/sqlx: migrate to v2
darccio Mar 10, 2024
6fa36c9
contrib/k8s.io/client-go: remove unused code
darccio Mar 10, 2024
5ff770a
contrib/miekg/dns: migrate to v2
darccio Mar 10, 2024
7ee262c
contrib/olivere/elastic: migrate to v2
darccio Mar 10, 2024
f441d8a
contrib/redis/go-redis.v9: migrate to v2
darccio Mar 11, 2024
069a708
contrib/segmentio/kafka.go.v0: migrate to v2
darccio Mar 11, 2024
8340083
contrib/Shopify/sarama: migrate to v2
darccio Mar 11, 2024
4b90fdf
contrib/sirupsen/logrus: migrate to v2
darccio Mar 11, 2024
62ca3fd
contrib/syndtr/goleveldb: migrate to v2
darccio Mar 11, 2024
acb7964
contrib/tidwall/buntdb: migrate to v2
darccio Mar 11, 2024
7947f7d
contrib/twitchtv/twirp: migrate to v2
darccio Mar 11, 2024
5a42a1b
contrib/urfave/negroni: migrate to v2
darccio Mar 11, 2024
257c3ab
contrib/aws/internal/tags: remove unused code
darccio Mar 11, 2024
eb033d8
appsec: remove unused code
darccio Mar 11, 2024
fd3917f
internal/{namingschema,globalconfig}: use v2/v1internal package to ad…
darccio Mar 11, 2024
0edd4c8
Merge remote-tracking branch 'origin/main' into vits
darccio Mar 11, 2024
d03e5c4
internal/version: remove unexpected character
darccio Mar 11, 2024
097a4e8
internal/telemetry: reduce API to bare minimum
darccio Mar 11, 2024
c54f08c
v1internal: remove accidentally commited files
darccio Mar 11, 2024
25d9c14
internal/telemetry: add minimal API
darccio Mar 11, 2024
ece6a1f
contrib/cloud.google.com/go/pubsub.v1: fix tests
darccio Mar 11, 2024
e3fe531
contrib: fix multiple tests
darccio Mar 11, 2024
1e2dbbb
go.mod: upgrade dd-trace-go/v2
darccio Mar 11, 2024
a0b43de
go.mod: upgrade dd-trace-go/v2 & aws-sdk-go-v2 contribs/deps
darccio Mar 11, 2024
76ce080
ddtrace/tracer: add missing benchmarks
darccio Mar 11, 2024
3ed2932
go.sum: go mod tidy
darccio Mar 11, 2024
bb7c6dd
contrib/graphql-go/graphql: fix contrib tests
darccio Mar 11, 2024
24e0ae6
internal/namingschema: fix TestServiceName
darccio Mar 11, 2024
2024063
contrib/net/http: avoid mocktracer in BenchmarkHttpServeTrace
darccio Mar 12, 2024
1b34ecb
ddtrace/internal: replace BuildStartSpanConfigV2 with ApplyV1Options
darccio Mar 12, 2024
04057f4
ddtrace/tracer: remove unused code
darccio Mar 12, 2024
4650bad
ddtrace/tracer: reset BenchmarkTracerAddSpans' timer before main loop
darccio Mar 12, 2024
25ec7b7
contrib: fix tests
darccio Mar 13, 2024
c8a0f6d
contrib/google.golang.org/grpc: fix TestSpanTree
darccio Mar 13, 2024
6cb3d86
go.sum: go mod tidy
darccio Mar 13, 2024
571b860
.github/workflows: run go mod tidy before appsec tests
darccio Mar 13, 2024
0a1b680
.github/workflows: run go mod tidy before docker appsec tests
darccio Mar 13, 2024
e4d7380
go.mod: go mod tidy
darccio Mar 13, 2024
0156f97
Merge remote-tracking branch 'origin/main' into vits
darccio Mar 13, 2024
f41c89d
go.mod: upgrade dd-trace-go/v2{,/contrib/99designs/gqlgen}
darccio Mar 13, 2024
06d39db
.github/workflows: run go mod tidy before appsec tests
darccio Mar 13, 2024
96c61ed
Merge remote-tracking branch 'origin' into vits
darccio Mar 13, 2024
805c59e
contrib/emicklei/go-restful.v3: change expected error message
darccio Mar 13, 2024
f84b530
contrib/google.golang.org/grpc: update TestIgnoredMethods
darccio Mar 13, 2024
dc97190
contrib/google.golang.org/grpc: update TestIgnoredMethods II
darccio Mar 13, 2024
15fa312
ddtrace/tracer: flush spans in genBigTraces
darccio Mar 13, 2024
38ce044
ddtrace/internal: introduce WrapSpan & ApplyV1FinishOptions
darccio Mar 14, 2024
68249e2
ddtrace/internal: reduce allocations in ApplyV1Options
darccio Mar 14, 2024
7207cb2
ddtrace/internal: reduce allocations in ApplyV1FinishOptions
darccio Mar 14, 2024
7c8966c
ddtrace/internal: memoize {Get,Set}GlobalTracer
darccio Mar 15, 2024
027b737
ddtrace/internal: make sure we return the concrete TracerV2Adapter va…
darccio Mar 15, 2024
6fa18c6
ddtrace/tracer: ignore tracer output
darccio Mar 15, 2024
161111e
contrib/net/http: ignore tracer output
darccio Mar 15, 2024
4844ec5
contrib/net/http: use pool to reduce memory allocations in TraceAndServe
darccio Mar 15, 2024
ee9a61b
go.mod: upgrade dd-trace-go/v2 & contribs
darccio Mar 15, 2024
c6c5c75
ddtrace/internal: fix TracerV2Adapter.StartSpan calling ApplyV1Options
darccio Mar 15, 2024
7a912d5
Merge remote-tracking branch 'origin' into vits
darccio Mar 19, 2024
20eb64a
chore: fix merge errors
darccio Mar 19, 2024
b072ed3
Merge remote-tracking branch 'origin' into vits
darccio Mar 19, 2024
84eed22
go.sum: force go mod cache miss
darccio Mar 19, 2024
06b443e
go.mod: upgrade dd-trace-go/v2 & contribs
darccio Mar 19, 2024
9c12d7b
.github/workflows/appsec.yml: test with default GOMODCACHE
darccio Mar 19, 2024
f284f36
Revert ".github/workflows/appsec.yml: test with default GOMODCACHE"
darccio Mar 19, 2024
bba4225
contrib/google.golang.org/grpc: update test
darccio Mar 20, 2024
7a9629a
contrib/google.golang.org/grpc: remove "_dd.measured" according to tr…
darccio Mar 20, 2024
390dd0b
ddtrace/mocktracer: reset global tracer on Stop
darccio Mar 20, 2024
9a1b86d
go.mod: upgrade dd-trace-go/v2 & contribs
darccio Mar 20, 2024
578878a
go.mod: upgrade dd-trace-go/v2 & contribs
darccio Mar 20, 2024
b5a7970
Reapply ".github/workflows/appsec.yml: test with default GOMODCACHE"
darccio Mar 20, 2024
6e6db3f
ddtrace: revert memoization of global tracer
darccio Mar 21, 2024
0855dc0
contrib/confluentinc/confluent-kafka-go/kafka: fix TestDeprecatedContext
darccio Mar 21, 2024
6934f7f
contrib/google.golang.org/grpc: go fmt (re: golang/go#58370)
darccio Mar 21, 2024
db804dc
Revert "contrib/confluentinc/confluent-kafka-go/kafka: fix TestDeprec…
darccio Mar 25, 2024
8db376f
.github/workflows: allow to manually run PR tests
darccio Mar 25, 2024
86f23da
.github/workflows: revert allowing to manually run PR tests
darccio Mar 25, 2024
7ba4a8b
Merge remote-tracking branch 'origin' into vits
darccio May 16, 2024
2091004
internal/appsec/config: remove test code that used telemetrytest
darccio May 17, 2024
94111d9
go.mod: go mod tidy
darccio May 17, 2024
eb5003f
v2: fix unnecessary code for vits
darccio May 17, 2024
265d09d
Merge remote-tracking branch 'origin' into vits
darccio Sep 17, 2024
52a5610
fix: make almost all tests green
darccio Sep 18, 2024
8a1d078
chore: comment profiler_version test in v1 shim
darccio Sep 18, 2024
3355619
chore: update dd-trace-go/v2
darccio Sep 18, 2024
3e43886
Merge remote-tracking branch 'origin' into vits
darccio Sep 18, 2024
1608989
chore(exectracetest): go mod tidy
darccio Sep 18, 2024
f5817ad
chore: make reviewdog happy
darccio Sep 18, 2024
1ca83b5
chore(go.mod): go mod tidy
darccio Oct 9, 2024
9e9a77a
refactor: reduce v1internal usage
darccio Oct 9, 2024
ca04959
chore: remove namingschematest stuff
darccio Oct 10, 2024
9698c25
fix: remove (almost) all references to v1internal
darccio Oct 10, 2024
507c79e
Merge remote-tracking branch 'origin' into vits
darccio Oct 11, 2024
8b37585
update to beta.5
hannahkm Nov 5, 2024
547b6ce
chore: set v1 shim dogfooding version tag
darccio Nov 5, 2024
71a33a5
chore: set v1 shim dogfooding version tag to v1.999.0-beta.5: aligned…
darccio Nov 5, 2024
4d783ee
fix: SQLCommentCarrier tests working with compatibility change in v2
darccio Nov 6, 2024
803bd8f
fix(ddtrace/tracer): allow beta versions in logging tests
darccio Nov 6, 2024
b6b2f63
chore: tag v1.999.0-beta.7
darccio Nov 6, 2024
4006612
chore(internal): go mod tidy
darccio Nov 6, 2024
9db84c8
chore: upgrade dd-trace-go/[email protected] and go mod tidy
darccio Nov 8, 2024
6911f43
chore: set version to v1.999.0-beta.8
darccio Nov 8, 2024
0f1c83a
chore: set version to v1.999.0-beta.9 and sync go.mod with v1.69.1
darccio Nov 8, 2024
569f12e
chore: go mod tidy internal apps
darccio Nov 9, 2024
298b3fa
chore: update to v2.0.0-beta.10
hannahkm Nov 13, 2024
261f0a7
chore(internal): upgrade to v1.999.0-beta.10 and tidy
hannahkm Nov 13, 2024
0031e91
chore: upgrade to v1.999.0-beta.11
hannahkm Nov 19, 2024
ac868e3
chore(internal): upgrade internal pkgs to beta.11
hannahkm Nov 19, 2024
bd92328
fix(ddtrace/tracer): account for MockspanV2Adapter in SetUser()
hannahkm Nov 19, 2024
1543ccf
Revert "fix(ddtrace/tracer): account for MockspanV2Adapter in SetUser()"
hannahkm Nov 20, 2024
9da18a2
fix(ddtrace/tracer): temporary workaround for tracer.SetUser
darccio Nov 25, 2024
2afbc4a
chore(ddtracer/tracer): add test for mockspan support in SetUser
darccio Nov 25, 2024
a3a4704
release: v1.999.0-beta.12
darccio Nov 25, 2024
effbed6
release: v1.999.0-beta.13
darccio Nov 25, 2024
a5052df
release: v1.999.0-beta.14
darccio Nov 25, 2024
7e63654
hack(profiler): filter nil options on the shim
darccio Nov 25, 2024
b1bf144
release: v1.999.0-beta.15
darccio Nov 25, 2024
a30b5c4
chore(profiler): add extra assertion to ensure we get the expected er…
darccio Nov 25, 2024
80bbee4
internal/version: v1.999.0-beta.16
darccio Nov 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
17 changes: 4 additions & 13 deletions .github/workflows/appsec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ jobs:
id: cfg
run: |
echo "key=go-pkg-mod-${{ hashFiles('**/go.sum') }}" >> $GITHUB_OUTPUT
echo "path=go_pkg_mod_cache" >> $GITHUB_OUTPUT
echo "path=$(go env GOMODCACHE)" >> $GITHUB_OUTPUT

- uses: actions/setup-go@v5
with:
Expand All @@ -87,8 +87,6 @@ jobs:

- name: Download Go modules
if: steps.cache.outputs.cache-hit != 'true'
env:
GOMODCACHE: ${{ github.workspace }}/${{ steps.cfg.outputs.path }}
run: go mod download -x

macos:
Expand Down Expand Up @@ -122,8 +120,6 @@ jobs:
# reruns under different settings.
- name: go test
shell: bash
env:
GOMODCACHE: ${{ github.workspace }}/${{ needs.go-mod-caching.outputs.path }}
run: |
set -euxo pipefail
cgocheck="GOEXPERIMENT=cgocheck2"
Expand Down Expand Up @@ -164,15 +160,11 @@ jobs:
go-version: stable
cache: false # we manage the caching ourselves

- run: go env -w GOMODCACHE=${{ github.workspace }}\${{ needs.go-mod-caching.outputs.path }}
if: runner.os == 'Windows'
- run: go env -w GOMODCACHE=${{ github.workspace }}/${{ needs.go-mod-caching.outputs.path }}
if: runner.os != 'Windows'

- name: go test
shell: bash
run: |
set -euxo pipefail
go mod tidy
for appsec_enabled_env in "" "DD_APPSEC_ENABLED=true" "DD_APPSEC_ENABLED=false"; do
for go_tags in "" "-tags datadog.no_waf"; do
if ! env $appsec_enabled_env go test -v $go_tags $TESTS; then
Expand Down Expand Up @@ -223,8 +215,6 @@ jobs:
sudo apt install -y docker-ce docker-ce-cli containerd.io

- name: Create container
env:
GOMODCACHE: ${{ github.workspace }}/${{ needs.go-mod-caching.outputs.path }}
run: |-
sudo docker run \
--rm \
Expand All @@ -241,7 +231,8 @@ jobs:
run: sudo docker exec -i test.runner apk add gcc musl-dev libc6-compat
- name: Output go env
run: sudo docker exec -i test.runner go env

- name: Ensure go.mod is up to date
run: sudo docker exec -i test.runner go mod tidy
- name: NOCGO, undefined appsec state
run: sudo docker exec -i test.runner env CGO_ENABLED=0 go test -v $TESTS
- name: NOCGO, appsec disabled
Expand Down
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"go.toolsEnvVars": {
"GOTOOLCHAIN": "go1.22.6",
"CGO_CXXFLAGS_ALLOW": "-lpthread|-ltcmalloc",
"CGO_CFLAGS_ALLOW": "-ltcmalloc",
"CGO_CPPFLAGS": "-I/opt/homebrew/Cellar/[email protected]/6.20.3/include -I/opt/homebrew/Cellar/[email protected]/6.2.30/include -I/opt/homebrew/opt/gperftools/include -DCMAKE_EXE_LINKER_FLAG"
}
}
83 changes: 6 additions & 77 deletions appsec/appsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,11 @@ package appsec

import (
"context"
"sync"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
v2 "github.com/DataDog/dd-trace-go/v2/appsec"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/appsec"
"gopkg.in/DataDog/dd-trace-go.v1/internal/appsec/emitter/httpsec"
"gopkg.in/DataDog/dd-trace-go.v1/internal/appsec/emitter/usersec"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
)

var appsecDisabledLog sync.Once

// MonitorParsedHTTPBody runs the security monitoring rules on the given *parsed*
// HTTP request body and returns if the HTTP request is suspicious and configured to be blocked.
// The given context must be the HTTP request context as returned
Expand All @@ -34,11 +27,7 @@ var appsecDisabledLog sync.Once
// result in inaccurate attack detection.
// This function always returns nil when appsec is disabled.
func MonitorParsedHTTPBody(ctx context.Context, body any) error {
if !appsec.Enabled() {
appsecDisabledLog.Do(func() { log.Warn("appsec: not enabled. Body blocking checks won't be performed.") })
return nil
}
return httpsec.MonitorParsedBody(ctx, body)
return v2.MonitorParsedHTTPBody(ctx, body)
}

// SetUser wraps tracer.SetUser() and extends it with user blocking.
Expand All @@ -50,25 +39,7 @@ func MonitorParsedHTTPBody(ctx context.Context, body any) error {
// APM tracer middleware on use according to your blocking configuration.
// This function always returns nil when appsec is disabled and doesn't block users.
func SetUser(ctx context.Context, id string, opts ...tracer.UserMonitoringOption) error {
s, ok := tracer.SpanFromContext(ctx)
if !ok {
log.Debug("appsec: could not retrieve span from context. User ID tag won't be set")
return nil
}
tracer.SetUser(s, id, opts...)
if !appsec.Enabled() {
appsecDisabledLog.Do(func() { log.Warn("appsec: not enabled. User blocking checks won't be performed.") })
return nil
}

op, errPtr := usersec.StartUserLoginOperation(ctx, usersec.UserLoginOperationArgs{})
op.Finish(usersec.UserLoginOperationRes{
UserID: id,
SessionID: getSessionID(opts...),
Success: true,
})

return *errPtr
return v2.SetUser(ctx, id, opts...)
}

// TrackUserLoginSuccessEvent sets a successful user login event, with the given
Expand All @@ -84,8 +55,7 @@ func SetUser(ctx context.Context, id string, opts ...tracer.UserMonitoringOption
// Take-Over (ATO) monitoring, ultimately blocking the IP address and/or user id
// associated to them.
func TrackUserLoginSuccessEvent(ctx context.Context, uid string, md map[string]string, opts ...tracer.UserMonitoringOption) error {
TrackCustomEvent(ctx, "users.login.success", md)
return SetUser(ctx, uid, opts...)
return v2.TrackUserLoginSuccessEvent(ctx, uid, md, opts...)
}

// TrackUserLoginFailureEvent sets a failed user login event, with the given
Expand All @@ -99,20 +69,7 @@ func TrackUserLoginSuccessEvent(ctx context.Context, uid string, md map[string]s
// Take-Over (ATO) monitoring, ultimately blocking the IP address and/or user id
// associated to them.
func TrackUserLoginFailureEvent(ctx context.Context, uid string, exists bool, md map[string]string) {
span := getRootSpan(ctx)
if span == nil {
return
}

// We need to do the first call to SetTag ourselves because the map taken by TrackCustomEvent is map[string]string
// and not map [string]any, so the `exists` boolean variable does not fit int
span.SetTag("appsec.events.users.login.failure.usr.exists", exists)
span.SetTag("appsec.events.users.login.failure.usr.id", uid)

TrackCustomEvent(ctx, "users.login.failure", md)

op, _ := usersec.StartUserLoginOperation(ctx, usersec.UserLoginOperationArgs{})
op.Finish(usersec.UserLoginOperationRes{UserID: uid, Success: false})
v2.TrackUserLoginFailureEvent(ctx, uid, exists, md)
}

// TrackCustomEvent sets a custom event as service entry span tags. This span is
Expand All @@ -122,35 +79,7 @@ func TrackUserLoginFailureEvent(ctx context.Context, uid string, exists bool, md
// Such events trigger the backend-side events monitoring ultimately blocking
// the IP address and/or user id associated to them.
func TrackCustomEvent(ctx context.Context, name string, md map[string]string) {
span := getRootSpan(ctx)
if span == nil {
return
}

tagPrefix := "appsec.events." + name + "."
span.SetTag(tagPrefix+"track", true)
span.SetTag(ext.SamplingPriority, ext.PriorityUserKeep)
for k, v := range md {
span.SetTag(tagPrefix+k, v)
}
}

// Return the root span from the span stored in the given Go context if it
// implements the Root method. It returns nil otherwise.
func getRootSpan(ctx context.Context) tracer.Span {
span, _ := tracer.SpanFromContext(ctx)
if span == nil {
log.Error("appsec: could not find a span in the given Go context")
return nil
}
type rooter interface {
Root() tracer.Span
}
if lrs, ok := span.(rooter); ok {
return lrs.Root()
}
log.Error("appsec: could not access the root span")
return nil
v2.TrackCustomEvent(ctx, name, md)
}

func getSessionID(opts ...tracer.UserMonitoringOption) string {
Expand Down
43 changes: 6 additions & 37 deletions appsec/appsec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ package appsec_test

import (
"context"
"strconv"
"testing"

"gopkg.in/DataDog/dd-trace-go.v1/appsec"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
privateAppsec "gopkg.in/DataDog/dd-trace-go.v1/internal/appsec"

"github.com/stretchr/testify/require"
)
Expand All @@ -30,7 +30,7 @@ func TestTrackUserLoginSuccessEvent(t *testing.T) {
require.Len(t, mt.FinishedSpans(), 1)
finished := mt.FinishedSpans()[0]
expectedEventPrefix := "appsec.events.users.login.success."
require.Equal(t, true, finished.Tag(expectedEventPrefix+"track"))
require.Equal(t, "true", finished.Tag(expectedEventPrefix+"track"))
require.Equal(t, "user id", finished.Tag("usr.id"))
require.Equal(t, "us-east-1", finished.Tag(expectedEventPrefix+"region"))
require.Equal(t, "username", finished.Tag("usr.name"))
Expand All @@ -48,7 +48,7 @@ func TestTrackUserLoginSuccessEvent(t *testing.T) {
require.Len(t, mt.FinishedSpans(), 1)
finished := mt.FinishedSpans()[0]
expectedEventPrefix := "appsec.events.users.login.success."
require.Equal(t, true, finished.Tag(expectedEventPrefix+"track"))
require.Equal(t, "true", finished.Tag(expectedEventPrefix+"track"))
require.Equal(t, "user id", finished.Tag("usr.id"))
})

Expand Down Expand Up @@ -80,9 +80,9 @@ func TestTrackUserLoginFailureEvent(t *testing.T) {
require.Len(t, mt.FinishedSpans(), 1)
finished := mt.FinishedSpans()[0]
expectedEventPrefix := "appsec.events.users.login.failure."
require.Equal(t, true, finished.Tag(expectedEventPrefix+"track"))
require.Equal(t, "true", finished.Tag(expectedEventPrefix+"track"))
require.Equal(t, "user id", finished.Tag(expectedEventPrefix+"usr.id"))
require.Equal(t, userExists, finished.Tag(expectedEventPrefix+"usr.exists"))
require.Equal(t, strconv.FormatBool(userExists), finished.Tag(expectedEventPrefix+"usr.exists"))
require.Equal(t, "us-east-1", finished.Tag(expectedEventPrefix+"region"))
}
}
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestCustomEvent(t *testing.T) {
require.Len(t, mt.FinishedSpans(), 1)
finished := mt.FinishedSpans()[0]
expectedEventPrefix := "appsec.events.my-custom-event."
require.Equal(t, true, finished.Tag(expectedEventPrefix+"track"))
require.Equal(t, "true", finished.Tag(expectedEventPrefix+"track"))
for k, v := range md {
require.Equal(t, v, finished.Tag(expectedEventPrefix+k))
}
Expand All @@ -136,37 +136,6 @@ func TestCustomEvent(t *testing.T) {
})
}

func TestSetUser(t *testing.T) {
t.Run("early-return/appsec-disabled", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
span, ctx := tracer.StartSpanFromContext(context.Background(), "example")
defer span.Finish()
err := appsec.SetUser(ctx, "usr.id")
require.NoError(t, err)
})

privateAppsec.Start()
defer privateAppsec.Stop()
if !privateAppsec.Enabled() {
t.Skip("AppSec needs to be enabled for this test")
}

t.Run("early-return/nil-ctx", func(t *testing.T) {
err := appsec.SetUser(nil, "usr.id")
require.NoError(t, err)
})

t.Run("no-early-return", func(t *testing.T) {
mt := mocktracer.Start()
defer mt.Stop()
span, ctx := tracer.StartSpanFromContext(context.Background(), "example")
defer span.Finish()
err := appsec.SetUser(ctx, "usr.id")
require.Nil(t, err)
})
}

func ExampleTrackUserLoginSuccessEvent() {
// Create an example span and set a user login success appsec event example to it.
span, ctx := tracer.StartSpanFromContext(context.Background(), "example")
Expand Down
Loading
Loading