aws / aws/amazon-cloudwatch-agent

Exits with status code 0 when configuration JSON is malformed

Open
#966 3 comments 1 reaction 0 assignees View on GitHub
status/backlog under-investigation
Dominant language
Go
Stars
550
Forks
271
Avg merge
1d 21h
Merged PRs (30d)
13

Description

**Describe the bug**
With malformed configuration JSON file, the command `amazon-cloudwatch-agent-ctl` exits as if it succeeds.

**Steps to reproduce**
- create a configuration file with broken content, which causes JSON parse error
- execute `/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json` as a root user

**What did you expect to see?**
- the command `amazon-cloudwatch-agent-ctl` exits with non-zero status code
- the command `amazon-cloudwatch-agent-ctl` DOES NOT say `Configuration validation succeeded`

**What did you see instead?**
- the command `amazon-cloudwatch-agent-ctl` exits with 0 status code
- the command `amazon-cloudwatch-agent-ctl` DOES say `Configuration validation succeeded`

**What version did you use?**
Version: `1.300031.0b313`

**What config did you use?**
Config: any file with JSON parse error. For example, a file with only single character `a` will work for reproduction.

**Environment**
OS: Ubuntu 22.04 on EC2

**Additional context**
console log

```
root@ip-172-31-21-121:/opt/aws/amazon-cloudwatch-agent/bin# /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -s -c file:/opt/aws/amazon-cloudwatch-agent/bin/config.json
****** processing amazon-cloudwatch-agent ******
I! Trying to detect region from ec2 D! [EC2] Found active network interface I! imds retry client will retry 1 timesSuccessfully fetched the config and saved in /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_config.json.tmp
Start configuration validation...
2023/11/23 11:18:27 Reading json config file path: /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/file_config.json.tmp ...
2023/11/23 11:18:27 unable to scan config dir /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d with error: unable to parse json, error: invalid character 'a' looking for beginning of value
2023/11/23 11:18:27 No json config files found, use the default one
2023/11/23 11:18:27 I! Valid Json input schema.
2023/11/23 11:18:27 D! ec2tagger processor required because append_dimensions is set
2023/11/23 11:18:27 D! pipeline hostDeltaMetrics has no receivers
2023/11/23 11:18:27 Configuration validation first phase succeeded
I! Detecting run_as_user...
I! Trying to detect region from ec2
D! [EC2] Found active network interface
I! imds retry client will retry 1 times
/opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent -schematest -config /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.toml
Configuration validation second phase succeeded
Configuration validation succeeded
amazon-cloudwatch-agent has already been stopped

root@ip-172-31-21-121:/opt/aws/amazon-cloudwatch-agent/bin# echo $?
0

root@ip-172-31-21-121:/opt/aws/amazon-cloudwatch-agent/bin# /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status
{
"status": "stopped",
"starttime": "",
"configstatus": "configured",
"version": "1.300031.0b313"
}
```

Contributor guide

Open the contributing guide

Research direction

Run the provided amazon-cloudwatch-agent-ctl fetch-config reproduction with a malformed /opt/aws/amazon-cloudwatch-agent/bin/config.json, then trace the configuration-validation path from that command. Done means malformed JSON produces a non-zero exit status and does not report “Configuration validation succeeded.”

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.