2018-03-28 04:33:05 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2018-05-16 15:53:33 +00:00
|
|
|
func TestAPI_ConnectIntentionCreateListGetUpdateDelete(t *testing.T) {
|
2018-03-28 04:33:05 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2020-10-07 18:32:53 +00:00
|
|
|
s.WaitForServiceIntentions(t)
|
|
|
|
|
2018-03-28 04:33:05 +00:00
|
|
|
connect := c.Connect()
|
|
|
|
|
|
|
|
// Create
|
|
|
|
ixn := testIntention()
|
|
|
|
id, _, err := connect.IntentionCreate(ixn, nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotEmpty(t, id)
|
2018-03-28 04:33:05 +00:00
|
|
|
|
|
|
|
// List it
|
|
|
|
list, _, err := connect.Intentions(nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, list, 1)
|
2018-03-28 04:33:05 +00:00
|
|
|
|
|
|
|
actual := list[0]
|
|
|
|
ixn.ID = id
|
|
|
|
ixn.CreatedAt = actual.CreatedAt
|
|
|
|
ixn.UpdatedAt = actual.UpdatedAt
|
|
|
|
ixn.CreateIndex = actual.CreateIndex
|
|
|
|
ixn.ModifyIndex = actual.ModifyIndex
|
2019-06-18 00:52:01 +00:00
|
|
|
ixn.Hash = actual.Hash
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Equal(t, ixn, actual)
|
2018-03-28 17:14:32 +00:00
|
|
|
|
|
|
|
// Get it
|
|
|
|
actual, _, err = connect.IntentionGet(id, nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, ixn, actual)
|
2018-05-12 05:19:21 +00:00
|
|
|
|
2018-05-16 15:53:33 +00:00
|
|
|
// Update it
|
2020-06-26 21:59:15 +00:00
|
|
|
ixn.SourceName = ixn.SourceName + "-different"
|
2018-05-16 15:53:33 +00:00
|
|
|
_, err = connect.IntentionUpdate(ixn, nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, err)
|
2018-05-16 15:53:33 +00:00
|
|
|
|
|
|
|
// Get it
|
|
|
|
actual, _, err = connect.IntentionGet(id, nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, err)
|
2018-05-16 15:53:33 +00:00
|
|
|
ixn.UpdatedAt = actual.UpdatedAt
|
|
|
|
ixn.ModifyIndex = actual.ModifyIndex
|
2019-06-18 00:52:01 +00:00
|
|
|
ixn.Hash = actual.Hash
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Equal(t, ixn, actual)
|
2018-05-16 15:53:33 +00:00
|
|
|
|
2018-05-12 05:19:21 +00:00
|
|
|
// Delete it
|
|
|
|
_, err = connect.IntentionDelete(id, nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Nil(t, err)
|
2018-05-12 05:19:21 +00:00
|
|
|
|
|
|
|
// Get it (should be gone)
|
|
|
|
actual, _, err = connect.IntentionGet(id, nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Nil(t, actual)
|
2018-03-28 17:14:32 +00:00
|
|
|
}
|
|
|
|
|
2018-06-27 05:40:06 +00:00
|
|
|
func TestAPI_ConnectIntentionGet_invalidId(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2020-10-07 18:32:53 +00:00
|
|
|
s.WaitForServiceIntentions(t)
|
|
|
|
|
2018-06-27 05:40:06 +00:00
|
|
|
connect := c.Connect()
|
|
|
|
|
|
|
|
// Get it
|
|
|
|
actual, _, err := connect.IntentionGet("hello", nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Nil(t, actual)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "UUID") // verify it contains the message
|
2018-06-27 05:40:06 +00:00
|
|
|
}
|
|
|
|
|
2018-03-28 17:14:32 +00:00
|
|
|
func TestAPI_ConnectIntentionMatch(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2020-10-07 18:32:53 +00:00
|
|
|
s.WaitForServiceIntentions(t)
|
|
|
|
|
2018-03-28 17:14:32 +00:00
|
|
|
connect := c.Connect()
|
|
|
|
|
|
|
|
// Create
|
|
|
|
{
|
|
|
|
insert := [][]string{
|
2020-06-26 21:59:15 +00:00
|
|
|
{"default", "*"},
|
|
|
|
{"default", "bar"},
|
|
|
|
{"default", "baz"}, // shouldn't match
|
2018-03-28 17:14:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range insert {
|
|
|
|
ixn := testIntention()
|
|
|
|
ixn.DestinationNS = v[0]
|
|
|
|
ixn.DestinationName = v[1]
|
|
|
|
id, _, err := connect.IntentionCreate(ixn, nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotEmpty(t, id)
|
2018-03-28 17:14:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match it
|
|
|
|
result, _, err := connect.IntentionMatch(&IntentionMatch{
|
|
|
|
By: IntentionMatchDestination,
|
2020-06-26 21:59:15 +00:00
|
|
|
Names: []string{"bar"},
|
2018-03-28 17:14:32 +00:00
|
|
|
}, nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Len(t, result, 1)
|
2018-03-28 17:14:32 +00:00
|
|
|
|
|
|
|
var actual [][]string
|
2020-06-26 21:59:15 +00:00
|
|
|
expected := [][]string{
|
|
|
|
{"default", "bar"},
|
|
|
|
{"default", "*"},
|
|
|
|
}
|
|
|
|
for _, ixn := range result["bar"] {
|
2018-03-28 17:14:32 +00:00
|
|
|
actual = append(actual, []string{ixn.DestinationNS, ixn.DestinationName})
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Equal(t, expected, actual)
|
2018-03-28 04:33:05 +00:00
|
|
|
}
|
|
|
|
|
2018-05-12 00:19:54 +00:00
|
|
|
func TestAPI_ConnectIntentionCheck(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
c, s := makeClient(t)
|
|
|
|
defer s.Stop()
|
|
|
|
|
2020-10-07 18:32:53 +00:00
|
|
|
s.WaitForServiceIntentions(t)
|
|
|
|
|
2018-05-12 00:19:54 +00:00
|
|
|
connect := c.Connect()
|
|
|
|
|
|
|
|
// Create
|
|
|
|
{
|
|
|
|
insert := [][]string{
|
2020-06-26 21:59:15 +00:00
|
|
|
{"default", "*", "default", "bar", "deny"},
|
|
|
|
{"default", "foo", "default", "bar", "allow"},
|
2018-05-12 00:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range insert {
|
|
|
|
ixn := testIntention()
|
|
|
|
ixn.SourceNS = v[0]
|
|
|
|
ixn.SourceName = v[1]
|
|
|
|
ixn.DestinationNS = v[2]
|
|
|
|
ixn.DestinationName = v[3]
|
2020-06-26 21:59:15 +00:00
|
|
|
ixn.Action = IntentionAction(v[4])
|
2018-05-12 00:19:54 +00:00
|
|
|
id, _, err := connect.IntentionCreate(ixn, nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotEmpty(t, id)
|
2018-05-12 00:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 21:59:15 +00:00
|
|
|
// Match the deny rule
|
2018-05-12 00:19:54 +00:00
|
|
|
{
|
|
|
|
result, _, err := connect.IntentionCheck(&IntentionCheck{
|
2020-06-26 21:59:15 +00:00
|
|
|
Source: "default/qux",
|
|
|
|
Destination: "default/bar",
|
2018-05-12 00:19:54 +00:00
|
|
|
}, nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, result)
|
2018-05-12 00:19:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-26 21:59:15 +00:00
|
|
|
// Match the allow rule
|
2018-05-12 00:19:54 +00:00
|
|
|
{
|
|
|
|
result, _, err := connect.IntentionCheck(&IntentionCheck{
|
2020-06-26 21:59:15 +00:00
|
|
|
Source: "default/foo",
|
|
|
|
Destination: "default/bar",
|
2018-05-12 00:19:54 +00:00
|
|
|
}, nil)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, result)
|
2018-05-12 00:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-28 04:33:05 +00:00
|
|
|
func testIntention() *Intention {
|
|
|
|
return &Intention{
|
2020-06-26 21:59:15 +00:00
|
|
|
SourceNS: "default",
|
2018-03-28 04:33:05 +00:00
|
|
|
SourceName: "api",
|
2020-06-26 21:59:15 +00:00
|
|
|
DestinationNS: "default",
|
2018-03-28 04:33:05 +00:00
|
|
|
DestinationName: "db",
|
2018-06-13 19:01:05 +00:00
|
|
|
Precedence: 9,
|
2018-03-28 04:33:05 +00:00
|
|
|
Action: IntentionActionAllow,
|
|
|
|
SourceType: IntentionSourceConsul,
|
|
|
|
}
|
|
|
|
}
|