Skip to content

Commit

Permalink
Change environment prefix from nms to nginx_agent (#706)
Browse files Browse the repository at this point in the history
* changed env variable prefix from NMS to NGINX_AGENT

* changed env variable prefix from NMS to NGINX_AGENT

* renamed env prefix constants

* renamed env prefix constants

* refactored string building of oldKey and newKey

* refactored string building of oldKey and newKey

* added test for env prefix migration

* added test for env prefix migration

* refactored string builing of oldKey and newKey

* refactored string builing of oldKey and newKey

* fixed deprecated env prefix migration test
  • Loading branch information
spencerugbo authored Jun 17, 2024
1 parent 3dc9eb3 commit 174be3d
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 6 deletions.
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) == "" {
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.

0 comments on commit 174be3d

Please sign in to comment.