-
Notifications
You must be signed in to change notification settings - Fork 1
/
devconsole.go
169 lines (140 loc) · 5.24 KB
/
devconsole.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
package devconsole
import (
"encoding/json"
"fmt"
"net/http"
"github.com/ambientkit/ambient"
"github.com/ambientkit/ambient/internal/secureconfig"
"github.com/ambientkit/ambient/pkg/envdetect"
"github.com/ambientkit/away/router"
)
// DevConsole represents a web interface to receive commands from the amb tool.
type DevConsole struct {
log ambient.AppLogger
storage ambient.Storage
pluginsystem ambient.PluginSystem
securestorage *secureconfig.SecureSite
}
// NewDevConsole returns the dev console object to receive commands from the amb
// tool.
func NewDevConsole(logger ambient.AppLogger, ps ambient.PluginSystem, storage ambient.Storage, site *secureconfig.SecureSite) *DevConsole {
return &DevConsole{
log: logger,
storage: storage,
pluginsystem: ps,
securestorage: site,
}
}
// JSON sends data to the writer.
func JSON(w http.ResponseWriter, data interface{}) error {
b, err := json.Marshal(data)
if err != nil {
return ambient.StatusError{Code: http.StatusInternalServerError, Err: err}
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(b))
return nil
}
// EnableDevConsole turns on the dev console web listener.
func (dc *DevConsole) EnableDevConsole() {
dc.log.Info("started and available at: %v/%v", envdetect.DevConsoleURL(), envdetect.DevConsolePort())
go func() {
mux := router.New()
// Encrypt the site JSON file on disk.
mux.Post("/storage/encrypt", func(w http.ResponseWriter, r *http.Request) error {
dc.log.Debug("site.bin encrypted")
err := dc.storage.LoadDecrypted()
if err != nil {
return ambient.StatusError{Code: http.StatusInternalServerError, Err: err}
}
err = dc.storage.Save()
if err != nil {
return ambient.StatusError{Code: http.StatusInternalServerError, Err: err}
}
return nil
})
// Decrypt the site JSON file on disk.
mux.Post("/storage/decrypt", func(w http.ResponseWriter, r *http.Request) error {
dc.log.Debug("site.bin decrypted")
err := dc.storage.SaveDecrypted()
if err != nil {
return ambient.StatusError{Code: http.StatusInternalServerError, Err: err}
}
return nil
})
// Return a list of plugin names.
mux.Get("/plugins", func(w http.ResponseWriter, r *http.Request) error {
dc.log.Debug("get plugin names")
return JSON(w, dc.pluginsystem.TrustedPluginNames())
})
// Enable one plugin.
mux.Post("/plugins/{pluginName}/enable", func(w http.ResponseWriter, r *http.Request) error {
pluginName := mux.Param(r, "pluginName")
dc.log.Debug("enable plugin: %v", pluginName)
err := dc.securestorage.EnablePlugin(pluginName, true)
if err != nil {
return ambient.StatusError{Code: http.StatusBadRequest, Err: err}
}
return nil
})
// Enable all plugins.
mux.Post("/plugins/enable", func(w http.ResponseWriter, r *http.Request) error {
dc.log.Debug("enable all plugins")
// Loop through all the trusted plugins.
for _, pluginName := range dc.pluginsystem.TrustedPluginNames() {
err := dc.securestorage.EnablePlugin(pluginName, true)
if err != nil {
// TODO: Should return an error at the end if at least one fails.
dc.log.Error("failed to enable plugin (%v): %v", pluginName, err.Error())
// Continue on
}
}
return nil
})
// Enable all grants for one plugin.
mux.Post("/plugins/{pluginName}/grant", func(w http.ResponseWriter, r *http.Request) error {
pluginName := mux.Param(r, "pluginName")
dc.log.Debug("enable plugin grants: %v", pluginName)
p, err := dc.pluginsystem.Plugin(pluginName)
if err != nil {
return ambient.StatusError{Code: http.StatusBadRequest,
Err: fmt.Errorf("failed to get plugin (%v) for grants: %v", pluginName, err.Error())}
}
for _, request := range p.GrantRequests() {
dc.log.Debug("plugin (%v), add grant: %v", pluginName, request.Grant)
err := dc.securestorage.SetNeighborPluginGrant(pluginName, request.Grant, true)
if err != nil {
return ambient.StatusError{Code: http.StatusBadRequest,
Err: fmt.Errorf("failed to enable plugin (%v) for grant, %v: %v", pluginName, request.Grant, err.Error())}
}
}
return nil
})
// Enable all grants for all plugins.
mux.Post("/plugins/grant", func(w http.ResponseWriter, r *http.Request) error {
pluginName := mux.Param(r, "pluginName")
dc.log.Debug("enable plugin grant: %v", pluginName)
// Loop through all the trusted plugins.
for _, pluginName := range dc.pluginsystem.TrustedPluginNames() {
p, err := dc.pluginsystem.Plugin(pluginName)
if err != nil {
return ambient.StatusError{Code: http.StatusBadRequest,
Err: fmt.Errorf("failed to get plugin (%v) for grants: %v", pluginName, err.Error())}
}
for _, request := range p.GrantRequests() {
dc.log.Debug("plugin (%v), add grant: %v", pluginName, request.Grant)
err := dc.securestorage.SetNeighborPluginGrant(pluginName, request.Grant, true)
if err != nil {
return ambient.StatusError{Code: http.StatusBadRequest,
Err: fmt.Errorf("failed to enable plugin (%v) for grant, %v: %v", pluginName, request.Grant, err.Error())}
}
}
}
return nil
})
err := http.ListenAndServe(":"+envdetect.DevConsolePort(), mux)
if err != nil {
dc.log.Error("listener cannot start: %v", err.Error())
}
}()
}