-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
keys_controller.go
151 lines (129 loc) · 3.7 KB
/
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package web
import (
"fmt"
"io"
"net/http"
"github.com/gin-gonic/gin"
"github.com/manyminds/api2go/jsonapi"
"github.com/smartcontractkit/chainlink/v2/core/logger"
"github.com/smartcontractkit/chainlink/v2/core/logger/audit"
"github.com/smartcontractkit/chainlink/v2/core/services/keystore"
)
type Keystore[K keystore.Key] interface {
Get(id string) (K, error)
GetAll() ([]K, error)
Create() (K, error)
Delete(id string) (K, error)
Import(keyJSON []byte, password string) (K, error)
Export(id string, password string) ([]byte, error)
}
type KeysController interface {
// Index lists keys
Index(*gin.Context)
// Create and return a key
Create(*gin.Context)
// Delete a key
Delete(*gin.Context)
// Import imports a key
Import(*gin.Context)
// Export exports a key
Export(*gin.Context)
}
type keysController[K keystore.Key, R jsonapi.EntityNamer] struct {
ks Keystore[K]
lggr logger.SugaredLogger
auditLogger audit.AuditLogger
typ string
resourceName string
newResource func(K) *R
newResources func([]K) []R
}
func NewKeysController[K keystore.Key, R jsonapi.EntityNamer](ks Keystore[K], lggr logger.Logger, auditLogger audit.AuditLogger, resourceName string,
newResource func(K) *R, newResources func([]K) []R) KeysController {
var k K
typ, err := keystore.GetFieldNameForKey(k)
if err != nil {
panic(fmt.Errorf("unable to create keys controller: %v", err))
}
return &keysController[K, R]{
ks: ks,
lggr: logger.Sugared(lggr),
auditLogger: auditLogger,
typ: typ,
resourceName: resourceName,
newResource: newResource,
newResources: newResources,
}
}
func (kc *keysController[K, R]) Index(c *gin.Context) {
keys, err := kc.ks.GetAll()
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
jsonAPIResponse(c, kc.newResources(keys), kc.resourceName)
}
func (kc *keysController[K, R]) Create(c *gin.Context) {
key, err := kc.ks.Create()
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
kc.auditLogger.Audit(audit.KeyCreated, map[string]interface{}{
"type": kc.typ,
"id": key.ID(),
})
jsonAPIResponse(c, kc.newResource(key), kc.resourceName)
}
func (kc *keysController[K, R]) Delete(c *gin.Context) {
keyID := c.Param("keyID")
key, err := kc.ks.Get(keyID)
if err != nil {
jsonAPIError(c, http.StatusNotFound, err)
return
}
_, err = kc.ks.Delete(key.ID())
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
kc.auditLogger.Audit(audit.KeyDeleted, map[string]interface{}{
"type": kc.typ,
"id": key.ID(),
})
jsonAPIResponse(c, kc.newResource(key), kc.resourceName)
}
func (kc *keysController[K, R]) Import(c *gin.Context) {
defer kc.lggr.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 := kc.ks.Import(bytes, oldPassword)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
kc.auditLogger.Audit(audit.KeyImported, map[string]interface{}{
"type": kc.typ,
"id": key.ID(),
})
jsonAPIResponse(c, kc.newResource(key), kc.resourceName)
}
func (kc *keysController[K, R]) Export(c *gin.Context) {
defer kc.lggr.ErrorIfFn(c.Request.Body.Close, "Error closing Export request body")
keyID := c.Param("ID")
newPassword := c.Query("newpassword")
bytes, err := kc.ks.Export(keyID, newPassword)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
kc.auditLogger.Audit(audit.KeyExported, map[string]interface{}{
"type": kc.typ,
"id": keyID,
})
c.Data(http.StatusOK, MediaType, bytes)
}