Skip to content

Commit

Permalink
fix: expose temp file with environment variables in Windows (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspin authored Oct 2, 2024
1 parent 2368d3c commit 6a53759
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 32 deletions.
46 changes: 29 additions & 17 deletions pkg/executors/shell_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ func (e *ShellExecutor) Start() int {
return 0
}

func (e *ShellExecutor) envFileName() string {
//
// On Windows, we do not use the environment file at all during the job,
// but we still need it to be created for debug sessions to work properly.
// And, in order for users to be able to "source" the file in a PowerShell debug session,
// the file has to be suffixed with the .ps1 suffix.
//
if runtime.GOOS == "windows" {
return filepath.Join(e.tmpDirectory, fmt.Sprintf(".env-%d.ps1", time.Now().UnixNano()))
}

return filepath.Join(e.tmpDirectory, fmt.Sprintf(".env-%d", time.Now().UnixNano()))
}

func (e *ShellExecutor) ExportEnvVars(envVars []api.EnvVar, hostEnvVars []config.HostEnvVar) int {
commandStartedAt := int(time.Now().Unix())
directive := "Exporting environment variables"
Expand All @@ -111,23 +125,13 @@ func (e *ShellExecutor) ExportEnvVars(envVars []api.EnvVar, hostEnvVars []config
}

/*
* In windows, no PTY is used, so the environment state
* is tracked in the shell itself.
* Create a temporary file containing all the environment variables.
* For Windows agents, we don't use this file for the job itself,
* since we keep track of the environment in-memory due to the lack of a PTY,
* But we still need the file to be created for debug sessions.
*/
if runtime.GOOS == "windows" {
e.Shell.Env.Append(environment, func(name, value string) {
e.Logger.LogCommandOutput(fmt.Sprintf("Exporting %s\n", name))
})

exitCode = 0
return exitCode
}

/*
* If not windows, we use a PTY, so there's no need to track
* the environment state here.
*/
envFileName := filepath.Join(e.tmpDirectory, fmt.Sprintf(".env-%d", time.Now().UnixNano()))
envFileName := e.envFileName()
e.cleanupAfterClose = append(e.cleanupAfterClose, envFileName)
err = environment.ToFile(envFileName, func(name string) {
e.Logger.LogCommandOutput(fmt.Sprintf("Exporting %s\n", name))
})
Expand All @@ -137,7 +141,15 @@ func (e *ShellExecutor) ExportEnvVars(envVars []api.EnvVar, hostEnvVars []config
return exitCode
}

e.cleanupAfterClose = append(e.cleanupAfterClose, envFileName)
/*
* In windows, no PTY is used, so we don't source the environment file.
* Instead, we keep track of the environment changes in the shell object itself.
*/
if runtime.GOOS == "windows" {
e.Shell.Env.Append(environment, nil)
exitCode = 0
return exitCode
}

cmd := fmt.Sprintf("source %s", envFileName)
exitCode = e.RunCommand(cmd, true, "")
Expand Down
36 changes: 28 additions & 8 deletions pkg/kubernetes/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/base64"
"fmt"
stdruntime "runtime"
"testing"
"time"

Expand All @@ -12,7 +13,7 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
runtime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
)
Expand Down Expand Up @@ -40,9 +41,15 @@ func Test__CreateSecret(t *testing.T) {
assert.True(t, *secret.Immutable)
assert.Equal(t, secret.Name, secretName)
assert.Empty(t, secret.Labels)
assert.Equal(t, secret.StringData, map[string]string{
".env": "export A=AAA\nexport B=BBB\nexport C=CCC\n",
})

var expected string
if stdruntime.GOOS == "windows" {
expected = "$env:A = \"AAA\"\n$env:B = \"BBB\"\n$env:C = \"CCC\"\n"
} else {
expected = "export A=AAA\nexport B=BBB\nexport C=CCC\n"
}

assert.Equal(t, secret.StringData, map[string]string{".env": expected})
})

t.Run("stores files in secret, with base64-encoded keys", func(t *testing.T) {
Expand Down Expand Up @@ -82,8 +89,16 @@ func Test__CreateSecret(t *testing.T) {
assert.True(t, *secret.Immutable)
assert.Equal(t, secret.Name, secretName)
assert.Empty(t, secret.Labels)

var expected string
if stdruntime.GOOS == "windows" {
expected = "$env:A = \"AAA\"\n$env:B = \"BBB\"\n$env:C = \"CCC\"\n"
} else {
expected = "export A=AAA\nexport B=BBB\nexport C=CCC\n"
}

assert.Equal(t, secret.StringData, map[string]string{
".env": "export A=AAA\nexport B=BBB\nexport C=CCC\n",
".env": expected,
key1: "Random content",
key2: "Random content 2",
})
Expand Down Expand Up @@ -122,9 +137,14 @@ func Test__CreateSecret(t *testing.T) {
"semaphoreci.com/agent-type": "s1-test",
})

assert.Equal(t, secret.StringData, map[string]string{
".env": "export A=AAA\nexport B=BBB\nexport C=CCC\n",
})
var expected string
if stdruntime.GOOS == "windows" {
expected = "$env:A = \"AAA\"\n$env:B = \"BBB\"\n$env:C = \"CCC\"\n"
} else {
expected = "export A=AAA\nexport B=BBB\nexport C=CCC\n"
}

assert.Equal(t, secret.StringData, map[string]string{".env": expected})
})
}

Expand Down
13 changes: 12 additions & 1 deletion pkg/shell/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io/ioutil"
"regexp"
"runtime"
"sort"
"strings"

Expand Down Expand Up @@ -125,7 +126,12 @@ func (e *Environment) ToFile(fileName string, callback func(name string)) error
fileContent := ""
for _, name := range e.Keys() {
value, _ := e.Get(name)
fileContent += fmt.Sprintf("export %s=%s\n", name, shellQuote(value))
if runtime.GOOS == "windows" {
fileContent += fmt.Sprintf("$env:%s = \"%s\"\n", name, escapePowershellQuotes(value))
} else {
fileContent += fmt.Sprintf("export %s=%s\n", name, shellQuote(value))
}

if callback != nil {
callback(name)
}
Expand All @@ -140,6 +146,11 @@ func (e *Environment) ToFile(fileName string, callback func(name string)) error
return nil
}

func escapePowershellQuotes(s string) string {
r := strings.Replace(s, "`", "``", -1)
return strings.Replace(r, "\"", "`\"", -1)
}

func shellQuote(s string) string {
pattern := regexp.MustCompile(`[^\w@%+=:,./-]`)

Expand Down
27 changes: 21 additions & 6 deletions pkg/shell/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,12 @@ VAR_C=CCC
}

func Test__EnvironmentToFile(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Environment.ToFile() is only used in non-windows")
}

vars := []api.EnvVar{
{Name: "Z", Value: base64.StdEncoding.EncodeToString([]byte("ZZZ"))},
{Name: "O", Value: base64.StdEncoding.EncodeToString([]byte("OOO"))},
{Name: "QUOTED", Value: base64.StdEncoding.EncodeToString([]byte("This is going to get quoted"))},
{Name: "SPACES_ARE_SINGLE_QUOTED_ON_UNIX", Value: base64.StdEncoding.EncodeToString([]byte("This is going to get quoted"))},
{Name: "DOUBLE_QUOTES_ARE_ESCAPED_ON_POWERSHELL", Value: base64.StdEncoding.EncodeToString([]byte("\"This\" is going to get escaped"))},
{Name: "BACKTICKS_ARE_ESCAPED_ON_POWERSHELL", Value: base64.StdEncoding.EncodeToString([]byte("`This` is going to get escaped too"))},
}

env, err := CreateEnvironment(vars, []config.HostEnvVar{})
Expand All @@ -124,8 +122,25 @@ func Test__EnvironmentToFile(t *testing.T) {

content, err := ioutil.ReadFile(file.Name())
assert.Nil(t, err)
assert.Equal(t, string(content), "export O=OOO\nexport QUOTED='This is going to get quoted'\nexport Z=ZZZ\n")

var expected string
if runtime.GOOS == "windows" {
expected = `$env:BACKTICKS_ARE_ESCAPED_ON_POWERSHELL = "` + "``This``" + ` is going to get escaped too"
$env:DOUBLE_QUOTES_ARE_ESCAPED_ON_POWERSHELL = "` + "`\"This`\"" + ` is going to get escaped"
$env:O = "OOO"
$env:SPACES_ARE_SINGLE_QUOTED_ON_UNIX = "This is going to get quoted"
$env:Z = "ZZZ"
`
} else {
expected = `export BACKTICKS_ARE_ESCAPED_ON_POWERSHELL='` + "`This`" + ` is going to get escaped too'
export DOUBLE_QUOTES_ARE_ESCAPED_ON_POWERSHELL='"This" is going to get escaped'
export O=OOO
export SPACES_ARE_SINGLE_QUOTED_ON_UNIX='This is going to get quoted'
export Z=ZZZ
`
}

assert.Equal(t, expected, string(content))
file.Close()
os.Remove(file.Name())
}
Expand Down

0 comments on commit 6a53759

Please sign in to comment.