From d2bb10a8604049264fbf78df2e992f2aa28c3378 Mon Sep 17 00:00:00 2001 From: Charles de Beauchesne Date: Mon, 2 Sep 2024 09:27:48 +0200 Subject: [PATCH 1/8] [golang] Use /healthcheck to get library version #2799 --- utils/_context/_scenarios/parametric.py | 3 +- utils/_context/containers.py | 7 ++-- .../build/docker/golang/app/net-http/main.go | 34 +++++++++++++++++++ .../system_tests_library_version.sh | 3 ++ 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100755 utils/build/docker/golang/parametric/system_tests_library_version.sh diff --git a/utils/_context/_scenarios/parametric.py b/utils/_context/_scenarios/parametric.py index ab1e776c0e..593c04d488 100644 --- a/utils/_context/_scenarios/parametric.py +++ b/utils/_context/_scenarios/parametric.py @@ -150,7 +150,7 @@ def configure(self, config): self._clean_networks() # https://github.com/DataDog/system-tests/issues/2799 - if library in ("nodejs", "python"): + if library in ("nodejs", "python", "golang"): output = _get_client().containers.run( self.apm_test_server_definition.container_tag, remove=True, @@ -423,6 +423,7 @@ def golang_library_factory(): COPY {golang_reldir}/. /app # 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 RUN /binaries/install_ddtrace.sh RUN go install diff --git a/utils/_context/containers.py b/utils/_context/containers.py index 8cc25abac9..7f964570a5 100644 --- a/utils/_context/containers.py +++ b/utils/_context/containers.py @@ -668,7 +668,7 @@ def configure(self, replay): ) # https://github.com/DataDog/system-tests/issues/2799 - if self.library in ("nodejs", "python"): + if self.library in ("nodejs", "python", "golang"): self.healthcheck = { "test": f"curl --fail --silent --show-error localhost:{self.port}/healthcheck", "retries": 60, @@ -702,13 +702,14 @@ def post_start(self): # new way of getting info from the weblog. Only working for nodejs and python right now # https://github.com/DataDog/system-tests/issues/2799 - if self.library in ("nodejs", "python"): + if self.library in ("nodejs", "python", "golang"): with open(self.healthcheck_log_file, mode="r", encoding="utf-8") as f: data = json.load(f) lib = data["library"] self._library = LibraryVersion(lib["language"], lib["version"]) - self.libddwaf_version = LibraryVersion("libddwaf", lib["libddwaf_version"]).version + if "libddwaf_version" in lib: + self.libddwaf_version = LibraryVersion("libddwaf", lib["libddwaf_version"]).version if self.appsec_rules_version == "0.0.0" and "appsec_event_rules_version" in lib: self.appsec_rules_version = LibraryVersion("appsec_rules", lib["appsec_event_rules_version"]).version diff --git a/utils/build/docker/golang/app/net-http/main.go b/utils/build/docker/golang/app/net-http/main.go index 70ca102639..2822ef3ac6 100644 --- a/utils/build/docker/golang/app/net-http/main.go +++ b/utils/build/docker/golang/app/net-http/main.go @@ -49,6 +49,40 @@ func main() { w.WriteHeader(http.StatusOK) }) + mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { + tracerVersion, err := os.ReadFile("SYSTEM_TESTS_LIBRARY_VERSION") + if err != nil { + http.Error(w, "Can't get SYSTEM_TESTS_LIBRARY_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), + } + + 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) + }) + mux.HandleFunc("/waf", func(w http.ResponseWriter, r *http.Request) { body, err := common.ParseBody(r) if err == nil { diff --git a/utils/build/docker/golang/parametric/system_tests_library_version.sh b/utils/build/docker/golang/parametric/system_tests_library_version.sh new file mode 100755 index 0000000000..d132b39b18 --- /dev/null +++ b/utils/build/docker/golang/parametric/system_tests_library_version.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +cat SYSTEM_TESTS_LIBRARY_VERSION From f10b1378a296c89e714fe01e83aa414e63b6495f Mon Sep 17 00:00:00 2001 From: Charles de Beauchesne Date: Mon, 9 Sep 2024 15:05:19 +0200 Subject: [PATCH 2/8] Other weblogs --- utils/_context/_scenarios/parametric.py | 1 + utils/build/docker/golang/app/chi/main.go | 50 +++++++++++++++++ utils/build/docker/golang/app/echo/main.go | 52 ++++++++++++++++++ utils/build/docker/golang/app/gin/main.go | 53 +++++++++++++++++++ .../build/docker/golang/app/gqlgen/server.go | 50 +++++++++++++++++ .../docker/golang/app/graph-gophers/main.go | 50 +++++++++++++++++ .../docker/golang/app/graphql-go/main.go | 51 ++++++++++++++++++ .../build/docker/golang/app/net-http/main.go | 24 +++++++-- utils/build/docker/golang/install_ddtrace.sh | 7 ++- .../system_tests_library_version.go | 29 ++++++++++ .../system_tests_library_version.sh | 2 +- 11 files changed, 360 insertions(+), 9 deletions(-) create mode 100755 utils/build/docker/golang/parametric/system_tests_library_version.go diff --git a/utils/_context/_scenarios/parametric.py b/utils/_context/_scenarios/parametric.py index 593c04d488..0f2ff60dfa 100644 --- a/utils/_context/_scenarios/parametric.py +++ b/utils/_context/_scenarios/parametric.py @@ -424,6 +424,7 @@ 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/system_tests_library_version.go 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 0bbfc0ae3a..52b5dc0a55 100644 --- a/utils/build/docker/golang/app/chi/main.go +++ b/utils/build/docker/golang/app/chi/main.go @@ -1,6 +1,7 @@ package main import ( + "runtime/debug" "context" "log" "net/http" @@ -35,6 +36,55 @@ func main() { w.Write([]byte("Hello, WAF!\n")) }) + 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") + 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) + }) + mux.HandleFunc("/waf/*", func(w http.ResponseWriter, r *http.Request) { body, err := common.ParseBody(r) if err == nil { diff --git a/utils/build/docker/golang/app/echo/main.go b/utils/build/docker/golang/app/echo/main.go index 4489a7759c..4611529d32 100644 --- a/utils/build/docker/golang/app/echo/main.go +++ b/utils/build/docker/golang/app/echo/main.go @@ -1,6 +1,7 @@ package main import ( + "runtime/debug" "log" "net/http" "os" @@ -17,6 +18,18 @@ 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() @@ -29,6 +42,45 @@ func main() { return c.NoContent(http.StatusOK) }) + 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) + + 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 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) + }) + r.Any("/*", func(c echo.Context) error { return c.NoContent(http.StatusNotFound) }) diff --git a/utils/build/docker/golang/app/gin/main.go b/utils/build/docker/golang/app/gin/main.go index f79a40f24c..f61af36256 100644 --- a/utils/build/docker/golang/app/gin/main.go +++ b/utils/build/docker/golang/app/gin/main.go @@ -1,6 +1,7 @@ package main import ( + "runtime/debug" "log" "net/http" "os" @@ -17,6 +18,18 @@ 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() @@ -27,6 +40,46 @@ func main() { r.Any("/", func(ctx *gin.Context) { ctx.Writer.WriteHeader(http.StatusOK) }) + + 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) + + 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 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) + }) + r.Any("/waf", func(ctx *gin.Context) { body, err := common.ParseBody(ctx.Request) if err == nil { diff --git a/utils/build/docker/golang/app/gqlgen/server.go b/utils/build/docker/golang/app/gqlgen/server.go index e62bbf6d11..64f739dd2f 100644 --- a/utils/build/docker/golang/app/gqlgen/server.go +++ b/utils/build/docker/golang/app/gqlgen/server.go @@ -1,6 +1,7 @@ package main import ( + "runtime/debug" "net/http" "weblog/gqlgen/graph" "weblog/internal/common" @@ -36,6 +37,55 @@ func main() { w.WriteHeader(http.StatusOK) }) + 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") + 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) + }) + common.InitDatadog() panic(http.ListenAndServe(":7777", mux)) diff --git a/utils/build/docker/golang/app/graph-gophers/main.go b/utils/build/docker/golang/app/graph-gophers/main.go index 166fba8bb7..df05702ef4 100644 --- a/utils/build/docker/golang/app/graph-gophers/main.go +++ b/utils/build/docker/golang/app/graph-gophers/main.go @@ -1,6 +1,7 @@ package main import ( + "runtime/debug" "net/http" "weblog/internal/common" @@ -48,6 +49,55 @@ func main() { w.WriteHeader(http.StatusOK) }) + 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") + 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) + }) + common.InitDatadog() panic(http.ListenAndServe(":7777", mux)) diff --git a/utils/build/docker/golang/app/graphql-go/main.go b/utils/build/docker/golang/app/graphql-go/main.go index dca2c8358d..470982674b 100644 --- a/utils/build/docker/golang/app/graphql-go/main.go +++ b/utils/build/docker/golang/app/graphql-go/main.go @@ -1,6 +1,7 @@ package main import ( + "runtime/debug" "net/http" "weblog/internal/common" @@ -72,6 +73,56 @@ func main() { w.WriteHeader(http.StatusOK) }) + 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") + 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) + }) + + common.InitDatadog() panic(http.ListenAndServe(":7777", mux)) diff --git a/utils/build/docker/golang/app/net-http/main.go b/utils/build/docker/golang/app/net-http/main.go index 2822ef3ac6..3b9e9fea76 100644 --- a/utils/build/docker/golang/app/net-http/main.go +++ b/utils/build/docker/golang/app/net-http/main.go @@ -1,6 +1,7 @@ package main import ( + "runtime/debug" "context" "encoding/json" "errors" @@ -50,9 +51,23 @@ func main() { }) mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { - tracerVersion, err := os.ReadFile("SYSTEM_TESTS_LIBRARY_VERSION") - if err != nil { - http.Error(w, "Can't get SYSTEM_TESTS_LIBRARY_VERSION", http.StatusInternalServerError) + 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 } @@ -66,6 +81,7 @@ func main() { "language": "golang", "version": string(tracerVersion), "appsec_event_rules_version": string(appsec_rules_version), + "libddwaf_version": libddwafVersion, } data := map[string]interface{}{ @@ -75,7 +91,7 @@ func main() { jsonData, err := json.Marshal(data) if err != nil { - http.Error(w, "Can;t build JSON data", http.StatusInternalServerError) + 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 bc9decf857..d6020d5fa4 100755 --- a/utils/build/docker/golang/install_ddtrace.sh +++ b/utils/build/docker/golang/install_ddtrace.sh @@ -20,10 +20,9 @@ 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 $version > SYSTEM_TESTS_LIBRARY_VERSION - -touch 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 "0.0.0" > SYSTEM_TESTS_LIBRARY_VERSION +echo "0.0.0" > 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/system_tests_library_version.go b/utils/build/docker/golang/parametric/system_tests_library_version.go new file mode 100755 index 0000000000..3f0dd23cf2 --- /dev/null +++ b/utils/build/docker/golang/parametric/system_tests_library_version.go @@ -0,0 +1,29 @@ +package main + +import ( + "fmt" + "runtime/debug" + "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" +) + + +func main() { + moduleName := "gopkg.in/DataDog/dd-trace-go.v1" + + // Obtenir les informations de build (dont les versions des modules) + buildInfo, ok := debug.ReadBuildInfo() + if !ok { + fmt.Println("Can't read build informations.\n") + os.Exit(1) + } + + for _, mod := range buildInfo.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 d132b39b18..4d205f051a 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 -cat SYSTEM_TESTS_LIBRARY_VERSION +go system_tests_library_version.go From 384e80770f37311fab31334c4d319efa95bf2c6f Mon Sep 17 00:00:00 2001 From: Charles de Beauchesne Date: Mon, 9 Sep 2024 15:23:14 +0200 Subject: [PATCH 3/8] Fix import --- .github/workflows/run-graphql.yml | 2 +- utils/build/docker/golang/app/chi/main.go | 1 + utils/build/docker/golang/app/gqlgen/server.go | 2 ++ utils/build/docker/golang/app/graph-gophers/main.go | 2 ++ utils/build/docker/golang/app/graphql-go/main.go | 2 ++ .../docker/golang/parametric/system_tests_library_version.go | 1 + 6 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/run-graphql.yml b/.github/workflows/run-graphql.yml index e3966fb386..be610f9705 100644 --- a/.github/workflows/run-graphql.yml +++ b/.github/workflows/run-graphql.yml @@ -79,7 +79,7 @@ jobs: - name: Compress logs id: compress_logs - if: always() + if: always() && steps.build.outcome == 'success' run: tar -czvf artifact.tar.gz $(ls | grep logs) - name: Upload artifact if: always() && steps.compress_logs.outcome == 'success' diff --git a/utils/build/docker/golang/app/chi/main.go b/utils/build/docker/golang/app/chi/main.go index 52b5dc0a55..a08d60c524 100644 --- a/utils/build/docker/golang/app/chi/main.go +++ b/utils/build/docker/golang/app/chi/main.go @@ -6,6 +6,7 @@ import ( "log" "net/http" "os" + "encoding/json" "strconv" "time" "weblog/internal/rasp" diff --git a/utils/build/docker/golang/app/gqlgen/server.go b/utils/build/docker/golang/app/gqlgen/server.go index 64f739dd2f..d790678622 100644 --- a/utils/build/docker/golang/app/gqlgen/server.go +++ b/utils/build/docker/golang/app/gqlgen/server.go @@ -3,6 +3,8 @@ package main import ( "runtime/debug" "net/http" + "os" + "encoding/json" "weblog/gqlgen/graph" "weblog/internal/common" diff --git a/utils/build/docker/golang/app/graph-gophers/main.go b/utils/build/docker/golang/app/graph-gophers/main.go index df05702ef4..2b46cbd0c0 100644 --- a/utils/build/docker/golang/app/graph-gophers/main.go +++ b/utils/build/docker/golang/app/graph-gophers/main.go @@ -3,6 +3,8 @@ package main import ( "runtime/debug" "net/http" + "os" + "encoding/json" "weblog/internal/common" graphqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/graph-gophers/graphql-go" diff --git a/utils/build/docker/golang/app/graphql-go/main.go b/utils/build/docker/golang/app/graphql-go/main.go index 470982674b..52dd2b6243 100644 --- a/utils/build/docker/golang/app/graphql-go/main.go +++ b/utils/build/docker/golang/app/graphql-go/main.go @@ -3,6 +3,8 @@ package main import ( "runtime/debug" "net/http" + "os" + "encoding/json" "weblog/internal/common" graphqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/graphql-go/graphql" diff --git a/utils/build/docker/golang/parametric/system_tests_library_version.go b/utils/build/docker/golang/parametric/system_tests_library_version.go index 3f0dd23cf2..f1edb37ea6 100755 --- a/utils/build/docker/golang/parametric/system_tests_library_version.go +++ b/utils/build/docker/golang/parametric/system_tests_library_version.go @@ -1,6 +1,7 @@ package main import ( + "os" "fmt" "runtime/debug" "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" From eaf333c2328f2b87c7e036d2905e24d8dbf31aec Mon Sep 17 00:00:00 2001 From: Charles de Beauchesne Date: Mon, 9 Sep 2024 15:39:59 +0200 Subject: [PATCH 4/8] Typos --- utils/build/docker/golang/app/chi/main.go | 2 +- utils/build/docker/golang/app/gqlgen/server.go | 2 +- utils/build/docker/golang/app/graph-gophers/main.go | 2 +- utils/build/docker/golang/app/graphql-go/main.go | 2 +- utils/build/docker/golang/app/net-http/main.go | 2 +- .../docker/golang/parametric/system_tests_library_version.go | 3 +-- 6 files changed, 6 insertions(+), 7 deletions(-) diff --git a/utils/build/docker/golang/app/chi/main.go b/utils/build/docker/golang/app/chi/main.go index a08d60c524..a5f171c0e9 100644 --- a/utils/build/docker/golang/app/chi/main.go +++ b/utils/build/docker/golang/app/chi/main.go @@ -48,7 +48,7 @@ func main() { 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 + libddwafVersion = mod.Version } } } diff --git a/utils/build/docker/golang/app/gqlgen/server.go b/utils/build/docker/golang/app/gqlgen/server.go index d790678622..6a8d309c15 100644 --- a/utils/build/docker/golang/app/gqlgen/server.go +++ b/utils/build/docker/golang/app/gqlgen/server.go @@ -50,7 +50,7 @@ func main() { 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 + libddwafVersion = mod.Version } } } diff --git a/utils/build/docker/golang/app/graph-gophers/main.go b/utils/build/docker/golang/app/graph-gophers/main.go index 2b46cbd0c0..263a43ee59 100644 --- a/utils/build/docker/golang/app/graph-gophers/main.go +++ b/utils/build/docker/golang/app/graph-gophers/main.go @@ -62,7 +62,7 @@ func main() { 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 + libddwafVersion = mod.Version } } } diff --git a/utils/build/docker/golang/app/graphql-go/main.go b/utils/build/docker/golang/app/graphql-go/main.go index 52dd2b6243..d81fd13bf7 100644 --- a/utils/build/docker/golang/app/graphql-go/main.go +++ b/utils/build/docker/golang/app/graphql-go/main.go @@ -86,7 +86,7 @@ func main() { 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 + libddwafVersion = mod.Version } } } diff --git a/utils/build/docker/golang/app/net-http/main.go b/utils/build/docker/golang/app/net-http/main.go index 3b9e9fea76..bde13c8c85 100644 --- a/utils/build/docker/golang/app/net-http/main.go +++ b/utils/build/docker/golang/app/net-http/main.go @@ -61,7 +61,7 @@ func main() { 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 + libddwafVersion = mod.Version } } } diff --git a/utils/build/docker/golang/parametric/system_tests_library_version.go b/utils/build/docker/golang/parametric/system_tests_library_version.go index f1edb37ea6..c261c98599 100755 --- a/utils/build/docker/golang/parametric/system_tests_library_version.go +++ b/utils/build/docker/golang/parametric/system_tests_library_version.go @@ -1,10 +1,9 @@ -package main +package getversion import ( "os" "fmt" "runtime/debug" - "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" ) From e1755a2c8f26d90b8b5956e5839fc3b1f65f8101 Mon Sep 17 00:00:00 2001 From: Charles de Beauchesne Date: Mon, 9 Sep 2024 16:14:39 +0200 Subject: [PATCH 5/8] fix parametric test --- utils/_context/_scenarios/parametric.py | 2 +- .../system_tests_library_version.go | 23 +++++++++++++++ .../system_tests_library_version.go | 29 ------------------- .../system_tests_library_version.sh | 2 +- 4 files changed, 25 insertions(+), 31 deletions(-) create mode 100755 utils/build/docker/golang/parametric/get_version/system_tests_library_version.go delete mode 100755 utils/build/docker/golang/parametric/system_tests_library_version.go diff --git a/utils/_context/_scenarios/parametric.py b/utils/_context/_scenarios/parametric.py index 22f9228adb..1a982bdb2c 100644 --- a/utils/_context/_scenarios/parametric.py +++ b/utils/_context/_scenarios/parametric.py @@ -423,7 +423,7 @@ 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/system_tests_library_version.go system_tests_library_version.go +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/parametric/get_version/system_tests_library_version.go b/utils/build/docker/golang/parametric/get_version/system_tests_library_version.go new file mode 100755 index 0000000000..0c8d6c10d1 --- /dev/null +++ b/utils/build/docker/golang/parametric/get_version/system_tests_library_version.go @@ -0,0 +1,23 @@ +package get_version + +import ( + "os" + "fmt" + "runtime/debug" +) + +func main() { + 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.go b/utils/build/docker/golang/parametric/system_tests_library_version.go deleted file mode 100755 index c261c98599..0000000000 --- a/utils/build/docker/golang/parametric/system_tests_library_version.go +++ /dev/null @@ -1,29 +0,0 @@ -package getversion - -import ( - "os" - "fmt" - "runtime/debug" -) - - -func main() { - moduleName := "gopkg.in/DataDog/dd-trace-go.v1" - - // Obtenir les informations de build (dont les versions des modules) - buildInfo, ok := debug.ReadBuildInfo() - if !ok { - fmt.Println("Can't read build informations.\n") - os.Exit(1) - } - - for _, mod := range buildInfo.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 4d205f051a..eefbc2d33b 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 system_tests_library_version.go +go run get_version/system_tests_library_version.go From 57bcc77ce331f020c8e40a94236d6ea4aef76a65 Mon Sep 17 00:00:00 2001 From: Charles de Beauchesne Date: Mon, 9 Sep 2024 16:42:51 +0200 Subject: [PATCH 6/8] Fix parametric --- .../parametric/get_version/system_tests_library_version.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) 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 index 0c8d6c10d1..e743969220 100755 --- 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 @@ -1,12 +1,15 @@ -package get_version +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 { From 43c760d58f1160b714505b45ffdb765faaf5a53a Mon Sep 17 00:00:00 2001 From: Charles de Beauchesne Date: Mon, 9 Sep 2024 19:08:11 +0200 Subject: [PATCH 7/8] Get back to simpler mode --- utils/_context/_scenarios/parametric.py | 1 - utils/build/docker/golang/app/chi/main.go | 39 +------------ utils/build/docker/golang/app/echo/main.go | 50 ++-------------- utils/build/docker/golang/app/gin/main.go | 50 ++-------------- .../build/docker/golang/app/gqlgen/server.go | 55 ++++-------------- .../docker/golang/app/graph-gophers/main.go | 58 ++++--------------- .../docker/golang/app/graphql-go/main.go | 40 +------------ .../golang/app/internal/common/common.go | 49 ++++++++++++++++ .../build/docker/golang/app/net-http/main.go | 39 +------------ utils/build/docker/golang/install_ddtrace.sh | 7 ++- .../system_tests_library_version.go | 26 --------- .../system_tests_library_version.sh | 2 +- 12 files changed, 94 insertions(+), 322 deletions(-) delete mode 100755 utils/build/docker/golang/parametric/get_version/system_tests_library_version.go 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 From d164c7d60deb1d95c2a82f3f4fd4f74adc39e3eb Mon Sep 17 00:00:00 2001 From: Charles de Beauchesne Date: Mon, 9 Sep 2024 20:00:23 +0200 Subject: [PATCH 8/8] fix gin --- utils/build/docker/golang/app/gin/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/build/docker/golang/app/gin/main.go b/utils/build/docker/golang/app/gin/main.go index 54f1a0fe86..507c787959 100644 --- a/utils/build/docker/golang/app/gin/main.go +++ b/utils/build/docker/golang/app/gin/main.go @@ -32,10 +32,10 @@ func main() { healthCheck, err := common.GetHealtchCheck() if err != nil { - c.JSON(http.StatusInternalServerError, err) + ctx.JSON(http.StatusInternalServerError, err) } - c.JSON(http.StatusOK, healthCheck) + ctx.JSON(http.StatusOK, healthCheck) }) r.Any("/waf", func(ctx *gin.Context) {