Skip to content

Retain custom environment variables in env-config.json between translation#2131

Open
jefchien wants to merge 6 commits into
mainfrom
retain-envconfig
Open

Retain custom environment variables in env-config.json between translation#2131
jefchien wants to merge 6 commits into
mainfrom
retain-envconfig

Conversation

@jefchien

Copy link
Copy Markdown
Contributor

Description of the issue

When the config translator runs (e.g. on fetch-config), it overwrites env-config.json entirely. Any values written to that file outside of translation are lost. This means custom environment variables are not retained.

Description of changes

Adds file-level helpers to the existing cfg/envconfig package (ReadEnvConfigFile, LoadEnvConfigFile, MergeEnvConfigFile, ReplaceEnvConfigFile) that centralize all env-config.json I/O.

The key behavioral change is in TranslateJSONMapToEnvConfigFile. Instead of overwriting env-config.json, it now calls ReplaceEnvConfigFile with toenvconfig.ManagedKeys, the explicit set of keys that translation owns. On each translation, managed keys are reset to reflect the current JSON config (clearing stale values), while unmanaged keys are preserved.

License

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Tests

Added unit tests. Built and tested on EC2 instance.

Requirements

Before commiting your code, please do the following steps.

  1. Run make fmt and make fmt-sh
  2. Run make lint

Integration Tests

To run integration tests against this PR, add the ready for testing label.

@jefchien jefchien requested a review from a team as a code owner May 28, 2026 14:28
@jefchien jefchien added the ready for testing Indicates this PR is ready for integration tests to run label May 28, 2026
JayPolanco
JayPolanco previously approved these changes May 28, 2026
Comment thread cmd/amazon-cloudwatch-agent/amazon-cloudwatch-agent.go Outdated
@jefchien jefchien force-pushed the retain-envconfig branch from bc795a3 to 0a5f49e Compare May 28, 2026 20:23
Comment thread cfg/envconfig/file.go

// MergeEnvConfigFile merges the given values into the env-config.json at path.
// Existing values are retained; the provided values take precedence.
func MergeEnvConfigFile(path string, values map[string]string) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: is this just to clearly distinguish between merge and replace? seems unnecessary wrapper when the end result is always replace.

Comment thread cfg/envconfig/file.go
if err != nil {
return err
}
return os.WriteFile(path, data, 0644) //nolint:gosec // G306: 0644 is intentional for env-config.json

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why 0644?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the permissions that the env-config.json currently has. Don't want to introduce a potential regression by changing it.

Comment thread cfg/envconfig/file.go
}
for k, v := range envVars {
if os.Setenv(k, v) == nil && visitor != nil {
visitor(k, v)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we log out if we fail to setEnv ?

Comment thread cfg/envconfig/file.go

// ReplaceEnvConfigFile merges the given values into the env-config.json at path,
// first removing keysToRemove so stale values don't persist.
func ReplaceEnvConfigFile(path string, values map[string]string, keysToRemove []string) error {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that this function is NOT atomic so it can technically corrupt the env.json via a race condition. I think it should be okay since we have the 0644; however, if the user sets to run CWAgent as X user, is it possible for that user to right to the env-config.json and cause the race condition?

Comment thread cfg/envconfig/file.go
}

// MergeEnvConfigFile merges the given values into the env-config.json at path.
// Existing values are retained; the provided values take precedence.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would say overwrite rather than precedence

@github-actions

Copy link
Copy Markdown
Contributor

This PR was marked stale due to lack of activity.

@github-actions github-actions Bot added the Stale label Jun 11, 2026
@github-actions github-actions Bot removed the Stale label Jun 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR was marked stale due to lack of activity.

@github-actions github-actions Bot added the Stale label Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready for testing Indicates this PR is ready for integration tests to run Stale

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants