2018-02-28 06:25:05 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2021-03-14 04:44:01 +00:00
|
|
|
"sort"
|
2018-02-28 06:25:05 +00:00
|
|
|
"testing"
|
2018-03-03 16:43:19 +00:00
|
|
|
"time"
|
2018-02-28 06:25:05 +00:00
|
|
|
|
2021-02-03 23:10:38 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2020-10-08 00:35:34 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2018-02-28 06:25:05 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-10-06 18:24:05 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2018-02-28 06:25:05 +00:00
|
|
|
)
|
|
|
|
|
2020-11-13 20:42:21 +00:00
|
|
|
var (
|
|
|
|
testLocation = time.FixedZone("UTC-8", -8*60*60)
|
|
|
|
|
|
|
|
testTimeA = time.Date(1955, 11, 5, 6, 15, 0, 0, testLocation)
|
|
|
|
testTimeB = time.Date(1985, 10, 26, 1, 35, 0, 0, testLocation)
|
|
|
|
testTimeC = time.Date(2015, 10, 21, 16, 29, 0, 0, testLocation)
|
|
|
|
)
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func testBothIntentionFormats(t *testing.T, f func(t *testing.T, s *Store, legacy bool)) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// Within the body of the callback, only use Legacy CRUD functions to edit
|
|
|
|
// data (pivoting on the legacy flag), and exclusively use the generic
|
|
|
|
// functions that flip-flop between tables to read it.
|
|
|
|
|
|
|
|
t.Run("legacy", func(t *testing.T) {
|
|
|
|
// NOTE: This one tests that the old state machine functions still do
|
|
|
|
// what we expect. No newly initiated user edits should go through
|
|
|
|
// these paths, just lingering raft log entries from before the upgrade
|
|
|
|
// to 1.9.0.
|
|
|
|
s := testStateStore(t)
|
|
|
|
f(t, s, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("config-entries", func(t *testing.T) {
|
|
|
|
s := testConfigStateStore(t)
|
|
|
|
f(t, s, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-02-28 06:25:05 +00:00
|
|
|
func TestStore_IntentionGet_none(t *testing.T) {
|
2020-10-06 18:24:05 +00:00
|
|
|
testBothIntentionFormats(t, func(t *testing.T, s *Store, legacy bool) {
|
|
|
|
assert := assert.New(t)
|
2018-02-28 06:25:05 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Querying with no results returns nil.
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
idx, _, res, err := s.IntentionGet(ws, testUUID())
|
|
|
|
assert.Equal(uint64(1), idx)
|
|
|
|
assert.Nil(res)
|
|
|
|
assert.Nil(err)
|
|
|
|
})
|
2018-02-28 06:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_IntentionSetGet_basic(t *testing.T) {
|
2020-10-06 18:24:05 +00:00
|
|
|
testBothIntentionFormats(t, func(t *testing.T, s *Store, legacy bool) {
|
|
|
|
lastIndex := uint64(1)
|
|
|
|
|
|
|
|
// Call Get to populate the watch set
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
_, _, _, err := s.IntentionGet(ws, testUUID())
|
|
|
|
require.Nil(t, err)
|
|
|
|
|
|
|
|
// Build a valid intention
|
|
|
|
var (
|
|
|
|
legacyIxn *structs.Intention
|
|
|
|
configEntry *structs.ServiceIntentionsConfigEntry
|
|
|
|
|
|
|
|
expected *structs.Intention
|
|
|
|
)
|
|
|
|
if legacy {
|
|
|
|
legacyIxn = &structs.Intention{
|
|
|
|
ID: testUUID(),
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "*",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "web",
|
|
|
|
Meta: map[string]string{},
|
2020-11-13 20:42:21 +00:00
|
|
|
CreatedAt: testTimeA,
|
|
|
|
UpdatedAt: testTimeA,
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
2018-02-28 06:25:05 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Inserting a with empty ID is disallowed.
|
|
|
|
lastIndex++
|
|
|
|
require.NoError(t, s.LegacyIntentionSet(lastIndex, legacyIxn))
|
|
|
|
|
|
|
|
// Make sure the right index got updated.
|
2021-01-30 00:17:40 +00:00
|
|
|
require.Equal(t, lastIndex, s.maxIndex(tableConnectIntentions))
|
2021-01-29 01:34:34 +00:00
|
|
|
require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
expected = &structs.Intention{
|
|
|
|
ID: legacyIxn.ID,
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "*",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "web",
|
|
|
|
Meta: map[string]string{},
|
2020-11-13 20:42:21 +00:00
|
|
|
CreatedAt: testTimeA,
|
|
|
|
UpdatedAt: testTimeA,
|
2020-10-06 18:24:05 +00:00
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: lastIndex,
|
|
|
|
ModifyIndex: lastIndex,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
//nolint:staticcheck
|
|
|
|
expected.UpdatePrecedence()
|
|
|
|
} else {
|
|
|
|
srcID := testUUID()
|
|
|
|
configEntry = &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "web",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
2020-11-13 20:42:21 +00:00
|
|
|
LegacyID: srcID,
|
|
|
|
Name: "*",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
LegacyMeta: map[string]string{},
|
|
|
|
LegacyCreateTime: &testTimeA,
|
|
|
|
LegacyUpdateTime: &testTimeA,
|
2020-10-06 18:24:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-02-28 06:25:05 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
lastIndex++
|
|
|
|
require.NoError(t, configEntry.LegacyNormalize())
|
|
|
|
require.NoError(t, configEntry.LegacyValidate())
|
2021-02-03 23:10:38 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(lastIndex, configEntry.Clone()))
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
// Make sure the config entry index got updated instead of the old intentions one
|
2021-01-29 01:34:34 +00:00
|
|
|
require.Equal(t, lastIndex, s.maxIndex(tableConfigEntries))
|
2021-01-30 00:17:40 +00:00
|
|
|
require.Equal(t, uint64(0), s.maxIndex(tableConnectIntentions))
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
expected = &structs.Intention{
|
|
|
|
ID: srcID,
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "*",
|
|
|
|
SourceType: structs.IntentionSourceConsul,
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "web",
|
|
|
|
Meta: map[string]string{},
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: lastIndex,
|
|
|
|
ModifyIndex: lastIndex,
|
|
|
|
},
|
|
|
|
CreatedAt: *configEntry.Sources[0].LegacyCreateTime,
|
|
|
|
UpdatedAt: *configEntry.Sources[0].LegacyUpdateTime,
|
|
|
|
}
|
|
|
|
//nolint:staticcheck
|
|
|
|
expected.UpdatePrecedence()
|
|
|
|
//nolint:staticcheck
|
|
|
|
expected.SetHash()
|
|
|
|
}
|
|
|
|
require.True(t, watchFired(ws), "watch fired")
|
|
|
|
|
|
|
|
// Read it back out and verify it.
|
|
|
|
ws = memdb.NewWatchSet()
|
|
|
|
idx, _, actual, err := s.IntentionGet(ws, expected.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected.CreateIndex, idx)
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
|
|
|
|
if legacy {
|
|
|
|
// Change a value and test updating
|
|
|
|
legacyIxn.SourceNS = "foo"
|
|
|
|
lastIndex++
|
|
|
|
require.NoError(t, s.LegacyIntentionSet(lastIndex, legacyIxn))
|
|
|
|
|
|
|
|
// Change a value that isn't in the unique 4 tuple and check we don't
|
|
|
|
// incorrectly consider this a duplicate when updating.
|
|
|
|
legacyIxn.Action = structs.IntentionActionDeny
|
|
|
|
lastIndex++
|
|
|
|
require.NoError(t, s.LegacyIntentionSet(lastIndex, legacyIxn))
|
|
|
|
|
|
|
|
// Make sure the index got updated.
|
2021-01-30 00:17:40 +00:00
|
|
|
require.Equal(t, lastIndex, s.maxIndex(tableConnectIntentions))
|
2021-01-29 01:34:34 +00:00
|
|
|
require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
expected.SourceNS = legacyIxn.SourceNS
|
|
|
|
expected.Action = structs.IntentionActionDeny
|
|
|
|
expected.ModifyIndex = lastIndex
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Change a value and test updating
|
|
|
|
configEntry.Sources[0].Description = "test-desc1"
|
|
|
|
lastIndex++
|
|
|
|
require.NoError(t, configEntry.LegacyNormalize())
|
|
|
|
require.NoError(t, configEntry.LegacyValidate())
|
2021-02-03 23:10:38 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(lastIndex, configEntry.Clone()))
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
// Change a value that isn't in the unique 4 tuple and check we don't
|
|
|
|
// incorrectly consider this a duplicate when updating.
|
|
|
|
configEntry.Sources[0].Action = structs.IntentionActionDeny
|
|
|
|
lastIndex++
|
|
|
|
require.NoError(t, configEntry.LegacyNormalize())
|
|
|
|
require.NoError(t, configEntry.LegacyValidate())
|
2021-02-03 23:10:38 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(lastIndex, configEntry.Clone()))
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
// Make sure the config entry index got updated instead of the old intentions one
|
2021-01-29 01:34:34 +00:00
|
|
|
require.Equal(t, lastIndex, s.maxIndex(tableConfigEntries))
|
2021-01-30 00:17:40 +00:00
|
|
|
require.Equal(t, uint64(0), s.maxIndex(tableConnectIntentions))
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
expected.Description = configEntry.Sources[0].Description
|
|
|
|
expected.Action = structs.IntentionActionDeny
|
|
|
|
expected.UpdatedAt = *configEntry.Sources[0].LegacyUpdateTime
|
|
|
|
expected.ModifyIndex = lastIndex
|
|
|
|
//nolint:staticcheck
|
|
|
|
expected.UpdatePrecedence()
|
|
|
|
//nolint:staticcheck
|
|
|
|
expected.SetHash()
|
|
|
|
}
|
|
|
|
require.True(t, watchFired(ws), "watch fired")
|
|
|
|
|
|
|
|
// Read it back and verify the data was updated
|
|
|
|
ws = memdb.NewWatchSet()
|
|
|
|
idx, _, actual, err = s.IntentionGet(ws, expected.ID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected.ModifyIndex, idx)
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
|
|
|
|
if legacy {
|
|
|
|
// Attempt to insert another intention with duplicate 4-tuple
|
|
|
|
legacyIxn = &structs.Intention{
|
|
|
|
ID: testUUID(),
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "*",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "web",
|
|
|
|
Meta: map[string]string{},
|
|
|
|
}
|
2018-02-28 06:25:05 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Duplicate 4-tuple should cause an error
|
|
|
|
ws = memdb.NewWatchSet()
|
|
|
|
lastIndex++
|
|
|
|
require.Error(t, s.LegacyIntentionSet(lastIndex, legacyIxn))
|
2018-02-28 06:25:05 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Make sure the index did NOT get updated.
|
2021-01-30 00:17:40 +00:00
|
|
|
require.Equal(t, lastIndex-1, s.maxIndex(tableConnectIntentions))
|
2021-01-29 01:34:34 +00:00
|
|
|
require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
|
2020-10-06 18:24:05 +00:00
|
|
|
require.False(t, watchFired(ws), "watch not fired")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-02-28 06:25:05 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestStore_LegacyIntentionSet_failsAfterUpgrade(t *testing.T) {
|
|
|
|
// note: special case test doesn't need variants
|
|
|
|
s := testConfigStateStore(t)
|
|
|
|
|
|
|
|
ixn := structs.Intention{
|
2018-04-05 11:41:49 +00:00
|
|
|
ID: testUUID(),
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "*",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "web",
|
2020-10-06 18:24:05 +00:00
|
|
|
Action: structs.IntentionActionAllow,
|
2018-04-05 11:41:49 +00:00
|
|
|
Meta: map[string]string{},
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
err := s.LegacyIntentionSet(1, &ixn)
|
|
|
|
testutil.RequireErrorContains(t, err, ErrLegacyIntentionsAreDisabled.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_LegacyIntentionDelete_failsAfterUpgrade(t *testing.T) {
|
|
|
|
// note: special case test doesn't need variants
|
|
|
|
s := testConfigStateStore(t)
|
2018-04-05 11:41:49 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
err := s.LegacyIntentionDelete(1, testUUID())
|
|
|
|
testutil.RequireErrorContains(t, err, ErrLegacyIntentionsAreDisabled.Error())
|
2018-02-28 06:25:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 20:42:21 +00:00
|
|
|
func TestStore_IntentionMutation(t *testing.T) {
|
|
|
|
testBothIntentionFormats(t, func(t *testing.T, s *Store, legacy bool) {
|
|
|
|
if legacy {
|
|
|
|
mut := &structs.IntentionMutation{}
|
|
|
|
err := s.IntentionMutation(1, structs.IntentionOpCreate, mut)
|
|
|
|
testutil.RequireErrorContains(t, err, "state: IntentionMutation() is not allowed when intentions are not stored in config entries")
|
|
|
|
} else {
|
|
|
|
testStore_IntentionMutation(t, s)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testStore_IntentionMutation(t *testing.T, s *Store) {
|
|
|
|
lastIndex := uint64(1)
|
|
|
|
|
2021-07-22 18:20:45 +00:00
|
|
|
defaultEntMeta := structs.DefaultEnterpriseMetaInDefaultPartition()
|
2020-11-13 20:42:21 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
id1 = testUUID()
|
|
|
|
id2 = testUUID()
|
|
|
|
id3 = testUUID()
|
|
|
|
)
|
|
|
|
|
|
|
|
eqEntry := func(t *testing.T, expect, got *structs.ServiceIntentionsConfigEntry) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// Zero out some fields for comparison.
|
|
|
|
got = got.Clone()
|
|
|
|
got.RaftIndex = structs.RaftIndex{}
|
|
|
|
for _, src := range got.Sources {
|
|
|
|
src.LegacyCreateTime = nil
|
|
|
|
src.LegacyUpdateTime = nil
|
|
|
|
if len(src.LegacyMeta) == 0 {
|
|
|
|
src.LegacyMeta = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, expect, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to create an intention without an ID to prove that LegacyValidate is being called.
|
|
|
|
testutil.RequireErrorContains(t, s.IntentionMutation(lastIndex, structs.IntentionOpCreate, &structs.IntentionMutation{
|
|
|
|
Destination: structs.NewServiceName("api", defaultEntMeta),
|
|
|
|
Value: &structs.SourceIntention{
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
LegacyCreateTime: &testTimeA,
|
|
|
|
LegacyUpdateTime: &testTimeA,
|
|
|
|
},
|
|
|
|
}), `Sources[0].LegacyID must be set`)
|
|
|
|
|
|
|
|
// Create intention and create config entry
|
|
|
|
{
|
|
|
|
require.NoError(t, s.IntentionMutation(lastIndex, structs.IntentionOpCreate, &structs.IntentionMutation{
|
|
|
|
Destination: structs.NewServiceName("api", defaultEntMeta),
|
|
|
|
Value: &structs.SourceIntention{
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
LegacyID: id1,
|
|
|
|
LegacyCreateTime: &testTimeA,
|
|
|
|
LegacyUpdateTime: &testTimeA,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Ensure it's there now.
|
|
|
|
idx, entry, ixn, err := s.IntentionGet(nil, id1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, entry)
|
|
|
|
require.NotNil(t, ixn)
|
|
|
|
require.Equal(t, lastIndex, idx)
|
|
|
|
|
|
|
|
eqEntry(t, &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
LegacyID: id1,
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
Precedence: 9,
|
|
|
|
Type: structs.IntentionSourceConsul,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, entry)
|
|
|
|
|
|
|
|
// only one
|
|
|
|
_, entries, err := s.ConfigEntries(nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
|
|
|
|
lastIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to create a duplicate intention.
|
|
|
|
{
|
|
|
|
testutil.RequireErrorContains(t, s.IntentionMutation(lastIndex, structs.IntentionOpCreate, &structs.IntentionMutation{
|
|
|
|
Destination: structs.NewServiceName("api", defaultEntMeta),
|
|
|
|
Value: &structs.SourceIntention{
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionDeny,
|
|
|
|
LegacyID: id2,
|
|
|
|
LegacyCreateTime: &testTimeB,
|
|
|
|
LegacyUpdateTime: &testTimeB,
|
|
|
|
},
|
|
|
|
}), `more than once`)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create intention with existing config entry
|
|
|
|
{
|
|
|
|
require.NoError(t, s.IntentionMutation(lastIndex, structs.IntentionOpCreate, &structs.IntentionMutation{
|
|
|
|
Destination: structs.NewServiceName("api", defaultEntMeta),
|
|
|
|
Value: &structs.SourceIntention{
|
|
|
|
Name: "debug",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionDeny,
|
|
|
|
LegacyID: id2,
|
|
|
|
LegacyCreateTime: &testTimeB,
|
|
|
|
LegacyUpdateTime: &testTimeB,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Ensure it's there now.
|
|
|
|
idx, entry, ixn, err := s.IntentionGet(nil, id2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, entry)
|
|
|
|
require.NotNil(t, ixn)
|
|
|
|
require.Equal(t, lastIndex, idx)
|
|
|
|
|
|
|
|
eqEntry(t, &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
LegacyID: id1,
|
|
|
|
Precedence: 9,
|
|
|
|
Type: structs.IntentionSourceConsul,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "debug",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionDeny,
|
|
|
|
LegacyID: id2,
|
|
|
|
Precedence: 9,
|
|
|
|
Type: structs.IntentionSourceConsul,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, entry)
|
|
|
|
|
|
|
|
// only one
|
|
|
|
_, entries, err := s.ConfigEntries(nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
|
|
|
|
lastIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to update an intention without specifying an ID
|
|
|
|
testutil.RequireErrorContains(t, s.IntentionMutation(lastIndex, structs.IntentionOpUpdate, &structs.IntentionMutation{
|
|
|
|
ID: "",
|
|
|
|
Destination: structs.NewServiceName("api", defaultEntMeta),
|
|
|
|
Value: &structs.SourceIntention{
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
},
|
|
|
|
}), `failed config entry lookup: index error: UUID must be 36 characters`)
|
|
|
|
|
|
|
|
// Try to update a non-existent intention
|
|
|
|
testutil.RequireErrorContains(t, s.IntentionMutation(lastIndex, structs.IntentionOpUpdate, &structs.IntentionMutation{
|
|
|
|
ID: id3,
|
|
|
|
Destination: structs.NewServiceName("api", defaultEntMeta),
|
|
|
|
Value: &structs.SourceIntention{
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
},
|
|
|
|
}), `Cannot modify non-existent intention`)
|
|
|
|
|
|
|
|
// Update an existing intention by ID
|
|
|
|
{
|
|
|
|
require.NoError(t, s.IntentionMutation(lastIndex, structs.IntentionOpUpdate, &structs.IntentionMutation{
|
|
|
|
ID: id2,
|
|
|
|
Destination: structs.NewServiceName("api", defaultEntMeta),
|
|
|
|
Value: &structs.SourceIntention{
|
|
|
|
Name: "debug",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionDeny,
|
|
|
|
LegacyID: id2,
|
|
|
|
LegacyCreateTime: &testTimeB,
|
|
|
|
LegacyUpdateTime: &testTimeC,
|
|
|
|
Description: "op update",
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Ensure it's there now.
|
|
|
|
idx, entry, ixn, err := s.IntentionGet(nil, id2)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, entry)
|
|
|
|
require.NotNil(t, ixn)
|
|
|
|
require.Equal(t, lastIndex, idx)
|
|
|
|
|
|
|
|
eqEntry(t, &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
LegacyID: id1,
|
|
|
|
Precedence: 9,
|
|
|
|
Type: structs.IntentionSourceConsul,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "debug",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionDeny,
|
|
|
|
LegacyID: id2,
|
|
|
|
Precedence: 9,
|
|
|
|
Type: structs.IntentionSourceConsul,
|
|
|
|
Description: "op update",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, entry)
|
|
|
|
|
|
|
|
// only one
|
|
|
|
_, entries, err := s.ConfigEntries(nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
|
|
|
|
lastIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to delete a non-existent intention
|
|
|
|
testutil.RequireErrorContains(t, s.IntentionMutation(lastIndex, structs.IntentionOpDelete, &structs.IntentionMutation{
|
|
|
|
ID: id3,
|
|
|
|
}), `Cannot delete non-existent intention`)
|
|
|
|
|
|
|
|
// delete by id
|
|
|
|
{
|
|
|
|
require.NoError(t, s.IntentionMutation(lastIndex, structs.IntentionOpDelete, &structs.IntentionMutation{
|
|
|
|
ID: id1,
|
|
|
|
}))
|
|
|
|
|
|
|
|
// only one
|
|
|
|
_, entries, err := s.ConfigEntries(nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
|
|
|
|
eqEntry(t, &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "debug",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionDeny,
|
|
|
|
LegacyID: id2,
|
|
|
|
Precedence: 9,
|
|
|
|
Type: structs.IntentionSourceConsul,
|
|
|
|
Description: "op update",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, entries[0].(*structs.ServiceIntentionsConfigEntry))
|
|
|
|
|
|
|
|
lastIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete last one by id
|
|
|
|
{
|
|
|
|
require.NoError(t, s.IntentionMutation(lastIndex, structs.IntentionOpDelete, &structs.IntentionMutation{
|
|
|
|
ID: id2,
|
|
|
|
}))
|
|
|
|
|
|
|
|
// none one
|
|
|
|
_, entries, err := s.ConfigEntries(nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, entries)
|
|
|
|
|
|
|
|
lastIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
// upsert intention for first time
|
|
|
|
{
|
|
|
|
require.NoError(t, s.IntentionMutation(lastIndex, structs.IntentionOpUpsert, &structs.IntentionMutation{
|
|
|
|
Destination: structs.NewServiceName("api", defaultEntMeta),
|
|
|
|
Value: &structs.SourceIntention{
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Ensure it's there now.
|
|
|
|
idx, entry, ixn, err := s.IntentionGetExact(nil, &structs.IntentionQueryExact{
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "web",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "api",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, entry)
|
|
|
|
require.NotNil(t, ixn)
|
|
|
|
require.Equal(t, lastIndex, idx)
|
|
|
|
|
|
|
|
eqEntry(t, &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
Precedence: 9,
|
|
|
|
Type: structs.IntentionSourceConsul,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, entry)
|
|
|
|
|
|
|
|
// only one
|
|
|
|
_, entries, err := s.ConfigEntries(nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
|
|
|
|
lastIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
// upsert over itself (REPLACE)
|
|
|
|
{
|
|
|
|
require.NoError(t, s.IntentionMutation(lastIndex, structs.IntentionOpUpsert, &structs.IntentionMutation{
|
|
|
|
Destination: structs.NewServiceName("api", defaultEntMeta),
|
|
|
|
Source: structs.NewServiceName("web", defaultEntMeta),
|
|
|
|
Value: &structs.SourceIntention{
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
Description: "upserted over",
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Ensure it's there now.
|
|
|
|
idx, entry, ixn, err := s.IntentionGetExact(nil, &structs.IntentionQueryExact{
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "web",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "api",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, entry)
|
|
|
|
require.NotNil(t, ixn)
|
|
|
|
require.Equal(t, lastIndex, idx)
|
|
|
|
require.Equal(t, "upserted over", ixn.Description)
|
|
|
|
|
|
|
|
eqEntry(t, &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
Precedence: 9,
|
|
|
|
Type: structs.IntentionSourceConsul,
|
|
|
|
Description: "upserted over",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, entry)
|
|
|
|
|
|
|
|
// only one
|
|
|
|
_, entries, err := s.ConfigEntries(nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
|
|
|
|
lastIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
// upsert into existing config entry (APPEND)
|
|
|
|
{
|
|
|
|
require.NoError(t, s.IntentionMutation(lastIndex, structs.IntentionOpUpsert, &structs.IntentionMutation{
|
|
|
|
Destination: structs.NewServiceName("api", defaultEntMeta),
|
|
|
|
Source: structs.NewServiceName("debug", defaultEntMeta),
|
|
|
|
Value: &structs.SourceIntention{
|
|
|
|
Name: "debug",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionDeny,
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Ensure it's there now.
|
|
|
|
idx, entry, ixn, err := s.IntentionGetExact(nil, &structs.IntentionQueryExact{
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "debug",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "api",
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, entry)
|
|
|
|
require.NotNil(t, ixn)
|
|
|
|
require.Equal(t, lastIndex, idx)
|
|
|
|
|
|
|
|
eqEntry(t, &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
Precedence: 9,
|
|
|
|
Type: structs.IntentionSourceConsul,
|
|
|
|
Description: "upserted over",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "debug",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionDeny,
|
|
|
|
Precedence: 9,
|
|
|
|
Type: structs.IntentionSourceConsul,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, entry)
|
|
|
|
|
|
|
|
// only one
|
|
|
|
_, entries, err := s.ConfigEntries(nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
|
|
|
|
lastIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to delete a non-existent intention by name
|
|
|
|
require.NoError(t, s.IntentionMutation(lastIndex, structs.IntentionOpDelete, &structs.IntentionMutation{
|
|
|
|
Destination: structs.NewServiceName("api", defaultEntMeta),
|
|
|
|
Source: structs.NewServiceName("blurb", defaultEntMeta),
|
|
|
|
}))
|
|
|
|
|
|
|
|
// delete by name
|
|
|
|
{
|
|
|
|
require.NoError(t, s.IntentionMutation(lastIndex, structs.IntentionOpDelete, &structs.IntentionMutation{
|
|
|
|
Destination: structs.NewServiceName("api", defaultEntMeta),
|
|
|
|
Source: structs.NewServiceName("web", defaultEntMeta),
|
|
|
|
}))
|
|
|
|
|
|
|
|
// only one
|
|
|
|
_, entries, err := s.ConfigEntries(nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
|
|
|
|
eqEntry(t, &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "debug",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionDeny,
|
|
|
|
Precedence: 9,
|
|
|
|
Type: structs.IntentionSourceConsul,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, entries[0].(*structs.ServiceIntentionsConfigEntry))
|
|
|
|
|
|
|
|
lastIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete last one by name
|
|
|
|
{
|
|
|
|
require.NoError(t, s.IntentionMutation(lastIndex, structs.IntentionOpDelete, &structs.IntentionMutation{
|
|
|
|
Destination: structs.NewServiceName("api", defaultEntMeta),
|
|
|
|
Source: structs.NewServiceName("debug", defaultEntMeta),
|
|
|
|
}))
|
|
|
|
|
|
|
|
// none one
|
|
|
|
_, entries, err := s.ConfigEntries(nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, entries)
|
|
|
|
|
|
|
|
lastIndex++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to update an intention with an ID on a non-legacy config entry.
|
|
|
|
{
|
|
|
|
idFake := testUUID()
|
|
|
|
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(lastIndex, &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "new",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
Precedence: 9,
|
|
|
|
Type: structs.IntentionSourceConsul,
|
|
|
|
},
|
|
|
|
},
|
2021-02-03 23:10:38 +00:00
|
|
|
}))
|
2020-11-13 20:42:21 +00:00
|
|
|
|
|
|
|
lastIndex++
|
|
|
|
|
|
|
|
// ...via create
|
|
|
|
testutil.RequireErrorContains(t, s.IntentionMutation(lastIndex, structs.IntentionOpCreate, &structs.IntentionMutation{
|
|
|
|
Destination: structs.NewServiceName("new", defaultEntMeta),
|
|
|
|
Value: &structs.SourceIntention{
|
|
|
|
Name: "old",
|
|
|
|
EnterpriseMeta: *defaultEntMeta,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
LegacyID: idFake,
|
|
|
|
},
|
|
|
|
}), `cannot use legacy intention API to edit intentions with a destination`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestStore_LegacyIntentionSet_emptyId(t *testing.T) {
|
|
|
|
// note: irrelevant test for config entries variant
|
2018-02-28 06:25:05 +00:00
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
2020-10-06 18:24:05 +00:00
|
|
|
_, _, _, err := s.IntentionGet(ws, testUUID())
|
|
|
|
require.NoError(t, err)
|
2018-02-28 06:25:05 +00:00
|
|
|
|
|
|
|
// Inserting a with empty ID is disallowed.
|
2020-10-06 18:24:05 +00:00
|
|
|
err = s.LegacyIntentionSet(1, &structs.Intention{})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), ErrMissingIntentionID.Error())
|
2018-02-28 06:25:05 +00:00
|
|
|
|
|
|
|
// Index is not updated if nothing is saved.
|
2021-01-30 00:17:40 +00:00
|
|
|
require.Equal(t, s.maxIndex(tableConnectIntentions), uint64(0))
|
2021-01-29 01:34:34 +00:00
|
|
|
require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
|
2020-10-06 18:24:05 +00:00
|
|
|
|
|
|
|
require.False(t, watchFired(ws), "watch fired")
|
2018-02-28 06:25:05 +00:00
|
|
|
}
|
2018-02-28 17:53:21 +00:00
|
|
|
|
2018-03-03 16:43:19 +00:00
|
|
|
func TestStore_IntentionSet_updateCreatedAt(t *testing.T) {
|
2020-10-06 18:24:05 +00:00
|
|
|
testBothIntentionFormats(t, func(t *testing.T, s *Store, legacy bool) {
|
|
|
|
// Build a valid intention
|
|
|
|
var (
|
2020-11-13 20:42:21 +00:00
|
|
|
id = testUUID()
|
2020-10-06 18:24:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if legacy {
|
|
|
|
ixn := structs.Intention{
|
2020-11-13 20:42:21 +00:00
|
|
|
ID: id,
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "*",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "web",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
CreatedAt: testTimeA,
|
|
|
|
UpdatedAt: testTimeA,
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
2018-03-03 16:43:19 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Insert
|
|
|
|
require.NoError(t, s.LegacyIntentionSet(1, &ixn))
|
|
|
|
|
|
|
|
// Change a value and test updating
|
|
|
|
ixnUpdate := ixn
|
2020-11-13 20:42:21 +00:00
|
|
|
ixnUpdate.CreatedAt = testTimeB
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, s.LegacyIntentionSet(2, &ixnUpdate))
|
|
|
|
|
|
|
|
id = ixn.ID
|
|
|
|
|
|
|
|
} else {
|
|
|
|
conf := &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "web",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
2020-11-13 20:42:21 +00:00
|
|
|
LegacyID: id,
|
|
|
|
Name: "*",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
LegacyMeta: map[string]string{},
|
|
|
|
LegacyCreateTime: &testTimeA,
|
|
|
|
LegacyUpdateTime: &testTimeA,
|
2020-10-06 18:24:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-03-03 16:43:19 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, conf.LegacyNormalize())
|
|
|
|
require.NoError(t, conf.LegacyValidate())
|
2021-02-03 23:10:38 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(1, conf.Clone()))
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
2018-03-03 16:43:19 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Read it back and verify
|
|
|
|
_, _, actual, err := s.IntentionGet(nil, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, actual)
|
2020-11-13 20:42:21 +00:00
|
|
|
require.Equal(t, testTimeA, actual.CreatedAt)
|
2020-10-06 18:24:05 +00:00
|
|
|
})
|
2018-03-03 16:43:19 +00:00
|
|
|
}
|
|
|
|
|
2018-03-03 17:16:26 +00:00
|
|
|
func TestStore_IntentionSet_metaNil(t *testing.T) {
|
2020-10-06 18:24:05 +00:00
|
|
|
testBothIntentionFormats(t, func(t *testing.T, s *Store, legacy bool) {
|
|
|
|
id := testUUID()
|
|
|
|
if legacy {
|
|
|
|
// Build a valid intention
|
|
|
|
ixn := &structs.Intention{
|
2020-11-13 20:42:21 +00:00
|
|
|
ID: id,
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "*",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "web",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
CreatedAt: testTimeA,
|
|
|
|
UpdatedAt: testTimeA,
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
2018-03-03 17:16:26 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Insert
|
|
|
|
require.NoError(t, s.LegacyIntentionSet(1, ixn))
|
|
|
|
} else {
|
|
|
|
// Build a valid intention
|
|
|
|
conf := &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "web",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
2020-11-13 20:42:21 +00:00
|
|
|
LegacyID: id,
|
|
|
|
Name: "*",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
LegacyCreateTime: &testTimeA,
|
|
|
|
LegacyUpdateTime: &testTimeA,
|
2020-10-06 18:24:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-03-03 17:16:26 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Insert
|
|
|
|
require.NoError(t, conf.LegacyNormalize())
|
|
|
|
require.NoError(t, conf.LegacyValidate())
|
2021-02-03 23:10:38 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(1, conf.Clone()))
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
2018-03-03 17:16:26 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Read it back and verify
|
|
|
|
_, _, actual, err := s.IntentionGet(nil, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, actual.Meta)
|
|
|
|
})
|
2018-03-03 17:16:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_IntentionSet_metaSet(t *testing.T) {
|
2020-10-06 18:24:05 +00:00
|
|
|
testBothIntentionFormats(t, func(t *testing.T, s *Store, legacy bool) {
|
|
|
|
var (
|
|
|
|
id = testUUID()
|
|
|
|
expectMeta = map[string]string{"foo": "bar"}
|
|
|
|
)
|
|
|
|
if legacy {
|
|
|
|
// Build a valid intention
|
|
|
|
ixn := structs.Intention{
|
2020-11-13 20:42:21 +00:00
|
|
|
ID: id,
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "*",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "web",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
CreatedAt: testTimeA,
|
|
|
|
UpdatedAt: testTimeA,
|
|
|
|
Meta: expectMeta,
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
2018-03-03 17:16:26 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Insert
|
|
|
|
require.NoError(t, s.LegacyIntentionSet(1, &ixn))
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Build a valid intention
|
|
|
|
conf := &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "web",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
2020-11-13 20:42:21 +00:00
|
|
|
LegacyID: id,
|
|
|
|
Name: "*",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
LegacyCreateTime: &testTimeA,
|
|
|
|
LegacyUpdateTime: &testTimeA,
|
|
|
|
LegacyMeta: expectMeta,
|
2020-10-06 18:24:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-03-03 17:16:26 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Insert
|
|
|
|
require.NoError(t, conf.LegacyNormalize())
|
|
|
|
require.NoError(t, conf.LegacyValidate())
|
2021-02-03 23:10:38 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(1, conf.Clone()))
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
2018-03-03 17:16:26 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Read it back and verify
|
|
|
|
_, _, actual, err := s.IntentionGet(nil, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expectMeta, actual.Meta)
|
|
|
|
})
|
2018-03-03 17:16:26 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 05:21:59 +00:00
|
|
|
func TestStore_IntentionDelete(t *testing.T) {
|
2020-10-06 18:24:05 +00:00
|
|
|
testBothIntentionFormats(t, func(t *testing.T, s *Store, legacy bool) {
|
|
|
|
lastIndex := uint64(1)
|
2018-03-01 05:21:59 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Call Get to populate the watch set
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
_, _, _, err := s.IntentionGet(ws, testUUID())
|
|
|
|
require.NoError(t, err)
|
2018-03-01 05:21:59 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
id := testUUID()
|
|
|
|
// Create
|
|
|
|
if legacy {
|
|
|
|
ixn := &structs.Intention{
|
2020-11-13 20:42:21 +00:00
|
|
|
ID: id,
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: "*",
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: "web",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
CreatedAt: testTimeA,
|
|
|
|
UpdatedAt: testTimeA,
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
|
|
|
lastIndex++
|
|
|
|
require.NoError(t, s.LegacyIntentionSet(lastIndex, ixn))
|
|
|
|
|
|
|
|
// Make sure the index got updated.
|
2021-01-30 00:17:40 +00:00
|
|
|
require.Equal(t, s.maxIndex(tableConnectIntentions), lastIndex)
|
2021-01-29 01:34:34 +00:00
|
|
|
require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
|
2020-10-06 18:24:05 +00:00
|
|
|
} else {
|
|
|
|
conf := &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "web",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
2020-11-13 20:42:21 +00:00
|
|
|
LegacyID: id,
|
|
|
|
Name: "*",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
LegacyCreateTime: &testTimeA,
|
|
|
|
LegacyUpdateTime: &testTimeA,
|
2020-10-06 18:24:05 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-03-01 05:21:59 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Insert
|
|
|
|
require.NoError(t, conf.LegacyNormalize())
|
|
|
|
require.NoError(t, conf.LegacyValidate())
|
2021-02-03 23:10:38 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(1, conf.Clone()))
|
2018-03-01 05:21:59 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Make sure the index got updated.
|
2021-01-29 01:34:34 +00:00
|
|
|
require.Equal(t, s.maxIndex(tableConfigEntries), lastIndex)
|
2021-01-30 00:17:40 +00:00
|
|
|
require.Equal(t, uint64(0), s.maxIndex(tableConnectIntentions))
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
|
|
|
require.True(t, watchFired(ws), "watch fired")
|
|
|
|
|
|
|
|
// Sanity check to make sure it's there.
|
|
|
|
idx, _, actual, err := s.IntentionGet(nil, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, idx, lastIndex)
|
|
|
|
require.NotNil(t, actual)
|
|
|
|
|
|
|
|
// Delete
|
|
|
|
if legacy {
|
|
|
|
lastIndex++
|
|
|
|
require.NoError(t, s.LegacyIntentionDelete(lastIndex, id))
|
|
|
|
|
|
|
|
// Make sure the index got updated.
|
2021-01-30 00:17:40 +00:00
|
|
|
require.Equal(t, s.maxIndex(tableConnectIntentions), lastIndex)
|
2021-01-29 01:34:34 +00:00
|
|
|
require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
|
2020-10-06 18:24:05 +00:00
|
|
|
} else {
|
|
|
|
lastIndex++
|
|
|
|
require.NoError(t, s.DeleteConfigEntry(lastIndex, structs.ServiceIntentions, "web", nil))
|
|
|
|
|
|
|
|
// Make sure the index got updated.
|
2021-01-29 01:34:34 +00:00
|
|
|
require.Equal(t, s.maxIndex(tableConfigEntries), lastIndex)
|
2021-01-30 00:17:40 +00:00
|
|
|
require.Equal(t, uint64(0), s.maxIndex(tableConnectIntentions))
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
|
|
|
require.True(t, watchFired(ws), "watch fired")
|
|
|
|
|
|
|
|
// Sanity check to make sure it's not there.
|
|
|
|
idx, _, actual, err = s.IntentionGet(nil, id)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, idx, lastIndex)
|
|
|
|
require.Nil(t, actual)
|
|
|
|
})
|
2018-03-01 05:21:59 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 17:53:21 +00:00
|
|
|
func TestStore_IntentionsList(t *testing.T) {
|
2020-10-06 18:24:05 +00:00
|
|
|
testBothIntentionFormats(t, func(t *testing.T, s *Store, legacy bool) {
|
|
|
|
lastIndex := uint64(0)
|
|
|
|
if legacy {
|
|
|
|
lastIndex = 1 // minor state machine implementation difference
|
|
|
|
}
|
2018-02-28 17:53:21 +00:00
|
|
|
|
2021-07-22 18:20:45 +00:00
|
|
|
entMeta := structs.WildcardEnterpriseMetaInDefaultPartition()
|
2020-06-26 21:59:15 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Querying with no results returns nil.
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
idx, res, fromConfig, err := s.Intentions(ws, entMeta)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, !legacy, fromConfig)
|
|
|
|
require.Nil(t, res)
|
|
|
|
require.Equal(t, lastIndex, idx)
|
|
|
|
|
|
|
|
testIntention := func(src, dst string) *structs.Intention {
|
|
|
|
return &structs.Intention{
|
|
|
|
ID: testUUID(),
|
|
|
|
SourceNS: "default",
|
|
|
|
SourceName: src,
|
|
|
|
DestinationNS: "default",
|
|
|
|
DestinationName: dst,
|
|
|
|
SourceType: structs.IntentionSourceConsul,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
Meta: map[string]string{},
|
2020-11-13 20:42:21 +00:00
|
|
|
CreatedAt: testTimeA,
|
|
|
|
UpdatedAt: testTimeA,
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-26 21:59:15 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
testConfigEntry := func(dst string, srcs ...string) *structs.ServiceIntentionsConfigEntry {
|
|
|
|
conf := &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: dst,
|
|
|
|
}
|
|
|
|
id := testUUID()
|
|
|
|
for _, src := range srcs {
|
|
|
|
conf.Sources = append(conf.Sources, &structs.SourceIntention{
|
2020-11-13 20:42:21 +00:00
|
|
|
LegacyID: id,
|
|
|
|
Name: src,
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
LegacyCreateTime: &testTimeA,
|
|
|
|
LegacyUpdateTime: &testTimeA,
|
2020-10-06 18:24:05 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return conf
|
2020-06-26 21:59:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-13 20:42:21 +00:00
|
|
|
clearIrrelevantFields := func(ixns ...*structs.Intention) {
|
2020-10-06 18:24:05 +00:00
|
|
|
// Clear fields irrelevant for comparison.
|
|
|
|
for _, ixn := range ixns {
|
|
|
|
ixn.Hash = nil
|
|
|
|
ixn.CreateIndex = 0
|
|
|
|
ixn.ModifyIndex = 0
|
|
|
|
ixn.CreatedAt = time.Time{}
|
|
|
|
ixn.UpdatedAt = time.Time{}
|
|
|
|
}
|
|
|
|
}
|
2018-02-28 17:53:21 +00:00
|
|
|
|
2020-11-13 20:42:21 +00:00
|
|
|
cmpIntention := func(ixn *structs.Intention, id string) *structs.Intention {
|
|
|
|
ixn2 := ixn.Clone()
|
|
|
|
ixn2.ID = id
|
|
|
|
clearIrrelevantFields(ixn2)
|
|
|
|
//nolint:staticcheck
|
|
|
|
ixn2.UpdatePrecedence()
|
|
|
|
return ixn2
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
var (
|
|
|
|
expectIDs []string
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create some intentions
|
|
|
|
if legacy {
|
|
|
|
ixns := structs.Intentions{
|
|
|
|
testIntention("foo", "bar"),
|
|
|
|
testIntention("*", "bar"),
|
|
|
|
testIntention("foo", "*"),
|
|
|
|
testIntention("*", "*"),
|
|
|
|
}
|
2018-02-28 17:53:21 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
for _, ixn := range ixns {
|
|
|
|
expectIDs = append(expectIDs, ixn.ID)
|
|
|
|
lastIndex++
|
|
|
|
require.NoError(t, s.LegacyIntentionSet(lastIndex, ixn))
|
|
|
|
}
|
2020-06-26 21:59:15 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
} else {
|
|
|
|
confs := []*structs.ServiceIntentionsConfigEntry{
|
|
|
|
testConfigEntry("bar", "foo", "*"),
|
|
|
|
testConfigEntry("*", "foo", "*"),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, conf := range confs {
|
|
|
|
require.NoError(t, conf.LegacyNormalize())
|
|
|
|
require.NoError(t, conf.LegacyValidate())
|
|
|
|
lastIndex++
|
2021-02-03 23:10:38 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(lastIndex, conf))
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expectIDs = []string{
|
|
|
|
confs[0].Sources[0].LegacyID, // foo->bar
|
|
|
|
confs[0].Sources[1].LegacyID, // *->bar
|
|
|
|
confs[1].Sources[0].LegacyID, // foo->*
|
|
|
|
confs[1].Sources[1].LegacyID, // *->*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.True(t, watchFired(ws), "watch fired")
|
|
|
|
|
|
|
|
// Read it back and verify.
|
|
|
|
expected := structs.Intentions{
|
|
|
|
cmpIntention(testIntention("foo", "bar"), expectIDs[0]),
|
|
|
|
cmpIntention(testIntention("*", "bar"), expectIDs[1]),
|
|
|
|
cmpIntention(testIntention("foo", "*"), expectIDs[2]),
|
|
|
|
cmpIntention(testIntention("*", "*"), expectIDs[3]),
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, actual, fromConfig, err := s.Intentions(nil, entMeta)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, !legacy, fromConfig)
|
|
|
|
require.Equal(t, lastIndex, idx)
|
|
|
|
|
2020-11-13 20:42:21 +00:00
|
|
|
clearIrrelevantFields(actual...)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
})
|
2018-02-28 17:53:21 +00:00
|
|
|
}
|
2018-03-02 20:56:39 +00:00
|
|
|
|
|
|
|
// Test the matrix of match logic.
|
|
|
|
//
|
|
|
|
// Note that this doesn't need to test the intention sort logic exhaustively
|
|
|
|
// since this is tested in their sort implementation in the structs.
|
|
|
|
func TestStore_IntentionMatch_table(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
Name string
|
|
|
|
Insert [][]string // List of intentions to insert
|
|
|
|
Query [][]string // List of intentions to match
|
|
|
|
Expected [][][]string // List of matches, where each match is a list of intentions
|
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testCase{
|
|
|
|
{
|
2020-10-06 18:24:05 +00:00
|
|
|
"single exact name",
|
2018-03-02 20:56:39 +00:00
|
|
|
[][]string{
|
2020-10-06 18:24:05 +00:00
|
|
|
{"bar", "example"},
|
|
|
|
{"baz", "example"}, // shouldn't match
|
|
|
|
{"*", "example"},
|
2018-03-02 20:56:39 +00:00
|
|
|
},
|
|
|
|
[][]string{
|
2020-10-06 18:24:05 +00:00
|
|
|
{"bar"},
|
2018-03-02 20:56:39 +00:00
|
|
|
},
|
|
|
|
[][][]string{
|
|
|
|
{
|
2020-10-06 18:24:05 +00:00
|
|
|
{"bar", "example"},
|
|
|
|
{"*", "example"},
|
2018-03-02 20:56:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-10-06 18:24:05 +00:00
|
|
|
"multiple exact name",
|
2018-03-02 20:56:39 +00:00
|
|
|
[][]string{
|
2020-10-06 18:24:05 +00:00
|
|
|
{"bar", "example"},
|
|
|
|
{"baz", "example"}, // shouldn't match
|
|
|
|
{"*", "example"},
|
2018-03-02 20:56:39 +00:00
|
|
|
},
|
|
|
|
[][]string{
|
2020-10-06 18:24:05 +00:00
|
|
|
{"bar"},
|
|
|
|
{"baz"},
|
2018-03-02 20:56:39 +00:00
|
|
|
},
|
|
|
|
[][][]string{
|
|
|
|
{
|
2020-10-06 18:24:05 +00:00
|
|
|
{"bar", "example"},
|
|
|
|
{"*", "example"},
|
2018-03-02 20:56:39 +00:00
|
|
|
},
|
|
|
|
{
|
2020-10-06 18:24:05 +00:00
|
|
|
{"baz", "example"},
|
|
|
|
{"*", "example"},
|
2018-03-02 20:56:39 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-04-05 11:41:49 +00:00
|
|
|
|
|
|
|
{
|
2020-10-06 18:24:05 +00:00
|
|
|
"single exact name with duplicate destinations",
|
2018-04-05 11:41:49 +00:00
|
|
|
[][]string{
|
2020-10-06 18:24:05 +00:00
|
|
|
// 2-tuple specifies src and destination to test duplicate destinations
|
2018-04-05 11:41:49 +00:00
|
|
|
// with different sources. We flip them around to test in both
|
|
|
|
// directions. The first pair are the ones searched on in both cases so
|
|
|
|
// the duplicates need to be there.
|
2020-10-06 18:24:05 +00:00
|
|
|
{"bar", "*"},
|
|
|
|
{"*", "*"},
|
2018-04-05 11:41:49 +00:00
|
|
|
},
|
|
|
|
[][]string{
|
2020-10-06 18:24:05 +00:00
|
|
|
{"bar"},
|
2018-04-05 11:41:49 +00:00
|
|
|
},
|
|
|
|
[][][]string{
|
|
|
|
{
|
2020-10-06 18:24:05 +00:00
|
|
|
{"bar", "*"},
|
|
|
|
{"*", "*"},
|
2018-04-05 11:41:49 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-03-02 20:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// testRunner implements the test for a single case, but can be
|
|
|
|
// parameterized to run for both source and destination so we can
|
|
|
|
// test both cases.
|
2020-10-06 18:24:05 +00:00
|
|
|
testRunner := func(t *testing.T, s *Store, legacy bool, tc testCase, typ structs.IntentionMatchType) {
|
|
|
|
lastIndex := uint64(0)
|
|
|
|
if legacy {
|
|
|
|
lastIndex = 1 // minor state machine implementation difference
|
|
|
|
}
|
|
|
|
|
2018-03-02 20:56:39 +00:00
|
|
|
// Insert the set
|
2020-10-06 18:24:05 +00:00
|
|
|
var ixns []*structs.Intention
|
2018-03-02 20:56:39 +00:00
|
|
|
for _, v := range tc.Insert {
|
2020-10-06 18:24:05 +00:00
|
|
|
if len(v) != 2 {
|
|
|
|
panic("invalid input")
|
|
|
|
}
|
|
|
|
ixn := &structs.Intention{
|
|
|
|
ID: testUUID(),
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
}
|
2018-03-02 20:56:39 +00:00
|
|
|
switch typ {
|
|
|
|
case structs.IntentionMatchDestination:
|
2020-10-06 18:24:05 +00:00
|
|
|
ixn.DestinationNS = "default"
|
|
|
|
ixn.DestinationName = v[0]
|
|
|
|
ixn.SourceNS = "default"
|
2018-03-02 20:56:39 +00:00
|
|
|
ixn.SourceName = v[1]
|
2020-10-06 18:24:05 +00:00
|
|
|
case structs.IntentionMatchSource:
|
|
|
|
ixn.SourceNS = "default"
|
|
|
|
ixn.SourceName = v[0]
|
|
|
|
ixn.DestinationNS = "default"
|
|
|
|
ixn.DestinationName = v[1]
|
|
|
|
default:
|
|
|
|
panic("unexpected")
|
2018-03-02 20:56:39 +00:00
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
ixns = append(ixns, ixn)
|
|
|
|
}
|
2018-03-02 20:56:39 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
if legacy {
|
|
|
|
for _, ixn := range ixns {
|
|
|
|
lastIndex++
|
|
|
|
require.NoError(t, s.LegacyIntentionSet(lastIndex, ixn))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entries := structs.MigrateIntentions(ixns)
|
|
|
|
for _, conf := range entries {
|
|
|
|
require.NoError(t, conf.LegacyNormalize())
|
|
|
|
require.NoError(t, conf.LegacyValidate())
|
|
|
|
lastIndex++
|
2021-02-03 23:10:38 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(lastIndex, conf))
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
2018-03-02 20:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Build the arguments
|
|
|
|
args := &structs.IntentionQueryMatch{Type: typ}
|
|
|
|
for _, q := range tc.Query {
|
2020-10-06 18:24:05 +00:00
|
|
|
if len(q) != 1 {
|
|
|
|
panic("wrong length")
|
|
|
|
}
|
2018-03-02 20:56:39 +00:00
|
|
|
args.Entries = append(args.Entries, structs.IntentionMatchEntry{
|
2020-10-06 18:24:05 +00:00
|
|
|
Namespace: "default",
|
|
|
|
Name: q[0],
|
2018-03-02 20:56:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match
|
|
|
|
_, matches, err := s.IntentionMatch(nil, args)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, err)
|
2018-03-02 20:56:39 +00:00
|
|
|
|
|
|
|
// Should have equal lengths
|
2018-04-05 11:41:49 +00:00
|
|
|
require.Len(t, matches, len(tc.Expected))
|
2018-03-02 20:56:39 +00:00
|
|
|
|
|
|
|
// Verify matches
|
|
|
|
for i, expected := range tc.Expected {
|
2020-10-06 18:24:05 +00:00
|
|
|
for _, exp := range expected {
|
|
|
|
if len(exp) != 2 {
|
|
|
|
panic("invalid input")
|
|
|
|
}
|
|
|
|
}
|
2018-03-02 20:56:39 +00:00
|
|
|
var actual [][]string
|
|
|
|
for _, ixn := range matches[i] {
|
|
|
|
switch typ {
|
|
|
|
case structs.IntentionMatchDestination:
|
2020-10-06 18:24:05 +00:00
|
|
|
actual = append(actual, []string{
|
|
|
|
ixn.DestinationName,
|
|
|
|
ixn.SourceName,
|
|
|
|
})
|
2018-03-02 20:56:39 +00:00
|
|
|
case structs.IntentionMatchSource:
|
2020-10-06 18:24:05 +00:00
|
|
|
actual = append(actual, []string{
|
|
|
|
ixn.SourceName,
|
|
|
|
ixn.DestinationName,
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
panic("unexpected")
|
2018-03-02 20:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Equal(t, expected, actual)
|
2018-03-02 20:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name+" (destination)", func(t *testing.T) {
|
2020-10-06 18:24:05 +00:00
|
|
|
testBothIntentionFormats(t, func(t *testing.T, s *Store, legacy bool) {
|
|
|
|
testRunner(t, s, legacy, tc, structs.IntentionMatchDestination)
|
|
|
|
})
|
2018-03-02 20:56:39 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run(tc.Name+" (source)", func(t *testing.T) {
|
2020-10-06 18:24:05 +00:00
|
|
|
testBothIntentionFormats(t, func(t *testing.T, s *Store, legacy bool) {
|
|
|
|
testRunner(t, s, legacy, tc, structs.IntentionMatchSource)
|
|
|
|
})
|
2018-03-02 20:56:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 17:31:21 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Equivalent to TestStore_IntentionMatch_table but for IntentionMatchOne which
|
|
|
|
// matches a single service
|
2020-08-11 23:20:41 +00:00
|
|
|
func TestStore_IntentionMatchOne_table(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
Name string
|
2020-10-06 18:24:05 +00:00
|
|
|
Insert [][]string // List of intentions to insert
|
|
|
|
Query []string // List of intentions to match
|
|
|
|
Expected [][][]string // List of matches, where each match is a list of intentions
|
2020-08-11 23:20:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cases := []testCase{
|
|
|
|
{
|
2020-10-06 18:24:05 +00:00
|
|
|
"stress test the intention-source index on config entries",
|
2020-08-11 23:20:41 +00:00
|
|
|
[][]string{
|
|
|
|
{"foo", "bar"},
|
2020-10-06 18:24:05 +00:00
|
|
|
{"foo", "baz"},
|
|
|
|
{"foo", "zab"},
|
|
|
|
{"oof", "bar"},
|
|
|
|
{"oof", "baz"},
|
|
|
|
{"oof", "zab"},
|
2020-08-11 23:20:41 +00:00
|
|
|
},
|
|
|
|
[]string{
|
2020-10-06 18:24:05 +00:00
|
|
|
"foo",
|
|
|
|
"oof",
|
2020-08-11 23:20:41 +00:00
|
|
|
},
|
2020-10-06 18:24:05 +00:00
|
|
|
[][][]string{
|
|
|
|
{
|
|
|
|
{"foo", "bar"},
|
|
|
|
{"foo", "baz"},
|
|
|
|
{"foo", "zab"},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{"oof", "bar"},
|
|
|
|
{"oof", "baz"},
|
|
|
|
{"oof", "zab"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"single exact name",
|
2020-08-11 23:20:41 +00:00
|
|
|
[][]string{
|
2020-10-06 18:24:05 +00:00
|
|
|
{"bar", "example"},
|
|
|
|
{"baz", "example"}, // shouldn't match
|
|
|
|
{"*", "example"},
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"bar",
|
|
|
|
},
|
|
|
|
[][][]string{
|
|
|
|
{
|
|
|
|
{"bar", "example"},
|
|
|
|
{"*", "example"},
|
|
|
|
},
|
2020-08-11 23:20:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2020-10-06 18:24:05 +00:00
|
|
|
"single exact name with duplicate destinations",
|
2020-08-11 23:20:41 +00:00
|
|
|
[][]string{
|
2020-10-06 18:24:05 +00:00
|
|
|
// 2-tuple specifies src and destination to test duplicate destinations
|
2020-08-11 23:20:41 +00:00
|
|
|
// with different sources. We flip them around to test in both
|
|
|
|
// directions. The first pair are the ones searched on in both cases so
|
|
|
|
// the duplicates need to be there.
|
2020-10-06 18:24:05 +00:00
|
|
|
{"bar", "*"},
|
|
|
|
{"*", "*"},
|
2020-08-11 23:20:41 +00:00
|
|
|
},
|
|
|
|
[]string{
|
2020-10-06 18:24:05 +00:00
|
|
|
"bar",
|
2020-08-11 23:20:41 +00:00
|
|
|
},
|
2020-10-06 18:24:05 +00:00
|
|
|
[][][]string{
|
|
|
|
{
|
|
|
|
{"bar", "*"},
|
|
|
|
{"*", "*"},
|
|
|
|
},
|
2020-08-11 23:20:41 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
testRunner := func(t *testing.T, s *Store, legacy bool, tc testCase, typ structs.IntentionMatchType) {
|
|
|
|
lastIndex := uint64(0)
|
|
|
|
if legacy {
|
|
|
|
lastIndex = 1 // minor state machine implementation difference
|
|
|
|
}
|
|
|
|
|
2020-08-11 23:20:41 +00:00
|
|
|
// Insert the set
|
2020-10-06 18:24:05 +00:00
|
|
|
var ixns []*structs.Intention
|
2020-08-11 23:20:41 +00:00
|
|
|
for _, v := range tc.Insert {
|
2020-10-06 18:24:05 +00:00
|
|
|
if len(v) != 2 {
|
|
|
|
panic("invalid input")
|
|
|
|
}
|
|
|
|
ixn := &structs.Intention{
|
|
|
|
ID: testUUID(),
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
}
|
2020-08-11 23:20:41 +00:00
|
|
|
switch typ {
|
|
|
|
case structs.IntentionMatchSource:
|
2020-10-06 18:24:05 +00:00
|
|
|
ixn.SourceNS = "default"
|
|
|
|
ixn.SourceName = v[0]
|
|
|
|
ixn.DestinationNS = "default"
|
|
|
|
ixn.DestinationName = v[1]
|
|
|
|
case structs.IntentionMatchDestination:
|
|
|
|
ixn.DestinationNS = "default"
|
|
|
|
ixn.DestinationName = v[0]
|
|
|
|
ixn.SourceNS = "default"
|
2020-08-11 23:20:41 +00:00
|
|
|
ixn.SourceName = v[1]
|
2020-10-06 18:24:05 +00:00
|
|
|
default:
|
|
|
|
panic("unexpected")
|
2020-08-11 23:20:41 +00:00
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
ixns = append(ixns, ixn)
|
|
|
|
}
|
2020-08-11 23:20:41 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
if legacy {
|
|
|
|
for _, ixn := range ixns {
|
|
|
|
lastIndex++
|
|
|
|
require.NoError(t, s.LegacyIntentionSet(lastIndex, ixn))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
entries := structs.MigrateIntentions(ixns)
|
|
|
|
for _, conf := range entries {
|
|
|
|
require.NoError(t, conf.LegacyNormalize())
|
|
|
|
require.NoError(t, conf.LegacyValidate())
|
|
|
|
lastIndex++
|
2021-02-03 23:10:38 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(lastIndex, conf))
|
2020-10-06 18:24:05 +00:00
|
|
|
}
|
2020-08-11 23:20:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
if len(tc.Expected) != len(tc.Query) {
|
|
|
|
panic("invalid input")
|
2020-08-11 23:20:41 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
for i, query := range tc.Query {
|
|
|
|
expected := tc.Expected[i]
|
|
|
|
for _, exp := range expected {
|
|
|
|
if len(exp) != 2 {
|
|
|
|
panic("invalid input")
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 23:20:41 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
t.Run("query: "+query, func(t *testing.T) {
|
|
|
|
// Build the arguments and match
|
|
|
|
entry := structs.IntentionMatchEntry{
|
|
|
|
Namespace: "default",
|
|
|
|
Name: query,
|
2020-08-11 23:20:41 +00:00
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
_, matches, err := s.IntentionMatchOne(nil, entry, typ)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Verify matches
|
|
|
|
var actual [][]string
|
|
|
|
for _, ixn := range matches {
|
|
|
|
switch typ {
|
|
|
|
case structs.IntentionMatchDestination:
|
|
|
|
actual = append(actual, []string{
|
|
|
|
ixn.DestinationName,
|
|
|
|
ixn.SourceName,
|
|
|
|
})
|
|
|
|
case structs.IntentionMatchSource:
|
|
|
|
actual = append(actual, []string{
|
|
|
|
ixn.SourceName,
|
|
|
|
ixn.DestinationName,
|
|
|
|
})
|
|
|
|
}
|
2020-08-11 23:20:41 +00:00
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
})
|
2020-08-11 23:20:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Name+" (destination)", func(t *testing.T) {
|
2020-10-06 18:24:05 +00:00
|
|
|
testBothIntentionFormats(t, func(t *testing.T, s *Store, legacy bool) {
|
|
|
|
testRunner(t, s, legacy, tc, structs.IntentionMatchDestination)
|
|
|
|
})
|
2020-08-11 23:20:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run(tc.Name+" (source)", func(t *testing.T) {
|
2020-10-06 18:24:05 +00:00
|
|
|
testBothIntentionFormats(t, func(t *testing.T, s *Store, legacy bool) {
|
|
|
|
testRunner(t, s, legacy, tc, structs.IntentionMatchSource)
|
|
|
|
})
|
2020-08-11 23:20:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-29 20:28:31 +00:00
|
|
|
func TestStore_IntentionMatch_WatchesDuringUpgrade(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
args := structs.IntentionQueryMatch{
|
|
|
|
Type: structs.IntentionMatchDestination,
|
|
|
|
Entries: []structs.IntentionMatchEntry{
|
|
|
|
{Namespace: "default", Name: "api"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start with an empty, un-upgraded database and do a watch.
|
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
_, matches, err := s.IntentionMatch(ws, &args)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, matches, 1) // one request gets one response
|
|
|
|
require.Len(t, matches[0], 0) // but no intentions
|
|
|
|
|
|
|
|
disableLegacyIntentions(s)
|
|
|
|
conf := &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{Name: "web", Action: structs.IntentionActionAllow},
|
|
|
|
},
|
|
|
|
}
|
2021-02-03 23:10:38 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(1, conf))
|
2020-10-29 20:28:31 +00:00
|
|
|
|
|
|
|
require.True(t, watchFired(ws))
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestStore_LegacyIntention_Snapshot_Restore(t *testing.T) {
|
|
|
|
// note: irrelevant test for config entries variant
|
2018-03-06 17:31:21 +00:00
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
// Create some intentions.
|
|
|
|
ixns := structs.Intentions{
|
|
|
|
&structs.Intention{
|
|
|
|
DestinationName: "foo",
|
|
|
|
},
|
|
|
|
&structs.Intention{
|
|
|
|
DestinationName: "bar",
|
|
|
|
},
|
|
|
|
&structs.Intention{
|
|
|
|
DestinationName: "baz",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Force the sort order of the UUIDs before we create them so the
|
|
|
|
// order is deterministic.
|
|
|
|
id := testUUID()
|
|
|
|
ixns[0].ID = "a" + id[1:]
|
|
|
|
ixns[1].ID = "b" + id[1:]
|
|
|
|
ixns[2].ID = "c" + id[1:]
|
|
|
|
|
|
|
|
// Now create
|
|
|
|
for i, ixn := range ixns {
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, s.LegacyIntentionSet(uint64(4+i), ixn))
|
2018-03-06 17:31:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Snapshot the queries.
|
|
|
|
snap := s.Snapshot()
|
|
|
|
defer snap.Close()
|
|
|
|
|
|
|
|
// Alter the real state store.
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, s.LegacyIntentionDelete(7, ixns[0].ID))
|
2018-03-06 17:31:21 +00:00
|
|
|
|
|
|
|
// Verify the snapshot.
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Equal(t, snap.LastIndex(), uint64(6))
|
2018-06-08 12:58:46 +00:00
|
|
|
|
|
|
|
// Expect them sorted in insertion order
|
2018-03-06 17:31:21 +00:00
|
|
|
expected := structs.Intentions{
|
|
|
|
&structs.Intention{
|
|
|
|
ID: ixns[0].ID,
|
|
|
|
DestinationName: "foo",
|
|
|
|
Meta: map[string]string{},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 4,
|
|
|
|
ModifyIndex: 4,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.Intention{
|
|
|
|
ID: ixns[1].ID,
|
|
|
|
DestinationName: "bar",
|
|
|
|
Meta: map[string]string{},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 5,
|
|
|
|
ModifyIndex: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.Intention{
|
|
|
|
ID: ixns[2].ID,
|
|
|
|
DestinationName: "baz",
|
|
|
|
Meta: map[string]string{},
|
|
|
|
RaftIndex: structs.RaftIndex{
|
|
|
|
CreateIndex: 6,
|
|
|
|
ModifyIndex: 6,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-06-08 12:58:46 +00:00
|
|
|
for i := range expected {
|
2020-10-06 18:24:05 +00:00
|
|
|
//nolint:staticcheck
|
2018-06-08 12:58:46 +00:00
|
|
|
expected[i].UpdatePrecedence() // to match what is returned...
|
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
dump, err := snap.LegacyIntentions()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, expected, dump)
|
2018-03-06 17:31:21 +00:00
|
|
|
|
|
|
|
// Restore the values into a new state store.
|
|
|
|
func() {
|
|
|
|
s := testStateStore(t)
|
|
|
|
restore := s.Restore()
|
|
|
|
for _, ixn := range dump {
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, restore.LegacyIntention(ixn))
|
2018-03-06 17:31:21 +00:00
|
|
|
}
|
|
|
|
restore.Commit()
|
|
|
|
|
2018-06-08 12:58:46 +00:00
|
|
|
// Read the restored values back out and verify that they match. Note that
|
|
|
|
// Intentions are returned precedence sorted unlike the snapshot so we need
|
|
|
|
// to rearrange the expected slice some.
|
|
|
|
expected[0], expected[1], expected[2] = expected[1], expected[2], expected[0]
|
2021-07-22 18:20:45 +00:00
|
|
|
entMeta := structs.WildcardEnterpriseMetaInDefaultPartition()
|
2020-10-06 18:24:05 +00:00
|
|
|
idx, actual, fromConfig, err := s.Intentions(nil, entMeta)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, idx, uint64(6))
|
|
|
|
require.False(t, fromConfig)
|
|
|
|
require.Equal(t, expected, actual)
|
2018-03-06 17:31:21 +00:00
|
|
|
}()
|
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
|
2020-10-08 00:35:34 +00:00
|
|
|
// Note: This test does not have an equivalent with legacy intentions as an input.
|
|
|
|
// That's because the config vs legacy split is handled by store.IntentionMatch
|
|
|
|
// which has its own tests
|
|
|
|
func TestStore_IntentionDecision(t *testing.T) {
|
|
|
|
// web to redis allowed and with permissions
|
|
|
|
// api to redis denied and without perms (so redis has multiple matches as destination)
|
|
|
|
// api to web without permissions and with meta
|
|
|
|
entries := []structs.ConfigEntry{
|
|
|
|
&structs.ProxyConfigEntry{
|
|
|
|
Kind: structs.ProxyDefaults,
|
|
|
|
Name: structs.ProxyConfigGlobal,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"protocol": "http",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "redis",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
Permissions: []*structs.IntentionPermission{
|
|
|
|
{
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
HTTP: &structs.IntentionHTTPPermission{
|
|
|
|
Methods: []string{"GET"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "api",
|
|
|
|
Action: structs.IntentionActionDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "web",
|
|
|
|
Meta: map[string]string{structs.MetaExternalSource: "nomad"},
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "api",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-10-23 16:45:41 +00:00
|
|
|
&structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "mysql",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "*",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-10-08 00:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s := testConfigStateStore(t)
|
|
|
|
for _, entry := range entries {
|
2021-02-03 23:10:38 +00:00
|
|
|
require.NoError(t, s.EnsureConfigEntry(1, entry))
|
2020-10-08 00:35:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tt := []struct {
|
2021-03-13 04:59:47 +00:00
|
|
|
name string
|
|
|
|
src string
|
|
|
|
dst string
|
|
|
|
matchType structs.IntentionMatchType
|
|
|
|
defaultDecision acl.EnforcementDecision
|
|
|
|
allowPermissions bool
|
|
|
|
expect structs.IntentionDecisionSummary
|
2020-10-08 00:35:34 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no matching intention and default deny",
|
|
|
|
src: "does-not-exist",
|
|
|
|
dst: "ditto",
|
2021-03-13 04:59:47 +00:00
|
|
|
matchType: structs.IntentionMatchDestination,
|
2020-10-08 00:35:34 +00:00
|
|
|
defaultDecision: acl.Deny,
|
2021-04-13 01:32:09 +00:00
|
|
|
expect: structs.IntentionDecisionSummary{
|
|
|
|
Allowed: false,
|
|
|
|
DefaultAllow: false,
|
|
|
|
},
|
2020-10-08 00:35:34 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no matching intention and default allow",
|
|
|
|
src: "does-not-exist",
|
|
|
|
dst: "ditto",
|
2021-03-13 04:59:47 +00:00
|
|
|
matchType: structs.IntentionMatchDestination,
|
2020-10-08 00:35:34 +00:00
|
|
|
defaultDecision: acl.Allow,
|
2021-04-13 01:32:09 +00:00
|
|
|
expect: structs.IntentionDecisionSummary{
|
|
|
|
Allowed: true,
|
|
|
|
DefaultAllow: true,
|
|
|
|
},
|
2020-10-08 00:35:34 +00:00
|
|
|
},
|
|
|
|
{
|
2021-03-13 04:59:47 +00:00
|
|
|
name: "denied with permissions",
|
|
|
|
src: "web",
|
|
|
|
dst: "redis",
|
|
|
|
matchType: structs.IntentionMatchDestination,
|
2020-10-08 00:35:34 +00:00
|
|
|
expect: structs.IntentionDecisionSummary{
|
|
|
|
Allowed: false,
|
|
|
|
HasPermissions: true,
|
2020-10-23 16:45:41 +00:00
|
|
|
HasExact: true,
|
2020-10-08 00:35:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-13 04:59:47 +00:00
|
|
|
name: "allowed with permissions",
|
|
|
|
src: "web",
|
|
|
|
dst: "redis",
|
|
|
|
allowPermissions: true,
|
|
|
|
matchType: structs.IntentionMatchDestination,
|
|
|
|
expect: structs.IntentionDecisionSummary{
|
|
|
|
Allowed: true,
|
|
|
|
HasPermissions: true,
|
|
|
|
HasExact: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "denied without permissions",
|
|
|
|
src: "api",
|
|
|
|
dst: "redis",
|
|
|
|
matchType: structs.IntentionMatchDestination,
|
2020-10-08 00:35:34 +00:00
|
|
|
expect: structs.IntentionDecisionSummary{
|
|
|
|
Allowed: false,
|
|
|
|
HasPermissions: false,
|
2020-10-23 16:45:41 +00:00
|
|
|
HasExact: true,
|
2020-10-08 00:35:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-13 04:59:47 +00:00
|
|
|
name: "allowed from external source",
|
|
|
|
src: "api",
|
|
|
|
dst: "web",
|
|
|
|
matchType: structs.IntentionMatchDestination,
|
2020-10-08 00:35:34 +00:00
|
|
|
expect: structs.IntentionDecisionSummary{
|
|
|
|
Allowed: true,
|
|
|
|
HasPermissions: false,
|
|
|
|
ExternalSource: "nomad",
|
2020-10-23 16:45:41 +00:00
|
|
|
HasExact: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2021-03-13 04:59:47 +00:00
|
|
|
name: "allowed by source wildcard not exact",
|
|
|
|
src: "anything",
|
|
|
|
dst: "mysql",
|
|
|
|
matchType: structs.IntentionMatchDestination,
|
|
|
|
expect: structs.IntentionDecisionSummary{
|
|
|
|
Allowed: true,
|
|
|
|
HasPermissions: false,
|
|
|
|
HasExact: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "allowed by matching on source",
|
|
|
|
src: "web",
|
|
|
|
dst: "api",
|
|
|
|
matchType: structs.IntentionMatchSource,
|
2020-10-23 16:45:41 +00:00
|
|
|
expect: structs.IntentionDecisionSummary{
|
|
|
|
Allowed: true,
|
|
|
|
HasPermissions: false,
|
|
|
|
HasExact: false,
|
2020-10-08 00:35:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range tt {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-03-13 04:59:47 +00:00
|
|
|
entry := structs.IntentionMatchEntry{
|
2020-10-08 00:35:34 +00:00
|
|
|
Namespace: structs.IntentionDefaultNamespace,
|
2021-03-13 04:59:47 +00:00
|
|
|
Name: tc.src,
|
|
|
|
}
|
|
|
|
_, intentions, err := s.IntentionMatchOne(nil, entry, structs.IntentionMatchSource)
|
|
|
|
if err != nil {
|
|
|
|
require.NoError(t, err)
|
2020-10-08 00:35:34 +00:00
|
|
|
}
|
2021-03-13 04:59:47 +00:00
|
|
|
decision, err := s.IntentionDecision(tc.dst, structs.IntentionDefaultNamespace, intentions, tc.matchType, tc.defaultDecision, tc.allowPermissions)
|
2020-10-08 00:35:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.expect, decision)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func disableLegacyIntentions(s *Store) error {
|
|
|
|
return s.SystemMetadataSet(1, &structs.SystemMetadataEntry{
|
|
|
|
Key: structs.SystemMetadataIntentionFormatKey,
|
|
|
|
Value: structs.SystemMetadataIntentionFormatConfigValue,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testConfigStateStore(t *testing.T) *Store {
|
|
|
|
s := testStateStore(t)
|
|
|
|
disableLegacyIntentions(s)
|
|
|
|
return s
|
|
|
|
}
|
2021-03-14 04:44:01 +00:00
|
|
|
|
|
|
|
func TestStore_IntentionTopology(t *testing.T) {
|
|
|
|
node := structs.Node{
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}
|
|
|
|
services := []structs.NodeService{
|
2021-03-17 19:40:04 +00:00
|
|
|
{
|
|
|
|
ID: structs.ConsulServiceID,
|
|
|
|
Service: structs.ConsulServiceName,
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-17 19:40:04 +00:00
|
|
|
},
|
2021-03-14 04:44:01 +00:00
|
|
|
{
|
|
|
|
ID: "api-1",
|
|
|
|
Service: "api",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "mysql-1",
|
|
|
|
Service: "mysql",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
ID: "web-1",
|
|
|
|
Service: "web",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Kind: structs.ServiceKindConnectProxy,
|
|
|
|
ID: "web-proxy-1",
|
|
|
|
Service: "web-proxy",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Kind: structs.ServiceKindTerminatingGateway,
|
|
|
|
ID: "terminating-gateway-1",
|
|
|
|
Service: "terminating-gateway",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Kind: structs.ServiceKindIngressGateway,
|
|
|
|
ID: "ingress-gateway-1",
|
|
|
|
Service: "ingress-gateway",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Kind: structs.ServiceKindMeshGateway,
|
|
|
|
ID: "mesh-gateway-1",
|
|
|
|
Service: "mesh-gateway",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
type expect struct {
|
|
|
|
idx uint64
|
|
|
|
services structs.ServiceList
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
defaultDecision acl.EnforcementDecision
|
|
|
|
intentions []structs.ServiceIntentionsConfigEntry
|
|
|
|
target structs.ServiceName
|
|
|
|
downstreams bool
|
|
|
|
expect expect
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "(upstream) acl allow all but intentions deny one",
|
|
|
|
defaultDecision: acl.Allow,
|
|
|
|
intentions: []structs.ServiceIntentionsConfigEntry{
|
|
|
|
{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
Action: structs.IntentionActionDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
target: structs.NewServiceName("web", nil),
|
|
|
|
downstreams: false,
|
|
|
|
expect: expect{
|
2021-03-17 19:40:04 +00:00
|
|
|
idx: 10,
|
2021-03-14 04:44:01 +00:00
|
|
|
services: structs.ServiceList{
|
|
|
|
{
|
|
|
|
Name: "mysql",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "(upstream) acl deny all intentions allow one",
|
|
|
|
defaultDecision: acl.Deny,
|
|
|
|
intentions: []structs.ServiceIntentionsConfigEntry{
|
|
|
|
{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
target: structs.NewServiceName("web", nil),
|
|
|
|
downstreams: false,
|
|
|
|
expect: expect{
|
2021-03-17 19:40:04 +00:00
|
|
|
idx: 10,
|
2021-03-14 04:44:01 +00:00
|
|
|
services: structs.ServiceList{
|
|
|
|
{
|
|
|
|
Name: "api",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "(downstream) acl allow all but intentions deny one",
|
|
|
|
defaultDecision: acl.Allow,
|
|
|
|
intentions: []structs.ServiceIntentionsConfigEntry{
|
|
|
|
{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
Action: structs.IntentionActionDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
target: structs.NewServiceName("api", nil),
|
|
|
|
downstreams: true,
|
|
|
|
expect: expect{
|
2021-03-17 19:40:04 +00:00
|
|
|
idx: 10,
|
2021-03-14 04:44:01 +00:00
|
|
|
services: structs.ServiceList{
|
|
|
|
{
|
|
|
|
Name: "ingress-gateway",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mysql",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "(downstream) acl deny all intentions allow one",
|
|
|
|
defaultDecision: acl.Deny,
|
|
|
|
intentions: []structs.ServiceIntentionsConfigEntry{
|
|
|
|
{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
target: structs.NewServiceName("api", nil),
|
|
|
|
downstreams: true,
|
|
|
|
expect: expect{
|
2021-03-17 19:40:04 +00:00
|
|
|
idx: 10,
|
2021-03-14 04:44:01 +00:00
|
|
|
services: structs.ServiceList{
|
|
|
|
{
|
|
|
|
Name: "web",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "acl deny but intention allow all overrides it",
|
|
|
|
defaultDecision: acl.Deny,
|
|
|
|
intentions: []structs.ServiceIntentionsConfigEntry{
|
|
|
|
{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "*",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "*",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
target: structs.NewServiceName("web", nil),
|
|
|
|
downstreams: false,
|
|
|
|
expect: expect{
|
2021-03-17 19:40:04 +00:00
|
|
|
idx: 10,
|
2021-03-14 04:44:01 +00:00
|
|
|
services: structs.ServiceList{
|
|
|
|
{
|
|
|
|
Name: "api",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mysql",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "acl allow but intention deny all overrides it",
|
|
|
|
defaultDecision: acl.Allow,
|
|
|
|
intentions: []structs.ServiceIntentionsConfigEntry{
|
|
|
|
{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "*",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "*",
|
|
|
|
Action: structs.IntentionActionDeny,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
target: structs.NewServiceName("web", nil),
|
|
|
|
downstreams: false,
|
|
|
|
expect: expect{
|
2021-03-17 19:40:04 +00:00
|
|
|
idx: 10,
|
2021-03-14 04:44:01 +00:00
|
|
|
services: structs.ServiceList{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "acl deny but intention allow all overrides it",
|
|
|
|
defaultDecision: acl.Deny,
|
|
|
|
intentions: []structs.ServiceIntentionsConfigEntry{
|
|
|
|
{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "*",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "*",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
target: structs.NewServiceName("web", nil),
|
|
|
|
downstreams: false,
|
|
|
|
expect: expect{
|
2021-03-17 19:40:04 +00:00
|
|
|
idx: 10,
|
2021-03-14 04:44:01 +00:00
|
|
|
services: structs.ServiceList{
|
|
|
|
{
|
|
|
|
Name: "api",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "mysql",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
s := testConfigStateStore(t)
|
|
|
|
|
|
|
|
var idx uint64 = 1
|
|
|
|
require.NoError(t, s.EnsureNode(idx, &node))
|
|
|
|
idx++
|
|
|
|
|
|
|
|
for _, svc := range services {
|
|
|
|
require.NoError(t, s.EnsureService(idx, "foo", &svc))
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
for _, ixn := range tt.intentions {
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(idx, &ixn))
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, got, err := s.IntentionTopology(nil, tt.target, tt.downstreams, tt.defaultDecision)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tt.expect.idx, idx)
|
|
|
|
|
|
|
|
// ServiceList is from a map, so it is not deterministically sorted
|
|
|
|
sort.Slice(got, func(i, j int) bool {
|
|
|
|
return got[i].String() < got[j].String()
|
|
|
|
})
|
|
|
|
require.Equal(t, tt.expect.services, got)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStore_IntentionTopology_Watches(t *testing.T) {
|
|
|
|
s := testConfigStateStore(t)
|
|
|
|
|
|
|
|
var i uint64 = 1
|
|
|
|
require.NoError(t, s.EnsureNode(i, &structs.Node{
|
|
|
|
Node: "foo",
|
|
|
|
Address: "127.0.0.1",
|
|
|
|
}))
|
|
|
|
i++
|
|
|
|
|
2021-07-22 18:20:45 +00:00
|
|
|
target := structs.NewServiceName("web", structs.DefaultEnterpriseMetaInDefaultPartition())
|
2021-03-14 04:44:01 +00:00
|
|
|
|
|
|
|
ws := memdb.NewWatchSet()
|
|
|
|
index, got, err := s.IntentionTopology(ws, target, false, acl.Deny)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(0), index)
|
|
|
|
require.Empty(t, got)
|
|
|
|
|
|
|
|
// Watch should fire after adding a relevant config entry
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(i, &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "api",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
i++
|
|
|
|
|
|
|
|
require.True(t, watchFired(ws))
|
|
|
|
|
|
|
|
// Reset the WatchSet
|
|
|
|
ws = memdb.NewWatchSet()
|
|
|
|
index, got, err = s.IntentionTopology(ws, target, false, acl.Deny)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(2), index)
|
|
|
|
require.Empty(t, got)
|
|
|
|
|
|
|
|
// Watch should not fire after unrelated intention changes
|
|
|
|
require.NoError(t, s.EnsureConfigEntry(i, &structs.ServiceIntentionsConfigEntry{
|
|
|
|
Kind: structs.ServiceIntentions,
|
|
|
|
Name: "another service",
|
|
|
|
Sources: []*structs.SourceIntention{
|
|
|
|
{
|
|
|
|
Name: "any other service",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
i++
|
|
|
|
|
|
|
|
// TODO(freddy) Why is this firing?
|
2021-03-17 23:25:29 +00:00
|
|
|
// require.False(t, watchFired(ws))
|
2021-03-14 04:44:01 +00:00
|
|
|
|
|
|
|
// Result should not have changed
|
|
|
|
index, got, err = s.IntentionTopology(ws, target, false, acl.Deny)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(3), index)
|
|
|
|
require.Empty(t, got)
|
|
|
|
|
|
|
|
// Watch should fire after service list changes
|
|
|
|
require.NoError(t, s.EnsureService(i, "foo", &structs.NodeService{
|
|
|
|
ID: "api-1",
|
|
|
|
Service: "api",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
}))
|
|
|
|
|
|
|
|
require.True(t, watchFired(ws))
|
|
|
|
|
|
|
|
// Reset the WatchSet
|
|
|
|
index, got, err = s.IntentionTopology(nil, target, false, acl.Deny)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(4), index)
|
|
|
|
|
|
|
|
expect := structs.ServiceList{
|
|
|
|
{
|
|
|
|
Name: "api",
|
2021-07-22 18:20:45 +00:00
|
|
|
EnterpriseMeta: *structs.DefaultEnterpriseMetaInDefaultPartition(),
|
2021-03-14 04:44:01 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
require.Equal(t, expect, got)
|
|
|
|
}
|