Skip to content

Commit

Permalink
Merge pull request #257 from newrelic/kav/ExplicitEnvs
Browse files Browse the repository at this point in the history
feat: explicit flag to allow command envs & additional logging
  • Loading branch information
varas authored Dec 16, 2020
2 parents 10738bb + 0670a1e commit 1724c16
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/basics/configure.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,5 @@ integrations: # OHI configuration starts here
### <a name='specialenvs'></a>Special environment variables
- `FLEX_META` - setting this environment variable with a flat JSON payload will unpack this into global custom attributes.
- `FLEX_CMD_PREPEND` - automatically prepend to commands being run.
- `FLEX_CMD_APPEND` - automatically append to a commands being run.
- `FLEX_CMD_PREPEND` - automatically prepend to commands being run (Requires AllowEnvCommands enabled).
- `FLEX_CMD_APPEND` - automatically append to a commands being run (Requires AllowEnvCommands enabled).
15 changes: 14 additions & 1 deletion internal/inputs/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func RunCommands(dataStore *[]interface{}, yml *load.Config, apiNo int) {
}

func commandRun(dataStore *[]interface{}, yml *load.Config, command load.Command, api load.API, startTime int64, dataSample map[string]interface{}, processType string) {
command.Run = os.Getenv("FLEX_CMD_PREPEND") + command.Run + os.Getenv("FLEX_CMD_APPEND")
command.Run = envCommandCheck(command.Run)
runCommand := command.Run
if command.Output == load.Jmx {
SetJMXCommand(&runCommand, command, api, yml)
Expand Down Expand Up @@ -163,6 +163,19 @@ func commandRun(dataStore *[]interface{}, yml *load.Config, command load.Command
}
}

// checks if explicitedly enabled log
func envCommandCheck(commandStr string) string {
if load.Args.AllowEnvCommands {
load.Logrus.WithFields(logrus.Fields{
"command": commandStr,
"prepend": os.Getenv("FLEX_CMD_PREPEND"),
"append": os.Getenv("FLEX_CMD_APPEND"),
}).Info("command: environment modification enabled")
return os.Getenv("FLEX_CMD_PREPEND") + commandStr + os.Getenv("FLEX_CMD_APPEND")
}
return commandStr
}

func splitOutput(dataStore *[]interface{}, output string, command load.Command, startTime int64) {
lines := strings.Split(strings.TrimSuffix(output, "\n"), "\n")
var outputBlocks [][]string
Expand Down
19 changes: 19 additions & 0 deletions internal/inputs/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package inputs

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -222,3 +223,21 @@ func TestDf2(t *testing.T) {
assert.Equal(t, expectedValue, actualValue)
}
}

func TestEnvCommandCheck(t *testing.T) {
load.Refresh()

os.Setenv("FLEX_CMD_PREPEND", "echo hi && ")

command := "echo hello"
command = envCommandCheck(command)

// should not be modified
assert.Equal(t, "echo hello", command)

// should now be modified
load.Args.AllowEnvCommands = true
command = envCommandCheck(command)
assert.Equal(t, "echo hi && echo hello", command)

}
1 change: 1 addition & 0 deletions internal/load/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type ArgumentList struct {
DiscoverProcessLinux bool `default:"false" help:"Discover Process info on Linux OS"`
NRJMXToolPath string `default:"/usr/lib/nrjmx/" help:"Set a custom path for nrjmx tool"`
StructuredLogs bool `default:"false" help:"output logs in Json structure format for external tool parsing"`
AllowEnvCommands bool `default:"false" help:"enable to allow the use of FLEX_CMD_PREPEND & FLEX_CMD_APPEND"`
}

// Args Infrastructure SDK Arguments List
Expand Down

0 comments on commit 1724c16

Please sign in to comment.