2023-03-28 22:48:58 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 22:48:58 +00:00
|
|
|
|
2023-03-27 15:35:39 +00:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-22 14:53:55 +00:00
|
|
|
"fmt"
|
2023-10-16 17:55:30 +00:00
|
|
|
"strings"
|
2023-09-22 14:53:55 +00:00
|
|
|
"sync"
|
2023-03-27 15:35:39 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/mock"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"google.golang.org/grpc/status"
|
2023-08-07 21:37:03 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2023-03-27 15:35:39 +00:00
|
|
|
|
2023-09-22 14:53:55 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2023-04-11 11:10:14 +00:00
|
|
|
"github.com/hashicorp/consul/acl/resolver"
|
2023-09-22 14:53:55 +00:00
|
|
|
"github.com/hashicorp/consul/agent/grpc-external/testutils"
|
2023-03-27 15:35:39 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
2023-04-06 09:40:04 +00:00
|
|
|
"github.com/hashicorp/consul/internal/resource/demo"
|
2023-03-27 15:35:39 +00:00
|
|
|
"github.com/hashicorp/consul/internal/storage"
|
2023-10-10 18:30:23 +00:00
|
|
|
"github.com/hashicorp/consul/internal/tenancy"
|
2023-03-27 15:35:39 +00:00
|
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
|
|
"github.com/hashicorp/consul/proto/private/prototest"
|
2023-09-22 14:53:55 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2023-03-27 15:35:39 +00:00
|
|
|
)
|
|
|
|
|
2023-04-17 21:33:20 +00:00
|
|
|
func TestRead_InputValidation(t *testing.T) {
|
|
|
|
server := testServer(t)
|
|
|
|
client := testClient(t, server)
|
2023-10-10 18:30:23 +00:00
|
|
|
tenancy.RegisterTypes(server.Registry)
|
2023-04-25 11:52:35 +00:00
|
|
|
demo.RegisterTypes(server.Registry)
|
2023-04-17 21:33:20 +00:00
|
|
|
|
2023-10-16 17:55:30 +00:00
|
|
|
type testCase struct {
|
|
|
|
modFn func(artistId, recordlabelId, executiveId *pbresource.ID) *pbresource.ID
|
|
|
|
errContains string
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]testCase{
|
|
|
|
"no id": {
|
|
|
|
modFn: func(_, _, _ *pbresource.ID) *pbresource.ID {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
errContains: "id is required",
|
|
|
|
},
|
|
|
|
"no type": {
|
|
|
|
modFn: func(artistId, _, _ *pbresource.ID) *pbresource.ID {
|
|
|
|
artistId.Type = nil
|
|
|
|
return artistId
|
|
|
|
},
|
|
|
|
errContains: "id.type is required",
|
|
|
|
},
|
|
|
|
"no name": {
|
|
|
|
modFn: func(artistId, _, _ *pbresource.ID) *pbresource.ID {
|
|
|
|
artistId.Name = ""
|
|
|
|
return artistId
|
|
|
|
},
|
|
|
|
errContains: "id.name invalid",
|
|
|
|
},
|
|
|
|
"name is mixed case": {
|
|
|
|
modFn: func(artistId, _, _ *pbresource.ID) *pbresource.ID {
|
|
|
|
artistId.Name = "MixedCaseNotAllowed"
|
|
|
|
return artistId
|
|
|
|
},
|
|
|
|
errContains: "id.name invalid",
|
2023-08-07 21:37:03 +00:00
|
|
|
},
|
2023-10-16 17:55:30 +00:00
|
|
|
"name too long": {
|
|
|
|
modFn: func(artistId, _, _ *pbresource.ID) *pbresource.ID {
|
|
|
|
artistId.Name = strings.Repeat("a", resource.MaxNameLength+1)
|
|
|
|
return artistId
|
|
|
|
},
|
|
|
|
errContains: "id.name invalid",
|
2023-04-17 21:33:20 +00:00
|
|
|
},
|
2023-10-16 17:55:30 +00:00
|
|
|
"partition is mixed case": {
|
|
|
|
modFn: func(artistId, _, _ *pbresource.ID) *pbresource.ID {
|
|
|
|
artistId.Tenancy.Partition = "Default"
|
|
|
|
return artistId
|
|
|
|
},
|
|
|
|
errContains: "id.tenancy.partition invalid",
|
2023-04-17 21:33:20 +00:00
|
|
|
},
|
2023-10-16 17:55:30 +00:00
|
|
|
"partition too long": {
|
|
|
|
modFn: func(artistId, _, _ *pbresource.ID) *pbresource.ID {
|
|
|
|
artistId.Tenancy.Partition = strings.Repeat("p", resource.MaxNameLength+1)
|
|
|
|
return artistId
|
|
|
|
},
|
|
|
|
errContains: "id.tenancy.partition invalid",
|
2023-10-10 18:30:23 +00:00
|
|
|
},
|
2023-10-16 17:55:30 +00:00
|
|
|
"namespace is mixed case": {
|
|
|
|
modFn: func(artistId, _, _ *pbresource.ID) *pbresource.ID {
|
|
|
|
artistId.Tenancy.Namespace = "Default"
|
|
|
|
return artistId
|
|
|
|
},
|
|
|
|
errContains: "id.tenancy.namespace invalid",
|
|
|
|
},
|
|
|
|
"namespace too long": {
|
|
|
|
modFn: func(artistId, _, _ *pbresource.ID) *pbresource.ID {
|
|
|
|
artistId.Tenancy.Namespace = strings.Repeat("n", resource.MaxNameLength+1)
|
|
|
|
return artistId
|
|
|
|
},
|
|
|
|
errContains: "id.tenancy.namespace invalid",
|
|
|
|
},
|
|
|
|
"partition scope with non-empty namespace": {
|
|
|
|
modFn: func(_, recordLabelId, _ *pbresource.ID) *pbresource.ID {
|
|
|
|
recordLabelId.Tenancy.Namespace = "ishouldnothaveanamespace"
|
|
|
|
return recordLabelId
|
|
|
|
},
|
|
|
|
errContains: "cannot have a namespace",
|
|
|
|
},
|
|
|
|
"cluster scope with non-empty partition": {
|
|
|
|
modFn: func(_, _, executiveId *pbresource.ID) *pbresource.ID {
|
|
|
|
executiveId.Tenancy = &pbresource.Tenancy{Partition: resource.DefaultPartitionName}
|
|
|
|
return executiveId
|
|
|
|
},
|
|
|
|
errContains: "cannot have a partition",
|
|
|
|
},
|
|
|
|
"cluster scope with non-empty namespace": {
|
|
|
|
modFn: func(_, _, executiveId *pbresource.ID) *pbresource.ID {
|
|
|
|
executiveId.Tenancy = &pbresource.Tenancy{Namespace: resource.DefaultNamespaceName}
|
|
|
|
return executiveId
|
|
|
|
},
|
|
|
|
errContains: "cannot have a namespace",
|
2023-10-10 18:30:23 +00:00
|
|
|
},
|
2023-04-17 21:33:20 +00:00
|
|
|
}
|
2023-10-16 17:55:30 +00:00
|
|
|
for desc, tc := range testCases {
|
2023-04-17 21:33:20 +00:00
|
|
|
t.Run(desc, func(t *testing.T) {
|
2023-08-07 21:37:03 +00:00
|
|
|
artist, err := demo.GenerateV2Artist()
|
2023-04-17 21:33:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-10-16 17:55:30 +00:00
|
|
|
recordLabel, err := demo.GenerateV1RecordLabel("looney-tunes")
|
2023-08-07 21:37:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-10-16 17:55:30 +00:00
|
|
|
executive, err := demo.GenerateV1Executive("music-man", "CEO")
|
2023-10-10 18:30:23 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-08-07 21:37:03 +00:00
|
|
|
// Each test case picks which resource to use based on the resource type's scope.
|
2023-10-16 17:55:30 +00:00
|
|
|
req := &pbresource.ReadRequest{Id: tc.modFn(artist.Id, recordLabel.Id, executive.Id)}
|
2023-04-17 21:33:20 +00:00
|
|
|
|
|
|
|
_, err = client.Read(testContext(t), req)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, codes.InvalidArgument.String(), status.Code(err).String())
|
2023-10-16 17:55:30 +00:00
|
|
|
require.ErrorContains(t, err, tc.errContains)
|
2023-04-17 21:33:20 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-27 15:35:39 +00:00
|
|
|
func TestRead_TypeNotFound(t *testing.T) {
|
2023-04-06 09:40:04 +00:00
|
|
|
server := NewServer(Config{Registry: resource.NewRegistry()})
|
2023-03-27 15:35:39 +00:00
|
|
|
client := testClient(t, server)
|
|
|
|
|
2023-04-06 09:40:04 +00:00
|
|
|
artist, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = client.Read(context.Background(), &pbresource.ReadRequest{Id: artist.Id})
|
2023-03-27 15:35:39 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, codes.InvalidArgument.String(), status.Code(err).String())
|
2023-06-26 12:25:14 +00:00
|
|
|
require.Contains(t, err.Error(), "resource type demo.v2.Artist not registered")
|
2023-03-27 15:35:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRead_ResourceNotFound(t *testing.T) {
|
|
|
|
for desc, tc := range readTestCases() {
|
|
|
|
t.Run(desc, func(t *testing.T) {
|
2023-10-16 17:55:30 +00:00
|
|
|
type tenancyCase struct {
|
|
|
|
modFn func(artistId, recordlabelId *pbresource.ID) *pbresource.ID
|
|
|
|
errContains string
|
|
|
|
}
|
|
|
|
tenancyCases := map[string]tenancyCase{
|
|
|
|
"resource not found by name": {
|
|
|
|
modFn: func(artistId, _ *pbresource.ID) *pbresource.ID {
|
|
|
|
artistId.Name = "bogusname"
|
|
|
|
return artistId
|
|
|
|
},
|
|
|
|
errContains: "resource not found",
|
2023-08-07 21:37:03 +00:00
|
|
|
},
|
2023-10-16 17:55:30 +00:00
|
|
|
"partition not found when namespace scoped": {
|
|
|
|
modFn: func(artistId, _ *pbresource.ID) *pbresource.ID {
|
|
|
|
id := clone(artistId)
|
|
|
|
id.Tenancy.Partition = "boguspartition"
|
|
|
|
return id
|
|
|
|
},
|
|
|
|
errContains: "partition not found",
|
2023-08-07 21:37:03 +00:00
|
|
|
},
|
2023-10-16 17:55:30 +00:00
|
|
|
"namespace not found when namespace scoped": {
|
|
|
|
modFn: func(artistId, _ *pbresource.ID) *pbresource.ID {
|
|
|
|
id := clone(artistId)
|
|
|
|
id.Tenancy.Namespace = "bogusnamespace"
|
|
|
|
return id
|
|
|
|
},
|
|
|
|
errContains: "namespace not found",
|
2023-08-07 21:37:03 +00:00
|
|
|
},
|
2023-10-16 17:55:30 +00:00
|
|
|
"partition not found when partition scoped": {
|
|
|
|
modFn: func(_, recordLabelId *pbresource.ID) *pbresource.ID {
|
|
|
|
id := clone(recordLabelId)
|
|
|
|
id.Tenancy.Partition = "boguspartition"
|
|
|
|
return id
|
|
|
|
},
|
|
|
|
errContains: "partition not found",
|
2023-08-07 21:37:03 +00:00
|
|
|
},
|
|
|
|
}
|
2023-10-16 17:55:30 +00:00
|
|
|
for tenancyDesc, tenancyCase := range tenancyCases {
|
2023-08-07 21:37:03 +00:00
|
|
|
t.Run(tenancyDesc, func(t *testing.T) {
|
|
|
|
server := testServer(t)
|
|
|
|
demo.RegisterTypes(server.Registry)
|
|
|
|
client := testClient(t, server)
|
|
|
|
|
2023-10-16 17:55:30 +00:00
|
|
|
recordLabel, err := demo.GenerateV1RecordLabel("looney-tunes")
|
2023-08-07 21:37:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
recordLabel, err = server.Backend.WriteCAS(tc.ctx, recordLabel)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
artist, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
artist, err = server.Backend.WriteCAS(tc.ctx, artist)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Each tenancy test case picks which resource to use based on the resource type's scope.
|
2023-10-16 17:55:30 +00:00
|
|
|
_, err = client.Read(tc.ctx, &pbresource.ReadRequest{Id: tenancyCase.modFn(artist.Id, recordLabel.Id)})
|
2023-08-07 21:37:03 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, codes.NotFound.String(), status.Code(err).String())
|
2023-10-16 17:55:30 +00:00
|
|
|
require.ErrorContains(t, err, tenancyCase.errContains)
|
2023-08-07 21:37:03 +00:00
|
|
|
})
|
|
|
|
}
|
2023-03-27 15:35:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRead_GroupVersionMismatch(t *testing.T) {
|
|
|
|
for desc, tc := range readTestCases() {
|
|
|
|
t.Run(desc, func(t *testing.T) {
|
|
|
|
server := testServer(t)
|
2023-04-11 11:10:14 +00:00
|
|
|
|
2023-04-25 11:52:35 +00:00
|
|
|
demo.RegisterTypes(server.Registry)
|
2023-03-27 15:35:39 +00:00
|
|
|
client := testClient(t, server)
|
|
|
|
|
2023-04-06 09:40:04 +00:00
|
|
|
artist, err := demo.GenerateV2Artist()
|
2023-03-27 15:35:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-04-06 09:40:04 +00:00
|
|
|
_, err = server.Backend.WriteCAS(tc.ctx, artist)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
id := clone(artist.Id)
|
|
|
|
id.Type = demo.TypeV1Artist
|
|
|
|
|
|
|
|
_, err = client.Read(tc.ctx, &pbresource.ReadRequest{Id: id})
|
2023-03-27 15:35:39 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Equal(t, codes.InvalidArgument.String(), status.Code(err).String())
|
|
|
|
require.Contains(t, err.Error(), "resource was requested with GroupVersion")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRead_Success(t *testing.T) {
|
|
|
|
for desc, tc := range readTestCases() {
|
|
|
|
t.Run(desc, func(t *testing.T) {
|
2023-08-16 16:44:10 +00:00
|
|
|
for tenancyDesc, modFn := range tenancyCases() {
|
2023-08-07 21:37:03 +00:00
|
|
|
t.Run(tenancyDesc, func(t *testing.T) {
|
|
|
|
server := testServer(t)
|
|
|
|
demo.RegisterTypes(server.Registry)
|
|
|
|
client := testClient(t, server)
|
|
|
|
|
2023-10-16 17:55:30 +00:00
|
|
|
recordLabel, err := demo.GenerateV1RecordLabel("looney-tunes")
|
2023-08-07 21:37:03 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
recordLabel, err = server.Backend.WriteCAS(tc.ctx, recordLabel)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
artist, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
artist, err = server.Backend.WriteCAS(tc.ctx, artist)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Each tenancy test case picks which resource to use based on the resource type's scope.
|
|
|
|
req := &pbresource.ReadRequest{Id: modFn(artist.Id, recordLabel.Id)}
|
|
|
|
rsp, err := client.Read(tc.ctx, req)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case proto.Equal(rsp.Resource.Id.Type, demo.TypeV2Artist):
|
|
|
|
prototest.AssertDeepEqual(t, artist, rsp.Resource)
|
|
|
|
case proto.Equal(rsp.Resource.Id.Type, demo.TypeV1RecordLabel):
|
|
|
|
prototest.AssertDeepEqual(t, recordLabel, rsp.Resource)
|
|
|
|
default:
|
|
|
|
require.Fail(t, "unexpected resource type")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-03-27 15:35:39 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRead_VerifyReadConsistencyArg(t *testing.T) {
|
|
|
|
// Uses a mockBackend instead of the inmem Backend to verify the ReadConsistency argument is set correctly.
|
|
|
|
for desc, tc := range readTestCases() {
|
|
|
|
t.Run(desc, func(t *testing.T) {
|
2023-04-11 11:10:14 +00:00
|
|
|
server := testServer(t)
|
2023-03-27 15:35:39 +00:00
|
|
|
mockBackend := NewMockBackend(t)
|
2023-04-11 11:10:14 +00:00
|
|
|
server.Backend = mockBackend
|
2023-04-25 11:52:35 +00:00
|
|
|
demo.RegisterTypes(server.Registry)
|
2023-04-06 09:40:04 +00:00
|
|
|
|
|
|
|
artist, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
mockBackend.On("Read", mock.Anything, mock.Anything, mock.Anything).Return(artist, nil)
|
2023-03-27 15:35:39 +00:00
|
|
|
client := testClient(t, server)
|
|
|
|
|
2023-04-06 09:40:04 +00:00
|
|
|
rsp, err := client.Read(tc.ctx, &pbresource.ReadRequest{Id: artist.Id})
|
2023-03-27 15:35:39 +00:00
|
|
|
require.NoError(t, err)
|
2023-04-06 09:40:04 +00:00
|
|
|
prototest.AssertDeepEqual(t, artist, rsp.Resource)
|
2023-03-27 15:35:39 +00:00
|
|
|
mockBackend.AssertCalled(t, "Read", mock.Anything, tc.consistency, mock.Anything)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-25 11:52:35 +00:00
|
|
|
// N.B. Uses key ACLs for now. See demo.RegisterTypes()
|
2023-04-11 11:10:14 +00:00
|
|
|
func TestRead_ACLs(t *testing.T) {
|
|
|
|
type testCase struct {
|
2023-09-22 14:53:55 +00:00
|
|
|
res *pbresource.Resource
|
|
|
|
authz resolver.Result
|
|
|
|
codeNotExist codes.Code
|
|
|
|
codeExists codes.Code
|
2023-04-11 11:10:14 +00:00
|
|
|
}
|
2023-09-22 14:53:55 +00:00
|
|
|
|
|
|
|
artist, err := demo.GenerateV2Artist()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
label, err := demo.GenerateV1RecordLabel("blink1982")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
testcases := map[string]testCase{
|
2023-09-22 14:53:55 +00:00
|
|
|
"artist-v1/read hook denied": {
|
|
|
|
res: artist,
|
|
|
|
authz: AuthorizerFrom(t, demo.ArtistV1ReadPolicy),
|
|
|
|
codeNotExist: codes.PermissionDenied,
|
|
|
|
codeExists: codes.PermissionDenied,
|
|
|
|
},
|
|
|
|
"artist-v2/read hook allowed": {
|
|
|
|
res: artist,
|
|
|
|
authz: AuthorizerFrom(t, demo.ArtistV2ReadPolicy),
|
|
|
|
codeNotExist: codes.NotFound,
|
|
|
|
codeExists: codes.OK,
|
2023-04-11 11:10:14 +00:00
|
|
|
},
|
2023-09-22 14:53:55 +00:00
|
|
|
// Labels have the read ACL that requires reading the data.
|
|
|
|
"label-v1/read hook denied": {
|
|
|
|
res: label,
|
|
|
|
authz: AuthorizerFrom(t, demo.LabelV1ReadPolicy),
|
|
|
|
codeNotExist: codes.NotFound,
|
|
|
|
codeExists: codes.PermissionDenied,
|
2023-04-11 11:10:14 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-09-22 14:53:55 +00:00
|
|
|
adminAuthz := AuthorizerFrom(t, `key_prefix "" { policy = "write" }`)
|
|
|
|
|
|
|
|
idx := 0
|
|
|
|
nextTokenContext := func(t *testing.T) context.Context {
|
|
|
|
// Each query should use a distinct token string to avoid caching so we can
|
|
|
|
// change the behavior each call.
|
|
|
|
token := fmt.Sprintf("token-%d", idx)
|
|
|
|
idx++
|
|
|
|
//nolint:staticcheck
|
|
|
|
return context.WithValue(testContext(t), "x-consul-token", token)
|
|
|
|
}
|
|
|
|
|
2023-04-11 11:10:14 +00:00
|
|
|
for desc, tc := range testcases {
|
|
|
|
t.Run(desc, func(t *testing.T) {
|
|
|
|
server := testServer(t)
|
|
|
|
client := testClient(t, server)
|
|
|
|
|
2023-09-22 14:53:55 +00:00
|
|
|
dr := &dummyACLResolver{
|
|
|
|
result: testutils.ACLsDisabled(t),
|
|
|
|
}
|
|
|
|
server.ACLResolver = dr
|
2023-04-11 11:10:14 +00:00
|
|
|
|
2023-09-22 14:53:55 +00:00
|
|
|
demo.RegisterTypes(server.Registry)
|
2023-04-11 11:10:14 +00:00
|
|
|
|
2023-09-22 14:53:55 +00:00
|
|
|
dr.SetResult(tc.authz)
|
|
|
|
testutil.RunStep(t, "does not exist", func(t *testing.T) {
|
|
|
|
_, err = client.Read(nextTokenContext(t), &pbresource.ReadRequest{Id: tc.res.Id})
|
|
|
|
if tc.codeNotExist == codes.OK {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
require.Equal(t, tc.codeNotExist.String(), status.Code(err).String(), "%v", err)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Create it.
|
|
|
|
dr.SetResult(adminAuthz)
|
|
|
|
_, err = client.Write(nextTokenContext(t), &pbresource.WriteRequest{Resource: tc.res})
|
|
|
|
require.NoError(t, err, "could not write resource")
|
|
|
|
|
|
|
|
dr.SetResult(tc.authz)
|
|
|
|
testutil.RunStep(t, "does exist", func(t *testing.T) {
|
|
|
|
// exercise ACL when the data does exist
|
|
|
|
_, err = client.Read(nextTokenContext(t), &pbresource.ReadRequest{Id: tc.res.Id})
|
|
|
|
if tc.codeExists == codes.OK {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
require.Equal(t, tc.codeExists.String(), status.Code(err).String())
|
|
|
|
})
|
2023-04-11 11:10:14 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 14:53:55 +00:00
|
|
|
type dummyACLResolver struct {
|
|
|
|
lock sync.Mutex
|
|
|
|
result resolver.Result
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ ACLResolver = (*dummyACLResolver)(nil)
|
|
|
|
|
|
|
|
func (r *dummyACLResolver) SetResult(result resolver.Result) {
|
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
r.result = result
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *dummyACLResolver) ResolveTokenAndDefaultMeta(string, *acl.EnterpriseMeta, *acl.AuthorizerContext) (resolver.Result, error) {
|
|
|
|
r.lock.Lock()
|
|
|
|
defer r.lock.Unlock()
|
|
|
|
return r.result, nil
|
|
|
|
}
|
|
|
|
|
2023-03-27 15:35:39 +00:00
|
|
|
type readTestCase struct {
|
|
|
|
consistency storage.ReadConsistency
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func readTestCases() map[string]readTestCase {
|
|
|
|
return map[string]readTestCase{
|
|
|
|
"eventually consistent read": {
|
|
|
|
consistency: storage.EventualConsistency,
|
|
|
|
ctx: context.Background(),
|
|
|
|
},
|
|
|
|
"strongly consistent read": {
|
|
|
|
consistency: storage.StrongConsistency,
|
|
|
|
ctx: metadata.NewOutgoingContext(
|
|
|
|
context.Background(),
|
|
|
|
metadata.New(map[string]string{"x-consul-consistency-mode": "consistent"}),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|