2018-02-28 19:36:54 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2018-02-28 23:54:48 +00:00
|
|
|
"fmt"
|
2018-02-28 19:36:54 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2021-09-08 15:59:30 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-02-28 19:36:54 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-10-06 18:24:05 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
|
|
|
"github.com/hashicorp/consul/testrpc"
|
2018-02-28 19:36:54 +00:00
|
|
|
)
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestIntentionList(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-02-28 19:36:54 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-02-28 19:36:54 +00:00
|
|
|
defer a.Shutdown()
|
2020-10-06 18:24:05 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-02-28 19:36:54 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
t.Run("empty", func(t *testing.T) {
|
|
|
|
// Make sure an empty list is non-nil.
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/connect/intentions", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionList(resp, req)
|
|
|
|
require.NoError(t, err)
|
2018-02-28 19:36:54 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
value := obj.(structs.Intentions)
|
|
|
|
require.NotNil(t, value)
|
|
|
|
require.Len(t, value, 0)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("values", func(t *testing.T) {
|
|
|
|
// Create some intentions, note we create the lowest precedence first to test
|
|
|
|
// sorting.
|
|
|
|
//
|
|
|
|
// Also create one non-legacy one using a different destination.
|
|
|
|
var ids []string
|
|
|
|
for _, v := range []string{"*", "foo", "bar", "zim"} {
|
|
|
|
req := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: structs.TestIntention(t),
|
|
|
|
}
|
|
|
|
req.Intention.SourceName = v
|
2018-02-28 19:36:54 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
if v == "zim" {
|
|
|
|
req.Op = structs.IntentionOpUpsert // non-legacy
|
|
|
|
req.Intention.DestinationName = "gir"
|
|
|
|
}
|
2018-02-28 19:36:54 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
var reply string
|
|
|
|
require.NoError(t, a.RPC("Intention.Apply", &req, &reply))
|
|
|
|
ids = append(ids, reply)
|
2018-02-28 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Request
|
|
|
|
req, err := http.NewRequest("GET", "/v1/connect/intentions", nil)
|
|
|
|
require.NoError(t, err)
|
2018-02-28 19:36:54 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionList(resp, req)
|
|
|
|
require.NoError(t, err)
|
2018-02-28 19:36:54 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
value := obj.(structs.Intentions)
|
|
|
|
require.Len(t, value, 4)
|
|
|
|
|
|
|
|
require.Equal(t, []string{"bar->db", "foo->db", "zim->gir", "*->db"},
|
|
|
|
[]string{
|
|
|
|
value[0].SourceName + "->" + value[0].DestinationName,
|
|
|
|
value[1].SourceName + "->" + value[1].DestinationName,
|
|
|
|
value[2].SourceName + "->" + value[2].DestinationName,
|
|
|
|
value[3].SourceName + "->" + value[3].DestinationName,
|
|
|
|
})
|
|
|
|
require.Equal(t, []string{ids[2], ids[1], "", ids[0]},
|
|
|
|
[]string{
|
|
|
|
value[0].ID,
|
|
|
|
value[1].ID,
|
|
|
|
value[2].ID,
|
|
|
|
value[3].ID,
|
|
|
|
})
|
|
|
|
})
|
2018-02-28 19:36:54 +00:00
|
|
|
}
|
2018-02-28 22:02:00 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestIntentionMatch(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:17:21 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-03-02 22:17:21 +00:00
|
|
|
defer a.Shutdown()
|
2020-10-06 18:24:05 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-03-02 22:17:21 +00:00
|
|
|
|
|
|
|
// Create some intentions
|
|
|
|
{
|
|
|
|
insert := [][]string{
|
2020-06-26 21:59:15 +00:00
|
|
|
{"default", "*", "default", "*"},
|
|
|
|
{"default", "*", "default", "bar"},
|
|
|
|
{"default", "*", "default", "baz"}, // shouldn't match
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range insert {
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
2018-03-03 18:12:05 +00:00
|
|
|
Intention: structs.TestIntention(t),
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
2018-04-05 11:53:42 +00:00
|
|
|
ixn.Intention.SourceNS = v[0]
|
|
|
|
ixn.Intention.SourceName = v[1]
|
|
|
|
ixn.Intention.DestinationNS = v[2]
|
|
|
|
ixn.Intention.DestinationName = v[3]
|
2018-03-02 22:17:21 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
if ixn.Intention.DestinationName == "baz" {
|
|
|
|
// make the "baz" destination be non-legacy
|
|
|
|
ixn.Op = structs.IntentionOpUpsert
|
|
|
|
}
|
|
|
|
|
2018-03-02 22:17:21 +00:00
|
|
|
// Create
|
|
|
|
var reply string
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, a.RPC("Intention.Apply", &ixn, &reply))
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
t.Run("no by", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/connect/intentions/match?name=foo/bar", nil)
|
|
|
|
require.NoError(t, err)
|
2018-03-02 22:17:21 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
|
|
|
testutil.RequireErrorContains(t, err, "by")
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
2018-03-02 22:17:21 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
t.Run("by invalid", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/connect/intentions/match?by=datacenter", nil)
|
|
|
|
require.NoError(t, err)
|
2018-03-02 22:17:21 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
|
|
|
testutil.RequireErrorContains(t, err, "'by' parameter")
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
2018-03-02 22:17:21 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
t.Run("no name", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/connect/intentions/match?by=source", nil)
|
|
|
|
require.NoError(t, err)
|
2018-03-02 22:17:21 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
|
|
|
testutil.RequireErrorContains(t, err, "'name' not set")
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
2018-03-02 22:17:21 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/connect/intentions/match?by=destination&name=bar", nil)
|
|
|
|
require.NoError(t, err)
|
2018-03-02 22:17:21 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
|
|
|
require.NoError(t, err)
|
2018-03-02 22:17:21 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
value := obj.(map[string]structs.Intentions)
|
|
|
|
require.Len(t, value, 1)
|
2018-03-02 22:17:21 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
var actual [][]string
|
|
|
|
expected := [][]string{
|
|
|
|
{"default", "*", "default", "bar"},
|
|
|
|
{"default", "*", "default", "*"},
|
|
|
|
}
|
|
|
|
for _, ixn := range value["bar"] {
|
|
|
|
actual = append(actual, []string{
|
|
|
|
ixn.SourceNS,
|
|
|
|
ixn.SourceName,
|
|
|
|
ixn.DestinationNS,
|
|
|
|
ixn.DestinationName,
|
|
|
|
})
|
|
|
|
}
|
2018-03-02 22:17:21 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
})
|
2020-10-08 19:51:53 +00:00
|
|
|
|
|
|
|
t.Run("success with cache", func(t *testing.T) {
|
|
|
|
// First request is a MISS, but it primes the cache for the second attempt
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/connect/intentions/match?by=destination&name=bar&cached", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// The GET request primes the cache so the POST is a hit.
|
|
|
|
if i == 0 {
|
|
|
|
// Should be a cache miss
|
|
|
|
require.Equal(t, "MISS", resp.Header().Get("X-Cache"))
|
|
|
|
} else {
|
|
|
|
// Should be a cache HIT now!
|
|
|
|
require.Equal(t, "HIT", resp.Header().Get("X-Cache"))
|
|
|
|
}
|
|
|
|
|
|
|
|
value := obj.(map[string]structs.Intentions)
|
|
|
|
require.Len(t, value, 1)
|
|
|
|
|
|
|
|
var actual [][]string
|
|
|
|
expected := [][]string{
|
|
|
|
{"default", "*", "default", "bar"},
|
|
|
|
{"default", "*", "default", "*"},
|
|
|
|
}
|
|
|
|
for _, ixn := range value["bar"] {
|
|
|
|
actual = append(actual, []string{
|
|
|
|
ixn.SourceNS,
|
|
|
|
ixn.SourceName,
|
|
|
|
ixn.DestinationNS,
|
|
|
|
ixn.DestinationName,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, expected, actual)
|
|
|
|
}
|
|
|
|
})
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestIntentionCheck(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-05-11 05:52:57 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-05-11 05:52:57 +00:00
|
|
|
defer a.Shutdown()
|
2020-10-06 18:24:05 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-05-11 05:52:57 +00:00
|
|
|
|
|
|
|
// Create some intentions
|
|
|
|
{
|
|
|
|
insert := [][]string{
|
2020-06-26 21:59:15 +00:00
|
|
|
{"default", "*", "default", "baz"},
|
|
|
|
{"default", "*", "default", "bar"},
|
2018-05-11 05:52:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range insert {
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: structs.TestIntention(t),
|
|
|
|
}
|
|
|
|
ixn.Intention.SourceNS = v[0]
|
|
|
|
ixn.Intention.SourceName = v[1]
|
|
|
|
ixn.Intention.DestinationNS = v[2]
|
|
|
|
ixn.Intention.DestinationName = v[3]
|
|
|
|
ixn.Intention.Action = structs.IntentionActionDeny
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
if ixn.Intention.DestinationName == "baz" {
|
|
|
|
// make the "baz" destination be non-legacy
|
|
|
|
ixn.Op = structs.IntentionOpUpsert
|
|
|
|
}
|
|
|
|
|
2018-05-11 05:52:57 +00:00
|
|
|
// Create
|
|
|
|
var reply string
|
2020-06-26 21:59:15 +00:00
|
|
|
require.NoError(t, a.RPC("Intention.Apply", &ixn, &reply))
|
2018-05-11 05:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
t.Run("no source", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/connect/intentions/test?destination=B", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionCheck(resp, req)
|
|
|
|
testutil.RequireErrorContains(t, err, "'source' not set")
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no destination", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/connect/intentions/test?source=B", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionCheck(resp, req)
|
|
|
|
testutil.RequireErrorContains(t, err, "'destination' not set")
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - matching intention", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/connect/intentions/test?source=bar&destination=baz", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2018-05-11 05:52:57 +00:00
|
|
|
resp := httptest.NewRecorder()
|
2018-05-11 16:19:22 +00:00
|
|
|
obj, err := a.srv.IntentionCheck(resp, req)
|
2020-06-26 21:59:15 +00:00
|
|
|
require.NoError(t, err)
|
2018-05-11 16:19:22 +00:00
|
|
|
value := obj.(*structs.IntentionQueryCheckResponse)
|
2020-06-26 21:59:15 +00:00
|
|
|
require.False(t, value.Allowed)
|
2020-10-06 18:24:05 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success - non-matching intention", func(t *testing.T) {
|
|
|
|
req, err := http.NewRequest("GET", "/v1/connect/intentions/test?source=bar&destination=qux", nil)
|
|
|
|
require.NoError(t, err)
|
2018-05-11 05:52:57 +00:00
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
2018-05-11 16:19:22 +00:00
|
|
|
obj, err := a.srv.IntentionCheck(resp, req)
|
2020-06-26 21:59:15 +00:00
|
|
|
require.NoError(t, err)
|
2018-05-11 16:19:22 +00:00
|
|
|
value := obj.(*structs.IntentionQueryCheckResponse)
|
2020-06-26 21:59:15 +00:00
|
|
|
require.True(t, value.Allowed)
|
2020-10-06 18:24:05 +00:00
|
|
|
})
|
2018-05-11 05:52:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestIntentionPutExact(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-05-11 05:52:57 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-05-11 05:52:57 +00:00
|
|
|
defer a.Shutdown()
|
2020-10-06 18:24:05 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-05-11 05:52:57 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
t.Run("no body", func(t *testing.T) {
|
|
|
|
// Create with no body
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/connect/intentions", nil)
|
|
|
|
require.NoError(t, err)
|
2018-05-11 05:52:57 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err = a.srv.IntentionExact(resp, req)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("source is required", func(t *testing.T) {
|
|
|
|
ixn := structs.TestIntention(t)
|
|
|
|
ixn.SourceName = "foo"
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/connect/intentions?source=&destination=db", jsonReader(ixn))
|
|
|
|
require.NoError(t, err)
|
2018-05-11 05:52:57 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err = a.srv.IntentionExact(resp, req)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("destination is required", func(t *testing.T) {
|
|
|
|
ixn := structs.TestIntention(t)
|
|
|
|
ixn.SourceName = "foo"
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/connect/intentions?source=foo&destination=", jsonReader(ixn))
|
|
|
|
require.NoError(t, err)
|
2018-02-28 22:02:00 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err = a.srv.IntentionExact(resp, req)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
ixn := structs.TestIntention(t)
|
|
|
|
ixn.SourceName = "foo"
|
|
|
|
req, err := http.NewRequest("PUT", "/v1/connect/intentions?source=foo&destination=db", jsonReader(ixn))
|
|
|
|
require.NoError(t, err)
|
2018-02-28 22:02:00 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionExact(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, obj.(bool))
|
2018-02-28 22:02:00 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
// Read the value
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Exact: ixn.ToExact(),
|
|
|
|
}
|
2018-02-28 22:02:00 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
var resp structs.IndexedIntentions
|
|
|
|
require.NoError(t, a.RPC("Intention.Get", req, &resp))
|
|
|
|
require.Len(t, resp.Intentions, 1)
|
|
|
|
actual := resp.Intentions[0]
|
|
|
|
require.Equal(t, "foo", actual.SourceName)
|
|
|
|
require.Empty(t, actual.ID) // new style
|
2018-02-28 22:02:00 +00:00
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
})
|
2018-02-28 22:02:00 +00:00
|
|
|
}
|
2018-02-28 23:54:48 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestIntentionCreate(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-06-05 18:09:45 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-06-05 18:09:45 +00:00
|
|
|
defer a.Shutdown()
|
2020-10-06 18:24:05 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-06-05 18:09:45 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
t.Run("no body", func(t *testing.T) {
|
|
|
|
// Create with no body
|
|
|
|
req, _ := http.NewRequest("POST", "/v1/connect/intentions", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.IntentionCreate(resp, req)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
// Make sure an empty list is non-nil.
|
|
|
|
args := structs.TestIntention(t)
|
|
|
|
args.SourceName = "foo"
|
|
|
|
req, _ := http.NewRequest("POST", "/v1/connect/intentions", jsonReader(args))
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionCreate(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
value := obj.(intentionCreateResponse)
|
|
|
|
require.NotEmpty(t, value.ID)
|
|
|
|
|
|
|
|
// Read the value
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: value.ID,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
|
|
|
require.NoError(t, a.RPC("Intention.Get", req, &resp))
|
|
|
|
require.Len(t, resp.Intentions, 1)
|
|
|
|
actual := resp.Intentions[0]
|
|
|
|
require.Equal(t, "foo", actual.SourceName)
|
|
|
|
}
|
|
|
|
})
|
2021-10-01 17:18:57 +00:00
|
|
|
|
|
|
|
t.Run("partition rejected", func(t *testing.T) {
|
|
|
|
{
|
|
|
|
args := structs.TestIntention(t)
|
|
|
|
args.SourcePartition = "part1"
|
|
|
|
req, _ := http.NewRequest("POST", "/v1/connect/intentions", jsonReader(args))
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.IntentionCreate(resp, req)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "Cannot specify a source partition")
|
|
|
|
}
|
|
|
|
{
|
|
|
|
args := structs.TestIntention(t)
|
|
|
|
args.DestinationPartition = "part2"
|
|
|
|
req, _ := http.NewRequest("POST", "/v1/connect/intentions", jsonReader(args))
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.IntentionCreate(resp, req)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "Cannot specify a destination partition")
|
|
|
|
}
|
|
|
|
})
|
2018-06-05 18:09:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestIntentionSpecificGet(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-02-28 23:54:48 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-02-28 23:54:48 +00:00
|
|
|
defer a.Shutdown()
|
2020-10-06 18:24:05 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-02-28 23:54:48 +00:00
|
|
|
|
2018-03-03 18:12:05 +00:00
|
|
|
ixn := structs.TestIntention(t)
|
2018-02-28 23:54:48 +00:00
|
|
|
|
|
|
|
// Create an intention directly
|
|
|
|
var reply string
|
|
|
|
{
|
|
|
|
req := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: ixn,
|
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, a.RPC("Intention.Apply", &req, &reply))
|
2018-02-28 23:54:48 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
t.Run("invalid id", func(t *testing.T) {
|
|
|
|
// Read intention with bad ID
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/connect/intentions/hello", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionSpecific(resp, req)
|
|
|
|
require.Nil(t, obj)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.IsType(t, BadRequestError{}, err)
|
|
|
|
require.Contains(t, err.Error(), "UUID")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
// Get the value
|
|
|
|
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/connect/intentions/%s", reply), nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionSpecific(resp, req)
|
|
|
|
require.NoError(t, err)
|
2018-06-27 05:40:06 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
value := obj.(*structs.Intention)
|
|
|
|
require.Equal(t, reply, value.ID)
|
2018-06-27 05:40:06 +00:00
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
ixn.ID = value.ID
|
|
|
|
ixn.Precedence = value.Precedence
|
|
|
|
ixn.RaftIndex = value.RaftIndex
|
|
|
|
ixn.Hash = value.Hash
|
|
|
|
ixn.CreatedAt, ixn.UpdatedAt = value.CreatedAt, value.UpdatedAt
|
|
|
|
require.Equal(t, ixn, value)
|
|
|
|
})
|
2018-06-27 05:40:06 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestIntentionSpecificUpdate(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-03-02 00:53:31 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-03-02 00:53:31 +00:00
|
|
|
defer a.Shutdown()
|
2020-10-06 18:24:05 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-03-02 00:53:31 +00:00
|
|
|
|
|
|
|
// The intention
|
2018-03-03 18:12:05 +00:00
|
|
|
ixn := structs.TestIntention(t)
|
2018-03-02 00:53:31 +00:00
|
|
|
|
|
|
|
// Create an intention directly
|
|
|
|
var reply string
|
|
|
|
{
|
|
|
|
req := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: ixn,
|
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, a.RPC("Intention.Apply", &req, &reply))
|
2018-03-02 00:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the intention
|
|
|
|
ixn.ID = "bogus"
|
|
|
|
ixn.SourceName = "bar"
|
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/connect/intentions/%s", reply), jsonReader(ixn))
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionSpecific(resp, req)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, err)
|
2018-06-13 10:54:27 +00:00
|
|
|
|
|
|
|
value := obj.(intentionCreateResponse)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Equal(t, reply, value.ID)
|
2018-03-02 00:53:31 +00:00
|
|
|
|
|
|
|
// Read the value
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: reply,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, a.RPC("Intention.Get", req, &resp))
|
|
|
|
require.Len(t, resp.Intentions, 1)
|
2018-03-02 00:53:31 +00:00
|
|
|
actual := resp.Intentions[0]
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Equal(t, "bar", actual.SourceName)
|
2018-03-02 00:53:31 +00:00
|
|
|
}
|
2021-10-01 17:18:57 +00:00
|
|
|
|
|
|
|
t.Run("partitions rejected", func(t *testing.T) {
|
|
|
|
{
|
|
|
|
ixn.DestinationPartition = "part1"
|
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/connect/intentions/%s", reply), jsonReader(ixn))
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.IntentionSpecific(resp, req)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "Cannot specify a destination partition")
|
|
|
|
}
|
|
|
|
{
|
|
|
|
ixn.DestinationPartition = "default"
|
|
|
|
ixn.SourcePartition = "part2"
|
|
|
|
req, _ := http.NewRequest("PUT", fmt.Sprintf("/v1/connect/intentions/%s", reply), jsonReader(ixn))
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err := a.srv.IntentionSpecific(resp, req)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "Cannot specify a source partition")
|
|
|
|
}
|
|
|
|
})
|
2018-03-02 00:53:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestIntentionDeleteExact(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2018-03-01 23:54:03 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-03-31 19:59:56 +00:00
|
|
|
a := NewTestAgent(t, "")
|
2018-03-01 23:54:03 +00:00
|
|
|
defer a.Shutdown()
|
2020-10-06 18:24:05 +00:00
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
|
|
|
|
|
|
|
ixn := structs.TestIntention(t)
|
|
|
|
ixn.SourceName = "foo"
|
|
|
|
|
2020-11-24 18:44:20 +00:00
|
|
|
t.Run("cannot delete non-existent intention", func(t *testing.T) {
|
|
|
|
// Delete the intention
|
|
|
|
req, err := http.NewRequest("DELETE", "/v1/connect/intentions/exact?source=foo&destination=db", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionExact(resp, req)
|
2021-01-04 17:27:00 +00:00
|
|
|
require.NoError(t, err) // by-name deletions are idempotent
|
|
|
|
require.Equal(t, true, obj)
|
2020-11-24 18:44:20 +00:00
|
|
|
})
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
exact := ixn.ToExact()
|
|
|
|
|
|
|
|
// Create an intention directly
|
|
|
|
{
|
|
|
|
req := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpUpsert,
|
|
|
|
Intention: ixn,
|
|
|
|
}
|
|
|
|
var ignored string
|
|
|
|
require.NoError(t, a.RPC("Intention.Apply", &req, &ignored))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check that the intention exists
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Exact: exact,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
|
|
|
require.NoError(t, a.RPC("Intention.Get", req, &resp))
|
|
|
|
require.Len(t, resp.Intentions, 1)
|
|
|
|
actual := resp.Intentions[0]
|
|
|
|
require.Equal(t, "foo", actual.SourceName)
|
|
|
|
require.Empty(t, actual.ID) // new style
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("source is required", func(t *testing.T) {
|
|
|
|
// Delete the intention
|
|
|
|
req, err := http.NewRequest("DELETE", "/v1/connect/intentions/exact?source=&destination=db", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err = a.srv.IntentionExact(resp, req)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("destination is required", func(t *testing.T) {
|
|
|
|
// Delete the intention
|
|
|
|
req, err := http.NewRequest("DELETE", "/v1/connect/intentions/exact?source=foo&destination=", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
_, err = a.srv.IntentionExact(resp, req)
|
|
|
|
require.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("success", func(t *testing.T) {
|
|
|
|
// Delete the intention
|
|
|
|
req, err := http.NewRequest("DELETE", "/v1/connect/intentions/exact?source=foo&destination=db", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionExact(resp, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, obj)
|
|
|
|
|
|
|
|
// Verify the intention is gone
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Exact: exact,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
|
|
|
err := a.RPC("Intention.Get", req, &resp)
|
|
|
|
testutil.RequireErrorContains(t, err, "not found")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntentionSpecificDelete(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
a := NewTestAgent(t, "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
testrpc.WaitForTestAgent(t, a.RPC, "dc1")
|
2018-03-01 23:54:03 +00:00
|
|
|
|
|
|
|
// The intention
|
2018-03-03 18:12:05 +00:00
|
|
|
ixn := structs.TestIntention(t)
|
|
|
|
ixn.SourceName = "foo"
|
2018-03-01 23:54:03 +00:00
|
|
|
|
2021-01-04 17:27:00 +00:00
|
|
|
t.Run("cannot delete non-existent intention", func(t *testing.T) {
|
|
|
|
fakeID := generateUUID()
|
|
|
|
|
|
|
|
req, err := http.NewRequest("DELETE", "/v1/connect/intentions/"+fakeID, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionSpecific(resp, req)
|
|
|
|
testutil.RequireErrorContains(t, err, "Cannot delete non-existent intention")
|
|
|
|
require.Nil(t, obj)
|
|
|
|
})
|
|
|
|
|
2018-03-01 23:54:03 +00:00
|
|
|
// Create an intention directly
|
|
|
|
var reply string
|
|
|
|
{
|
|
|
|
req := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: ixn,
|
|
|
|
}
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, a.RPC("Intention.Apply", &req, &reply))
|
2018-03-01 23:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sanity check that the intention exists
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: reply,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, a.RPC("Intention.Get", req, &resp))
|
|
|
|
require.Len(t, resp.Intentions, 1)
|
2018-03-01 23:54:03 +00:00
|
|
|
actual := resp.Intentions[0]
|
2020-10-06 18:24:05 +00:00
|
|
|
require.Equal(t, "foo", actual.SourceName)
|
2018-03-01 23:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the intention
|
|
|
|
req, _ := http.NewRequest("DELETE", fmt.Sprintf("/v1/connect/intentions/%s", reply), nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionSpecific(resp, req)
|
2020-10-06 18:24:05 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, true, obj)
|
2018-03-01 23:54:03 +00:00
|
|
|
|
|
|
|
// Verify the intention is gone
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: reply,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
|
|
|
err := a.RPC("Intention.Get", req, &resp)
|
2020-10-06 18:24:05 +00:00
|
|
|
testutil.RequireErrorContains(t, err, "not found")
|
2018-03-01 23:54:03 +00:00
|
|
|
}
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 18:24:05 +00:00
|
|
|
func TestParseIntentionStringComponent(t *testing.T) {
|
2018-03-02 22:17:21 +00:00
|
|
|
cases := []struct {
|
2021-10-01 16:36:12 +00:00
|
|
|
TestName string
|
|
|
|
Input string
|
|
|
|
ExpectedAP string
|
|
|
|
ExpectedNS string
|
|
|
|
ExpectedName string
|
|
|
|
Err bool
|
2018-03-02 22:17:21 +00:00
|
|
|
}{
|
|
|
|
{
|
2021-10-01 16:36:12 +00:00
|
|
|
TestName: "single name",
|
|
|
|
Input: "foo",
|
|
|
|
ExpectedAP: "",
|
|
|
|
ExpectedNS: "",
|
|
|
|
ExpectedName: "foo",
|
2018-03-02 22:17:21 +00:00
|
|
|
},
|
|
|
|
{
|
2021-10-01 16:36:12 +00:00
|
|
|
TestName: "namespace and name",
|
|
|
|
Input: "foo/bar",
|
|
|
|
ExpectedAP: "",
|
|
|
|
ExpectedNS: "foo",
|
|
|
|
ExpectedName: "bar",
|
2020-10-06 18:24:05 +00:00
|
|
|
},
|
|
|
|
{
|
2021-10-01 16:36:12 +00:00
|
|
|
TestName: "partition, namespace, and name",
|
|
|
|
Input: "foo/bar/baz",
|
|
|
|
ExpectedAP: "foo",
|
|
|
|
ExpectedNS: "bar",
|
|
|
|
ExpectedName: "baz",
|
2018-03-02 22:17:21 +00:00
|
|
|
},
|
|
|
|
{
|
2021-10-01 16:36:12 +00:00
|
|
|
TestName: "empty ns",
|
|
|
|
Input: "/bar",
|
|
|
|
ExpectedAP: "",
|
|
|
|
ExpectedNS: "",
|
|
|
|
ExpectedName: "bar",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TestName: "invalid input",
|
|
|
|
Input: "uhoh/blah/foo/bar",
|
|
|
|
Err: true,
|
2018-03-02 22:17:21 +00:00
|
|
|
},
|
|
|
|
}
|
2018-03-01 23:54:03 +00:00
|
|
|
|
2018-03-02 22:17:21 +00:00
|
|
|
for _, tc := range cases {
|
2021-10-01 16:36:12 +00:00
|
|
|
t.Run(tc.TestName, func(t *testing.T) {
|
2020-06-26 21:59:15 +00:00
|
|
|
var entMeta structs.EnterpriseMeta
|
2021-10-01 16:36:12 +00:00
|
|
|
ap, ns, name, err := parseIntentionStringComponent(tc.Input, &entMeta)
|
2020-10-06 18:24:05 +00:00
|
|
|
if tc.Err {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-10-01 16:36:12 +00:00
|
|
|
assert.Equal(t, tc.ExpectedAP, ap)
|
2020-10-06 18:24:05 +00:00
|
|
|
assert.Equal(t, tc.ExpectedNS, ns)
|
|
|
|
assert.Equal(t, tc.ExpectedName, name)
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-03-01 23:54:03 +00:00
|
|
|
}
|