From e118613e1c27a8ea65987d1d1cbe3a00081843d6 Mon Sep 17 00:00:00 2001 From: Moshe Immerman Date: Wed, 18 Oct 2023 14:00:57 +0300 Subject: [PATCH] feat: add http pretty printing + fix auth bug --- checks/http.go | 11 ++++++++--- fixtures/minimal/http_no_auth_pass.yaml | 12 ++++++++++++ fixtures/minimal/http_pass_single.yaml | 5 +++++ go.mod | 2 +- go.sum | 4 ++-- main.go | 4 +++- 6 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 fixtures/minimal/http_no_auth_pass.yaml diff --git a/checks/http.go b/checks/http.go index 45606ea63..e37253d22 100644 --- a/checks/http.go +++ b/checks/http.go @@ -19,6 +19,7 @@ import ( v1 "github.com/flanksource/canary-checker/api/v1" "github.com/flanksource/canary-checker/pkg" "github.com/flanksource/canary-checker/pkg/metrics" + "github.com/flanksource/canary-checker/pkg/runner" "github.com/flanksource/canary-checker/pkg/utils" ) @@ -63,7 +64,7 @@ func (c *HTTPChecker) Run(ctx *context.Context) pkg.Results { } func (c *HTTPChecker) generateHTTPRequest(ctx *context.Context, check v1.HTTPCheck, connection *models.Connection) (*http.Request, error) { - client := http.NewClient() + client := http.NewClient().UserAgent("canary-checker/" + runner.Version) for _, header := range check.Headers { value, err := ctx.GetEnvValueFromCache(header) @@ -91,7 +92,11 @@ func (c *HTTPChecker) generateHTTPRequest(ctx *context.Context, check v1.HTTPChe // TODO: Add finer controls over tracing to the canary if ctx.IsTrace() { - client.Trace(http.TraceConfig{MaxBodyLength: 512, Body: true, Headers: true, ResponseHeaders: true}) + client.TraceToStdout(http.TraceAll) + client.Trace(http.TraceAll) + } else if ctx.IsDebug() { + client.TraceToStdout(http.TraceHeaders) + client.Trace(http.TraceHeaders) } return client.R(ctx), nil @@ -199,7 +204,7 @@ func (c *HTTPChecker) Check(ctx *context.Context, extConfig external.Check) pkg. data := map[string]interface{}{ "code": status, - "headers": response.Header, + "headers": response.GetHeaders(), "elapsed": time.Since(start), "sslAge": utils.Deref(age), "json": make(map[string]any), diff --git a/fixtures/minimal/http_no_auth_pass.yaml b/fixtures/minimal/http_no_auth_pass.yaml new file mode 100644 index 000000000..8d4ec6c09 --- /dev/null +++ b/fixtures/minimal/http_no_auth_pass.yaml @@ -0,0 +1,12 @@ +apiVersion: canaries.flanksource.com/v1 +kind: Canary +metadata: + name: http-no-auth + annotations: + trace: "true" +spec: + http: + - name: http-no-auth + url: https://httpbin.demo.aws.flanksource.com/headers + test: + expr: "! ('Authorization' in json.headers.keys())" diff --git a/fixtures/minimal/http_pass_single.yaml b/fixtures/minimal/http_pass_single.yaml index 7a6f2ebcc..dab245877 100644 --- a/fixtures/minimal/http_pass_single.yaml +++ b/fixtures/minimal/http_pass_single.yaml @@ -33,3 +33,8 @@ spec: expr: "code in [200,201,301] && sslAge > Duration('7d')" display: template: "code={{.code}}, age={{.sslAge}}" + + - name: http-headers + url: https://httpbin.demo.aws.flanksource.com/headers + test: + expr: json.headers["User-Agent"].startsWith("canary-checker/") diff --git a/go.mod b/go.mod index 4598e39e3..f2e1c0c4a 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/eko/gocache/store/bigcache/v4 v4.2.1 github.com/elastic/go-elasticsearch/v8 v8.10.0 github.com/fergusstrange/embedded-postgres v1.24.0 - github.com/flanksource/commons v1.15.0 + github.com/flanksource/commons v1.16.0 github.com/flanksource/duty v1.0.191 github.com/flanksource/gomplate/v3 v3.20.18 github.com/flanksource/is-healthy v0.0.0-20231003215854-76c51e3a3ff7 diff --git a/go.sum b/go.sum index a41ee9728..b5c489da8 100644 --- a/go.sum +++ b/go.sum @@ -819,8 +819,8 @@ github.com/evanphx/json-patch/v5 v5.7.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fergusstrange/embedded-postgres v1.24.0 h1:WqXbmYrBeT5JfNWQ8Qa+yHa5YJO/0sBIgL9k5rn3dFk= github.com/fergusstrange/embedded-postgres v1.24.0/go.mod h1:wL562t1V+iuFwq0UcgMi2e9rp8CROY9wxWZEfP8Y874= -github.com/flanksource/commons v1.15.0 h1:p74hrKzIz0r3H8YN3CuB8ePJOjzPFO0BRLVmpXmeqvY= -github.com/flanksource/commons v1.15.0/go.mod h1:FMZFLcQr98JwBKuKLs44DvCQ2JNoHz5maRIzVufQ9Cs= +github.com/flanksource/commons v1.16.0 h1:8kxeP9gPAuCKHNxLosi1uk+qIrZFs62YIzfkkneazTg= +github.com/flanksource/commons v1.16.0/go.mod h1:RDdQI0/QYC4GzicbDaXIvBPjWuQWKLzX8/rFBbFjG5U= github.com/flanksource/duty v1.0.191 h1:acnvyTeQlfqmtyXxWprNFGK/vBTUlqkYwxEPLtXSPrk= github.com/flanksource/duty v1.0.191/go.mod h1:ikyl/TcRy6Cc0R5b0wEHT7CecV7gyJvrDGq/4oIZHoc= github.com/flanksource/gomplate/v3 v3.20.4/go.mod h1:27BNWhzzSjDed1z8YShO6W+z6G9oZXuxfNFGd/iGSdc= diff --git a/main.go b/main.go index 9d8364e34..473988a4f 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,9 @@ var ( func main() { if len(commit) > 8 { version = fmt.Sprintf("%v, commit %v, built at %v", version, commit[0:8], date) + runner.Version = fmt.Sprintf("%v (%v)", version, commit[0:8]) + } else { + runner.Version = version } cmd.Root.AddCommand(&cobra.Command{ @@ -30,7 +33,6 @@ func main() { }, }) - runner.Version = version cmd.Root.SetUsageTemplate(cmd.Root.UsageTemplate() + fmt.Sprintf("\nversion: %s\n ", version)) defer func() { err := db.StopServer()