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

pathpolicy: tweak error messages and add test #853

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 6 additions & 6 deletions pkg/blueprint/filesystem_customizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package blueprint

import (
"encoding/json"
"errors"
"fmt"

"github.com/osbuild/images/internal/common"
Expand Down Expand Up @@ -73,16 +74,15 @@ func (fsc *FilesystemCustomization) UnmarshalJSON(data []byte) error {

// CheckMountpointsPolicy checks if the mountpoints are allowed by the policy
func CheckMountpointsPolicy(mountpoints []FilesystemCustomization, mountpointAllowList *pathpolicy.PathPolicies) error {
invalidMountpoints := []string{}
var errs []error
for _, m := range mountpoints {
err := mountpointAllowList.Check(m.Mountpoint)
if err != nil {
invalidMountpoints = append(invalidMountpoints, m.Mountpoint)
if err := mountpointAllowList.Check(m.Mountpoint); err != nil {
errs = append(errs, err)
}
}

if len(invalidMountpoints) > 0 {
return fmt.Errorf("The following custom mountpoints are not supported %+q", invalidMountpoints)
if len(errs) > 0 {
return fmt.Errorf("The following errors occurred while setting up custom mountpoints:\n%w", errors.Join(errs...))
}

return nil
Expand Down
26 changes: 26 additions & 0 deletions pkg/blueprint/filesystem_customizations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package blueprint

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/osbuild/images/pkg/pathpolicy"
)

func TestCheckMountpointsPolicy(t *testing.T) {
policy := pathpolicy.NewPathPolicies(map[string]pathpolicy.PathPolicy{
"/": {Exact: true},
})

mps := []FilesystemCustomization{
{Mountpoint: "/foo"},
{Mountpoint: "/boot/"},
}

expectedErr := `The following errors occurred while setting up custom mountpoints:
path "/foo" is not allowed
path "/boot/" must be canonical`
err := CheckMountpointsPolicy(mps, policy)
assert.EqualError(t, err, expectedErr)
}
7 changes: 3 additions & 4 deletions pkg/pathpolicy/path_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ func NewPathPolicies(entries map[string]PathPolicy) *PathPolicies {

// Check a given path against the PathPolicies
func (pol *PathPolicies) Check(fsPath string) error {

// Quickly check we have a path and it is absolute
if fsPath == "" || fsPath[0] != '/' {
return fmt.Errorf("path must be absolute")
return fmt.Errorf("path %q must be absolute", fsPath)
}

// ensure that only clean paths are valid
if fsPath != path.Clean(fsPath) {
return fmt.Errorf("path must be canonical")
return fmt.Errorf("path %q must be canonical", fsPath)
}

node, left := pol.Lookup(fsPath)
Expand All @@ -46,7 +45,7 @@ func (pol *PathPolicies) Check(fsPath string) error {
// 1) path is explicitly not allowed or
// 2) a subpath was match but an explicit match is required
if policy.Deny || (policy.Exact && len(left) > 0) {
return fmt.Errorf("path '%s ' is not allowed", fsPath)
return fmt.Errorf("path %q is not allowed", fsPath)
}

// exact match or recursive path allowed
Expand Down
10 changes: 10 additions & 0 deletions pkg/pathpolicy/path_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ func TestPathPolicyCheck(t *testing.T) {
}
}
}

func TestPathPolicyErrors(t *testing.T) {
policy := NewPathPolicies(map[string]PathPolicy{
"/": {Exact: true},
})

assert.EqualError(t, policy.Check("/foo"), `path "/foo" is not allowed`)
assert.EqualError(t, policy.Check("./"), `path "./" must be absolute`)
assert.EqualError(t, policy.Check("/boot/"), `path "/boot/" must be canonical`)
}
Loading