Skip to content

Commit 7d63690

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]>
1 parent 0066676 commit 7d63690

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
@@ -187,13 +187,6 @@ func WithEnv(environmentVariables []string) SpecOpts {
187187
}
188188
}
189189

190-
// WithDefaultPathEnv sets the $PATH environment variable to the
191-
// default PATH defined in this package.
192-
func WithDefaultPathEnv(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
193-
s.Process.Env = replaceOrAppendEnvValues(s.Process.Env, defaultUnixEnv)
194-
return nil
195-
}
196-
197190
// replaceOrAppendEnvValues returns the defaults with the overrides either
198191
// replaced by env key or appended to the list
199192
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
@@ -188,22 +188,6 @@ func Contains(a []string, x string) bool {
188188
return false
189189
}
190190

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

oci/spec_opts_windows.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,8 @@ func WithDevices(devicePath, containerPath, permissions string) SpecOpts {
6262
return nil
6363
}
6464
}
65+
66+
// Windows containers have default path configured at bootup
67+
func WithDefaultPathEnv(_ context.Context, _ Client, _ *containers.Container, s *Spec) error {
68+
return nil
69+
}

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)