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

Create DNSContext NetworkServiceServer chain elements #84

Merged
merged 5 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/golang/protobuf v1.3.3
github.com/google/uuid v1.1.1
github.com/grpc-ecosystem/grpc-opentracing v0.0.0-20180507213350-8e809c8a8645
github.com/kelseyhightower/envconfig v1.4.0
github.com/networkservicemesh/api v0.0.0-20200203181400-b7f0d29ed5cd
github.com/open-policy-agent/opa v0.16.1
github.com/opentracing/opentracing-go v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand Down
58 changes: 58 additions & 0 deletions pkg/networkservice/connectioncontext/dnscontext/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2020 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.

// Package dnscontext provides a dns context specific chain element. It just adds dns configs into dns context of connection context.
// It also provides a possibility to use custom dns configs getters for setup users endpoints.
package dnscontext

import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
)

// GetDNSConfigsFunc gets dns configs
type GetDNSConfigsFunc func(conn *networkservice.Connection) []*networkservice.DNSConfig
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved

type dnsContextServer struct {
getDNSConfigs GetDNSConfigsFunc
}

// NewServer creates dns context chain server element.
func NewServer(getter GetDNSConfigsFunc) networkservice.NetworkServiceServer {
return &dnsContextServer{getDNSConfigs: getter}
}

func (d *dnsContextServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
if d.getDNSConfigs != nil {
request.GetConnection().GetContext().DnsContext = &networkservice.DNSContext{
Configs: d.getDNSConfigs(request.GetConnection()),
}
}
return next.Server(ctx).Request(ctx, request)
}

func (d *dnsContextServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
if d.getDNSConfigs != nil {
conn.GetContext().DnsContext = &networkservice.DNSContext{
Configs: d.getDNSConfigs(conn),
}
}
return next.Server(ctx).Close(ctx, conn)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) 2020 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.

package tests

import (
"context"
"os"
"testing"

"github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/stretchr/testify/require"

"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
)

func TestServerBasic(t *testing.T) {
r := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Id: "0",
NetworkService: "icmp",
Context: &networkservice.ConnectionContext{
IpContext: &networkservice.IPContext{
DstIpAddr: "172.16.1.2",
},
},
},
}
s := next.NewNetworkServiceServer(dnscontext.NewServer(dnscontext.ConfigFromConnection()))
resp, err := s.Request(context.Background(), r)
require.Nil(t, err)
require.NotNil(t, resp.GetContext().GetDnsContext())
require.Len(t, resp.Context.DnsContext.Configs[0].SearchDomains, 1)
require.Len(t, resp.Context.DnsContext.Configs[0].DnsServerIps, 1)
require.Equal(t, resp.Context.DnsContext.Configs[0].SearchDomains[0], r.Connection.NetworkService)
require.Equal(t, resp.Context.DnsContext.Configs[0].DnsServerIps[0], r.Connection.Context.IpContext.DstIpAddr)
}

func TestServerBasicFromEnv(t *testing.T) {
r := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Context: &networkservice.ConnectionContext{},
},
}
expectedDomain := "test.domain"
expectedIP := "8.8.8.8"
err := os.Setenv("DNSCONFIG_DNSSERVERIPS", expectedIP)
require.Nil(t, err)
err = os.Setenv("DNSCONFIG_SEARCHDOMAINS", expectedDomain)
require.Nil(t, err)
s := next.NewNetworkServiceServer(dnscontext.NewServer(dnscontext.ConfigFromEnv()))
resp, err := s.Request(context.Background(), r)
require.Nil(t, err)
require.NotNil(t, resp.GetContext().GetDnsContext())
require.Len(t, resp.Context.DnsContext.Configs[0].SearchDomains, 1)
require.Len(t, resp.Context.DnsContext.Configs[0].DnsServerIps, 1)
require.Equal(t, resp.Context.DnsContext.Configs[0].SearchDomains[0], expectedDomain)
require.Equal(t, resp.Context.DnsContext.Configs[0].DnsServerIps[0], expectedIP)
}
49 changes: 49 additions & 0 deletions pkg/networkservice/connectioncontext/dnscontext/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) 2020 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.

package dnscontext

import (
"github.com/kelseyhightower/envconfig"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/sirupsen/logrus"
)

// ConfigFromEnv returns default getter for DNS configs. Builds config from env variables.
func ConfigFromEnv() GetDNSConfigsFunc {
denis-tingaikin marked this conversation as resolved.
Show resolved Hide resolved
return func(_ *networkservice.Connection) []*networkservice.DNSConfig {
config := &networkservice.DNSConfig{}
err := envconfig.Process("DNSCONFIG", config)
if err != nil {
logrus.Error(err.Error())
}
return []*networkservice.DNSConfig{config}
}
}

// ConfigFromConnection returns default getter for DNS configs. Builds config from the connection.
func ConfigFromConnection() GetDNSConfigsFunc {
return func(conn *networkservice.Connection) []*networkservice.DNSConfig {
domain := conn.GetNetworkService()
ip := conn.GetContext().GetIpContext().GetDstIpAddr()
return []*networkservice.DNSConfig{
{
DnsServerIps: []string{ip},
SearchDomains: []string{domain},
},
}
}
}