2018-02-28 18:04:27 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-03-03 16:43:19 +00:00
|
|
|
"time"
|
2018-02-28 18:04:27 +00:00
|
|
|
|
2018-03-04 08:39:56 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2018-02-28 18:04:27 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/testrpc"
|
|
|
|
"github.com/hashicorp/net-rpc-msgpackrpc"
|
2018-03-06 18:35:20 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-02-28 18:04:27 +00:00
|
|
|
)
|
|
|
|
|
2018-03-01 05:11:35 +00:00
|
|
|
// Test basic creation
|
2018-02-28 18:28:07 +00:00
|
|
|
func TestIntentionApply_new(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:35:20 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-02-28 18:28:07 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Setup a basic record to create
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: &structs.Intention{
|
2018-03-03 16:51:40 +00:00
|
|
|
SourceNS: structs.IntentionDefaultNamespace,
|
|
|
|
SourceName: "test",
|
|
|
|
DestinationNS: structs.IntentionDefaultNamespace,
|
|
|
|
DestinationName: "test",
|
2018-03-03 17:43:37 +00:00
|
|
|
Action: structs.IntentionActionAllow,
|
2018-03-03 17:55:27 +00:00
|
|
|
SourceType: structs.IntentionSourceConsul,
|
2018-03-03 17:43:37 +00:00
|
|
|
Meta: map[string]string{},
|
2018-02-28 18:28:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var reply string
|
|
|
|
|
2018-03-03 16:43:19 +00:00
|
|
|
// Record now to check created at time
|
|
|
|
now := time.Now()
|
|
|
|
|
2018-02-28 18:28:07 +00:00
|
|
|
// Create
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
|
|
|
assert.NotEmpty(reply)
|
2018-02-28 18:28:07 +00:00
|
|
|
|
2018-02-28 18:44:49 +00:00
|
|
|
// Read
|
|
|
|
ixn.Intention.ID = reply
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: ixn.Intention.ID,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Get", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 1)
|
2018-02-28 18:44:49 +00:00
|
|
|
actual := resp.Intentions[0]
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(resp.Index, actual.ModifyIndex)
|
|
|
|
assert.WithinDuration(now, actual.CreatedAt, 5*time.Second)
|
|
|
|
assert.WithinDuration(now, actual.UpdatedAt, 5*time.Second)
|
2018-03-03 16:43:19 +00:00
|
|
|
|
2018-02-28 18:44:49 +00:00
|
|
|
actual.CreateIndex, actual.ModifyIndex = 0, 0
|
2018-03-03 16:43:19 +00:00
|
|
|
actual.CreatedAt = ixn.Intention.CreatedAt
|
|
|
|
actual.UpdatedAt = ixn.Intention.UpdatedAt
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(ixn.Intention, actual)
|
2018-02-28 18:44:49 +00:00
|
|
|
}
|
2018-02-28 18:28:07 +00:00
|
|
|
}
|
|
|
|
|
2018-03-03 17:55:27 +00:00
|
|
|
// Test the source type defaults
|
|
|
|
func TestIntentionApply_defaultSourceType(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:35:20 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-03 17:55:27 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Setup a basic record to create
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: &structs.Intention{
|
|
|
|
SourceNS: structs.IntentionDefaultNamespace,
|
|
|
|
SourceName: "test",
|
|
|
|
DestinationNS: structs.IntentionDefaultNamespace,
|
|
|
|
DestinationName: "test",
|
|
|
|
Action: structs.IntentionActionAllow,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var reply string
|
|
|
|
|
|
|
|
// Create
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
|
|
|
assert.NotEmpty(reply)
|
2018-03-03 17:55:27 +00:00
|
|
|
|
|
|
|
// Read
|
|
|
|
ixn.Intention.ID = reply
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: ixn.Intention.ID,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Get", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 1)
|
2018-03-03 17:55:27 +00:00
|
|
|
actual := resp.Intentions[0]
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(structs.IntentionSourceConsul, actual.SourceType)
|
2018-03-03 17:55:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 05:16:45 +00:00
|
|
|
// Shouldn't be able to create with an ID set
|
|
|
|
func TestIntentionApply_createWithID(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:35:20 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-01 05:16:45 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Setup a basic record to create
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: &structs.Intention{
|
|
|
|
ID: generateUUID(),
|
|
|
|
SourceName: "test",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var reply string
|
|
|
|
|
|
|
|
// Create
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err, "ID must be empty")
|
2018-03-01 05:16:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 05:11:35 +00:00
|
|
|
// Test basic updating
|
|
|
|
func TestIntentionApply_updateGood(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:35:20 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-01 05:11:35 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Setup a basic record to create
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: &structs.Intention{
|
2018-03-03 16:51:40 +00:00
|
|
|
SourceNS: structs.IntentionDefaultNamespace,
|
|
|
|
SourceName: "test",
|
|
|
|
DestinationNS: structs.IntentionDefaultNamespace,
|
|
|
|
DestinationName: "test",
|
2018-03-03 17:43:37 +00:00
|
|
|
Action: structs.IntentionActionAllow,
|
2018-03-03 17:55:27 +00:00
|
|
|
SourceType: structs.IntentionSourceConsul,
|
2018-03-03 17:43:37 +00:00
|
|
|
Meta: map[string]string{},
|
2018-03-01 05:11:35 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var reply string
|
|
|
|
|
|
|
|
// Create
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
|
|
|
assert.NotEmpty(reply)
|
2018-03-01 05:11:35 +00:00
|
|
|
|
2018-03-03 16:43:19 +00:00
|
|
|
// Read CreatedAt
|
|
|
|
var createdAt time.Time
|
|
|
|
ixn.Intention.ID = reply
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: ixn.Intention.ID,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Get", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 1)
|
2018-03-03 16:43:19 +00:00
|
|
|
actual := resp.Intentions[0]
|
|
|
|
createdAt = actual.CreatedAt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep a bit so that the updated at will definitely be different, not much
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
|
2018-03-01 05:11:35 +00:00
|
|
|
// Update
|
|
|
|
ixn.Op = structs.IntentionOpUpdate
|
|
|
|
ixn.Intention.ID = reply
|
|
|
|
ixn.Intention.SourceName = "bar"
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-01 05:11:35 +00:00
|
|
|
|
|
|
|
// Read
|
|
|
|
ixn.Intention.ID = reply
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: ixn.Intention.ID,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Get", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 1)
|
2018-03-01 05:11:35 +00:00
|
|
|
actual := resp.Intentions[0]
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(createdAt, actual.CreatedAt)
|
|
|
|
assert.WithinDuration(time.Now(), actual.UpdatedAt, 5*time.Second)
|
2018-03-03 16:43:19 +00:00
|
|
|
|
2018-03-01 05:11:35 +00:00
|
|
|
actual.CreateIndex, actual.ModifyIndex = 0, 0
|
2018-03-03 16:43:19 +00:00
|
|
|
actual.CreatedAt = ixn.Intention.CreatedAt
|
|
|
|
actual.UpdatedAt = ixn.Intention.UpdatedAt
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(ixn.Intention, actual)
|
2018-03-01 05:11:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shouldn't be able to update a non-existent intention
|
|
|
|
func TestIntentionApply_updateNonExist(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:35:20 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-01 05:11:35 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Setup a basic record to create
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpUpdate,
|
|
|
|
Intention: &structs.Intention{
|
|
|
|
ID: generateUUID(),
|
|
|
|
SourceName: "test",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var reply string
|
|
|
|
|
|
|
|
// Create
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err, "Cannot modify non-existent intention")
|
2018-03-01 05:11:35 +00:00
|
|
|
}
|
|
|
|
|
2018-03-01 23:48:48 +00:00
|
|
|
// Test basic deleting
|
|
|
|
func TestIntentionApply_deleteGood(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:35:20 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-01 23:48:48 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Setup a basic record to create
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: &structs.Intention{
|
2018-03-03 17:43:37 +00:00
|
|
|
SourceNS: "test",
|
|
|
|
SourceName: "test",
|
|
|
|
DestinationNS: "test",
|
|
|
|
DestinationName: "test",
|
|
|
|
Action: structs.IntentionActionAllow,
|
2018-03-01 23:48:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
var reply string
|
|
|
|
|
|
|
|
// Create
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
|
|
|
assert.NotEmpty(reply)
|
2018-03-01 23:48:48 +00:00
|
|
|
|
|
|
|
// Delete
|
|
|
|
ixn.Op = structs.IntentionOpDelete
|
|
|
|
ixn.Intention.ID = reply
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-01 23:48:48 +00:00
|
|
|
|
|
|
|
// Read
|
|
|
|
ixn.Intention.ID = reply
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: ixn.Intention.ID,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Intention.Get", req, &resp)
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err, ErrIntentionNotFound.Error())
|
2018-03-01 23:48:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 08:39:56 +00:00
|
|
|
// Test apply with a deny ACL
|
|
|
|
func TestIntentionApply_aclDeny(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:51:26 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-04 08:39:56 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create an ACL with write permissions
|
|
|
|
var token string
|
|
|
|
{
|
|
|
|
var rules = `
|
|
|
|
service "foo" {
|
|
|
|
policy = "deny"
|
|
|
|
intentions = "write"
|
|
|
|
}`
|
|
|
|
|
|
|
|
req := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: rules,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "ACL.Apply", &req, &token))
|
2018-03-04 08:39:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a basic record to create
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: structs.TestIntention(t),
|
|
|
|
}
|
|
|
|
ixn.Intention.DestinationName = "foobar"
|
|
|
|
|
|
|
|
// Create without a token should error since default deny
|
|
|
|
var reply string
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply)
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.True(acl.IsErrPermissionDenied(err))
|
2018-03-04 08:39:56 +00:00
|
|
|
|
|
|
|
// Now add the token and try again.
|
|
|
|
ixn.WriteRequest.Token = token
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-04 08:39:56 +00:00
|
|
|
|
|
|
|
// Read
|
|
|
|
ixn.Intention.ID = reply
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
2018-03-06 18:51:26 +00:00
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: ixn.Intention.ID,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: "root"},
|
2018-03-04 08:39:56 +00:00
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Get", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 1)
|
2018-03-04 08:39:56 +00:00
|
|
|
actual := resp.Intentions[0]
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Equal(resp.Index, actual.ModifyIndex)
|
2018-03-04 08:39:56 +00:00
|
|
|
|
|
|
|
actual.CreateIndex, actual.ModifyIndex = 0, 0
|
|
|
|
actual.CreatedAt = ixn.Intention.CreatedAt
|
|
|
|
actual.UpdatedAt = ixn.Intention.UpdatedAt
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Equal(ixn.Intention, actual)
|
2018-03-04 08:39:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 08:55:23 +00:00
|
|
|
// Test apply with delete and a default deny ACL
|
|
|
|
func TestIntentionApply_aclDelete(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:51:26 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-04 08:55:23 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create an ACL with write permissions
|
|
|
|
var token string
|
|
|
|
{
|
|
|
|
var rules = `
|
|
|
|
service "foo" {
|
|
|
|
policy = "deny"
|
|
|
|
intentions = "write"
|
|
|
|
}`
|
|
|
|
|
|
|
|
req := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: rules,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "ACL.Apply", &req, &token))
|
2018-03-04 08:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a basic record to create
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: structs.TestIntention(t),
|
|
|
|
}
|
|
|
|
ixn.Intention.DestinationName = "foobar"
|
|
|
|
ixn.WriteRequest.Token = token
|
|
|
|
|
|
|
|
// Create
|
|
|
|
var reply string
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-04 08:55:23 +00:00
|
|
|
|
|
|
|
// Try to do a delete with no token; this should get rejected.
|
|
|
|
ixn.Op = structs.IntentionOpDelete
|
|
|
|
ixn.Intention.ID = reply
|
|
|
|
ixn.WriteRequest.Token = ""
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply)
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.True(acl.IsErrPermissionDenied(err))
|
2018-03-04 08:55:23 +00:00
|
|
|
|
|
|
|
// Try again with the original token. This should go through.
|
|
|
|
ixn.WriteRequest.Token = token
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-04 08:55:23 +00:00
|
|
|
|
|
|
|
// Verify it is gone
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: ixn.Intention.ID,
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Intention.Get", req, &resp)
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.NotNil(err)
|
|
|
|
assert.Contains(err.Error(), ErrIntentionNotFound.Error())
|
2018-03-04 08:55:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test apply with update and a default deny ACL
|
|
|
|
func TestIntentionApply_aclUpdate(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:51:26 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-04 08:55:23 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create an ACL with write permissions
|
|
|
|
var token string
|
|
|
|
{
|
|
|
|
var rules = `
|
|
|
|
service "foo" {
|
|
|
|
policy = "deny"
|
|
|
|
intentions = "write"
|
|
|
|
}`
|
|
|
|
|
|
|
|
req := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: rules,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "ACL.Apply", &req, &token))
|
2018-03-04 08:55:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a basic record to create
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: structs.TestIntention(t),
|
|
|
|
}
|
|
|
|
ixn.Intention.DestinationName = "foobar"
|
|
|
|
ixn.WriteRequest.Token = token
|
|
|
|
|
|
|
|
// Create
|
|
|
|
var reply string
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-04 08:55:23 +00:00
|
|
|
|
|
|
|
// Try to do an update without a token; this should get rejected.
|
|
|
|
ixn.Op = structs.IntentionOpUpdate
|
|
|
|
ixn.Intention.ID = reply
|
|
|
|
ixn.WriteRequest.Token = ""
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply)
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.True(acl.IsErrPermissionDenied(err))
|
2018-03-04 08:55:23 +00:00
|
|
|
|
|
|
|
// Try again with the original token; this should go through.
|
|
|
|
ixn.WriteRequest.Token = token
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-04 08:55:23 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 19:35:39 +00:00
|
|
|
// Test apply with a management token
|
|
|
|
func TestIntentionApply_aclManagement(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:51:26 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-04 19:35:39 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Setup a basic record to create
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: structs.TestIntention(t),
|
|
|
|
}
|
|
|
|
ixn.Intention.DestinationName = "foobar"
|
|
|
|
ixn.WriteRequest.Token = "root"
|
|
|
|
|
|
|
|
// Create
|
|
|
|
var reply string
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-04 19:35:39 +00:00
|
|
|
ixn.Intention.ID = reply
|
|
|
|
|
|
|
|
// Update
|
|
|
|
ixn.Op = structs.IntentionOpUpdate
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-04 19:35:39 +00:00
|
|
|
|
|
|
|
// Delete
|
|
|
|
ixn.Op = structs.IntentionOpDelete
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-04 19:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test update changing the name where an ACL won't allow it
|
|
|
|
func TestIntentionApply_aclUpdateChange(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:51:26 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-04 19:35:39 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create an ACL with write permissions
|
|
|
|
var token string
|
|
|
|
{
|
|
|
|
var rules = `
|
|
|
|
service "foo" {
|
|
|
|
policy = "deny"
|
|
|
|
intentions = "write"
|
|
|
|
}`
|
|
|
|
|
|
|
|
req := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: rules,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "ACL.Apply", &req, &token))
|
2018-03-04 19:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a basic record to create
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: structs.TestIntention(t),
|
|
|
|
}
|
|
|
|
ixn.Intention.DestinationName = "bar"
|
|
|
|
ixn.WriteRequest.Token = "root"
|
|
|
|
|
|
|
|
// Create
|
|
|
|
var reply string
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-04 19:35:39 +00:00
|
|
|
|
|
|
|
// Try to do an update without a token; this should get rejected.
|
|
|
|
ixn.Op = structs.IntentionOpUpdate
|
|
|
|
ixn.Intention.ID = reply
|
|
|
|
ixn.Intention.DestinationName = "foo"
|
|
|
|
ixn.WriteRequest.Token = token
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply)
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.True(acl.IsErrPermissionDenied(err))
|
2018-03-04 19:35:39 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 19:53:52 +00:00
|
|
|
// Test reading with ACLs
|
|
|
|
func TestIntentionGet_acl(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:51:26 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-04 19:53:52 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create an ACL with service write permissions. This will grant
|
|
|
|
// intentions read.
|
|
|
|
var token string
|
|
|
|
{
|
|
|
|
var rules = `
|
|
|
|
service "foo" {
|
|
|
|
policy = "write"
|
|
|
|
}`
|
|
|
|
|
|
|
|
req := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: rules,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "ACL.Apply", &req, &token))
|
2018-03-04 19:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup a basic record to create
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: structs.TestIntention(t),
|
|
|
|
}
|
|
|
|
ixn.Intention.DestinationName = "foobar"
|
|
|
|
ixn.WriteRequest.Token = "root"
|
|
|
|
|
|
|
|
// Create
|
|
|
|
var reply string
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-04 19:53:52 +00:00
|
|
|
ixn.Intention.ID = reply
|
|
|
|
|
|
|
|
// Read without token should be error
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: ixn.Intention.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp structs.IndexedIntentions
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Intention.Get", req, &resp)
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.True(acl.IsErrPermissionDenied(err))
|
|
|
|
assert.Len(resp.Intentions, 0)
|
2018-03-04 19:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read with token should work
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
IntentionID: ixn.Intention.ID,
|
|
|
|
QueryOptions: structs.QueryOptions{Token: token},
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Get", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 1)
|
2018-03-04 19:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 18:04:27 +00:00
|
|
|
func TestIntentionList(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:35:20 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-02-28 18:04:27 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Test with no intentions inserted yet
|
|
|
|
{
|
|
|
|
req := &structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.List", req, &resp))
|
|
|
|
assert.NotNil(resp.Intentions)
|
|
|
|
assert.Len(resp.Intentions, 0)
|
2018-02-28 18:04:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-03-02 21:40:03 +00:00
|
|
|
|
2018-03-05 02:32:28 +00:00
|
|
|
// Test listing with ACLs
|
|
|
|
func TestIntentionList_acl(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:51:26 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-05 02:32:28 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create an ACL with service write permissions. This will grant
|
|
|
|
// intentions read.
|
|
|
|
var token string
|
|
|
|
{
|
|
|
|
var rules = `
|
|
|
|
service "foo" {
|
|
|
|
policy = "write"
|
|
|
|
}`
|
|
|
|
|
|
|
|
req := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: rules,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "ACL.Apply", &req, &token))
|
2018-03-05 02:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a few records
|
|
|
|
for _, name := range []string{"foobar", "bar", "baz"} {
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: structs.TestIntention(t),
|
|
|
|
}
|
|
|
|
ixn.Intention.DestinationName = name
|
|
|
|
ixn.WriteRequest.Token = "root"
|
|
|
|
|
|
|
|
// Create
|
|
|
|
var reply string
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-05 02:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test with no token
|
|
|
|
{
|
|
|
|
req := &structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.List", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 0)
|
2018-03-05 02:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test with management token
|
|
|
|
{
|
|
|
|
req := &structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: "root"},
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.List", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 3)
|
2018-03-05 02:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test with user token
|
|
|
|
{
|
|
|
|
req := &structs.DCSpecificRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
QueryOptions: structs.QueryOptions{Token: token},
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentions
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.List", req, &resp))
|
|
|
|
assert.Len(resp.Intentions, 1)
|
2018-03-05 02:32:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 21:40:03 +00:00
|
|
|
// Test basic matching. We don't need to exhaustively test inputs since this
|
|
|
|
// is tested in the agent/consul/state package.
|
|
|
|
func TestIntentionMatch_good(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:35:20 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-02 21:40:03 +00:00
|
|
|
dir1, s1 := testServer(t)
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create some records
|
|
|
|
{
|
|
|
|
insert := [][]string{
|
2018-04-05 11:53:42 +00:00
|
|
|
{"foo", "*", "foo", "*"},
|
|
|
|
{"foo", "*", "foo", "bar"},
|
|
|
|
{"foo", "*", "foo", "baz"}, // shouldn't match
|
|
|
|
{"foo", "*", "bar", "bar"}, // shouldn't match
|
|
|
|
{"foo", "*", "bar", "*"}, // shouldn't match
|
|
|
|
{"foo", "*", "*", "*"},
|
|
|
|
{"bar", "*", "foo", "bar"}, // duplicate destination different source
|
2018-03-02 21:40:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range insert {
|
|
|
|
ixn := structs.IntentionRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.IntentionOpCreate,
|
|
|
|
Intention: &structs.Intention{
|
2018-04-05 11:53:42 +00:00
|
|
|
SourceNS: v[0],
|
|
|
|
SourceName: v[1],
|
|
|
|
DestinationNS: v[2],
|
|
|
|
DestinationName: v[3],
|
2018-03-03 17:43:37 +00:00
|
|
|
Action: structs.IntentionActionAllow,
|
2018-03-02 21:40:03 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create
|
|
|
|
var reply string
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-02 21:40:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Match: &structs.IntentionQueryMatch{
|
|
|
|
Type: structs.IntentionMatchDestination,
|
|
|
|
Entries: []structs.IntentionMatchEntry{
|
|
|
|
{
|
|
|
|
Namespace: "foo",
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentionMatches
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Match", req, &resp))
|
|
|
|
assert.Len(resp.Matches, 1)
|
2018-03-02 21:40:03 +00:00
|
|
|
|
2018-04-05 11:53:42 +00:00
|
|
|
expected := [][]string{
|
|
|
|
{"bar", "*", "foo", "bar"},
|
|
|
|
{"foo", "*", "foo", "bar"},
|
|
|
|
{"foo", "*", "foo", "*"},
|
|
|
|
{"foo", "*", "*", "*"},
|
|
|
|
}
|
2018-03-02 21:40:03 +00:00
|
|
|
var actual [][]string
|
|
|
|
for _, ixn := range resp.Matches[0] {
|
2018-04-05 11:53:42 +00:00
|
|
|
actual = append(actual, []string{
|
|
|
|
ixn.SourceNS,
|
|
|
|
ixn.SourceName,
|
|
|
|
ixn.DestinationNS,
|
|
|
|
ixn.DestinationName,
|
|
|
|
})
|
2018-03-02 21:40:03 +00:00
|
|
|
}
|
2018-03-06 18:35:20 +00:00
|
|
|
assert.Equal(expected, actual)
|
2018-03-02 21:40:03 +00:00
|
|
|
}
|
2018-03-05 02:32:28 +00:00
|
|
|
|
|
|
|
// Test matching with ACLs
|
|
|
|
func TestIntentionMatch_acl(t *testing.T) {
|
|
|
|
t.Parallel()
|
2018-03-06 18:51:26 +00:00
|
|
|
|
|
|
|
assert := assert.New(t)
|
2018-03-05 02:32:28 +00:00
|
|
|
dir1, s1 := testServerWithConfig(t, func(c *Config) {
|
|
|
|
c.ACLDatacenter = "dc1"
|
|
|
|
c.ACLMasterToken = "root"
|
|
|
|
c.ACLDefaultPolicy = "deny"
|
|
|
|
})
|
|
|
|
defer os.RemoveAll(dir1)
|
|
|
|
defer s1.Shutdown()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
defer codec.Close()
|
|
|
|
|
|
|
|
testrpc.WaitForLeader(t, s1.RPC, "dc1")
|
|
|
|
|
|
|
|
// Create an ACL with service write permissions. This will grant
|
|
|
|
// intentions read.
|
|
|
|
var token string
|
|
|
|
{
|
|
|
|
var rules = `
|
|
|
|
service "bar" {
|
|
|
|
policy = "write"
|
|
|
|
}`
|
|
|
|
|
|
|
|
req := structs.ACLRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Op: structs.ACLSet,
|
|
|
|
ACL: structs.ACL{
|
|
|
|
Name: "User token",
|
|
|
|
Type: structs.ACLTypeClient,
|
|
|
|
Rules: rules,
|
|
|
|
},
|
|
|
|
WriteRequest: structs.WriteRequest{Token: "root"},
|
|
|
|
}
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "ACL.Apply", &req, &token))
|
2018-03-05 02:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create some records
|
|
|
|
{
|
|
|
|
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,
|
|
|
|
Intention: structs.TestIntention(t),
|
|
|
|
}
|
|
|
|
ixn.Intention.DestinationNS = v[0]
|
|
|
|
ixn.Intention.DestinationName = v[1]
|
|
|
|
ixn.WriteRequest.Token = "root"
|
|
|
|
|
|
|
|
// Create
|
|
|
|
var reply string
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Apply", &ixn, &reply))
|
2018-03-05 02:32:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with no token
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Match: &structs.IntentionQueryMatch{
|
|
|
|
Type: structs.IntentionMatchDestination,
|
|
|
|
Entries: []structs.IntentionMatchEntry{
|
|
|
|
{
|
|
|
|
Namespace: "foo",
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentionMatches
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "Intention.Match", req, &resp)
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.True(acl.IsErrPermissionDenied(err))
|
|
|
|
assert.Len(resp.Matches, 0)
|
2018-03-05 02:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test with proper token
|
|
|
|
{
|
|
|
|
req := &structs.IntentionQueryRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Match: &structs.IntentionQueryMatch{
|
|
|
|
Type: structs.IntentionMatchDestination,
|
|
|
|
Entries: []structs.IntentionMatchEntry{
|
|
|
|
{
|
|
|
|
Namespace: "foo",
|
|
|
|
Name: "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
QueryOptions: structs.QueryOptions{Token: token},
|
|
|
|
}
|
|
|
|
var resp structs.IndexedIntentionMatches
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Nil(msgpackrpc.CallWithCodec(codec, "Intention.Match", req, &resp))
|
|
|
|
assert.Len(resp.Matches, 1)
|
2018-03-05 02:32:28 +00:00
|
|
|
|
|
|
|
expected := [][]string{{"foo", "bar"}, {"foo", "*"}, {"*", "*"}}
|
|
|
|
var actual [][]string
|
|
|
|
for _, ixn := range resp.Matches[0] {
|
|
|
|
actual = append(actual, []string{ixn.DestinationNS, ixn.DestinationName})
|
|
|
|
}
|
|
|
|
|
2018-03-06 18:51:26 +00:00
|
|
|
assert.Equal(expected, actual)
|
2018-03-05 02:32:28 +00:00
|
|
|
}
|
|
|
|
}
|