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

Allow dash in environment variable names #6080

Merged
merged 7 commits into from
Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
14 changes: 12 additions & 2 deletions client/taskenv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,15 +476,25 @@ func (b *Builder) Build() *TaskEnv {
// Clean keys (see #2405)
cleanedEnv := make(map[string]string, len(envMap))
for k, v := range envMap {
cleanedK := helper.CleanEnvVar(k, '_')
var cleanedK string
if strings.HasPrefix(k, "NOMAD_") {
cleanedK = helper.CleanEnvVar(k, '_', "")
} else {
cleanedK = helper.CleanEnvVar(k, '_', "-")
}
cleanedEnv[cleanedK] = v
}

var cleanedDeviceEnvs map[string]string
if deviceEnvs != nil {
cleanedDeviceEnvs = make(map[string]string, len(deviceEnvs))
for k, v := range deviceEnvs {
cleanedK := helper.CleanEnvVar(k, '_')
var cleanedK string
if strings.HasPrefix(k, "NOMAD_") {
lchayoun marked this conversation as resolved.
Show resolved Hide resolved
cleanedK = helper.CleanEnvVar(k, '_', "")
} else {
cleanedK = helper.CleanEnvVar(k, '_', "-")
}
cleanedDeviceEnvs[cleanedK] = v
}
}
Expand Down
12 changes: 9 additions & 3 deletions client/taskenv/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,11 +656,17 @@ func TestEnvironment_AppendHostEnvvars(t *testing.T) {
func TestEnvironment_DashesInTaskName(t *testing.T) {
a := mock.Alloc()
task := a.Job.TaskGroups[0].Tasks[0]
task.Env = map[string]string{"test-one-two": "three-four"}
task.Env = map[string]string{
"test-one-two": "three-four",
"NOMAD_test_one_two": "three-five",
}
envMap := NewBuilder(mock.Node(), a, task, "global").Build().Map()

if envMap["test_one_two"] != "three-four" {
t.Fatalf("Expected test_one_two=three-four in TaskEnv; found:\n%#v", envMap)
if envMap["test-one-two"] != "three-four" {
t.Fatalf("Expected test-one-two=three-four in TaskEnv; found:\n%#v", envMap)
}
if envMap["NOMAD_test_one_two"] != "three-five" {
t.Fatalf("Expected NOMAD_test_one_two=three-five in TaskEnv; found:\n%#v", envMap)
}
}

Expand Down
5 changes: 4 additions & 1 deletion helper/funcs.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package helper

import (
"bytes"
"crypto/sha512"
"fmt"
"regexp"
Expand Down Expand Up @@ -343,10 +344,12 @@ func CopySliceInt(s []int) []int {

// CleanEnvVar replaces all occurrences of illegal characters in an environment
// variable with the specified byte.
func CleanEnvVar(s string, r byte) string {
// any extra characters will be considered valid
func CleanEnvVar(s string, r byte, e string) string {
lchayoun marked this conversation as resolved.
Show resolved Hide resolved
b := []byte(s)
for i, c := range b {
switch {
case bytes.ContainsAny([]byte{c}, e):
case c == '_':
case c == '.':
case c >= 'a' && c <= 'z':
Expand Down
39 changes: 37 additions & 2 deletions helper/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ func TestClearEnvVar(t *testing.T) {
{"asd0", "asd0"},
{"_asd", "_asd"},
{"-asd", "_asd"},
{"NOMAD_aaa-asd", "NOMAD_aaa_asd"},
{"asd.fgh", "asd.fgh"},
{"A~!@#$%^&*()_+-={}[]|\\;:'\"<,>?/Z", "A______________________________Z"},
{"A\U0001f4a9Z", "A____Z"},
}
for _, c := range cases {
if output := CleanEnvVar(c.input, '_'); output != c.expected {
if output := CleanEnvVar(c.input, '_', ""); output != c.expected {
t.Errorf("CleanEnvVar(%q, '_') -> %q != %q", c.input, output, c.expected)
}
}
Expand All @@ -154,6 +155,40 @@ func BenchmarkCleanEnvVar(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
CleanEnvVar(in, replacement)
CleanEnvVar(in, replacement, "")
}
}

func TestClearEnvVarWithExtraChars(t *testing.T) {
type testCase struct {
input string
expected string
}
cases := []testCase{
{"asdf", "asdf"},
{"ASDF", "ASDF"},
{"0sdf", "_sdf"},
{"asd0", "asd0"},
{"_asd", "_asd"},
{"-asd", "-asd"},
{"asd.fgh", "asd.fgh"},
{"A~!@#$%^&*()_+-={}[]|\\;:'\"<,>?/Z", "A_____________-________________Z"},
{"A\U0001f4a9Z", "A____Z"},
}
for _, c := range cases {
if output := CleanEnvVar(c.input, '_', "-"); output != c.expected {
t.Errorf("CleanEnvVar(%q, '_') -> %q != %q", c.input, output, c.expected)
}
}
}

func BenchmarkCleanEnvVarWithExtraChars(b *testing.B) {
in := "NOMAD_ADDR_redis-cache"
replacement := byte('_')
b.SetBytes(int64(len(in)))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
CleanEnvVar(in, replacement, "-")
}
}