Skip to content

Commit

Permalink
Add more unit-test and comment fixes
Browse files Browse the repository at this point in the history
Signed-off-by: cedric lamoriniere <cedric.lamoriniere@datadoghq.com>
  • Loading branch information
clamoriniere committed Sep 17, 2020
1 parent f80a75d commit f587136
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
26 changes: 14 additions & 12 deletions pkg/secrets/check_rights_nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func checkRights(path string, allowGroupExec bool) error {
// get information about current user
usr, err := user.Current()
if err != nil {
return fmt.Errorf("can't query current user UID: %s", err)
return fmt.Errorf("can't query current user's GIDs: %s", err)
}

if !allowGroupExec {
Expand All @@ -31,7 +31,7 @@ func checkRights(path string, allowGroupExec bool) error {

userGroups, err := usr.GroupIds()
if err != nil {
return fmt.Errorf("can't query current user UID: %s", err)
return fmt.Errorf("can't query current user's GIDs: %s", err)
}
return checkGroupPermission(&stat, usr, userGroups, path)
}
Expand All @@ -55,14 +55,21 @@ func checkUserPermission(stat *syscall.Stat_t, usr *user.User, path string) erro
return nil
}

// checkUserPermission check that only the current User or one of his group can exec the path
// checkGroupPermission check that only the current User or one of his group can exec the path
func checkGroupPermission(stat *syscall.Stat_t, usr *user.User, userGroups []string, path string) error {
var isUserFile bool
if fmt.Sprintf("%d", stat.Uid) == usr.Uid {
isUserFile = true
var isUserHavePermission bool
// checking if the user is the owner and the owner have exec rights
if (fmt.Sprintf("%d", stat.Uid) == usr.Uid) && (stat.Mode&syscall.S_IXUSR != 0) {
isUserHavePermission = true
}

// If *group* executable, user can RWX, group can RX, and nothing else for anyone.
if stat.Mode&(syscall.S_IRWXO|syscall.S_IWGRP) != 0 {
return fmt.Errorf("invalid executable '%s', 'others' have rights on it or 'group' has write permissions on it", path)
}

// If the file is not owned by the user, let's check for one of his groups
if !isUserFile {
if !isUserHavePermission {
var isGroupFile bool
for _, userGroup := range userGroups {
if fmt.Sprintf("%d", stat.Gid) == userGroup {
Expand All @@ -80,10 +87,5 @@ func checkGroupPermission(stat *syscall.Stat_t, usr *user.User, userGroups []str
}
}

// If *group* executable, user can RWX, group can RX, and nothing else for anyone.
if stat.Mode&(syscall.S_IRWXO|syscall.S_IWGRP) != 0 {
return fmt.Errorf("invalid executable '%s', 'others' have rights on it or 'group' has write permissions on it", path)
}

return nil
}
4 changes: 4 additions & 0 deletions pkg/secrets/check_rights_nix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func TestGroupOtherRights(t *testing.T) {
// other should have no right
require.Nil(t, os.Chmod(tmpfile.Name(), 0701))
require.NotNil(t, checkRights(tmpfile.Name(), allowGroupExec))

// other should not have write permission
require.Nil(t, os.Chmod(tmpfile.Name(), 0702))
require.NotNil(t, checkRights(tmpfile.Name(), allowGroupExec))
}

func Test_checkGroupPermission(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion pkg/secrets/check_rights_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ var (

// checkRights check that the given filename has access controls set only for
// Administrator, Local System and the datadog user.
func checkRights(filename string, _ bool) error {
func checkRights(filename string, allowGroupExec bool) error {
// this function ignore `allowGroupExec` since it was design for the cluster-agent,
// but the cluster-agent is not delivered for windows.
if allowGroupExec {
return fmt.Errorf("the option 'allowGroupExec=true' is not allowed on windows")
}
if _, err := os.Stat(filename); err != nil {
if os.IsNotExist(err) {
return fmt.Errorf("secretBackendCommand %s does not exist", filename)
Expand Down

0 comments on commit f587136

Please sign in to comment.