From 458da5f8ce28cc08926ceb74a7831a0bc7146ddd Mon Sep 17 00:00:00 2001 From: Vladimir Popov Date: Fri, 9 Apr 2021 11:36:36 +0700 Subject: [PATCH] Use grpcfd.TransportCredentials to enable unix sockets Signed-off-by: Vladimir Popov --- pkg/tools/sandbox/builder.go | 15 ++++++------ pkg/tools/sandbox/grpc_utils.go | 5 +++- pkg/tools/sandbox/grpc_utils_notwindows.go | 28 ++++++++++++++++++++++ pkg/tools/sandbox/grpc_utils_windows.go | 25 +++++++++++++++++++ pkg/tools/sandbox/node.go | 15 ++---------- pkg/tools/sandbox/types.go | 5 ++-- pkg/tools/sandbox/utils.go | 3 ++- 7 files changed, 70 insertions(+), 26 deletions(-) create mode 100644 pkg/tools/sandbox/grpc_utils_notwindows.go create mode 100644 pkg/tools/sandbox/grpc_utils_windows.go diff --git a/pkg/tools/sandbox/builder.go b/pkg/tools/sandbox/builder.go index b4d98efca..f0f01097f 100644 --- a/pkg/tools/sandbox/builder.go +++ b/pkg/tools/sandbox/builder.go @@ -52,9 +52,8 @@ type Builder struct { supplyRegistryProxy SupplyRegistryProxyFunc setupNode SetupNodeFunc - DNSDomainName string - Resolver dnsresolve.Resolver - + dnsDomainName string + dnsResolver dnsresolve.Resolver generateTokenFunc token.GeneratorFunc registryExpiryDuration time.Duration @@ -73,8 +72,8 @@ func NewBuilder(ctx context.Context, t *testing.T) *Builder { supplyNSMgrProxy: nsmgrproxy.NewServer, supplyRegistry: memory.NewServer, supplyRegistryProxy: proxydns.NewServer, - DNSDomainName: "cluster.local", - Resolver: net.DefaultResolver, + dnsDomainName: "cluster.local", + dnsResolver: net.DefaultResolver, generateTokenFunc: GenerateTestToken, registryExpiryDuration: time.Minute, } @@ -126,13 +125,13 @@ func (b *Builder) SetNodeSetup(f SetupNodeFunc) *Builder { // SetDNSDomainName sets DNS domain name for the building NSM domain func (b *Builder) SetDNSDomainName(name string) *Builder { - b.DNSDomainName = name + b.dnsDomainName = name return b } // SetDNSResolver sets DNS resolver for proxy registries func (b *Builder) SetDNSResolver(d dnsresolve.Resolver) *Builder { - b.Resolver = d + b.dnsResolver = d return b } @@ -247,7 +246,7 @@ func (b *Builder) newRegistryProxy() { nsmgrProxyURL = b.domain.NSMgrProxy.URL } - registryProxy := b.supplyRegistryProxy(b.ctx, b.Resolver, b.DNSDomainName, nsmgrProxyURL, DefaultDialOptions(b.generateTokenFunc)...) + registryProxy := b.supplyRegistryProxy(b.ctx, b.dnsResolver, b.dnsDomainName, nsmgrProxyURL, DefaultDialOptions(b.generateTokenFunc)...) serveURL := b.domain.supplyURL("reg-proxy") serve(b.ctx, b.t, serveURL, registryProxy.Register) diff --git a/pkg/tools/sandbox/grpc_utils.go b/pkg/tools/sandbox/grpc_utils.go index c160f8f2c..af0a0e8b5 100644 --- a/pkg/tools/sandbox/grpc_utils.go +++ b/pkg/tools/sandbox/grpc_utils.go @@ -23,6 +23,7 @@ import ( "github.com/stretchr/testify/require" "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" "github.com/networkservicemesh/sdk/pkg/tools/grpcutils" "github.com/networkservicemesh/sdk/pkg/tools/log" @@ -31,7 +32,9 @@ import ( ) func serve(ctx context.Context, t *testing.T, u *url.URL, register func(server *grpc.Server)) { - server := grpc.NewServer(opentracing.WithTracing()...) + server := grpc.NewServer(append([]grpc.ServerOption{ + grpc.Creds(grpcfdTransportCredentials(insecure.NewCredentials())), + }, opentracing.WithTracing()...)...) register(server) errCh := grpcutils.ListenAndServe(ctx, u, server) diff --git a/pkg/tools/sandbox/grpc_utils_notwindows.go b/pkg/tools/sandbox/grpc_utils_notwindows.go new file mode 100644 index 000000000..375ba610b --- /dev/null +++ b/pkg/tools/sandbox/grpc_utils_notwindows.go @@ -0,0 +1,28 @@ +// Copyright (c) 2021 Doc.ai and/or its affiliates. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build !windows + +package sandbox + +import ( + "github.com/edwarnicke/grpcfd" + "google.golang.org/grpc/credentials" +) + +func grpcfdTransportCredentials(cred credentials.TransportCredentials) credentials.TransportCredentials { + return grpcfd.TransportCredentials(cred) +} diff --git a/pkg/tools/sandbox/grpc_utils_windows.go b/pkg/tools/sandbox/grpc_utils_windows.go new file mode 100644 index 000000000..bc5b9eb72 --- /dev/null +++ b/pkg/tools/sandbox/grpc_utils_windows.go @@ -0,0 +1,25 @@ +// Copyright (c) 2021 Doc.ai and/or its affiliates. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at: +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// +build windows + +package sandbox + +import "google.golang.org/grpc/credentials" + +func grpcfdTransportCredentials(cred credentials.TransportCredentials) credentials.TransportCredentials { + return cred +} \ No newline at end of file diff --git a/pkg/tools/sandbox/node.go b/pkg/tools/sandbox/node.go index f2f64e2e0..71ea9d20e 100644 --- a/pkg/tools/sandbox/node.go +++ b/pkg/tools/sandbox/node.go @@ -21,12 +21,10 @@ import ( "net/url" "testing" - "github.com/stretchr/testify/require" - "google.golang.org/grpc" - "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/networkservicemesh/api/pkg/api/networkservice/payload" registryapi "github.com/networkservicemesh/api/pkg/api/registry" + "github.com/stretchr/testify/require" "github.com/networkservicemesh/sdk/pkg/networkservice/chains/client" "github.com/networkservicemesh/sdk/pkg/networkservice/chains/endpoint" @@ -38,7 +36,6 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/core/adapters" registryclient "github.com/networkservicemesh/sdk/pkg/registry/chains/client" "github.com/networkservicemesh/sdk/pkg/tools/addressof" - "github.com/networkservicemesh/sdk/pkg/tools/grpcutils" "github.com/networkservicemesh/sdk/pkg/tools/log" "github.com/networkservicemesh/sdk/pkg/tools/token" ) @@ -207,17 +204,9 @@ func (n *Node) NewClient( generatorFunc token.GeneratorFunc, additionalFunctionality ...networkservice.NetworkServiceClient, ) networkservice.NetworkServiceClient { - cc, err := grpc.DialContext(ctx, grpcutils.URLToTarget(n.NSMgr.URL), DefaultDialOptions(generatorFunc)...) - require.NoError(n.t, err) - - go func() { - defer func() { _ = cc.Close() }() - <-ctx.Done() - }() - return client.NewClient( ctx, - cc, + dial(ctx, n.t, n.NSMgr.URL, generatorFunc), client.WithAuthorizeClient(authorize.NewClient(authorize.Any())), client.WithAdditionalFunctionality(additionalFunctionality...), ) diff --git a/pkg/tools/sandbox/types.go b/pkg/tools/sandbox/types.go index a77743a60..680c8ff54 100644 --- a/pkg/tools/sandbox/types.go +++ b/pkg/tools/sandbox/types.go @@ -72,9 +72,8 @@ type Domain struct { NSMgrProxy *EndpointEntry Registry *RegistryEntry RegistryProxy *RegistryEntry - DNSResolver dnsresolve.Resolver - Name string - supplyURL func(prefix string) *url.URL + + supplyURL func(prefix string) *url.URL } // NodeConfig keeps custom node configuration parameters diff --git a/pkg/tools/sandbox/utils.go b/pkg/tools/sandbox/utils.go index 00c8b83b5..2daeae70d 100644 --- a/pkg/tools/sandbox/utils.go +++ b/pkg/tools/sandbox/utils.go @@ -26,6 +26,7 @@ import ( registryapi "github.com/networkservicemesh/api/pkg/api/registry" "google.golang.org/grpc" "google.golang.org/grpc/credentials" + "google.golang.org/grpc/credentials/insecure" "github.com/networkservicemesh/api/pkg/api/networkservice" @@ -95,7 +96,7 @@ func NewCrossConnectClientFactory(additionalFunctionality ...networkservice.Netw // DefaultDialOptions returns default dial options for sandbox testing func DefaultDialOptions(genTokenFunc token.GeneratorFunc) []grpc.DialOption { return append([]grpc.DialOption{ - grpc.WithInsecure(), + grpc.WithTransportCredentials(grpcfdTransportCredentials(insecure.NewCredentials())), grpc.WithBlock(), grpc.WithDefaultCallOptions( grpc.WaitForReady(true),