Skip to content

Commit 7a87679

Browse files
tooryxcopybara-github
authored andcommitted
Filter out unsupported systems at runtime rather than compilation time for the /etc/passwd CIS checks' unit tests.
PiperOrigin-RevId: 652436006
1 parent 0878179 commit 7a87679

File tree

4 files changed

+96
-29
lines changed

4 files changed

+96
-29
lines changed

detector/cis/generic_linux/etcpasswdpermissions/detector_dummy.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package etcpasswdpermissions
1919
import (
2020
"context"
2121
"fmt"
22+
"io/fs"
2223

2324
"github.com/google/osv-scalibr/detector"
2425
"github.com/google/osv-scalibr/inventoryindex"
@@ -41,3 +42,8 @@ func (Detector) RequiredExtractors() []string { return []string{} }
4142
func (d Detector) Scan(ctx context.Context, scanRoot string, ix *inventoryindex.InventoryIndex) ([]*detector.Finding, error) {
4243
return nil, fmt.Errorf("plugin only supported on Linux")
4344
}
45+
46+
// ScanFS starts the scan from a pseudo-filesystem.
47+
func (Detector) ScanFS(ctx context.Context, fs fs.FS, ix *inventoryindex.InventoryIndex) ([]*detector.Finding, error) {
48+
return nil, fmt.Errorf("plugin only supported on Linux")
49+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build !linux
16+
17+
package etcpasswdpermissions_test
18+
19+
import (
20+
"errors"
21+
"io/fs"
22+
"time"
23+
)
24+
25+
func (f *fakeFS) Open(name string) (fs.File, error) { return nil, errors.New("unsupported system") }
26+
27+
func (f *fakeFile) Stat() (fs.FileInfo, error) { return nil, errors.New("unsupported system") }
28+
func (fakeFile) Read([]byte) (int, error) { return 0, errors.New("unsupported system") }
29+
func (fakeFile) Close() error { return nil }
30+
31+
func (fakeFileInfo) Name() string { return "unsupported" }
32+
func (fakeFileInfo) Size() int64 { return 0 }
33+
func (i *fakeFileInfo) Mode() fs.FileMode { return 0 }
34+
func (fakeFileInfo) ModTime() time.Time { return time.Now() }
35+
func (i *fakeFileInfo) IsDir() bool { return false }
36+
func (i *fakeFileInfo) Sys() any { return nil }
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//go:build linux
16+
17+
package etcpasswdpermissions_test
18+
19+
import (
20+
"errors"
21+
"io/fs"
22+
"os"
23+
"syscall"
24+
"time"
25+
)
26+
27+
func (f *fakeFS) Open(name string) (fs.File, error) {
28+
if name == "etc/passwd" {
29+
if f.exists {
30+
return &fakeFile{perms: f.perms, uid: f.uid, gid: f.gid}, nil
31+
}
32+
return nil, os.ErrNotExist
33+
}
34+
return nil, errors.New("failed to open")
35+
}
36+
37+
func (f *fakeFile) Stat() (fs.FileInfo, error) {
38+
return &fakeFileInfo{perms: f.perms, uid: f.uid, gid: f.gid}, nil
39+
}
40+
func (fakeFile) Read([]byte) (int, error) { return 0, errors.New("failed to read") }
41+
func (fakeFile) Close() error { return nil }
42+
43+
func (fakeFileInfo) Name() string { return "/etc/passwd" }
44+
func (fakeFileInfo) Size() int64 { return 1 }
45+
func (i *fakeFileInfo) Mode() fs.FileMode { return i.perms }
46+
func (fakeFileInfo) ModTime() time.Time { return time.Now() }
47+
func (i *fakeFileInfo) IsDir() bool { return false }
48+
func (i *fakeFileInfo) Sys() any { return &syscall.Stat_t{Uid: i.uid, Gid: i.gid} }

detector/cis/generic_linux/etcpasswdpermissions/detector_test.go

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
//go:build linux
16-
1715
package etcpasswdpermissions_test
1816

1917
import (
2018
"context"
21-
"errors"
2219
"io/fs"
23-
"os"
24-
"syscall"
20+
"runtime"
21+
"slices"
2522
"testing"
26-
"time"
2723

2824
"github.com/google/go-cmp/cmp"
2925
"github.com/google/go-cmp/cmp/cmpopts"
@@ -41,42 +37,23 @@ type fakeFS struct {
4137
gid uint32
4238
}
4339

44-
func (f *fakeFS) Open(name string) (fs.File, error) {
45-
if name == "etc/passwd" {
46-
if f.exists {
47-
return &fakeFile{perms: f.perms, uid: f.uid, gid: f.gid}, nil
48-
}
49-
return nil, os.ErrNotExist
50-
}
51-
return nil, errors.New("failed to open")
52-
}
53-
5440
type fakeFile struct {
5541
perms fs.FileMode
5642
uid uint32
5743
gid uint32
5844
}
5945

60-
func (f *fakeFile) Stat() (fs.FileInfo, error) {
61-
return &fakeFileInfo{perms: f.perms, uid: f.uid, gid: f.gid}, nil
62-
}
63-
func (fakeFile) Read([]byte) (int, error) { return 0, errors.New("failed to read") }
64-
func (fakeFile) Close() error { return nil }
65-
6646
type fakeFileInfo struct {
6747
perms fs.FileMode
6848
uid uint32
6949
gid uint32
7050
}
7151

72-
func (fakeFileInfo) Name() string { return "/etc/passwd" }
73-
func (fakeFileInfo) Size() int64 { return 1 }
74-
func (i *fakeFileInfo) Mode() fs.FileMode { return i.perms }
75-
func (fakeFileInfo) ModTime() time.Time { return time.Now() }
76-
func (i *fakeFileInfo) IsDir() bool { return false }
77-
func (i *fakeFileInfo) Sys() any { return &syscall.Stat_t{Uid: i.uid, Gid: i.gid} }
78-
7952
func TestScan(t *testing.T) {
53+
if !slices.Contains([]string{"linux"}, runtime.GOOS) {
54+
t.Skipf("Skipping test for unsupported OS %q", runtime.GOOS)
55+
}
56+
8057
wantTitle := "Ensure permissions on /etc/passwd are configured"
8158
wantDesc := "The /etc/passwd file contains user account information that " +
8259
"is used by many system utilities and therefore must be readable for these " +

0 commit comments

Comments
 (0)