diff --git a/pkg/networkservice/chains/nsmgr/dns_test.go b/pkg/networkservice/chains/nsmgr/dns_test.go index 7b37f3b96..4c5cf1fce 100644 --- a/pkg/networkservice/chains/nsmgr/dns_test.go +++ b/pkg/networkservice/chains/nsmgr/dns_test.go @@ -1,5 +1,7 @@ // Copyright (c) 2020-2022 Doc.ai and/or its affiliates. // +// Copyright (c) 2023 Cisco and/or its affiliates. +// // SPDX-License-Identifier: Apache-2.0 // // Licensed under the Apache License, Version 2.0 (the "License"); @@ -25,6 +27,7 @@ import ( "testing" "time" + "github.com/edwarnicke/genericsync" "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/cls" kernelmech "github.com/networkservicemesh/api/pkg/api/networkservice/mechanisms/kernel" @@ -33,7 +36,6 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/chains/client" "github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext" - "github.com/networkservicemesh/sdk/pkg/tools/dnsconfig" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/cache" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/dnsconfigs" @@ -74,7 +76,7 @@ func Test_DNSUsecase(t *testing.T) { nse := domain.Nodes[0].NewEndpoint(ctx, nseReg, sandbox.GenerateTestToken) - dnsConfigsMap := new(dnsconfig.Map) + dnsConfigsMap := new(genericsync.Map[string, []*networkservice.DNSConfig]) nsc := domain.Nodes[0].NewClient(ctx, sandbox.GenerateTestToken, client.WithAdditionalFunctionality(dnscontext.NewClient( dnscontext.WithChainContext(ctx), dnscontext.WithDNSConfigsMap(dnsConfigsMap), @@ -88,7 +90,7 @@ func Test_DNSUsecase(t *testing.T) { } // DNS server on nse side - dnsRecords := new(memory.Map) + dnsRecords := new(genericsync.Map[string, []net.IP]) dnsRecords.Store("my.domain.", []net.IP{net.ParseIP("4.4.4.4")}) dnsRecords.Store("my.domain.com.", []net.IP{net.ParseIP("5.5.5.5")}) dnsutils.ListenAndServe(ctx, memory.NewDNSHandler(dnsRecords), ":40053") diff --git a/pkg/networkservice/chains/nsmgr/vl3_test.go b/pkg/networkservice/chains/nsmgr/vl3_test.go index 30fd7bc5a..fb970b983 100644 --- a/pkg/networkservice/chains/nsmgr/vl3_test.go +++ b/pkg/networkservice/chains/nsmgr/vl3_test.go @@ -26,6 +26,7 @@ import ( "testing" "time" + "github.com/edwarnicke/genericsync" "github.com/google/uuid" "github.com/networkservicemesh/api/pkg/api/ipam" "github.com/networkservicemesh/api/pkg/api/networkservice" @@ -37,7 +38,6 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext/vl3dns" "github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/ipcontext/vl3" "github.com/networkservicemesh/sdk/pkg/networkservice/utils/checks/checkconnection" - "github.com/networkservicemesh/sdk/pkg/tools/dnsconfig" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/memory" "github.com/networkservicemesh/sdk/pkg/tools/sandbox" @@ -137,7 +137,7 @@ func Test_vl3NSE_ConnectsTo_vl3NSE(t *testing.T) { SetRegistryProxySupplier(nil). Build() - var records memory.Map + var records genericsync.Map[string, []net.IP] var dnsServer = memory.NewDNSHandler(&records) records.Store("nsc1.vl3.", []net.IP{net.ParseIP("1.1.1.1")}) @@ -156,7 +156,7 @@ func Test_vl3NSE_ConnectsTo_vl3NSE(t *testing.T) { serverPrefixCh <- &ipam.PrefixResponse{Prefix: "10.0.0.1/24"} - var dnsConfigs = new(dnsconfig.Map) + var dnsConfigs = new(genericsync.Map[string, []*networkservice.DNSConfig]) dnsServerIPCh := make(chan net.IP, 1) dnsServerIPCh <- net.ParseIP("0.0.0.0") diff --git a/pkg/networkservice/common/authorize/options.go b/pkg/networkservice/common/authorize/options.go index 583fdc339..7096f89d5 100644 --- a/pkg/networkservice/common/authorize/options.go +++ b/pkg/networkservice/common/authorize/options.go @@ -1,6 +1,6 @@ // Copyright (c) 2020-2022 Doc.ai and/or its affiliates. // -// Copyright (c) 2020-2022 Cisco Systems, Inc. +// Copyright (c) 2020-2023 Cisco Systems, Inc. // // SPDX-License-Identifier: Apache-2.0 // @@ -18,11 +18,14 @@ package authorize -import "github.com/networkservicemesh/sdk/pkg/tools/spire" +import ( + "github.com/edwarnicke/genericsync" + "github.com/spiffe/go-spiffe/v2/spiffeid" +) type options struct { policyPaths []string - spiffeIDConnectionMap *spire.SpiffeIDConnectionMap + spiffeIDConnectionMap *genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]] } // Option is authorization option for network service server @@ -42,7 +45,7 @@ func WithPolicies(policyPaths ...string) Option { } // WithSpiffeIDConnectionMap sets map to keep spiffeIDConnectionMap to authorize connections with MonitorConnectionServer -func WithSpiffeIDConnectionMap(s *spire.SpiffeIDConnectionMap) Option { +func WithSpiffeIDConnectionMap(s *genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]]) Option { return func(o *options) { o.spiffeIDConnectionMap = s } diff --git a/pkg/networkservice/common/authorize/server.go b/pkg/networkservice/common/authorize/server.go index 219647808..cf050b07a 100644 --- a/pkg/networkservice/common/authorize/server.go +++ b/pkg/networkservice/common/authorize/server.go @@ -1,6 +1,6 @@ // Copyright (c) 2020-2022 Doc.ai and/or its affiliates. // -// Copyright (c) 2020-2022 Cisco Systems, Inc. +// Copyright (c) 2020-2023 Cisco Systems, Inc. // // SPDX-License-Identifier: Apache-2.0 // @@ -22,8 +22,10 @@ package authorize import ( "context" + "github.com/edwarnicke/genericsync" "github.com/golang/protobuf/ptypes/empty" "github.com/pkg/errors" + "github.com/spiffe/go-spiffe/v2/spiffeid" "google.golang.org/grpc/peer" "github.com/networkservicemesh/api/pkg/api/networkservice" @@ -31,12 +33,11 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" "github.com/networkservicemesh/sdk/pkg/tools/opa" "github.com/networkservicemesh/sdk/pkg/tools/spire" - "github.com/networkservicemesh/sdk/pkg/tools/stringset" ) type authorizeServer struct { policies policiesList - spiffeIDConnectionMap *spire.SpiffeIDConnectionMap + spiffeIDConnectionMap *genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]] } // NewServer - returns a new authorization networkservicemesh.NetworkServiceServers @@ -47,7 +48,7 @@ func NewServer(opts ...Option) networkservice.NetworkServiceServer { "etc/nsm/opa/common/.*.rego", "etc/nsm/opa/server/.*.rego", }, - spiffeIDConnectionMap: &spire.SpiffeIDConnectionMap{}, + spiffeIDConnectionMap: &genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]]{}, } for _, opt := range opts { opt(o) @@ -86,7 +87,7 @@ func (a *authorizeServer) Request(ctx context.Context, request *networkservice.N connID := conn.GetPath().GetPathSegments()[index-1].GetId() ids, ok := a.spiffeIDConnectionMap.Load(spiffeID) if !ok { - ids = new(stringset.StringSet) + ids = new(genericsync.Map[string, struct{}]) } ids.Store(connID, struct{}{}) a.spiffeIDConnectionMap.Store(spiffeID, ids) diff --git a/pkg/networkservice/connectioncontext/dnscontext/client.go b/pkg/networkservice/connectioncontext/dnscontext/client.go index 6dbd35274..b93ec574e 100644 --- a/pkg/networkservice/connectioncontext/dnscontext/client.go +++ b/pkg/networkservice/connectioncontext/dnscontext/client.go @@ -23,6 +23,7 @@ import ( "os" "strings" + "github.com/edwarnicke/genericsync" "github.com/golang/protobuf/ptypes/empty" "github.com/miekg/dns" "github.com/networkservicemesh/api/pkg/api/networkservice" @@ -30,7 +31,6 @@ import ( "google.golang.org/grpc" "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" - "github.com/networkservicemesh/sdk/pkg/tools/dnsconfig" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils" "github.com/networkservicemesh/sdk/pkg/tools/log" ) @@ -40,7 +40,7 @@ type dnsContextClient struct { resolveConfigPath string defaultNameServerIP string resolvconfDNSConfig *networkservice.DNSConfig - dnsConfigsMap *dnsconfig.Map + dnsConfigsMap *genericsync.Map[string, []*networkservice.DNSConfig] } // NewClient creates a new DNS client chain component. Setups all DNS traffic to the localhost. Monitors DNS configs from connections. diff --git a/pkg/networkservice/connectioncontext/dnscontext/client_test.go b/pkg/networkservice/connectioncontext/dnscontext/client_test.go index 506e64041..327302a2c 100644 --- a/pkg/networkservice/connectioncontext/dnscontext/client_test.go +++ b/pkg/networkservice/connectioncontext/dnscontext/client_test.go @@ -1,6 +1,6 @@ // Copyright (c) 2020-2021 Doc.ai and/or its affiliates. // -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -26,6 +26,7 @@ import ( "testing" "time" + "github.com/edwarnicke/genericsync" "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/stretchr/testify/require" "go.uber.org/goleak" @@ -33,7 +34,6 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/connectioncontext/dnscontext" "github.com/networkservicemesh/sdk/pkg/networkservice/core/chain" "github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" - "github.com/networkservicemesh/sdk/pkg/tools/dnsconfig" ) func Test_DNSContextClient_Usecases(t *testing.T) { @@ -51,7 +51,7 @@ func Test_DNSContextClient_Usecases(t *testing.T) { dnscontext.NewClient( dnscontext.WithChainContext(ctx), dnscontext.WithResolveConfigPath(resolveConfigPath), - dnscontext.WithDNSConfigsMap(new(dnsconfig.Map)), + dnscontext.WithDNSConfigsMap(new(genericsync.Map[string, []*networkservice.DNSConfig])), ), ) diff --git a/pkg/networkservice/connectioncontext/dnscontext/options.go b/pkg/networkservice/connectioncontext/dnscontext/options.go index 61a27dd65..9e3f0c5cc 100644 --- a/pkg/networkservice/connectioncontext/dnscontext/options.go +++ b/pkg/networkservice/connectioncontext/dnscontext/options.go @@ -1,6 +1,6 @@ // Copyright (c) 2020-2021 Doc.ai and/or its affiliates. // -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -21,7 +21,8 @@ package dnscontext import ( "context" - "github.com/networkservicemesh/sdk/pkg/tools/dnsconfig" + "github.com/edwarnicke/genericsync" + "github.com/networkservicemesh/api/pkg/api/networkservice" ) // DNSOption is applying options for DNS client. @@ -50,7 +51,7 @@ func WithDefaultNameServerIP(ip string) DNSOption { } // WithDNSConfigsMap sets configs map for DNS client. -func WithDNSConfigsMap(configsMap *dnsconfig.Map) DNSOption { +func WithDNSConfigsMap(configsMap *genericsync.Map[string, []*networkservice.DNSConfig]) DNSOption { return applyFunc(func(c *dnsContextClient) { c.dnsConfigsMap = configsMap }) diff --git a/pkg/networkservice/connectioncontext/dnscontext/vl3dns/client.go b/pkg/networkservice/connectioncontext/dnscontext/vl3dns/client.go index 7db2d3dd4..61b1630d6 100644 --- a/pkg/networkservice/connectioncontext/dnscontext/vl3dns/client.go +++ b/pkg/networkservice/connectioncontext/dnscontext/vl3dns/client.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -20,23 +20,23 @@ import ( "context" "net" + "github.com/edwarnicke/genericsync" "github.com/golang/protobuf/ptypes/empty" "github.com/networkservicemesh/api/pkg/api/networkservice" "google.golang.org/grpc" "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" - "github.com/networkservicemesh/sdk/pkg/tools/dnsconfig" ) type vl3DNSClient struct { dnsServerIP net.IP - dnsConfigs *dnsconfig.Map + dnsConfigs *genericsync.Map[string, []*networkservice.DNSConfig] } // NewClient - returns a new null client that does nothing but call next.Client(ctx).{Request/Close} and return the result // // This is very useful in testing -func NewClient(dnsServerIP net.IP, dnsConfigs *dnsconfig.Map) networkservice.NetworkServiceClient { +func NewClient(dnsServerIP net.IP, dnsConfigs *genericsync.Map[string, []*networkservice.DNSConfig]) networkservice.NetworkServiceClient { return &vl3DNSClient{ dnsServerIP: dnsServerIP, dnsConfigs: dnsConfigs, diff --git a/pkg/networkservice/connectioncontext/dnscontext/vl3dns/options.go b/pkg/networkservice/connectioncontext/dnscontext/vl3dns/options.go index 5c6565abf..6572c6076 100644 --- a/pkg/networkservice/connectioncontext/dnscontext/vl3dns/options.go +++ b/pkg/networkservice/connectioncontext/dnscontext/vl3dns/options.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -21,7 +21,10 @@ import ( "fmt" "text/template" - "github.com/networkservicemesh/sdk/pkg/tools/dnsconfig" + "github.com/edwarnicke/genericsync" + + "github.com/networkservicemesh/api/pkg/api/networkservice" + "github.com/networkservicemesh/sdk/pkg/tools/dnsutils" ) @@ -29,7 +32,7 @@ import ( type Option func(*vl3DNSServer) // WithConfigs sets initial list to fanout queries -func WithConfigs(m *dnsconfig.Map) Option { +func WithConfigs(m *genericsync.Map[string, []*networkservice.DNSConfig]) Option { return func(vd *vl3DNSServer) { vd.dnsConfigs = m } diff --git a/pkg/networkservice/connectioncontext/dnscontext/vl3dns/server.go b/pkg/networkservice/connectioncontext/dnscontext/vl3dns/server.go index 997085b83..f2e20cc8c 100644 --- a/pkg/networkservice/connectioncontext/dnscontext/vl3dns/server.go +++ b/pkg/networkservice/connectioncontext/dnscontext/vl3dns/server.go @@ -26,6 +26,7 @@ import ( "sync/atomic" "text/template" + "github.com/edwarnicke/genericsync" "github.com/golang/protobuf/ptypes/empty" "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/pkg/errors" @@ -33,7 +34,6 @@ import ( "github.com/networkservicemesh/sdk/pkg/networkservice/common/monitor" "github.com/networkservicemesh/sdk/pkg/networkservice/core/next" "github.com/networkservicemesh/sdk/pkg/networkservice/utils/metadata" - "github.com/networkservicemesh/sdk/pkg/tools/dnsconfig" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils" dnschain "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/chain" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/dnsconfigs" @@ -47,8 +47,8 @@ import ( type vl3DNSServer struct { chainCtx context.Context - dnsServerRecords memory.Map - dnsConfigs *dnsconfig.Map + dnsServerRecords genericsync.Map[string, []net.IP] + dnsConfigs *genericsync.Map[string, []*networkservice.DNSConfig] domainSchemeTemplates []*template.Template dnsPort int dnsServer dnsutils.Handler @@ -71,7 +71,7 @@ func NewServer(chainCtx context.Context, dnsServerIPCh <-chan net.IP, opts ...Op chainCtx: chainCtx, dnsPort: 53, listenAndServeDNS: dnsutils.ListenAndServe, - dnsConfigs: new(dnsconfig.Map), + dnsConfigs: new(genericsync.Map[string, []*networkservice.DNSConfig]), dnsServerIPCh: dnsServerIPCh, } diff --git a/pkg/registry/common/authorize/common.go b/pkg/registry/common/authorize/common.go index b9f4db635..5b25d258a 100644 --- a/pkg/registry/common/authorize/common.go +++ b/pkg/registry/common/authorize/common.go @@ -19,6 +19,7 @@ package authorize import ( "context" + "github.com/edwarnicke/genericsync" "github.com/golang-jwt/jwt/v4" "github.com/pkg/errors" "github.com/spiffe/go-spiffe/v2/spiffeid" @@ -63,7 +64,7 @@ func (l *policiesList) check(ctx context.Context, input RegistryOpaInput) error return nil } -func getRawMap(m *PathIdsMap) map[string][]string { +func getRawMap(m *genericsync.Map[string, []string]) map[string][]string { rawMap := make(map[string][]string) m.Range(func(key string, value []string) bool { rawMap[key] = value diff --git a/pkg/registry/common/authorize/gen.go b/pkg/registry/common/authorize/gen.go deleted file mode 100644 index c6dca44ea..000000000 --- a/pkg/registry/common/authorize/gen.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2022 Cisco 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 authorize - -import ( - "sync" -) - -//go:generate go-syncmap -output sync_map.gen.go -type PathIdsMap - -// PathIdsMap - sync.Map with key == resource name and value == list of path ids associated with this resource -type PathIdsMap sync.Map diff --git a/pkg/registry/common/authorize/ns_client.go b/pkg/registry/common/authorize/ns_client.go index b26997618..e6b8e7876 100644 --- a/pkg/registry/common/authorize/ns_client.go +++ b/pkg/registry/common/authorize/ns_client.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -20,6 +20,7 @@ package authorize import ( "context" + "github.com/edwarnicke/genericsync" "github.com/golang/protobuf/ptypes/empty" "github.com/pkg/errors" "google.golang.org/grpc" @@ -34,14 +35,14 @@ import ( type authorizeNSClient struct { policies policiesList - nsPathIdsMap *PathIdsMap + nsPathIdsMap *genericsync.Map[string, []string] } // NewNetworkServiceRegistryClient - returns a new authorization registry.NetworkServiceRegistryClient // Authorize registry client checks spiffeID of NS. func NewNetworkServiceRegistryClient(opts ...Option) registry.NetworkServiceRegistryClient { o := &options{ - resourcePathIdsMap: new(PathIdsMap), + resourcePathIdsMap: new(genericsync.Map[string, []string]), } for _, opt := range opts { diff --git a/pkg/registry/common/authorize/ns_server.go b/pkg/registry/common/authorize/ns_server.go index 080fce7ad..a3660349b 100644 --- a/pkg/registry/common/authorize/ns_server.go +++ b/pkg/registry/common/authorize/ns_server.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -20,6 +20,7 @@ package authorize import ( "context" + "github.com/edwarnicke/genericsync" "github.com/golang/protobuf/ptypes/empty" "github.com/networkservicemesh/api/pkg/api/registry" @@ -30,14 +31,14 @@ import ( type authorizeNSServer struct { policies policiesList - nsPathIdsMap *PathIdsMap + nsPathIdsMap *genericsync.Map[string, []string] } // NewNetworkServiceRegistryServer - returns a new authorization registry.NetworkServiceRegistryServer // Authorize registry server checks spiffeID of NS. func NewNetworkServiceRegistryServer(opts ...Option) registry.NetworkServiceRegistryServer { o := &options{ - resourcePathIdsMap: new(PathIdsMap), + resourcePathIdsMap: new(genericsync.Map[string, []string]), } for _, opt := range opts { diff --git a/pkg/registry/common/authorize/nse_client.go b/pkg/registry/common/authorize/nse_client.go index fb22905d7..e3a1822d9 100644 --- a/pkg/registry/common/authorize/nse_client.go +++ b/pkg/registry/common/authorize/nse_client.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -20,6 +20,7 @@ package authorize import ( "context" + "github.com/edwarnicke/genericsync" "github.com/golang/protobuf/ptypes/empty" "github.com/pkg/errors" "google.golang.org/grpc" @@ -34,14 +35,14 @@ import ( type authorizeNSEClient struct { policies policiesList - nsePathIdsMap *PathIdsMap + nsePathIdsMap *genericsync.Map[string, []string] } // NewNetworkServiceEndpointRegistryClient - returns a new authorization registry.NetworkServiceEndpointRegistryClient // Authorize registry client checks path of NSE. func NewNetworkServiceEndpointRegistryClient(opts ...Option) registry.NetworkServiceEndpointRegistryClient { o := &options{ - resourcePathIdsMap: new(PathIdsMap), + resourcePathIdsMap: new(genericsync.Map[string, []string]), } for _, opt := range opts { diff --git a/pkg/registry/common/authorize/nse_server.go b/pkg/registry/common/authorize/nse_server.go index 69a9af4e0..ccb636262 100644 --- a/pkg/registry/common/authorize/nse_server.go +++ b/pkg/registry/common/authorize/nse_server.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -20,6 +20,7 @@ package authorize import ( "context" + "github.com/edwarnicke/genericsync" "github.com/golang/protobuf/ptypes/empty" "github.com/networkservicemesh/api/pkg/api/registry" @@ -30,14 +31,14 @@ import ( type authorizeNSEServer struct { policies policiesList - nsePathIdsMap *PathIdsMap + nsePathIdsMap *genericsync.Map[string, []string] } // NewNetworkServiceEndpointRegistryServer - returns a new authorization registry.NetworkServiceEndpointRegistryServer // Authorize registry server checks spiffeID of NSE. func NewNetworkServiceEndpointRegistryServer(opts ...Option) registry.NetworkServiceEndpointRegistryServer { o := &options{ - resourcePathIdsMap: new(PathIdsMap), + resourcePathIdsMap: new(genericsync.Map[string, []string]), } for _, opt := range opts { diff --git a/pkg/registry/common/authorize/options.go b/pkg/registry/common/authorize/options.go index ea088bef6..01fa4ded5 100644 --- a/pkg/registry/common/authorize/options.go +++ b/pkg/registry/common/authorize/options.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -17,6 +17,7 @@ package authorize import ( + "github.com/edwarnicke/genericsync" "github.com/pkg/errors" "github.com/networkservicemesh/sdk/pkg/tools/opa" @@ -24,7 +25,7 @@ import ( type options struct { policies policiesList - resourcePathIdsMap *PathIdsMap + resourcePathIdsMap *genericsync.Map[string, []string] } // Option is authorization option for server @@ -53,7 +54,7 @@ func WithPolicies(policyPaths ...string) Option { } // WithResourcePathIdsMap sets map to keep resourcePathIdsMap to authorize connections with Registry Authorize Chain Element -func WithResourcePathIdsMap(m *PathIdsMap) Option { +func WithResourcePathIdsMap(m *genericsync.Map[string, []string]) Option { return func(o *options) { o.resourcePathIdsMap = m } diff --git a/pkg/registry/common/authorize/sync_map.gen.go b/pkg/registry/common/authorize/sync_map.gen.go deleted file mode 100644 index 1e27e2973..000000000 --- a/pkg/registry/common/authorize/sync_map.gen.go +++ /dev/null @@ -1,75 +0,0 @@ -// Code generated by "-output sync_map.gen.go -type PathIdsMap -output sync_map.gen.go -type PathIdsMap"; DO NOT EDIT. -// Install -output sync_map.gen.go -type PathIdsMap by "go get -u github.com/searKing/golang/tools/-output sync_map.gen.go -type PathIdsMap" - -package authorize - -import ( - "sync" // Used by sync.Map. -) - -// Generate code that will fail if the constants change value. -func _() { - // An "cannot convert PathIdsMap literal (type PathIdsMap) to type sync.Map" compiler error signifies that the base type have changed. - // Re-run the go-syncmap command to generate them again. - _ = (sync.Map)(PathIdsMap{}) -} - -var _nil_PathIdsMap_string_value = func() (val []string) { return }() - -// Load returns the value stored in the map for a key, or nil if no -// value is present. -// The ok result indicates whether value was found in the map. -func (m *PathIdsMap) Load(key string) ([]string, bool) { - value, ok := (*sync.Map)(m).Load(key) - if value == nil { - return _nil_PathIdsMap_string_value, ok - } - return value.([]string), ok -} - -// Store sets the value for a key. -func (m *PathIdsMap) Store(key string, value []string) { - (*sync.Map)(m).Store(key, value) -} - -// LoadOrStore returns the existing value for the key if present. -// Otherwise, it stores and returns the given value. -// The loaded result is true if the value was loaded, false if stored. -func (m *PathIdsMap) LoadOrStore(key string, value []string) ([]string, bool) { - actual, loaded := (*sync.Map)(m).LoadOrStore(key, value) - if actual == nil { - return _nil_PathIdsMap_string_value, loaded - } - return actual.([]string), loaded -} - -// LoadAndDelete deletes the value for a key, returning the previous value if any. -// The loaded result reports whether the key was present. -func (m *PathIdsMap) LoadAndDelete(key string) (value []string, loaded bool) { - actual, loaded := (*sync.Map)(m).LoadAndDelete(key) - if actual == nil { - return _nil_PathIdsMap_string_value, loaded - } - return actual.([]string), loaded -} - -// Delete deletes the value for a key. -func (m *PathIdsMap) Delete(key string) { - (*sync.Map)(m).Delete(key) -} - -// Range calls f sequentially for each key and value present in the map. -// If f returns false, range stops the iteration. -// -// Range does not necessarily correspond to any consistent snapshot of the Map's -// contents: no key will be visited more than once, but if the value for any key -// is stored or deleted concurrently, Range may reflect any mapping for that key -// from any point during the Range call. -// -// Range may be O(N) with the number of elements in the map even if f returns -// false after a constant number of calls. -func (m *PathIdsMap) Range(f func(key string, value []string) bool) { - (*sync.Map)(m).Range(func(key, value interface{}) bool { - return f(key.(string), value.([]string)) - }) -} diff --git a/pkg/tools/dnsconfig/config_decoder.go b/pkg/tools/dnsconfig/config_decoder.go index ba333b8db..5cc05e75a 100644 --- a/pkg/tools/dnsconfig/config_decoder.go +++ b/pkg/tools/dnsconfig/config_decoder.go @@ -16,6 +16,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +// Package dnsconfig provides tools for handeling DNS configs package dnsconfig import ( diff --git a/pkg/tools/dnsconfig/gen.go b/pkg/tools/dnsconfig/gen.go deleted file mode 100644 index 05acb1c82..000000000 --- a/pkg/tools/dnsconfig/gen.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2022 Cisco Systems, Inc. -// -// 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 dnsconfig provides sync map like a Go map[string][]*DNSConfig but is safe for concurrent using -package dnsconfig - -import "sync" - -//go:generate go-syncmap -output sync_map.gen.go -type Map - -// Map is like a Go map[string][]*DNSConfig but is safe for concurrent use -// by multiple goroutines without additional locking or coordination -type Map sync.Map diff --git a/pkg/tools/dnsconfig/sync_map.gen.go b/pkg/tools/dnsconfig/sync_map.gen.go deleted file mode 100644 index 0b52c4d42..000000000 --- a/pkg/tools/dnsconfig/sync_map.gen.go +++ /dev/null @@ -1,77 +0,0 @@ -// Code generated by "-output sync_map.gen.go -type Map -output sync_map.gen.go -type Map"; DO NOT EDIT. -// Install -output sync_map.gen.go -type Map by "go get -u github.com/searKing/golang/tools/-output sync_map.gen.go -type Map" - -package dnsconfig - -import ( - "sync" // Used by sync.Map. - - "github.com/networkservicemesh/api/pkg/api/networkservice" -) - -// Generate code that will fail if the constants change value. -func _() { - // An "cannot convert Map literal (type Map) to type sync.Map" compiler error signifies that the base type have changed. - // Re-run the go-syncmap command to generate them again. - _ = (sync.Map)(Map{}) -} - -var _nil_Map_networkservice_DNSConfig_value = func() (val []*networkservice.DNSConfig) { return }() - -// Load returns the value stored in the map for a key, or nil if no -// value is present. -// The ok result indicates whether value was found in the map. -func (m *Map) Load(key string) ([]*networkservice.DNSConfig, bool) { - value, ok := (*sync.Map)(m).Load(key) - if value == nil { - return _nil_Map_networkservice_DNSConfig_value, ok - } - return value.([]*networkservice.DNSConfig), ok -} - -// Store sets the value for a key. -func (m *Map) Store(key string, value []*networkservice.DNSConfig) { - (*sync.Map)(m).Store(key, value) -} - -// LoadOrStore returns the existing value for the key if present. -// Otherwise, it stores and returns the given value. -// The loaded result is true if the value was loaded, false if stored. -func (m *Map) LoadOrStore(key string, value []*networkservice.DNSConfig) ([]*networkservice.DNSConfig, bool) { - actual, loaded := (*sync.Map)(m).LoadOrStore(key, value) - if actual == nil { - return _nil_Map_networkservice_DNSConfig_value, loaded - } - return actual.([]*networkservice.DNSConfig), loaded -} - -// LoadAndDelete deletes the value for a key, returning the previous value if any. -// The loaded result reports whether the key was present. -func (m *Map) LoadAndDelete(key string) (value []*networkservice.DNSConfig, loaded bool) { - actual, loaded := (*sync.Map)(m).LoadAndDelete(key) - if actual == nil { - return _nil_Map_networkservice_DNSConfig_value, loaded - } - return actual.([]*networkservice.DNSConfig), loaded -} - -// Delete deletes the value for a key. -func (m *Map) Delete(key string) { - (*sync.Map)(m).Delete(key) -} - -// Range calls f sequentially for each key and value present in the map. -// If f returns false, range stops the iteration. -// -// Range does not necessarily correspond to any consistent snapshot of the Map's -// contents: no key will be visited more than once, but if the value for any key -// is stored or deleted concurrently, Range may reflect any mapping for that key -// from any point during the Range call. -// -// Range may be O(N) with the number of elements in the map even if f returns -// false after a constant number of calls. -func (m *Map) Range(f func(key string, value []*networkservice.DNSConfig) bool) { - (*sync.Map)(m).Range(func(key, value interface{}) bool { - return f(key.(string), value.([]*networkservice.DNSConfig)) - }) -} diff --git a/pkg/tools/dnsutils/cache/gen.go b/pkg/tools/dnsutils/cache/gen.go deleted file mode 100644 index 19e4d40e0..000000000 --- a/pkg/tools/dnsutils/cache/gen.go +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) 2022 Cisco Systems, Inc. -// -// 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 cache - -import "sync" - -//go:generate go-syncmap -output sync_map.gen.go -type msgMap - -// msgMap is like a Go map[dns.Question]*dns.Msg but is safe for concurrent use -// by multiple goroutines without additional locking or coordination -type msgMap sync.Map diff --git a/pkg/tools/dnsutils/cache/handler.go b/pkg/tools/dnsutils/cache/handler.go index 1bbc7ea03..28d5d2642 100644 --- a/pkg/tools/dnsutils/cache/handler.go +++ b/pkg/tools/dnsutils/cache/handler.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -22,6 +22,7 @@ import ( "sync" "time" + "github.com/edwarnicke/genericsync" "github.com/miekg/dns" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils" @@ -30,7 +31,7 @@ import ( ) type dnsCacheHandler struct { - cache *msgMap + cache *genericsync.Map[dns.Question, *dns.Msg] lastTTLUpdate time.Time m sync.Mutex @@ -100,7 +101,7 @@ func validateMsg(m *dns.Msg) bool { // NewDNSHandler creates a new dns handler that stores successful requests to DNS server func NewDNSHandler() dnsutils.Handler { return &dnsCacheHandler{ - cache: new(msgMap), + cache: new(genericsync.Map[dns.Question, *dns.Msg]), lastTTLUpdate: time.Now(), } } diff --git a/pkg/tools/dnsutils/cache/handler_test.go b/pkg/tools/dnsutils/cache/handler_test.go index 9e825d29c..b6a1dd4b2 100644 --- a/pkg/tools/dnsutils/cache/handler_test.go +++ b/pkg/tools/dnsutils/cache/handler_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -21,6 +21,7 @@ import ( "testing" "time" + "github.com/edwarnicke/genericsync" "github.com/miekg/dns" "github.com/stretchr/testify/require" "golang.org/x/net/context" @@ -53,7 +54,7 @@ func TestCache(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() - records := new(memory.Map) + records := new(genericsync.Map[string, []net.IP]) records.Store("example.com.", []net.IP{net.ParseIP("1.1.1.1")}) check := &checkHandler{} diff --git a/pkg/tools/dnsutils/cache/response_writer_wrapper.go b/pkg/tools/dnsutils/cache/response_writer_wrapper.go index d731e4b65..77ea65a02 100644 --- a/pkg/tools/dnsutils/cache/response_writer_wrapper.go +++ b/pkg/tools/dnsutils/cache/response_writer_wrapper.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -17,12 +17,13 @@ package cache import ( + "github.com/edwarnicke/genericsync" "github.com/miekg/dns" ) type responseWriterWrapper struct { dns.ResponseWriter - cache *msgMap + cache *genericsync.Map[dns.Question, *dns.Msg] } func (r *responseWriterWrapper) WriteMsg(m *dns.Msg) error { diff --git a/pkg/tools/dnsutils/cache/sync_map.gen.go b/pkg/tools/dnsutils/cache/sync_map.gen.go deleted file mode 100644 index 6f989e7d8..000000000 --- a/pkg/tools/dnsutils/cache/sync_map.gen.go +++ /dev/null @@ -1,77 +0,0 @@ -// Code generated by "-output sync_map.gen.go -type msgMap -output sync_map.gen.go -type msgMap"; DO NOT EDIT. -// Install -output sync_map.gen.go -type msgMap by "go get -u github.com/searKing/golang/tools/-output sync_map.gen.go -type msgMap" - -package cache - -import ( - "sync" // Used by sync.Map. - - "github.com/miekg/dns" -) - -// Generate code that will fail if the constants change value. -func _() { - // An "cannot convert msgMap literal (type msgMap) to type sync.Map" compiler error signifies that the base type have changed. - // Re-run the go-syncmap command to generate them again. - _ = (sync.Map)(msgMap{}) -} - -var _nil_msgMap_dns_Msg_value = func() (val *dns.Msg) { return }() - -// Load returns the value stored in the map for a key, or nil if no -// value is present. -// The ok result indicates whether value was found in the map. -func (m *msgMap) Load(key dns.Question) (*dns.Msg, bool) { - value, ok := (*sync.Map)(m).Load(key) - if value == nil { - return _nil_msgMap_dns_Msg_value, ok - } - return value.(*dns.Msg), ok -} - -// Store sets the value for a key. -func (m *msgMap) Store(key dns.Question, value *dns.Msg) { - (*sync.Map)(m).Store(key, value) -} - -// LoadOrStore returns the existing value for the key if present. -// Otherwise, it stores and returns the given value. -// The loaded result is true if the value was loaded, false if stored. -func (m *msgMap) LoadOrStore(key dns.Question, value *dns.Msg) (*dns.Msg, bool) { - actual, loaded := (*sync.Map)(m).LoadOrStore(key, value) - if actual == nil { - return _nil_msgMap_dns_Msg_value, loaded - } - return actual.(*dns.Msg), loaded -} - -// LoadAndDelete deletes the value for a key, returning the previous value if any. -// The loaded result reports whether the key was present. -func (m *msgMap) LoadAndDelete(key dns.Question) (value *dns.Msg, loaded bool) { - actual, loaded := (*sync.Map)(m).LoadAndDelete(key) - if actual == nil { - return _nil_msgMap_dns_Msg_value, loaded - } - return actual.(*dns.Msg), loaded -} - -// Delete deletes the value for a key. -func (m *msgMap) Delete(key dns.Question) { - (*sync.Map)(m).Delete(key) -} - -// Range calls f sequentially for each key and value present in the map. -// If f returns false, range stops the iteration. -// -// Range does not necessarily correspond to any consistent snapshot of the Map's -// contents: no key will be visited more than once, but if the value for any key -// is stored or deleted concurrently, Range may reflect any mapping for that key -// from any point during the Range call. -// -// Range may be O(N) with the number of elements in the map even if f returns -// false after a constant number of calls. -func (m *msgMap) Range(f func(key dns.Question, value *dns.Msg) bool) { - (*sync.Map)(m).Range(func(key, value interface{}) bool { - return f(key.(dns.Question), value.(*dns.Msg)) - }) -} diff --git a/pkg/tools/dnsutils/dnsconfigs/handler.go b/pkg/tools/dnsutils/dnsconfigs/handler.go index dd13fd882..11a4c4052 100644 --- a/pkg/tools/dnsutils/dnsconfigs/handler.go +++ b/pkg/tools/dnsutils/dnsconfigs/handler.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -21,12 +21,12 @@ import ( "context" "net/url" + "github.com/edwarnicke/genericsync" "github.com/miekg/dns" "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/networkservicemesh/sdk/pkg/tools/clienturlctx" - "github.com/networkservicemesh/sdk/pkg/tools/dnsconfig" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/next" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/searches" @@ -34,7 +34,7 @@ import ( ) type dnsConfigsHandler struct { - configs *dnsconfig.Map + configs *genericsync.Map[string, []*networkservice.DNSConfig] } func (h *dnsConfigsHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, m *dns.Msg) { @@ -87,7 +87,7 @@ func (h *dnsConfigsHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, } // NewDNSHandler creates a new dns handler that stores DNS configs -func NewDNSHandler(configs *dnsconfig.Map) dnsutils.Handler { +func NewDNSHandler(configs *genericsync.Map[string, []*networkservice.DNSConfig]) dnsutils.Handler { return &dnsConfigsHandler{ configs: configs, } diff --git a/pkg/tools/dnsutils/dnsconfigs/handler_test.go b/pkg/tools/dnsutils/dnsconfigs/handler_test.go index 90318aea9..4fa41ef1e 100644 --- a/pkg/tools/dnsutils/dnsconfigs/handler_test.go +++ b/pkg/tools/dnsutils/dnsconfigs/handler_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -20,13 +20,13 @@ import ( "testing" "time" + "github.com/edwarnicke/genericsync" "github.com/miekg/dns" "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/stretchr/testify/require" "golang.org/x/net/context" "github.com/networkservicemesh/sdk/pkg/tools/clienturlctx" - "github.com/networkservicemesh/sdk/pkg/tools/dnsconfig" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/dnsconfigs" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/next" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/searches" @@ -61,7 +61,7 @@ func TestDNSConfigs(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() - configs := new(dnsconfig.Map) + configs := new(genericsync.Map[string, []*networkservice.DNSConfig]) configs.Store("1", []*networkservice.DNSConfig{ { diff --git a/pkg/tools/dnsutils/dnsconfigs/tcp_dns_server_test.go b/pkg/tools/dnsutils/dnsconfigs/tcp_dns_server_test.go index 3cb992d82..0dcd623c2 100644 --- a/pkg/tools/dnsutils/dnsconfigs/tcp_dns_server_test.go +++ b/pkg/tools/dnsutils/dnsconfigs/tcp_dns_server_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -26,12 +26,12 @@ import ( "testing" "time" + "github.com/edwarnicke/genericsync" "github.com/miekg/dns" "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/stretchr/testify/require" "go.uber.org/goleak" - "github.com/networkservicemesh/sdk/pkg/tools/dnsconfig" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/dnsconfigs" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/fanout" @@ -150,7 +150,7 @@ func Test_TCPDNSServerTimeout(t *testing.T) { require.NoError(t, err) clientAddr := fmt.Sprintf("127.0.0.1:%d", clientPort) - dnsConfigsMap := new(dnsconfig.Map) + dnsConfigsMap := new(genericsync.Map[string, []*networkservice.DNSConfig]) dnsConfigsMap.Store("1", []*networkservice.DNSConfig{ { DnsServerIps: []string{proxyAddr}, diff --git a/pkg/tools/dnsutils/memory/gen.go b/pkg/tools/dnsutils/memory/gen.go deleted file mode 100644 index b276ba453..000000000 --- a/pkg/tools/dnsutils/memory/gen.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2022 Cisco Systems, Inc. -// -// 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 memory - -import ( - "sync" -) - -//go:generate go-syncmap -output sync_map.gen.go -type Map - -// Map is like a Go map[string][]net.IP but is safe for concurrent use -// by multiple goroutines without additional locking or coordination -type Map sync.Map diff --git a/pkg/tools/dnsutils/memory/handler.go b/pkg/tools/dnsutils/memory/handler.go index 1ebe43824..8beea1a08 100644 --- a/pkg/tools/dnsutils/memory/handler.go +++ b/pkg/tools/dnsutils/memory/handler.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco Systems, Inc. +// Copyright (c) 2022-2023 Cisco Systems, Inc. // // SPDX-License-Identifier: Apache-2.0 // @@ -21,6 +21,7 @@ import ( "context" "net" + "github.com/edwarnicke/genericsync" "github.com/miekg/dns" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils" @@ -43,7 +44,7 @@ func (r *responseWriter) WriteMsg(m *dns.Msg) error { } type memoryHandler struct { - records *Map + records *genericsync.Map[string, []net.IP] } func (f *memoryHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, msg *dns.Msg) { @@ -96,7 +97,7 @@ func (f *memoryHandler) ServeDNS(ctx context.Context, rw dns.ResponseWriter, msg } // NewDNSHandler creates a new dns handler instance that stores a/aaaa answers -func NewDNSHandler(records *Map) dnsutils.Handler { +func NewDNSHandler(records *genericsync.Map[string, []net.IP]) dnsutils.Handler { if records == nil { panic("records cannot be nil") } diff --git a/pkg/tools/dnsutils/memory/handler_test.go b/pkg/tools/dnsutils/memory/handler_test.go index 9afc5dcba..30271efce 100644 --- a/pkg/tools/dnsutils/memory/handler_test.go +++ b/pkg/tools/dnsutils/memory/handler_test.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -22,6 +22,7 @@ import ( "testing" "time" + "github.com/edwarnicke/genericsync" "github.com/miekg/dns" "github.com/stretchr/testify/require" @@ -44,7 +45,7 @@ func TestDomainSearches(t *testing.T) { defer cancel() // Store two entries for IPv4 and IPv6 - records := new(memory.Map) + records := new(genericsync.Map[string, []net.IP]) records.Store("example.com.", []net.IP{net.ParseIP("1.1.1.1")}) records.Store("example.net.", []net.IP{net.ParseIP("2001:db8::68")}) diff --git a/pkg/tools/dnsutils/memory/sync_map.gen.go b/pkg/tools/dnsutils/memory/sync_map.gen.go deleted file mode 100644 index 67b5525a5..000000000 --- a/pkg/tools/dnsutils/memory/sync_map.gen.go +++ /dev/null @@ -1,76 +0,0 @@ -// Code generated by "-output sync_map.gen.go -type Map -output sync_map.gen.go -type Map"; DO NOT EDIT. -// Install -output sync_map.gen.go -type Map by "go get -u github.com/searKing/golang/tools/-output sync_map.gen.go -type Map" - -package memory - -import ( - "net" - "sync" // Used by sync.Map. -) - -// Generate code that will fail if the constants change value. -func _() { - // An "cannot convert Map literal (type Map) to type sync.Map" compiler error signifies that the base type have changed. - // Re-run the go-syncmap command to generate them again. - _ = (sync.Map)(Map{}) -} - -var _nil_Map_net_IP_value = func() (val []net.IP) { return }() - -// Load returns the value stored in the map for a key, or nil if no -// value is present. -// The ok result indicates whether value was found in the map. -func (m *Map) Load(key string) ([]net.IP, bool) { - value, ok := (*sync.Map)(m).Load(key) - if value == nil { - return _nil_Map_net_IP_value, ok - } - return value.([]net.IP), ok -} - -// Store sets the value for a key. -func (m *Map) Store(key string, value []net.IP) { - (*sync.Map)(m).Store(key, value) -} - -// LoadOrStore returns the existing value for the key if present. -// Otherwise, it stores and returns the given value. -// The loaded result is true if the value was loaded, false if stored. -func (m *Map) LoadOrStore(key string, value []net.IP) ([]net.IP, bool) { - actual, loaded := (*sync.Map)(m).LoadOrStore(key, value) - if actual == nil { - return _nil_Map_net_IP_value, loaded - } - return actual.([]net.IP), loaded -} - -// LoadAndDelete deletes the value for a key, returning the previous value if any. -// The loaded result reports whether the key was present. -func (m *Map) LoadAndDelete(key string) (value []net.IP, loaded bool) { - actual, loaded := (*sync.Map)(m).LoadAndDelete(key) - if actual == nil { - return _nil_Map_net_IP_value, loaded - } - return actual.([]net.IP), loaded -} - -// Delete deletes the value for a key. -func (m *Map) Delete(key string) { - (*sync.Map)(m).Delete(key) -} - -// Range calls f sequentially for each key and value present in the map. -// If f returns false, range stops the iteration. -// -// Range does not necessarily correspond to any consistent snapshot of the Map's -// contents: no key will be visited more than once, but if the value for any key -// is stored or deleted concurrently, Range may reflect any mapping for that key -// from any point during the Range call. -// -// Range may be O(N) with the number of elements in the map even if f returns -// false after a constant number of calls. -func (m *Map) Range(f func(key string, value []net.IP) bool) { - (*sync.Map)(m).Range(func(key, value interface{}) bool { - return f(key.(string), value.([]net.IP)) - }) -} diff --git a/pkg/tools/dnsutils/searches/handler_test.go b/pkg/tools/dnsutils/searches/handler_test.go index dd3ef92a6..d26380979 100644 --- a/pkg/tools/dnsutils/searches/handler_test.go +++ b/pkg/tools/dnsutils/searches/handler_test.go @@ -21,12 +21,12 @@ import ( "testing" "time" + "github.com/edwarnicke/genericsync" "github.com/miekg/dns" "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/stretchr/testify/require" "golang.org/x/net/context" - "github.com/networkservicemesh/sdk/pkg/tools/dnsconfig" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/dnsconfigs" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/memory" "github.com/networkservicemesh/sdk/pkg/tools/dnsutils/next" @@ -56,13 +56,13 @@ func TestDomainSearches(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() - configs := new(dnsconfig.Map) + configs := new(genericsync.Map[string, []*networkservice.DNSConfig]) configs.Store("1", []*networkservice.DNSConfig{ {SearchDomains: []string{"com", "net", "org"}, DnsServerIps: []string{"8.8.4.4"}}, }) - records := new(memory.Map) + records := new(genericsync.Map[string, []net.IP]) records.Store("example.com.", []net.IP{net.ParseIP("1.1.1.1")}) check := &checkHandler{} diff --git a/pkg/tools/monitorconnection/authorize/options.go b/pkg/tools/monitorconnection/authorize/options.go index d0435d485..e8d45f6cf 100644 --- a/pkg/tools/monitorconnection/authorize/options.go +++ b/pkg/tools/monitorconnection/authorize/options.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -16,11 +16,14 @@ package authorize -import "github.com/networkservicemesh/sdk/pkg/tools/spire" +import ( + "github.com/edwarnicke/genericsync" + "github.com/spiffe/go-spiffe/v2/spiffeid" +) type options struct { policies policiesList - spiffeIDConnectionMap *spire.SpiffeIDConnectionMap + spiffeIDConnectionMap *genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]] } // Option is authorization option for monitor connection server @@ -39,7 +42,7 @@ func WithPolicies(p ...Policy) Option { } // WithSpiffeIDConnectionMap sets map to keep spiffeIDConnectionMap to authorize connections with MonitorServer -func WithSpiffeIDConnectionMap(s *spire.SpiffeIDConnectionMap) Option { +func WithSpiffeIDConnectionMap(s *genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]]) Option { return func(o *options) { o.spiffeIDConnectionMap = s } diff --git a/pkg/tools/monitorconnection/authorize/server.go b/pkg/tools/monitorconnection/authorize/server.go index 8d283c23e..6c053dcc5 100644 --- a/pkg/tools/monitorconnection/authorize/server.go +++ b/pkg/tools/monitorconnection/authorize/server.go @@ -1,4 +1,4 @@ -// Copyright (c) 2022 Cisco and/or its affiliates. +// Copyright (c) 2022-2023 Cisco and/or its affiliates. // // SPDX-License-Identifier: Apache-2.0 // @@ -18,24 +18,24 @@ package authorize import ( + "github.com/edwarnicke/genericsync" "github.com/networkservicemesh/api/pkg/api/networkservice" "github.com/spiffe/go-spiffe/v2/spiffeid" "github.com/networkservicemesh/sdk/pkg/tools/monitorconnection/next" "github.com/networkservicemesh/sdk/pkg/tools/spire" - "github.com/networkservicemesh/sdk/pkg/tools/stringset" ) type authorizeMonitorConnectionsServer struct { policies policiesList - spiffeIDConnectionMap *spire.SpiffeIDConnectionMap + spiffeIDConnectionMap *genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]] } // NewMonitorConnectionServer - returns a new authorization networkservicemesh.MonitorConnectionServer func NewMonitorConnectionServer(opts ...Option) networkservice.MonitorConnectionServer { o := &options{ // policies: policiesList{opa.WithMonitorConnectionServerPolicy()}, - spiffeIDConnectionMap: &spire.SpiffeIDConnectionMap{}, + spiffeIDConnectionMap: &genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]]{}, } for _, opt := range opts { opt(o) @@ -59,7 +59,7 @@ func (a *authorizeMonitorConnectionsServer) MonitorConnections(in *networkservic simpleMap := make(map[string][]string) a.spiffeIDConnectionMap.Range( - func(sid spiffeid.ID, connIds *stringset.StringSet) bool { + func(sid spiffeid.ID, connIds *genericsync.Map[string, struct{}]) bool { connIds.Range( func(connId string, _ struct{}) bool { ids := simpleMap[sid.String()] diff --git a/pkg/tools/monitorconnection/authorize/server_test.go b/pkg/tools/monitorconnection/authorize/server_test.go index b3c3bdc2c..6b5cb6140 100644 --- a/pkg/tools/monitorconnection/authorize/server_test.go +++ b/pkg/tools/monitorconnection/authorize/server_test.go @@ -30,13 +30,12 @@ import ( "google.golang.org/grpc/peer" "google.golang.org/grpc/status" + "github.com/edwarnicke/genericsync" "github.com/pkg/errors" "github.com/spiffe/go-spiffe/v2/spiffeid" "github.com/networkservicemesh/sdk/pkg/tools/monitorconnection/authorize" "github.com/networkservicemesh/sdk/pkg/tools/opa" - "github.com/networkservicemesh/sdk/pkg/tools/spire" - "github.com/networkservicemesh/sdk/pkg/tools/stringset" "github.com/networkservicemesh/api/pkg/api/networkservice" @@ -188,9 +187,9 @@ func TestAuthzEndpoint(t *testing.T) { baseCtx, err = getContextWithTLSCert() require.NoError(t, err) } - spiffeIDConnectionMap := spire.SpiffeIDConnectionMap{} + var spiffeIDConnectionMap genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]] for spiffeIDstr, connIds := range s.spiffeIDConnMap { - connIDMap := stringset.StringSet{} + connIDMap := genericsync.Map[string, struct{}]{} for _, connID := range connIds { connIDMap.Store(connID, struct{}{}) } @@ -234,8 +233,8 @@ func TestAuthorize_ShouldCorrectlyWorkWithHeal(t *testing.T) { selector, &testEmptyMCMCServer{context: ctx}) require.NoError(t, err) - spiffeIDConnectionMap := spire.SpiffeIDConnectionMap{} - connMap := stringset.StringSet{} + spiffeIDConnectionMap := genericsync.Map[spiffeid.ID, *genericsync.Map[string, struct{}]]{} + connMap := genericsync.Map[string, struct{}]{} var placer struct{} connMap.Store("conn1", placer) var spiffeID spiffeid.ID diff --git a/pkg/tools/spire/gen_spiffeid_conn_map.go b/pkg/tools/spire/gen_spiffeid_conn_map.go deleted file mode 100644 index 599b39190..000000000 --- a/pkg/tools/spire/gen_spiffeid_conn_map.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2022 Cisco 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 spire - -import ( - "sync" -) - -//go:generate go-syncmap -output spiffe_id_connection_map.gen.go -type SpiffeIDConnectionMap - -// SpiffeIDConnectionMap - sync.Map with key == spiffeid.ID and value == *StringSet -type SpiffeIDConnectionMap sync.Map diff --git a/pkg/tools/spire/spiffe_id_connection_map.gen.go b/pkg/tools/spire/spiffe_id_connection_map.gen.go deleted file mode 100644 index 88cce6c68..000000000 --- a/pkg/tools/spire/spiffe_id_connection_map.gen.go +++ /dev/null @@ -1,79 +0,0 @@ -// Code generated by "-output spiffe_id_connection_map.gen.go -type SpiffeIDConnectionMap -output spiffe_id_connection_map.gen.go -type SpiffeIDConnectionMap"; DO NOT EDIT. -// Install -output spiffe_id_connection_map.gen.go -type SpiffeIDConnectionMap by "go get -u github.com/searKing/golang/tools/-output spiffe_id_connection_map.gen.go -type SpiffeIDConnectionMap" - -package spire - -import ( - "sync" // Used by sync.Map. - - "github.com/spiffe/go-spiffe/v2/spiffeid" - - "github.com/networkservicemesh/sdk/pkg/tools/stringset" -) - -// Generate code that will fail if the constants change value. -func _() { - // An "cannot convert SpiffeIDConnectionMap literal (type SpiffeIDConnectionMap) to type sync.Map" compiler error signifies that the base type have changed. - // Re-run the go-syncmap command to generate them again. - _ = (sync.Map)(SpiffeIDConnectionMap{}) -} - -var _nil_SpiffeIDConnectionMap_stringset_StringSet_value = func() (val *stringset.StringSet) { return }() - -// Load returns the value stored in the map for a key, or nil if no -// value is present. -// The ok result indicates whether value was found in the map. -func (m *SpiffeIDConnectionMap) Load(key spiffeid.ID) (*stringset.StringSet, bool) { - value, ok := (*sync.Map)(m).Load(key) - if value == nil { - return _nil_SpiffeIDConnectionMap_stringset_StringSet_value, ok - } - return value.(*stringset.StringSet), ok -} - -// Store sets the value for a key. -func (m *SpiffeIDConnectionMap) Store(key spiffeid.ID, value *stringset.StringSet) { - (*sync.Map)(m).Store(key, value) -} - -// LoadOrStore returns the existing value for the key if present. -// Otherwise, it stores and returns the given value. -// The loaded result is true if the value was loaded, false if stored. -func (m *SpiffeIDConnectionMap) LoadOrStore(key spiffeid.ID, value *stringset.StringSet) (*stringset.StringSet, bool) { - actual, loaded := (*sync.Map)(m).LoadOrStore(key, value) - if actual == nil { - return _nil_SpiffeIDConnectionMap_stringset_StringSet_value, loaded - } - return actual.(*stringset.StringSet), loaded -} - -// LoadAndDelete deletes the value for a key, returning the previous value if any. -// The loaded result reports whether the key was present. -func (m *SpiffeIDConnectionMap) LoadAndDelete(key spiffeid.ID) (value *stringset.StringSet, loaded bool) { - actual, loaded := (*sync.Map)(m).LoadAndDelete(key) - if actual == nil { - return _nil_SpiffeIDConnectionMap_stringset_StringSet_value, loaded - } - return actual.(*stringset.StringSet), loaded -} - -// Delete deletes the value for a key. -func (m *SpiffeIDConnectionMap) Delete(key spiffeid.ID) { - (*sync.Map)(m).Delete(key) -} - -// Range calls f sequentially for each key and value present in the map. -// If f returns false, range stops the iteration. -// -// Range does not necessarily correspond to any consistent snapshot of the Map's -// contents: no key will be visited more than once, but if the value for any key -// is stored or deleted concurrently, Range may reflect any mapping for that key -// from any point during the Range call. -// -// Range may be O(N) with the number of elements in the map even if f returns -// false after a constant number of calls. -func (m *SpiffeIDConnectionMap) Range(f func(key spiffeid.ID, value *stringset.StringSet) bool) { - (*sync.Map)(m).Range(func(key, value interface{}) bool { - return f(key.(spiffeid.ID), value.(*stringset.StringSet)) - }) -} diff --git a/pkg/tools/stringset/gen.go b/pkg/tools/stringset/gen.go deleted file mode 100644 index 1e4ef8600..000000000 --- a/pkg/tools/stringset/gen.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright (c) 2022-2023 Cisco 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 stringset contains StringSet type definition -package stringset - -import ( - "sync" -) - -//go:generate go-syncmap -output sync_map.gen.go -type StringSet - -// StringSet - sync.Map with key == string and value == struct{} -type StringSet sync.Map diff --git a/pkg/tools/stringset/sync_map.gen.go b/pkg/tools/stringset/sync_map.gen.go deleted file mode 100644 index 839d8e7cd..000000000 --- a/pkg/tools/stringset/sync_map.gen.go +++ /dev/null @@ -1,75 +0,0 @@ -// Code generated by "-output sync_map.gen.go -type StringSet -output sync_map.gen.go -type StringSet"; DO NOT EDIT. -// Install -output sync_map.gen.go -type StringSet by "go get -u github.com/searKing/golang/tools/-output sync_map.gen.go -type StringSet" - -package stringset - -import ( - "sync" // Used by sync.Map. -) - -// Generate code that will fail if the constants change value. -func _() { - // An "cannot convert StringSet literal (type StringSet) to type sync.Map" compiler error signifies that the base type have changed. - // Re-run the go-syncmap command to generate them again. - _ = (sync.Map)(StringSet{}) -} - -var _nil_StringSet_struct___value = func() (val struct{}) { return }() - -// Load returns the value stored in the map for a key, or nil if no -// value is present. -// The ok result indicates whether value was found in the map. -func (m *StringSet) Load(key string) (struct{}, bool) { - value, ok := (*sync.Map)(m).Load(key) - if value == nil { - return _nil_StringSet_struct___value, ok - } - return value.(struct{}), ok -} - -// Store sets the value for a key. -func (m *StringSet) Store(key string, value struct{}) { - (*sync.Map)(m).Store(key, value) -} - -// LoadOrStore returns the existing value for the key if present. -// Otherwise, it stores and returns the given value. -// The loaded result is true if the value was loaded, false if stored. -func (m *StringSet) LoadOrStore(key string, value struct{}) (struct{}, bool) { - actual, loaded := (*sync.Map)(m).LoadOrStore(key, value) - if actual == nil { - return _nil_StringSet_struct___value, loaded - } - return actual.(struct{}), loaded -} - -// LoadAndDelete deletes the value for a key, returning the previous value if any. -// The loaded result reports whether the key was present. -func (m *StringSet) LoadAndDelete(key string) (value struct{}, loaded bool) { - actual, loaded := (*sync.Map)(m).LoadAndDelete(key) - if actual == nil { - return _nil_StringSet_struct___value, loaded - } - return actual.(struct{}), loaded -} - -// Delete deletes the value for a key. -func (m *StringSet) Delete(key string) { - (*sync.Map)(m).Delete(key) -} - -// Range calls f sequentially for each key and value present in the map. -// If f returns false, range stops the iteration. -// -// Range does not necessarily correspond to any consistent snapshot of the Map's -// contents: no key will be visited more than once, but if the value for any key -// is stored or deleted concurrently, Range may reflect any mapping for that key -// from any point during the Range call. -// -// Range may be O(N) with the number of elements in the map even if f returns -// false after a constant number of calls. -func (m *StringSet) Range(f func(key string, value struct{}) bool) { - (*sync.Map)(m).Range(func(key, value interface{}) bool { - return f(key.(string), value.(struct{})) - }) -}