-
Notifications
You must be signed in to change notification settings - Fork 30
feat(auth): server side changes for new worker auth flow #790
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
Changes from 10 commits
b339c4c
746b399
8bf613c
c1f1463
1da6bdd
4d7b735
e925301
e620fa3
febb17d
4a12e25
25118c9
ad7db54
467b47f
f6dea85
85c0efd
75d4c1f
fa8ba2c
bbdcfc5
2f49e8e
7bc1b5c
0014ccd
077c267
66e3642
b140651
05b7cd2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Copyright (c) 2023 Target Brands, Inc. All rights reserved. | ||
| // | ||
| // Use of this source code is governed by the LICENSE file in this repository. | ||
|
|
||
| package admin | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/go-vela/server/internal/token" | ||
| "github.com/go-vela/server/util" | ||
| "github.com/go-vela/types/constants" | ||
| "github.com/go-vela/types/library" | ||
|
|
||
| "github.com/gin-gonic/gin" | ||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| // swagger:operation POST /api/v1/admin/workers/{worker}/register-token admin RegisterToken | ||
| // | ||
| // Get a worker registration token | ||
| // | ||
| // --- | ||
| // produces: | ||
| // - application/json | ||
| // parameters: | ||
| // - in: path | ||
| // name: worker | ||
| // description: Hostname of the worker | ||
| // required: true | ||
| // type: string | ||
| // security: | ||
| // - ApiKeyAuth: [] | ||
| // responses: | ||
| // '200': | ||
| // description: Successfully generated registration token | ||
| // schema: | ||
| // "$ref": "#/definitions/Token" | ||
| // '401': | ||
| // description: Unauthorized | ||
| // schema: | ||
| // "$ref": "#/definitions/Error" | ||
|
|
||
| // RegisterToken represents the API handler to | ||
| // generate a registration token for onboarding a worker. | ||
| func RegisterToken(c *gin.Context) { | ||
| logrus.Info("Admin: generating registration token") | ||
ecrupper marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| host := util.PathParameter(c, "worker") | ||
|
|
||
| tm := c.MustGet("token-manager").(*token.Manager) | ||
| rmto := &token.MintTokenOpts{ | ||
| Hostname: host, | ||
| TokenType: constants.WorkerRegisterTokenType, | ||
ecrupper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| TokenDuration: tm.WorkerRegisterTokenDuration, | ||
| } | ||
|
|
||
| rt, err := tm.MintToken(rmto) | ||
| if err != nil { | ||
| retErr := fmt.Errorf("unable to generate registration token: %w", err) | ||
|
|
||
| util.HandleError(c, http.StatusInternalServerError, retErr) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, library.Token{Token: &rt}) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,10 @@ import ( | |
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/go-vela/server/internal/token" | ||
| "github.com/go-vela/server/router/middleware/claims" | ||
| "github.com/go-vela/server/router/middleware/user" | ||
| "github.com/go-vela/types/constants" | ||
|
|
||
| "github.com/go-vela/server/database" | ||
| "github.com/go-vela/server/router/middleware/worker" | ||
|
|
@@ -55,6 +58,7 @@ import ( | |
| func CreateWorker(c *gin.Context) { | ||
| // capture middleware values | ||
| u := user.Retrieve(c) | ||
| cl := claims.Retrieve(c) | ||
|
|
||
| // capture body from API request | ||
| input := new(library.Worker) | ||
|
|
@@ -85,7 +89,42 @@ func CreateWorker(c *gin.Context) { | |
| return | ||
| } | ||
|
|
||
| c.JSON(http.StatusCreated, fmt.Sprintf("worker %s created", input.GetHostname())) | ||
| switch cl.TokenType { | ||
| case constants.ServerWorkerTokenType: | ||
| if secret, ok := c.Value("secret").(string); ok { | ||
| tkn := new(library.Token) | ||
| tkn.SetToken(secret) | ||
| c.JSON(http.StatusOK, tkn) | ||
| } | ||
|
|
||
| retErr := fmt.Errorf("symmetric token provided but not configured in server") | ||
| util.HandleError(c, http.StatusBadRequest, retErr) | ||
|
|
||
| return | ||
| default: | ||
| tm := c.MustGet("token-manager").(*token.Manager) | ||
|
|
||
| wmto := &token.MintTokenOpts{ | ||
| TokenType: constants.WorkerAuthTokenType, | ||
| TokenDuration: tm.WorkerAuthTokenDuration, | ||
| Hostname: cl.Subject, | ||
| } | ||
|
|
||
| tkn := new(library.Token) | ||
|
|
||
| wt, err := tm.MintToken(wmto) | ||
| if err != nil { | ||
| retErr := fmt.Errorf("unable to generate auth token for worker %s: %w", input.GetHostname(), err) | ||
|
|
||
| util.HandleError(c, http.StatusInternalServerError, retErr) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| tkn.SetToken(wt) | ||
|
|
||
| c.JSON(http.StatusCreated, tkn) | ||
| } | ||
| } | ||
|
|
||
| // swagger:operation GET /api/v1/workers workers GetWorkers | ||
|
|
@@ -231,6 +270,13 @@ func UpdateWorker(c *gin.Context) { | |
| // capture middleware values | ||
| u := user.Retrieve(c) | ||
| w := worker.Retrieve(c) | ||
| cl := claims.Retrieve(c) | ||
ecrupper marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // establish check in type | ||
| type WorkerCheckIn struct { | ||
| Worker *library.Worker `json:"worker,omitempty"` | ||
| Token *library.Token `json:"token,omitempty"` | ||
| } | ||
|
|
||
| // update engine logger with API metadata | ||
| // | ||
|
|
@@ -272,20 +318,47 @@ func UpdateWorker(c *gin.Context) { | |
| w.SetLastCheckedIn(input.GetLastCheckedIn()) | ||
| } | ||
|
|
||
| // send API call to update the worker | ||
| err = database.FromContext(c).UpdateWorker(w) | ||
ecrupper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| retErr := fmt.Errorf("unable to update worker %s: %w", w.GetHostname(), err) | ||
| // send API call to capture the updated worker | ||
| w, _ = database.FromContext(c).GetWorkerForHostname(w.GetHostname()) | ||
|
|
||
| util.HandleError(c, http.StatusInternalServerError, retErr) | ||
| switch cl.TokenType { | ||
ecrupper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| case constants.UserAccessTokenType: | ||
| c.JSON(http.StatusOK, w) | ||
| case constants.ServerWorkerTokenType: | ||
| if secret, ok := c.Value("secret").(string); ok { | ||
| tkn := new(library.Token) | ||
| tkn.SetToken(secret) | ||
| c.JSON(http.StatusOK, WorkerCheckIn{Worker: w, Token: tkn}) | ||
|
||
| } | ||
|
|
||
| retErr := fmt.Errorf("symmetric token provided but not configured in server") | ||
| util.HandleError(c, http.StatusBadRequest, retErr) | ||
|
|
||
| return | ||
| } | ||
| default: | ||
| tm := c.MustGet("token-manager").(*token.Manager) | ||
|
|
||
| // send API call to capture the updated worker | ||
| w, _ = database.FromContext(c).GetWorkerForHostname(w.GetHostname()) | ||
| wmto := &token.MintTokenOpts{ | ||
| TokenType: constants.WorkerAuthTokenType, | ||
ecrupper marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| TokenDuration: tm.WorkerAuthTokenDuration, | ||
| Hostname: cl.Subject, | ||
| } | ||
|
|
||
| c.JSON(http.StatusOK, w) | ||
| tkn := new(library.Token) | ||
|
|
||
| wt, err := tm.MintToken(wmto) | ||
| if err != nil { | ||
| retErr := fmt.Errorf("unable to generate auth token for worker %s: %w", w.GetHostname(), err) | ||
|
|
||
| util.HandleError(c, http.StatusInternalServerError, retErr) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| tkn.SetToken(wt) | ||
|
|
||
| c.JSON(http.StatusOK, WorkerCheckIn{Worker: w, Token: tkn}) | ||
| } | ||
| } | ||
|
|
||
| // swagger:operation DELETE /api/v1/workers/{worker} workers DeleteWorker | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.