Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
contrib/gin-gonic: add AppSec support to gin framework (#1165)
- Add a call to useAppsec in gintrace.go when AppSec is enabled - Add testing of AppSec functionalities in gintrace_test.go - Add appsec.go file that holds AppSec handling for gin
- Loading branch information
Showing
3 changed files
with
221 additions
and
1 deletion.
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed | ||
// under the Apache License Version 2.0. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
// Copyright 2022 Datadog, Inc. | ||
|
||
package gin | ||
|
||
import ( | ||
"net" | ||
|
||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
"gopkg.in/DataDog/dd-trace-go.v1/internal/appsec/dyngo/instrumentation/httpsec" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// useAppSec executes the AppSec logic related to the operation start and | ||
// returns the function to be executed upon finishing the operation | ||
func useAppSec(c *gin.Context, span tracer.Span) func() { | ||
req := c.Request | ||
httpsec.SetAppSecTags(span) | ||
var params map[string]string | ||
if l := len(c.Params); l > 0 { | ||
params = make(map[string]string, l) | ||
for _, p := range c.Params { | ||
params[p.Key] = p.Value | ||
} | ||
} | ||
args := httpsec.MakeHandlerOperationArgs(req, params) | ||
op := httpsec.StartOperation(args, nil) | ||
return func() { | ||
events := op.Finish(httpsec.HandlerOperationRes{Status: c.Writer.Status()}) | ||
if len(events) > 0 { | ||
remoteIP, _, err := net.SplitHostPort(req.RemoteAddr) | ||
if err != nil { | ||
remoteIP = req.RemoteAddr | ||
} | ||
httpsec.SetSecurityEventTags(span, events, remoteIP, args.Headers, c.Writer.Header()) | ||
} | ||
} | ||
} |
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