Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rhc-worker-script.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ verify_yaml: false

# temporary directory in which the temporary script will be placed and executed.
temporary_worker_directory: "/var/lib/rhc-worker-script"

env:
# environment variables to be set for the script
FOO: "some-string-value"
BAR: "other-string-value"
# ...
2 changes: 1 addition & 1 deletion src/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var ExampleYamlData = []byte(
interpreter: /bin/bash
content: |
#!/bin/sh
echo "$RHC_WORKER_FOO $RHC_WORKER_BAR!"
echo "$RHC_WORKER_FOO $RHC_WORKER_BAR $RHC_WORKER_NAME!"
content_vars:
FOO: Hello
BAR: World`)
9 changes: 8 additions & 1 deletion src/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ func verifyYamlFile(yamlData []byte) bool {
}

func setEnvVariablesForCommand(cmd *exec.Cmd, variables map[string]string) {
cmd.Env = os.Environ()
getEnvVarName := func(key string) string {
return fmt.Sprintf("RHC_WORKER_%s", strings.ToUpper(key))
}
Expand Down Expand Up @@ -125,8 +124,16 @@ func processSignedScript(incomingContent []byte) string {
// Execute script
log.Infoln("Executing script...")
cmd := exec.Command(yamlContent.Vars.Interpreter, scriptFileName) //nolint:gosec
cmd.Env = os.Environ()

// Set env vars from yaml envelope
setEnvVariablesForCommand(cmd, yamlContent.Vars.ContentVars)

// Set env vars from config
if config.Env != nil {
setEnvVariablesForCommand(cmd, *config.Env)
}

out, err := cmd.CombinedOutput()
if err != nil {
log.Errorln("Failed to execute script: ", err)
Expand Down
4 changes: 3 additions & 1 deletion src/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestProcessSignedScript(t *testing.T) {
name: "verification disabled, yaml data supplied = non-empty output",
verifyYAML: false,
yamlData: ExampleYamlData,
expectedResult: "Hello World!\n",
expectedResult: "Hello World Test!\n",
},
{
name: "verification enabled, invalid signature = error msg returned",
Expand All @@ -37,9 +37,11 @@ func TestProcessSignedScript(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
shouldVerifyYaml := tc.verifyYAML
temporaryWorkerDirectory := t.TempDir()
envMap := map[string]string{"NAME": "Test"}
config = &Config{
VerifyYAML: &shouldVerifyYaml,
TemporaryWorkerDirectory: &temporaryWorkerDirectory,
Env: &envMap,
}

defer os.RemoveAll(temporaryWorkerDirectory)
Expand Down
3 changes: 2 additions & 1 deletion src/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func TestProcessData(t *testing.T) {
{
name: "Expected data are present in result data",
yamlData: ExampleYamlData,
expectedOutput: "Hello World!",
expectedOutput: "Hello World Test!",
expectedDirective: "bar",
expectedReturnContent: "foo",
},
Expand All @@ -117,6 +117,7 @@ func TestProcessData(t *testing.T) {
config = &Config{
VerifyYAML: &shouldVerifyYaml,
TemporaryWorkerDirectory: &temporaryWorkerDirectory,
Env: &map[string]string{"NAME": "Test"},
}

returnURL := "bar"
Expand Down
15 changes: 11 additions & 4 deletions src/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ func constructMetadata(receivedMetadata map[string]string, contentType string) m
return ourMetadata
}

// Struc used fro worker global config
// Struct used for worker global config
type Config struct {
Directive *string `yaml:"directive,omitempty"`
VerifyYAML *bool `yaml:"verify_yaml,omitempty"`
TemporaryWorkerDirectory *string `yaml:"temporary_worker_directory,omitempty"`
Directive *string `yaml:"directive,omitempty"`
VerifyYAML *bool `yaml:"verify_yaml,omitempty"`
TemporaryWorkerDirectory *string `yaml:"temporary_worker_directory,omitempty"`
Env *map[string]string `yaml:"env,omitempty"`
}

// Set default values for the Config struct
Expand All @@ -120,6 +121,12 @@ func setDefaultValues(config *Config) {
log.Infof("config 'temporary_worker_directory' value is empty default value (%s) will be used", defaultTemporaryWorkerDirectoryValue)
config.TemporaryWorkerDirectory = &defaultTemporaryWorkerDirectoryValue
}

if config.Env == nil {
defaultEnvMap := map[string]string{}
log.Infof("config 'env' value is empty default value (%s) will be used", defaultEnvMap)
config.Env = &defaultEnvMap
}
}

// Load yaml config, if file doesn't exist or is invalid yaml then empty Config is returned
Expand Down
16 changes: 11 additions & 5 deletions src/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,17 @@ func boolPtr(b bool) *bool {
return &b
}

func mapStrPtr(m map[string]string) *map[string]string {
return &m
}

// Test YAML data
const validYAMLData = `
directive: "rhc-worker-script"
verify_yaml: true
verify_yaml_version_check: true
temporary_worker_directory: "/var/lib/rhc-worker-script"
env:
ENV_VAR1: "value1"
`

const validYAMLDataMissingValues = `
Expand All @@ -171,6 +176,7 @@ func TestLoadConfigOrDefault(t *testing.T) {
Directive: strPtr("rhc-worker-script"),
VerifyYAML: boolPtr(true),
TemporaryWorkerDirectory: strPtr("/var/lib/rhc-worker-script"),
Env: mapStrPtr(map[string]string{"ENV_VAR1": "value1"}),
}

testCases := []struct {
Expand Down Expand Up @@ -236,13 +242,13 @@ func TestSetDefaultValues(t *testing.T) {
}{
{
name: "test default values",
args: args{config: &Config{nil, nil, nil}},
want: args{config: &Config{strPtr("rhc-worker-script"), boolPtr(true), strPtr("/var/lib/rhc-worker-script")}},
args: args{config: &Config{nil, nil, nil, nil}},
want: args{config: &Config{strPtr("rhc-worker-script"), boolPtr(true), strPtr("/var/lib/rhc-worker-script"), mapStrPtr(map[string]string{})}},
},
{
name: "test non default values",
args: args{config: &Config{strPtr("rhc-worker-script"), boolPtr(true), strPtr("/var/lib/rhc-worker-script")}},
want: args{config: &Config{strPtr("rhc-worker-script"), boolPtr(true), strPtr("/var/lib/rhc-worker-script")}},
args: args{config: &Config{strPtr("rhc-worker-script"), boolPtr(true), strPtr("/var/lib/rhc-worker-script"), mapStrPtr(map[string]string{"ENV_VAR1": "value1"})}},
want: args{config: &Config{strPtr("rhc-worker-script"), boolPtr(true), strPtr("/var/lib/rhc-worker-script"), mapStrPtr(map[string]string{"ENV_VAR1": "value1"})}},
},
}
for _, tt := range tests {
Expand Down