forked from sensu/sensu-go
/
dashboardd.go
291 lines (248 loc) · 6.83 KB
/
dashboardd.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
package dashboardd
import (
"compress/gzip"
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/httputil"
"net/url"
"sync"
"sync/atomic"
"time"
"github.com/NYTimes/gziphandler"
"github.com/gorilla/mux"
"github.com/sensu/sensu-go/backend/dashboardd/asset"
"github.com/sensu/sensu-go/dashboard"
"github.com/sensu/sensu-go/types"
"github.com/sirupsen/logrus"
)
// Config represents the dashboard configuration
type Config struct {
Host string
Port int
TLS *types.TLSOptions
APIURL string
}
// Dashboardd represents the dashboard daemon
type Dashboardd struct {
stopping chan struct{}
running *atomic.Value
wg *sync.WaitGroup
errChan chan error
httpServer *http.Server
logger *logrus.Entry
Config
Assets *asset.Collection
}
// Option is a functional option.
type Option func(*Dashboardd) error
// New creates a new Dashboardd.
func New(cfg Config, opts ...Option) (*Dashboardd, error) {
d := &Dashboardd{
Config: cfg,
stopping: make(chan struct{}, 1),
running: &atomic.Value{},
wg: &sync.WaitGroup{},
errChan: make(chan error, 1),
logger: logrus.WithField(
"component", "dashboard",
),
}
// load assets
collection := asset.NewCollection()
collection.Extend(dashboard.OSS)
d.Assets = collection
// prepare server TLS config
tlsServerConfig, err := cfg.TLS.ToServerTLSConfig()
if err != nil {
return nil, err
}
d.httpServer = &http.Server{
Addr: fmt.Sprintf("%s:%d", d.Host, d.Port),
Handler: httpRouter(d),
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
TLSConfig: tlsServerConfig,
}
for _, o := range opts {
if err := o(d); err != nil {
return nil, err
}
}
return d, nil
}
// Start dashboardd
func (d *Dashboardd) Start() error {
d.logger.Info("starting dashboardd on address: ", d.httpServer.Addr)
d.wg.Add(1)
go func() {
defer d.wg.Done()
var err error
TLS := d.Config.TLS
if TLS != nil {
// TLS configuration comes from ToServerTLSConfig
err = d.httpServer.ListenAndServeTLS("", "")
} else {
err = d.httpServer.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
d.errChan <- fmt.Errorf("failed to start http/https server %s", err)
}
}()
return nil
}
// Stop dashboardd.
func (d *Dashboardd) Stop() error {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
err := d.httpServer.Shutdown(ctx)
close(d.stopping)
d.wg.Wait()
close(d.errChan)
return err
}
// Err returns a channel to listen for terminal errors on.
func (d *Dashboardd) Err() <-chan error {
return d.errChan
}
// Name returns the daemon name
func (d *Dashboardd) Name() string {
return "dashboardd"
}
func httpRouter(d *Dashboardd) *mux.Router {
r := mux.NewRouter()
backendProxy, err := newBackendProxy(d.Config.APIURL, d.Config.TLS)
if err != nil {
d.errChan <- err
}
// Gzip content
gziphandler, err := gziphandler.NewGzipLevelAndMinSize(
gzip.DefaultCompression,
gziphandler.DefaultMinSize,
)
if err != nil {
panic(err)
}
// Proxy endpoints
r.PathPrefix("/auth").Handler(backendProxy)
r.PathPrefix("/api").Handler(backendProxy)
r.PathPrefix("/graphql").Handler(gziphandler(backendProxy))
// Expose Asset Info
r.PathPrefix("/index.json").Handler(gziphandler(listAssetsHandler(d.Assets, d.logger)))
// Serve assets
r.PathPrefix("/static").Handler(gziphandler(staticHandler(d.Assets)))
r.PathPrefix("/").Handler(rootHandler(d.Assets))
return r
}
func staticHandler(fs http.FileSystem) http.Handler {
handler := http.FileServer(fs)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// set cache-control header to allow client to cache file indefinitely.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching#Freshness
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#Revalidation_and_reloading
w.Header().Set("cache-control", "max-age=31536000, immutable")
handler.ServeHTTP(w, r)
})
}
func rootHandler(fs http.FileSystem) http.Handler {
handler := http.FileServer(fs)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Fallback to index if path didn't match an asset
if f, _ := fs.Open(r.URL.Path); f == nil {
r.URL.Path = "/"
}
// Sets the proper headers to prevent any sort of caching for the non-static
// files such as the index and manifest.
w.Header().Set("cache-control", "no-cache, no-store, must-revalidate")
w.Header().Set("pragma", "no-cache")
w.Header().Set("expires", "0")
handler.ServeHTTP(w, r)
})
}
func newBackendProxy(aurl string, tlsOpts *types.TLSOptions) (*httputil.ReverseProxy, error) {
// API gateway to Sensu API
target, err := url.Parse(aurl)
if err != nil {
return nil, err
}
// Copy of values from http.DefaultTransport
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
// Configure TLS
if tlsOpts != nil {
cfg, err := tlsOpts.ToServerTLSConfig()
if err != nil {
return nil, err
}
// TODO(palourde): We should avoid using the loopback interface
cfg.InsecureSkipVerify = true
transport.TLSClientConfig = cfg
}
proxy := httputil.NewSingleHostReverseProxy(target)
proxy.Transport = transport
return proxy, nil
}
type Path struct {
Name string
IsDir bool
ModTime time.Time
Size int64
}
func listAssetsHandler(fs http.FileSystem, logger *logrus.Entry) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Derive contents of assets collection
paths, err := asset.ListContents(fs, "/")
if err != nil {
logger.WithError(err).Error("unable to list assets")
writeError(w)
return
}
// Sanitize
out := []Path{}
for _, path := range paths {
out = append(out, Path{
Name: path.Name,
IsDir: path.IsDir(),
ModTime: path.ModTime(),
Size: path.Size(),
})
}
// Write
if err = writeJSON(w, out); err != nil {
logger.WithError(err).Error("unable to write assets")
writeError(w)
}
})
}
func writeJSON(w http.ResponseWriter, d interface{}) error {
// Headers
w.Header().Set("Content-Type", "application/json")
// Marshal
bytes, err := json.Marshal(d)
if err != nil {
return err
}
// If we receive an err here it is most likely because the client has
// disconnected and if not a disconnect, there isn't much we can do either.
// https://github.com/sensu/sensu-go/pull/2786#discussion_r265258086
_, _ = w.Write(bytes)
return nil
}
func writeError(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(500)
_, _ = w.Write([]byte("{}"))
}