This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Introduce pre redirect hook plugin during auth callback #601
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
92d2a8b
Adding a predredirect hook plugin
pmahindrakar-oss 19ddf62
Add test logs
pmahindrakar-oss 256a582
test logs
pmahindrakar-oss 7c7915d
fix
pmahindrakar-oss 799f72d
Reading identity token for getting subject
pmahindrakar-oss ae9e5cc
reverting
pmahindrakar-oss f81f441
Adding PreRedirectHookError
pmahindrakar-oss 253e0b2
Add some more tests
pmahindrakar-oss 269b0e1
lint fixes
pmahindrakar-oss c7b92c7
removed log line
pmahindrakar-oss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -8,11 +8,6 @@ | |
"strings" | ||
"time" | ||
|
||
"github.com/flyteorg/flyteadmin/auth/interfaces" | ||
"github.com/flyteorg/flyteadmin/pkg/common" | ||
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/service" | ||
"github.com/flyteorg/flytestdlib/errors" | ||
"github.com/flyteorg/flytestdlib/logger" | ||
"github.com/grpc-ecosystem/go-grpc-middleware/util/metautils" | ||
"golang.org/x/oauth2" | ||
"google.golang.org/grpc" | ||
|
@@ -21,6 +16,13 @@ | |
"google.golang.org/grpc/peer" | ||
"google.golang.org/grpc/status" | ||
"google.golang.org/protobuf/runtime/protoiface" | ||
|
||
"github.com/flyteorg/flyteadmin/auth/interfaces" | ||
"github.com/flyteorg/flyteadmin/pkg/common" | ||
"github.com/flyteorg/flyteadmin/plugins" | ||
"github.com/flyteorg/flyteidl/gen/pb-go/flyteidl/service" | ||
"github.com/flyteorg/flytestdlib/errors" | ||
"github.com/flyteorg/flytestdlib/logger" | ||
) | ||
|
||
const ( | ||
|
@@ -29,6 +31,23 @@ | |
FromHTTPVal = "true" | ||
) | ||
|
||
type PreRedirectHookError struct { | ||
Message string | ||
Code int | ||
} | ||
|
||
func (e *PreRedirectHookError) Error() string { | ||
return e.Message | ||
} | ||
|
||
// PreRedirectHookFunc Interface used for running custom code before the redirect happens during a successful auth flow. | ||
// This might be useful in cases where the auth flow allows the user to login since the IDP has been configured | ||
// for eg: to allow all users from a particular domain to login | ||
// but you want to restrict access to only a particular set of user ids. eg : [email protected] are allowed to login but user [email protected], [email protected] | ||
// should only be allowed | ||
// PreRedirectHookError is the error interface which allows the user to set correct http status code and Message to be set in case the function returns an error | ||
// without which the current usage in GetCallbackHandler will set this to InternalServerError | ||
type PreRedirectHookFunc func(ctx context.Context, authCtx interfaces.AuthenticationContext, request *http.Request, w http.ResponseWriter) *PreRedirectHookError | ||
type HTTPRequestToMetadataAnnotator func(ctx context.Context, request *http.Request) metadata.MD | ||
type UserInfoForwardResponseHandler func(ctx context.Context, w http.ResponseWriter, m protoiface.MessageV1) error | ||
|
||
|
@@ -39,11 +58,11 @@ | |
Subject string | ||
} | ||
|
||
func RegisterHandlers(ctx context.Context, handler interfaces.HandlerRegisterer, authCtx interfaces.AuthenticationContext) { | ||
func RegisterHandlers(ctx context.Context, handler interfaces.HandlerRegisterer, authCtx interfaces.AuthenticationContext, pluginRegistry *plugins.Registry) { | ||
// Add HTTP handlers for OAuth2 endpoints | ||
handler.HandleFunc("/login", RefreshTokensIfExists(ctx, authCtx, | ||
GetLoginHandler(ctx, authCtx))) | ||
handler.HandleFunc("/callback", GetCallbackHandler(ctx, authCtx)) | ||
handler.HandleFunc("/callback", GetCallbackHandler(ctx, authCtx, pluginRegistry)) | ||
|
||
// The metadata endpoint is an RFC-defined constant, but we need a leading / for the handler to pattern match correctly. | ||
handler.HandleFunc(fmt.Sprintf("/%s", OIdCMetadataEndpoint), GetOIdCMetadataEndpointRedirectHandler(ctx, authCtx)) | ||
|
@@ -129,14 +148,13 @@ | |
logger.Errorf(ctx, "Was not able to create a redirect cookie") | ||
} | ||
} | ||
|
||
http.Redirect(writer, request, url, http.StatusTemporaryRedirect) | ||
} | ||
} | ||
|
||
// GetCallbackHandler returns a handler that is called by the OIdC provider with the authorization code to complete | ||
// the user authentication flow. | ||
func GetCallbackHandler(ctx context.Context, authCtx interfaces.AuthenticationContext) http.HandlerFunc { | ||
func GetCallbackHandler(ctx context.Context, authCtx interfaces.AuthenticationContext, pluginRegistry *plugins.Registry) http.HandlerFunc { | ||
return func(writer http.ResponseWriter, request *http.Request) { | ||
logger.Debugf(ctx, "Running callback handler... for RequestURI %v", request.RequestURI) | ||
authorizationCode := request.FormValue(AuthorizationResponseCodeType) | ||
|
@@ -178,6 +196,20 @@ | |
return | ||
} | ||
|
||
preRedirectHook := plugins.Get[PreRedirectHookFunc](pluginRegistry, plugins.PluginIDPreRedirectHook) | ||
if preRedirectHook != nil { | ||
logger.Infof(ctx, "preRedirect hook is set") | ||
if err := preRedirectHook(ctx, authCtx, request, writer); err != nil { | ||
logger.Errorf(ctx, "failed the preRedirect hook due %v with status code %v", err.Message, err.Code) | ||
if http.StatusText(err.Code) != "" { | ||
writer.WriteHeader(err.Code) | ||
} else { | ||
writer.WriteHeader(http.StatusInternalServerError) | ||
} | ||
return | ||
} | ||
logger.Info(ctx, "Successfully called the preRedirect hook") | ||
} | ||
redirectURL := getAuthFlowEndRedirect(ctx, authCtx, request) | ||
http.Redirect(writer, request, redirectURL, http.StatusTemporaryRedirect) | ||
} | ||
|
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we also check that it isn't status ok? https://go.dev/src/net/http/status.go
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So i am relying on the user code which set the hook to set the correct http status code .
If they choose to set OK, then thats what we will set on the header.