|
| 1 | +// Copyright (c) 2023 Target Brands, Inc. All rights reserved. |
| 2 | +// |
| 3 | +// Use of this source code is governed by the LICENSE file in this repository. |
| 4 | + |
| 5 | +package api |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + |
| 11 | + "github.com/gin-gonic/gin" |
| 12 | + "github.com/go-vela/worker/router/middleware/token" |
| 13 | + "github.com/golang-jwt/jwt/v5" |
| 14 | +) |
| 15 | + |
| 16 | +// swagger:operation POST /register system Register |
| 17 | +// |
| 18 | +// Fill registration token channel in worker to continue operation |
| 19 | +// |
| 20 | +// --- |
| 21 | +// produces: |
| 22 | +// - application/json |
| 23 | +// parameters: |
| 24 | +// security: |
| 25 | +// - ApiKeyAuth: [] |
| 26 | +// responses: |
| 27 | +// '200': |
| 28 | +// description: Successfully passed token to worker |
| 29 | +// schema: |
| 30 | +// type: string |
| 31 | +// '401': |
| 32 | +// description: No token was passed |
| 33 | +// schema: |
| 34 | +// "$ref": "#/definitions/Error" |
| 35 | +// '500': |
| 36 | +// description: Unable to pass token to worker |
| 37 | +// schema: |
| 38 | +// "$ref": "#/definitions/Error" |
| 39 | + |
| 40 | +// Register will pass the token given in the request header to the register token |
| 41 | +// channel of the worker. This will unblock operation if the worker has not been |
| 42 | +// registered and the provided registration token is valid. |
| 43 | +func Register(c *gin.Context) { |
| 44 | + // extract the worker hostname that was packed into gin context |
| 45 | + w, ok := c.Get("worker-hostname") |
| 46 | + if !ok { |
| 47 | + c.JSON(http.StatusInternalServerError, "no worker hostname in the context") |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + // extract the register token channel that was packed into gin context |
| 52 | + v, ok := c.Get("register-token") |
| 53 | + if !ok { |
| 54 | + c.JSON(http.StatusInternalServerError, "no register token channel in the context") |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + // make sure we configured the channel properly |
| 59 | + rChan, ok := v.(chan string) |
| 60 | + if !ok { |
| 61 | + c.JSON(http.StatusInternalServerError, "register token channel in the context is the wrong type") |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + // if token is present in the channel, deny registration |
| 66 | + // this will likely never happen as the channel is offloaded immediately |
| 67 | + if len(rChan) > 0 { |
| 68 | + c.JSON(http.StatusOK, "worker already registered") |
| 69 | + return |
| 70 | + } |
| 71 | + |
| 72 | + // retrieve auth token from header |
| 73 | + token, err := token.Retrieve(c.Request) |
| 74 | + if err != nil { |
| 75 | + // an error occurs when no token was passed |
| 76 | + c.JSON(http.StatusUnauthorized, err) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + // extract the subject from the token |
| 81 | + sub, err := getSubjectFromToken(token) |
| 82 | + if err != nil { |
| 83 | + c.JSON(http.StatusUnauthorized, err) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + // make sure we configured the hostname properly |
| 88 | + hostname, ok := w.(string) |
| 89 | + if !ok { |
| 90 | + c.JSON(http.StatusInternalServerError, "worker hostname in the context is the wrong type") |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + // if the subject doesn't match the worker hostname return an error |
| 95 | + if sub != hostname { |
| 96 | + c.JSON(http.StatusUnauthorized, "worker hostname is invalid") |
| 97 | + return |
| 98 | + } |
| 99 | + |
| 100 | + // write registration token to auth token channel |
| 101 | + rChan <- token |
| 102 | + |
| 103 | + c.JSON(http.StatusOK, "successfully passed token to worker") |
| 104 | +} |
| 105 | + |
| 106 | +// getSubjectFromToken is a helper function to extract |
| 107 | +// the subject from the token claims. |
| 108 | +func getSubjectFromToken(token string) (string, error) { |
| 109 | + // create a new JWT parser |
| 110 | + j := jwt.NewParser() |
| 111 | + |
| 112 | + // parse the payload |
| 113 | + t, _, err := j.ParseUnverified(token, jwt.MapClaims{}) |
| 114 | + if err != nil { |
| 115 | + return "", fmt.Errorf("unable to parse token") |
| 116 | + } |
| 117 | + |
| 118 | + sub, err := t.Claims.GetSubject() |
| 119 | + if err != nil { |
| 120 | + return "", fmt.Errorf("unable to get subject from token") |
| 121 | + } |
| 122 | + |
| 123 | + // make sure there was a subject defined |
| 124 | + if len(sub) == 0 { |
| 125 | + return "", fmt.Errorf("no subject defined in token") |
| 126 | + } |
| 127 | + |
| 128 | + return sub, nil |
| 129 | +} |
0 commit comments