Skip to content

[golang] AAP: enable response body schema tests for echo, gin, net-http #4760

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion manifests/golang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ tests/:
net-http: irrelevant (net-http cannot list path params)
net-http-orchestrion: irrelevant (net-http cannot list path params)
Test_Schema_Request_Query_Parameters: v1.60.0
Test_Schema_Response_Body: missing_feature
Test_Schema_Response_Body:
'*': missing_feature
echo: v2.1.0-dev # Fully transparent
gin: v2.1.0-dev # Using response helper function
net-http: v2.1.0-dev # Using SDK-style
Test_Schema_Response_Body_env_var: v2.0.0
Test_Schema_Response_Headers: v1.60.0
Test_Schema_Response_on_Block: missing_feature
Expand Down
22 changes: 17 additions & 5 deletions utils/build/docker/golang/app/echo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"os"
Expand Down Expand Up @@ -127,16 +126,29 @@ func main() {
}
}

var bodyMap map[string]any
switch {
case c.Request().Header.Get("Content-Type") == "application/json":
body, _ := io.ReadAll(c.Request().Body)
var bodyMap map[string]any
if err := json.Unmarshal(body, &bodyMap); err == nil {
appsec.MonitorParsedHTTPBody(c.Request().Context(), bodyMap)
dec := json.NewDecoder(c.Request().Body)
dec.UseNumber()
if err := dec.Decode(&bodyMap); err != nil {
return err
}
appsec.MonitorParsedHTTPBody(c.Request().Context(), bodyMap)
case c.Request().ParseForm() == nil:
bodyMap = make(map[string]any) // Bind assumes this is non-nil...
if err := c.Bind(&bodyMap); err != nil {
return err
}
appsec.MonitorParsedHTTPBody(c.Request().Context(), c.Request().PostForm)
default:
logrus.Warnf("Unsupported request content-type: %q", c.Request().Header.Get("Content-Type"))
}

if c.Request().Method == http.MethodPost && strings.HasPrefix(tag, "payload_in_response_body") {
return c.JSON(status, map[string]any{"payload": bodyMap})
}

return c.String(status, "Value tagged")
})

Expand Down
31 changes: 23 additions & 8 deletions utils/build/docker/golang/app/gin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"net/http"
"os"
Expand Down Expand Up @@ -114,18 +113,34 @@ func main() {
ctx.Writer.Header().Add(key, value)
}
}
ctx.Writer.WriteHeader(status)
ctx.Writer.Write([]byte("Value tagged"))
var bodyMap map[string]any
switch {
case ctx.Request.Header.Get("Content-Type") == "application/json":
body, _ := io.ReadAll(ctx.Request.Body)
var bodyMap map[string]any
if err := json.Unmarshal(body, &bodyMap); err == nil {
appsec.MonitorParsedHTTPBody(ctx.Request.Context(), bodyMap)
dec := json.NewDecoder(ctx.Request.Body)
dec.UseNumber()
if err := dec.Decode(&bodyMap); err != nil {
logrus.Errorf("Error decoding request JSON body: %v", err)
ctx.Error(err)
return
}
appsec.MonitorParsedHTTPBody(ctx.Request.Context(), bodyMap)
case ctx.Request.ParseForm() == nil:
appsec.MonitorParsedHTTPBody(ctx.Request.Context(), ctx.Request.PostForm)
bodyMap = make(map[string]any, len(ctx.Request.PostForm))
for key, value := range ctx.Request.PostForm {
bodyMap[key] = value
}
appsec.MonitorParsedHTTPBody(ctx.Request.Context(), bodyMap)
default:
logrus.Warnf("Unsupported request content-type: %q", ctx.Request.Header.Get("Content-Type"))
}

if ctx.Request.Method == http.MethodPost && strings.HasPrefix(tag, "payload_in_response_body") {
gintrace.JSON(ctx, status, map[string]any{"payload": bodyMap})
return
}

ctx.Writer.WriteHeader(status)
ctx.Writer.Write([]byte("Value tagged"))
})

r.Any("/status", func(ctx *gin.Context) {
Expand Down
35 changes: 17 additions & 18 deletions utils/build/docker/golang/app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ require (
github.com/DataDog/dd-trace-go/contrib/99designs/gqlgen/v2 v2.0.0
github.com/DataDog/dd-trace-go/contrib/IBM/sarama/v2 v2.0.0
github.com/DataDog/dd-trace-go/contrib/database/sql/v2 v2.0.0
github.com/DataDog/dd-trace-go/contrib/gin-gonic/gin/v2 v2.0.0
github.com/DataDog/dd-trace-go/contrib/gin-gonic/gin/v2 v2.1.0-dev.1.0.20250611144049-f92a95e4313d
github.com/DataDog/dd-trace-go/contrib/go-chi/chi.v5/v2 v2.0.0
github.com/DataDog/dd-trace-go/contrib/google.golang.org/grpc/v2 v2.0.0
github.com/DataDog/dd-trace-go/contrib/graph-gophers/graphql-go/v2 v2.0.0
github.com/DataDog/dd-trace-go/contrib/graphql-go/graphql/v2 v2.0.0
github.com/DataDog/dd-trace-go/contrib/labstack/echo.v4/v2 v2.0.0
github.com/DataDog/dd-trace-go/contrib/net/http/v2 v2.0.0
github.com/DataDog/dd-trace-go/contrib/sirupsen/logrus/v2 v2.0.0
github.com/DataDog/dd-trace-go/v2 v2.0.0
github.com/DataDog/dd-trace-go/v2 v2.1.0-dev.1.0.20250611144049-f92a95e4313d
github.com/IBM/sarama v1.40.0
github.com/gin-gonic/gin v1.9.1
github.com/go-chi/chi/v5 v5.2.1
Expand All @@ -29,23 +29,23 @@ require (
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0
go.opentelemetry.io/otel v1.35.0
go.opentelemetry.io/otel/trace v1.35.0
google.golang.org/grpc v1.71.0
google.golang.org/grpc v1.71.1
google.golang.org/protobuf v1.36.6
)

require (
github.com/DataDog/appsec-internal-go v1.11.2 // indirect
github.com/DataDog/datadog-agent/comp/core/tagger/origindetection v0.64.1 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.64.1 // indirect
github.com/DataDog/datadog-agent/pkg/proto v0.64.1 // indirect
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.64.1 // indirect
github.com/DataDog/datadog-agent/pkg/trace v0.64.1 // indirect
github.com/DataDog/datadog-agent/pkg/util/log v0.64.1 // indirect
github.com/DataDog/datadog-agent/pkg/util/scrubber v0.64.1 // indirect
github.com/DataDog/datadog-agent/pkg/version v0.64.1 // indirect
github.com/DataDog/appsec-internal-go v1.12.0 // indirect
github.com/DataDog/datadog-agent/comp/core/tagger/origindetection v0.66.0 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.66.0 // indirect
github.com/DataDog/datadog-agent/pkg/proto v0.66.0 // indirect
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.66.0 // indirect
github.com/DataDog/datadog-agent/pkg/trace v0.66.0 // indirect
github.com/DataDog/datadog-agent/pkg/util/log v0.66.0 // indirect
github.com/DataDog/datadog-agent/pkg/util/scrubber v0.66.0 // indirect
github.com/DataDog/datadog-agent/pkg/version v0.66.0 // indirect
github.com/DataDog/datadog-go/v5 v5.6.0 // indirect
github.com/DataDog/go-libddwaf/v3 v3.5.4 // indirect
github.com/DataDog/go-runtime-metrics-internal v0.0.4-0.20250319104955-81009b9bad14 // indirect
github.com/DataDog/go-libddwaf/v4 v4.2.0 // indirect
github.com/DataDog/go-runtime-metrics-internal v0.0.4-0.20250603194815-7edb7c2ad56a // indirect
github.com/DataDog/go-sqllexer v0.1.6 // indirect
github.com/DataDog/go-tuf v1.1.0-0.5.2 // indirect
github.com/DataDog/gostackparse v0.7.0 // indirect
Expand Down Expand Up @@ -113,7 +113,6 @@ require (
github.com/puzpuzpuz/xsync/v3 v3.5.1 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/richardartoul/molecule v1.0.1-0.20240531184615-7ca0df43c0b3 // indirect
github.com/rogpeppe/go-internal v1.14.1 // indirect
github.com/secure-systems-lab/go-securesystemslib v0.9.0 // indirect
github.com/shirou/gopsutil/v4 v4.25.2 // indirect
github.com/sosodev/duration v1.3.1 // indirect
Expand All @@ -136,12 +135,12 @@ require (
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/exp v0.0.0-20250210185358-939b2ce775ac // indirect
golang.org/x/mod v0.24.0 // indirect
golang.org/x/net v0.38.0 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/sys v0.33.0 // indirect
golang.org/x/text v0.23.0 // indirect
golang.org/x/text v0.24.0 // indirect
golang.org/x/time v0.11.0 // indirect
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250324211829-b45e905df463 // indirect
Expand Down
Loading
Loading