diff --git a/utils/_context/_scenarios/parametric.py b/utils/_context/_scenarios/parametric.py index 1a982bdb2c..09e874a399 100644 --- a/utils/_context/_scenarios/parametric.py +++ b/utils/_context/_scenarios/parametric.py @@ -423,7 +423,6 @@ def golang_library_factory(): # download the proper tracer version COPY utils/build/docker/golang/install_ddtrace.sh binaries* /binaries/ COPY utils/build/docker/golang/parametric/system_tests_library_version.sh system_tests_library_version.sh -COPY utils/build/docker/golang/parametric/get_version/system_tests_library_version.go get_version/system_tests_library_version.go RUN /binaries/install_ddtrace.sh RUN go install diff --git a/utils/build/docker/golang/app/chi/main.go b/utils/build/docker/golang/app/chi/main.go index a5f171c0e9..b8da109b40 100644 --- a/utils/build/docker/golang/app/chi/main.go +++ b/utils/build/docker/golang/app/chi/main.go @@ -1,7 +1,6 @@ package main import ( - "runtime/debug" "context" "log" "net/http" @@ -38,45 +37,13 @@ func main() { }) mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { - var tracerVersion string - var libddwafVersion string - - if bi, ok := debug.ReadBuildInfo(); ok { - for _, mod := range bi.Deps { - println(mod.Path, mod.Version) - - if mod.Path == "gopkg.in/DataDog/dd-trace-go.v1" { - tracerVersion = mod.Version - } else if mod.Path == "github.com/DataDog/go-libddwaf/v3" { - libddwafVersion = mod.Version - } - } - } - - if tracerVersion == "" { - http.Error(w, "Can't get dd-trace-go version", http.StatusInternalServerError) - return - } - appsec_rules_version, err := os.ReadFile("SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION") + healthCheck, err := common.GetHealtchCheck() if err != nil { - http.Error(w, "Can't get SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION", http.StatusInternalServerError) - return - } - - libray := map[string]interface{}{ - "language": "golang", - "version": string(tracerVersion), - "appsec_event_rules_version": string(appsec_rules_version), - "libddwaf_version": libddwafVersion, - } - - data := map[string]interface{}{ - "status": "ok", - "library": libray, + http.Error(w, "Can't get JSON data", http.StatusInternalServerError) } - jsonData, err := json.Marshal(data) + jsonData, err := json.Marshal(healthCheck) if err != nil { http.Error(w, "Can't build JSON data", http.StatusInternalServerError) return diff --git a/utils/build/docker/golang/app/echo/main.go b/utils/build/docker/golang/app/echo/main.go index 4611529d32..0517ba115b 100644 --- a/utils/build/docker/golang/app/echo/main.go +++ b/utils/build/docker/golang/app/echo/main.go @@ -1,7 +1,6 @@ package main import ( - "runtime/debug" "log" "net/http" "os" @@ -18,18 +17,6 @@ import ( "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" ) -type HealtchCheckLibrary struct { - Language string `json:"language"` - Version string `json:"version"` - AppsecEventRulesVersion string `json:"appsec_event_rules_version"` - LibddwafVersion string `json:"libddwaf_version"` -} - -type HealtchCheck struct { - Status string `json:"status"` - Library HealtchCheckLibrary `json:"library"` -} - func main() { tracer.Start() defer tracer.Stop() @@ -43,42 +30,13 @@ func main() { }) r.GET("/healthcheck", func(c echo.Context) error { - library := HealtchCheckLibrary{ - Language: "golang", - Version: "", - AppsecEventRulesVersion: "", - LibddwafVersion: "", - } - - result := HealtchCheck{ - Status: "ok", - Library: library, - } - - if bi, ok := debug.ReadBuildInfo(); ok { - for _, mod := range bi.Deps { - println(mod.Path, mod.Version) + healthCheck, err := common.GetHealtchCheck() - if mod.Path == "gopkg.in/DataDog/dd-trace-go.v1" { - library.Version = mod.Version - } else if mod.Path == "github.com/DataDog/go-libddwaf/v3" { - library.LibddwafVersion = mod.Version - } - } + if err != nil { + return c.JSON(http.StatusInternalServerError, err) } - - if library.Version == "" { - return c.JSON(http.StatusInternalServerError, "Can't get dd-trace-go version") - } - - appsecEventRulesVersion, err := os.ReadFile("SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION") - if err != nil { - return c.JSON(http.StatusInternalServerError, "Can't get SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION") - } - - library.AppsecEventRulesVersion = string(appsecEventRulesVersion) - return c.JSON(http.StatusOK, result) + return c.JSON(http.StatusOK, healthCheck) }) r.Any("/*", func(c echo.Context) error { diff --git a/utils/build/docker/golang/app/gin/main.go b/utils/build/docker/golang/app/gin/main.go index f61af36256..54f1a0fe86 100644 --- a/utils/build/docker/golang/app/gin/main.go +++ b/utils/build/docker/golang/app/gin/main.go @@ -1,7 +1,6 @@ package main import ( - "runtime/debug" "log" "net/http" "os" @@ -18,18 +17,6 @@ import ( "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" ) -type HealtchCheckLibrary struct { - Language string `json:"language"` - Version string `json:"version"` - AppsecEventRulesVersion string `json:"appsec_event_rules_version"` - LibddwafVersion string `json:"libddwaf_version"` -} - -type HealtchCheck struct { - Status string `json:"status"` - Library HealtchCheckLibrary `json:"library"` -} - func main() { tracer.Start() defer tracer.Stop() @@ -42,42 +29,13 @@ func main() { }) r.GET("/healthcheck", func(ctx *gin.Context) { - library := HealtchCheckLibrary{ - Language: "golang", - Version: "", - AppsecEventRulesVersion: "", - LibddwafVersion: "", - } - - result := HealtchCheck{ - Status: "ok", - Library: library, - } - - if bi, ok := debug.ReadBuildInfo(); ok { - for _, mod := range bi.Deps { - println(mod.Path, mod.Version) + healthCheck, err := common.GetHealtchCheck() - if mod.Path == "gopkg.in/DataDog/dd-trace-go.v1" { - library.Version = mod.Version - } else if mod.Path == "github.com/DataDog/go-libddwaf/v3" { - library.LibddwafVersion = mod.Version - } - } + if err != nil { + c.JSON(http.StatusInternalServerError, err) } - - if library.Version == "" { - ctx.JSON(http.StatusInternalServerError, "Can't get dd-trace-go version") - } - - appsecEventRulesVersion, err := os.ReadFile("SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION") - if err != nil { - ctx.JSON(http.StatusInternalServerError, "Can't get SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION") - } - - library.AppsecEventRulesVersion = string(appsecEventRulesVersion) - ctx.JSON(http.StatusOK, result) + c.JSON(http.StatusOK, healthCheck) }) r.Any("/waf", func(ctx *gin.Context) { diff --git a/utils/build/docker/golang/app/gqlgen/server.go b/utils/build/docker/golang/app/gqlgen/server.go index 6a8d309c15..b3a6ae3408 100644 --- a/utils/build/docker/golang/app/gqlgen/server.go +++ b/utils/build/docker/golang/app/gqlgen/server.go @@ -1,9 +1,7 @@ package main import ( - "runtime/debug" "net/http" - "os" "encoding/json" "weblog/gqlgen/graph" "weblog/internal/common" @@ -40,52 +38,21 @@ func main() { }) mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { - var tracerVersion string - var libddwafVersion string - if bi, ok := debug.ReadBuildInfo(); ok { - for _, mod := range bi.Deps { - println(mod.Path, mod.Version) - - if mod.Path == "gopkg.in/DataDog/dd-trace-go.v1" { - tracerVersion = mod.Version - } else if mod.Path == "github.com/DataDog/go-libddwaf/v3" { - libddwafVersion = mod.Version - } - } + healthCheck, err := common.GetHealtchCheck() + if err != nil { + http.Error(w, "Can't get JSON data", http.StatusInternalServerError) + return } - if tracerVersion == "" { - http.Error(w, "Can't get dd-trace-go version", http.StatusInternalServerError) - return - } - - appsec_rules_version, err := os.ReadFile("SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION") - if err != nil { - http.Error(w, "Can't get SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION", http.StatusInternalServerError) - return - } - - libray := map[string]interface{}{ - "language": "golang", - "version": string(tracerVersion), - "appsec_event_rules_version": string(appsec_rules_version), - "libddwaf_version": libddwafVersion, - } - - data := map[string]interface{}{ - "status": "ok", - "library": libray, - } - - jsonData, err := json.Marshal(data) - if err != nil { - http.Error(w, "Can't build JSON data", http.StatusInternalServerError) - return - } + jsonData, err := json.Marshal(healthCheck) + if err != nil { + http.Error(w, "Can't build JSON data", http.StatusInternalServerError) + return + } - w.Header().Set("Content-Type", "application/json") - w.Write(jsonData) + w.Header().Set("Content-Type", "application/json") + w.Write(jsonData) }) common.InitDatadog() diff --git a/utils/build/docker/golang/app/graph-gophers/main.go b/utils/build/docker/golang/app/graph-gophers/main.go index 263a43ee59..728610a126 100644 --- a/utils/build/docker/golang/app/graph-gophers/main.go +++ b/utils/build/docker/golang/app/graph-gophers/main.go @@ -1,9 +1,7 @@ package main import ( - "runtime/debug" "net/http" - "os" "encoding/json" "weblog/internal/common" @@ -52,52 +50,20 @@ func main() { }) mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { - var tracerVersion string - var libddwafVersion string - - if bi, ok := debug.ReadBuildInfo(); ok { - for _, mod := range bi.Deps { - println(mod.Path, mod.Version) - - if mod.Path == "gopkg.in/DataDog/dd-trace-go.v1" { - tracerVersion = mod.Version - } else if mod.Path == "github.com/DataDog/go-libddwaf/v3" { - libddwafVersion = mod.Version - } - } + + healthCheck, err := common.GetHealtchCheck() + if err != nil { + http.Error(w, "Can't get JSON data", http.StatusInternalServerError) + } + + jsonData, err := json.Marshal(healthCheck) + if err != nil { + http.Error(w, "Can't build JSON data", http.StatusInternalServerError) + return } - if tracerVersion == "" { - http.Error(w, "Can't get dd-trace-go version", http.StatusInternalServerError) - return - } - - appsec_rules_version, err := os.ReadFile("SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION") - if err != nil { - http.Error(w, "Can't get SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION", http.StatusInternalServerError) - return - } - - libray := map[string]interface{}{ - "language": "golang", - "version": string(tracerVersion), - "appsec_event_rules_version": string(appsec_rules_version), - "libddwaf_version": libddwafVersion, - } - - data := map[string]interface{}{ - "status": "ok", - "library": libray, - } - - jsonData, err := json.Marshal(data) - if err != nil { - http.Error(w, "Can't build JSON data", http.StatusInternalServerError) - return - } - - w.Header().Set("Content-Type", "application/json") - w.Write(jsonData) + w.Header().Set("Content-Type", "application/json") + w.Write(jsonData) }) common.InitDatadog() diff --git a/utils/build/docker/golang/app/graphql-go/main.go b/utils/build/docker/golang/app/graphql-go/main.go index d81fd13bf7..67a691282a 100644 --- a/utils/build/docker/golang/app/graphql-go/main.go +++ b/utils/build/docker/golang/app/graphql-go/main.go @@ -1,9 +1,7 @@ package main import ( - "runtime/debug" "net/http" - "os" "encoding/json" "weblog/internal/common" @@ -76,45 +74,13 @@ func main() { }) mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { - var tracerVersion string - var libddwafVersion string - - if bi, ok := debug.ReadBuildInfo(); ok { - for _, mod := range bi.Deps { - println(mod.Path, mod.Version) - - if mod.Path == "gopkg.in/DataDog/dd-trace-go.v1" { - tracerVersion = mod.Version - } else if mod.Path == "github.com/DataDog/go-libddwaf/v3" { - libddwafVersion = mod.Version - } - } - } - - if tracerVersion == "" { - http.Error(w, "Can't get dd-trace-go version", http.StatusInternalServerError) - return - } - appsec_rules_version, err := os.ReadFile("SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION") + healthCheck, err := common.GetHealtchCheck() if err != nil { - http.Error(w, "Can't get SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION", http.StatusInternalServerError) - return - } - - libray := map[string]interface{}{ - "language": "golang", - "version": string(tracerVersion), - "appsec_event_rules_version": string(appsec_rules_version), - "libddwaf_version": libddwafVersion, - } - - data := map[string]interface{}{ - "status": "ok", - "library": libray, + http.Error(w, "Can't get JSON data", http.StatusInternalServerError) } - jsonData, err := json.Marshal(data) + jsonData, err := json.Marshal(healthCheck) if err != nil { http.Error(w, "Can't build JSON data", http.StatusInternalServerError) return diff --git a/utils/build/docker/golang/app/internal/common/common.go b/utils/build/docker/golang/app/internal/common/common.go index 9776de7581..c2fa244d74 100644 --- a/utils/build/docker/golang/app/internal/common/common.go +++ b/utils/build/docker/golang/app/internal/common/common.go @@ -1,6 +1,8 @@ package common import ( + "os" + "errors" "encoding/json" "encoding/xml" "io" @@ -11,6 +13,20 @@ import ( "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" ) + +type DatadogInformations struct { + Language string `json:"language"` + Version string `json:"version"` + AppsecEventRulesVersion string `json:"appsec_event_rules_version"` + LibddwafVersion string `json:"libddwaf_version"` +} + +type HealtchCheck struct { + Status string `json:"status"` + Library DatadogInformations `json:"library"` +} + + func InitDatadog() { span := tracer.StartSpan("init.service") defer span.Finish() @@ -52,3 +68,36 @@ func ForceSpanIndexingTags() []ddtrace.StartSpanOption { tracer.Tag("_dd.filter.id", "system_tests_e2e"), } } + + +func GetHealtchCheck() (HealtchCheck, error) { + datadogInformations, err := GetDatadogInformations() + + if err != nil { + return HealtchCheck{}, err + } + + return HealtchCheck{ + Status: "ok", + Library: datadogInformations, + }, nil +} + +func GetDatadogInformations() (DatadogInformations, error) { + + tracerVersion, err := os.ReadFile("SYSTEM_TESTS_LIBRARY_VERSION") + if err != nil { + return DatadogInformations{}, errors.New("Can't get SYSTEM_TESTS_LIBRARY_VERSION") + } + + appsecRulesVersion, err := os.ReadFile("SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION") + if err != nil { + return DatadogInformations{}, errors.New("Can't get SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION") + } + + return DatadogInformations{ + Language: "golang", + Version: string(tracerVersion), + AppsecEventRulesVersion: string(appsecRulesVersion), + }, nil +} \ No newline at end of file diff --git a/utils/build/docker/golang/app/net-http/main.go b/utils/build/docker/golang/app/net-http/main.go index bde13c8c85..6c5dfd4b3b 100644 --- a/utils/build/docker/golang/app/net-http/main.go +++ b/utils/build/docker/golang/app/net-http/main.go @@ -1,7 +1,6 @@ package main import ( - "runtime/debug" "context" "encoding/json" "errors" @@ -51,45 +50,13 @@ func main() { }) mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { - var tracerVersion string - var libddwafVersion string - - if bi, ok := debug.ReadBuildInfo(); ok { - for _, mod := range bi.Deps { - println(mod.Path, mod.Version) - - if mod.Path == "gopkg.in/DataDog/dd-trace-go.v1" { - tracerVersion = mod.Version - } else if mod.Path == "github.com/DataDog/go-libddwaf/v3" { - libddwafVersion = mod.Version - } - } - } - - if tracerVersion == "" { - http.Error(w, "Can't get dd-trace-go version", http.StatusInternalServerError) - return - } - appsec_rules_version, err := os.ReadFile("SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION") + healthCheck, err := common.GetHealtchCheck() if err != nil { - http.Error(w, "Can't get SYSTEM_TESTS_APPSEC_EVENT_RULES_VERSION", http.StatusInternalServerError) - return - } - - libray := map[string]interface{}{ - "language": "golang", - "version": string(tracerVersion), - "appsec_event_rules_version": string(appsec_rules_version), - "libddwaf_version": libddwafVersion, - } - - data := map[string]interface{}{ - "status": "ok", - "library": libray, + http.Error(w, "Can't get JSON data", http.StatusInternalServerError) } - jsonData, err := json.Marshal(data) + jsonData, err := json.Marshal(healthCheck) if err != nil { http.Error(w, "Can't build JSON data", http.StatusInternalServerError) return diff --git a/utils/build/docker/golang/install_ddtrace.sh b/utils/build/docker/golang/install_ddtrace.sh index d6020d5fa4..bc9decf857 100755 --- a/utils/build/docker/golang/install_ddtrace.sh +++ b/utils/build/docker/golang/install_ddtrace.sh @@ -20,9 +20,10 @@ go mod tidy # Read the library version out of the version.go file lib_mod_dir=$(go list -f '{{.Dir}}' -m gopkg.in/DataDog/dd-trace-go.v1) -# version=$(sed -nrE 's#.*"v(.*)".*#\1#p' $lib_mod_dir/internal/version/version.go) # Parse the version string content "v.*" -echo "0.0.0" > SYSTEM_TESTS_LIBRARY_VERSION -echo "0.0.0" > SYSTEM_TESTS_LIBDDWAF_VERSION +version=$(sed -nrE 's#.*"v(.*)".*#\1#p' $lib_mod_dir/internal/version/version.go) # Parse the version string content "v.*" +echo $version > SYSTEM_TESTS_LIBRARY_VERSION + +touch SYSTEM_TESTS_LIBDDWAF_VERSION rules_mod_dir=$(go list -f '{{.Dir}}' -m github.com/DataDog/appsec-internal-go) diff --git a/utils/build/docker/golang/parametric/get_version/system_tests_library_version.go b/utils/build/docker/golang/parametric/get_version/system_tests_library_version.go deleted file mode 100755 index e743969220..0000000000 --- a/utils/build/docker/golang/parametric/get_version/system_tests_library_version.go +++ /dev/null @@ -1,26 +0,0 @@ -package main - -import ( - "os" - "fmt" - "runtime/debug" - "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" -) - -func main() { - tracer.Start() - defer tracer.Stop() - moduleName := "gopkg.in/DataDog/dd-trace-go.v1" - - if bi, ok := debug.ReadBuildInfo(); ok { - for _, mod := range bi.Deps { - if mod.Path == moduleName { - fmt.Printf(mod.Version) - return - } - } - } - - fmt.Printf("Module %s not found.\n", moduleName) - os.Exit(1) -} diff --git a/utils/build/docker/golang/parametric/system_tests_library_version.sh b/utils/build/docker/golang/parametric/system_tests_library_version.sh index eefbc2d33b..d132b39b18 100755 --- a/utils/build/docker/golang/parametric/system_tests_library_version.sh +++ b/utils/build/docker/golang/parametric/system_tests_library_version.sh @@ -1,3 +1,3 @@ #!/bin/bash -go run get_version/system_tests_library_version.go +cat SYSTEM_TESTS_LIBRARY_VERSION