-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
csa_keys_controller.go
95 lines (78 loc) · 2.55 KB
/
csa_keys_controller.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
package web
import (
"errors"
"io"
"net/http"
"github.com/gin-gonic/gin"
"github.com/smartcontractkit/chainlink/v2/core/logger/audit"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
)
// CSAKeysController manages CSA keys
type CSAKeysController struct {
App chainlink.Application
}
// Index lists CSA keys
// Example:
// "GET <application>/keys/csa"
func (ctrl *CSAKeysController) Index(c *gin.Context) {
keys, err := ctrl.App.GetKeyStore().CSA().GetAll()
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
jsonAPIResponse(c, presenters.NewCSAKeyResources(keys), "csaKeys")
}
// Create and return a CSA key
// Example:
// "POST <application>/keys/csa"
func (ctrl *CSAKeysController) Create(c *gin.Context) {
key, err := ctrl.App.GetKeyStore().CSA().Create()
if err != nil {
if errors.Is(err, keystore.ErrCSAKeyExists) {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
ctrl.App.GetAuditLogger().Audit(audit.CSAKeyCreated, map[string]interface{}{
"CSAPublicKey": key.PublicKey,
"CSVersion": key.Version,
})
jsonAPIResponse(c, presenters.NewCSAKeyResource(key), "csaKeys")
}
// Import imports a CSA key
func (ctrl *CSAKeysController) Import(c *gin.Context) {
defer ctrl.App.GetLogger().ErrorIfFn(c.Request.Body.Close, "Error closing Import request body")
bytes, err := io.ReadAll(c.Request.Body)
if err != nil {
jsonAPIError(c, http.StatusBadRequest, err)
return
}
oldPassword := c.Query("oldpassword")
key, err := ctrl.App.GetKeyStore().CSA().Import(bytes, oldPassword)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
ctrl.App.GetAuditLogger().Audit(audit.CSAKeyImported, map[string]interface{}{
"CSAPublicKey": key.PublicKey,
"CSVersion": key.Version,
})
jsonAPIResponse(c, presenters.NewCSAKeyResource(key), "csaKey")
}
// Export exports a key
func (ctrl *CSAKeysController) Export(c *gin.Context) {
defer ctrl.App.GetLogger().ErrorIfFn(c.Request.Body.Close, "Error closing Export request body")
keyID := c.Param("ID")
newPassword := c.Query("newpassword")
bytes, err := ctrl.App.GetKeyStore().CSA().Export(keyID, newPassword)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
ctrl.App.GetAuditLogger().Audit(audit.CSAKeyExported, map[string]interface{}{"keyID": keyID})
c.Data(http.StatusOK, MediaType, bytes)
}