-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(auth): add credential-master mode for follower nodes #1258
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cc6de0d
feat(auth): add credential-master mode for follower nodes
notdp 7de642d
Merge remote-tracking branch 'origin/main' into feature/credential-ma…
notdp ca598ef
refactor(auth): extract credential-master logic into dedicated file
notdp 73b83aa
refactor(api): move peer endpoints to management handler with bcrypt …
notdp db637ef
test(auth): add credential-master unit tests
notdp 2ab0e92
fix(auth): revert MarkResult to only suspend on 401, remove stale com…
notdp 9431487
fix(auth): restore X-Peer-Secret peer auth instead of management Midd…
notdp a09a053
fix(auth): use management Middleware for /v0/internal, drop PeerAuthM…
notdp 4e2adfd
refactor(auth): extract 401 retry logic into tryFetchFromMasterOnUnau…
notdp ff0bafa
refactor(auth): remove redundant peerSecret/credentialMaster fields
notdp b8aea4f
feat(auth): add PeerAuthMiddleware for /v0/internal routes
notdp 8dcad27
fix: correct comment to say 401 only (not 401/403)
notdp 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,94 @@ | ||
| package management | ||
|
|
||
| import ( | ||
| "crypto/subtle" | ||
| "net/http" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // PeerAuthMiddleware returns a middleware for peer-to-peer authentication. | ||
| // Both master and follower share the same secret-key value (typically a bcrypt hash), | ||
| // and this middleware does constant-time string comparison (not bcrypt verification). | ||
| // This differs from Middleware() which does bcrypt verification for human users. | ||
| func (h *Handler) PeerAuthMiddleware() gin.HandlerFunc { | ||
| return func(c *gin.Context) { | ||
| if h == nil || h.cfg == nil { | ||
| c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "peer authentication not configured"}) | ||
| return | ||
| } | ||
| expected := h.cfg.RemoteManagement.SecretKey | ||
| if expected == "" { | ||
| c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "peer authentication not configured"}) | ||
| return | ||
| } | ||
|
|
||
| // Accept Authorization: Bearer <secret> or X-Peer-Secret header | ||
| var provided string | ||
| if auth := c.GetHeader("Authorization"); auth != "" { | ||
| parts := strings.SplitN(auth, " ", 2) | ||
| if len(parts) == 2 && strings.EqualFold(parts[0], "bearer") { | ||
| provided = parts[1] | ||
| } | ||
| } | ||
| if provided == "" { | ||
| provided = c.GetHeader("X-Peer-Secret") | ||
| } | ||
|
|
||
| if provided == "" { | ||
| c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "missing peer secret"}) | ||
| return | ||
| } | ||
| if subtle.ConstantTimeCompare([]byte(provided), []byte(expected)) != 1 { | ||
| c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "invalid peer secret"}) | ||
| return | ||
| } | ||
| c.Next() | ||
| } | ||
| } | ||
|
|
||
| // HandleCredentialQuery returns the current access_token for a given auth ID. | ||
| // This endpoint is used by follower nodes to fetch credentials from master. | ||
| func (h *Handler) HandleCredentialQuery(c *gin.Context) { | ||
| if h == nil || h.authManager == nil { | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "server not initialized"}) | ||
| return | ||
| } | ||
|
|
||
| id := c.Query("id") | ||
| if id == "" { | ||
| c.JSON(http.StatusBadRequest, gin.H{"error": "id parameter is required"}) | ||
| return | ||
| } | ||
|
|
||
| h.authManager.RefreshIfNeeded(c.Request.Context(), id) | ||
|
|
||
| accessToken := h.authManager.GetAccessToken(id) | ||
| if accessToken == "" { | ||
| c.JSON(http.StatusNotFound, gin.H{"error": "credential not found or no access_token"}) | ||
| return | ||
| } | ||
|
|
||
| response := gin.H{ | ||
| "id": id, | ||
| "access_token": accessToken, | ||
| } | ||
| if expiredAt, ok := h.authManager.GetExpirationTime(id); ok && !expiredAt.IsZero() { | ||
| response["expired"] = expiredAt.Format(time.RFC3339) | ||
| } | ||
| c.JSON(http.StatusOK, response) | ||
| } | ||
|
|
||
| // HandleAuthList returns all auth entries (without refresh_token). | ||
| // This endpoint is used by follower nodes for startup sync. | ||
| func (h *Handler) HandleAuthList(c *gin.Context) { | ||
| if h == nil || h.authManager == nil { | ||
| c.JSON(http.StatusInternalServerError, gin.H{"error": "server not initialized"}) | ||
| return | ||
| } | ||
|
|
||
| auths := h.authManager.GetAllAuthsForSync() | ||
| c.JSON(http.StatusOK, gin.H{"auths": auths}) | ||
| } |
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
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.