Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change environment prefix from nms to nginx_agent #706

Merged
merged 11 commits into from
Jun 17, 2024
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func main() {
defer logFile.Close()
}

if config.MigratedEnv {
log.Warnf("The environment variable prefix 'NMS' is deprecated. Prefix has been migrated to 'NGINX_AGENT'. Please update your configuration to use the new prefix.")
}

log.Tracef("Config loaded from disk, %v", loadedConfig)

if loadedConfig.DisplayName == "" {
Expand Down
16 changes: 15 additions & 1 deletion src/core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ const (
`
)

var Viper = viper.NewWithOptions(viper.KeyDelimiter(agent_config.KeyDelimiter))
var (
Viper = viper.NewWithOptions(viper.KeyDelimiter(agent_config.KeyDelimiter))
MigratedEnv = false
)

func SetVersion(version, commit string) {
ROOT_COMMAND.Version = version + "-" + commit
Expand Down Expand Up @@ -149,6 +152,17 @@ func RegisterFlags() {
if err := Viper.BindPFlag(strings.ReplaceAll(flag.Name, "-", "_"), fs.Lookup(flag.Name)); err != nil {
return
}

oldKey := strings.ToUpper(LegacyEnvPrefix + agent_config.KeyDelimiter + strings.ReplaceAll(flag.Name, "-", agent_config.KeyDelimiter))
newKey := strings.ToUpper(EnvPrefix + agent_config.KeyDelimiter + strings.ReplaceAll(flag.Name, "-", agent_config.KeyDelimiter))

if os.Getenv(oldKey) != "" && os.Getenv(newKey) == "" {
Copy link
Contributor

Choose a reason for hiding this comment

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

What about the (admittedly unlikely) case where both oldKey and newKey are set?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If both oldKey and newKey are set we assume the newKey is the one with the correct value and ignore the value from the old key

if err := os.Setenv(newKey, os.Getenv(oldKey)); err != nil {
log.Warnf("Failed to set environment variable %s: %v", newKey, err)
}
MigratedEnv = true
}

err := Viper.BindEnv(flag.Name)
if err != nil {
log.Warnf("Error occurred binding env %s: %v", flag.Name, err)
Expand Down
30 changes: 30 additions & 0 deletions src/core/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,36 @@ func TestUpdateAgentConfig(t *testing.T) {
}
}

func TestDeprecatedEnvPrefixMigration(t *testing.T) {
want := true

curDir, err := os.Getwd()
require.NoError(t, err)

tempConfDeleteFunc, err := sysutils.CopyFile(fmt.Sprintf("%s/%s", testCfgDir, emptyConfigFile), tempCfgFile)
defer func() {
err := tempConfDeleteFunc()
require.NoError(t, err, "deletion of temp config file failed")
}()
require.NoError(t, err)

tempDynamicDeleteFunc, err := sysutils.CopyFile(fmt.Sprintf("%s/%s", testCfgDir, emptyConfigFile), tempDynamicCfgFile)
defer func() {
err := tempDynamicDeleteFunc()
require.NoError(t, err, "deletion of temp dynamic config file failed")
}()
require.NoError(t, err)

cleanEnv(t, tempCfgFile, fmt.Sprintf("%s/%s", curDir, tempDynamicCfgFile))
setEnvVariable(t, "tls_skip_verify", "true")

config, err := GetConfig("1234")
require.NoError(t, err)

got := config.TLS.SkipVerify
assert.Equal(t, want, got)
}

func setEnvVariable(t *testing.T, name string, value string) {
key := strings.ToUpper(EnvPrefix + agent_config.KeyDelimiter + name)
err := os.Setenv(key, value)
Expand Down
3 changes: 2 additions & 1 deletion src/core/config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ const (
DynamicConfigFileAbsFreeBsdPath = "/var/db/nginx-agent/agent-dynamic.conf"
ConfigFileName = "nginx-agent.conf"
ConfigFileType = "yaml"
EnvPrefix = "nms"
LegacyEnvPrefix = "nms"
EnvPrefix = "nginx_agent"
ConfigPathKey = "path"
DynamicConfigPathKey = "dynamic_config_path"

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading