Skip to content

Commit

Permalink
actualise authorize server with latest main changes (#1071)
Browse files Browse the repository at this point in the history
Signed-off-by: Denis Tingaikin <denis.tingajkin@xored.com>
  • Loading branch information
denis-tingaikin authored Aug 22, 2021
1 parent 9c4ad0c commit 49be40e
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pkg/networkservice/common/authorize/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewClient(opts ...Option) networkservice.NetworkServiceClient {
var result = &authorizeClient{
policies: []Policy{
opa.WithTokensValidPolicy(),
opa.WithCurrentTokenSignedPolicy(),
opa.WithNextTokenSignedPolicy(),
opa.WithTokensExpiredPolicy(),
opa.WithTokenChainPolicy(),
},
Expand Down
16 changes: 9 additions & 7 deletions pkg/networkservice/common/authorize/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc/peer"

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

Expand Down Expand Up @@ -57,9 +58,10 @@ func (a *authorizeServer) Request(ctx context.Context, request *networkservice.N
Index: index,
PathSegments: request.GetConnection().GetPath().GetPathSegments()[:index+1],
}

if err := a.policies.check(ctx, leftSide); err != nil {
return nil, err
if _, ok := peer.FromContext(ctx); ok {
if err := a.policies.check(ctx, leftSide); err != nil {
return nil, err
}
}
return next.Server(ctx).Request(ctx, request)
}
Expand All @@ -70,10 +72,10 @@ func (a *authorizeServer) Close(ctx context.Context, conn *networkservice.Connec
Index: index,
PathSegments: conn.GetPath().GetPathSegments()[:index+1],
}

if err := a.policies.check(ctx, leftSide); err != nil {
return nil, err
if _, ok := peer.FromContext(ctx); ok {
if err := a.policies.check(ctx, leftSide); err != nil {
return nil, err
}
}

return next.Server(ctx).Close(ctx, conn)
}
29 changes: 27 additions & 2 deletions pkg/networkservice/common/authorize/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/peer"
"google.golang.org/grpc/status"

"github.com/networkservicemesh/sdk/pkg/networkservice/common/authorize"
Expand Down Expand Up @@ -58,6 +59,28 @@ func requestWithToken(token string) *networkservice.NetworkServiceRequest {
}
}

func TestAuthorize_ShouldCorrectlyWorkWithHeal(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })

r := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Path: &networkservice.Path{
PathSegments: []*networkservice.PathSegment{
{},
},
},
},
}

// simulate heal request
conn, err := authorize.NewServer().Request(context.Background(), r)
require.NoError(t, err)

// simulate timeout close
_, err = authorize.NewServer().Close(context.Background(), conn)
require.NoError(t, err)
}

func TestAuthzEndpoint(t *testing.T) {
t.Cleanup(func() { goleak.VerifyNone(t) })
suits := []struct {
Expand Down Expand Up @@ -96,10 +119,12 @@ func TestAuthzEndpoint(t *testing.T) {
require.Equal(t, s.Code(), codes.PermissionDenied, "wrong error status code")
}

_, err := srv.Request(context.Background(), s.request)
ctx := peer.NewContext(context.Background(), &peer.Peer{})

_, err := srv.Request(ctx, s.request)
checkResult(err)

_, err = srv.Close(context.Background(), s.request.GetConnection())
_, err = srv.Close(ctx, s.request.GetConnection())
checkResult(err)
})
}
Expand Down
1 change: 0 additions & 1 deletion pkg/networkservice/common/heal/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,5 @@ func (f *healServer) createHealContext(requestCtx, cachedCtx context.Context) co
if candidates := discover.Candidates(ctx); candidates != nil {
healCtx = discover.WithCandidates(healCtx, candidates.Endpoints, candidates.NetworkService)
}

return healCtx
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,10 @@ func Test_CurrentTokenShouldBeSigned_Server(t *testing.T) {
validX509crt, err := x509.ParseCertificate(cert.Certificate[0])
require.Nil(t, err)

var p = opa.WithCurrentTokenSignedPolicy()
var p = opa.WithNextTokenSignedPolicy()
var input = &networkservice.Path{
PathSegments: []*networkservice.PathSegment{
{},
{
Token: token,
},
Expand Down
10 changes: 5 additions & 5 deletions pkg/tools/opa/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var tokensValidPolicySource string
//go:embed policies/prev_token_signed.rego
var prevTokenSignedPolicySource string

//go:embed policies/curr_token_signed.rego
//go:embed policies/next_token_signed.rego
var currTokenSignedPolicySource string

//go:embed policies/tokens_chained.rego
Expand All @@ -42,12 +42,12 @@ func WithTokensValidPolicy() *AuthorizationPolicy {
}
}

// WithCurrentTokenSignedPolicy returns default policy for checking that last token in path is signed.
func WithCurrentTokenSignedPolicy() *AuthorizationPolicy {
// WithNextTokenSignedPolicy returns default policy for checking that last token in path is signed.
func WithNextTokenSignedPolicy() *AuthorizationPolicy {
return &AuthorizationPolicy{
policySource: currTokenSignedPolicySource,
query: "curr_token_signed",
checker: True("curr_token_signed"),
query: "next_token_signed",
checker: True("next_token_signed"),
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

package nsm

default curr_token_signed = false
default index = 0
default next_token_signed = false
default index = 1

index = input.index
index = input.index + 1

curr_token_signed {
next_token_signed {
count(input.path_segments) > index
token := input.path_segments[index].token
cert := input.auth_info.certificate
io.jwt.verify_es256(token, cert) = true
Expand Down

0 comments on commit 49be40e

Please sign in to comment.