This repository has been archived by the owner on Jan 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
routes_rtfsp_management.go
356 lines (347 loc) · 10.8 KB
/
routes_rtfsp_management.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package v2
import (
"errors"
"net/http"
"strings"
"github.com/RTradeLtd/Temporal/eh"
"github.com/RTradeLtd/database/models"
nexus "github.com/RTradeLtd/grpc/nexus"
"github.com/gin-gonic/gin"
)
// these API calls are used to handle management of private IPFS networks
// CreateIPFSNetwork is used to create an entry in the database for a private ipfs network
func (api *API) createIPFSNetwork(c *gin.Context) {
if !dev {
Fail(c, errors.New("private networks not supported in production, please use https://dev.api.temporal.cloud"))
return
}
username, err := GetAuthenticatedUserFromContext(c)
if err != nil {
api.LogError(c, err, eh.NoAPITokenError)(http.StatusBadRequest)
return
}
// extract network name
networkName, exists := c.GetPostForm("network_name")
if !exists {
FailWithMissingField(c, "network_name")
return
}
// make sure the name is something other than public
if strings.ToLower(networkName) == "public" {
Fail(c, errors.New("network name can't be public, or PUBLIC"))
}
// retrieve parameters - thse are all optional
swarmKey, _ := c.GetPostForm("swarm_key")
bPeers, _ := c.GetPostFormArray("bootstrap_peers")
users := c.PostFormArray("users")
if users == nil {
users = []string{username}
} else {
users = append(users, username)
}
// create the network in our database
network, err := api.nm.CreateHostedPrivateNetwork(networkName, swarmKey, bPeers, models.NetworkAccessOptions{Users: users})
if err != nil {
api.LogError(c, err, eh.NetworkCreationError)(http.StatusBadRequest)
return
}
// request orchestrator to start up network and create it after registering it in the database
resp, err := api.orch.StartNetwork(c, &nexus.NetworkRequest{
Network: networkName,
})
if err != nil {
api.LogError(c, err, "failed to start private network",
"network_name", networkName,
)(http.StatusBadRequest)
return
}
logger := api.l.With("user", username, "network_name", networkName)
logger.Info("network creation request received")
logger.With("db_id", network.ID).Info("database entry created")
// update allows users who can access the network
if len(users) > 0 {
for _, v := range users {
if err := api.um.AddIPFSNetworkForUser(v, networkName); err != nil && err.Error() != "network already configured for user" {
api.LogError(c, err, eh.NetworkCreationError)(http.StatusBadRequest)
return
}
api.l.With("user", v).Info("network added to user)")
}
}
api.l.With("response", resp).Info("network node started")
// respond with network details
Respond(c, http.StatusOK, gin.H{
"response": gin.H{
"id": network.ID,
"peer_id": resp.GetPeerId(),
"network_name": networkName,
"swarm_key": resp.GetSwarmKey(),
"users": network.Users,
},
})
}
func (api *API) startIPFSPrivateNetwork(c *gin.Context) {
if !dev {
Fail(c, errors.New("private networks not supported in production, please use https://dev.api.temporal.cloud"))
return
}
username, err := GetAuthenticatedUserFromContext(c)
if err != nil {
api.LogError(c, err, eh.NoAPITokenError)(http.StatusBadRequest)
return
}
// get network name
networkName, exists := c.GetPostForm("network_name")
if !exists {
FailWithMissingField(c, "network_name")
return
}
logger := api.l.With("user", username, "network_name", networkName)
logger.Info("private ipfs network start requested")
// get all networks user has access too
networks, err := api.um.GetPrivateIPFSNetworksForUser(username)
if err != nil {
api.LogError(c, err, eh.PrivateNetworkAccessError)(http.StatusBadRequest)
return
}
// examine networks to ensure they can access it
var found bool
for _, network := range networks {
if network == networkName {
found = true
break
}
}
if !found {
logger.Info("user not authorized to access network")
Respond(c, http.StatusUnauthorized, gin.H{
"response": "user does not have access to requested network",
})
return
}
// send start network call
if _, err := api.orch.StartNetwork(c, &nexus.NetworkRequest{
Network: networkName}); err != nil {
api.LogError(c, err, "failed to start network")(http.StatusBadRequest)
return
}
// log and return
logger.Info("network started")
Respond(c, http.StatusOK, gin.H{
"response": gin.H{
"network_name": networkName,
"state": "started",
},
})
}
func (api *API) stopIPFSPrivateNetwork(c *gin.Context) {
if !dev {
Fail(c, errors.New("private networks not supported in production, please use https://dev.api.temporal.cloud"))
return
}
username, err := GetAuthenticatedUserFromContext(c)
if err != nil {
api.LogError(c, err, eh.NoAPITokenError)(http.StatusBadRequest)
return
}
// get network name
networkName, exists := c.GetPostForm("network_name")
if !exists {
FailWithMissingField(c, "network_name")
return
}
logger := api.l.With("user", username, "network_name", networkName)
logger.Info("private ipfs network shutdown requested")
// retrieve all networks user has access to
networks, err := api.um.GetPrivateIPFSNetworksForUser(username)
if err != nil {
api.LogError(c, err, eh.PrivateNetworkAccessError)(http.StatusBadRequest)
return
}
// examine all networks to ensure they have access to this one
var found bool
for _, n := range networks {
if n == networkName {
found = true
break
}
}
if !found {
logger.Info("user not authorized to access network")
Respond(c, http.StatusUnauthorized, gin.H{
"response": "user does not have access to requested network",
})
return
}
// send a stop network request
if _, err := api.orch.StopNetwork(c, &nexus.NetworkRequest{
Network: networkName}); err != nil {
api.LogError(c, err, "failed to stop network")(http.StatusBadRequest)
return
}
// log and return
logger.Info("network stopped")
Respond(c, http.StatusOK, gin.H{
"response": gin.H{
"network_name": networkName,
"state": "stopped",
},
})
}
func (api *API) removeIPFSPrivateNetwork(c *gin.Context) {
if !dev {
Fail(c, errors.New("private networks not supported in production, please use https://dev.api.temporal.cloud"))
return
}
username, err := GetAuthenticatedUserFromContext(c)
if err != nil {
api.LogError(c, err, eh.NoAPITokenError)(http.StatusBadRequest)
return
}
// get the network name
networkName, exists := c.GetPostForm("network_name")
if !exists {
FailWithMissingField(c, "network_name")
return
}
logger := api.l.With("user", username, "network_name", networkName)
logger.Info("private ipfs network shutdown requested")
// retrieve all networks the user has access to
networks, err := api.um.GetPrivateIPFSNetworksForUser(username)
if err != nil {
api.LogError(c, err, eh.PrivateNetworkAccessError)(http.StatusBadRequest)
return
}
// examine networks to make sure the user can access it
var found bool
for _, n := range networks {
if n == networkName {
found = true
break
}
}
//TODO: make sure that only the creator of the network can remove it
if !found {
logger.Info("user not authorized to access network")
Respond(c, http.StatusUnauthorized, gin.H{
"response": "user does not have access to requested network",
})
return
}
// send node removal request, removing all data stored
// this is a DESTRUCTIVE action
if _, err = api.orch.RemoveNetwork(c, &nexus.NetworkRequest{
Network: networkName}); err != nil {
api.LogError(c, err, "failed to remove network assets")(http.StatusBadRequest)
return
}
// search for the network to get list of users who have access
// this allows us to search through the user table, and remove the network from it
network, err := api.nm.GetNetworkByName(networkName)
if err != nil {
api.LogError(c, err, eh.NetworkSearchError)(http.StatusBadRequest)
return
}
// remove network from database
if err = api.nm.Delete(networkName); err != nil {
api.LogError(c, err, "failed to remove network from database")(http.StatusBadRequest)
return
}
// remove network from users authorized networks
for _, v := range network.Users {
if err = api.um.RemoveIPFSNetworkForUser(v, networkName); err != nil {
api.LogError(c, err, "failed to remove network from users")(http.StatusBadRequest)
return
}
}
// log and return
logger.Info("network removed")
Respond(c, http.StatusOK, gin.H{
"response": gin.H{
"network_name": networkName,
"state": "removed",
},
})
}
// GetIPFSPrivateNetworkByName is used to private ipfs network information
func (api *API) getIPFSPrivateNetworkByName(c *gin.Context) {
if !dev {
Fail(c, errors.New("private networks not supported in production, please use https://dev.api.temporal.cloud"))
return
}
username, err := GetAuthenticatedUserFromContext(c)
if err != nil {
api.LogError(c, err, eh.NoAPITokenError)(http.StatusBadRequest)
return
}
// get the network name
netName := c.Param("name")
// get all networks user has access to
networks, err := api.um.GetPrivateIPFSNetworksForUser(username)
if err != nil {
api.LogError(c, err, eh.PrivateNetworkAccessError)(http.StatusBadRequest)
return
}
// ensure they can access this network
var found bool
for _, v := range networks {
if v == netName {
found = true
break
}
}
if !found {
Fail(c, errors.New(eh.PrivateNetworkAccessError))
return
}
logger := api.l.With("user", username, "network_name", netName)
logger.Info("private ipfs network by name requested")
// get network details from database
net, err := api.nm.GetNetworkByName(netName)
if err != nil {
api.LogError(c, err, eh.NetworkSearchError)(http.StatusBadRequest)
return
}
// retrieve additional stats if requested
// otherwise send generic information from the database directly
if c.Param("stats") == "true" {
logger.Info("retrieving additional stats from orchestrator")
stats, err := api.orch.NetworkStats(c, &nexus.NetworkRequest{Network: netName})
if err != nil {
api.LogError(c, err, eh.NetworkSearchError)(http.StatusBadRequest)
return
}
// return
Respond(c, http.StatusOK, gin.H{"response": gin.H{
"database": net,
"network_stats": stats,
}})
} else {
// return
Respond(c, http.StatusOK, gin.H{"response": gin.H{
"database": net,
}})
}
}
// GetAuthorizedPrivateNetworks is used to retrieve authorized private networks
// an authorized private network is defined as a network a user has API access to
func (api *API) getAuthorizedPrivateNetworks(c *gin.Context) {
if !dev {
Fail(c, errors.New("private networks not supported in production, please use https://dev.api.temporal.cloud"))
return
}
username, err := GetAuthenticatedUserFromContext(c)
if err != nil {
api.LogError(c, err, eh.NoAPITokenError)(http.StatusBadRequest)
return
}
// get all networks the user has access too
networks, err := api.um.GetPrivateIPFSNetworksForUser(username)
if err != nil {
api.LogError(c, err, eh.PrivateNetworkAccessError)(http.StatusBadRequest)
return
}
// log and return
api.l.Infow("authorized private ipfs network listing requested", "user", username)
Respond(c, http.StatusOK, gin.H{"response": networks})
}