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/_context/_scenarios/parametric.py b/utils/_context/_scenarios/parametric.py index ba5ca69d37..09e874a399 100644 --- a/utils/_context/_scenarios/parametric.py +++ b/utils/_context/_scenarios/parametric.py @@ -149,7 +149,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, @@ -422,6 +422,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 aa94ca2e3d..0bbfd3e2e1 100644 --- a/utils/_context/containers.py +++ b/utils/_context/containers.py @@ -681,7 +681,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 --max-time 2 localhost:{self.port}/healthcheck", "retries": 60, @@ -715,13 +715,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/chi/main.go b/utils/build/docker/golang/app/chi/main.go index 0bbfc0ae3a..b8da109b40 100644 --- a/utils/build/docker/golang/app/chi/main.go +++ b/utils/build/docker/golang/app/chi/main.go @@ -5,6 +5,7 @@ import ( "log" "net/http" "os" + "encoding/json" "strconv" "time" "weblog/internal/rasp" @@ -35,6 +36,23 @@ func main() { w.Write([]byte("Hello, WAF!\n")) }) + mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { + + 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 + } + + 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..0517ba115b 100644 --- a/utils/build/docker/golang/app/echo/main.go +++ b/utils/build/docker/golang/app/echo/main.go @@ -29,6 +29,16 @@ func main() { return c.NoContent(http.StatusOK) }) + r.GET("/healthcheck", func(c echo.Context) error { + healthCheck, err := common.GetHealtchCheck() + + if err != nil { + return c.JSON(http.StatusInternalServerError, err) + } + + return c.JSON(http.StatusOK, healthCheck) + }) + 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..507c787959 100644 --- a/utils/build/docker/golang/app/gin/main.go +++ b/utils/build/docker/golang/app/gin/main.go @@ -27,6 +27,17 @@ func main() { r.Any("/", func(ctx *gin.Context) { ctx.Writer.WriteHeader(http.StatusOK) }) + + r.GET("/healthcheck", func(ctx *gin.Context) { + healthCheck, err := common.GetHealtchCheck() + + if err != nil { + ctx.JSON(http.StatusInternalServerError, err) + } + + ctx.JSON(http.StatusOK, healthCheck) + }) + 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..b3a6ae3408 100644 --- a/utils/build/docker/golang/app/gqlgen/server.go +++ b/utils/build/docker/golang/app/gqlgen/server.go @@ -2,6 +2,7 @@ package main import ( "net/http" + "encoding/json" "weblog/gqlgen/graph" "weblog/internal/common" @@ -36,6 +37,24 @@ func main() { w.WriteHeader(http.StatusOK) }) + mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { + + healthCheck, err := common.GetHealtchCheck() + if err != nil { + http.Error(w, "Can't get 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) + }) + 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..728610a126 100644 --- a/utils/build/docker/golang/app/graph-gophers/main.go +++ b/utils/build/docker/golang/app/graph-gophers/main.go @@ -2,6 +2,7 @@ package main import ( "net/http" + "encoding/json" "weblog/internal/common" graphqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/graph-gophers/graphql-go" @@ -48,6 +49,23 @@ func main() { w.WriteHeader(http.StatusOK) }) + mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { + + 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 + } + + 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..67a691282a 100644 --- a/utils/build/docker/golang/app/graphql-go/main.go +++ b/utils/build/docker/golang/app/graphql-go/main.go @@ -2,6 +2,7 @@ package main import ( "net/http" + "encoding/json" "weblog/internal/common" graphqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/graphql-go/graphql" @@ -72,6 +73,24 @@ func main() { w.WriteHeader(http.StatusOK) }) + mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { + + 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 + } + + 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/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 70ca102639..6c5dfd4b3b 100644 --- a/utils/build/docker/golang/app/net-http/main.go +++ b/utils/build/docker/golang/app/net-http/main.go @@ -49,6 +49,23 @@ func main() { w.WriteHeader(http.StatusOK) }) + mux.HandleFunc("/healthcheck", func(w http.ResponseWriter, r *http.Request) { + + 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 + } + + 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