Merge pull request #3944 from crazy-max/policy-windows-policy-paths
policy: normalize local policy paths
This commit is contained in:
@@ -200,7 +200,7 @@ func (p *policyPathFSRef) resolve(name string) (fs.StatFS, string, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
return cwd, filepath.Clean(v), nil
|
return cwd, path.Clean(filepath.ToSlash(v)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
contextFS, err := p.getContextFS()
|
contextFS, err := p.getContextFS()
|
||||||
@@ -276,13 +276,12 @@ func (p *policyPathFSRef) Close() error {
|
|||||||
func normalizeLocalPolicyPath(name, contextDir string) string {
|
func normalizeLocalPolicyPath(name, contextDir string) string {
|
||||||
if filepath.IsAbs(name) && contextDir != "" {
|
if filepath.IsAbs(name) && contextDir != "" {
|
||||||
if rel, err := filepath.Rel(contextDir, name); err == nil {
|
if rel, err := filepath.Rel(contextDir, name); err == nil {
|
||||||
rel = filepath.Clean(rel)
|
|
||||||
if rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
if rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||||
return rel
|
return path.Clean(filepath.ToSlash(rel))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return filepath.Clean(name)
|
return path.Clean(filepath.ToSlash(name))
|
||||||
}
|
}
|
||||||
|
|
||||||
type memoizedPolicyFS struct {
|
type memoizedPolicyFS struct {
|
||||||
|
|||||||
@@ -11,6 +11,55 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestLoadPolicyDataLocalPaths(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
policyData := []byte("package docker\n")
|
||||||
|
policyRelPath := filepath.Join("policy", "allow.rego")
|
||||||
|
require.NoError(t, os.MkdirAll(filepath.Join(dir, "policy"), 0700))
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(dir, policyRelPath), policyData, 0600))
|
||||||
|
|
||||||
|
t.Run("context-relative", func(t *testing.T) {
|
||||||
|
provider := newPolicyPathFS(context.Background(), nil, policyOpt{
|
||||||
|
ContextDir: dir,
|
||||||
|
})
|
||||||
|
|
||||||
|
dt, ok, err := loadPolicyData(provider, policyRelPath)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Equal(t, policyData, dt)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("context-absolute", func(t *testing.T) {
|
||||||
|
provider := newPolicyPathFS(context.Background(), nil, policyOpt{
|
||||||
|
ContextDir: dir,
|
||||||
|
})
|
||||||
|
|
||||||
|
dt, ok, err := loadPolicyData(provider, filepath.Join(dir, policyRelPath))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Equal(t, policyData, dt)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("cwd", func(t *testing.T) {
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, os.Chdir(dir))
|
||||||
|
t.Cleanup(func() {
|
||||||
|
require.NoError(t, os.Chdir(cwd))
|
||||||
|
})
|
||||||
|
|
||||||
|
provider := newPolicyPathFS(context.Background(), nil, policyOpt{})
|
||||||
|
dt, ok, err := loadPolicyData(provider, "cwd://"+policyRelPath)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, ok)
|
||||||
|
require.Equal(t, policyData, dt)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNormalizeLocalPolicyPath(t *testing.T) {
|
||||||
|
require.Equal(t, "policy/allow.rego", normalizeLocalPolicyPath(filepath.Join("policy", "allow.rego"), ""))
|
||||||
|
}
|
||||||
|
|
||||||
func TestMemoizedPolicyFSRefCountedClose(t *testing.T) {
|
func TestMemoizedPolicyFSRefCountedClose(t *testing.T) {
|
||||||
var initCalls int
|
var initCalls int
|
||||||
var closeCalls int
|
var closeCalls int
|
||||||
|
|||||||
Reference in New Issue
Block a user