Skip to content

Commit

Permalink
check empty path in getSpiffeIDFromPath function (#1400)
Browse files Browse the repository at this point in the history
Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>

Signed-off-by: Nikita Skrynnik <nikita.skrynnik@xored.com>
  • Loading branch information
NikitaSkrynnik authored Dec 19, 2022
1 parent 12616b1 commit 554e6ac
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 48 deletions.
23 changes: 17 additions & 6 deletions pkg/registry/common/authorize/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"

"github.com/golang-jwt/jwt/v4"
"github.com/pkg/errors"
"github.com/spiffe/go-spiffe/v2/spiffeid"

"github.com/networkservicemesh/sdk/pkg/registry/common/grpcmetadata"
Expand Down Expand Up @@ -73,22 +72,34 @@ func getRawMap(m *PathIdsMap) map[string][]string {
return rawMap
}

func getSpiffeIDFromPath(path *grpcmetadata.Path) (spiffeid.ID, error) {
func getSpiffeIDFromPath(ctx context.Context, path *grpcmetadata.Path) spiffeid.ID {
if len(path.PathSegments) == 0 {
log.FromContext(ctx).Warn("can't get spiffe id from empty path")
}
tokenString := path.PathSegments[0].Token

claims := jwt.MapClaims{}
_, _, err := jwt.NewParser().ParseUnverified(tokenString, &claims)
if err != nil {
return spiffeid.ID{}, errors.Errorf("failed to parse jwt token: %s", err.Error())
log.FromContext(ctx).Warnf("failed to parse jwt token: %s", err.Error())
return spiffeid.ID{}
}

sub, ok := claims["sub"]
if !ok {
return spiffeid.ID{}, errors.New("failed to get field 'sub' from jwt token payload")
log.FromContext(ctx).Warn("failed to get field 'sub' from jwt token payload")
return spiffeid.ID{}
}
subString, ok := sub.(string)
if !ok {
return spiffeid.ID{}, errors.New("failed to convert field 'sub' from jwt token payload to string")
log.FromContext(ctx).Warn("failed to convert field 'sub' from jwt token payload to string")
return spiffeid.ID{}
}

id, err := spiffeid.FromString(subString)
if err != nil {
log.FromContext(ctx).Warn("failed to parse spiffeid from string: %s", err.Error())
return spiffeid.ID{}
}
return spiffeid.FromString(subString)
return id
}
16 changes: 4 additions & 12 deletions pkg/registry/common/authorize/ns_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,9 @@ func (c *authorizeNSClient) Register(ctx context.Context, ns *registry.NetworkSe
}

path = grpcmetadata.PathFromContext(ctx)

spiffeID, err := getSpiffeIDFromPath(path)
if err != nil {
return nil, err
}

spiffeID := getSpiffeIDFromPath(ctx, path)
rawMap := getRawMap(c.nsPathIdsMap)

input := RegistryOpaInput{
ResourceID: spiffeID.String(),
ResourceName: resp.Name,
Expand Down Expand Up @@ -127,13 +123,9 @@ func (c *authorizeNSClient) Unregister(ctx context.Context, ns *registry.Network
}

path = grpcmetadata.PathFromContext(ctx)

spiffeID, err := getSpiffeIDFromPath(path)
if err != nil {
return nil, err
}

spiffeID := getSpiffeIDFromPath(ctx, path)
rawMap := getRawMap(c.nsPathIdsMap)

input := RegistryOpaInput{
ResourceID: spiffeID.String(),
ResourceName: ns.Name,
Expand Down
12 changes: 2 additions & 10 deletions pkg/registry/common/authorize/ns_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ func (s *authorizeNSServer) Register(ctx context.Context, ns *registry.NetworkSe
}

path := grpcmetadata.PathFromContext(ctx)

spiffeID, err := getSpiffeIDFromPath(path)
if err != nil {
return nil, err
}
spiffeID := getSpiffeIDFromPath(ctx, path)

index := path.Index
var leftSide = &grpcmetadata.Path{
Expand Down Expand Up @@ -94,11 +90,7 @@ func (s *authorizeNSServer) Unregister(ctx context.Context, ns *registry.Network
}

path := grpcmetadata.PathFromContext(ctx)

spiffeID, err := getSpiffeIDFromPath(path)
if err != nil {
return nil, err
}
spiffeID := getSpiffeIDFromPath(ctx, path)

index := path.Index
var leftSide = &grpcmetadata.Path{
Expand Down
12 changes: 2 additions & 10 deletions pkg/registry/common/authorize/nse_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,7 @@ func (c *authorizeNSEClient) Register(ctx context.Context, nse *registry.Network
}

path = grpcmetadata.PathFromContext(ctx)

spiffeID, err := getSpiffeIDFromPath(path)
if err != nil {
return nil, err
}
spiffeID := getSpiffeIDFromPath(ctx, path)

rawMap := getRawMap(c.nsePathIdsMap)
input := RegistryOpaInput{
Expand Down Expand Up @@ -134,11 +130,7 @@ func (c *authorizeNSEClient) Unregister(ctx context.Context, nse *registry.Netwo
}

path = grpcmetadata.PathFromContext(ctx)

spiffeID, err := getSpiffeIDFromPath(path)
if err != nil {
return nil, err
}
spiffeID := getSpiffeIDFromPath(ctx, path)

rawMap := getRawMap(c.nsePathIdsMap)
input := RegistryOpaInput{
Expand Down
12 changes: 2 additions & 10 deletions pkg/registry/common/authorize/nse_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,7 @@ func (s *authorizeNSEServer) Register(ctx context.Context, nse *registry.Network
}

path := grpcmetadata.PathFromContext(ctx)

spiffeID, err := getSpiffeIDFromPath(path)
if err != nil {
return nil, err
}
spiffeID := getSpiffeIDFromPath(ctx, path)

index := path.Index
var leftSide = &grpcmetadata.Path{
Expand Down Expand Up @@ -95,11 +91,7 @@ func (s *authorizeNSEServer) Unregister(ctx context.Context, nse *registry.Netwo
}

path := grpcmetadata.PathFromContext(ctx)

spiffeID, err := getSpiffeIDFromPath(path)
if err != nil {
return nil, err
}
spiffeID := getSpiffeIDFromPath(ctx, path)

index := path.Index
var leftSide = &grpcmetadata.Path{
Expand Down

0 comments on commit 554e6ac

Please sign in to comment.