2017-08-04 00:41:33 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestParse(t *testing.T) {
|
|
|
|
type tcase struct {
|
|
|
|
Raw string
|
|
|
|
ErrStr string
|
|
|
|
Expect *Policy
|
|
|
|
}
|
|
|
|
tcases := []tcase{
|
|
|
|
{
|
|
|
|
`
|
|
|
|
namespace "default" {
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
"",
|
|
|
|
&Policy{
|
|
|
|
Namespaces: []*NamespacePolicy{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-08-04 00:41:33 +00:00
|
|
|
Name: "default",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
Capabilities: []string{
|
|
|
|
NamespaceCapabilityListJobs,
|
|
|
|
NamespaceCapabilityReadJob,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`
|
|
|
|
namespace "default" {
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
namespace "other" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
|
|
|
namespace "secret" {
|
|
|
|
capabilities = ["deny", "read-logs"]
|
|
|
|
}
|
|
|
|
agent {
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
node {
|
|
|
|
policy = "write"
|
|
|
|
}
|
|
|
|
operator {
|
|
|
|
policy = "deny"
|
|
|
|
}
|
2017-10-13 21:36:02 +00:00
|
|
|
quota {
|
|
|
|
policy = "read"
|
|
|
|
}
|
2017-08-04 00:41:33 +00:00
|
|
|
`,
|
|
|
|
"",
|
|
|
|
&Policy{
|
|
|
|
Namespaces: []*NamespacePolicy{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-08-04 00:41:33 +00:00
|
|
|
Name: "default",
|
|
|
|
Policy: PolicyRead,
|
|
|
|
Capabilities: []string{
|
|
|
|
NamespaceCapabilityListJobs,
|
|
|
|
NamespaceCapabilityReadJob,
|
|
|
|
},
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-08-04 00:41:33 +00:00
|
|
|
Name: "other",
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
Capabilities: []string{
|
|
|
|
NamespaceCapabilityListJobs,
|
|
|
|
NamespaceCapabilityReadJob,
|
|
|
|
NamespaceCapabilitySubmitJob,
|
2017-09-29 21:22:36 +00:00
|
|
|
NamespaceCapabilityDispatchJob,
|
2017-08-04 00:41:33 +00:00
|
|
|
NamespaceCapabilityReadLogs,
|
|
|
|
NamespaceCapabilityReadFS,
|
2019-04-28 20:45:15 +00:00
|
|
|
NamespaceCapabilityAllocExec,
|
2019-03-05 09:39:06 +00:00
|
|
|
NamespaceCapabilityAllocLifecycle,
|
2017-08-04 00:41:33 +00:00
|
|
|
},
|
|
|
|
},
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-08-04 00:41:33 +00:00
|
|
|
Name: "secret",
|
|
|
|
Capabilities: []string{
|
|
|
|
NamespaceCapabilityDeny,
|
|
|
|
NamespaceCapabilityReadLogs,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Agent: &AgentPolicy{
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
|
|
|
Node: &NodePolicy{
|
|
|
|
Policy: PolicyWrite,
|
|
|
|
},
|
|
|
|
Operator: &OperatorPolicy{
|
|
|
|
Policy: PolicyDeny,
|
|
|
|
},
|
2017-10-13 21:36:02 +00:00
|
|
|
Quota: &QuotaPolicy{
|
|
|
|
Policy: PolicyRead,
|
|
|
|
},
|
2017-08-04 00:41:33 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`
|
|
|
|
namespace "default" {
|
|
|
|
policy = "foo"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
"Invalid namespace policy",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`
|
|
|
|
namespace "default" {
|
|
|
|
capabilities = ["deny", "foo"]
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
"Invalid namespace capability",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`
|
|
|
|
agent {
|
|
|
|
policy = "foo"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
"Invalid agent policy",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`
|
|
|
|
node {
|
|
|
|
policy = "foo"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
"Invalid node policy",
|
|
|
|
nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`
|
|
|
|
operator {
|
|
|
|
policy = "foo"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
"Invalid operator policy",
|
|
|
|
nil,
|
|
|
|
},
|
2017-10-13 21:36:02 +00:00
|
|
|
{
|
|
|
|
`
|
|
|
|
quota {
|
|
|
|
policy = "foo"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
"Invalid quota policy",
|
|
|
|
nil,
|
|
|
|
},
|
2017-10-17 15:40:39 +00:00
|
|
|
{
|
|
|
|
`
|
|
|
|
{
|
|
|
|
"Name": "my-policy",
|
|
|
|
"Description": "This is a great policy",
|
|
|
|
"Rules": "anything"
|
|
|
|
}
|
|
|
|
`,
|
2017-10-17 16:21:38 +00:00
|
|
|
"Invalid policy",
|
2017-10-17 15:40:39 +00:00
|
|
|
nil,
|
|
|
|
},
|
2017-08-05 00:34:22 +00:00
|
|
|
{
|
|
|
|
`
|
|
|
|
namespace "has a space"{
|
|
|
|
policy = "read"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
"Invalid namespace name",
|
|
|
|
nil,
|
|
|
|
},
|
2017-09-19 14:47:10 +00:00
|
|
|
{
|
|
|
|
`
|
|
|
|
namespace "default" {
|
|
|
|
capabilities = ["sentinel-override"]
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
"",
|
|
|
|
&Policy{
|
|
|
|
Namespaces: []*NamespacePolicy{
|
2017-09-26 22:26:33 +00:00
|
|
|
{
|
2017-09-19 14:47:10 +00:00
|
|
|
Name: "default",
|
|
|
|
Policy: "",
|
|
|
|
Capabilities: []string{
|
|
|
|
NamespaceCapabilitySentinelOverride,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-05-08 11:14:24 +00:00
|
|
|
{
|
|
|
|
`
|
|
|
|
host_volume "production-tls-*" {
|
2019-08-21 18:13:16 +00:00
|
|
|
capabilities = ["mount-readonly"]
|
2019-05-08 11:14:24 +00:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
"",
|
|
|
|
&Policy{
|
|
|
|
HostVolumes: []*HostVolumePolicy{
|
|
|
|
{
|
|
|
|
Name: "production-tls-*",
|
|
|
|
Policy: "",
|
|
|
|
Capabilities: []string{
|
2019-08-21 18:13:16 +00:00
|
|
|
HostVolumeCapabilityMountReadOnly,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`
|
|
|
|
host_volume "production-tls-*" {
|
|
|
|
capabilities = ["mount-readwrite"]
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
"",
|
|
|
|
&Policy{
|
|
|
|
HostVolumes: []*HostVolumePolicy{
|
|
|
|
{
|
|
|
|
Name: "production-tls-*",
|
|
|
|
Policy: "",
|
|
|
|
Capabilities: []string{
|
|
|
|
HostVolumeCapabilityMountReadWrite,
|
2019-05-08 11:14:24 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
`
|
|
|
|
host_volume "volume has a space" {
|
2019-08-21 18:13:16 +00:00
|
|
|
capabilities = ["mount-readwrite"]
|
2019-05-08 11:14:24 +00:00
|
|
|
}
|
|
|
|
`,
|
|
|
|
"Invalid host volume name",
|
|
|
|
nil,
|
|
|
|
},
|
2017-08-04 00:41:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for idx, tc := range tcases {
|
|
|
|
t.Run(fmt.Sprintf("%d", idx), func(t *testing.T) {
|
|
|
|
p, err := Parse(tc.Raw)
|
|
|
|
if err != nil {
|
|
|
|
if tc.ErrStr == "" {
|
|
|
|
t.Fatalf("Unexpected err: %v", err)
|
|
|
|
}
|
|
|
|
if !strings.Contains(err.Error(), tc.ErrStr) {
|
|
|
|
t.Fatalf("Unexpected err: %v", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err == nil && tc.ErrStr != "" {
|
|
|
|
t.Fatalf("Missing expected err")
|
|
|
|
}
|
|
|
|
tc.Expect.Raw = tc.Raw
|
|
|
|
assert.EqualValues(t, tc.Expect, p)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|