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"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2018-03-06 18:35:20 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-02-28 19:36:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestIntentionsList_empty(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2018-02-28 19:36:54 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// 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)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(err)
|
2018-02-28 19:36:54 +00:00
|
|
|
|
|
|
|
value := obj.(structs.Intentions)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotNil(value)
|
|
|
|
assert.Len(value, 0)
|
2018-02-28 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntentionsList_values(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2018-02-28 19:36:54 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Create some intentions
|
|
|
|
for _, v := range []string{"foo", "bar"} {
|
|
|
|
req := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
2018-03-03 18:12:05 +00:00
|
|
|
Intention: structs.TestIntention(t),
|
2018-02-28 19:36:54 +00:00
|
|
|
}
|
2018-03-03 18:12:05 +00:00
|
|
|
req.Intention.SourceName = v
|
|
|
|
|
2018-02-28 19:36:54 +00:00
|
|
|
var reply string
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Apply", &req, &reply))
|
2018-02-28 19:36:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET", "/v1/connect/intentions", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionList(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(err)
|
2018-02-28 19:36:54 +00:00
|
|
|
|
|
|
|
value := obj.(structs.Intentions)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Len(value, 2)
|
2018-02-28 19:36:54 +00:00
|
|
|
|
|
|
|
expected := []string{"bar", "foo"}
|
|
|
|
actual := []string{value[0].SourceName, value[1].SourceName}
|
|
|
|
sort.Strings(actual)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(expected, actual)
|
2018-02-28 19:36:54 +00:00
|
|
|
}
|
2018-02-28 22:02:00 +00:00
|
|
|
|
2018-03-02 22:17:21 +00:00
|
|
|
func TestIntentionsMatch_basic(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2018-03-02 22:17:21 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Create some intentions
|
|
|
|
{
|
|
|
|
insert := [][]string{
|
|
|
|
{"foo", "*"},
|
|
|
|
{"foo", "bar"},
|
|
|
|
{"foo", "baz"}, // shouldn't match
|
|
|
|
{"bar", "bar"}, // shouldn't match
|
|
|
|
{"bar", "*"}, // shouldn't match
|
|
|
|
{"*", "*"},
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-03 18:12:05 +00:00
|
|
|
ixn.Intention.DestinationNS = v[0]
|
|
|
|
ixn.Intention.DestinationName = v[1]
|
2018-03-02 22:17:21 +00:00
|
|
|
|
|
|
|
// Create
|
|
|
|
var reply string
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Apply", &ixn, &reply))
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"/v1/connect/intentions/match?by=destination&name=foo/bar", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(err)
|
2018-03-02 22:17:21 +00:00
|
|
|
|
|
|
|
value := obj.(map[string]structs.Intentions)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Len(value, 1)
|
2018-03-02 22:17:21 +00:00
|
|
|
|
|
|
|
var actual [][]string
|
|
|
|
expected := [][]string{{"foo", "bar"}, {"foo", "*"}, {"*", "*"}}
|
|
|
|
for _, ixn := range value["foo/bar"] {
|
|
|
|
actual = append(actual, []string{ixn.DestinationNS, ixn.DestinationName})
|
|
|
|
}
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(expected, actual)
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntentionsMatch_noBy(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2018-03-02 22:17:21 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"/v1/connect/intentions/match?name=foo/bar", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err.Error(), "by")
|
|
|
|
assert.Nil(obj)
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntentionsMatch_byInvalid(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2018-03-02 22:17:21 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"/v1/connect/intentions/match?by=datacenter", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err.Error(), "'by' parameter")
|
|
|
|
assert.Nil(obj)
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestIntentionsMatch_noName(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2018-03-02 22:17:21 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Request
|
|
|
|
req, _ := http.NewRequest("GET",
|
|
|
|
"/v1/connect/intentions/match?by=source", nil)
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionMatch(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err.Error(), "'name' not set")
|
|
|
|
assert.Nil(obj)
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 22:02:00 +00:00
|
|
|
func TestIntentionsCreate_good(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2018-02-28 22:02:00 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// Make sure an empty list is non-nil.
|
2018-03-03 18:12:05 +00:00
|
|
|
args := structs.TestIntention(t)
|
|
|
|
args.SourceName = "foo"
|
2018-02-28 22:02:00 +00:00
|
|
|
req, _ := http.NewRequest("POST", "/v1/connect/intentions", jsonReader(args))
|
|
|
|
resp := httptest.NewRecorder()
|
|
|
|
obj, err := a.srv.IntentionCreate(resp, req)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(err)
|
2018-02-28 22:02:00 +00:00
|
|
|
|
|
|
|
value := obj.(intentionCreateResponse)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotEqual("", value.ID)
|
2018-02-28 22:02:00 +00:00
|
|
|
|
|
|
|
// Read the value
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: value.ID,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Get", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 1)
|
2018-02-28 22:02:00 +00:00
|
|
|
actual := resp.Intentions[0]
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal("foo", actual.SourceName)
|
2018-02-28 22:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-28 23:54:48 +00:00
|
|
|
|
|
|
|
func TestIntentionsSpecificGet_good(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2018-02-28 23:54:48 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// The intention
|
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,
|
|
|
|
}
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Apply", &req, &reply))
|
2018-02-28 23:54:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(err)
|
2018-02-28 23:54:48 +00:00
|
|
|
|
|
|
|
value := obj.(*structs.Intention)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(reply, value.ID)
|
2018-02-28 23:54:48 +00:00
|
|
|
|
|
|
|
ixn.ID = value.ID
|
|
|
|
ixn.RaftIndex = value.RaftIndex
|
2018-03-03 16:51:40 +00:00
|
|
|
ixn.CreatedAt, ixn.UpdatedAt = value.CreatedAt, value.UpdatedAt
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(ixn, value)
|
2018-02-28 23:54:48 +00:00
|
|
|
}
|
2018-03-01 23:54:03 +00:00
|
|
|
|
2018-03-02 00:53:31 +00:00
|
|
|
func TestIntentionsSpecificUpdate_good(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2018-03-02 00:53:31 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
}
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(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)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(err)
|
|
|
|
assert.Nil(obj)
|
2018-03-02 00:53:31 +00:00
|
|
|
|
|
|
|
// Read the value
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: reply,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Get", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 1)
|
2018-03-02 00:53:31 +00:00
|
|
|
actual := resp.Intentions[0]
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal("bar", actual.SourceName)
|
2018-03-02 00:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 23:54:03 +00:00
|
|
|
func TestIntentionsSpecificDelete_good(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2018-03-01 23:54:03 +00:00
|
|
|
a := NewTestAgent(t.Name(), "")
|
|
|
|
defer a.Shutdown()
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
// Create an intention directly
|
|
|
|
var reply string
|
|
|
|
{
|
|
|
|
req := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: ixn,
|
|
|
|
}
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(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
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(a.RPC("Intention.Get", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 1)
|
2018-03-01 23:54:03 +00:00
|
|
|
actual := resp.Intentions[0]
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal("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)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(err)
|
|
|
|
assert.Nil(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)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err.Error(), "not found")
|
2018-03-01 23:54:03 +00:00
|
|
|
}
|
2018-03-02 22:17:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseIntentionMatchEntry(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
Input string
|
|
|
|
Expected structs.IntentionMatchEntry
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"foo",
|
|
|
|
structs.IntentionMatchEntry{
|
2018-03-03 16:51:40 +00:00
|
|
|
Namespace: structs.IntentionDefaultNamespace,
|
|
|
|
Name: "foo",
|
2018-03-02 22:17:21 +00:00
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"foo/bar",
|
|
|
|
structs.IntentionMatchEntry{
|
|
|
|
Namespace: "foo",
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
"foo/bar/baz",
|
|
|
|
structs.IntentionMatchEntry{},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
2018-03-01 23:54:03 +00:00
|
|
|
|
2018-03-02 22:17:21 +00:00
|
|
|
for _, tc := range cases {
|
|
|
|
t.Run(tc.Input, func(t *testing.T) {
|
2018-03-06 18:35:20 +00:00
|
|
|
assert := assert.New(t)
|
2018-03-02 22:17:21 +00:00
|
|
|
actual, err := parseIntentionMatchEntry(tc.Input)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(err != nil, tc.Err, err)
|
2018-03-02 22:17:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(tc.Expected, actual)
|
2018-03-02 22:17:21 +00:00
|
|
|
})
|
|
|
|
}
|
2018-03-01 23:54:03 +00:00
|
|
|
}
|