Add --base64 option to decode base64-encoded JSON responses

Add --base64 option to decode base64-encoded JSON responses

- Introduced the --base64 argument to the CLI.
- When specified, the plugin decodes the HTTP response body from base64 before parsing it as JSON.
- Maintains existing behavior if --base64 is not used.

Example usage with Passwork:
  ./check_http_json.py -vv -H passwork.intranet -p api/v1/app/health-check -X POST \
    --header '{"Accept": "application/json", "Content-Type": "application/json"}' \
    --data '{"token": "0123456789ABCD"}' --ssl --base64

  Passwork responds with:
    {
      "format": "base64",
      "content": "eyJzdGF0ZXMiOnsibW9uZ29kYiI6Im9rIiwidGFza3MiOiJvayJ9LCJlcnJvcnMiOltdfQ=="
    }

  The script decodes the 'content' field and parses the resulting JSON.

# ./check_http_json.py -vv -H passwork.intranet -p api/v1/app/health-check -X POST --header '{"Accept": "application/json", "Content-Type": "application/json"}' --data '{"token": "0123456789ABCD"}' --ssl --base64 content -Q states.mongodb,ok states.tasks,ok errors,[] 
{
  "states": {
    "mongodb": "ok",
    "tasks": "ok"
  },
  "errors": []
}
OK: Status OK.
This commit is contained in:
Grischa Zengel 2025-05-15 20:16:41 +02:00 committed by GitHub
parent ccf05d469a
commit 8d24e42026
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -529,6 +529,7 @@ def parseArgs(args):
help='The http payload to send as a POST')
parser.add_argument('-A', '--headers', dest='headers',
help='The http headers in JSON format.')
parser.add_argument('--base64', metavar='PATH', help='JSON path to base64 encoded content (dot notation, e.g., data.content)')
parser.add_argument('-f', '--field_separator', dest='separator',
help='''JSON Field separator, defaults to ".";
Select element in an array with "(" ")"''')
@ -741,8 +742,21 @@ def main(cliargs):
sys.exit(nagios.getCode())
try:
# Loading the JSON data from the request
data = json.loads(json_data)
if args.base64:
# Parse the HTTP response (json_data) as JSON into a Python dictionary
data = json.loads(json_data)
# Split the --base64 argument (dot notation) into a list of keys for nested access
path = args.base64.split('.')
# Traverse the dictionary to reach the nested base64-encoded string
for key in path:
data = data[key]
# Decode the base64 string to get the original JSON string
decoded = base64.b64decode(data)
# Parse the decoded JSON string into a Python dictionary
data = json.loads(decoded)
else:
# If --base64 is not set, just parse the HTTP response as JSON
data = json.loads(json_data)
except ValueError as e:
exit_code = args.invalid_json_state
debugPrint(args.debug, traceback.format_exc())