2014-08-11 21:01:45 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
2018-10-19 16:04:07 +00:00
|
|
|
"fmt"
|
2021-09-16 14:17:02 +00:00
|
|
|
"strings"
|
2019-01-22 18:14:43 +00:00
|
|
|
"sync/atomic"
|
2014-08-11 21:01:45 +00:00
|
|
|
"testing"
|
2019-01-22 18:14:43 +00:00
|
|
|
"time"
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2021-09-21 22:48:50 +00:00
|
|
|
"github.com/hashicorp/go-uuid"
|
2020-11-17 23:15:07 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/consul-net-rpc/net-rpc-msgpackrpc"
|
|
|
|
|
2014-08-11 21:18:51 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2022-06-17 09:24:43 +00:00
|
|
|
"github.com/hashicorp/consul/acl/resolver"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2021-04-14 16:39:35 +00:00
|
|
|
"github.com/hashicorp/consul/agent/token"
|
2019-04-26 17:49:28 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2019-03-27 12:54:56 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil/retry"
|
2014-08-11 21:01:45 +00:00
|
|
|
)
|
|
|
|
|
2016-12-09 00:01:01 +00:00
|
|
|
var testACLPolicy = `
|
|
|
|
key "" {
|
|
|
|
policy = "deny"
|
|
|
|
}
|
|
|
|
key "foo/" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
var testACLPolicyNew = `
|
|
|
|
key_prefix "" {
|
|
|
|
policy = "deny"
|
|
|
|
}
|
|
|
|
key_prefix "foo/" {
|
|
|
|
policy = "write"
|
|
|
|
}
|
|
|
|
`
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2019-01-22 18:14:43 +00:00
|
|
|
type asyncResolutionResult struct {
|
|
|
|
authz acl.Authorizer
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2022-06-17 09:24:43 +00:00
|
|
|
func verifyAuthorizerChain(t *testing.T, expected resolver.Result, actual resolver.Result) {
|
2022-01-23 17:31:48 +00:00
|
|
|
t.Helper()
|
|
|
|
expectedChainAuthz, ok := expected.Authorizer.(*acl.ChainedAuthorizer)
|
2019-10-25 15:06:16 +00:00
|
|
|
require.True(t, ok, "expected Authorizer is not a ChainedAuthorizer")
|
2022-01-23 17:31:48 +00:00
|
|
|
actualChainAuthz, ok := actual.Authorizer.(*acl.ChainedAuthorizer)
|
2019-10-25 15:06:16 +00:00
|
|
|
require.True(t, ok, "actual Authorizer is not a ChainedAuthorizer")
|
|
|
|
|
|
|
|
expectedChain := expectedChainAuthz.AuthorizerChain()
|
|
|
|
actualChain := actualChainAuthz.AuthorizerChain()
|
|
|
|
|
|
|
|
require.Equal(t, len(expectedChain), len(actualChain), "ChainedAuthorizers have different length chains")
|
|
|
|
for idx, expectedAuthz := range expectedChain {
|
|
|
|
actualAuthz := actualChain[idx]
|
|
|
|
|
|
|
|
// pointer equality - because we want to verify authorizer reuse
|
|
|
|
require.True(t, expectedAuthz == actualAuthz, "Authorizer pointers are not equal")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-22 18:14:43 +00:00
|
|
|
func resolveTokenAsync(r *ACLResolver, token string, ch chan *asyncResolutionResult) {
|
2022-01-23 17:31:48 +00:00
|
|
|
authz, err := r.ResolveToken(token)
|
2019-01-22 18:14:43 +00:00
|
|
|
ch <- &asyncResolutionResult{authz: authz, err: err}
|
|
|
|
}
|
|
|
|
|
2020-11-17 23:15:07 +00:00
|
|
|
func resolveToken(t *testing.T, r *ACLResolver, token string) acl.Authorizer {
|
|
|
|
t.Helper()
|
2022-01-23 17:31:48 +00:00
|
|
|
authz, err := r.ResolveToken(token)
|
2020-11-17 23:15:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
return authz
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func testIdentityForToken(token string) (bool, structs.ACLIdentity, error) {
|
|
|
|
switch token {
|
2019-04-15 20:43:19 +00:00
|
|
|
case "missing-role":
|
|
|
|
return true, &structs.ACLToken{
|
|
|
|
AccessorID: "435a75af-1763-4980-89f4-f0951dda53b4",
|
|
|
|
SecretID: "b1b6be70-ed2e-4c80-8495-bdb3db110b1e",
|
|
|
|
Roles: []structs.ACLTokenRoleLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "not-found",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "acl-ro",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2018-10-19 16:04:07 +00:00
|
|
|
case "found":
|
|
|
|
return true, &structs.ACLToken{
|
|
|
|
AccessorID: "5f57c1f6-6a89-4186-9445-531b316e01df",
|
|
|
|
SecretID: "a1a54629-5050-4d17-8a4e-560d2423f835",
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2018-10-19 16:04:07 +00:00
|
|
|
ID: "node-wr",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2018-10-19 16:04:07 +00:00
|
|
|
ID: "dc2-key-wr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2019-04-15 20:43:19 +00:00
|
|
|
case "found-role":
|
|
|
|
// This should be permission-wise identical to "found", except it
|
|
|
|
// gets it's policies indirectly by way of a Role.
|
|
|
|
return true, &structs.ACLToken{
|
|
|
|
AccessorID: "5f57c1f6-6a89-4186-9445-531b316e01df",
|
|
|
|
SecretID: "a1a54629-5050-4d17-8a4e-560d2423f835",
|
|
|
|
Roles: []structs.ACLTokenRoleLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "found",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case "found-policy-and-role":
|
|
|
|
return true, &structs.ACLToken{
|
|
|
|
AccessorID: "5f57c1f6-6a89-4186-9445-531b316e01df",
|
|
|
|
SecretID: "a1a54629-5050-4d17-8a4e-560d2423f835",
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "node-wr",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "dc2-key-wr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Roles: []structs.ACLTokenRoleLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "service-ro",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2020-06-16 16:54:27 +00:00
|
|
|
case "found-role-node-identity":
|
|
|
|
return true, &structs.ACLToken{
|
|
|
|
AccessorID: "f3f47a09-de29-4c57-8f54-b65a9be79641",
|
|
|
|
SecretID: "e96aca00-5951-4b97-b0e5-5816f42dfb93",
|
|
|
|
Roles: []structs.ACLTokenRoleLink{
|
|
|
|
{
|
|
|
|
ID: "node-identity",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2018-10-19 16:04:07 +00:00
|
|
|
case "acl-ro":
|
|
|
|
return true, &structs.ACLToken{
|
|
|
|
AccessorID: "435a75af-1763-4980-89f4-f0951dda53b4",
|
|
|
|
SecretID: "b1b6be70-ed2e-4c80-8495-bdb3db110b1e",
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2018-10-19 16:04:07 +00:00
|
|
|
ID: "acl-ro",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case "acl-wr":
|
|
|
|
return true, &structs.ACLToken{
|
|
|
|
AccessorID: "435a75af-1763-4980-89f4-f0951dda53b4",
|
|
|
|
SecretID: "b1b6be70-ed2e-4c80-8495-bdb3db110b1e",
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2018-10-19 16:04:07 +00:00
|
|
|
ID: "acl-wr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2019-01-22 18:14:43 +00:00
|
|
|
case "racey-unmodified":
|
|
|
|
return true, &structs.ACLToken{
|
|
|
|
AccessorID: "5f57c1f6-6a89-4186-9445-531b316e01df",
|
|
|
|
SecretID: "a1a54629-5050-4d17-8a4e-560d2423f835",
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-01-22 18:14:43 +00:00
|
|
|
ID: "node-wr",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-01-22 18:14:43 +00:00
|
|
|
ID: "acl-wr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case "racey-modified":
|
|
|
|
return true, &structs.ACLToken{
|
|
|
|
AccessorID: "5f57c1f6-6a89-4186-9445-531b316e01df",
|
|
|
|
SecretID: "a1a54629-5050-4d17-8a4e-560d2423f835",
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-01-22 18:14:43 +00:00
|
|
|
ID: "node-wr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2019-03-14 14:35:34 +00:00
|
|
|
case "concurrent-resolve":
|
2019-01-22 18:14:43 +00:00
|
|
|
return true, &structs.ACLToken{
|
|
|
|
AccessorID: "5f57c1f6-6a89-4186-9445-531b316e01df",
|
|
|
|
SecretID: "a1a54629-5050-4d17-8a4e-560d2423f835",
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-01-22 18:14:43 +00:00
|
|
|
ID: "node-wr",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-01-22 18:14:43 +00:00
|
|
|
ID: "acl-wr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2018-10-19 16:04:07 +00:00
|
|
|
default:
|
2021-09-16 14:17:02 +00:00
|
|
|
return true, nil, acl.ErrNotFound
|
2014-08-11 21:01:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func testPolicyForID(policyID string) (bool, *structs.ACLPolicy, error) {
|
|
|
|
switch policyID {
|
|
|
|
case "acl-ro":
|
2021-07-16 21:59:56 +00:00
|
|
|
p := &structs.ACLPolicy{
|
2018-10-19 16:04:07 +00:00
|
|
|
ID: "acl-ro",
|
|
|
|
Name: "acl-ro",
|
|
|
|
Description: "acl-ro",
|
|
|
|
Rules: `acl = "read"`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
2021-07-16 21:59:56 +00:00
|
|
|
}
|
|
|
|
p.SetHash(false)
|
|
|
|
return true, p, nil
|
2018-10-19 16:04:07 +00:00
|
|
|
case "acl-wr":
|
2021-06-09 22:14:36 +00:00
|
|
|
p := &structs.ACLPolicy{
|
2018-10-19 16:04:07 +00:00
|
|
|
ID: "acl-wr",
|
|
|
|
Name: "acl-wr",
|
|
|
|
Description: "acl-wr",
|
|
|
|
Rules: `acl = "write"`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
2021-06-09 22:14:36 +00:00
|
|
|
}
|
|
|
|
p.SetHash(false)
|
|
|
|
return true, p, nil
|
2019-04-15 20:43:19 +00:00
|
|
|
case "service-ro":
|
2021-07-16 21:59:56 +00:00
|
|
|
p := &structs.ACLPolicy{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "service-ro",
|
|
|
|
Name: "service-ro",
|
|
|
|
Description: "service-ro",
|
|
|
|
Rules: `service_prefix "" { policy = "read" }`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
2021-07-16 21:59:56 +00:00
|
|
|
}
|
|
|
|
p.SetHash(false)
|
|
|
|
return true, p, nil
|
2019-04-15 20:43:19 +00:00
|
|
|
case "service-wr":
|
2021-07-16 21:59:56 +00:00
|
|
|
p := &structs.ACLPolicy{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "service-wr",
|
|
|
|
Name: "service-wr",
|
|
|
|
Description: "service-wr",
|
|
|
|
Rules: `service_prefix "" { policy = "write" }`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
2021-07-16 21:59:56 +00:00
|
|
|
}
|
|
|
|
p.SetHash(false)
|
|
|
|
return true, p, nil
|
2018-10-19 16:04:07 +00:00
|
|
|
case "node-wr":
|
2021-06-09 22:14:36 +00:00
|
|
|
p := &structs.ACLPolicy{
|
2018-10-19 16:04:07 +00:00
|
|
|
ID: "node-wr",
|
|
|
|
Name: "node-wr",
|
|
|
|
Description: "node-wr",
|
|
|
|
Rules: `node_prefix "" { policy = "write"}`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
Datacenters: []string{"dc1"},
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
2021-06-09 22:14:36 +00:00
|
|
|
}
|
|
|
|
p.SetHash(false)
|
|
|
|
return true, p, nil
|
2018-10-19 16:04:07 +00:00
|
|
|
case "dc2-key-wr":
|
2021-07-16 21:59:56 +00:00
|
|
|
p := &structs.ACLPolicy{
|
2018-10-19 16:04:07 +00:00
|
|
|
ID: "dc2-key-wr",
|
|
|
|
Name: "dc2-key-wr",
|
|
|
|
Description: "dc2-key-wr",
|
|
|
|
Rules: `key_prefix "" { policy = "write"}`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
Datacenters: []string{"dc2"},
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
2021-07-16 21:59:56 +00:00
|
|
|
}
|
|
|
|
p.SetHash(false)
|
|
|
|
return true, p, nil
|
2018-10-19 16:04:07 +00:00
|
|
|
default:
|
2021-09-16 14:17:02 +00:00
|
|
|
return true, nil, acl.ErrNotFound
|
2014-08-12 17:58:02 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2014-08-12 17:58:02 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func testRoleForID(roleID string) (bool, *structs.ACLRole, error) {
|
|
|
|
switch roleID {
|
|
|
|
case "service-ro":
|
|
|
|
return true, &structs.ACLRole{
|
|
|
|
ID: "service-ro",
|
|
|
|
Name: "service-ro",
|
|
|
|
Description: "service-ro",
|
|
|
|
Policies: []structs.ACLRolePolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "service-ro",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
}, nil
|
|
|
|
case "service-wr":
|
|
|
|
return true, &structs.ACLRole{
|
|
|
|
ID: "service-wr",
|
|
|
|
Name: "service-wr",
|
|
|
|
Description: "service-wr",
|
|
|
|
Policies: []structs.ACLRolePolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "service-wr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
}, nil
|
|
|
|
case "found":
|
|
|
|
return true, &structs.ACLRole{
|
|
|
|
ID: "found",
|
|
|
|
Name: "found",
|
|
|
|
Description: "found",
|
|
|
|
Policies: []structs.ACLRolePolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "node-wr",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "dc2-key-wr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case "acl-ro":
|
|
|
|
return true, &structs.ACLRole{
|
|
|
|
ID: "acl-ro",
|
|
|
|
Name: "acl-ro",
|
|
|
|
Description: "acl-ro",
|
|
|
|
Policies: []structs.ACLRolePolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "acl-ro",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case "acl-wr":
|
|
|
|
return true, &structs.ACLRole{
|
|
|
|
ID: "acl-rw",
|
|
|
|
Name: "acl-rw",
|
|
|
|
Description: "acl-rw",
|
|
|
|
Policies: []structs.ACLRolePolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "acl-wr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case "racey-unmodified":
|
|
|
|
return true, &structs.ACLRole{
|
|
|
|
ID: "racey-unmodified",
|
|
|
|
Name: "racey-unmodified",
|
|
|
|
Description: "racey-unmodified",
|
|
|
|
Policies: []structs.ACLRolePolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "node-wr",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "acl-wr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case "racey-modified":
|
|
|
|
return true, &structs.ACLRole{
|
|
|
|
ID: "racey-modified",
|
|
|
|
Name: "racey-modified",
|
|
|
|
Description: "racey-modified",
|
|
|
|
Policies: []structs.ACLRolePolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "node-wr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case "concurrent-resolve-1":
|
|
|
|
return true, &structs.ACLRole{
|
|
|
|
ID: "concurrent-resolve-1",
|
|
|
|
Name: "concurrent-resolve-1",
|
|
|
|
Description: "concurrent-resolve-1",
|
|
|
|
Policies: []structs.ACLRolePolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "node-wr",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "acl-wr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
case "concurrent-resolve-2":
|
|
|
|
return true, &structs.ACLRole{
|
|
|
|
ID: "concurrent-resolve-2",
|
|
|
|
Name: "concurrent-resolve-2",
|
|
|
|
Description: "concurrent-resolve-2",
|
|
|
|
Policies: []structs.ACLRolePolicyLink{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "node-wr",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2019-04-15 20:43:19 +00:00
|
|
|
ID: "acl-wr",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2020-06-16 16:54:27 +00:00
|
|
|
case "node-identity":
|
|
|
|
return true, &structs.ACLRole{
|
|
|
|
ID: "node-identity",
|
|
|
|
Name: "node-identity",
|
|
|
|
Description: "node-identity",
|
|
|
|
NodeIdentities: []*structs.ACLNodeIdentity{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-06-16 16:54:27 +00:00
|
|
|
NodeName: "test-node",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-06-16 16:54:27 +00:00
|
|
|
NodeName: "test-node-dc2",
|
|
|
|
Datacenter: "dc2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, nil
|
2019-04-15 20:43:19 +00:00
|
|
|
default:
|
2021-09-16 14:17:02 +00:00
|
|
|
return true, nil, acl.ErrNotFound
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// ACLResolverTestDelegate is used to test
|
|
|
|
// the ACLResolver without running Agents
|
|
|
|
type ACLResolverTestDelegate struct {
|
2020-07-03 20:52:08 +00:00
|
|
|
// enabled is no longer part of the delegate. It is still here as a field on
|
|
|
|
// the fake delegate because many tests use this field to enable ACLs. This field
|
|
|
|
// is now used to set ACLResolverConfig.Config.ACLsEnabled.
|
2018-10-19 16:04:07 +00:00
|
|
|
enabled bool
|
|
|
|
datacenter string
|
|
|
|
legacy bool
|
|
|
|
localTokens bool
|
|
|
|
localPolicies bool
|
2019-04-15 20:43:19 +00:00
|
|
|
localRoles bool
|
2018-10-31 20:00:46 +00:00
|
|
|
tokenReadFn func(*structs.ACLTokenGetRequest, *structs.ACLTokenResponse) error
|
|
|
|
policyResolveFn func(*structs.ACLPolicyBatchGetRequest, *structs.ACLPolicyBatchResponse) error
|
2019-04-15 20:43:19 +00:00
|
|
|
roleResolveFn func(*structs.ACLRoleBatchGetRequest, *structs.ACLRoleBatchResponse) error
|
|
|
|
|
2021-09-16 14:17:02 +00:00
|
|
|
// testTokens is used by plainTokenReadFn if not nil
|
|
|
|
testTokens map[string]*structs.ACLToken
|
|
|
|
// testPolicies is used by plainPolicyResolveFn if not nil
|
|
|
|
testPolicies map[string]*structs.ACLPolicy
|
|
|
|
// testRoles is used by plainRoleResolveFn if not nil
|
|
|
|
testRoles map[string]*structs.ACLRole
|
|
|
|
|
2020-05-13 17:00:08 +00:00
|
|
|
localTokenResolutions int32
|
|
|
|
remoteTokenResolutions int32
|
|
|
|
localPolicyResolutions int32
|
|
|
|
remotePolicyResolutions int32
|
|
|
|
localRoleResolutions int32
|
|
|
|
remoteRoleResolutions int32
|
|
|
|
remoteLegacyResolutions int32
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
// state for the optional default resolver function defaultTokenReadFn
|
|
|
|
tokenCached bool
|
|
|
|
// state for the optional default resolver function defaultPolicyResolveFn
|
|
|
|
policyCached bool
|
|
|
|
// state for the optional default resolver function defaultRoleResolveFn
|
|
|
|
roleCached bool
|
2019-10-25 15:06:16 +00:00
|
|
|
|
|
|
|
EnterpriseACLResolverTestDelegate
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
2021-09-16 14:17:02 +00:00
|
|
|
// UseTestLocalData will force delegate-local maps to be used in lieu of the
|
|
|
|
// global factory functions.
|
|
|
|
func (d *ACLResolverTestDelegate) UseTestLocalData(data []interface{}) {
|
|
|
|
d.testTokens = make(map[string]*structs.ACLToken)
|
|
|
|
d.testPolicies = make(map[string]*structs.ACLPolicy)
|
|
|
|
d.testRoles = make(map[string]*structs.ACLRole)
|
|
|
|
|
|
|
|
var rest []interface{}
|
|
|
|
for _, item := range data {
|
|
|
|
switch x := item.(type) {
|
|
|
|
case *structs.ACLToken:
|
|
|
|
d.testTokens[x.SecretID] = x
|
|
|
|
case *structs.ACLPolicy:
|
|
|
|
d.testPolicies[x.ID] = x
|
|
|
|
case *structs.ACLRole:
|
|
|
|
d.testRoles[x.ID] = x
|
|
|
|
case string:
|
|
|
|
parts := strings.SplitN(x, ":", 2)
|
|
|
|
switch parts[0] {
|
|
|
|
case "token-not-found":
|
|
|
|
d.testTokens[parts[1]] = nil
|
|
|
|
case "policy-not-found":
|
|
|
|
d.testPolicies[parts[1]] = nil
|
|
|
|
case "role-not-found":
|
|
|
|
d.testRoles[parts[1]] = nil
|
|
|
|
default:
|
|
|
|
rest = append(rest, item)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
rest = append(rest, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
d.EnterpriseACLResolverTestDelegate.UseTestLocalData(rest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UseDefaultData will force the global factory functions to be used instead of
|
|
|
|
// delegate-local maps.
|
|
|
|
func (d *ACLResolverTestDelegate) UseDefaultData() {
|
|
|
|
d.testTokens = nil
|
|
|
|
d.testPolicies = nil
|
|
|
|
d.testRoles = nil
|
|
|
|
d.EnterpriseACLResolverTestDelegate.UseDefaultData()
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
func (d *ACLResolverTestDelegate) Reset() {
|
|
|
|
d.tokenCached = false
|
|
|
|
d.policyCached = false
|
|
|
|
d.roleCached = false
|
2021-09-16 20:53:10 +00:00
|
|
|
d.EnterpriseACLResolverTestDelegate.Reset()
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
var errRPC = fmt.Errorf("Induced RPC Error")
|
|
|
|
|
|
|
|
func (d *ACLResolverTestDelegate) defaultTokenReadFn(errAfterCached error) func(*structs.ACLTokenGetRequest, *structs.ACLTokenResponse) error {
|
|
|
|
return func(args *structs.ACLTokenGetRequest, reply *structs.ACLTokenResponse) error {
|
|
|
|
if !d.tokenCached {
|
2019-04-26 17:49:28 +00:00
|
|
|
err := d.plainTokenReadFn(args, reply)
|
2019-04-15 20:43:19 +00:00
|
|
|
d.tokenCached = true
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
return errAfterCached
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
func (d *ACLResolverTestDelegate) plainTokenReadFn(args *structs.ACLTokenGetRequest, reply *structs.ACLTokenResponse) error {
|
2021-09-16 14:17:02 +00:00
|
|
|
if d.testTokens != nil {
|
|
|
|
token, ok := d.testTokens[args.TokenID]
|
|
|
|
if ok {
|
|
|
|
if token == nil {
|
|
|
|
return acl.ErrNotFound
|
|
|
|
}
|
|
|
|
reply.Token = token
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
_, token, err := testIdentityForToken(args.TokenID)
|
|
|
|
if token != nil {
|
|
|
|
reply.Token = token.(*structs.ACLToken)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func (d *ACLResolverTestDelegate) defaultPolicyResolveFn(errAfterCached error) func(*structs.ACLPolicyBatchGetRequest, *structs.ACLPolicyBatchResponse) error {
|
|
|
|
return func(args *structs.ACLPolicyBatchGetRequest, reply *structs.ACLPolicyBatchResponse) error {
|
|
|
|
if !d.policyCached {
|
2019-04-26 17:49:28 +00:00
|
|
|
err := d.plainPolicyResolveFn(args, reply)
|
2019-04-15 20:43:19 +00:00
|
|
|
d.policyCached = true
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return errAfterCached
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
func (d *ACLResolverTestDelegate) plainPolicyResolveFn(args *structs.ACLPolicyBatchGetRequest, reply *structs.ACLPolicyBatchResponse) error {
|
|
|
|
// TODO: if we were being super correct about it, we'd verify the token first
|
|
|
|
// TODO: and possibly return a not-found or permission-denied here
|
|
|
|
|
|
|
|
for _, policyID := range args.PolicyIDs {
|
2021-09-16 14:17:02 +00:00
|
|
|
if d.testPolicies != nil {
|
|
|
|
if policy := d.testPolicies[policyID]; policy != nil {
|
|
|
|
reply.Policies = append(reply.Policies, policy)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, policy, _ := testPolicyForID(policyID)
|
|
|
|
if policy != nil {
|
|
|
|
reply.Policies = append(reply.Policies, policy)
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func (d *ACLResolverTestDelegate) defaultRoleResolveFn(errAfterCached error) func(*structs.ACLRoleBatchGetRequest, *structs.ACLRoleBatchResponse) error {
|
|
|
|
return func(args *structs.ACLRoleBatchGetRequest, reply *structs.ACLRoleBatchResponse) error {
|
|
|
|
if !d.roleCached {
|
2019-04-26 17:49:28 +00:00
|
|
|
err := d.plainRoleResolveFn(args, reply)
|
2019-04-15 20:43:19 +00:00
|
|
|
d.roleCached = true
|
2019-04-26 17:49:28 +00:00
|
|
|
return err
|
2019-04-15 20:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return errAfterCached
|
|
|
|
}
|
2014-08-12 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
// plainRoleResolveFn tries to follow the normal logic of ACL.RoleResolve using
|
|
|
|
// the test fixtures.
|
|
|
|
func (d *ACLResolverTestDelegate) plainRoleResolveFn(args *structs.ACLRoleBatchGetRequest, reply *structs.ACLRoleBatchResponse) error {
|
|
|
|
// TODO: if we were being super correct about it, we'd verify the token first
|
|
|
|
// TODO: and possibly return a not-found or permission-denied here
|
|
|
|
|
|
|
|
for _, roleID := range args.RoleIDs {
|
2021-09-16 14:17:02 +00:00
|
|
|
if d.testRoles != nil {
|
|
|
|
if role := d.testRoles[roleID]; role != nil {
|
|
|
|
reply.Roles = append(reply.Roles, role)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, role, _ := testRoleForID(roleID)
|
|
|
|
if role != nil {
|
|
|
|
reply.Roles = append(reply.Roles, role)
|
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-04 22:54:49 +00:00
|
|
|
func (d *ACLResolverTestDelegate) ACLDatacenter() string {
|
2018-10-19 16:04:07 +00:00
|
|
|
return d.datacenter
|
|
|
|
}
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (d *ACLResolverTestDelegate) UseLegacyACLs() bool {
|
|
|
|
return d.legacy
|
2014-08-11 21:01:45 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (d *ACLResolverTestDelegate) ResolveIdentityFromToken(token string) (bool, structs.ACLIdentity, error) {
|
|
|
|
if !d.localTokens {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2020-05-13 17:00:08 +00:00
|
|
|
atomic.AddInt32(&d.localTokenResolutions, 1)
|
2021-09-16 14:17:02 +00:00
|
|
|
if d.testTokens != nil {
|
|
|
|
if token, ok := d.testTokens[token]; ok {
|
|
|
|
if token != nil {
|
|
|
|
return true, token, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil, acl.ErrNotFound
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
return testIdentityForToken(token)
|
|
|
|
}
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (d *ACLResolverTestDelegate) ResolvePolicyFromID(policyID string) (bool, *structs.ACLPolicy, error) {
|
|
|
|
if !d.localPolicies {
|
|
|
|
return false, nil, nil
|
2014-08-11 21:01:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-13 17:00:08 +00:00
|
|
|
atomic.AddInt32(&d.localPolicyResolutions, 1)
|
2021-09-16 14:17:02 +00:00
|
|
|
if d.testPolicies != nil {
|
|
|
|
if policy, ok := d.testPolicies[policyID]; ok {
|
|
|
|
if policy != nil {
|
|
|
|
return true, policy, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil, acl.ErrNotFound
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
return testPolicyForID(policyID)
|
|
|
|
}
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func (d *ACLResolverTestDelegate) ResolveRoleFromID(roleID string) (bool, *structs.ACLRole, error) {
|
|
|
|
if !d.localRoles {
|
|
|
|
return false, nil, nil
|
|
|
|
}
|
|
|
|
|
2020-05-13 17:00:08 +00:00
|
|
|
atomic.AddInt32(&d.localRoleResolutions, 1)
|
2021-09-16 14:17:02 +00:00
|
|
|
if d.testRoles != nil {
|
|
|
|
if role, ok := d.testRoles[roleID]; ok {
|
|
|
|
if role != nil {
|
|
|
|
return true, role, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil, acl.ErrNotFound
|
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
return testRoleForID(roleID)
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func (d *ACLResolverTestDelegate) RPC(method string, args interface{}, reply interface{}) error {
|
|
|
|
switch method {
|
|
|
|
case "ACL.TokenRead":
|
2020-05-13 17:00:08 +00:00
|
|
|
atomic.AddInt32(&d.remoteTokenResolutions, 1)
|
2018-10-19 16:04:07 +00:00
|
|
|
if d.tokenReadFn != nil {
|
2018-10-31 20:00:46 +00:00
|
|
|
return d.tokenReadFn(args.(*structs.ACLTokenGetRequest), reply.(*structs.ACLTokenResponse))
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
panic("Bad Test Implementation: should provide a tokenReadFn to the ACLResolverTestDelegate")
|
2018-10-19 16:04:07 +00:00
|
|
|
case "ACL.PolicyResolve":
|
2020-05-13 17:00:08 +00:00
|
|
|
atomic.AddInt32(&d.remotePolicyResolutions, 1)
|
2018-10-19 16:04:07 +00:00
|
|
|
if d.policyResolveFn != nil {
|
2018-10-31 20:00:46 +00:00
|
|
|
return d.policyResolveFn(args.(*structs.ACLPolicyBatchGetRequest), reply.(*structs.ACLPolicyBatchResponse))
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
panic("Bad Test Implementation: should provide a policyResolveFn to the ACLResolverTestDelegate")
|
|
|
|
case "ACL.RoleResolve":
|
2020-05-13 17:00:08 +00:00
|
|
|
atomic.AddInt32(&d.remoteRoleResolutions, 1)
|
2019-04-15 20:43:19 +00:00
|
|
|
if d.roleResolveFn != nil {
|
|
|
|
return d.roleResolveFn(args.(*structs.ACLRoleBatchGetRequest), reply.(*structs.ACLRoleBatchResponse))
|
|
|
|
}
|
|
|
|
panic("Bad Test Implementation: should provide a roleResolveFn to the ACLResolverTestDelegate")
|
2014-08-11 21:01:45 +00:00
|
|
|
}
|
2019-10-25 15:06:16 +00:00
|
|
|
if handled, err := d.EnterpriseACLResolverTestDelegate.RPC(method, args, reply); handled {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
panic("Bad Test Implementation: Was the ACLResolver updated to use new RPC methods")
|
2014-08-11 21:01:45 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 20:52:08 +00:00
|
|
|
func newTestACLResolver(t *testing.T, delegate *ACLResolverTestDelegate, cb func(*ACLResolverConfig)) *ACLResolver {
|
2018-10-19 16:04:07 +00:00
|
|
|
config := DefaultConfig()
|
2021-08-06 22:39:39 +00:00
|
|
|
config.ACLResolverSettings.ACLDefaultPolicy = "deny"
|
|
|
|
config.ACLResolverSettings.ACLDownPolicy = "extend-cache"
|
|
|
|
config.ACLResolverSettings.ACLsEnabled = delegate.enabled
|
2018-10-19 16:04:07 +00:00
|
|
|
rconf := &ACLResolverConfig{
|
2021-08-06 22:39:39 +00:00
|
|
|
Config: config.ACLResolverSettings,
|
2020-05-06 20:40:16 +00:00
|
|
|
Logger: testutil.Logger(t),
|
2018-10-19 16:04:07 +00:00
|
|
|
CacheConfig: &structs.ACLCachesConfig{
|
|
|
|
Identities: 4,
|
|
|
|
Policies: 4,
|
|
|
|
ParsedPolicies: 4,
|
|
|
|
Authorizers: 4,
|
2019-04-15 20:43:19 +00:00
|
|
|
Roles: 4,
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
2021-08-09 20:29:21 +00:00
|
|
|
DisableDuration: aclClientDisabledTTL,
|
2021-07-30 23:20:02 +00:00
|
|
|
Backend: delegate,
|
2014-08-11 21:54:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
if cb != nil {
|
|
|
|
cb(rconf)
|
2014-08-11 21:54:18 +00:00
|
|
|
}
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
resolver, err := NewACLResolver(rconf)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return resolver
|
2014-08-11 21:54:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func TestACLResolver_Disabled(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2014-08-11 21:54:18 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: false,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
2014-08-11 21:54:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
r := newTestACLResolver(t, delegate, nil)
|
|
|
|
|
|
|
|
authz, err := r.ResolveToken("does not exist")
|
2022-06-17 09:24:43 +00:00
|
|
|
require.Equal(t, resolver.Result{Authorizer: acl.ManageAll()}, authz)
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Nil(t, err)
|
2014-08-11 21:54:18 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func TestACLResolver_ResolveRootACL(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2018-10-19 16:04:07 +00:00
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
}
|
|
|
|
r := newTestACLResolver(t, delegate, nil)
|
|
|
|
|
|
|
|
t.Run("Allow", func(t *testing.T) {
|
2022-01-23 17:31:48 +00:00
|
|
|
_, err := r.ResolveToken("allow")
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, acl.IsErrRootDenied(err))
|
2014-08-12 17:38:57 +00:00
|
|
|
})
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Run("Deny", func(t *testing.T) {
|
2022-01-23 17:31:48 +00:00
|
|
|
_, err := r.ResolveToken("deny")
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, acl.IsErrRootDenied(err))
|
|
|
|
})
|
2014-08-12 17:38:57 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Run("Manage", func(t *testing.T) {
|
2022-01-23 17:31:48 +00:00
|
|
|
_, err := r.ResolveToken("manage")
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, acl.IsErrRootDenied(err))
|
|
|
|
})
|
2014-08-12 17:38:57 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func TestACLResolver_DownPolicy(t *testing.T) {
|
2022-05-04 16:38:45 +00:00
|
|
|
requireIdentityCached := func(t *testing.T, r *ACLResolver, secretID string, present bool, msg string) {
|
2019-03-12 15:23:43 +00:00
|
|
|
t.Helper()
|
|
|
|
|
2022-05-04 16:38:45 +00:00
|
|
|
cacheVal := r.cache.GetIdentityWithSecretToken(secretID)
|
2019-03-12 15:23:43 +00:00
|
|
|
if present {
|
2022-05-04 16:38:45 +00:00
|
|
|
require.NotNil(t, cacheVal, msg)
|
2019-03-12 15:23:43 +00:00
|
|
|
require.NotNil(t, cacheVal.Identity, msg)
|
|
|
|
} else {
|
2022-05-04 16:38:45 +00:00
|
|
|
require.Nil(t, cacheVal, msg)
|
2019-03-12 15:23:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
requirePolicyCached := func(t *testing.T, r *ACLResolver, policyID string, present bool, msg string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
cacheVal := r.cache.GetPolicy(policyID)
|
|
|
|
require.NotNil(t, cacheVal)
|
|
|
|
if present {
|
|
|
|
require.NotNil(t, cacheVal.Policy, msg)
|
|
|
|
} else {
|
|
|
|
require.Nil(t, cacheVal.Policy, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Run("Deny", func(t *testing.T) {
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: false,
|
|
|
|
localPolicies: true,
|
2019-04-15 20:43:19 +00:00
|
|
|
localRoles: true,
|
2018-10-31 20:00:46 +00:00
|
|
|
tokenReadFn: func(*structs.ACLTokenGetRequest, *structs.ACLTokenResponse) error {
|
2019-04-15 20:43:19 +00:00
|
|
|
return errRPC
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "deny"
|
|
|
|
})
|
|
|
|
|
|
|
|
authz, err := r.ResolveToken("foo")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2022-06-17 09:24:43 +00:00
|
|
|
expected := resolver.Result{
|
2022-01-23 17:31:48 +00:00
|
|
|
Authorizer: acl.DenyAll(),
|
|
|
|
ACLIdentity: &missingIdentity{reason: "primary-dc-down", token: "foo"},
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, authz)
|
2019-03-12 15:23:43 +00:00
|
|
|
|
2022-05-04 16:38:45 +00:00
|
|
|
requireIdentityCached(t, r, "foo", false, "not present")
|
2014-08-11 21:01:45 +00:00
|
|
|
})
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Run("Allow", func(t *testing.T) {
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: false,
|
|
|
|
localPolicies: true,
|
2019-04-15 20:43:19 +00:00
|
|
|
localRoles: true,
|
2018-10-31 20:00:46 +00:00
|
|
|
tokenReadFn: func(*structs.ACLTokenGetRequest, *structs.ACLTokenResponse) error {
|
2019-04-15 20:43:19 +00:00
|
|
|
return errRPC
|
2018-10-19 16:04:07 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "allow"
|
|
|
|
})
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
authz, err := r.ResolveToken("foo")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2022-06-17 09:24:43 +00:00
|
|
|
expected := resolver.Result{
|
2022-01-23 17:31:48 +00:00
|
|
|
Authorizer: acl.AllowAll(),
|
|
|
|
ACLIdentity: &missingIdentity{reason: "primary-dc-down", token: "foo"},
|
|
|
|
}
|
|
|
|
require.Equal(t, expected, authz)
|
2019-03-12 15:23:43 +00:00
|
|
|
|
2022-05-04 16:38:45 +00:00
|
|
|
requireIdentityCached(t, r, "foo", false, "not present")
|
2018-10-19 16:04:07 +00:00
|
|
|
})
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Run("Expired-Policy", func(t *testing.T) {
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: true,
|
|
|
|
localPolicies: false,
|
2019-04-15 20:43:19 +00:00
|
|
|
localRoles: false,
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
delegate.policyResolveFn = delegate.defaultPolicyResolveFn(errRPC)
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "deny"
|
|
|
|
config.Config.ACLPolicyTTL = 0
|
2019-04-15 20:43:19 +00:00
|
|
|
config.Config.ACLRoleTTL = 0
|
2018-10-19 16:04:07 +00:00
|
|
|
})
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
authz, err := r.ResolveToken("found")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-03-12 15:23:43 +00:00
|
|
|
requirePolicyCached(t, r, "node-wr", true, "cached") // from "found" token
|
|
|
|
requirePolicyCached(t, r, "dc2-key-wr", true, "cached") // from "found" token
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// policy cache expired - so we will fail to resolve that policy and use the default policy only
|
|
|
|
authz2, err := r.ResolveToken("found")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz2)
|
2019-10-25 15:06:16 +00:00
|
|
|
require.NotEqual(t, authz, authz2)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Deny, authz2.NodeWrite("foo", nil))
|
2019-03-12 15:23:43 +00:00
|
|
|
|
|
|
|
requirePolicyCached(t, r, "node-wr", false, "expired") // from "found" token
|
|
|
|
requirePolicyCached(t, r, "dc2-key-wr", false, "expired") // from "found" token
|
2018-10-19 16:04:07 +00:00
|
|
|
})
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
t.Run("Expired-Role", func(t *testing.T) {
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: true,
|
|
|
|
localPolicies: false,
|
|
|
|
localRoles: false,
|
|
|
|
}
|
|
|
|
delegate.policyResolveFn = delegate.defaultPolicyResolveFn(errRPC)
|
|
|
|
delegate.roleResolveFn = delegate.defaultRoleResolveFn(errRPC)
|
|
|
|
|
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "deny"
|
|
|
|
config.Config.ACLPolicyTTL = 0
|
|
|
|
config.Config.ACLRoleTTL = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
authz, err := r.ResolveToken("found-role")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
// role cache expired - so we will fail to resolve that role and use the default policy only
|
|
|
|
authz2, err := r.ResolveToken("found-role")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz2)
|
|
|
|
require.False(t, authz == authz2)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Deny, authz2.NodeWrite("foo", nil))
|
2019-04-15 20:43:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Extend-Cache-Policy", func(t *testing.T) {
|
2018-10-19 16:04:07 +00:00
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: false,
|
|
|
|
localPolicies: true,
|
2019-04-15 20:43:19 +00:00
|
|
|
localRoles: true,
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
delegate.tokenReadFn = delegate.defaultTokenReadFn(errRPC)
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "extend-cache"
|
|
|
|
config.Config.ACLTokenTTL = 0
|
|
|
|
})
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
authz, err := r.ResolveToken("found")
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2022-05-04 16:38:45 +00:00
|
|
|
requireIdentityCached(t, r, "found", true, "cached")
|
2019-03-12 15:23:43 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
authz2, err := r.ResolveToken("found")
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz2)
|
2019-10-25 15:06:16 +00:00
|
|
|
verifyAuthorizerChain(t, authz, authz2)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz2.NodeWrite("foo", nil))
|
2019-04-15 20:43:19 +00:00
|
|
|
})
|
|
|
|
|
2021-09-23 22:11:16 +00:00
|
|
|
t.Run("Extend-Cache with no cache entry defaults to default_policy", func(t *testing.T) {
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
localPolicies: true,
|
|
|
|
localRoles: true,
|
|
|
|
}
|
|
|
|
delegate.tokenReadFn = func(*structs.ACLTokenGetRequest, *structs.ACLTokenResponse) error {
|
|
|
|
return ACLRemoteError{Err: fmt.Errorf("connection problem")}
|
|
|
|
}
|
|
|
|
|
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "extend-cache"
|
|
|
|
})
|
|
|
|
|
2022-01-23 17:31:48 +00:00
|
|
|
authz, err := r.ResolveToken("not-found")
|
2021-09-23 22:11:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("foo", nil))
|
|
|
|
})
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
t.Run("Extend-Cache-Role", func(t *testing.T) {
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: false,
|
|
|
|
localPolicies: true,
|
|
|
|
localRoles: true,
|
|
|
|
}
|
|
|
|
delegate.tokenReadFn = delegate.defaultTokenReadFn(errRPC)
|
|
|
|
|
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "extend-cache"
|
|
|
|
config.Config.ACLTokenTTL = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
authz, err := r.ResolveToken("found-role")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2019-03-12 15:23:43 +00:00
|
|
|
|
2022-05-04 16:38:45 +00:00
|
|
|
requireIdentityCached(t, r, "found-role", true, "still cached")
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
authz2, err := r.ResolveToken("found-role")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz2)
|
|
|
|
// testing pointer equality - these will be the same object because it is cached.
|
2019-10-25 15:06:16 +00:00
|
|
|
verifyAuthorizerChain(t, authz, authz2)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz2.NodeWrite("foo", nil))
|
2014-08-11 21:01:45 +00:00
|
|
|
})
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Run("Extend-Cache-Expired-Policy", func(t *testing.T) {
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: true,
|
|
|
|
localPolicies: false,
|
2019-04-15 20:43:19 +00:00
|
|
|
localRoles: false,
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
delegate.policyResolveFn = delegate.defaultPolicyResolveFn(errRPC)
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "extend-cache"
|
|
|
|
config.Config.ACLPolicyTTL = 0
|
2019-04-15 20:43:19 +00:00
|
|
|
config.Config.ACLRoleTTL = 0
|
2018-10-19 16:04:07 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
authz, err := r.ResolveToken("found")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-03-12 15:23:43 +00:00
|
|
|
requirePolicyCached(t, r, "node-wr", true, "cached") // from "found" token
|
|
|
|
requirePolicyCached(t, r, "dc2-key-wr", true, "cached") // from "found" token
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// Will just use the policy cache
|
|
|
|
authz2, err := r.ResolveToken("found")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz2)
|
2019-10-25 15:06:16 +00:00
|
|
|
verifyAuthorizerChain(t, authz, authz2)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2019-03-12 15:23:43 +00:00
|
|
|
|
|
|
|
requirePolicyCached(t, r, "node-wr", true, "still cached") // from "found" token
|
|
|
|
requirePolicyCached(t, r, "dc2-key-wr", true, "still cached") // from "found" token
|
2014-08-11 21:01:45 +00:00
|
|
|
})
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
t.Run("Extend-Cache-Expired-Role", func(t *testing.T) {
|
2018-10-19 16:04:07 +00:00
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: true,
|
|
|
|
localPolicies: false,
|
2019-04-15 20:43:19 +00:00
|
|
|
localRoles: false,
|
|
|
|
}
|
|
|
|
delegate.policyResolveFn = delegate.defaultPolicyResolveFn(errRPC)
|
|
|
|
delegate.roleResolveFn = delegate.defaultRoleResolveFn(errRPC)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "extend-cache"
|
|
|
|
config.Config.ACLPolicyTTL = 0
|
|
|
|
config.Config.ACLRoleTTL = 0
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
authz, err := r.ResolveToken("found-role")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
// Will just use the policy cache
|
|
|
|
authz2, err := r.ResolveToken("found-role")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz2)
|
2019-10-25 15:06:16 +00:00
|
|
|
verifyAuthorizerChain(t, authz, authz2)
|
|
|
|
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2019-04-15 20:43:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Async-Cache-Expired-Policy", func(t *testing.T) {
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: true,
|
|
|
|
localPolicies: false,
|
|
|
|
localRoles: false,
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
// We don't need to return acl.ErrNotFound here but we could. The ACLResolver will search for any
|
|
|
|
// policies not in the response and emit an ACL not found for any not-found within the result set.
|
|
|
|
delegate.policyResolveFn = delegate.defaultPolicyResolveFn(nil)
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "async-cache"
|
|
|
|
config.Config.ACLPolicyTTL = 0
|
2019-04-15 20:43:19 +00:00
|
|
|
config.Config.ACLRoleTTL = 0
|
2018-10-19 16:04:07 +00:00
|
|
|
})
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
authz, err := r.ResolveToken("found")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-03-12 15:23:43 +00:00
|
|
|
requirePolicyCached(t, r, "node-wr", true, "cached") // from "found" token
|
|
|
|
requirePolicyCached(t, r, "dc2-key-wr", true, "cached") // from "found" token
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// The identity should have been cached so this should still be valid
|
|
|
|
authz2, err := r.ResolveToken("found")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz2)
|
|
|
|
// testing pointer equality - these will be the same object because it is cached.
|
2019-10-25 15:06:16 +00:00
|
|
|
verifyAuthorizerChain(t, authz, authz2)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
// the go routine spawned will eventually return with a authz that doesn't have the policy
|
|
|
|
retry.Run(t, func(t *retry.R) {
|
|
|
|
authz3, err := r.ResolveToken("found")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, authz3)
|
2019-10-15 20:58:50 +00:00
|
|
|
assert.Equal(t, acl.Deny, authz3.NodeWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
})
|
2019-03-12 15:23:43 +00:00
|
|
|
|
|
|
|
requirePolicyCached(t, r, "node-wr", false, "no longer cached") // from "found" token
|
|
|
|
requirePolicyCached(t, r, "dc2-key-wr", false, "no longer cached") // from "found" token
|
2018-10-19 16:04:07 +00:00
|
|
|
})
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
t.Run("Async-Cache-Expired-Role", func(t *testing.T) {
|
2018-10-19 16:04:07 +00:00
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
2019-04-15 20:43:19 +00:00
|
|
|
localTokens: true,
|
2018-10-19 16:04:07 +00:00
|
|
|
localPolicies: false,
|
2019-04-15 20:43:19 +00:00
|
|
|
localRoles: false,
|
|
|
|
}
|
|
|
|
// We don't need to return acl.ErrNotFound here but we could. The ACLResolver will search for any
|
|
|
|
// policies not in the response and emit an ACL not found for any not-found within the result set.
|
|
|
|
delegate.policyResolveFn = delegate.defaultPolicyResolveFn(nil)
|
|
|
|
delegate.roleResolveFn = delegate.defaultRoleResolveFn(nil)
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "async-cache"
|
|
|
|
config.Config.ACLPolicyTTL = 0
|
|
|
|
config.Config.ACLRoleTTL = 0
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
authz, err := r.ResolveToken("found-role")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
// The identity should have been cached so this should still be valid
|
|
|
|
authz2, err := r.ResolveToken("found-role")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz2)
|
|
|
|
// testing pointer equality - these will be the same object because it is cached.
|
2019-10-25 15:06:16 +00:00
|
|
|
verifyAuthorizerChain(t, authz, authz2)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
// the go routine spawned will eventually return with a authz that doesn't have the policy
|
|
|
|
retry.Run(t, func(t *retry.R) {
|
|
|
|
authz3, err := r.ResolveToken("found-role")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, authz3)
|
2019-10-15 20:58:50 +00:00
|
|
|
assert.Equal(t, acl.Deny, authz3.NodeWrite("foo", nil))
|
2019-04-15 20:43:19 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Extend-Cache-Client-Policy", func(t *testing.T) {
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: false,
|
|
|
|
localPolicies: false,
|
|
|
|
localRoles: false,
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
delegate.tokenReadFn = delegate.defaultTokenReadFn(errRPC)
|
|
|
|
delegate.policyResolveFn = delegate.defaultPolicyResolveFn(errRPC)
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "extend-cache"
|
|
|
|
config.Config.ACLTokenTTL = 0
|
|
|
|
config.Config.ACLPolicyTTL = 0
|
2019-04-15 20:43:19 +00:00
|
|
|
config.Config.ACLRoleTTL = 0
|
2018-10-19 16:04:07 +00:00
|
|
|
})
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
authz, err := r.ResolveToken("found")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2019-03-12 15:23:43 +00:00
|
|
|
requirePolicyCached(t, r, "node-wr", true, "cached") // from "found" token
|
|
|
|
requirePolicyCached(t, r, "dc2-key-wr", true, "cached") // from "found" token
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
authz2, err := r.ResolveToken("found")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz2)
|
|
|
|
// testing pointer equality - these will be the same object because it is cached.
|
2019-10-25 15:06:16 +00:00
|
|
|
verifyAuthorizerChain(t, authz, authz2)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz2.NodeWrite("foo", nil))
|
2019-04-15 20:43:19 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Extend-Cache-Client-Role", func(t *testing.T) {
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: false,
|
|
|
|
localPolicies: false,
|
|
|
|
localRoles: false,
|
|
|
|
}
|
|
|
|
delegate.tokenReadFn = delegate.defaultTokenReadFn(errRPC)
|
|
|
|
delegate.policyResolveFn = delegate.defaultPolicyResolveFn(errRPC)
|
|
|
|
delegate.roleResolveFn = delegate.defaultRoleResolveFn(errRPC)
|
|
|
|
|
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "extend-cache"
|
|
|
|
config.Config.ACLTokenTTL = 0
|
|
|
|
config.Config.ACLPolicyTTL = 0
|
|
|
|
config.Config.ACLRoleTTL = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
authz, err := r.ResolveToken("found-role")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2019-03-12 15:23:43 +00:00
|
|
|
|
|
|
|
requirePolicyCached(t, r, "node-wr", true, "still cached") // from "found" token
|
|
|
|
requirePolicyCached(t, r, "dc2-key-wr", true, "still cached") // from "found" token
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
authz2, err := r.ResolveToken("found-role")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz2)
|
|
|
|
// testing pointer equality - these will be the same object because it is cached.
|
2019-10-25 15:06:16 +00:00
|
|
|
verifyAuthorizerChain(t, authz, authz2)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz2.NodeWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
})
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Run("Async-Cache", func(t *testing.T) {
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: false,
|
|
|
|
localPolicies: true,
|
2019-04-15 20:43:19 +00:00
|
|
|
localRoles: true,
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-15 20:43:19 +00:00
|
|
|
delegate.tokenReadFn = delegate.defaultTokenReadFn(acl.ErrNotFound)
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "async-cache"
|
|
|
|
config.Config.ACLTokenTTL = 0
|
|
|
|
})
|
2014-08-11 21:01:45 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
authz, err := r.ResolveToken("found")
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
|
2022-05-04 16:38:45 +00:00
|
|
|
requireIdentityCached(t, r, "found", true, "cached")
|
2019-03-12 15:23:43 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
// The identity should have been cached so this should still be valid
|
2019-04-15 20:43:19 +00:00
|
|
|
authz2, err := r.ResolveToken("found")
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz2)
|
2019-10-25 15:06:16 +00:00
|
|
|
verifyAuthorizerChain(t, authz, authz2)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz2.NodeWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
|
|
|
|
// the go routine spawned will eventually return and this will be a not found error
|
|
|
|
retry.Run(t, func(t *retry.R) {
|
2022-01-23 17:31:48 +00:00
|
|
|
_, err := r.ResolveToken("found")
|
2018-10-19 16:04:07 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.True(t, acl.IsErrNotFound(err))
|
|
|
|
})
|
2019-03-12 15:23:43 +00:00
|
|
|
|
2022-05-04 16:38:45 +00:00
|
|
|
requireIdentityCached(t, r, "found", false, "no longer cached")
|
2022-04-28 16:08:55 +00:00
|
|
|
})
|
|
|
|
|
2019-03-14 14:35:34 +00:00
|
|
|
t.Run("PolicyResolve-TokenNotFound", func(t *testing.T) {
|
|
|
|
_, rawToken, _ := testIdentityForToken("found")
|
|
|
|
foundToken := rawToken.(*structs.ACLToken)
|
|
|
|
secretID := foundToken.SecretID
|
|
|
|
|
|
|
|
tokenResolved := false
|
|
|
|
policyResolved := false
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: false,
|
|
|
|
localPolicies: false,
|
2020-11-05 16:18:59 +00:00
|
|
|
tokenReadFn: func(_ *structs.ACLTokenGetRequest, reply *structs.ACLTokenResponse) error {
|
2019-03-14 14:35:34 +00:00
|
|
|
if !tokenResolved {
|
|
|
|
reply.Token = foundToken
|
|
|
|
tokenResolved = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Not Supposed to be Invoked again")
|
|
|
|
},
|
|
|
|
policyResolveFn: func(args *structs.ACLPolicyBatchGetRequest, reply *structs.ACLPolicyBatchResponse) error {
|
|
|
|
if !policyResolved {
|
|
|
|
for _, policyID := range args.PolicyIDs {
|
|
|
|
_, policy, _ := testPolicyForID(policyID)
|
|
|
|
if policy != nil {
|
|
|
|
reply.Policies = append(reply.Policies, policy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
policyResolved = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return acl.ErrNotFound // test condition
|
|
|
|
},
|
|
|
|
}
|
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "extend-cache"
|
|
|
|
config.Config.ACLTokenTTL = 0
|
|
|
|
config.Config.ACLPolicyTTL = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
// Prime the standard caches.
|
|
|
|
authz, err := r.ResolveToken(secretID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2019-03-14 14:35:34 +00:00
|
|
|
|
|
|
|
// Verify that the caches are setup properly.
|
2022-05-04 16:38:45 +00:00
|
|
|
requireIdentityCached(t, r, secretID, true, "cached")
|
2019-03-14 14:35:34 +00:00
|
|
|
requirePolicyCached(t, r, "node-wr", true, "cached") // from "found" token
|
|
|
|
requirePolicyCached(t, r, "dc2-key-wr", true, "cached") // from "found" token
|
|
|
|
|
|
|
|
// Nuke 1 policy from the cache so that we force a policy resolve
|
|
|
|
// during token resolve.
|
|
|
|
r.cache.RemovePolicy("dc2-key-wr")
|
|
|
|
|
|
|
|
_, err = r.ResolveToken(secretID)
|
|
|
|
require.True(t, acl.IsErrNotFound(err))
|
|
|
|
|
2022-05-04 16:38:45 +00:00
|
|
|
requireIdentityCached(t, r, secretID, false, "identity not found cached")
|
2019-03-14 14:35:34 +00:00
|
|
|
requirePolicyCached(t, r, "node-wr", true, "still cached")
|
|
|
|
require.Nil(t, r.cache.GetPolicy("dc2-key-wr"), "not stored at all")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("PolicyResolve-PermissionDenied", func(t *testing.T) {
|
|
|
|
_, rawToken, _ := testIdentityForToken("found")
|
|
|
|
foundToken := rawToken.(*structs.ACLToken)
|
|
|
|
secretID := foundToken.SecretID
|
|
|
|
|
|
|
|
policyResolved := false
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: false,
|
|
|
|
localPolicies: false,
|
2020-11-05 16:18:59 +00:00
|
|
|
tokenReadFn: func(_ *structs.ACLTokenGetRequest, reply *structs.ACLTokenResponse) error {
|
2019-03-14 14:35:34 +00:00
|
|
|
// no limit
|
|
|
|
reply.Token = foundToken
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
policyResolveFn: func(args *structs.ACLPolicyBatchGetRequest, reply *structs.ACLPolicyBatchResponse) error {
|
|
|
|
if !policyResolved {
|
|
|
|
for _, policyID := range args.PolicyIDs {
|
|
|
|
_, policy, _ := testPolicyForID(policyID)
|
|
|
|
if policy != nil {
|
|
|
|
reply.Policies = append(reply.Policies, policy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
policyResolved = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return acl.ErrPermissionDenied // test condition
|
|
|
|
},
|
|
|
|
}
|
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLDownPolicy = "extend-cache"
|
|
|
|
config.Config.ACLTokenTTL = 0
|
|
|
|
config.Config.ACLPolicyTTL = 0
|
|
|
|
})
|
|
|
|
|
|
|
|
// Prime the standard caches.
|
|
|
|
authz, err := r.ResolveToken(secretID)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2019-03-14 14:35:34 +00:00
|
|
|
|
|
|
|
// Verify that the caches are setup properly.
|
2022-05-04 16:38:45 +00:00
|
|
|
requireIdentityCached(t, r, secretID, true, "cached")
|
2019-03-14 14:35:34 +00:00
|
|
|
requirePolicyCached(t, r, "node-wr", true, "cached") // from "found" token
|
|
|
|
requirePolicyCached(t, r, "dc2-key-wr", true, "cached") // from "found" token
|
|
|
|
|
|
|
|
// Nuke 1 policy from the cache so that we force a policy resolve
|
|
|
|
// during token resolve.
|
|
|
|
r.cache.RemovePolicy("dc2-key-wr")
|
|
|
|
|
|
|
|
_, err = r.ResolveToken(secretID)
|
|
|
|
require.True(t, acl.IsErrPermissionDenied(err))
|
|
|
|
|
2022-05-04 16:38:45 +00:00
|
|
|
require.Nil(t, r.cache.GetIdentityWithSecretToken(secretID), "identity not stored at all")
|
2019-03-14 14:35:34 +00:00
|
|
|
requirePolicyCached(t, r, "node-wr", true, "still cached")
|
|
|
|
require.Nil(t, r.cache.GetPolicy("dc2-key-wr"), "not stored at all")
|
|
|
|
})
|
2014-08-11 21:01:45 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
func TestACLResolver_DatacenterScoping(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Run("dc1", func(t *testing.T) {
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: true,
|
|
|
|
localPolicies: true,
|
2019-04-15 20:43:19 +00:00
|
|
|
localRoles: true,
|
2018-10-19 16:04:07 +00:00
|
|
|
// No need to provide any of the RPC callbacks
|
|
|
|
}
|
|
|
|
r := newTestACLResolver(t, delegate, nil)
|
|
|
|
|
|
|
|
authz, err := r.ResolveToken("found")
|
|
|
|
require.NotNil(t, authz)
|
|
|
|
require.NoError(t, err)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Deny, authz.ACLRead(nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.KeyWrite("foo", nil))
|
2014-08-12 17:54:56 +00:00
|
|
|
})
|
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
t.Run("dc2", func(t *testing.T) {
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc2",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: true,
|
|
|
|
localPolicies: true,
|
2019-04-15 20:43:19 +00:00
|
|
|
localRoles: true,
|
2018-10-19 16:04:07 +00:00
|
|
|
// No need to provide any of the RPC callbacks
|
|
|
|
}
|
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.Datacenter = "dc2"
|
|
|
|
})
|
2014-08-12 17:54:56 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
authz, err := r.ResolveToken("found")
|
|
|
|
require.NotNil(t, authz)
|
|
|
|
require.NoError(t, err)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Deny, authz.ACLRead(nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("foo", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.KeyWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
})
|
2014-08-12 17:54:56 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
// TODO(rb): replicate this sort of test but for roles
|
2019-01-22 18:14:43 +00:00
|
|
|
func TestACLResolver_Client(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2019-01-22 18:14:43 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
t.Run("Racey-Token-Mod-Policy-Resolve", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
var tokenReads int32
|
|
|
|
var policyResolves int32
|
|
|
|
modified := false
|
|
|
|
deleted := false
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: false,
|
|
|
|
localPolicies: false,
|
2020-11-05 16:18:59 +00:00
|
|
|
tokenReadFn: func(_ *structs.ACLTokenGetRequest, reply *structs.ACLTokenResponse) error {
|
2019-01-22 18:14:43 +00:00
|
|
|
atomic.AddInt32(&tokenReads, 1)
|
|
|
|
if deleted {
|
|
|
|
return acl.ErrNotFound
|
|
|
|
} else if modified {
|
|
|
|
_, token, _ := testIdentityForToken("racey-modified")
|
|
|
|
reply.Token = token.(*structs.ACLToken)
|
|
|
|
} else {
|
|
|
|
_, token, _ := testIdentityForToken("racey-unmodified")
|
|
|
|
reply.Token = token.(*structs.ACLToken)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
policyResolveFn: func(args *structs.ACLPolicyBatchGetRequest, reply *structs.ACLPolicyBatchResponse) error {
|
|
|
|
atomic.AddInt32(&policyResolves, 1)
|
|
|
|
if deleted {
|
|
|
|
return acl.ErrNotFound
|
|
|
|
} else if !modified {
|
|
|
|
modified = true
|
|
|
|
return acl.ErrPermissionDenied
|
|
|
|
} else {
|
|
|
|
deleted = true
|
|
|
|
for _, policyID := range args.PolicyIDs {
|
|
|
|
_, policy, _ := testPolicyForID(policyID)
|
|
|
|
if policy != nil {
|
|
|
|
reply.Policies = append(reply.Policies, policy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
modified = true
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLTokenTTL = 600 * time.Second
|
|
|
|
config.Config.ACLPolicyTTL = 30 * time.Millisecond
|
2019-04-15 20:43:19 +00:00
|
|
|
config.Config.ACLRoleTTL = 30 * time.Millisecond
|
2019-01-22 18:14:43 +00:00
|
|
|
config.Config.ACLDownPolicy = "extend-cache"
|
|
|
|
})
|
|
|
|
|
|
|
|
// resolves the token
|
|
|
|
// gets a permission denied resolving the policies - token updated
|
|
|
|
// invalidates the token
|
|
|
|
// refetches the token
|
|
|
|
// fetches the policies from the modified token
|
|
|
|
// creates the authorizers
|
|
|
|
//
|
|
|
|
// Must use the token secret here in order for the cached identity
|
|
|
|
// to be removed properly. Many other tests just resolve some other
|
|
|
|
// random name and it wont matter but this one cannot.
|
|
|
|
authz, err := r.ResolveToken("a1a54629-5050-4d17-8a4e-560d2423f835")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.ACLRead(nil))
|
2019-01-22 18:14:43 +00:00
|
|
|
require.True(t, modified)
|
|
|
|
require.True(t, deleted)
|
|
|
|
require.Equal(t, int32(2), tokenReads)
|
|
|
|
require.Equal(t, int32(2), policyResolves)
|
|
|
|
|
|
|
|
// sleep long enough for the policy cache to expire
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
|
|
|
// this round the identity will be resolved from the cache
|
|
|
|
// then the policy will be resolved but resolution will return ACL not found
|
|
|
|
// resolution will stop with the not found error (even though we still have the
|
|
|
|
// policies within the cache)
|
2022-05-23 14:26:45 +00:00
|
|
|
_, err = r.ResolveToken("a1a54629-5050-4d17-8a4e-560d2423f835")
|
2019-01-22 18:14:43 +00:00
|
|
|
require.EqualError(t, err, acl.ErrNotFound.Error())
|
|
|
|
|
|
|
|
require.True(t, modified)
|
|
|
|
require.True(t, deleted)
|
|
|
|
require.Equal(t, tokenReads, int32(2))
|
|
|
|
require.Equal(t, policyResolves, int32(3))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("Concurrent-Token-Resolve", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var tokenReads int32
|
|
|
|
var policyResolves int32
|
|
|
|
readyCh := make(chan struct{})
|
|
|
|
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: false,
|
|
|
|
localPolicies: false,
|
|
|
|
tokenReadFn: func(args *structs.ACLTokenGetRequest, reply *structs.ACLTokenResponse) error {
|
|
|
|
atomic.AddInt32(&tokenReads, 1)
|
|
|
|
|
|
|
|
switch args.TokenID {
|
|
|
|
case "a1a54629-5050-4d17-8a4e-560d2423f835":
|
2019-03-14 14:35:34 +00:00
|
|
|
_, token, _ := testIdentityForToken("concurrent-resolve")
|
2019-01-22 18:14:43 +00:00
|
|
|
reply.Token = token.(*structs.ACLToken)
|
|
|
|
default:
|
|
|
|
return acl.ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-readyCh:
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
policyResolveFn: func(args *structs.ACLPolicyBatchGetRequest, reply *structs.ACLPolicyBatchResponse) error {
|
|
|
|
atomic.AddInt32(&policyResolves, 1)
|
2019-03-14 14:35:34 +00:00
|
|
|
|
2019-01-22 18:14:43 +00:00
|
|
|
for _, policyID := range args.PolicyIDs {
|
|
|
|
_, policy, _ := testPolicyForID(policyID)
|
|
|
|
if policy != nil {
|
|
|
|
reply.Policies = append(reply.Policies, policy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
// effectively disable caching - so the only way we end up with 1 token read is if they were
|
|
|
|
// being resolved concurrently
|
|
|
|
config.Config.ACLTokenTTL = 0 * time.Second
|
2020-08-21 18:05:25 +00:00
|
|
|
config.Config.ACLPolicyTTL = 30 * time.Second
|
|
|
|
config.Config.ACLRoleTTL = 30 * time.Second
|
2019-01-22 18:14:43 +00:00
|
|
|
config.Config.ACLDownPolicy = "extend-cache"
|
|
|
|
})
|
|
|
|
|
|
|
|
ch1 := make(chan *asyncResolutionResult)
|
|
|
|
ch2 := make(chan *asyncResolutionResult)
|
|
|
|
go resolveTokenAsync(r, "a1a54629-5050-4d17-8a4e-560d2423f835", ch1)
|
|
|
|
go resolveTokenAsync(r, "a1a54629-5050-4d17-8a4e-560d2423f835", ch2)
|
|
|
|
close(readyCh)
|
|
|
|
|
|
|
|
res1 := <-ch1
|
|
|
|
res2 := <-ch2
|
|
|
|
require.NoError(t, res1.err)
|
|
|
|
require.NoError(t, res2.err)
|
|
|
|
require.Equal(t, res1.authz, res2.authz)
|
|
|
|
require.Equal(t, int32(1), tokenReads)
|
|
|
|
require.Equal(t, int32(1), policyResolves)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
func TestACLResolver_Client_TokensPoliciesAndRoles(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: false,
|
|
|
|
localPolicies: false,
|
|
|
|
localRoles: false,
|
|
|
|
}
|
|
|
|
delegate.tokenReadFn = delegate.plainTokenReadFn
|
|
|
|
delegate.policyResolveFn = delegate.plainPolicyResolveFn
|
|
|
|
delegate.roleResolveFn = delegate.plainRoleResolveFn
|
|
|
|
|
|
|
|
testACLResolver_variousTokens(t, delegate)
|
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func TestACLResolver_LocalTokensPoliciesAndRoles(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2018-10-19 16:04:07 +00:00
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: true,
|
|
|
|
localPolicies: true,
|
2019-04-15 20:43:19 +00:00
|
|
|
localRoles: true,
|
2018-10-19 16:04:07 +00:00
|
|
|
// No need to provide any of the RPC callbacks
|
|
|
|
}
|
2014-08-11 21:18:51 +00:00
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
testACLResolver_variousTokens(t, delegate)
|
2014-08-11 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 20:43:19 +00:00
|
|
|
func TestACLResolver_LocalPoliciesAndRoles(t *testing.T) {
|
2017-06-27 13:22:18 +00:00
|
|
|
t.Parallel()
|
2019-04-15 20:43:19 +00:00
|
|
|
|
2018-10-19 16:04:07 +00:00
|
|
|
delegate := &ACLResolverTestDelegate{
|
|
|
|
enabled: true,
|
|
|
|
datacenter: "dc1",
|
|
|
|
legacy: false,
|
|
|
|
localTokens: false,
|
|
|
|
localPolicies: true,
|
2019-04-15 20:43:19 +00:00
|
|
|
localRoles: true,
|
2018-10-19 16:04:07 +00:00
|
|
|
}
|
2019-04-26 17:49:28 +00:00
|
|
|
delegate.tokenReadFn = delegate.plainTokenReadFn
|
2019-04-15 20:43:19 +00:00
|
|
|
|
|
|
|
testACLResolver_variousTokens(t, delegate)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testACLResolver_variousTokens(t *testing.T, delegate *ACLResolverTestDelegate) {
|
|
|
|
t.Helper()
|
2019-04-26 17:49:28 +00:00
|
|
|
r := newTestACLResolver(t, delegate, func(config *ACLResolverConfig) {
|
|
|
|
config.Config.ACLTokenTTL = 600 * time.Second
|
|
|
|
config.Config.ACLPolicyTTL = 30 * time.Millisecond
|
|
|
|
config.Config.ACLRoleTTL = 30 * time.Millisecond
|
|
|
|
config.Config.ACLDownPolicy = "extend-cache"
|
|
|
|
})
|
|
|
|
reset := func() {
|
|
|
|
// prevent subtest bleedover
|
|
|
|
r.cache.Purge()
|
|
|
|
delegate.Reset()
|
|
|
|
}
|
|
|
|
|
|
|
|
runTwiceAndReset := func(name string, f func(t *testing.T)) {
|
|
|
|
t.Helper()
|
|
|
|
defer reset() // reset the stateful resolve AND blow away the cache
|
|
|
|
|
|
|
|
t.Run(name+" (no-cache)", f)
|
|
|
|
delegate.Reset() // allow the stateful resolve functions to reset
|
|
|
|
t.Run(name+" (cached)", f)
|
|
|
|
}
|
2014-08-11 21:18:51 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
runTwiceAndReset("Missing Identity", func(t *testing.T) {
|
2021-09-16 14:17:02 +00:00
|
|
|
delegate.UseTestLocalData(nil)
|
2022-01-23 17:31:48 +00:00
|
|
|
_, err := r.ResolveToken("doesn't exist")
|
2018-10-19 16:04:07 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.True(t, acl.IsErrNotFound(err))
|
2014-08-11 21:18:51 +00:00
|
|
|
})
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
runTwiceAndReset("Missing Policy", func(t *testing.T) {
|
2021-09-16 14:17:02 +00:00
|
|
|
delegate.UseTestLocalData([]interface{}{
|
|
|
|
&structs.ACLToken{
|
|
|
|
AccessorID: "435a75af-1763-4980-89f4-f0951dda53b4",
|
|
|
|
SecretID: "missing-policy",
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
|
|
|
{ID: "not-found"},
|
|
|
|
{ID: "acl-ro"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"policy-not-found:not-found",
|
|
|
|
&structs.ACLPolicy{
|
|
|
|
ID: "acl-ro",
|
|
|
|
Name: "acl-ro",
|
|
|
|
Description: "acl-ro",
|
|
|
|
Rules: `acl = "read"`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
})
|
2020-11-17 23:15:07 +00:00
|
|
|
authz := resolveToken(t, r, "missing-policy")
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.ACLRead(nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
})
|
2014-08-11 21:18:51 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
runTwiceAndReset("Missing Role", func(t *testing.T) {
|
2021-09-16 14:17:02 +00:00
|
|
|
delegate.UseTestLocalData([]interface{}{
|
|
|
|
&structs.ACLToken{
|
|
|
|
AccessorID: "435a75af-1763-4980-89f4-f0951dda53b4",
|
|
|
|
SecretID: "missing-role",
|
|
|
|
Roles: []structs.ACLTokenRoleLink{
|
|
|
|
{ID: "not-found"},
|
|
|
|
{ID: "acl-ro"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"role-not-found:not-found",
|
|
|
|
&structs.ACLRole{
|
|
|
|
ID: "acl-ro",
|
|
|
|
Name: "acl-ro",
|
|
|
|
Description: "acl-ro",
|
|
|
|
Policies: []structs.ACLRolePolicyLink{
|
|
|
|
{ID: "acl-ro"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ACLPolicy{
|
|
|
|
ID: "acl-ro",
|
|
|
|
Name: "acl-ro",
|
|
|
|
Description: "acl-ro",
|
|
|
|
Rules: `acl = "read"`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
})
|
2020-11-17 23:15:07 +00:00
|
|
|
authz := resolveToken(t, r, "missing-role")
|
2019-04-15 20:43:19 +00:00
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.ACLRead(nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("foo", nil))
|
2019-04-15 20:43:19 +00:00
|
|
|
})
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
runTwiceAndReset("Missing Policy on Role", func(t *testing.T) {
|
2021-09-16 14:17:02 +00:00
|
|
|
delegate.UseTestLocalData([]interface{}{
|
|
|
|
&structs.ACLToken{
|
|
|
|
AccessorID: "435a75af-1763-4980-89f4-f0951dda53b4",
|
|
|
|
SecretID: "missing-policy-on-role",
|
|
|
|
Roles: []structs.ACLTokenRoleLink{
|
|
|
|
{ID: "missing-policy"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ACLRole{
|
|
|
|
ID: "missing-policy",
|
|
|
|
Name: "missing-policy",
|
|
|
|
Description: "missing-policy",
|
|
|
|
Policies: []structs.ACLRolePolicyLink{
|
|
|
|
{ID: "not-found"},
|
|
|
|
{ID: "acl-ro"},
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
"policy-not-found:not-found",
|
|
|
|
&structs.ACLPolicy{
|
|
|
|
ID: "acl-ro",
|
|
|
|
Name: "acl-ro",
|
|
|
|
Description: "acl-ro",
|
|
|
|
Rules: `acl = "read"`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
})
|
2020-11-17 23:15:07 +00:00
|
|
|
authz := resolveToken(t, r, "missing-policy-on-role")
|
2019-04-15 20:43:19 +00:00
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.ACLRead(nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("foo", nil))
|
2019-04-15 20:43:19 +00:00
|
|
|
})
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
runTwiceAndReset("Normal with Policy", func(t *testing.T) {
|
2021-09-16 14:17:02 +00:00
|
|
|
delegate.UseTestLocalData([]interface{}{
|
|
|
|
&structs.ACLToken{
|
|
|
|
AccessorID: "5f57c1f6-6a89-4186-9445-531b316e01df",
|
|
|
|
SecretID: "found",
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
|
|
|
{ID: "node-wr"},
|
|
|
|
{ID: "dc2-key-wr"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ACLPolicy{
|
|
|
|
ID: "node-wr",
|
|
|
|
Name: "node-wr",
|
|
|
|
Description: "node-wr",
|
|
|
|
Rules: `node_prefix "" { policy = "write"}`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
Datacenters: []string{"dc1"},
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
&structs.ACLPolicy{
|
|
|
|
ID: "dc2-key-wr",
|
|
|
|
Name: "dc2-key-wr",
|
|
|
|
Description: "dc2-key-wr",
|
|
|
|
Rules: `key_prefix "" { policy = "write"}`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
Datacenters: []string{"dc2"},
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
})
|
2020-11-17 23:15:07 +00:00
|
|
|
authz := resolveToken(t, r, "found")
|
2018-10-19 16:04:07 +00:00
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Deny, authz.ACLRead(nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
})
|
2014-08-11 21:18:51 +00:00
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
runTwiceAndReset("Normal with Role", func(t *testing.T) {
|
2021-09-16 14:17:02 +00:00
|
|
|
delegate.UseTestLocalData([]interface{}{
|
|
|
|
&structs.ACLToken{
|
|
|
|
AccessorID: "5f57c1f6-6a89-4186-9445-531b316e01df",
|
|
|
|
SecretID: "found-role",
|
|
|
|
Roles: []structs.ACLTokenRoleLink{
|
|
|
|
{ID: "found"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ACLRole{
|
|
|
|
ID: "found",
|
|
|
|
Name: "found",
|
|
|
|
Description: "found",
|
|
|
|
Policies: []structs.ACLRolePolicyLink{
|
|
|
|
{ID: "node-wr"},
|
|
|
|
{ID: "dc2-key-wr"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ACLPolicy{
|
|
|
|
ID: "node-wr",
|
|
|
|
Name: "node-wr",
|
|
|
|
Description: "node-wr",
|
|
|
|
Rules: `node_prefix "" { policy = "write"}`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
Datacenters: []string{"dc1"},
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
&structs.ACLPolicy{
|
|
|
|
ID: "dc2-key-wr",
|
|
|
|
Name: "dc2-key-wr",
|
|
|
|
Description: "dc2-key-wr",
|
|
|
|
Rules: `key_prefix "" { policy = "write"}`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
Datacenters: []string{"dc2"},
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
})
|
2020-11-17 23:15:07 +00:00
|
|
|
authz := resolveToken(t, r, "found-role")
|
2019-04-15 20:43:19 +00:00
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Deny, authz.ACLRead(nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2019-04-15 20:43:19 +00:00
|
|
|
})
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
runTwiceAndReset("Normal with Policy and Role", func(t *testing.T) {
|
2021-09-16 14:17:02 +00:00
|
|
|
delegate.UseTestLocalData([]interface{}{
|
|
|
|
&structs.ACLToken{
|
|
|
|
AccessorID: "5f57c1f6-6a89-4186-9445-531b316e01df",
|
|
|
|
SecretID: "found-policy-and-role",
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
|
|
|
{ID: "node-wr"},
|
|
|
|
{ID: "dc2-key-wr"},
|
|
|
|
},
|
|
|
|
Roles: []structs.ACLTokenRoleLink{
|
|
|
|
{ID: "service-ro"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ACLPolicy{
|
|
|
|
ID: "node-wr",
|
|
|
|
Name: "node-wr",
|
|
|
|
Description: "node-wr",
|
|
|
|
Rules: `node_prefix "" { policy = "write"}`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
Datacenters: []string{"dc1"},
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
&structs.ACLPolicy{
|
|
|
|
ID: "dc2-key-wr",
|
|
|
|
Name: "dc2-key-wr",
|
|
|
|
Description: "dc2-key-wr",
|
|
|
|
Rules: `key_prefix "" { policy = "write"}`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
Datacenters: []string{"dc2"},
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
&structs.ACLRole{
|
|
|
|
ID: "service-ro",
|
|
|
|
Name: "service-ro",
|
|
|
|
Description: "service-ro",
|
|
|
|
Policies: []structs.ACLRolePolicyLink{
|
|
|
|
{ID: "service-ro"},
|
|
|
|
},
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
&structs.ACLPolicy{
|
|
|
|
ID: "service-ro",
|
|
|
|
Name: "service-ro",
|
|
|
|
Description: "service-ro",
|
|
|
|
Rules: `service_prefix "" { policy = "read" }`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
})
|
2020-11-17 23:15:07 +00:00
|
|
|
authz := resolveToken(t, r, "found-policy-and-role")
|
2019-04-15 20:43:19 +00:00
|
|
|
require.NotNil(t, authz)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Deny, authz.ACLRead(nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.ServiceRead("bar", nil))
|
2019-04-15 20:43:19 +00:00
|
|
|
})
|
|
|
|
|
2020-06-16 16:54:27 +00:00
|
|
|
runTwiceAndReset("Role With Node Identity", func(t *testing.T) {
|
2021-09-16 14:17:02 +00:00
|
|
|
delegate.UseTestLocalData([]interface{}{
|
|
|
|
&structs.ACLToken{
|
|
|
|
AccessorID: "f3f47a09-de29-4c57-8f54-b65a9be79641",
|
|
|
|
SecretID: "found-role-node-identity",
|
|
|
|
Roles: []structs.ACLTokenRoleLink{
|
|
|
|
{ID: "node-identity"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ACLRole{
|
|
|
|
ID: "node-identity",
|
|
|
|
Name: "node-identity",
|
|
|
|
Description: "node-identity",
|
|
|
|
NodeIdentities: []*structs.ACLNodeIdentity{
|
|
|
|
{
|
|
|
|
NodeName: "test-node",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
NodeName: "test-node-dc2",
|
|
|
|
Datacenter: "dc2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2020-11-17 23:15:07 +00:00
|
|
|
authz := resolveToken(t, r, "found-role-node-identity")
|
2020-06-16 16:54:27 +00:00
|
|
|
require.NotNil(t, authz)
|
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("test-node", nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("test-node-dc2", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.ServiceRead("something", nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.ServiceWrite("something", nil))
|
|
|
|
})
|
|
|
|
|
2019-04-26 17:49:28 +00:00
|
|
|
runTwiceAndReset("Synthetic Policies Independently Cache", func(t *testing.T) {
|
2021-09-16 14:17:02 +00:00
|
|
|
delegate.UseTestLocalData([]interface{}{
|
|
|
|
&structs.ACLToken{
|
|
|
|
AccessorID: "f6c5a5fb-4da4-422b-9abf-2c942813fc71",
|
|
|
|
SecretID: "found-synthetic-policy-1",
|
|
|
|
ServiceIdentities: []*structs.ACLServiceIdentity{
|
|
|
|
{ServiceName: "service1"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ACLToken{
|
|
|
|
AccessorID: "7c87dfad-be37-446e-8305-299585677cb5",
|
|
|
|
SecretID: "found-synthetic-policy-2",
|
|
|
|
ServiceIdentities: []*structs.ACLServiceIdentity{
|
|
|
|
{ServiceName: "service2"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ACLToken{
|
|
|
|
AccessorID: "bebccc92-3987-489d-84c2-ffd00d93ef93",
|
|
|
|
SecretID: "found-synthetic-policy-3",
|
|
|
|
NodeIdentities: []*structs.ACLNodeIdentity{
|
|
|
|
{
|
|
|
|
NodeName: "test-node1",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
},
|
|
|
|
// as the resolver is in dc1 this identity should be ignored
|
|
|
|
{
|
|
|
|
NodeName: "test-node-dc2",
|
|
|
|
Datacenter: "dc2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ACLToken{
|
|
|
|
AccessorID: "359b9927-25fd-46b9-bd14-3470f848ec65",
|
|
|
|
SecretID: "found-synthetic-policy-4",
|
|
|
|
NodeIdentities: []*structs.ACLNodeIdentity{
|
|
|
|
{
|
|
|
|
NodeName: "test-node2",
|
|
|
|
Datacenter: "dc1",
|
|
|
|
},
|
|
|
|
// as the resolver is in dc1 this identity should be ignored
|
|
|
|
{
|
|
|
|
NodeName: "test-node-dc2",
|
|
|
|
Datacenter: "dc2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2020-06-16 16:54:27 +00:00
|
|
|
// We resolve these tokens in the same cache session
|
2019-04-26 17:49:28 +00:00
|
|
|
// to verify that the keys for caching synthetic policies don't bleed
|
|
|
|
// over between each other.
|
2021-09-16 14:17:02 +00:00
|
|
|
t.Run("synthetic-policy-1", func(t *testing.T) { // service identity
|
2019-04-26 17:49:28 +00:00
|
|
|
authz, err := r.ResolveToken("found-synthetic-policy-1")
|
|
|
|
require.NotNil(t, authz)
|
|
|
|
require.NoError(t, err)
|
|
|
|
// spot check some random perms
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Deny, authz.ACLRead(nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("foo", nil))
|
2019-04-26 17:49:28 +00:00
|
|
|
// ensure we didn't bleed over to the other synthetic policy
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Deny, authz.ServiceWrite("service2", nil))
|
2019-04-26 17:49:28 +00:00
|
|
|
// check our own synthetic policy
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.ServiceWrite("service1", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.ServiceRead("literally-anything", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.NodeRead("any-node", nil))
|
2021-09-16 14:17:02 +00:00
|
|
|
})
|
|
|
|
t.Run("synthetic-policy-2", func(t *testing.T) { // service identity
|
2019-04-26 17:49:28 +00:00
|
|
|
authz, err := r.ResolveToken("found-synthetic-policy-2")
|
|
|
|
require.NotNil(t, authz)
|
|
|
|
require.NoError(t, err)
|
|
|
|
// spot check some random perms
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Deny, authz.ACLRead(nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("foo", nil))
|
2019-04-26 17:49:28 +00:00
|
|
|
// ensure we didn't bleed over to the other synthetic policy
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Deny, authz.ServiceWrite("service1", nil))
|
2019-04-26 17:49:28 +00:00
|
|
|
// check our own synthetic policy
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.ServiceWrite("service2", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.ServiceRead("literally-anything", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.NodeRead("any-node", nil))
|
2021-09-16 14:17:02 +00:00
|
|
|
})
|
|
|
|
t.Run("synthetic-policy-3", func(t *testing.T) { // node identity
|
2020-06-16 16:54:27 +00:00
|
|
|
authz, err := r.ResolveToken("found-synthetic-policy-3")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
|
|
|
|
|
|
|
// spot check some random perms
|
|
|
|
require.Equal(t, acl.Deny, authz.ACLRead(nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("foo", nil))
|
|
|
|
// ensure we didn't bleed over to the other synthetic policy
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("test-node2", nil))
|
|
|
|
// check our own synthetic policy
|
|
|
|
require.Equal(t, acl.Allow, authz.ServiceRead("literally-anything", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("test-node1", nil))
|
|
|
|
// ensure node identity for other DC is ignored
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("test-node-dc2", nil))
|
2021-09-16 14:17:02 +00:00
|
|
|
})
|
|
|
|
t.Run("synthetic-policy-4", func(t *testing.T) { // node identity
|
2020-06-16 16:54:27 +00:00
|
|
|
authz, err := r.ResolveToken("found-synthetic-policy-4")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
|
|
|
|
|
|
|
// spot check some random perms
|
|
|
|
require.Equal(t, acl.Deny, authz.ACLRead(nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("foo", nil))
|
|
|
|
// ensure we didn't bleed over to the other synthetic policy
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("test-node1", nil))
|
|
|
|
// check our own synthetic policy
|
|
|
|
require.Equal(t, acl.Allow, authz.ServiceRead("literally-anything", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("test-node2", nil))
|
|
|
|
// ensure node identity for other DC is ignored
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("test-node-dc2", nil))
|
2021-09-16 14:17:02 +00:00
|
|
|
})
|
2019-04-26 17:49:28 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
runTwiceAndReset("Anonymous", func(t *testing.T) {
|
2021-09-16 14:17:02 +00:00
|
|
|
delegate.UseTestLocalData([]interface{}{
|
|
|
|
&structs.ACLToken{
|
|
|
|
AccessorID: "00000000-0000-0000-0000-000000000002",
|
|
|
|
SecretID: anonymousToken,
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
|
|
|
{ID: "node-wr"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ACLPolicy{
|
|
|
|
ID: "node-wr",
|
|
|
|
Name: "node-wr",
|
|
|
|
Description: "node-wr",
|
|
|
|
Rules: `node_prefix "" { policy = "write"}`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
Datacenters: []string{"dc1"},
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
})
|
2018-10-19 16:04:07 +00:00
|
|
|
authz, err := r.ResolveToken("")
|
|
|
|
require.NotNil(t, authz)
|
|
|
|
require.NoError(t, err)
|
2019-10-15 20:58:50 +00:00
|
|
|
require.Equal(t, acl.Deny, authz.ACLRead(nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.NodeWrite("foo", nil))
|
2018-10-19 16:04:07 +00:00
|
|
|
})
|
2014-08-11 21:18:51 +00:00
|
|
|
|
2021-09-16 16:08:45 +00:00
|
|
|
runTwiceAndReset("service and intention wildcard write", func(t *testing.T) {
|
|
|
|
delegate.UseTestLocalData([]interface{}{
|
|
|
|
&structs.ACLToken{
|
|
|
|
AccessorID: "5f57c1f6-6a89-4186-9445-531b316e01df",
|
|
|
|
SecretID: "with-intentions",
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{
|
|
|
|
{ID: "ixn-write"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
&structs.ACLPolicy{
|
|
|
|
ID: "ixn-write",
|
|
|
|
Name: "ixn-write",
|
|
|
|
Description: "ixn-write",
|
|
|
|
Rules: `service_prefix "" { policy = "write" intentions = "write" }`,
|
|
|
|
Syntax: acl.SyntaxCurrent,
|
|
|
|
RaftIndex: structs.RaftIndex{CreateIndex: 1, ModifyIndex: 2},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
authz, err := r.ResolveToken("with-intentions")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
|
|
|
require.Equal(t, acl.Allow, authz.ServiceRead("", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.ServiceRead("foo", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.ServiceRead("bar", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.ServiceWrite("", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.ServiceWrite("foo", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.ServiceWrite("bar", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.IntentionRead("", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.IntentionRead("foo", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.IntentionRead("bar", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.IntentionWrite("", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.IntentionWrite("foo", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.IntentionWrite("bar", nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeRead("server", nil))
|
|
|
|
})
|
2014-08-11 21:18:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-09 19:13:09 +00:00
|
|
|
func TestACL_LocalToken(t *testing.T) {
|
|
|
|
t.Run("local token in same dc", func(t *testing.T) {
|
|
|
|
d := &ACLResolverTestDelegate{
|
|
|
|
datacenter: "dc1",
|
|
|
|
tokenReadFn: func(_ *structs.ACLTokenGetRequest, reply *structs.ACLTokenResponse) error {
|
|
|
|
reply.Token = &structs.ACLToken{Local: true}
|
|
|
|
// different dc
|
|
|
|
reply.SourceDatacenter = "dc1"
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
r := newTestACLResolver(t, d, nil)
|
|
|
|
_, err := r.fetchAndCacheIdentityFromToken("", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("non local token in remote dc", func(t *testing.T) {
|
|
|
|
d := &ACLResolverTestDelegate{
|
|
|
|
datacenter: "dc1",
|
|
|
|
tokenReadFn: func(_ *structs.ACLTokenGetRequest, reply *structs.ACLTokenResponse) error {
|
|
|
|
reply.Token = &structs.ACLToken{Local: false}
|
|
|
|
// different dc
|
|
|
|
reply.SourceDatacenter = "remote"
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
r := newTestACLResolver(t, d, nil)
|
|
|
|
_, err := r.fetchAndCacheIdentityFromToken("", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("local token in remote dc", func(t *testing.T) {
|
|
|
|
d := &ACLResolverTestDelegate{
|
|
|
|
datacenter: "dc1",
|
|
|
|
tokenReadFn: func(_ *structs.ACLTokenGetRequest, reply *structs.ACLTokenResponse) error {
|
|
|
|
reply.Token = &structs.ACLToken{Local: true}
|
|
|
|
// different dc
|
|
|
|
reply.SourceDatacenter = "remote"
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
r := newTestACLResolver(t, d, nil)
|
|
|
|
_, err := r.fetchAndCacheIdentityFromToken("", nil)
|
|
|
|
require.Equal(t, acl.PermissionDeniedError{Cause: "This is a local token in datacenter \"remote\""}, err)
|
|
|
|
})
|
|
|
|
}
|
2021-04-14 16:39:35 +00:00
|
|
|
|
2022-01-20 12:47:50 +00:00
|
|
|
func TestACLResolver_AgentRecovery(t *testing.T) {
|
2021-04-14 16:39:35 +00:00
|
|
|
var tokens token.Store
|
|
|
|
|
|
|
|
d := &ACLResolverTestDelegate{
|
|
|
|
datacenter: "dc1",
|
|
|
|
enabled: true,
|
|
|
|
}
|
|
|
|
r := newTestACLResolver(t, d, func(cfg *ACLResolverConfig) {
|
|
|
|
cfg.Tokens = &tokens
|
|
|
|
cfg.Config.NodeName = "foo"
|
2021-08-09 20:29:21 +00:00
|
|
|
cfg.DisableDuration = 0
|
2021-04-14 16:39:35 +00:00
|
|
|
})
|
|
|
|
|
2021-12-07 12:12:47 +00:00
|
|
|
tokens.UpdateAgentRecoveryToken("9a184a11-5599-459e-b71a-550e5f9a5a23", token.TokenSourceConfig)
|
2021-04-14 16:39:35 +00:00
|
|
|
|
2022-01-23 17:31:48 +00:00
|
|
|
authz, err := r.ResolveToken("9a184a11-5599-459e-b71a-550e5f9a5a23")
|
2021-04-14 16:39:35 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-23 17:31:48 +00:00
|
|
|
require.NotNil(t, authz.ACLIdentity)
|
|
|
|
require.Equal(t, "agent-recovery:foo", authz.ACLIdentity.ID())
|
|
|
|
require.NotNil(t, authz.Authorizer)
|
|
|
|
require.Equal(t, r.agentRecoveryAuthz, authz.Authorizer)
|
2021-04-14 16:39:35 +00:00
|
|
|
require.Equal(t, acl.Allow, authz.AgentWrite("foo", nil))
|
|
|
|
require.Equal(t, acl.Allow, authz.NodeRead("bar", nil))
|
|
|
|
require.Equal(t, acl.Deny, authz.NodeWrite("bar", nil))
|
|
|
|
}
|
2021-08-09 20:29:21 +00:00
|
|
|
|
|
|
|
func TestACLResolver_ACLsEnabled(t *testing.T) {
|
|
|
|
type testCase struct {
|
|
|
|
name string
|
|
|
|
resolver *ACLResolver
|
|
|
|
enabled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
run := func(t *testing.T, tc testCase) {
|
|
|
|
require.Equal(t, tc.enabled, tc.resolver.ACLsEnabled())
|
|
|
|
}
|
|
|
|
|
|
|
|
var testCases = []testCase{
|
|
|
|
{
|
|
|
|
name: "config disabled",
|
|
|
|
resolver: &ACLResolver{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "config enabled, disableDuration=0 (Server)",
|
|
|
|
resolver: &ACLResolver{
|
|
|
|
config: ACLResolverSettings{ACLsEnabled: true},
|
|
|
|
},
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "config enabled, disabled by RPC (Client)",
|
|
|
|
resolver: &ACLResolver{
|
|
|
|
config: ACLResolverSettings{ACLsEnabled: true},
|
|
|
|
disableDuration: 10 * time.Second,
|
|
|
|
disabledUntil: time.Now().Add(5 * time.Second),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "config enabled, past disabledUntil (Client)",
|
|
|
|
resolver: &ACLResolver{
|
|
|
|
config: ACLResolverSettings{ACLsEnabled: true},
|
|
|
|
disableDuration: 10 * time.Second,
|
|
|
|
disabledUntil: time.Now().Add(-5 * time.Second),
|
|
|
|
},
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "config enabled, no disabledUntil (Client)",
|
|
|
|
resolver: &ACLResolver{
|
|
|
|
config: ACLResolverSettings{ACLsEnabled: true},
|
|
|
|
disableDuration: 10 * time.Second,
|
|
|
|
},
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
run(t, tc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-09-21 22:48:50 +00:00
|
|
|
|
2022-01-23 17:31:48 +00:00
|
|
|
func TestACLResolver_ResolveToken_UpdatesPurgeTheCache(t *testing.T) {
|
2021-09-21 22:48:50 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
_, srv, codec := testACLServerWithConfig(t, nil, false)
|
|
|
|
waitForLeaderEstablishment(t, srv)
|
|
|
|
|
|
|
|
reqPolicy := structs.ACLPolicySetRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Policy: structs.ACLPolicy{
|
|
|
|
Name: "the-policy",
|
|
|
|
Rules: `key_prefix "" { policy = "read"}`,
|
|
|
|
},
|
2022-01-20 12:47:50 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: TestDefaultInitialManagementToken},
|
2021-09-21 22:48:50 +00:00
|
|
|
}
|
|
|
|
var respPolicy = structs.ACLPolicy{}
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ACL.PolicySet", &reqPolicy, &respPolicy)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
token, err := uuid.GenerateUUID()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
reqToken := structs.ACLTokenSetRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
ACLToken: structs.ACLToken{
|
|
|
|
SecretID: token,
|
|
|
|
Policies: []structs.ACLTokenPolicyLink{{Name: "the-policy"}},
|
|
|
|
},
|
2022-01-20 12:47:50 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: TestDefaultInitialManagementToken},
|
2021-09-21 22:48:50 +00:00
|
|
|
}
|
|
|
|
var respToken structs.ACLToken
|
|
|
|
err = msgpackrpc.CallWithCodec(codec, "ACL.TokenSet", &reqToken, &respToken)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-10 20:25:51 +00:00
|
|
|
testutil.RunStep(t, "first resolve", func(t *testing.T) {
|
2022-01-23 17:31:48 +00:00
|
|
|
authz, err := srv.ACLResolver.ResolveToken(token)
|
2021-09-21 22:48:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
|
|
|
require.Equal(t, acl.Allow, authz.KeyRead("foo", nil))
|
|
|
|
})
|
|
|
|
|
2022-05-10 20:25:51 +00:00
|
|
|
testutil.RunStep(t, "update the policy and resolve again", func(t *testing.T) {
|
2021-09-21 22:48:50 +00:00
|
|
|
reqPolicy := structs.ACLPolicySetRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
Policy: structs.ACLPolicy{
|
|
|
|
ID: respPolicy.ID,
|
|
|
|
Name: "the-policy",
|
|
|
|
Rules: `{"key_prefix": {"": {"policy": "deny"}}}`,
|
|
|
|
},
|
2022-01-20 12:47:50 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: TestDefaultInitialManagementToken},
|
2021-09-21 22:48:50 +00:00
|
|
|
}
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ACL.PolicySet", &reqPolicy, &structs.ACLPolicy{})
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-23 17:31:48 +00:00
|
|
|
authz, err := srv.ACLResolver.ResolveToken(token)
|
2021-09-21 22:48:50 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, authz)
|
|
|
|
require.Equal(t, acl.Deny, authz.KeyRead("foo", nil))
|
|
|
|
})
|
|
|
|
|
2022-05-10 20:25:51 +00:00
|
|
|
testutil.RunStep(t, "delete the token", func(t *testing.T) {
|
2021-09-21 22:48:50 +00:00
|
|
|
req := structs.ACLTokenDeleteRequest{
|
|
|
|
Datacenter: "dc1",
|
|
|
|
TokenID: respToken.AccessorID,
|
2022-01-20 12:47:50 +00:00
|
|
|
WriteRequest: structs.WriteRequest{Token: TestDefaultInitialManagementToken},
|
2021-09-21 22:48:50 +00:00
|
|
|
}
|
|
|
|
var resp string
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, "ACL.TokenDelete", &req, &resp)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-01-23 17:31:48 +00:00
|
|
|
_, err = srv.ACLResolver.ResolveToken(token)
|
2021-09-21 22:48:50 +00:00
|
|
|
require.True(t, acl.IsErrNotFound(err), "Error %v is not acl.ErrNotFound", err)
|
|
|
|
})
|
|
|
|
}
|