-
Notifications
You must be signed in to change notification settings - Fork 28
/
secret.go
118 lines (97 loc) · 2.8 KB
/
secret.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright (c) 2022 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
// nolint: dupl // ignore similar code
package admin
import (
"fmt"
"net/http"
"github.com/go-vela/server/database"
"github.com/go-vela/server/util"
"github.com/go-vela/types/library"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)
// swagger:operation GET /api/v1/admin/secrets admin AdminAllSecrets
//
// Get all of the secrets in the database
//
// ---
// produces:
// - application/json
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved all secrets from the database
// schema:
// type: array
// items:
// "$ref": "#/definitions/Secret"
// '500':
// description: Unable to retrieve all secrets from the database
// schema:
// "$ref": "#/definitions/Error"
// AllSecrets represents the API handler to
// captures all secrets stored in the database.
func AllSecrets(c *gin.Context) {
logrus.Info("Admin: reading all secrets")
// send API call to capture all secrets
s, err := database.FromContext(c).GetSecretList()
if err != nil {
retErr := fmt.Errorf("unable to capture all secrets: %w", err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
c.JSON(http.StatusOK, s)
}
// swagger:operation PUT /api/v1/admin/secret admin AdminUpdateSecret
//
// Update a secret in the database
//
// ---
// produces:
// - application/json
// parameters:
// - in: body
// name: body
// description: Payload containing secret to update
// required: true
// schema:
// "$ref": "#/definitions/Secret"
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully updated the secret in the database
// schema:
// "$ref": "#/definitions/Secret"
// '404':
// description: Unable to update the secret in the database
// schema:
// "$ref": "#/definitions/Error"
// '501':
// description: Unable to update the secret in the database
// schema:
// "$ref": "#/definitions/Error"
// UpdateSecret represents the API handler to
// update any secret stored in the database.
func UpdateSecret(c *gin.Context) {
logrus.Info("Admin: updating secret in database")
// capture body from API request
input := new(library.Secret)
err := c.Bind(input)
if err != nil {
retErr := fmt.Errorf("unable to decode JSON for secret %d: %w", input.GetID(), err)
util.HandleError(c, http.StatusNotFound, retErr)
return
}
// send API call to update the secret
err = database.FromContext(c).UpdateSecret(input)
if err != nil {
retErr := fmt.Errorf("unable to update secret %d: %w", input.GetID(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
c.JSON(http.StatusOK, input)
}