troubleshoot icon indicating copy to clipboard operation
troubleshoot copied to clipboard

JSONCompare for `http` response body

Open dexhorthy opened this issue 1 year ago • 1 comments

Describe the rationale for the suggested feature.

I am trying to use an HTTP collector to check if a helm value is a valid authentication token. The response that comes back from

        - http:
            collectorName: helmValuesSlackUserToken
            post:
              url: https://slack.com/api/auth.test
              headers:
                Authorization: Bearer {{.Values.slack.userToken}}

unfortunately has the "successful" response body returned as serialized json:

{
  "response": {
    "status": 200,
    "body": "{\"ok\":true,\"url\":\"https:\\/\\/dexxxxxxx.slack.com\\/\",\"team\":\"dex\",\"user\":\"dexter.i.horthy\",\"team_id\":\"TTRFVVBQC\",\"user_id\":\"UTRV8CKNU\",\"is_enterprise_install\":false}",
    "headers": {
      "Access-Control-Allow-Headers": "slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, x-b3-sampled, x-b3-flags",
      "Access-Control-Allow-Origin": "*"
    }
}

(some headers omitted for brevity). I can't use the response code because it's always 200, instead I need to get the value of ok in the body. Is there a way I can deserialize that json in my troubleshoot spec?

Here's what the "failed" response looks like:

{
  "response": {
    "status": 200,
    "body": "{\"ok\":false,\"error\":\"invalid_auth\"}",
    "headers": {
      "Access-Control-Allow-Headers": "slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, x-b3-sampled, x-b3-flags",
      "Access-Control-Allow-Origin": "*"
    }
}

Describe the feature

A way to access the body of a response from an http collector from a jsonCompare analyzer. Maybe include a json field on the http object, e.g.

{
  "response": {
    "status": 200,
    "body": "{\"ok\":false,\"error\":\"invalid_auth\"}",
    "json": {"ok": false, "error": "invalid_auth"},
    "headers": {
      "Access-Control-Allow-Headers": "slack-route, x-slack-version-ts, x-b3-traceid, x-b3-spanid, x-b3-parentspanid, x-b3-sampled, x-b3-flags",
      "Access-Control-Allow-Origin": "*"
    }
}

This would match the behavior of libraries like js fetch, which returns a response object with both text() and json() methods.

I guess json would come back absent or null if it couldn't be parsed?

Describe alternatives you've considered

I guess I could hack together a nasty regex.

Additional context

Here's my full spec for reference

# values.yaml
slack:
  userToken: ""
# support-bundle.yaml
apiVersion: v1
kind: Secret
metadata:
  name: slackernews-support-bundle
  labels:
    troubleshoot.sh/kind: support-bundle
stringData: 
  support-bundle-spec: |-
    apiVersion: troubleshoot.sh/v1beta2
    kind: SupportBundle
    metadata:
      name: slackernews-support-bundle
    spec:
      collectors:
        - http:
            collectorName: helmValuesSlackUserToken
            post:
              url: https://slack.com/api/auth.test
              headers:
                Authorization: Bearer {{.Values.slack.userToken}}
      analyzers:
        - jsonCompare:
            checkName: Slack User Token
            fileName: helmValuesSlackUserToken.json
            path: "ok"
            value: |
               true
            outcomes:
              - fail:
                  when: "false"
                  message: No Valid Slack User token in helm values
              - pass:
                  when: "true"
                  message: Slack User Token valid

dexhorthy avatar Jan 15 '24 19:01 dexhorthy