-
Notifications
You must be signed in to change notification settings - Fork 2k
vault: add generic authorizer with jwt auth support #21714
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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,139 @@ | ||
| package vault | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/hex" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| jsonrpc "github.com/smartcontractkit/chainlink-common/pkg/jsonrpc2" | ||
| "github.com/smartcontractkit/chainlink-common/pkg/logger" | ||
| "github.com/smartcontractkit/chainlink-evm/gethwrappers/workflow/generated/workflow_registry_wrapper_v2" | ||
| workflowsyncerv2 "github.com/smartcontractkit/chainlink/v2/core/services/workflows/syncer/v2" | ||
| ) | ||
|
|
||
| const ( | ||
| allowListBasedAuthRetryCount = 3 | ||
| allowListBasedAuthRetryInterval = 3 * time.Second | ||
| ) | ||
|
|
||
| type allowListBasedAuth struct { | ||
| workflowRegistrySyncer workflowsyncerv2.WorkflowRegistrySyncer | ||
| lggr logger.Logger | ||
| retryCount int | ||
| retryInterval time.Duration | ||
| } | ||
|
|
||
| // AuthorizeRequest authorizes a request using AllowListBasedAuth. | ||
| // It does NOT check if the request method is allowed. | ||
| func (r *allowListBasedAuth) AuthorizeRequest(ctx context.Context, req jsonrpc.Request[json.RawMessage]) (*AuthResult, error) { | ||
| r.lggr.Debugw("AllowListBasedAuth authorizing request", "method", req.Method, "requestID", req.ID) | ||
| requestDigest, err := req.Digest() | ||
| if err != nil { | ||
| r.lggr.Debugw("AllowListBasedAuth failed to create digest", "method", req.Method, "requestID", req.ID, "error", err) | ||
| return nil, err | ||
| } | ||
| requestDigestBytes, err := hex.DecodeString(requestDigest) | ||
| if err != nil { | ||
| r.lggr.Debugw("AllowListBasedAuth failed to decode digest", "method", req.Method, "requestID", req.ID, "requestDigest", requestDigest, "error", err) | ||
| return nil, err | ||
| } | ||
| requestDigestBytes32 := [32]byte(requestDigestBytes) | ||
| if r.workflowRegistrySyncer == nil { | ||
| r.lggr.Errorw("AllowListBasedAuth workflowRegistrySyncer is nil", "method", req.Method, "requestID", req.ID) | ||
| return nil, errors.New("internal error: workflowRegistrySyncer is nil") | ||
| } | ||
| allowlistedRequest, allowedRequestsStrs, err := r.findAllowlistedItemWithRetry(ctx, req, requestDigest, requestDigestBytes32) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if allowlistedRequest == nil { | ||
| r.lggr.Debugw("AllowListBasedAuth request digest not allowlisted", | ||
| "method", req.Method, | ||
| "requestID", req.ID, | ||
| "digestHexStr", requestDigest, | ||
| "allowedRequestsStrs", allowedRequestsStrs) | ||
| return nil, errors.New("request not allowlisted") | ||
| } | ||
|
|
||
| if time.Now().UTC().Unix() > int64(allowlistedRequest.ExpiryTimestamp) { | ||
| authorizedRequestStr := string(allowlistedRequest.RequestDigest[:]) | ||
| r.lggr.Debugw("AllowListBasedAuth authorization expired", "method", req.Method, "requestID", req.ID, "authorizedRequestStr", authorizedRequestStr, "expiryTimestamp", allowlistedRequest.ExpiryTimestamp) | ||
| return nil, errors.New("request authorization expired") | ||
| } | ||
|
|
||
| digestKey := string(allowlistedRequest.RequestDigest[:]) | ||
| r.lggr.Debugw("AllowListBasedAuth authorization succeeded", "method", req.Method, "requestID", req.ID, "authorizedRequestStr", digestKey, "owner", allowlistedRequest.Owner.Hex(), "expiryTimestamp", allowlistedRequest.ExpiryTimestamp) | ||
| return &AuthResult{ | ||
| workflowOwner: allowlistedRequest.Owner.Hex(), | ||
| digest: digestKey, | ||
| expiresAt: int64(allowlistedRequest.ExpiryTimestamp), | ||
| }, nil | ||
| } | ||
|
|
||
| func (r *allowListBasedAuth) findAllowlistedItemWithRetry(ctx context.Context, req jsonrpc.Request[json.RawMessage], requestDigest string, requestDigestBytes32 [32]byte) (*workflow_registry_wrapper_v2.WorkflowRegistryOwnerAllowlistedRequest, []string, error) { | ||
| for attempt := 0; attempt <= r.retryCount; attempt++ { | ||
| allowedRequests := r.workflowRegistrySyncer.GetAllowlistedRequests(ctx) | ||
| allowedRequestsStrs := make([]string, 0, len(allowedRequests)) | ||
| for _, rr := range allowedRequests { | ||
| allowedReqStr := fmt.Sprintf("AuthorizedOwner: %s, RequestDigest: %s, ExpiryTimestamp: %d", rr.Owner.Hex(), hex.EncodeToString(rr.RequestDigest[:]), rr.ExpiryTimestamp) | ||
| allowedRequestsStrs = append(allowedRequestsStrs, allowedReqStr) | ||
| } | ||
| r.lggr.Debugw("AllowListBasedAuth loaded allowlisted requests", "method", req.Method, "requestID", req.ID, "attempt", attempt+1, "allowedRequests", allowedRequestsStrs) | ||
|
|
||
| allowlistedRequest := r.fetchAllowlistedItem(allowedRequests, requestDigestBytes32) | ||
| if allowlistedRequest != nil { | ||
| return allowlistedRequest, allowedRequestsStrs, nil | ||
| } | ||
| if attempt == r.retryCount { | ||
| return nil, allowedRequestsStrs, nil | ||
| } | ||
|
|
||
| r.lggr.Debugw("AllowListBasedAuth request digest not yet allowlisted, retrying", | ||
| "method", req.Method, | ||
| "requestID", req.ID, | ||
| "digestHexStr", requestDigest, | ||
| "attempt", attempt+1, | ||
| "maxAttempts", r.retryCount+1, | ||
| "retryInterval", r.retryInterval) | ||
| if err := sleepWithContext(ctx, r.retryInterval); err != nil { | ||
| r.lggr.Debugw("AllowListBasedAuth retry canceled", "method", req.Method, "requestID", req.ID, "error", err) | ||
| return nil, nil, err | ||
| } | ||
| } | ||
|
|
||
| return nil, nil, nil // unreachable: loop always returns | ||
| } | ||
|
|
||
| func (r *allowListBasedAuth) fetchAllowlistedItem(allowListedRequests []workflow_registry_wrapper_v2.WorkflowRegistryOwnerAllowlistedRequest, digest [32]byte) *workflow_registry_wrapper_v2.WorkflowRegistryOwnerAllowlistedRequest { | ||
| for _, item := range allowListedRequests { | ||
| if item.RequestDigest == digest { | ||
| return &item | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // NewAllowListBasedAuth creates the allowlist-backed Vault auth mechanism. | ||
| func NewAllowListBasedAuth(lggr logger.Logger, workflowRegistrySyncer workflowsyncerv2.WorkflowRegistrySyncer) *allowListBasedAuth { | ||
| return &allowListBasedAuth{ | ||
| workflowRegistrySyncer: workflowRegistrySyncer, | ||
| lggr: logger.Named(lggr, "VaultAllowListBasedAuth"), | ||
| retryCount: allowListBasedAuthRetryCount, | ||
| retryInterval: allowListBasedAuthRetryInterval, | ||
| } | ||
| } | ||
|
|
||
| func sleepWithContext(ctx context.Context, d time.Duration) error { | ||
| timer := time.NewTimer(d) | ||
| defer timer.Stop() | ||
|
|
||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case <-timer.C: | ||
| return nil | ||
| } | ||
| } |
This file contains hidden or 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.
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.
This file is pretty much just a rename of previous request_authorizer.go file