-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
p2p_keys_controller.go
135 lines (116 loc) · 3.61 KB
/
p2p_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
package web
import (
"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/keys/p2pkey"
"github.com/smartcontractkit/chainlink/v2/core/web/presenters"
)
// P2PKeysController manages P2P keys
type P2PKeysController struct {
App chainlink.Application
}
// Index lists P2P keys
// Example:
// "GET <application>/keys/p2p"
func (p2pkc *P2PKeysController) Index(c *gin.Context) {
keys, err := p2pkc.App.GetKeyStore().P2P().GetAll()
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
jsonAPIResponse(c, presenters.NewP2PKeyResources(keys), "p2pKey")
}
// Create and return a P2P key
// Example:
// "POST <application>/keys/p2p"
func (p2pkc *P2PKeysController) Create(c *gin.Context) {
key, err := p2pkc.App.GetKeyStore().P2P().Create()
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
p2pkc.App.GetAuditLogger().Audit(audit.KeyCreated, map[string]interface{}{
"type": "p2p",
"id": key.ID(),
"p2pPublicKey": key.PublicKeyHex(),
"p2pPeerID": key.PeerID(),
"p2pType": key.Type(),
})
jsonAPIResponse(c, presenters.NewP2PKeyResource(key), "p2pKey")
}
// Delete a P2P key
// Example:
// "DELETE <application>/keys/p2p/:keyID"
// "DELETE <application>/keys/p2p/:keyID?hard=true"
func (p2pkc *P2PKeysController) Delete(c *gin.Context) {
keyID, err := p2pkey.MakePeerID(c.Param("keyID"))
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
key, err := p2pkc.App.GetKeyStore().P2P().Get(keyID)
if err != nil {
jsonAPIError(c, http.StatusNotFound, err)
return
}
_, err = p2pkc.App.GetKeyStore().P2P().Delete(key.PeerID())
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
p2pkc.App.GetAuditLogger().Audit(audit.KeyDeleted, map[string]interface{}{
"type": "p2p",
"id": keyID,
})
jsonAPIResponse(c, presenters.NewP2PKeyResource(key), "p2pKey")
}
// Import imports a P2P key
// Example:
// "Post <application>/keys/p2p/import"
func (p2pkc *P2PKeysController) Import(c *gin.Context) {
defer p2pkc.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 := p2pkc.App.GetKeyStore().P2P().Import(bytes, oldPassword)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
p2pkc.App.GetAuditLogger().Audit(audit.KeyImported, map[string]interface{}{
"type": "p2p",
"id": key.ID(),
"p2pPublicKey": key.PublicKeyHex(),
"p2pPeerID": key.PeerID(),
"p2pType": key.Type(),
})
jsonAPIResponse(c, presenters.NewP2PKeyResource(key), "p2pKey")
}
// Export exports a P2P key
// Example:
// "Post <application>/keys/p2p/export"
func (p2pkc *P2PKeysController) Export(c *gin.Context) {
defer p2pkc.App.GetLogger().ErrorIfFn(c.Request.Body.Close, "Error closing Export request body")
keyID, err := p2pkey.MakePeerID(c.Param("ID"))
if err != nil {
jsonAPIError(c, http.StatusUnprocessableEntity, err)
return
}
newPassword := c.Query("newpassword")
bytes, err := p2pkc.App.GetKeyStore().P2P().Export(keyID, newPassword)
if err != nil {
jsonAPIError(c, http.StatusInternalServerError, err)
return
}
p2pkc.App.GetAuditLogger().Audit(audit.KeyExported, map[string]interface{}{
"type": "p2p",
"id": keyID,
})
c.Data(http.StatusOK, MediaType, bytes)
}