-
Notifications
You must be signed in to change notification settings - Fork 665
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Plugins for flyteadmin server middleware (try2) (#428)
- Loading branch information
Showing
5 changed files
with
98 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func BlanketAuthorization(ctx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) ( | ||
resp interface{}, err error) { | ||
|
||
identityContext := IdentityContextFromContext(ctx) | ||
if identityContext.IsEmpty() { | ||
return handler(ctx, req) | ||
} | ||
|
||
if !identityContext.Scopes().Has(ScopeAll) { | ||
return nil, status.Errorf(codes.Unauthenticated, "authenticated user doesn't have required scope") | ||
} | ||
|
||
return handler(ctx, req) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
) | ||
|
||
func TestBlanketAuthorization(t *testing.T) { | ||
t.Run("authenticated and authorized", func(t *testing.T) { | ||
allScopes := sets.NewString(ScopeAll) | ||
identityCtx := IdentityContext{ | ||
audience: "aud", | ||
userID: "uid", | ||
appID: "appid", | ||
scopes: &allScopes, | ||
} | ||
handlerCalled := false | ||
handler := func(ctx context.Context, req interface{}) (interface{}, error) { | ||
handlerCalled = true | ||
return nil, nil | ||
} | ||
ctx := context.WithValue(context.TODO(), ContextKeyIdentityContext, identityCtx) | ||
_, err := BlanketAuthorization(ctx, nil, nil, handler) | ||
assert.NoError(t, err) | ||
assert.True(t, handlerCalled) | ||
}) | ||
t.Run("unauthenticated", func(t *testing.T) { | ||
handlerCalled := false | ||
handler := func(ctx context.Context, req interface{}) (interface{}, error) { | ||
handlerCalled = true | ||
return nil, nil | ||
} | ||
ctx := context.TODO() | ||
_, err := BlanketAuthorization(ctx, nil, nil, handler) | ||
assert.NoError(t, err) | ||
assert.True(t, handlerCalled) | ||
}) | ||
t.Run("authenticated and not authorized", func(t *testing.T) { | ||
identityCtx := IdentityContext{ | ||
audience: "aud", | ||
userID: "uid", | ||
appID: "appid", | ||
} | ||
handlerCalled := false | ||
handler := func(ctx context.Context, req interface{}) (interface{}, error) { | ||
handlerCalled = true | ||
return nil, nil | ||
} | ||
ctx := context.WithValue(context.TODO(), ContextKeyIdentityContext, identityCtx) | ||
_, err := BlanketAuthorization(ctx, nil, nil, handler) | ||
asStatus, ok := status.FromError(err) | ||
assert.True(t, ok) | ||
assert.Equal(t, asStatus.Code(), codes.Unauthenticated) | ||
assert.False(t, handlerCalled) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters