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

feat: Add support for custom policies #1395

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
15 changes: 7 additions & 8 deletions pkg/networkservice/chains/nsmgr/single_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ import (
"github.com/networkservicemesh/sdk/pkg/registry/chains/memory"
"github.com/networkservicemesh/sdk/pkg/registry/common/authorize"
"github.com/networkservicemesh/sdk/pkg/tools/clientinfo"
"github.com/networkservicemesh/sdk/pkg/tools/opa"
"github.com/networkservicemesh/sdk/pkg/tools/sandbox"
"github.com/networkservicemesh/sdk/pkg/tools/token"
)
Expand Down Expand Up @@ -501,13 +500,13 @@ func Test_FailedRegistryAuthorization(t *testing.T) {
nsmgrSuppier := func(ctx context.Context, tokenGenerator token.GeneratorFunc, options ...nsmgr.Option) nsmgr.Nsmgr {
options = append(options,
nsmgr.WithAuthorizeNSERegistryServer(
authorize.NewNetworkServiceEndpointRegistryServer(authorize.WithPolicies(opa.WithRegistryClientAllowedPolicy()))),
authorize.NewNetworkServiceEndpointRegistryServer(authorize.WithPolicies("etc/nsm/opa/registry_client_allowed.rego"))),
nsmgr.WithAuthorizeNSRegistryServer(
authorize.NewNetworkServiceRegistryServer(authorize.WithPolicies(opa.WithRegistryClientAllowedPolicy()))),
authorize.NewNetworkServiceRegistryServer(authorize.WithPolicies("etc/nsm/opa/registry_client_allowed.rego"))),
nsmgr.WithAuthorizeNSERegistryClient(
authorize.NewNetworkServiceEndpointRegistryClient(authorize.WithPolicies(opa.WithRegistryClientAllowedPolicy()))),
authorize.NewNetworkServiceEndpointRegistryClient(authorize.WithPolicies("etc/nsm/opa/registry_client_allowed.rego"))),
nsmgr.WithAuthorizeNSRegistryClient(
authorize.NewNetworkServiceRegistryClient(authorize.WithPolicies(opa.WithRegistryClientAllowedPolicy()))),
authorize.NewNetworkServiceRegistryClient(authorize.WithPolicies("etc/nsm/opa/registry_client_allowed.rego"))),
)
return nsmgr.NewServer(ctx, tokenGenerator, options...)
}
Expand All @@ -527,7 +526,7 @@ func Test_FailedRegistryAuthorization(t *testing.T) {
memory.WithProxyRegistryURL(proxyRegistryURL),
memory.WithDialOptions(options...),
memory.WithAuthorizeNSRegistryServer(
authorize.NewNetworkServiceRegistryServer(authorize.WithPolicies(opa.WithRegistryClientAllowedPolicy()))),
authorize.NewNetworkServiceRegistryServer(authorize.WithPolicies("etc/nsm/opa/registry_client_allowed.rego"))),
)
}
domain := sandbox.NewBuilder(ctx, t).
Expand All @@ -554,15 +553,15 @@ func Test_FailedRegistryAuthorization(t *testing.T) {

nsRegistryClient1 := domain.NewNSRegistryClient(ctx, tokenGeneratorFunc("spiffe://test.com/ns-1"),
registryclient.WithAuthorizeNSRegistryClient(
authorize.NewNetworkServiceRegistryClient(authorize.WithPolicies(opa.WithRegistryClientAllowedPolicy()))))
authorize.NewNetworkServiceRegistryClient(authorize.WithPolicies("etc/nsm/opa/registry_client_allowed.rego"))))

ns1 := defaultRegistryService("ns-1")
_, err := nsRegistryClient1.Register(ctx, ns1)
require.NoError(t, err)

nsRegistryClient2 := domain.NewNSRegistryClient(ctx, tokenGeneratorFunc("spiffe://test.com/ns-2"),
registryclient.WithAuthorizeNSRegistryClient(
authorize.NewNetworkServiceRegistryClient(authorize.WithPolicies(opa.WithRegistryClientAllowedPolicy()))))
authorize.NewNetworkServiceRegistryClient(authorize.WithPolicies("etc/nsm/opa/registry_client_allowed.rego"))))

ns2 := defaultRegistryService("ns-1")
_, err = nsRegistryClient2.Register(ctx, ns2)
Expand Down
10 changes: 1 addition & 9 deletions pkg/networkservice/common/authorize/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/networkservicemesh/api/pkg/api/networkservice"

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

Expand All @@ -42,14 +41,7 @@ type authorizeClient struct {
// NewClient - returns a new authorization networkservicemesh.NetworkServiceClient
// Authorize client checks rigiht side of path.
func NewClient(opts ...Option) networkservice.NetworkServiceClient {
o := &options{
policies: policiesList{
opa.WithTokensValidPolicy(),
opa.WithNextTokenSignedPolicy(),
opa.WithTokensExpiredPolicy(),
opa.WithTokenChainPolicy(),
},
}
o := &options{}
for _, opt := range opts {
opt(o)
}
Expand Down
7 changes: 0 additions & 7 deletions pkg/networkservice/common/authorize/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/networkservicemesh/api/pkg/api/networkservice"

"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"
)
Expand All @@ -42,12 +41,6 @@ type authorizeServer struct {
// Authorize server checks left side of Path.
func NewServer(opts ...Option) networkservice.NetworkServiceServer {
o := &options{
policies: policiesList{
opa.WithTokensValidPolicy(),
opa.WithPrevTokenSignedPolicy(),
opa.WithTokensExpiredPolicy(),
opa.WithTokenChainPolicy(),
},
spiffeIDConnectionMap: &spire.SpiffeIDConnectionMap{},
}
for _, opt := range opts {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !linux
// +build !linux

package recvfd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !linux
// +build !linux

package recvfd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !linux
// +build !linux

package sendfd
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:build !linux
// +build !linux

package sendfd
Expand Down
19 changes: 11 additions & 8 deletions pkg/registry/common/authorize/ns_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,25 @@ type authorizeNSClient struct {
// Authorize registry client checks spiffeID of NS.
func NewNetworkServiceRegistryClient(opts ...Option) registry.NetworkServiceRegistryClient {
o := &options{
policies: policiesList{
opa.WithTokensValidPolicy(),
opa.WithNextTokenSignedPolicy(),
opa.WithTokensExpiredPolicy(),
opa.WithTokenChainPolicy(),
opa.WithRegistryClientAllowedPolicy(),
},
resourcePathIdsMap: new(PathIdsMap),
}

for _, opt := range opts {
opt(o)
}

policies, err := opa.PoliciesByFileMask(o.policyPaths...)
if err != nil {
panic(errors.Wrap(err, "failed to read policies in NetworkServiceRegistry authorize client").Error())
}

var policyList policiesList
for _, p := range policies {
policyList = append(policyList, p)
}

return &authorizeNSClient{
policies: o.policies,
policies: policyList,
nsPathIdsMap: o.resourcePathIdsMap,
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/registry/common/authorize/ns_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ import (

"github.com/networkservicemesh/sdk/pkg/registry/common/authorize"
"github.com/networkservicemesh/sdk/pkg/registry/common/grpcmetadata"
"github.com/networkservicemesh/sdk/pkg/tools/opa"

"go.uber.org/goleak"
)

func TestNSRegistryAuthorizeClient(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })
client := authorize.NewNetworkServiceRegistryClient(authorize.WithPolicies(opa.WithRegistryClientAllowedPolicy()))

client := authorize.NewNetworkServiceRegistryClient(authorize.WithPolicies("etc/nsm/opa/registry_client_allowed.rego"))
require.NotNil(t, client)

ns := &registry.NetworkService{Name: "ns"}
path1 := getPath(t, spiffeid1)
Expand Down
20 changes: 12 additions & 8 deletions pkg/registry/common/authorize/ns_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"

"github.com/networkservicemesh/api/pkg/api/registry"

Expand All @@ -38,22 +39,25 @@ type authorizeNSServer struct {
// Authorize registry server checks spiffeID of NS.
func NewNetworkServiceRegistryServer(opts ...Option) registry.NetworkServiceRegistryServer {
o := &options{
policies: policiesList{
opa.WithTokensValidPolicy(),
opa.WithPrevTokenSignedPolicy(),
opa.WithTokensExpiredPolicy(),
opa.WithTokenChainPolicy(),
opa.WithRegistryClientAllowedPolicy(),
},
resourcePathIdsMap: new(PathIdsMap),
}

for _, opt := range opts {
opt(o)
}

policies, err := opa.PoliciesByFileMask(o.policyPaths...)
if err != nil {
panic(errors.Wrap(err, "failed to read policies in NetworkServiceRegistry authorize client").Error())
}

var policyList policiesList
for _, p := range policies {
policyList = append(policyList, p)
}

return &authorizeNSServer{
policies: o.policies,
policies: policyList,
nsPathIdsMap: o.resourcePathIdsMap,
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/registry/common/authorize/ns_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ import (

"github.com/networkservicemesh/sdk/pkg/registry/common/authorize"
"github.com/networkservicemesh/sdk/pkg/registry/common/grpcmetadata"
"github.com/networkservicemesh/sdk/pkg/tools/opa"

"go.uber.org/goleak"
)

func TestNetworkServiceRegistryAuthorization(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })
server := authorize.NewNetworkServiceRegistryServer(authorize.WithPolicies(opa.WithRegistryClientAllowedPolicy()))

server := authorize.NewNetworkServiceRegistryServer(authorize.WithPolicies("etc/nsm/opa/registry_client_allowed.rego"))
require.NotNil(t, server)

ns := &registry.NetworkService{Name: "ns"}
path1 := getPath(t, spiffeid1)
Expand Down
19 changes: 11 additions & 8 deletions pkg/registry/common/authorize/nse_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,25 @@ type authorizeNSEClient struct {
// Authorize registry client checks path of NSE.
func NewNetworkServiceEndpointRegistryClient(opts ...Option) registry.NetworkServiceEndpointRegistryClient {
o := &options{
policies: policiesList{
opa.WithTokensValidPolicy(),
opa.WithNextTokenSignedPolicy(),
opa.WithTokensExpiredPolicy(),
opa.WithTokenChainPolicy(),
opa.WithRegistryClientAllowedPolicy(),
},
resourcePathIdsMap: new(PathIdsMap),
}

for _, opt := range opts {
opt(o)
}

policies, err := opa.PoliciesByFileMask(o.policyPaths...)
if err != nil {
panic(errors.Wrap(err, "failed to read policies in NetworkServiceRegistry authorize client").Error())
}

var policyList policiesList
for _, p := range policies {
policyList = append(policyList, p)
}

return &authorizeNSEClient{
policies: o.policies,
policies: policyList,
nsePathIdsMap: o.resourcePathIdsMap,
}
}
Expand Down
15 changes: 8 additions & 7 deletions pkg/registry/common/authorize/nse_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ import (

"github.com/networkservicemesh/sdk/pkg/registry/common/authorize"
"github.com/networkservicemesh/sdk/pkg/registry/common/grpcmetadata"
"github.com/networkservicemesh/sdk/pkg/tools/opa"

"go.uber.org/goleak"
)

func TestNSERegistryAuthorizeClient(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })
server := authorize.NewNetworkServiceEndpointRegistryClient(authorize.WithPolicies(opa.WithRegistryClientAllowedPolicy()))

client := authorize.NewNetworkServiceEndpointRegistryClient(authorize.WithPolicies("etc/nsm/opa/registry_client_allowed.rego"))
require.NotNil(t, client)

nse := &registry.NetworkServiceEndpoint{Name: "nse"}
path1 := getPath(t, spiffeid1)
Expand All @@ -42,22 +43,22 @@ func TestNSERegistryAuthorizeClient(t *testing.T) {
ctx2 := grpcmetadata.PathWithContext(context.Background(), path2)

nse.PathIds = []string{spiffeid1}
_, err := server.Register(ctx1, nse)
_, err := client.Register(ctx1, nse)
require.NoError(t, err)

nse.PathIds = []string{spiffeid2}
_, err = server.Register(ctx2, nse)
_, err = client.Register(ctx2, nse)
require.Error(t, err)

nse.PathIds = []string{spiffeid1}
_, err = server.Register(ctx1, nse)
_, err = client.Register(ctx1, nse)
require.NoError(t, err)

nse.PathIds = []string{spiffeid2}
_, err = server.Unregister(ctx2, nse)
_, err = client.Unregister(ctx2, nse)
require.Error(t, err)

nse.PathIds = []string{spiffeid1}
_, err = server.Unregister(ctx1, nse)
_, err = client.Unregister(ctx1, nse)
require.NoError(t, err)
}
20 changes: 12 additions & 8 deletions pkg/registry/common/authorize/nse_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/pkg/errors"

"github.com/networkservicemesh/api/pkg/api/registry"

Expand All @@ -38,22 +39,25 @@ type authorizeNSEServer struct {
// Authorize registry server checks spiffeID of NSE.
func NewNetworkServiceEndpointRegistryServer(opts ...Option) registry.NetworkServiceEndpointRegistryServer {
o := &options{
policies: policiesList{
opa.WithTokensValidPolicy(),
opa.WithPrevTokenSignedPolicy(),
opa.WithTokensExpiredPolicy(),
opa.WithTokenChainPolicy(),
opa.WithRegistryClientAllowedPolicy(),
},
resourcePathIdsMap: new(PathIdsMap),
}

for _, opt := range opts {
opt(o)
}

policies, err := opa.PoliciesByFileMask(o.policyPaths...)
if err != nil {
panic(errors.Wrap(err, "failed to read policies in NetworkServiceRegistry authorize client").Error())
}

var policyList policiesList
for _, p := range policies {
policyList = append(policyList, p)
}

return &authorizeNSEServer{
policies: o.policies,
policies: policyList,
nsePathIdsMap: o.resourcePathIdsMap,
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/registry/common/authorize/nse_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ import (

"github.com/networkservicemesh/sdk/pkg/registry/common/authorize"
"github.com/networkservicemesh/sdk/pkg/registry/common/grpcmetadata"
"github.com/networkservicemesh/sdk/pkg/tools/opa"

"go.uber.org/goleak"
)

func TestNetworkServiceEndpointRegistryAuthorization(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })
server := authorize.NewNetworkServiceEndpointRegistryServer(authorize.WithPolicies(opa.WithRegistryClientAllowedPolicy()))

server := authorize.NewNetworkServiceEndpointRegistryServer(authorize.WithPolicies("etc/nsm/opa/registry_client_allowed.rego"))
require.NotNil(t, server)

nse := &registry.NetworkServiceEndpoint{Name: "nse"}
path1 := getPath(t, spiffeid1)
Expand Down
Loading