-
Notifications
You must be signed in to change notification settings - Fork 28
/
worker.go
71 lines (58 loc) · 1.67 KB
/
worker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: Apache-2.0
package admin
import (
"fmt"
"net/http"
"github.com/go-vela/server/internal/token"
"github.com/go-vela/server/router/middleware/user"
"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) {
// retrieve user from context
u := user.Retrieve(c)
logrus.Infof("Platform admin %s: generating registration token", u.GetName())
host := util.PathParameter(c, "worker")
tm := c.MustGet("token-manager").(*token.Manager)
rmto := &token.MintTokenOpts{
Hostname: host,
TokenType: constants.WorkerRegisterTokenType,
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})
}