Skip to content

Commit 357f8e3

Browse files
fix: require SELinux Permissive mode for scenario tests (#414)
* Initial plan * Add SELinux mode check and remove ContainerMountFlagZ - Created SELinux helper functions in scenario/internal/testhelpers/selinux.go - Added TestMain to scenario package to check SELinux mode before running tests - Removed ContainerMountFlagZ from image_customize_test.go that caused issues with SELinux enforcing - Tests now fail fast with clear error message if SELinux is in enforcing mode Co-authored-by: tobiasb_microsoft <115835401+tobiasb_microsoft@users.noreply.github.com> * Fix linting issues in SELinux test - Changed test package to testhelpers_test for proper separation - Use require.NoError instead of assert.NoError for error assertions - Updated references to use qualified names with testhelpers prefix Co-authored-by: tobiasb_microsoft <115835401+tobiasb_microsoft@users.noreply.github.com> * Address code review feedback - Consolidated multiple fmt.Fprintf calls into single call in setup_test.go - Kept newline before error format specifier per repo coding standards Co-authored-by: tobiasb_microsoft <115835401+tobiasb_microsoft@users.noreply.github.com> * Use getenforce command instead of reading file directly - Use exec.CommandContext with getenforce as primary method - Fall back to reading /sys/fs/selinux/enforce if getenforce not available - Leverages system's standard way of determining SELinux status Co-authored-by: tobiasb_microsoft <115835401+tobiasb_microsoft@users.noreply.github.com> * Use opencontainers/selinux package for SELinux detection - Replaced custom implementation with github.com/opencontainers/selinux/go-selinux - Simplified code by leveraging existing, well-maintained package - Uses GetEnabled() and EnforceMode() from the package Co-authored-by: tobiasb_microsoft <115835401+tobiasb_microsoft@users.noreply.github.com> * Add comments explaining edge cases in SELinux detection - Clarify that Disabled case is for completeness but shouldn't be reached - Document that unknown modes are treated as disabled to avoid blocking tests Co-authored-by: tobiasb_microsoft <115835401+tobiasb_microsoft@users.noreply.github.com> * Remove redundant GetEnabled() check - EnforceMode() already returns selinux.Disabled when SELinux is not enabled - Simplified code by removing unnecessary GetEnabled() call - Removed obsolete comment about unreachable case Co-authored-by: tobiasb_microsoft <115835401+tobiasb_microsoft@users.noreply.github.com> * Remove unused error return from GetSELinuxMode - Function never returns an error, so simplified signature - Updated callers to not handle error that can never occur - Removed unused require import from test Co-authored-by: tobiasb_microsoft <115835401+tobiasb_microsoft@users.noreply.github.com> * Remove SELinux helper and use go-selinux APIs directly - Deleted scenario/internal/testhelpers/selinux.go and selinux_test.go - Updated setup_test.go to use selinux.EnforceMode() directly - Simplified by removing wrapper types and using go-selinux constants - go-selinux package now available for production code use Co-authored-by: tobiasb_microsoft <115835401+tobiasb_microsoft@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tobiasb_microsoft <115835401+tobiasb_microsoft@users.noreply.github.com>
1 parent 664e664 commit 357f8e3

4 files changed

Lines changed: 53 additions & 1 deletion

File tree

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ require (
3535
github.com/mattn/go-isatty v0.0.20
3636
github.com/muesli/termenv v0.16.0
3737
github.com/nxadm/tail v1.4.11
38+
github.com/opencontainers/selinux v1.13.1
3839
github.com/pelletier/go-toml/v2 v2.2.4
3940
github.com/samber/lo v1.52.0
4041
github.com/samber/slog-multi v1.7.0
@@ -71,6 +72,7 @@ require (
7172
github.com/containerd/platforms v0.2.1 // indirect
7273
github.com/cpuguy83/dockercfg v0.3.2 // indirect
7374
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
75+
github.com/cyphar/filepath-securejoin v0.5.1 // indirect
7476
github.com/davecgh/go-spew v1.1.1 // indirect
7577
github.com/distribution/reference v0.6.0 // indirect
7678
github.com/docker/go-connections v0.6.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ github.com/creachadair/tomledit v0.0.29/go.mod h1:4SoTXxzHgvzHRMIJPw+o6zK/yXii4V
7777
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
7878
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
7979
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
80+
github.com/cyphar/filepath-securejoin v0.5.1 h1:eYgfMq5yryL4fbWfkLpFFy2ukSELzaJOTaUTuh+oF48=
81+
github.com/cyphar/filepath-securejoin v0.5.1/go.mod h1:Sdj7gXlvMcPZsbhwhQ33GguGLDGQL7h7bg04C/+u9jI=
8082
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
8183
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
8284
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -219,6 +221,8 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
219221
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
220222
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
221223
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
224+
github.com/opencontainers/selinux v1.13.1 h1:A8nNeceYngH9Ow++M+VVEwJVpdFmrlxsN22F+ISDCJE=
225+
github.com/opencontainers/selinux v1.13.1/go.mod h1:S10WXZ/osk2kWOYKy1x2f/eXF5ZHJoUs8UU/2caNRbg=
222226
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
223227
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
224228
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=

scenario/image_customize_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ fi
113113
// Mount the docker socket to allow the container to run docker commands
114114
containertest.NewContainerMount("/var/run/docker.sock", "/var/run/docker.sock", nil),
115115
// Need to mount /dev to allow device mounting inside the container
116-
containertest.NewContainerMount("/dev", "/dev", []containertest.ContainerMountFlag{containertest.ContainerMountFlagZ}),
116+
containertest.NewContainerMount("/dev", "/dev", nil),
117117
// Mount the output dir so we can see the output of the inner container in the outer container
118118
containertest.NewContainerMount(outputDir, outputDir, nil),
119119
})

scenario/setup_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
//go:build scenario
5+
6+
package scenario_tests
7+
8+
import (
9+
"fmt"
10+
"os"
11+
"testing"
12+
13+
"github.com/opencontainers/selinux/go-selinux"
14+
)
15+
16+
// TestMain is the entry point for all scenario tests.
17+
// It checks SELinux mode and fails fast if it's in Enforcing mode.
18+
func TestMain(m *testing.M) {
19+
// Check SELinux mode
20+
mode := selinux.EnforceMode()
21+
22+
// Fail fast if SELinux is in Enforcing mode
23+
if mode == selinux.Enforcing {
24+
fmt.Fprintf(os.Stderr, `Scenario tests require SELinux to be in Permissive mode or disabled.
25+
Current SELinux mode: Enforcing
26+
27+
To set SELinux to Permissive mode temporarily, run:
28+
sudo setenforce 0
29+
30+
To make it permanent, edit /etc/selinux/config and set:
31+
SELINUX=permissive
32+
`)
33+
os.Exit(1)
34+
}
35+
36+
// Log the current SELinux mode for informational purposes
37+
if mode == selinux.Permissive {
38+
fmt.Printf("SELinux is in Permissive mode - tests will run.\n")
39+
} else {
40+
fmt.Printf("SELinux is disabled - tests will run.\n")
41+
}
42+
43+
// Run the tests
44+
os.Exit(m.Run())
45+
}
46+

0 commit comments

Comments
 (0)