2022-06-10 13:41:54 +00:00
|
|
|
package nomad
|
|
|
|
|
|
|
|
import (
|
2022-08-22 20:37:21 +00:00
|
|
|
"encoding/json"
|
2022-06-10 13:41:54 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2022-08-22 20:37:21 +00:00
|
|
|
"time"
|
2022-06-10 13:41:54 +00:00
|
|
|
|
2022-08-15 15:19:53 +00:00
|
|
|
msgpackrpc "github.com/hashicorp/net-rpc-msgpackrpc"
|
|
|
|
"github.com/shoenig/test/must"
|
|
|
|
|
2022-06-20 15:21:03 +00:00
|
|
|
"github.com/hashicorp/nomad/acl"
|
2022-06-10 13:41:54 +00:00
|
|
|
"github.com/hashicorp/nomad/ci"
|
|
|
|
"github.com/hashicorp/nomad/nomad/mock"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
"github.com/hashicorp/nomad/testutil"
|
|
|
|
)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestVariablesEndpoint_auth(t *testing.T) {
|
2022-06-10 13:41:54 +00:00
|
|
|
|
|
|
|
ci.Parallel(t)
|
|
|
|
srv, _, shutdown := TestACLServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0 // Prevent automatic dequeue
|
|
|
|
})
|
|
|
|
defer shutdown()
|
|
|
|
testutil.WaitForLeader(t, srv.RPC)
|
|
|
|
|
|
|
|
const ns = "nondefault-namespace"
|
|
|
|
|
2022-06-20 15:21:03 +00:00
|
|
|
alloc1 := mock.Alloc()
|
|
|
|
alloc1.ClientStatus = structs.AllocClientStatusFailed
|
|
|
|
alloc1.Job.Namespace = ns
|
|
|
|
alloc1.Namespace = ns
|
|
|
|
jobID := alloc1.JobID
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
// create an alloc that will have no access to variables we create
|
2022-06-20 15:21:03 +00:00
|
|
|
alloc2 := mock.Alloc()
|
|
|
|
alloc2.Job.TaskGroups[0].Name = "other-no-permissions"
|
|
|
|
alloc2.TaskGroup = "other-no-permissions"
|
|
|
|
alloc2.ClientStatus = structs.AllocClientStatusRunning
|
|
|
|
alloc2.Job.Namespace = ns
|
|
|
|
alloc2.Namespace = ns
|
2022-06-10 13:41:54 +00:00
|
|
|
|
2022-07-21 13:05:54 +00:00
|
|
|
alloc3 := mock.Alloc()
|
|
|
|
alloc3.ClientStatus = structs.AllocClientStatusRunning
|
|
|
|
alloc3.Job.Namespace = ns
|
|
|
|
alloc3.Namespace = ns
|
|
|
|
alloc3.Job.ParentID = jobID
|
|
|
|
|
2022-10-18 20:43:59 +00:00
|
|
|
alloc4 := mock.Alloc()
|
|
|
|
alloc4.ClientStatus = structs.AllocClientStatusRunning
|
|
|
|
alloc4.Job.Namespace = ns
|
|
|
|
alloc4.Namespace = ns
|
|
|
|
|
2022-06-10 13:41:54 +00:00
|
|
|
store := srv.fsm.State()
|
2022-08-15 15:19:53 +00:00
|
|
|
must.NoError(t, store.UpsertNamespaces(1000, []*structs.Namespace{{Name: ns}}))
|
|
|
|
must.NoError(t, store.UpsertAllocs(
|
2022-10-18 20:43:59 +00:00
|
|
|
structs.MsgTypeTestSetup, 1001, []*structs.Allocation{alloc1, alloc2, alloc3, alloc4}))
|
2022-06-10 13:41:54 +00:00
|
|
|
|
2022-07-21 13:05:54 +00:00
|
|
|
claims1 := alloc1.ToTaskIdentityClaims(nil, "web")
|
2022-11-01 19:00:50 +00:00
|
|
|
idToken, _, err := srv.encrypter.SignClaims(claims1)
|
2022-08-15 15:19:53 +00:00
|
|
|
must.NoError(t, err)
|
2022-06-10 13:41:54 +00:00
|
|
|
|
2022-07-21 13:05:54 +00:00
|
|
|
claims2 := alloc2.ToTaskIdentityClaims(nil, "web")
|
2022-11-01 19:00:50 +00:00
|
|
|
noPermissionsToken, _, err := srv.encrypter.SignClaims(claims2)
|
2022-08-15 15:19:53 +00:00
|
|
|
must.NoError(t, err)
|
2022-06-10 13:41:54 +00:00
|
|
|
|
2022-07-21 13:05:54 +00:00
|
|
|
claims3 := alloc3.ToTaskIdentityClaims(alloc3.Job, "web")
|
2022-11-01 19:00:50 +00:00
|
|
|
idDispatchToken, _, err := srv.encrypter.SignClaims(claims3)
|
2022-08-15 15:19:53 +00:00
|
|
|
must.NoError(t, err)
|
2022-07-21 13:05:54 +00:00
|
|
|
|
2022-06-10 13:41:54 +00:00
|
|
|
// corrupt the signature of the token
|
|
|
|
idTokenParts := strings.Split(idToken, ".")
|
2022-08-15 15:19:53 +00:00
|
|
|
must.Len(t, 3, idTokenParts)
|
2022-06-10 13:41:54 +00:00
|
|
|
sig := []string(strings.Split(idTokenParts[2], ""))
|
|
|
|
rand.Shuffle(len(sig), func(i, j int) {
|
|
|
|
sig[i], sig[j] = sig[j], sig[i]
|
|
|
|
})
|
|
|
|
idTokenParts[2] = strings.Join(sig, "")
|
|
|
|
invalidIDToken := strings.Join(idTokenParts, ".")
|
|
|
|
|
2022-10-18 20:43:59 +00:00
|
|
|
claims4 := alloc4.ToTaskIdentityClaims(alloc4.Job, "web")
|
2022-11-01 19:00:50 +00:00
|
|
|
wiOnlyToken, _, err := srv.encrypter.SignClaims(claims4)
|
2022-10-18 20:43:59 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
|
2022-06-20 15:21:03 +00:00
|
|
|
policy := mock.ACLPolicy()
|
|
|
|
policy.Rules = `namespace "nondefault-namespace" {
|
2022-08-26 18:03:56 +00:00
|
|
|
variables {
|
2022-08-22 20:41:21 +00:00
|
|
|
path "nomad/jobs/*" { capabilities = ["list"] }
|
2022-06-20 15:21:03 +00:00
|
|
|
path "other/path" { capabilities = ["read"] }
|
|
|
|
}}`
|
2022-08-22 20:41:21 +00:00
|
|
|
policy.JobACL = &structs.JobACL{
|
|
|
|
Namespace: ns,
|
|
|
|
JobID: jobID,
|
|
|
|
Group: alloc1.TaskGroup,
|
|
|
|
}
|
2022-06-20 15:21:03 +00:00
|
|
|
policy.SetHash()
|
|
|
|
err = store.UpsertACLPolicies(structs.MsgTypeTestSetup, 1100, []*structs.ACLPolicy{policy})
|
2022-08-15 15:19:53 +00:00
|
|
|
must.NoError(t, err)
|
2022-06-20 15:21:03 +00:00
|
|
|
|
|
|
|
aclToken := mock.ACLToken()
|
|
|
|
aclToken.Policies = []string{policy.Name}
|
|
|
|
err = store.UpsertACLTokens(structs.MsgTypeTestSetup, 1150, []*structs.ACLToken{aclToken})
|
2022-08-15 15:19:53 +00:00
|
|
|
must.NoError(t, err)
|
2022-06-20 15:21:03 +00:00
|
|
|
|
2022-12-02 15:12:05 +00:00
|
|
|
variablesRPC := NewVariablesEndpoint(srv, nil, srv.encrypter)
|
|
|
|
|
2023-01-24 15:52:07 +00:00
|
|
|
testFn := func(args *structs.QueryOptions, cap, path string) error {
|
|
|
|
err := srv.Authenticate(nil, args)
|
|
|
|
if err != nil {
|
|
|
|
return structs.ErrPermissionDenied
|
|
|
|
}
|
|
|
|
_, _, err = variablesRPC.handleMixedAuthEndpoint(
|
|
|
|
*args, cap, path)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-06-10 13:41:54 +00:00
|
|
|
t.Run("terminal alloc should be denied", func(t *testing.T) {
|
2023-01-24 15:52:07 +00:00
|
|
|
err := testFn(
|
|
|
|
&structs.QueryOptions{AuthToken: idToken, Namespace: ns}, acl.PolicyList,
|
2022-07-19 20:17:34 +00:00
|
|
|
fmt.Sprintf("nomad/jobs/%s/web/web", jobID))
|
2022-08-15 15:19:53 +00:00
|
|
|
must.EqError(t, err, structs.ErrPermissionDenied.Error())
|
2022-06-10 13:41:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// make alloc non-terminal
|
2022-06-20 15:21:03 +00:00
|
|
|
alloc1.ClientStatus = structs.AllocClientStatusRunning
|
2022-08-15 15:19:53 +00:00
|
|
|
must.NoError(t, store.UpsertAllocs(
|
2022-06-20 15:21:03 +00:00
|
|
|
structs.MsgTypeTestSetup, 1200, []*structs.Allocation{alloc1}))
|
2022-06-10 13:41:54 +00:00
|
|
|
|
|
|
|
t.Run("wrong namespace should be denied", func(t *testing.T) {
|
2023-01-24 15:52:07 +00:00
|
|
|
err := testFn(&structs.QueryOptions{
|
|
|
|
AuthToken: idToken, Namespace: structs.DefaultNamespace}, acl.PolicyList,
|
2022-07-19 20:17:34 +00:00
|
|
|
fmt.Sprintf("nomad/jobs/%s/web/web", jobID))
|
2022-08-15 15:19:53 +00:00
|
|
|
must.EqError(t, err, structs.ErrPermissionDenied.Error())
|
2022-06-10 13:41:54 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
token string
|
|
|
|
cap string
|
|
|
|
path string
|
|
|
|
expectedErr error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "valid claim for path with task secret",
|
|
|
|
token: idToken,
|
2022-10-18 20:43:59 +00:00
|
|
|
cap: acl.PolicyRead,
|
2022-07-19 20:17:34 +00:00
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web/web", jobID),
|
2022-06-10 13:41:54 +00:00
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid claim for path with group secret",
|
|
|
|
token: idToken,
|
2022-10-18 20:43:59 +00:00
|
|
|
cap: acl.PolicyRead,
|
2022-07-19 20:17:34 +00:00
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web", jobID),
|
2022-06-10 13:41:54 +00:00
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid claim for path with job secret",
|
|
|
|
token: idToken,
|
2022-10-18 20:43:59 +00:00
|
|
|
cap: acl.PolicyRead,
|
2022-07-19 20:17:34 +00:00
|
|
|
path: fmt.Sprintf("nomad/jobs/%s", jobID),
|
2022-06-10 13:41:54 +00:00
|
|
|
expectedErr: nil,
|
|
|
|
},
|
2022-07-21 13:05:54 +00:00
|
|
|
{
|
|
|
|
name: "valid claim for path with dispatch job secret",
|
|
|
|
token: idDispatchToken,
|
2022-10-18 20:43:59 +00:00
|
|
|
cap: acl.PolicyRead,
|
2022-07-21 13:05:54 +00:00
|
|
|
path: fmt.Sprintf("nomad/jobs/%s", jobID),
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
2022-06-10 13:41:54 +00:00
|
|
|
{
|
|
|
|
name: "valid claim for path with namespace secret",
|
|
|
|
token: idToken,
|
2022-10-18 20:43:59 +00:00
|
|
|
cap: acl.PolicyRead,
|
2022-07-19 20:17:34 +00:00
|
|
|
path: "nomad/jobs",
|
2022-06-10 13:41:54 +00:00
|
|
|
expectedErr: nil,
|
|
|
|
},
|
2022-06-20 15:21:03 +00:00
|
|
|
{
|
2022-08-22 20:41:21 +00:00
|
|
|
name: "valid claim for job-attached policy",
|
2022-06-20 15:21:03 +00:00
|
|
|
token: idToken,
|
|
|
|
cap: acl.PolicyRead,
|
|
|
|
path: "other/path",
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
2022-08-22 20:41:21 +00:00
|
|
|
name: "valid claim for job-attached policy path denied",
|
2022-06-20 15:21:03 +00:00
|
|
|
token: idToken,
|
|
|
|
cap: acl.PolicyRead,
|
|
|
|
path: "other/not-allowed",
|
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
|
|
|
{
|
2022-08-22 20:41:21 +00:00
|
|
|
name: "valid claim for job-attached policy capability denied",
|
2022-06-20 15:21:03 +00:00
|
|
|
token: idToken,
|
|
|
|
cap: acl.PolicyWrite,
|
|
|
|
path: "other/path",
|
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
2022-08-22 20:41:21 +00:00
|
|
|
{
|
|
|
|
name: "valid claim for job-attached policy capability with cross-job access",
|
|
|
|
token: idToken,
|
|
|
|
cap: acl.PolicyList,
|
|
|
|
path: "nomad/jobs/some-other",
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
2022-06-20 15:21:03 +00:00
|
|
|
{
|
|
|
|
name: "valid claim with no permissions denied by path",
|
|
|
|
token: noPermissionsToken,
|
2022-10-18 20:43:59 +00:00
|
|
|
cap: acl.PolicyList,
|
2022-07-19 20:17:34 +00:00
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/w", jobID),
|
2022-06-20 15:21:03 +00:00
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid claim with no permissions allowed by namespace",
|
|
|
|
token: noPermissionsToken,
|
2022-10-18 20:43:59 +00:00
|
|
|
cap: acl.PolicyList,
|
2022-07-19 20:17:34 +00:00
|
|
|
path: "nomad/jobs",
|
2022-06-20 15:21:03 +00:00
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "valid claim with no permissions denied by capability",
|
|
|
|
token: noPermissionsToken,
|
|
|
|
cap: acl.PolicyRead,
|
2022-07-19 20:17:34 +00:00
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/w", jobID),
|
2022-06-20 15:21:03 +00:00
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
2022-06-10 13:41:54 +00:00
|
|
|
{
|
|
|
|
name: "missing auth token is denied",
|
2022-10-18 20:43:59 +00:00
|
|
|
cap: acl.PolicyList,
|
2022-07-19 20:17:34 +00:00
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web/web", jobID),
|
2022-06-10 13:41:54 +00:00
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid signature is denied",
|
|
|
|
token: invalidIDToken,
|
2022-10-18 20:43:59 +00:00
|
|
|
cap: acl.PolicyList,
|
2022-07-19 20:17:34 +00:00
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web/web", jobID),
|
2022-06-10 13:41:54 +00:00
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
2022-07-21 13:05:54 +00:00
|
|
|
{
|
|
|
|
name: "invalid claim for dispatched ID",
|
|
|
|
token: idDispatchToken,
|
2022-10-18 20:43:59 +00:00
|
|
|
cap: acl.PolicyList,
|
2022-07-21 13:05:54 +00:00
|
|
|
path: fmt.Sprintf("nomad/jobs/%s", alloc3.JobID),
|
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
2022-06-20 15:21:03 +00:00
|
|
|
{
|
|
|
|
name: "acl token read policy is allowed to list",
|
|
|
|
token: aclToken.SecretID,
|
|
|
|
cap: acl.PolicyList,
|
2022-07-19 20:17:34 +00:00
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web/web", jobID),
|
2022-06-20 15:21:03 +00:00
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "acl token read policy is not allowed to write",
|
|
|
|
token: aclToken.SecretID,
|
|
|
|
cap: acl.PolicyWrite,
|
2022-07-19 20:17:34 +00:00
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web/web", jobID),
|
2022-06-20 15:21:03 +00:00
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
2022-10-18 20:43:59 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
name: "WI token can read own task",
|
|
|
|
token: wiOnlyToken,
|
|
|
|
cap: acl.PolicyRead,
|
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web/web", alloc4.JobID),
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WI token can list own task",
|
|
|
|
token: wiOnlyToken,
|
|
|
|
cap: acl.PolicyList,
|
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web/web", alloc4.JobID),
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WI token can read own group",
|
|
|
|
token: wiOnlyToken,
|
|
|
|
cap: acl.PolicyRead,
|
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web", alloc4.JobID),
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WI token can list own group",
|
|
|
|
token: wiOnlyToken,
|
|
|
|
cap: acl.PolicyList,
|
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web", alloc4.JobID),
|
|
|
|
expectedErr: nil,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "WI token cannot read another task in group",
|
|
|
|
token: wiOnlyToken,
|
|
|
|
cap: acl.PolicyRead,
|
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web/other", alloc4.JobID),
|
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WI token cannot list another task in group",
|
|
|
|
token: wiOnlyToken,
|
|
|
|
cap: acl.PolicyList,
|
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web/other", alloc4.JobID),
|
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WI token cannot read another task in group",
|
|
|
|
token: wiOnlyToken,
|
|
|
|
cap: acl.PolicyRead,
|
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web/other", alloc4.JobID),
|
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WI token cannot list a task in another group",
|
|
|
|
token: wiOnlyToken,
|
|
|
|
cap: acl.PolicyRead,
|
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/other/web", alloc4.JobID),
|
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WI token cannot read a task in another group",
|
|
|
|
token: wiOnlyToken,
|
|
|
|
cap: acl.PolicyRead,
|
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/other/web", alloc4.JobID),
|
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WI token cannot read a group in another job",
|
|
|
|
token: wiOnlyToken,
|
|
|
|
cap: acl.PolicyRead,
|
|
|
|
path: "nomad/jobs/other/web/web",
|
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WI token cannot list a group in another job",
|
|
|
|
token: wiOnlyToken,
|
|
|
|
cap: acl.PolicyList,
|
|
|
|
path: "nomad/jobs/other/web/web",
|
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
|
|
name: "WI token extra trailing slash is denied",
|
|
|
|
token: wiOnlyToken,
|
|
|
|
cap: acl.PolicyList,
|
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/web/", alloc4.JobID),
|
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "WI token invalid prefix is denied",
|
|
|
|
token: wiOnlyToken,
|
|
|
|
cap: acl.PolicyList,
|
|
|
|
path: fmt.Sprintf("nomad/jobs/%s/w", alloc4.JobID),
|
|
|
|
expectedErr: structs.ErrPermissionDenied,
|
|
|
|
},
|
2022-06-10 13:41:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2023-01-24 15:52:07 +00:00
|
|
|
err := testFn(
|
|
|
|
&structs.QueryOptions{AuthToken: tc.token, Namespace: ns},
|
|
|
|
tc.cap, tc.path)
|
2022-06-10 13:41:54 +00:00
|
|
|
if tc.expectedErr == nil {
|
2022-08-15 15:19:53 +00:00
|
|
|
must.NoError(t, err)
|
2022-06-10 13:41:54 +00:00
|
|
|
} else {
|
2022-08-15 15:19:53 +00:00
|
|
|
must.EqError(t, err, tc.expectedErr.Error())
|
2022-06-10 13:41:54 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestVariablesEndpoint_Apply_ACL(t *testing.T) {
|
2022-08-15 15:19:53 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
srv, rootToken, shutdown := TestACLServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0 // Prevent automatic dequeue
|
|
|
|
})
|
|
|
|
defer shutdown()
|
|
|
|
testutil.WaitForLeader(t, srv.RPC)
|
|
|
|
codec := rpcClient(t, srv)
|
|
|
|
state := srv.fsm.State()
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
pol := mock.NamespacePolicyWithVariables(
|
2022-08-15 15:19:53 +00:00
|
|
|
structs.DefaultNamespace, "", []string{"list-jobs"},
|
|
|
|
map[string][]string{
|
|
|
|
"dropbox/*": {"write"},
|
|
|
|
})
|
|
|
|
writeToken := mock.CreatePolicyAndToken(t, state, 1003, "test-invalid", pol)
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
sv1 := mock.Variable()
|
2022-08-15 15:19:53 +00:00
|
|
|
sv1.ModifyIndex = 0
|
2022-08-26 18:03:56 +00:00
|
|
|
var svHold *structs.VariableDecrypted
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
opMap := map[string]structs.VarOp{
|
|
|
|
"set": structs.VarOpSet,
|
|
|
|
"cas": structs.VarOpCAS,
|
|
|
|
"delete": structs.VarOpDelete,
|
|
|
|
"delete-cas": structs.VarOpDeleteCAS,
|
2022-08-15 15:19:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for name, op := range opMap {
|
|
|
|
t.Run(name+"/no token", func(t *testing.T) {
|
|
|
|
sv1 := sv1
|
2022-08-26 18:03:56 +00:00
|
|
|
applyReq := structs.VariablesApplyRequest{
|
2022-08-15 15:19:53 +00:00
|
|
|
Op: op,
|
|
|
|
Var: sv1,
|
|
|
|
WriteRequest: structs.WriteRequest{Region: "global"},
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
applyResp := new(structs.VariablesApplyResponse)
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, structs.VariablesApplyRPCMethod, &applyReq, applyResp)
|
2022-08-15 15:19:53 +00:00
|
|
|
must.EqError(t, err, structs.ErrPermissionDenied.Error())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("cas/management token/new", func(t *testing.T) {
|
2022-08-26 18:03:56 +00:00
|
|
|
applyReq := structs.VariablesApplyRequest{
|
|
|
|
Op: structs.VarOpCAS,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv1,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: "global",
|
|
|
|
AuthToken: rootToken.SecretID,
|
|
|
|
},
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
applyResp := new(structs.VariablesApplyResponse)
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, structs.VariablesApplyRPCMethod, &applyReq, applyResp)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
must.NoError(t, err)
|
2022-08-26 18:03:56 +00:00
|
|
|
must.Eq(t, structs.VarOpResultOk, applyResp.Result)
|
2022-09-26 13:28:03 +00:00
|
|
|
must.Eq(t, sv1.Items, applyResp.Output.Items)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
svHold = applyResp.Output
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("cas with current", func(t *testing.T) {
|
|
|
|
must.NotNil(t, svHold)
|
|
|
|
sv := svHold
|
|
|
|
sv.Items["new"] = "newVal"
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
applyReq := structs.VariablesApplyRequest{
|
|
|
|
Op: structs.VarOpCAS,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: "global",
|
|
|
|
AuthToken: rootToken.SecretID,
|
|
|
|
},
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
applyResp := new(structs.VariablesApplyResponse)
|
2022-08-15 15:19:53 +00:00
|
|
|
applyReq.AuthToken = rootToken.SecretID
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, structs.VariablesApplyRPCMethod, &applyReq, &applyResp)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
must.NoError(t, err)
|
2022-08-26 18:03:56 +00:00
|
|
|
must.Eq(t, structs.VarOpResultOk, applyResp.Result)
|
2022-09-26 13:28:03 +00:00
|
|
|
must.Eq(t, sv.Items, applyResp.Output.Items)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
svHold = applyResp.Output
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("cas with stale", func(t *testing.T) {
|
|
|
|
must.NotNil(t, sv1) // TODO: query these directly
|
|
|
|
must.NotNil(t, svHold)
|
|
|
|
|
|
|
|
sv1 := sv1
|
|
|
|
svHold := svHold
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
applyReq := structs.VariablesApplyRequest{
|
|
|
|
Op: structs.VarOpCAS,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv1,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: "global",
|
|
|
|
AuthToken: rootToken.SecretID,
|
|
|
|
},
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
applyResp := new(structs.VariablesApplyResponse)
|
2022-08-15 15:19:53 +00:00
|
|
|
applyReq.AuthToken = rootToken.SecretID
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, structs.VariablesApplyRPCMethod, &applyReq, &applyResp)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
must.NoError(t, err)
|
2022-08-26 18:03:56 +00:00
|
|
|
must.Eq(t, structs.VarOpResultConflict, applyResp.Result)
|
2022-09-26 13:28:03 +00:00
|
|
|
must.Eq(t, svHold.VariableMetadata, applyResp.Conflict.VariableMetadata)
|
|
|
|
must.Eq(t, svHold.Items, applyResp.Conflict.Items)
|
2022-08-15 15:19:53 +00:00
|
|
|
})
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
sv3 := mock.Variable()
|
2022-08-15 15:19:53 +00:00
|
|
|
sv3.Path = "dropbox/a"
|
|
|
|
sv3.ModifyIndex = 0
|
|
|
|
|
|
|
|
t.Run("cas/write-only/read own new", func(t *testing.T) {
|
|
|
|
sv3 := sv3
|
2022-08-26 18:03:56 +00:00
|
|
|
applyReq := structs.VariablesApplyRequest{
|
|
|
|
Op: structs.VarOpCAS,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv3,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: "global",
|
|
|
|
AuthToken: writeToken.SecretID,
|
|
|
|
},
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
applyResp := new(structs.VariablesApplyResponse)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
err := msgpackrpc.CallWithCodec(codec, structs.VariablesApplyRPCMethod, &applyReq, &applyResp)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
must.NoError(t, err)
|
2022-08-26 18:03:56 +00:00
|
|
|
must.Eq(t, structs.VarOpResultOk, applyResp.Result)
|
2022-09-26 13:28:03 +00:00
|
|
|
must.Eq(t, sv3.Items, applyResp.Output.Items)
|
2022-08-15 15:19:53 +00:00
|
|
|
svHold = applyResp.Output
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("cas/write only/conflict redacted", func(t *testing.T) {
|
|
|
|
must.NotNil(t, sv3)
|
|
|
|
must.NotNil(t, svHold)
|
|
|
|
sv3 := sv3
|
|
|
|
svHold := svHold
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
applyReq := structs.VariablesApplyRequest{
|
|
|
|
Op: structs.VarOpCAS,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv3,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: "global",
|
|
|
|
AuthToken: writeToken.SecretID,
|
|
|
|
},
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
applyResp := new(structs.VariablesApplyResponse)
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, structs.VariablesApplyRPCMethod, &applyReq, &applyResp)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
must.NoError(t, err)
|
2022-08-26 18:03:56 +00:00
|
|
|
must.Eq(t, structs.VarOpResultRedacted, applyResp.Result)
|
|
|
|
must.Eq(t, svHold.VariableMetadata, applyResp.Conflict.VariableMetadata)
|
2022-08-15 15:19:53 +00:00
|
|
|
must.Nil(t, applyResp.Conflict.Items)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("cas/write only/read own upsert", func(t *testing.T) {
|
|
|
|
must.NotNil(t, svHold)
|
|
|
|
sv := svHold
|
|
|
|
sv.Items["upsert"] = "read"
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
applyReq := structs.VariablesApplyRequest{
|
|
|
|
Op: structs.VarOpCAS,
|
2022-08-15 15:19:53 +00:00
|
|
|
Var: sv,
|
|
|
|
WriteRequest: structs.WriteRequest{
|
|
|
|
Region: "global",
|
|
|
|
AuthToken: writeToken.SecretID,
|
|
|
|
},
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
applyResp := new(structs.VariablesApplyResponse)
|
|
|
|
err := msgpackrpc.CallWithCodec(codec, structs.VariablesApplyRPCMethod, &applyReq, &applyResp)
|
2022-08-15 15:19:53 +00:00
|
|
|
|
|
|
|
must.NoError(t, err)
|
2022-08-26 18:03:56 +00:00
|
|
|
must.Eq(t, structs.VarOpResultOk, applyResp.Result)
|
2022-09-26 13:28:03 +00:00
|
|
|
must.Eq(t, sv.Items, applyResp.Output.Items)
|
2022-08-15 15:19:53 +00:00
|
|
|
})
|
|
|
|
}
|
2022-08-15 15:38:20 +00:00
|
|
|
|
2022-10-18 20:43:59 +00:00
|
|
|
func TestVariablesEndpoint_ListFiltering(t *testing.T) {
|
|
|
|
ci.Parallel(t)
|
|
|
|
srv, _, shutdown := TestACLServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0 // Prevent automatic dequeue
|
|
|
|
})
|
|
|
|
defer shutdown()
|
|
|
|
testutil.WaitForLeader(t, srv.RPC)
|
|
|
|
codec := rpcClient(t, srv)
|
|
|
|
|
|
|
|
ns := "nondefault-namespace"
|
|
|
|
idx := uint64(1000)
|
|
|
|
|
|
|
|
alloc := mock.Alloc()
|
|
|
|
alloc.Job.ID = "job1"
|
|
|
|
alloc.JobID = "job1"
|
|
|
|
alloc.TaskGroup = "group"
|
|
|
|
alloc.Job.TaskGroups[0].Name = "group"
|
|
|
|
alloc.ClientStatus = structs.AllocClientStatusRunning
|
|
|
|
alloc.Job.Namespace = ns
|
|
|
|
alloc.Namespace = ns
|
|
|
|
|
|
|
|
store := srv.fsm.State()
|
|
|
|
must.NoError(t, store.UpsertNamespaces(idx, []*structs.Namespace{{Name: ns}}))
|
|
|
|
idx++
|
|
|
|
must.NoError(t, store.UpsertAllocs(
|
|
|
|
structs.MsgTypeTestSetup, idx, []*structs.Allocation{alloc}))
|
|
|
|
|
|
|
|
claims := alloc.ToTaskIdentityClaims(alloc.Job, "web")
|
2022-11-01 19:00:50 +00:00
|
|
|
token, _, err := srv.encrypter.SignClaims(claims)
|
2022-10-18 20:43:59 +00:00
|
|
|
must.NoError(t, err)
|
|
|
|
|
|
|
|
writeVar := func(ns, path string) {
|
|
|
|
idx++
|
|
|
|
sv := mock.VariableEncrypted()
|
|
|
|
sv.Namespace = ns
|
|
|
|
sv.Path = path
|
|
|
|
resp := store.VarSet(idx, &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
|
|
|
Var: sv,
|
|
|
|
})
|
|
|
|
must.NoError(t, resp.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
writeVar(ns, "nomad/jobs/job1/group/web")
|
|
|
|
writeVar(ns, "nomad/jobs/job1/group")
|
|
|
|
writeVar(ns, "nomad/jobs/job1")
|
|
|
|
|
|
|
|
writeVar(ns, "nomad/jobs/job1/group/other")
|
|
|
|
writeVar(ns, "nomad/jobs/job1/other/web")
|
|
|
|
writeVar(ns, "nomad/jobs/job2/group/web")
|
|
|
|
|
|
|
|
req := &structs.VariablesListRequest{
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Namespace: ns,
|
|
|
|
Prefix: "nomad",
|
|
|
|
AuthToken: token,
|
|
|
|
Region: "global",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
var resp structs.VariablesListResponse
|
|
|
|
must.NoError(t, msgpackrpc.CallWithCodec(codec, "Variables.List", req, &resp))
|
|
|
|
found := []string{}
|
|
|
|
for _, variable := range resp.Data {
|
|
|
|
found = append(found, variable.Path)
|
|
|
|
}
|
|
|
|
expect := []string{
|
|
|
|
"nomad/jobs/job1",
|
|
|
|
"nomad/jobs/job1/group",
|
|
|
|
"nomad/jobs/job1/group/web",
|
|
|
|
}
|
|
|
|
must.Eq(t, expect, found)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestVariablesEndpoint_ComplexACLPolicies(t *testing.T) {
|
2022-08-15 15:38:20 +00:00
|
|
|
|
|
|
|
ci.Parallel(t)
|
|
|
|
srv, _, shutdown := TestACLServer(t, func(c *Config) {
|
|
|
|
c.NumSchedulers = 0 // Prevent automatic dequeue
|
|
|
|
})
|
|
|
|
defer shutdown()
|
|
|
|
testutil.WaitForLeader(t, srv.RPC)
|
|
|
|
codec := rpcClient(t, srv)
|
|
|
|
|
|
|
|
idx := uint64(1000)
|
|
|
|
|
|
|
|
policyRules := `
|
|
|
|
namespace "dev" {
|
2022-08-26 18:03:56 +00:00
|
|
|
variables {
|
2022-08-15 15:38:20 +00:00
|
|
|
path "*" { capabilities = ["list", "read"] }
|
|
|
|
path "system/*" { capabilities = ["deny"] }
|
|
|
|
path "config/system/*" { capabilities = ["deny"] }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace "prod" {
|
2022-08-26 18:03:56 +00:00
|
|
|
variables {
|
2022-08-15 15:38:20 +00:00
|
|
|
path "*" {
|
|
|
|
capabilities = ["list"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace "*" {}
|
|
|
|
`
|
|
|
|
|
|
|
|
store := srv.fsm.State()
|
|
|
|
|
|
|
|
must.NoError(t, store.UpsertNamespaces(1000, []*structs.Namespace{
|
|
|
|
{Name: "dev"}, {Name: "prod"}, {Name: "other"}}))
|
|
|
|
|
|
|
|
idx++
|
|
|
|
token := mock.CreatePolicyAndToken(t, store, idx, "developer", policyRules)
|
|
|
|
|
|
|
|
writeVar := func(ns, path string) {
|
|
|
|
idx++
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := mock.VariableEncrypted()
|
2022-08-15 15:38:20 +00:00
|
|
|
sv.Namespace = ns
|
|
|
|
sv.Path = path
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := store.VarSet(idx, &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
2022-08-15 15:38:20 +00:00
|
|
|
Var: sv,
|
|
|
|
})
|
|
|
|
must.NoError(t, resp.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
writeVar("dev", "system/never-list")
|
|
|
|
writeVar("dev", "config/system/never-list")
|
|
|
|
writeVar("dev", "config/can-read")
|
|
|
|
writeVar("dev", "project/can-read")
|
|
|
|
|
|
|
|
writeVar("prod", "system/can-list")
|
|
|
|
writeVar("prod", "config/system/can-list")
|
|
|
|
writeVar("prod", "config/can-list")
|
|
|
|
writeVar("prod", "project/can-list")
|
|
|
|
|
|
|
|
writeVar("other", "system/never-list")
|
|
|
|
writeVar("other", "config/system/never-list")
|
|
|
|
writeVar("other", "config/never-list")
|
|
|
|
writeVar("other", "project/never-list")
|
|
|
|
|
|
|
|
testListPrefix := func(ns, prefix string, expectedCount int, expectErr error) {
|
|
|
|
t.Run(fmt.Sprintf("ns=%s-prefix=%s", ns, prefix), func(t *testing.T) {
|
2022-08-26 18:03:56 +00:00
|
|
|
req := &structs.VariablesListRequest{
|
2022-08-15 15:38:20 +00:00
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Namespace: ns,
|
|
|
|
Prefix: prefix,
|
|
|
|
AuthToken: token.SecretID,
|
|
|
|
Region: "global",
|
|
|
|
},
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
var resp structs.VariablesListResponse
|
2022-08-15 15:38:20 +00:00
|
|
|
|
|
|
|
if expectErr != nil {
|
|
|
|
must.EqError(t,
|
2022-08-26 18:03:56 +00:00
|
|
|
msgpackrpc.CallWithCodec(codec, "Variables.List", req, &resp),
|
2022-08-15 15:38:20 +00:00
|
|
|
expectErr.Error())
|
|
|
|
return
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
must.NoError(t, msgpackrpc.CallWithCodec(codec, "Variables.List", req, &resp))
|
2022-08-15 15:38:20 +00:00
|
|
|
|
|
|
|
found := "found:\n"
|
|
|
|
for _, sv := range resp.Data {
|
|
|
|
found += fmt.Sprintf(" ns=%s path=%s\n", sv.Namespace, sv.Path)
|
|
|
|
}
|
2023-01-09 15:37:08 +00:00
|
|
|
must.Len(t, expectedCount, resp.Data, must.Sprintf("%s", found))
|
2022-08-15 15:38:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
testListPrefix("dev", "system", 0, nil)
|
|
|
|
testListPrefix("dev", "config/system", 0, nil)
|
|
|
|
testListPrefix("dev", "config", 1, nil)
|
|
|
|
testListPrefix("dev", "project", 1, nil)
|
|
|
|
testListPrefix("dev", "", 2, nil)
|
|
|
|
|
|
|
|
testListPrefix("prod", "system", 1, nil)
|
|
|
|
testListPrefix("prod", "config/system", 1, nil)
|
|
|
|
testListPrefix("prod", "config", 2, nil)
|
|
|
|
testListPrefix("prod", "project", 1, nil)
|
|
|
|
testListPrefix("prod", "", 4, nil)
|
|
|
|
|
2022-10-18 20:43:59 +00:00
|
|
|
// list gives empty but no error!
|
|
|
|
testListPrefix("other", "system", 0, nil)
|
|
|
|
testListPrefix("other", "config/system", 0, nil)
|
|
|
|
testListPrefix("other", "config", 0, nil)
|
|
|
|
testListPrefix("other", "project", 0, nil)
|
|
|
|
testListPrefix("other", "", 0, nil)
|
2022-08-15 15:38:20 +00:00
|
|
|
|
|
|
|
testListPrefix("*", "system", 1, nil)
|
|
|
|
testListPrefix("*", "config/system", 1, nil)
|
|
|
|
testListPrefix("*", "config", 3, nil)
|
|
|
|
testListPrefix("*", "project", 2, nil)
|
|
|
|
testListPrefix("*", "", 6, nil)
|
|
|
|
|
|
|
|
}
|
2022-08-22 20:37:21 +00:00
|
|
|
|
2022-08-26 18:03:56 +00:00
|
|
|
func TestVariablesEndpoint_GetVariable_Blocking(t *testing.T) {
|
2022-08-22 20:37:21 +00:00
|
|
|
ci.Parallel(t)
|
|
|
|
|
|
|
|
s1, cleanupS1 := TestServer(t, nil)
|
|
|
|
defer cleanupS1()
|
|
|
|
state := s1.fsm.State()
|
|
|
|
codec := rpcClient(t, s1)
|
|
|
|
testutil.WaitForLeader(t, s1.RPC)
|
|
|
|
|
|
|
|
// First create an unrelated variable.
|
|
|
|
delay := 100 * time.Millisecond
|
|
|
|
time.AfterFunc(delay, func() {
|
|
|
|
writeVar(t, s1, 100, "default", "aaa")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Upsert the variable we are watching later
|
|
|
|
delay = 200 * time.Millisecond
|
|
|
|
time.AfterFunc(delay, func() {
|
|
|
|
writeVar(t, s1, 200, "default", "bbb")
|
|
|
|
})
|
|
|
|
|
|
|
|
// Lookup the variable
|
2022-08-26 18:03:56 +00:00
|
|
|
req := &structs.VariablesReadRequest{
|
2022-08-22 20:37:21 +00:00
|
|
|
Path: "bbb",
|
|
|
|
QueryOptions: structs.QueryOptions{
|
|
|
|
Region: "global",
|
|
|
|
MinQueryIndex: 150,
|
|
|
|
MaxQueryTime: 500 * time.Millisecond,
|
|
|
|
},
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
var resp structs.VariablesReadResponse
|
2022-08-22 20:37:21 +00:00
|
|
|
start := time.Now()
|
2022-08-26 18:03:56 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Variables.Read", req, &resp); err != nil {
|
2022-08-22 20:37:21 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
|
|
|
|
if elapsed < delay {
|
|
|
|
t.Fatalf("should block (returned in %s) %#v", elapsed, resp)
|
|
|
|
}
|
|
|
|
if elapsed > req.MaxQueryTime {
|
|
|
|
t.Fatalf("blocking query timed out %#v", resp)
|
|
|
|
}
|
|
|
|
if resp.Index != 200 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp.Index, 200)
|
|
|
|
}
|
|
|
|
if resp.Data == nil || resp.Data.Path != "bbb" {
|
|
|
|
t.Fatalf("bad: %#v", resp.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variable update triggers watches
|
|
|
|
delay = 100 * time.Millisecond
|
|
|
|
|
|
|
|
time.AfterFunc(delay, func() {
|
|
|
|
writeVar(t, s1, 300, "default", "bbb")
|
|
|
|
})
|
|
|
|
|
|
|
|
req.QueryOptions.MinQueryIndex = 250
|
2022-08-26 18:03:56 +00:00
|
|
|
var resp2 structs.VariablesReadResponse
|
2022-08-22 20:37:21 +00:00
|
|
|
start = time.Now()
|
2022-08-26 18:03:56 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Variables.Read", req, &resp2); err != nil {
|
2022-08-22 20:37:21 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
elapsed = time.Since(start)
|
|
|
|
|
|
|
|
if elapsed < delay {
|
|
|
|
t.Fatalf("should block (returned in %s) %#v", elapsed, resp2)
|
|
|
|
}
|
|
|
|
if elapsed > req.MaxQueryTime {
|
|
|
|
t.Fatal("blocking query timed out")
|
|
|
|
}
|
|
|
|
if resp2.Index != 300 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp2.Index, 300)
|
|
|
|
}
|
|
|
|
if resp2.Data == nil || resp2.Data.Path != "bbb" {
|
|
|
|
t.Fatalf("bad: %#v", resp2.Data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Variable delete triggers watches
|
|
|
|
delay = 100 * time.Millisecond
|
|
|
|
time.AfterFunc(delay, func() {
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := mock.VariableEncrypted()
|
2022-08-22 20:37:21 +00:00
|
|
|
sv.Path = "bbb"
|
2022-08-26 18:03:56 +00:00
|
|
|
if resp := state.VarDelete(400, &structs.VarApplyStateRequest{Op: structs.VarOpDelete, Var: sv}); !resp.IsOk() {
|
2022-08-22 20:37:21 +00:00
|
|
|
t.Fatalf("err: %v", resp.Error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
req.QueryOptions.MinQueryIndex = 350
|
2022-08-26 18:03:56 +00:00
|
|
|
var resp3 structs.VariablesReadResponse
|
2022-08-22 20:37:21 +00:00
|
|
|
start = time.Now()
|
2022-08-26 18:03:56 +00:00
|
|
|
if err := msgpackrpc.CallWithCodec(codec, "Variables.Read", req, &resp3); err != nil {
|
2022-08-22 20:37:21 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
elapsed = time.Since(start)
|
|
|
|
|
|
|
|
if elapsed < delay {
|
|
|
|
t.Fatalf("should block (returned in %s) %#v", elapsed, resp)
|
|
|
|
}
|
|
|
|
if elapsed > req.MaxQueryTime {
|
|
|
|
t.Fatal("blocking query timed out")
|
|
|
|
}
|
|
|
|
if resp3.Index != 400 {
|
|
|
|
t.Fatalf("Bad index: %d %d", resp3.Index, 400)
|
|
|
|
}
|
|
|
|
if resp3.Data != nil {
|
|
|
|
t.Fatalf("bad: %#v", resp3.Data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeVar(t *testing.T, s *Server, idx uint64, ns, path string) {
|
|
|
|
store := s.fsm.State()
|
2022-08-26 18:03:56 +00:00
|
|
|
sv := mock.Variable()
|
2022-08-22 20:37:21 +00:00
|
|
|
sv.Namespace = ns
|
|
|
|
sv.Path = path
|
|
|
|
bPlain, err := json.Marshal(sv.Items)
|
|
|
|
must.NoError(t, err)
|
|
|
|
bEnc, kID, err := s.encrypter.Encrypt(bPlain)
|
|
|
|
must.NoError(t, err)
|
2022-08-26 18:03:56 +00:00
|
|
|
sve := &structs.VariableEncrypted{
|
|
|
|
VariableMetadata: sv.VariableMetadata,
|
|
|
|
VariableData: structs.VariableData{
|
2022-08-22 20:37:21 +00:00
|
|
|
Data: bEnc,
|
|
|
|
KeyID: kID,
|
|
|
|
},
|
|
|
|
}
|
2022-08-26 18:03:56 +00:00
|
|
|
resp := store.VarSet(idx, &structs.VarApplyStateRequest{
|
|
|
|
Op: structs.VarOpSet,
|
2022-08-22 20:37:21 +00:00
|
|
|
Var: sve,
|
|
|
|
})
|
|
|
|
must.NoError(t, resp.Error)
|
|
|
|
}
|