Skip to content

Commit

Permalink
Use grpcfd.TransportCredentials to enable unix sockets
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Popov <vladimir.popov@xored.com>
  • Loading branch information
Vladimir Popov committed Apr 9, 2021
1 parent 4a0ea61 commit 458da5f
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 26 deletions.
15 changes: 7 additions & 8 deletions pkg/tools/sandbox/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 4 additions & 1 deletion pkg/tools/sandbox/grpc_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions pkg/tools/sandbox/grpc_utils_notwindows.go
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions pkg/tools/sandbox/grpc_utils_windows.go
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 2 additions & 13 deletions pkg/tools/sandbox/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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...),
)
Expand Down
5 changes: 2 additions & 3 deletions pkg/tools/sandbox/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/tools/sandbox/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 458da5f

Please sign in to comment.