Skip to content

Commit

Permalink
[NET-6417] Add validation of MeshGateway name + listeners (hashicorp#…
Browse files Browse the repository at this point in the history
…20425)

* Add validation of MeshGateway name + listeners

* Adds test for ValidateMeshGateway

* Fixes data fetcher test for gatewayproxy

---------

Co-authored-by: Nathan Coleman <[email protected]>
  • Loading branch information
missylbytes and nathancoleman authored Jan 31, 2024
1 parent b6f10bc commit 7c00d39
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ func (suite *dataFetcherSuite) setupWithTenancy(tenancy *pbresource.Tenancy) {
).
Write(suite.T(), suite.client)

suite.meshGateway = resourcetest.Resource(pbmesh.MeshGatewayType, "mesh-gateway-1").
suite.meshGateway = resourcetest.Resource(pbmesh.MeshGatewayType, "mesh-gateway").
WithData(suite.T(), &pbmesh.MeshGateway{
GatewayClassName: "gateway-class-1",
Listeners: []*pbmesh.MeshGatewayListener{
{
Name: "wan",
},
},
}).
Write(suite.T(), suite.client)

Expand Down
26 changes: 25 additions & 1 deletion internal/mesh/internal/types/mesh_gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
package types

import (
"errors"
"fmt"

"github.com/hashicorp/go-multierror"

"github.com/hashicorp/consul/internal/mesh/internal/controllers/meshgateways"
"github.com/hashicorp/consul/internal/resource"
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
)
Expand All @@ -15,6 +21,24 @@ func RegisterMeshGateway(r resource.Registry) {
Scope: resource.ScopePartition,
ACLs: nil, // TODO NET-6416
Mutate: nil, // TODO NET-6418
Validate: nil, // TODO NET-6417
Validate: resource.DecodeAndValidate(validateMeshGateway),
})
}

func validateMeshGateway(res *DecodedMeshGateway) error {
var merr error

if res.GetId().GetName() != meshgateways.GatewayName {
merr = multierror.Append(merr, fmt.Errorf("invalid gateway name, must be %q", meshgateways.GatewayName))
}

if len(res.GetData().Listeners) != 1 {
merr = multierror.Append(merr, errors.New("invalid listeners, must have exactly one listener"))
}

if len(res.GetData().Listeners) > 0 && (res.GetData().Listeners[0].GetName() != meshgateways.WANPortName) {
merr = multierror.Append(merr, fmt.Errorf("invalid listener name, must be %q", meshgateways.WANPortName))
}

return merr
}
97 changes: 97 additions & 0 deletions internal/mesh/internal/types/mesh_gateway_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package types

import (
"github.com/hashicorp/consul/internal/resource"
"testing"

"github.com/hashicorp/consul/internal/resource/resourcetest"
pbmesh "github.com/hashicorp/consul/proto-public/pbmesh/v2beta1"
"github.com/hashicorp/consul/sdk/testutil"
"github.com/stretchr/testify/require"
)

func TestValidateMeshGateway(t *testing.T) {
type testcase struct {
mgwName string
mgw *pbmesh.MeshGateway
expectErr string
}

run := func(t *testing.T, tc testcase) {
res := resourcetest.Resource(pbmesh.MeshGatewayType, tc.mgwName).
WithData(t, tc.mgw).
Build()

err := resource.DecodeAndValidate(validateMeshGateway)(res)

if tc.expectErr == "" {
require.NoError(t, err)
} else {
testutil.RequireErrorContains(t, err, tc.expectErr)
}
}

cases := map[string]testcase{
"happy path": {
mgwName: "mesh-gateway",
mgw: &pbmesh.MeshGateway{
Listeners: []*pbmesh.MeshGatewayListener{
{
Name: "wan",
},
},
},
expectErr: "",
},
"wrong name for mesh-gateway": {
mgwName: "my-mesh-gateway",
mgw: &pbmesh.MeshGateway{
Listeners: []*pbmesh.MeshGatewayListener{
{
Name: "wan",
},
},
},
expectErr: "invalid gateway name, must be \"mesh-gateway\"",
},
"too many listeners on mesh-gateway": {
mgwName: "mesh-gateway",
mgw: &pbmesh.MeshGateway{
Listeners: []*pbmesh.MeshGatewayListener{
{
Name: "obi",
},
{
Name: "wan",
},
},
},
expectErr: "invalid listeners, must have exactly one listener",
},
"zero listeners on mesh-gateway": {
mgwName: "mesh-gateway",
mgw: &pbmesh.MeshGateway{},
expectErr: "invalid listeners, must have exactly one listener",
},
"incorrect listener name on mesh-gateway": {
mgwName: "mesh-gateway",
mgw: &pbmesh.MeshGateway{
Listeners: []*pbmesh.MeshGatewayListener{
{
Name: "kenobi",
},
},
},
expectErr: "invalid listener name, must be \"wan\"",
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
run(t, tc)
})
}
}

0 comments on commit 7c00d39

Please sign in to comment.