Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read endpoint #18268

Merged
merged 38 commits into from
Aug 11, 2023
Merged
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
183298e
init commit
xwa153 Jul 17, 2023
eaf297d
pass x-consul-token to the grpc server
xwa153 Jul 20, 2023
97dacbc
query params and add logger
xwa153 Jul 20, 2023
7062429
log message
xwa153 Jul 20, 2023
e69aa2d
change log message
xwa153 Jul 20, 2023
4975291
fix unit test
xwa153 Jul 20, 2023
f5e9707
remove read
xwa153 Jul 21, 2023
f1bfb5d
add detailed error handling message
xwa153 Jul 21, 2023
7c19794
Merge branch 'main' into NET-2678/xw-http-api-write
xwa153 Jul 21, 2023
42d6ccf
fix unit tests
xwa153 Jul 21, 2023
595ac3e
fix unit tests
xwa153 Jul 21, 2023
4ccddf3
read endpoint
xwa153 Jul 24, 2023
eb96780
general refactor
xwa153 Jul 25, 2023
1c2efc2
add more tests
xwa153 Jul 25, 2023
884b79d
Merge branch 'main' into NET-2678/xw-http-api-write
xwa153 Jul 25, 2023
dacbda0
refactor test
xwa153 Jul 27, 2023
b5edf4a
add add owner and remove extra check
xwa153 Jul 27, 2023
8db104c
add more tests
xwa153 Aug 2, 2023
30938f1
Merge branch 'main' into NET-2678/xw-http-api-write
xwa153 Aug 2, 2023
0079424
merge write branch
xwa153 Aug 3, 2023
f326d53
refactor checkUrl function
xwa153 Aug 3, 2023
388a465
write the error msg back
xwa153 Aug 3, 2023
c3775a7
add owner test
xwa153 Aug 3, 2023
f769295
merge write
xwa153 Aug 3, 2023
7e2c9fa
could use the consistent mode
xwa153 Aug 3, 2023
adfab07
add new test
xwa153 Aug 3, 2023
996f9e8
add tests regarding the version query parameter
xwa153 Aug 3, 2023
52bbf65
Merge branch 'NET-2678/xw-http-api-write' into NET-2705/xw-http-api-read
xwa153 Aug 3, 2023
071d612
remove duplicate test case
xwa153 Aug 3, 2023
00bab51
add test case
xwa153 Aug 3, 2023
c043f45
code refactor
xwa153 Aug 3, 2023
b1682ef
Merge branch 'NET-2678/xw-http-api-write' into NET-2705/xw-http-api-read
xwa153 Aug 3, 2023
0c13388
merge main
xwa153 Aug 4, 2023
4ae4941
merge main
xwa153 Aug 11, 2023
0a189db
compare whole body in test
xwa153 Aug 11, 2023
e04c8df
refactor
xwa153 Aug 11, 2023
76a0a9f
linter
xwa153 Aug 11, 2023
129bd83
refactor tests
xwa153 Aug 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor test
  • Loading branch information
xwa153 authored and JadhavPoonam committed Jul 28, 2023
commit dacbda0183e58e2a1c4e2e92c2b8889c3631cfdb
66 changes: 66 additions & 0 deletions agent/grpc-external/services/resource/testing/testing.go
Original file line number Diff line number Diff line change
@@ -8,15 +8,42 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"

"github.com/hashicorp/consul/acl"
"github.com/hashicorp/consul/acl/resolver"
svc "github.com/hashicorp/consul/agent/grpc-external/services/resource"
internal "github.com/hashicorp/consul/agent/grpc-internal"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/storage/inmem"
"github.com/hashicorp/consul/proto-public/pbresource"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/hashicorp/go-uuid"
)

func randomACLIdentity(t *testing.T) structs.ACLIdentity {
id, err := uuid.GenerateUUID()
require.NoError(t, err)

return &structs.ACLToken{AccessorID: id}
}

func AuthorizerFrom(t *testing.T, policyStrs ...string) resolver.Result {
policies := []*acl.Policy{}
for _, policyStr := range policyStrs {
policy, err := acl.NewPolicyFromSource(policyStr, nil, nil)
require.NoError(t, err)
policies = append(policies, policy)
}

authz, err := acl.NewPolicyAuthorizerWithDefaults(acl.DenyAll(), policies, nil)
require.NoError(t, err)

return resolver.Result{
Authorizer: authz,
ACLIdentity: randomACLIdentity(t),
}
}

// RunResourceService runs a Resource Service for the duration of the test and
// returns a client to interact with it. ACLs will be disabled.
func RunResourceService(t *testing.T, registerFns ...func(resource.Registry)) pbresource.ResourceServiceClient {
@@ -57,3 +84,42 @@ func RunResourceService(t *testing.T, registerFns ...func(resource.Registry)) pb

return pbresource.NewResourceServiceClient(conn)
}

func RunResourceServiceWithACL(t *testing.T, aclResolver svc.ACLResolver, registerFns ...func(resource.Registry)) pbresource.ResourceServiceClient {
t.Helper()

backend, err := inmem.NewBackend()
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
go backend.Run(ctx)

registry := resource.NewRegistry()
for _, fn := range registerFns {
fn(registry)
}

server := grpc.NewServer()

svc.NewServer(svc.Config{
Backend: backend,
Registry: registry,
Logger: testutil.Logger(t),
ACLResolver: aclResolver,
}).Register(server)

pipe := internal.NewPipeListener()
go server.Serve(pipe)
t.Cleanup(server.Stop)

conn, err := grpc.Dial("",
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(pipe.DialContext),
grpc.WithBlock(),
)
require.NoError(t, err)
t.Cleanup(func() { _ = conn.Close() })

return pbresource.NewResourceServiceClient(conn)
}
106 changes: 73 additions & 33 deletions internal/resource/http/http_test.go
Original file line number Diff line number Diff line change
@@ -8,19 +8,34 @@ import (
"testing"

"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"

"github.com/hashicorp/consul/acl"
resourceSvc "github.com/hashicorp/consul/agent/grpc-external/services/resource"
svctest "github.com/hashicorp/consul/agent/grpc-external/services/resource/testing"

"github.com/hashicorp/consul/internal/resource"
"github.com/hashicorp/consul/internal/resource/demo"
"github.com/hashicorp/consul/proto-public/pbresource"
pbdemov2 "github.com/hashicorp/consul/proto/private/pbdemo/v2"
"github.com/hashicorp/consul/sdk/testutil"
)

func TestResourceHandler(t *testing.T) {
client := svctest.RunResourceService(t, demo.RegisterTypes)
const testACLToken = acl.AnonymousTokenID

func parseToken(req *http.Request, token *string) {
*token = req.Header.Get("x-Consul-Token")
}

func TestResourceHandler_InputValidation(t *testing.T) {
type testCase struct {
description string
request *http.Request
response *httptest.ResponseRecorder
expectedResponseCode int
}
client := svctest.RunResourceService(t, demo.RegisterTypes)
resourceHandler := resourceHandler{
resource.Registration{
Type: demo.TypeV2Artist,
@@ -31,43 +46,66 @@ func TestResourceHandler(t *testing.T) {
hclog.NewNullLogger(),
}

t.Run("should return bad request due to missing resource name", func(t *testing.T) {
rsp := httptest.NewRecorder()
req := httptest.NewRequest("PUT", "/?partition=default&peer_name=local&namespace=default", strings.NewReader(`
{
"metadata": {
"foo": "bar"
},
"data": {
"name": "Keith Urban",
"genre": "GENRE_COUNTRY"
testCases := []testCase{
{
description: "missing resource name",
request: httptest.NewRequest("PUT", "/?partition=default&peer_name=local&namespace=default", strings.NewReader(`
{
"metadata": {
"foo": "bar"
},
"data": {
"name": "Keith Urban",
"genre": "GENRE_COUNTRY"
}
}
}
`))
`)),
response: httptest.NewRecorder(),
expectedResponseCode: http.StatusBadRequest,
},
{
description: "wrong schema",
request: httptest.NewRequest("PUT", "/?partition=default&peer_name=local&namespace=default", strings.NewReader(`
{
"metadata": {
"foo": "bar"
},
"tada": {
"name": "Keith Urban",
"genre": "GENRE_COUNTRY"
}
}
`)),
response: httptest.NewRecorder(),
expectedResponseCode: http.StatusBadRequest,
},
}

resourceHandler.ServeHTTP(rsp, req)
for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
resourceHandler.ServeHTTP(tc.response, tc.request)

require.Equal(t, http.StatusBadRequest, rsp.Result().StatusCode)
})
require.Equal(t, tc.expectedResponseCode, tc.response.Result().StatusCode)
})
}
}

t.Run("should return bad request due to wrong schema", func(t *testing.T) {
rsp := httptest.NewRecorder()
req := httptest.NewRequest("PUT", "/?partition=default&peer_name=local&namespace=default", strings.NewReader(`
{
"metadata": {
"foo": "bar"
},
"tada": {
"name": "Keith Urban",
"genre": "GENRE_COUNTRY"
}
}
`))
func TestResourceWriteHandler(t *testing.T) {
aclResolver := &resourceSvc.MockACLResolver{}
aclResolver.On("ResolveTokenAndDefaultMeta", testACLToken, mock.Anything, mock.Anything).
Return(svctest.AuthorizerFrom(t, demo.ArtistV2WritePolicy), nil)

resourceHandler.ServeHTTP(rsp, req)
client := svctest.RunResourceServiceWithACL(t, aclResolver, demo.RegisterTypes)

require.Equal(t, http.StatusBadRequest, rsp.Result().StatusCode)
})
resourceHandler := resourceHandler{
resource.Registration{
Type: demo.TypeV2Artist,
Proto: &pbdemov2.Artist{},
},
client,
parseToken,
hclog.NewNullLogger(),
}

t.Run("should write to the resource backend", func(t *testing.T) {
rsp := httptest.NewRecorder()
@@ -83,6 +121,8 @@ func TestResourceHandler(t *testing.T) {
}
`))

req.Header.Add("x-consul-token", testACLToken)

resourceHandler.ServeHTTP(rsp, req)

require.Equal(t, http.StatusOK, rsp.Result().StatusCode)