Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uid/gid check in file module #883

Merged
merged 1 commit into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,13 @@ definitions:
owner:
type: string
default: root
uid:
type: integer
group:
type: string
default: root
gid:
type: integer
filetype:
type: string
default: file
Expand Down
2 changes: 2 additions & 0 deletions integration-tests/goss/goss-shared.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ file:
exists: true
mode: '0644'
owner: root
uid: 0
group: root
gid: 0
filetype: file
contents:
- root
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ out=$(docker_exec "/goss/$os/goss-linux-$arch" --vars "/goss/vars.yaml" --vars-i
echo "$out"

if [[ $os == "arch" ]]; then
egrep -q 'Count: 100, Failed: 0, Skipped: 3' <<<"$out"
egrep -q 'Count: 104, Failed: 0, Skipped: 3' <<<"$out"
else
egrep -q 'Count: 121, Failed: 0, Skipped: 5' <<<"$out"
egrep -q 'Count: 125, Failed: 0, Skipped: 5' <<<"$out"
fi

if [[ ! $os == "arch" ]]; then
Expand Down
8 changes: 8 additions & 0 deletions resource/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ type File struct {
Mode matcher `json:"mode,omitempty" yaml:"mode,omitempty"`
Size matcher `json:"size,omitempty" yaml:"size,omitempty"`
Owner matcher `json:"owner,omitempty" yaml:"owner,omitempty"`
Uid matcher `json:"uid,omitempty" yaml:"uid,omitempty"`
Group matcher `json:"group,omitempty" yaml:"group,omitempty"`
Gid matcher `json:"gid,omitempty" yaml:"gid,omitempty"`
LinkedTo matcher `json:"linked-to,omitempty" yaml:"linked-to,omitempty"`
Filetype matcher `json:"filetype,omitempty" yaml:"filetype,omitempty"`
Contains matcher `json:"contains,omitempty" yaml:"contains,omitempty"`
Expand Down Expand Up @@ -74,9 +76,15 @@ func (f *File) Validate(sys *system.System) []TestResult {
if f.Owner != nil {
results = append(results, ValidateValue(f, "owner", f.Owner, sysFile.Owner, skip))
}
if f.Uid != nil {
results = append(results, ValidateValue(f, "uid", f.Uid, sysFile.Uid, skip))
}
if f.Group != nil {
results = append(results, ValidateValue(f, "group", f.Group, sysFile.Group, skip))
}
if f.Gid != nil {
results = append(results, ValidateValue(f, "gid", f.Gid, sysFile.Gid, skip))
}
if f.LinkedTo != nil {
results = append(results, ValidateValue(f, "linkedto", f.LinkedTo, sysFile.LinkedTo, skip))
}
Expand Down
2 changes: 2 additions & 0 deletions system/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ type File interface {
Size() (int, error)
Filetype() (string, error)
Owner() (string, error)
Uid() (int, error)
Group() (string, error)
Gid() (int, error)
LinkedTo() (string, error)
Md5() (string, error)
Sha256() (string, error)
Expand Down
30 changes: 30 additions & 0 deletions system/file_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ func (f *DefFile) Owner() (string, error) {
return getUserForUid(uid)
}

func (f *DefFile) Uid() (int, error) {
uidS, err := f.getFileInfo(func(fi os.FileInfo) string {
return fmt.Sprint(fi.Sys().(*syscall.Stat_t).Uid)
})
if err != nil {
return -1, err
}

uid, err := strconv.Atoi(uidS)
if err != nil {
return -1, err
}
return uid, nil
}

func (f *DefFile) Group() (string, error) {
gidS, err := f.getFileInfo(func(fi os.FileInfo) string {
return fmt.Sprint(fi.Sys().(*syscall.Stat_t).Gid)
Expand All @@ -52,6 +67,21 @@ func (f *DefFile) Group() (string, error) {
return getGroupForGid(gid)
}

func (f *DefFile) Gid() (int, error) {
gidS, err := f.getFileInfo(func(fi os.FileInfo) string {
return fmt.Sprint(fi.Sys().(*syscall.Stat_t).Gid)
})
if err != nil {
return -1, err
}

gid, err := strconv.Atoi(gidS)
if err != nil {
return -1, err
}
return gid, nil
}

func (f *DefFile) getFileInfo(selectorFunc func(os.FileInfo) string) (string, error) {
if err := f.setup(); err != nil {
return "", err
Expand Down
8 changes: 8 additions & 0 deletions system/file_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ func (f *DefFile) Owner() (string, error) {
return "-1", nil // not applicable on Windows
}

func (f *DefFile) Uid() (int, error) {
return -1, nil // not applicable on Windows
}

func (f *DefFile) Group() (string, error) {
return "-1", nil // not applicable on Windows
}

func (f *DefFile) Gid() (int, error) {
return -1, nil // not applicable on Windows
}