forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apid.go
283 lines (256 loc) · 8.22 KB
/
apid.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
package apid
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sensu/sensu-go/backend/apid/actions"
"github.com/sensu/sensu-go/backend/apid/middlewares"
"github.com/sensu/sensu-go/backend/apid/routers"
"github.com/sensu/sensu-go/backend/authentication"
"github.com/sensu/sensu-go/backend/authorization/rbac"
"github.com/sensu/sensu-go/backend/messaging"
"github.com/sensu/sensu-go/backend/store"
"github.com/sensu/sensu-go/types"
)
// APId is the backend HTTP API.
type APId struct {
Authenticator *authentication.Authenticator
HTTPServer *http.Server
CoreSubrouter *mux.Router
GraphQLSubrouter *mux.Router
stopping chan struct{}
running *atomic.Value
wg *sync.WaitGroup
errChan chan error
bus messaging.MessageBus
store store.Store
eventStore store.EventStore
queueGetter types.QueueGetter
tls *types.TLSOptions
cluster clientv3.Cluster
etcdClientTLSConfig *tls.Config
clusterVersion string
}
// Option is a functional option.
type Option func(*APId) error
// Config configures APId.
type Config struct {
ListenAddress string
URL string
Bus messaging.MessageBus
Store store.Store
EventStore store.EventStore
QueueGetter types.QueueGetter
TLS *types.TLSOptions
Cluster clientv3.Cluster
EtcdClientTLSConfig *tls.Config
Authenticator *authentication.Authenticator
ClusterVersion string
}
// New creates a new APId.
func New(c Config, opts ...Option) (*APId, error) {
a := &APId{
store: c.Store,
eventStore: c.EventStore,
queueGetter: c.QueueGetter,
tls: c.TLS,
bus: c.Bus,
stopping: make(chan struct{}, 1),
running: &atomic.Value{},
wg: &sync.WaitGroup{},
errChan: make(chan error, 1),
cluster: c.Cluster,
etcdClientTLSConfig: c.EtcdClientTLSConfig,
Authenticator: c.Authenticator,
clusterVersion: c.ClusterVersion,
}
// prepare TLS configs (both server and client)
var tlsServerConfig, tlsClientConfig *tls.Config
var err error
if c.TLS != nil {
tlsServerConfig, err = c.TLS.ToServerTLSConfig()
if err != nil {
return nil, err
}
// TODO(gbolo): since the backend does not yet support mutual TLS
// this client does not need a cert and key. Once we do support
// mutual TLS we need new options for client cert and key
cTLSOptions := types.TLSOptions{
TrustedCAFile: c.TLS.TrustedCAFile,
// TODO(palourde): We should avoid using the loopback interface
InsecureSkipVerify: true,
}
tlsClientConfig, err = cTLSOptions.ToClientTLSConfig()
if err != nil {
return nil, err
}
}
router := mux.NewRouter().UseEncodedPath()
router.NotFoundHandler = middlewares.SimpleLogger{}.Then(http.HandlerFunc(notFoundHandler))
router.Handle("/metrics", promhttp.Handler())
registerUnauthenticatedResources(router, a.store, a.cluster, a.etcdClientTLSConfig, a.clusterVersion, a.bus)
a.registerGraphQLService(router, c.URL, tlsClientConfig)
registerAuthenticationResources(router, a.store, a.Authenticator)
a.registerRestrictedResources(router)
a.HTTPServer = &http.Server{
Addr: c.ListenAddress,
Handler: router,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
TLSConfig: tlsServerConfig,
}
for _, o := range opts {
if err := o(a); err != nil {
return nil, err
}
}
return a, nil
}
func notFoundHandler(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNotFound)
resp := map[string]interface{}{
"message": "not found", "code": actions.NotFound,
}
_ = json.NewEncoder(w).Encode(resp)
}
// Start APId.
func (a *APId) Start() error {
logger.Info("starting apid on address: ", a.HTTPServer.Addr)
a.wg.Add(1)
go func() {
defer a.wg.Done()
var err error
if a.tls != nil {
// TLS configuration comes from ToServerTLSConfig
err = a.HTTPServer.ListenAndServeTLS("", "")
} else {
err = a.HTTPServer.ListenAndServe()
}
if err != nil && err != http.ErrServerClosed {
a.errChan <- fmt.Errorf("failed to start http/https server %s", err)
}
}()
return nil
}
// Stop httpApi.
func (a *APId) Stop() error {
if err := a.HTTPServer.Shutdown(context.TODO()); err != nil {
// failure/timeout shutting down the server gracefully
logger.Error("failed to shutdown http server gracefully - forcing shutdown")
if closeErr := a.HTTPServer.Close(); closeErr != nil {
logger.Error("failed to shutdown http server forcefully")
}
}
a.running.Store(false)
close(a.stopping)
a.wg.Wait()
close(a.errChan)
return nil
}
// Err returns a channel to listen for terminal errors on.
func (a *APId) Err() <-chan error {
return a.errChan
}
// Name returns the daemon name
func (a *APId) Name() string {
return "apid"
}
func registerUnauthenticatedResources(
router *mux.Router,
store store.Store,
cluster clientv3.Cluster,
etcdClientTLSConfig *tls.Config,
clusterVersion string,
bus messaging.MessageBus,
) {
mountRouters(
NewSubrouter(
router.NewRoute(),
middlewares.SimpleLogger{},
middlewares.LimitRequest{},
),
routers.NewHealthRouter(actions.NewHealthController(store, cluster, etcdClientTLSConfig)),
routers.NewVersionRouter(actions.NewVersionController(clusterVersion)),
routers.NewTessenMetricRouter(actions.NewTessenMetricController(bus)),
)
}
func (a *APId) registerGraphQLService(router *mux.Router, url string, tls *tls.Config) {
a.GraphQLSubrouter = NewSubrouter(
router.NewRoute(),
middlewares.SimpleLogger{},
middlewares.LimitRequest{},
// TODO: Currently the web app relies on receiving a 401 to determine if
// a user is not authenticated. However, in the future we should
// allow requests without an access token to continue so that
// unauthenticated clients can still fetch the schema. Useful for
// implementing tools like GraphiQL.
//
// https://github.com/graphql/graphiql
// https://graphql.org/learn/introspection/
middlewares.Authentication{IgnoreUnauthorized: false},
middlewares.AllowList{Store: a.store, IgnoreMissingClaims: true},
)
mountRouters(
a.GraphQLSubrouter,
routers.NewGraphQLRouter(url, tls, a.store),
)
}
func registerAuthenticationResources(router *mux.Router, store store.Store, authenticator *authentication.Authenticator) {
mountRouters(
NewSubrouter(
router.NewRoute(),
middlewares.SimpleLogger{},
middlewares.RefreshToken{},
middlewares.LimitRequest{},
),
routers.NewAuthenticationRouter(store, authenticator),
)
}
func (a *APId) registerRestrictedResources(router *mux.Router) {
a.CoreSubrouter = NewSubrouter(
router.NewRoute().
PathPrefix("/api/{group:core}/{version:v2}/"),
middlewares.SimpleLogger{},
middlewares.Namespace{},
middlewares.Authentication{},
middlewares.AllowList{Store: a.store},
middlewares.AuthorizationAttributes{},
middlewares.Authorization{Authorizer: &rbac.Authorizer{Store: a.store}},
middlewares.LimitRequest{},
middlewares.Pagination{},
)
mountRouters(
a.CoreSubrouter,
routers.NewAssetRouter(a.store),
routers.NewChecksRouter(a.store, a.queueGetter),
routers.NewClusterRolesRouter(a.store),
routers.NewClusterRoleBindingsRouter(a.store),
routers.NewClusterRouter(actions.NewClusterController(a.cluster, a.store)),
routers.NewEntitiesRouter(a.store, a.eventStore),
routers.NewEventFiltersRouter(a.store),
routers.NewEventsRouter(a.eventStore, a.bus),
routers.NewExtensionsRouter(a.store),
routers.NewHandlersRouter(a.store),
routers.NewHooksRouter(a.store),
routers.NewMutatorsRouter(a.store),
routers.NewNamespacesRouter(a.store),
routers.NewRolesRouter(a.store),
routers.NewRoleBindingsRouter(a.store),
routers.NewSilencedRouter(a.store),
routers.NewTessenRouter(actions.NewTessenController(a.store, a.bus)),
routers.NewUsersRouter(a.store),
)
}
func mountRouters(parent *mux.Router, subRouters ...routers.Router) {
for _, subRouter := range subRouters {
subRouter.Mount(parent)
}
}