2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-03-02 19:53:40 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
2018-03-03 17:43:37 +00:00
|
|
|
"strings"
|
2018-03-02 19:53:40 +00:00
|
|
|
"testing"
|
2018-03-06 18:35:20 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-01-13 20:51:40 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-05-21 17:44:30 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/acl"
|
2018-03-02 19:53:40 +00:00
|
|
|
)
|
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
func TestIntention_ACLs(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
intention Intention
|
|
|
|
rules string
|
|
|
|
read bool
|
|
|
|
write bool
|
|
|
|
}
|
2018-03-05 02:46:33 +00:00
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
cases := map[string]testCase{
|
2020-06-16 17:19:31 +00:00
|
|
|
"all-denied": {
|
2020-01-13 20:51:40 +00:00
|
|
|
intention: Intention{
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "web",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "api",
|
|
|
|
},
|
|
|
|
read: false,
|
|
|
|
write: false,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"deny-write-read-dest": {
|
2020-01-13 20:51:40 +00:00
|
|
|
rules: `service "api" { policy = "deny" intentions = "read" }`,
|
|
|
|
intention: Intention{
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "web",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "api",
|
|
|
|
},
|
|
|
|
read: true,
|
|
|
|
write: false,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"deny-write-read-source": {
|
2020-01-13 20:51:40 +00:00
|
|
|
rules: `service "web" { policy = "deny" intentions = "read" }`,
|
|
|
|
intention: Intention{
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "web",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "api",
|
|
|
|
},
|
|
|
|
read: true,
|
|
|
|
write: false,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"allow-write-with-dest-write": {
|
2020-01-13 20:51:40 +00:00
|
|
|
rules: `service "api" { policy = "deny" intentions = "write" }`,
|
|
|
|
intention: Intention{
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "web",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "api",
|
|
|
|
},
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"deny-write-with-source-write": {
|
2020-01-13 20:51:40 +00:00
|
|
|
rules: `service "web" { policy = "deny" intentions = "write" }`,
|
|
|
|
intention: Intention{
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "web",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "api",
|
|
|
|
},
|
|
|
|
read: true,
|
|
|
|
write: false,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"deny-wildcard-write-allow-read": {
|
2020-01-13 20:51:40 +00:00
|
|
|
rules: `service "*" { policy = "deny" intentions = "write" }`,
|
|
|
|
intention: Intention{
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "*",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "*",
|
|
|
|
},
|
|
|
|
// technically having been granted read/write on any intention will allow
|
|
|
|
// read access for this rule
|
|
|
|
read: true,
|
|
|
|
write: false,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"allow-wildcard-write": {
|
2020-01-13 20:51:40 +00:00
|
|
|
rules: `service_prefix "" { policy = "deny" intentions = "write" }`,
|
|
|
|
intention: Intention{
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "*",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "*",
|
|
|
|
},
|
|
|
|
read: true,
|
|
|
|
write: true,
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"allow-wildcard-read": {
|
2020-01-13 20:51:40 +00:00
|
|
|
rules: `service "foo" { policy = "deny" intentions = "read" }`,
|
|
|
|
intention: Intention{
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "*",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "*",
|
|
|
|
},
|
|
|
|
read: true,
|
|
|
|
write: false,
|
2018-03-05 02:46:33 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
config := acl.Config{
|
|
|
|
WildcardName: WildcardSpecifier,
|
|
|
|
}
|
2018-03-05 02:46:33 +00:00
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
for name, tcase := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2023-02-06 15:35:52 +00:00
|
|
|
authz, err := acl.NewAuthorizerFromRules(tcase.rules, &config, nil)
|
2020-01-13 20:51:40 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-05 02:46:33 +00:00
|
|
|
|
2020-01-13 20:51:40 +00:00
|
|
|
require.Equal(t, tcase.read, tcase.intention.CanRead(authz))
|
|
|
|
require.Equal(t, tcase.write, tcase.intention.CanWrite(authz))
|
2018-03-05 02:46:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 17:43:37 +00:00
|
|
|
func TestIntentionValidate(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Name string
|
|
|
|
Modify func(*Intention)
|
|
|
|
Err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"long description",
|
|
|
|
func(x *Intention) {
|
|
|
|
x.Description = strings.Repeat("x", metaValueMaxLength+1)
|
|
|
|
},
|
|
|
|
"description exceeds",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"no action set",
|
|
|
|
func(x *Intention) { x.Action = "" },
|
|
|
|
"action must be set",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"no SourceNS",
|
|
|
|
func(x *Intention) { x.SourceNS = "" },
|
|
|
|
"SourceNS must be set",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"no SourceName",
|
|
|
|
func(x *Intention) { x.SourceName = "" },
|
|
|
|
"SourceName must be set",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"no DestinationNS",
|
|
|
|
func(x *Intention) { x.DestinationNS = "" },
|
|
|
|
"DestinationNS must be set",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"no DestinationName",
|
|
|
|
func(x *Intention) { x.DestinationName = "" },
|
|
|
|
"DestinationName must be set",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"SourceNS partial wildcard",
|
|
|
|
func(x *Intention) { x.SourceNS = "foo*" },
|
|
|
|
"partial value",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"SourceName partial wildcard",
|
|
|
|
func(x *Intention) { x.SourceName = "foo*" },
|
|
|
|
"partial value",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"SourceName exact following wildcard",
|
|
|
|
func(x *Intention) {
|
|
|
|
x.SourceNS = "*"
|
|
|
|
x.SourceName = "foo"
|
|
|
|
},
|
|
|
|
"follow wildcard",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"DestinationNS partial wildcard",
|
|
|
|
func(x *Intention) { x.DestinationNS = "foo*" },
|
|
|
|
"partial value",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"DestinationName partial wildcard",
|
|
|
|
func(x *Intention) { x.DestinationName = "foo*" },
|
|
|
|
"partial value",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"DestinationName exact following wildcard",
|
|
|
|
func(x *Intention) {
|
|
|
|
x.DestinationNS = "*"
|
|
|
|
x.DestinationName = "foo"
|
|
|
|
},
|
|
|
|
"follow wildcard",
|
|
|
|
},
|
2018-03-03 17:55:27 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
"SourceType is not set",
|
|
|
|
func(x *Intention) { x.SourceType = "" },
|
|
|
|
"SourceType must",
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"SourceType is other",
|
|
|
|
func(x *Intention) { x.SourceType = IntentionSourceType("other") },
|
|
|
|
"SourceType must",
|
|
|
|
},
|
2018-03-03 17:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
|
|
|
ixn := TestIntention(t)
|
|
|
|
tc.Modify(ixn)
|
|
|
|
|
|
|
|
err := ixn.Validate()
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
assert.Equal(t, err != nil, tc.Err != "", err)
|
2018-03-03 17:43:37 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2018-03-06 18:35:20 +00:00
|
|
|
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
assert.Contains(t, strings.ToLower(err.Error()), strings.ToLower(tc.Err))
|
2018-03-03 17:43:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 19:53:40 +00:00
|
|
|
func TestIntentionPrecedenceSorter(t *testing.T) {
|
2022-06-08 17:24:10 +00:00
|
|
|
type fields struct {
|
2023-04-20 16:16:04 +00:00
|
|
|
SrcSamenessGroup string
|
|
|
|
SrcPeer string
|
|
|
|
SrcNS string
|
|
|
|
SrcN string
|
|
|
|
DstNS string
|
|
|
|
DstN string
|
2022-06-08 17:24:10 +00:00
|
|
|
}
|
2018-03-02 19:53:40 +00:00
|
|
|
cases := []struct {
|
|
|
|
Name string
|
2022-06-08 17:24:10 +00:00
|
|
|
Input []fields
|
|
|
|
Expected []fields
|
2018-03-02 19:53:40 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
"exhaustive list",
|
2022-06-08 17:24:10 +00:00
|
|
|
[]fields{
|
2023-04-20 16:16:04 +00:00
|
|
|
// Sameness fields
|
|
|
|
{SrcSamenessGroup: "group", SrcNS: "*", SrcN: "*", DstNS: "exact", DstN: "*"},
|
|
|
|
{SrcSamenessGroup: "group", SrcNS: "*", SrcN: "*", DstNS: "*", DstN: "*"},
|
|
|
|
{SrcSamenessGroup: "group", SrcNS: "exact", SrcN: "*", DstNS: "exact", DstN: "exact"},
|
|
|
|
{SrcSamenessGroup: "group", SrcNS: "*", SrcN: "*", DstNS: "exact", DstN: "exact"},
|
|
|
|
{SrcSamenessGroup: "group", SrcNS: "exact", SrcN: "exact", DstNS: "*", DstN: "*"},
|
|
|
|
{SrcSamenessGroup: "group", SrcNS: "exact", SrcN: "exact", DstNS: "exact", DstN: "exact"},
|
|
|
|
{SrcSamenessGroup: "group", SrcNS: "exact", SrcN: "exact", DstNS: "exact", DstN: "*"},
|
|
|
|
{SrcSamenessGroup: "group", SrcNS: "exact", SrcN: "*", DstNS: "exact", DstN: "*"},
|
|
|
|
{SrcSamenessGroup: "group", SrcNS: "exact", SrcN: "*", DstNS: "*", DstN: "*"},
|
2022-06-08 17:24:10 +00:00
|
|
|
// Peer fields
|
|
|
|
{SrcPeer: "peer", SrcNS: "*", SrcN: "*", DstNS: "exact", DstN: "*"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "*", SrcN: "*", DstNS: "*", DstN: "*"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "exact", SrcN: "*", DstNS: "exact", DstN: "exact"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "*", SrcN: "*", DstNS: "exact", DstN: "exact"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "exact", SrcN: "exact", DstNS: "*", DstN: "*"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "exact", SrcN: "exact", DstNS: "exact", DstN: "exact"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "exact", SrcN: "exact", DstNS: "exact", DstN: "*"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "exact", SrcN: "*", DstNS: "exact", DstN: "*"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "exact", SrcN: "*", DstNS: "*", DstN: "*"},
|
|
|
|
|
|
|
|
{SrcNS: "*", SrcN: "*", DstNS: "exact", DstN: "*"},
|
|
|
|
{SrcNS: "*", SrcN: "*", DstNS: "*", DstN: "*"},
|
|
|
|
{SrcNS: "exact", SrcN: "*", DstNS: "exact", DstN: "exact"},
|
|
|
|
{SrcNS: "*", SrcN: "*", DstNS: "exact", DstN: "exact"},
|
|
|
|
{SrcNS: "exact", SrcN: "exact", DstNS: "*", DstN: "*"},
|
|
|
|
{SrcNS: "exact", SrcN: "exact", DstNS: "exact", DstN: "exact"},
|
|
|
|
{SrcNS: "exact", SrcN: "exact", DstNS: "exact", DstN: "*"},
|
|
|
|
{SrcNS: "exact", SrcN: "*", DstNS: "exact", DstN: "*"},
|
|
|
|
{SrcNS: "exact", SrcN: "*", DstNS: "*", DstN: "*"},
|
2018-03-02 19:53:40 +00:00
|
|
|
},
|
2022-06-08 17:24:10 +00:00
|
|
|
[]fields{
|
|
|
|
{SrcPeer: "", SrcNS: "exact", SrcN: "exact", DstNS: "exact", DstN: "exact"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "exact", SrcN: "exact", DstNS: "exact", DstN: "exact"},
|
2023-04-20 16:16:04 +00:00
|
|
|
{SrcSamenessGroup: "group", SrcNS: "exact", SrcN: "exact", DstNS: "exact", DstN: "exact"},
|
2022-06-08 17:24:10 +00:00
|
|
|
{SrcPeer: "", SrcNS: "exact", SrcN: "*", DstNS: "exact", DstN: "exact"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "exact", SrcN: "*", DstNS: "exact", DstN: "exact"},
|
2023-04-20 16:16:04 +00:00
|
|
|
{SrcSamenessGroup: "group", SrcNS: "exact", SrcN: "*", DstNS: "exact", DstN: "exact"},
|
2022-06-08 17:24:10 +00:00
|
|
|
{SrcPeer: "", SrcNS: "*", SrcN: "*", DstNS: "exact", DstN: "exact"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "*", SrcN: "*", DstNS: "exact", DstN: "exact"},
|
2023-04-20 16:16:04 +00:00
|
|
|
{SrcSamenessGroup: "group", SrcNS: "*", SrcN: "*", DstNS: "exact", DstN: "exact"},
|
2022-06-08 17:24:10 +00:00
|
|
|
{SrcPeer: "", SrcNS: "exact", SrcN: "exact", DstNS: "exact", DstN: "*"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "exact", SrcN: "exact", DstNS: "exact", DstN: "*"},
|
2023-04-20 16:16:04 +00:00
|
|
|
{SrcSamenessGroup: "group", SrcNS: "exact", SrcN: "exact", DstNS: "exact", DstN: "*"},
|
2022-06-08 17:24:10 +00:00
|
|
|
{SrcPeer: "", SrcNS: "exact", SrcN: "*", DstNS: "exact", DstN: "*"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "exact", SrcN: "*", DstNS: "exact", DstN: "*"},
|
2023-04-20 16:16:04 +00:00
|
|
|
{SrcSamenessGroup: "group", SrcNS: "exact", SrcN: "*", DstNS: "exact", DstN: "*"},
|
2022-06-08 17:24:10 +00:00
|
|
|
{SrcPeer: "", SrcNS: "*", SrcN: "*", DstNS: "exact", DstN: "*"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "*", SrcN: "*", DstNS: "exact", DstN: "*"},
|
2023-04-20 16:16:04 +00:00
|
|
|
{SrcSamenessGroup: "group", SrcNS: "*", SrcN: "*", DstNS: "exact", DstN: "*"},
|
2022-06-08 17:24:10 +00:00
|
|
|
{SrcPeer: "", SrcNS: "exact", SrcN: "exact", DstNS: "*", DstN: "*"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "exact", SrcN: "exact", DstNS: "*", DstN: "*"},
|
2023-04-20 16:16:04 +00:00
|
|
|
{SrcSamenessGroup: "group", SrcNS: "exact", SrcN: "exact", DstNS: "*", DstN: "*"},
|
2022-06-08 17:24:10 +00:00
|
|
|
{SrcPeer: "", SrcNS: "exact", SrcN: "*", DstNS: "*", DstN: "*"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "exact", SrcN: "*", DstNS: "*", DstN: "*"},
|
2023-04-20 16:16:04 +00:00
|
|
|
{SrcSamenessGroup: "group", SrcNS: "exact", SrcN: "*", DstNS: "*", DstN: "*"},
|
2022-06-08 17:24:10 +00:00
|
|
|
{SrcPeer: "", SrcNS: "*", SrcN: "*", DstNS: "*", DstN: "*"},
|
|
|
|
{SrcPeer: "peer", SrcNS: "*", SrcN: "*", DstNS: "*", DstN: "*"},
|
2023-04-20 16:16:04 +00:00
|
|
|
{SrcSamenessGroup: "group", SrcNS: "*", SrcN: "*", DstNS: "*", DstN: "*"},
|
2018-03-02 19:53:40 +00:00
|
|
|
},
|
|
|
|
},
|
2018-04-05 11:41:49 +00:00
|
|
|
{
|
|
|
|
"tiebreak deterministically",
|
2022-06-08 17:24:10 +00:00
|
|
|
[]fields{
|
|
|
|
{SrcNS: "a", SrcN: "*", DstNS: "a", DstN: "b"},
|
|
|
|
{SrcNS: "a", SrcN: "*", DstNS: "a", DstN: "a"},
|
|
|
|
{SrcNS: "b", SrcN: "a", DstNS: "a", DstN: "a"},
|
|
|
|
{SrcNS: "a", SrcN: "b", DstNS: "a", DstN: "a"},
|
|
|
|
{SrcNS: "a", SrcN: "a", DstNS: "b", DstN: "a"},
|
|
|
|
{SrcNS: "a", SrcN: "a", DstNS: "a", DstN: "b"},
|
|
|
|
{SrcNS: "a", SrcN: "a", DstNS: "a", DstN: "a"},
|
2018-04-05 11:41:49 +00:00
|
|
|
},
|
2022-06-08 17:24:10 +00:00
|
|
|
[]fields{
|
2018-04-05 11:41:49 +00:00
|
|
|
// Exact matches first in lexicographical order (arbitrary but
|
|
|
|
// deterministic)
|
2022-06-08 17:24:10 +00:00
|
|
|
{SrcNS: "a", SrcN: "a", DstNS: "a", DstN: "a"},
|
|
|
|
{SrcNS: "a", SrcN: "a", DstNS: "a", DstN: "b"},
|
|
|
|
{SrcNS: "a", SrcN: "a", DstNS: "b", DstN: "a"},
|
|
|
|
{SrcNS: "a", SrcN: "b", DstNS: "a", DstN: "a"},
|
|
|
|
{SrcNS: "b", SrcN: "a", DstNS: "a", DstN: "a"},
|
2018-04-05 11:41:49 +00:00
|
|
|
// Wildcards next, lexicographical
|
2022-06-08 17:24:10 +00:00
|
|
|
{SrcNS: "a", SrcN: "*", DstNS: "a", DstN: "a"},
|
|
|
|
{SrcNS: "a", SrcN: "*", DstNS: "a", DstN: "b"},
|
2018-04-05 11:41:49 +00:00
|
|
|
},
|
|
|
|
},
|
2018-03-02 19:53:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name, func(t *testing.T) {
|
2018-03-06 18:35:20 +00:00
|
|
|
|
2018-03-02 19:53:40 +00:00
|
|
|
var input Intentions
|
|
|
|
for _, v := range tc.Input {
|
|
|
|
input = append(input, &Intention{
|
2023-04-20 16:16:04 +00:00
|
|
|
SourceSamenessGroup: v.SrcSamenessGroup,
|
|
|
|
SourcePeer: v.SrcPeer,
|
|
|
|
SourceNS: v.SrcNS,
|
|
|
|
SourceName: v.SrcN,
|
|
|
|
DestinationNS: v.DstNS,
|
|
|
|
DestinationName: v.DstN,
|
2018-03-02 19:53:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-06-07 04:11:37 +00:00
|
|
|
// Set all the precedence values
|
|
|
|
for _, ixn := range input {
|
|
|
|
ixn.UpdatePrecedence()
|
|
|
|
}
|
|
|
|
|
2018-03-02 19:53:40 +00:00
|
|
|
// Sort
|
|
|
|
sort.Sort(IntentionPrecedenceSorter(input))
|
|
|
|
|
|
|
|
// Get back into a comparable form
|
2022-06-08 17:24:10 +00:00
|
|
|
var actual []fields
|
2018-03-02 19:53:40 +00:00
|
|
|
for _, v := range input {
|
2022-06-08 17:24:10 +00:00
|
|
|
actual = append(actual, fields{
|
2023-04-20 16:16:04 +00:00
|
|
|
SrcSamenessGroup: v.SourceSamenessGroup,
|
|
|
|
SrcPeer: v.SourcePeer,
|
|
|
|
SrcNS: v.SourceNS,
|
|
|
|
SrcN: v.SourceName,
|
|
|
|
DstNS: v.DestinationNS,
|
|
|
|
DstN: v.DestinationName,
|
2018-03-02 19:53:40 +00:00
|
|
|
})
|
|
|
|
}
|
bulk rewrite using this script
set -euo pipefail
unset CDPATH
cd "$(dirname "$0")"
for f in $(git grep '\brequire := require\.New(' | cut -d':' -f1 | sort -u); do
echo "=== require: $f ==="
sed -i '/require := require.New(t)/d' $f
# require.XXX(blah) but not require.XXX(tblah) or require.XXX(rblah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\([^tr]\)/require.\1(t,\2/g' $f
# require.XXX(tblah) but not require.XXX(t, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/require.\1(t,\2/g' $f
# require.XXX(rblah) but not require.XXX(r, blah)
sed -i 's/\brequire\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/require.\1(t,\2/g' $f
gofmt -s -w $f
done
for f in $(git grep '\bassert := assert\.New(' | cut -d':' -f1 | sort -u); do
echo "=== assert: $f ==="
sed -i '/assert := assert.New(t)/d' $f
# assert.XXX(blah) but not assert.XXX(tblah) or assert.XXX(rblah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\([^tr]\)/assert.\1(t,\2/g' $f
# assert.XXX(tblah) but not assert.XXX(t, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(t[^,]\)/assert.\1(t,\2/g' $f
# assert.XXX(rblah) but not assert.XXX(r, blah)
sed -i 's/\bassert\.\([a-zA-Z0-9_]*\)(\(r[^,]\)/assert.\1(t,\2/g' $f
gofmt -s -w $f
done
2022-01-20 16:46:23 +00:00
|
|
|
assert.Equal(t, tc.Expected, actual)
|
2018-03-02 19:53:40 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 01:10:03 +00:00
|
|
|
|
|
|
|
func TestIntention_SetHash(t *testing.T) {
|
|
|
|
i := Intention{
|
|
|
|
ID: "the-id",
|
|
|
|
Description: "the-description",
|
|
|
|
SourceNS: "source-ns",
|
|
|
|
SourceName: "source-name",
|
|
|
|
DestinationNS: "dest-ns",
|
|
|
|
DestinationName: "dest-name",
|
|
|
|
SourceType: "source-type",
|
|
|
|
Action: "action",
|
|
|
|
Precedence: 123,
|
|
|
|
Meta: map[string]string{
|
|
|
|
"meta1": "one",
|
|
|
|
"meta2": "two",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
i.SetHash()
|
|
|
|
expected := []byte{
|
|
|
|
0x20, 0x89, 0x55, 0xdb, 0x69, 0x34, 0xce, 0x89, 0xd8, 0xb9, 0x2e, 0x3a,
|
|
|
|
0x85, 0xb6, 0xea, 0x43, 0xb2, 0x23, 0x16, 0x93, 0x94, 0x13, 0x2a, 0xe4,
|
|
|
|
0x81, 0xfe, 0xe, 0x34, 0x91, 0x99, 0xe9, 0x8d,
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, i.Hash)
|
|
|
|
}
|
2020-10-06 22:09:13 +00:00
|
|
|
|
|
|
|
func TestIntention_String(t *testing.T) {
|
|
|
|
type testcase struct {
|
|
|
|
ixn *Intention
|
|
|
|
expect string
|
|
|
|
}
|
|
|
|
|
|
|
|
testID := generateUUID()
|
|
|
|
|
2021-09-08 15:59:30 +00:00
|
|
|
partitionPrefix := DefaultEnterpriseMetaInDefaultPartition().PartitionOrEmpty()
|
|
|
|
if partitionPrefix != "" {
|
|
|
|
partitionPrefix += "/"
|
|
|
|
}
|
|
|
|
|
2020-10-06 22:09:13 +00:00
|
|
|
cases := map[string]testcase{
|
|
|
|
"legacy allow": {
|
|
|
|
&Intention{
|
|
|
|
ID: testID,
|
|
|
|
SourceName: "foo",
|
|
|
|
DestinationName: "bar",
|
|
|
|
Action: IntentionActionAllow,
|
|
|
|
},
|
2021-09-08 15:59:30 +00:00
|
|
|
partitionPrefix + `default/foo => ` + partitionPrefix + `default/bar (ID: ` + testID + `, Precedence: 9, Action: ALLOW)`,
|
2020-10-06 22:09:13 +00:00
|
|
|
},
|
|
|
|
"legacy deny": {
|
|
|
|
&Intention{
|
|
|
|
ID: testID,
|
|
|
|
SourceName: "foo",
|
|
|
|
DestinationName: "bar",
|
|
|
|
Action: IntentionActionDeny,
|
|
|
|
},
|
2021-09-08 15:59:30 +00:00
|
|
|
partitionPrefix + `default/foo => ` + partitionPrefix + `default/bar (ID: ` + testID + `, Precedence: 9, Action: DENY)`,
|
2020-10-06 22:09:13 +00:00
|
|
|
},
|
|
|
|
"L4 allow": {
|
|
|
|
&Intention{
|
|
|
|
SourceName: "foo",
|
|
|
|
DestinationName: "bar",
|
|
|
|
Action: IntentionActionAllow,
|
|
|
|
},
|
2021-09-08 15:59:30 +00:00
|
|
|
partitionPrefix + `default/foo => ` + partitionPrefix + `default/bar (Precedence: 9, Action: ALLOW)`,
|
2020-10-06 22:09:13 +00:00
|
|
|
},
|
|
|
|
"L4 deny": {
|
|
|
|
&Intention{
|
|
|
|
SourceName: "foo",
|
|
|
|
DestinationName: "bar",
|
|
|
|
Action: IntentionActionDeny,
|
|
|
|
},
|
2021-09-08 15:59:30 +00:00
|
|
|
partitionPrefix + `default/foo => ` + partitionPrefix + `default/bar (Precedence: 9, Action: DENY)`,
|
2020-10-06 22:09:13 +00:00
|
|
|
},
|
|
|
|
"L7 one perm": {
|
|
|
|
&Intention{
|
|
|
|
SourceName: "foo",
|
|
|
|
DestinationName: "bar",
|
|
|
|
Permissions: []*IntentionPermission{
|
|
|
|
{
|
|
|
|
Action: IntentionActionAllow,
|
|
|
|
HTTP: &IntentionHTTPPermission{
|
|
|
|
PathPrefix: "/foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-09-08 15:59:30 +00:00
|
|
|
partitionPrefix + `default/foo => ` + partitionPrefix + `default/bar (Precedence: 9, Permissions: 1)`,
|
2020-10-06 22:09:13 +00:00
|
|
|
},
|
|
|
|
"L7 two perms": {
|
|
|
|
&Intention{
|
|
|
|
SourceName: "foo",
|
|
|
|
DestinationName: "bar",
|
|
|
|
Permissions: []*IntentionPermission{
|
|
|
|
{
|
|
|
|
Action: IntentionActionDeny,
|
|
|
|
HTTP: &IntentionHTTPPermission{
|
|
|
|
PathExact: "/foo/admin",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Action: IntentionActionAllow,
|
|
|
|
HTTP: &IntentionHTTPPermission{
|
|
|
|
PathPrefix: "/foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-09-08 15:59:30 +00:00
|
|
|
partitionPrefix + `default/foo => ` + partitionPrefix + `default/bar (Precedence: 9, Permissions: 2)`,
|
2020-10-06 22:09:13 +00:00
|
|
|
},
|
2022-06-08 17:24:10 +00:00
|
|
|
"L4 allow with source peer": {
|
|
|
|
&Intention{
|
|
|
|
SourceName: "foo",
|
|
|
|
SourcePeer: "billing",
|
|
|
|
DestinationName: "bar",
|
|
|
|
Action: IntentionActionAllow,
|
|
|
|
},
|
|
|
|
`peer(billing)/default/foo => ` + partitionPrefix + `default/bar (Precedence: 9, Action: ALLOW)`,
|
|
|
|
},
|
2023-04-20 16:16:04 +00:00
|
|
|
"L4 allow with source sameness group": {
|
|
|
|
&Intention{
|
|
|
|
SourceName: "foo",
|
|
|
|
SourceSamenessGroup: "group-1",
|
|
|
|
DestinationName: "bar",
|
|
|
|
Action: IntentionActionAllow,
|
|
|
|
},
|
|
|
|
`sameness-group(group-1)/default/foo => ` + partitionPrefix + `default/bar (Precedence: 9, Action: ALLOW)`,
|
|
|
|
},
|
2020-10-06 22:09:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range cases {
|
|
|
|
tc := tc
|
|
|
|
// Add a bunch of required fields.
|
2021-09-08 15:59:30 +00:00
|
|
|
tc.ixn.FillPartitionAndNamespace(DefaultEnterpriseMetaInDefaultPartition(), true)
|
2020-10-06 22:09:13 +00:00
|
|
|
tc.ixn.UpdatePrecedence()
|
|
|
|
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
got := tc.ixn.String()
|
|
|
|
require.Equal(t, tc.expect, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-05-21 17:44:30 +00:00
|
|
|
|
|
|
|
func TestIntentionQueryRequest_CacheInfoKey(t *testing.T) {
|
2021-05-31 21:22:16 +00:00
|
|
|
assertCacheInfoKeyIsComplete(t, &IntentionQueryRequest{})
|
2021-05-21 17:44:30 +00:00
|
|
|
}
|