-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance(auth): implement registration flow (#452)
* initial work * more work * backwards compatibility with comments * adjust middleware to only use validate-token for server tokens * more work * rename auth token channel to RegisterToken * rename token channel and update api comments * updating some comments and not loggin token * fix docker compose * fix local replace * token expiration func has two return vals * docker compose no register * name register token middleware file correctly * update swagger for register
- Loading branch information
Showing
14 changed files
with
280 additions
and
39 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,70 @@ | ||
// Copyright (c) 2023 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/go-vela/worker/router/middleware/token" | ||
) | ||
|
||
// swagger:operation POST /register system Register | ||
// | ||
// Fill registration token channel in worker to continue operation | ||
// | ||
// --- | ||
// produces: | ||
// - application/json | ||
// parameters: | ||
// security: | ||
// - ApiKeyAuth: [] | ||
// responses: | ||
// '200': | ||
// description: Successfully passed token to worker | ||
// schema: | ||
// type: string | ||
// '500': | ||
// description: Unable to pass token to worker | ||
// schema: | ||
// "$ref": "#/definitions/Error" | ||
|
||
// Register will pass the token given in the request header to the register token | ||
// channel of the worker. This will unblock operation if the worker has not been | ||
// registered and the provided registration token is valid. | ||
func Register(c *gin.Context) { | ||
// extract the register token channel that was packed into gin context | ||
v, ok := c.Get("register-token") | ||
if !ok { | ||
c.JSON(http.StatusInternalServerError, "no register token channel in the context") | ||
return | ||
} | ||
|
||
// make sure we configured the channel properly | ||
rChan, ok := v.(chan string) | ||
if !ok { | ||
c.JSON(http.StatusInternalServerError, "register token channel in the context is the wrong type") | ||
return | ||
} | ||
|
||
// if token is present in the channel, deny registration | ||
// this will likely never happen as the channel is offloaded immediately | ||
if len(rChan) > 0 { | ||
c.JSON(http.StatusOK, "worker already registered") | ||
return | ||
} | ||
|
||
// retrieve auth token from header | ||
token, err := token.Retrieve(c.Request) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, err) | ||
return | ||
} | ||
|
||
// write registration token to auth token channel | ||
rChan <- token | ||
|
||
c.JSON(http.StatusOK, "successfully passed token to worker") | ||
} |
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
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
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,18 @@ | ||
// Copyright (c) 2023 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package middleware | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// RegisterToken is a middleware function that attaches the | ||
// auth-token channel to the context of every http.Request. | ||
func RegisterToken(r chan string) gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
c.Set("register-token", r) | ||
c.Next() | ||
} | ||
} |
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,48 @@ | ||
// Copyright (c) 2023 Target Brands, Inc. All rights reserved. | ||
// | ||
// Use of this source code is governed by the LICENSE file in this repository. | ||
|
||
package middleware | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func TestMiddleware_RegisterToken(t *testing.T) { | ||
// setup types | ||
want := make(chan string, 1) | ||
got := make(chan string, 1) | ||
|
||
want <- "foo" | ||
|
||
// setup context | ||
gin.SetMode(gin.TestMode) | ||
|
||
resp := httptest.NewRecorder() | ||
context, engine := gin.CreateTestContext(resp) | ||
context.Request, _ = http.NewRequest(http.MethodGet, "/health", nil) | ||
|
||
// setup mock server | ||
engine.Use(RegisterToken(want)) | ||
engine.GET("/health", func(c *gin.Context) { | ||
got = c.Value("register-token").(chan string) | ||
|
||
c.Status(http.StatusOK) | ||
}) | ||
|
||
// run test | ||
engine.ServeHTTP(context.Writer, context.Request) | ||
|
||
if resp.Code != http.StatusOK { | ||
t.Errorf("RegisterToken returned %v, want %v", resp.Code, http.StatusOK) | ||
} | ||
|
||
if !reflect.DeepEqual(got, want) { | ||
t.Errorf("RegisterToken is %v, want foo", got) | ||
} | ||
} |
Oops, something went wrong.