question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Integers are being quoted in resulting JSON structure when passed in via the environment, resulting in Type Cast Errors in downstream systems

See original GitHub issue

Describe the bug

[2021-11-24 18:37:32 ERROR]   => failed: chaoslib.exceptions.ActivityFailed: Turbulence atttack failed: {
      "error": "Unmarshalling task type 'Stress': json: cannot unmarshal string into Go struct field StressOptions.NumCPUWorkers of type int"
    }

Is occurring in the downstream Turbulence/Stress system call behind the ChaosTK driver, this appears to be a result of the quoting scheme that chaostk/python are using for inputs passed in via the environment.

consider the following configuration definition fragment:

configuration: {
    "cpu_workers": {
      "type": "env",
      "key": "STRESS_CPU_NUM_WORKERS",
      "default": 2
    }
...
}

When no environment variable for STRESS_CPU_NUM_WORKERS is set in the environment, then the “default” value of 2 is not quoted in the resulting JSON body (correct integer handling), and we can see this in the log when the attack works properly:

[2021-11-24 18:14:40 DEBUG] [actions:30] Posting `{'Tasks': [{'Type': 'Stress', 'NumCPUWorkers': 2 ...

but as soon as I export STRESS_CPU_NUM_WORKERS=3 and re-run the experiment, I see this in the log (and the attacks fails with the very first error message described in this bug report at the top):

[2021-11-24 18:15:58 DEBUG] [actions:30] Posting `{'Tasks': [{'Type': 'Stress', 'NumCPUWorkers': '3' ...

which quotes the integer with single quotes, and that breaks the integer parsing by GO in the downstream Stress/Turbulence binary.

I have also tried this with no success either:

chaos run --no-verify-tls --var cpu_workers=3 experiments/turbulence/stress_cpu_random_job.json

How do I pass an Integer in via environment variable and have it quoted correctly in the JSON passed to Turbulence? (which would be no quoting at all for integers)

Runtime versions Please run:

# python3 --version
Python 3.6.9

# chaos info core
NAME                VERSION
CLI                 1.10.1
Core library        1.23.0

# chaos info extensions
NAME                                    VERSION   LICENSE                       DESCRIPTION
chaostoolkit-cloud-foundry              0.7.1     Apache License Version 2.0    Cloud Foundry
chaostoolkit-turbulence                 0.1.0     UNKNOWN                       Chaos Toolkit Turbulence support

To Reproduce

Define a configuration variable for a simple integer as shown above, and have it be passed along in the resulting JSON payload without quotes (proper integer handling).

Expected behavior

Integers in JSON have no quotes, so I would expect an integer value to not have quotes in the resulting JSON body

Additional context

This only seems to be a problem when the downstream system expects strict typing adherence in the JSON body.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dav1dc-pcfcommented, Dec 2, 2021

It Werkz!! 😄

Two Thumbs Up and a Tonne of Thanks Folks!! 🙏

Keep up the Great Work. 😎

1reaction
dav1dc-pcfcommented, Dec 1, 2021

Wow, that was a quick response AND a new feature ready for us to test in a debug build - I ♥️ you folks!

I can confirm that YES, this does solve our issues. A quick re-cap I added the following to all INT types in the experiment definition:

    "cpu_workers": {
      "type": "env",
      "key": "STRESS_CPU_NUM_WORKERS",
      "env_var_type": "int",
      "default": 1
    },
    "hdd_workers": {
      "type": "env",
      "key": "STRESS_HDD_NUM_WORKERS",
      "env_var_type": "int",
      "default": 1
    },
...

and then set some INT values to be passed in via the environment:

$ export STRESS_CPU_NUM_WORKERS=2
$ export STRESS_HDD_NUM_WORKERS=1
$ export STRESS_IO_NUM_WORKERS=2
$ export STRESS_RAM_NUM_WORKERS=1

and I can see in the chaostoolkit.log file that the JSON payload destined for the Turbulence API server is properly formed with respect to integer handling:

[2021-12-01 08:15:43 DEBUG] [actions:30] Posting `{'Tasks': [{'Type': 'Stress', 'NumCPUWorkers': 2, 'NumHDDWorkers': 1, 'HDDWorkerBytes': '512K', 'NumIOWorkers': 2, 'NumMemoryWorkers': 1, 'MemoryWorkerBytes': '15K', 'Timeout': '2m'}], 'Selector': {'Deployment': {'Name': 'cf-abcdefg12345678'}, 'Group': {'Name': 'diego_cell'}, 'ID': {'Limit': 1}}}` to `https://TURBULENCE_URL/api/v1/incidents`
[2021-12-01 08:15:43 DEBUG] [actions:35] attack submitted sucessfully: {
...

Well done and a quick turn around - many many thanks!

Any idea how soon this might show up in the mainline releases??

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Error when deserializing JSON to Object - Stack Overflow
The real issue here is that you are trying to deserialize into a List<object> but your JSON actually represents a single object containing...
Read more >
Solve common issues with JSON in SQL Server
I want to create a JSON text result from a simple SQL query on a single table. FOR JSON PATH and FOR JSON...
Read more >
Using JSONPath effectively in AWS Step Functions
This blog post explains how you can effectively use JSONPath in a Step Functions workflow. It shows how you can separate concerns between...
Read more >
An Exploration of JSON Interoperability Vulnerabilities
In this research, I conducted a survey of 49 JSON parsers, cataloged their quirks, and present a variety of attack scenarios and Docker...
Read more >
19 Generation of JSON Data With SQL/JSON Functions
19.1 Overview of SQL/JSON Generation Functions · A VARCHAR2 or CLOB value is wrapped in double quotation marks ( " ). · A...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found