Skip to content

Commit ede0ad5

Browse files
author
Charity Kathure
committed
Fix windows default path overwrite issue
Windows Containers have a default path already configured at bootup. WithDefaultPathEnv overwrites this with a unix path Signed-off-by: charitykathure <[email protected]> (cherry picked from commit 7d63690) Signed-off-by: Charity Kathure <[email protected]>
1 parent d8f198a commit ede0ad5

6 files changed

Lines changed: 104 additions & 23 deletions

oci/spec_opts.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,6 @@ func WithEnv(environmentVariables []string) SpecOpts {
176176
}
177177
}
178178

179-
// WithDefaultPathEnv sets the $PATH environment variable to the
180-
// default PATH defined in this package.
181-
func WithDefaultPathEnv(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
182-
s.Process.Env = replaceOrAppendEnvValues(s.Process.Env, defaultUnixEnv)
183-
return nil
184-
}
185-
186179
// replaceOrAppendEnvValues returns the defaults with the overrides either
187180
// replaced by env key or appended to the list
188181
func replaceOrAppendEnvValues(defaults, overrides []string) []string {

oci/spec_opts_nonwindows.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//go:build !windows
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package oci
20+
21+
import (
22+
"context"
23+
24+
"github.com/containerd/containerd/containers"
25+
)
26+
27+
// WithDefaultPathEnv sets the $PATH environment variable to the
28+
// default PATH defined in this package.
29+
func WithDefaultPathEnv(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
30+
s.Process.Env = replaceOrAppendEnvValues(s.Process.Env, defaultUnixEnv)
31+
return nil
32+
}

oci/spec_opts_nonwindows_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//go:build !windows
2+
3+
/*
4+
Copyright The containerd Authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
*/
18+
19+
package oci
20+
21+
import (
22+
"context"
23+
"testing"
24+
25+
"github.com/containerd/containerd/namespaces"
26+
specs "github.com/opencontainers/runtime-spec/specs-go"
27+
)
28+
29+
func TestWithDefaultPathEnv(t *testing.T) {
30+
t.Parallel()
31+
s := Spec{}
32+
s.Process = &specs.Process{
33+
Env: []string{},
34+
}
35+
var (
36+
defaultUnixEnv = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
37+
ctx = namespaces.WithNamespace(context.Background(), "test")
38+
)
39+
WithDefaultPathEnv(ctx, nil, nil, &s)
40+
if !Contains(s.Process.Env, defaultUnixEnv) {
41+
t.Fatal("default Unix Env not found")
42+
}
43+
}

oci/spec_opts_test.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,6 @@ func Contains(a []string, x string) bool {
185185
return false
186186
}
187187

188-
func TestWithDefaultPathEnv(t *testing.T) {
189-
t.Parallel()
190-
s := Spec{}
191-
s.Process = &specs.Process{
192-
Env: []string{},
193-
}
194-
var (
195-
defaultUnixEnv = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
196-
ctx = namespaces.WithNamespace(context.Background(), "test")
197-
)
198-
WithDefaultPathEnv(ctx, nil, nil, &s)
199-
if !Contains(s.Process.Env, defaultUnixEnv) {
200-
t.Fatal("default Unix Env not found")
201-
}
202-
}
203-
204188
func TestWithProcessCwd(t *testing.T) {
205189
t.Parallel()
206190
s := Spec{}

oci/spec_opts_windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ func WithWindowsNetworkNamespace(ns string) SpecOpts {
103103
}
104104
}
105105

106+
// Windows containers have default path configured at bootup
107+
func WithDefaultPathEnv(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
108+
return nil
109+
}
110+
106111
func escapeAndCombineArgs(args []string) string {
107112
escaped := make([]string, len(args))
108113
for i, a := range args {

oci/spec_opts_windows_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package oci
1818

1919
import (
2020
"context"
21+
"os"
2122
"testing"
2223

2324
"github.com/containerd/containerd/containers"
@@ -524,3 +525,26 @@ func TestWithImageConfigArgsEscapedWindows(t *testing.T) {
524525
})
525526
}
526527
}
528+
529+
func TestWindowsDefaultPathEnv(t *testing.T) {
530+
t.Parallel()
531+
s := Spec{}
532+
s.Process = &specs.Process{
533+
Env: []string{},
534+
}
535+
536+
var (
537+
defaultUnixEnv = "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
538+
ctx = namespaces.WithNamespace(context.Background(), "test")
539+
)
540+
541+
//check that the default PATH environment is not null
542+
if os.Getenv("PATH") == "" {
543+
t.Fatal("PATH environment variable is not set")
544+
}
545+
WithDefaultPathEnv(ctx, nil, nil, &s)
546+
//check that the path is not overwritten by the unix default path
547+
if Contains(s.Process.Env, defaultUnixEnv) {
548+
t.Fatal("default Windows Env overwritten by the default Unix Env")
549+
}
550+
}

0 commit comments

Comments
 (0)