Skip to content

Commit

Permalink
Merge pull request #1359 from flanksource/pr/http-pretty
Browse files Browse the repository at this point in the history
feat: add http pretty printing + fix auth bug
  • Loading branch information
moshloop authored Oct 18, 2023
2 parents 78fa5e0 + e118613 commit 9ee0ccd
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 7 deletions.
11 changes: 8 additions & 3 deletions checks/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
12 changes: 12 additions & 0 deletions fixtures/minimal/http_no_auth_pass.yaml
Original file line number Diff line number Diff line change
@@ -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())"
5 changes: 5 additions & 0 deletions fixtures/minimal/http_pass_single.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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/")
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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()
Expand Down

0 comments on commit 9ee0ccd

Please sign in to comment.