-
Notifications
You must be signed in to change notification settings - Fork 0
/
apiserver.go
1181 lines (1066 loc) · 36.6 KB
/
apiserver.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/names/v5"
"github.com/juju/pubsub/v2"
"github.com/juju/ratelimit"
"github.com/juju/worker/v3/dependency"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/tomb.v2"
"github.com/juju/juju/apiserver/apiserverhttp"
"github.com/juju/juju/apiserver/authentication"
"github.com/juju/juju/apiserver/authentication/jwt"
"github.com/juju/juju/apiserver/authentication/macaroon"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/common/apihttp"
"github.com/juju/juju/apiserver/common/crossmodel"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/httpcontext"
"github.com/juju/juju/apiserver/logsink"
"github.com/juju/juju/apiserver/observer"
"github.com/juju/juju/apiserver/stateauthenticator"
"github.com/juju/juju/apiserver/websocket"
"github.com/juju/juju/controller"
"github.com/juju/juju/core/auditlog"
"github.com/juju/juju/core/cache"
coredatabase "github.com/juju/juju/core/database"
"github.com/juju/juju/core/lease"
"github.com/juju/juju/core/multiwatcher"
"github.com/juju/juju/core/presence"
"github.com/juju/juju/core/resources"
"github.com/juju/juju/pubsub/apiserver"
controllermsg "github.com/juju/juju/pubsub/controller"
"github.com/juju/juju/resource"
"github.com/juju/juju/rpc"
"github.com/juju/juju/rpc/jsoncodec"
"github.com/juju/juju/state"
"github.com/juju/juju/worker/syslogger"
)
var logger = loggo.GetLogger("juju.apiserver")
var defaultHTTPMethods = []string{"GET", "POST", "HEAD", "PUT", "DELETE", "OPTIONS"}
// Server holds the server side of the API.
type Server struct {
tomb tomb.Tomb
clock clock.Clock
pingClock clock.Clock
wg sync.WaitGroup
shared *sharedServerContext
// tag of the machine where the API server is running.
tag names.Tag
dataDir string
logDir string
facades *facade.Registry
localMacaroonAuthenticator macaroon.LocalMacaroonAuthenticator
jwtAuthenticator jwt.Authenticator
httpAuthenticators []authentication.HTTPAuthenticator
loginAuthenticators []authentication.LoginAuthenticator
offerAuthCtxt *crossmodel.AuthContext
lastConnectionID uint64
newObserver observer.ObserverFactory
allowModelAccess bool
logSinkWriter io.WriteCloser
logsinkRateLimitConfig logsink.RateLimitConfig
apiServerLoggers apiServerLoggers
getAuditConfig func() auditlog.Config
upgradeComplete func() bool
mux *apiserverhttp.Mux
metricsCollector *Collector
execEmbeddedCommand ExecEmbeddedCommandFunc
// mu guards the fields below it.
mu sync.Mutex
// healthStatus is returned from the health endpoint.
healthStatus string
// publicDNSName_ holds the value that will be returned in
// LoginResult.PublicDNSName. Currently this is set once and does
// not change but in the future it may change when a server
// certificate is explicitly set, hence it's here guarded by the
// mutex.
publicDNSName_ string
// agentRateLimitMax and agentRateLimitRate are values used to create
// the token bucket that ratelimits the agent connections. These values
// come from controller config, and can be updated on the fly to adjust
// the rate limiting.
agentRateLimitMax int
agentRateLimitRate time.Duration
agentRateLimit *ratelimit.Bucket
// resourceLock is used to limit the number of
// concurrent resource downloads to units.
resourceLock resource.ResourceDownloadLock
// registerIntrospectionHandlers is a function that will
// call a function with (path, http.Handler) tuples. This
// is to support registering the handlers underneath the
// "/introspection" prefix.
registerIntrospectionHandlers func(func(string, http.Handler))
}
// ServerConfig holds parameters required to set up an API server.
type ServerConfig struct {
Clock clock.Clock
PingClock clock.Clock
Tag names.Tag
DataDir string
LogDir string
Hub *pubsub.StructuredHub
Presence presence.Recorder
Mux *apiserverhttp.Mux
// LocalMacaroonAuthenticator is the request authenticator used for verifying
// local user macaroons.
LocalMacaroonAuthenticator macaroon.LocalMacaroonAuthenticator
// JWTAuthenticator is the request authenticator used for validating jwt
// tokens when the controller has been bootstrapped with a trusted token
// provider.
JWTAuthenticator jwt.Authenticator
// MultiwatcherFactory is used by the API server to create
// multiwatchers. The real factory is managed by the multiwatcher
// worker.
MultiwatcherFactory multiwatcher.Factory
// StatePool is the StatePool used for looking up State
// to pass to facades. StatePool will not be closed by the
// server; it is the callers responsibility to close it
// after the apiserver has exited.
StatePool *state.StatePool
// Controller is the in-memory representation of the models
// in the controller. It is kept up to date with an all model
// watcher and the modelcache worker.
Controller *cache.Controller
// UpgradeComplete is a function that reports whether or not
// the if the agent running the API server has completed
// running upgrade steps. This is used by the API server to
// limit logins during upgrades.
UpgradeComplete func() bool
// PublicDNSName is reported to the API clients who connect.
PublicDNSName string
// AllowModelAccess holds whether users will be allowed to
// access models that they have access rights to even when
// they don't have access to the controller.
AllowModelAccess bool
// NewObserver is a function which will return an observer. This
// is used per-connection to instantiate a new observer to be
// notified of key events during API requests.
NewObserver observer.ObserverFactory
// RegisterIntrospectionHandlers is a function that will
// call a function with (path, http.Handler) tuples. This
// is to support registering the handlers underneath the
// "/introspection" prefix.
RegisterIntrospectionHandlers func(func(string, http.Handler))
// LogSinkConfig holds parameters to control the API server's
// logsink endpoint behaviour. If this is nil, the values from
// DefaultLogSinkConfig() will be used.
LogSinkConfig *LogSinkConfig
// SysLogger is a logger that will tee the output from logging
// to the local syslog.
SysLogger syslogger.SysLogger
// GetAuditConfig holds a function that returns the current audit
// logging config. The function may return updated values, so
// should be called every time a new login is handled.
GetAuditConfig func() auditlog.Config
// LeaseManager gives access to leadership and singular claimers
// and checkers for use in API facades.
LeaseManager lease.Manager
// MetricsCollector defines all the metrics to be collected for the
// apiserver
MetricsCollector *Collector
// ExecEmbeddedCommand is a function which creates an embedded Juju CLI instance.
ExecEmbeddedCommand ExecEmbeddedCommandFunc
// CharmhubHTTPClient is the HTTP client used for Charmhub API requests.
CharmhubHTTPClient facade.HTTPClient
// DBGetter supplies sql.DB references on request, for named databases.
DBGetter coredatabase.DBGetter
}
// Validate validates the API server configuration.
func (c ServerConfig) Validate() error {
if c.StatePool == nil {
return errors.NotValidf("missing StatePool")
}
if c.Controller == nil {
return errors.NotValidf("missing Controller")
}
if c.MultiwatcherFactory == nil {
return errors.NotValidf("missing MultiwatcherFactory")
}
if c.Hub == nil {
return errors.NotValidf("missing Hub")
}
if c.Presence == nil {
return errors.NotValidf("missing Presence")
}
if c.Mux == nil {
return errors.NotValidf("missing Mux")
}
if c.LocalMacaroonAuthenticator == nil {
return errors.NotValidf("missing local macaroon authenticator")
}
if c.Clock == nil {
return errors.NotValidf("missing Clock")
}
if c.NewObserver == nil {
return errors.NotValidf("missing NewObserver")
}
if c.UpgradeComplete == nil {
return errors.NotValidf("nil UpgradeComplete")
}
if c.GetAuditConfig == nil {
return errors.NotValidf("missing GetAuditConfig")
}
if c.LogSinkConfig != nil {
if err := c.LogSinkConfig.Validate(); err != nil {
return errors.Annotate(err, "validating logsink configuration")
}
}
if c.SysLogger == nil {
return errors.NotValidf("nil SysLogger")
}
if c.MetricsCollector == nil {
return errors.NotValidf("missing MetricsCollector")
}
return nil
}
func (c ServerConfig) pingClock() clock.Clock {
if c.PingClock == nil {
return c.Clock
}
return c.PingClock
}
// NewServer serves API requests using the given configuration.
func NewServer(cfg ServerConfig) (*Server, error) {
if cfg.LogSinkConfig == nil {
logSinkConfig := DefaultLogSinkConfig()
cfg.LogSinkConfig = &logSinkConfig
}
if err := cfg.Validate(); err != nil {
return nil, errors.Trace(err)
}
// Important note:
// Do not manipulate the state within NewServer as the API
// server needs to run before mongo upgrades have happened and
// any state manipulation may be relying on features of the
// database added by upgrades. Here be dragons.
return newServer(cfg)
}
const readyTimeout = time.Second * 30
func newServer(cfg ServerConfig) (_ *Server, err error) {
systemState, err := cfg.StatePool.SystemState()
if err != nil {
return nil, errors.Trace(err)
}
controllerConfig, err := systemState.ControllerConfig()
if err != nil {
return nil, errors.Annotate(err, "unable to get controller config")
}
shared, err := newSharedServerContext(sharedServerConfig{
statePool: cfg.StatePool,
controller: cfg.Controller,
multiwatcherFactory: cfg.MultiwatcherFactory,
centralHub: cfg.Hub,
presence: cfg.Presence,
leaseManager: cfg.LeaseManager,
controllerConfig: controllerConfig,
logger: loggo.GetLogger("juju.apiserver"),
charmhubHTTPClient: cfg.CharmhubHTTPClient,
dbGetter: cfg.DBGetter,
})
if err != nil {
return nil, errors.Trace(err)
}
systemState, err = cfg.StatePool.SystemState()
if err != nil {
return nil, errors.Trace(err)
}
model, err := systemState.Model()
if err != nil {
return nil, errors.Trace(err)
}
modelConfig, err := model.Config()
if err != nil {
return nil, errors.Trace(err)
}
loggingOutputs, _ := modelConfig.LoggingOutput()
httpAuthenticators := []authentication.HTTPAuthenticator{cfg.LocalMacaroonAuthenticator}
loginAuthenticators := []authentication.LoginAuthenticator{cfg.LocalMacaroonAuthenticator}
// We only want to add the jwt authenticator if it's not nil.
if cfg.JWTAuthenticator != nil {
httpAuthenticators = append([]authentication.HTTPAuthenticator{cfg.JWTAuthenticator}, httpAuthenticators...)
loginAuthenticators = append([]authentication.LoginAuthenticator{cfg.JWTAuthenticator}, loginAuthenticators...)
}
srv := &Server{
clock: cfg.Clock,
pingClock: cfg.pingClock(),
newObserver: cfg.NewObserver,
shared: shared,
tag: cfg.Tag,
dataDir: cfg.DataDir,
logDir: cfg.LogDir,
upgradeComplete: cfg.UpgradeComplete,
facades: AllFacades(),
mux: cfg.Mux,
localMacaroonAuthenticator: cfg.LocalMacaroonAuthenticator,
jwtAuthenticator: cfg.JWTAuthenticator,
httpAuthenticators: httpAuthenticators,
loginAuthenticators: loginAuthenticators,
allowModelAccess: cfg.AllowModelAccess,
publicDNSName_: cfg.PublicDNSName,
registerIntrospectionHandlers: cfg.RegisterIntrospectionHandlers,
logsinkRateLimitConfig: logsink.RateLimitConfig{
Refill: cfg.LogSinkConfig.RateLimitRefill,
Burst: cfg.LogSinkConfig.RateLimitBurst,
Clock: cfg.Clock,
},
getAuditConfig: cfg.GetAuditConfig,
apiServerLoggers: apiServerLoggers{
syslogger: cfg.SysLogger,
loggingOutputs: loggingOutputs,
clock: cfg.Clock,
loggerBufferSize: cfg.LogSinkConfig.DBLoggerBufferSize,
loggerFlushInterval: cfg.LogSinkConfig.DBLoggerFlushInterval,
},
metricsCollector: cfg.MetricsCollector,
execEmbeddedCommand: cfg.ExecEmbeddedCommand,
healthStatus: "starting",
}
srv.updateAgentRateLimiter(controllerConfig)
srv.updateResourceDownloadLimiters(controllerConfig)
// We are able to get the current controller config before subscribing to changes
// because the changes are only ever published in response to an API call,
// and we know that we can't make any API calls until the server has started.
unsubscribeControllerConfig, err := cfg.Hub.Subscribe(
controllermsg.ConfigChanged,
func(topic string, data controllermsg.ConfigChangedMessage, err error) {
if err != nil {
logger.Criticalf("programming error in %s message data: %v", topic, err)
return
}
srv.updateAgentRateLimiter(data.Config)
srv.updateResourceDownloadLimiters(data.Config)
})
if err != nil {
logger.Criticalf("programming error in subscribe function: %v", err)
return nil, errors.Trace(err)
}
srv.shared.cancel = srv.tomb.Dying()
// The auth context for authenticating access to application offers.
srv.offerAuthCtxt, err = newOfferAuthcontext(cfg.StatePool)
if err != nil {
unsubscribeControllerConfig()
return nil, errors.Trace(err)
}
if model.Type() == state.ModelTypeCAAS {
// CAAS controller writes log to stdout. We should ensure that we don't
// close the logSinkWriter when we stopping the tomb, otherwise we get
// no output to stdout anymore.
srv.logSinkWriter = nonCloseableWriter{
WriteCloser: os.Stdout,
}
} else {
srv.logSinkWriter, err = logsink.NewFileWriter(
filepath.Join(srv.logDir, "logsink.log"),
controllerConfig.AgentLogfileMaxSizeMB(),
controllerConfig.AgentLogfileMaxBackups(),
)
if err != nil {
return nil, errors.Annotate(err, "creating logsink writer")
}
}
unsubscribe, err := cfg.Hub.Subscribe(apiserver.RestartTopic, func(string, map[string]interface{}) {
srv.tomb.Kill(dependency.ErrBounce)
})
if err != nil {
unsubscribeControllerConfig()
return nil, errors.Annotate(err, "unable to subscribe to restart message")
}
ready := make(chan struct{})
srv.tomb.Go(func() error {
defer srv.apiServerLoggers.dispose()
defer srv.logSinkWriter.Close()
defer srv.shared.Close()
defer unsubscribe()
defer unsubscribeControllerConfig()
return srv.loop(ready)
})
// Don't return until all handlers have been registered.
select {
case <-ready:
case <-srv.clock.After(readyTimeout):
return nil, errors.New("loop never signalled ready")
}
return srv, nil
}
// nonCloseableWriter ensures that we never close the underlying writer. If the
// underlying writer is os.stdout and we close that, then nothing will be
// written until a new instance of the program is launched.
type nonCloseableWriter struct {
io.WriteCloser
}
// Close does not do anything in this instance.
func (nonCloseableWriter) Close() error {
return nil
}
// Report is shown in the juju_engine_report.
func (srv *Server) Report() map[string]interface{} {
srv.mu.Lock()
defer srv.mu.Unlock()
result := map[string]interface{}{
"agent-ratelimit-max": srv.agentRateLimitMax,
"agent-ratelimit-rate": srv.agentRateLimitRate,
}
if srv.publicDNSName_ != "" {
result["public-dns-name"] = srv.publicDNSName_
}
return result
}
// Dead returns a channel that signals when the server has exited.
func (srv *Server) Dead() <-chan struct{} {
return srv.tomb.Dead()
}
// Stop stops the server and returns when all running requests
// have completed.
func (srv *Server) Stop() error {
srv.tomb.Kill(nil)
return srv.tomb.Wait()
}
// Kill implements worker.Worker.Kill.
func (srv *Server) Kill() {
srv.tomb.Kill(nil)
}
// Wait implements worker.Worker.Wait.
func (srv *Server) Wait() error {
return srv.tomb.Wait()
}
func (srv *Server) updateAgentRateLimiter(cfg controller.Config) {
srv.mu.Lock()
defer srv.mu.Unlock()
srv.agentRateLimitMax = cfg.AgentRateLimitMax()
srv.agentRateLimitRate = cfg.AgentRateLimitRate()
if srv.agentRateLimitMax > 0 {
srv.agentRateLimit = ratelimit.NewBucketWithClock(
srv.agentRateLimitRate, int64(srv.agentRateLimitMax), rateClock{srv.clock})
} else {
srv.agentRateLimit = nil
}
}
func (srv *Server) updateResourceDownloadLimiters(cfg controller.Config) {
srv.mu.Lock()
defer srv.mu.Unlock()
globalLimit := cfg.ControllerResourceDownloadLimit()
appLimit := cfg.ApplicationResourceDownloadLimit()
srv.resourceLock = resource.NewResourceDownloadLimiter(globalLimit, appLimit)
}
func (srv *Server) getResourceDownloadLimiter() resource.ResourceDownloadLock {
srv.mu.Lock()
defer srv.mu.Unlock()
return srv.resourceLock
}
type rateClock struct {
clock.Clock
}
func (rateClock) Sleep(time.Duration) {
// no-op, we don't sleep.
}
func (srv *Server) getAgentToken() error {
srv.mu.Lock()
defer srv.mu.Unlock()
// agentRateLimit is nil if rate limiting is disabled.
if srv.agentRateLimit == nil {
return nil
}
// Try to take one token, but don't wait any time for it.
if _, ok := srv.agentRateLimit.TakeMaxDuration(1, 0); !ok {
return apiservererrors.ErrTryAgain
}
return nil
}
// logsinkMetricsCollectorWrapper defines a wrapper for exposing the essentials
// for the logsink api handler to interact with the metrics collector.
type logsinkMetricsCollectorWrapper struct {
collector *Collector
}
func (w logsinkMetricsCollectorWrapper) TotalConnections() prometheus.Counter {
return w.collector.TotalConnections
}
func (w logsinkMetricsCollectorWrapper) Connections() prometheus.Gauge {
return w.collector.APIConnections.WithLabelValues("logsink")
}
func (w logsinkMetricsCollectorWrapper) PingFailureCount(modelUUID string) prometheus.Counter {
return w.collector.PingFailureCount.WithLabelValues(modelUUID, "logsink")
}
func (w logsinkMetricsCollectorWrapper) LogWriteCount(modelUUID, state string) prometheus.Counter {
return w.collector.LogWriteCount.WithLabelValues(modelUUID, state)
}
func (w logsinkMetricsCollectorWrapper) LogReadCount(modelUUID, state string) prometheus.Counter {
return w.collector.LogReadCount.WithLabelValues(modelUUID, state)
}
// httpRequestRecorderWrapper defines a wrapper from exposing the
// essentials for the http request recorder.
type httpRequestRecorderWrapper struct {
collector *Collector
modelUUID string
}
// Record an outgoing request which produced an http.Response.
func (w httpRequestRecorderWrapper) Record(method string, url *url.URL, res *http.Response, rtt time.Duration) {
// Note: Do not log url.Path as REST queries _can_ include the name of the
// entities (charms, architectures, etc).
w.collector.TotalRequests.WithLabelValues(w.modelUUID, url.Host, strconv.FormatInt(int64(res.StatusCode), 10)).Inc()
if res.StatusCode >= 400 {
w.collector.TotalRequestErrors.WithLabelValues(w.modelUUID, url.Host).Inc()
}
w.collector.TotalRequestsDuration.WithLabelValues(w.modelUUID, url.Host).Observe(rtt.Seconds())
}
// RecordError records an outgoing request that returned back an error.
func (w httpRequestRecorderWrapper) RecordError(method string, url *url.URL, err error) {
// Note: Do not log url.Path as REST queries _can_ include the name of the
// entities (charms, architectures, etc).
w.collector.TotalRequests.WithLabelValues(w.modelUUID, url.Host, "unknown").Inc()
w.collector.TotalRequestErrors.WithLabelValues(w.modelUUID, url.Host).Inc()
}
// loop is the main loop for the server.
func (srv *Server) loop(ready chan struct{}) error {
// for pat based handlers, they are matched in-order of being
// registered, first match wins. So more specific ones have to be
// registered first.
endpoints, err := srv.endpoints()
if err != nil {
return errors.Trace(err)
}
for _, ep := range endpoints {
_ = srv.mux.AddHandler(ep.Method, ep.Pattern, ep.Handler)
defer srv.mux.RemoveHandler(ep.Method, ep.Pattern)
if ep.Method == "GET" {
_ = srv.mux.AddHandler("HEAD", ep.Pattern, ep.Handler)
defer srv.mux.RemoveHandler("HEAD", ep.Pattern)
}
}
close(ready)
srv.mu.Lock()
srv.healthStatus = "running"
srv.mu.Unlock()
<-srv.tomb.Dying()
srv.mu.Lock()
srv.healthStatus = "stopping"
srv.mu.Unlock()
srv.wg.Wait() // wait for any outstanding requests to complete.
return tomb.ErrDying
}
func (srv *Server) endpoints() ([]apihttp.Endpoint, error) {
const modelRoutePrefix = "/model/:modeluuid"
const charmsObjectsRoutePrefix = "/model-:modeluuid/charms/:object"
type handler struct {
pattern string
methods []string
handler http.Handler
unauthenticated bool
authorizer authentication.Authorizer
tracked bool
noModelUUID bool
}
var endpoints []apihttp.Endpoint
systemState, err := srv.shared.statePool.SystemState()
if err != nil {
return nil, errors.Trace(err)
}
controllerModelUUID := systemState.ModelUUID()
httpAuthenticator := authentication.HTTPStrategicAuthenticator(srv.httpAuthenticators)
addHandler := func(handler handler) {
methods := handler.methods
if methods == nil {
methods = defaultHTTPMethods
}
h := handler.handler
if handler.tracked {
h = srv.trackRequests(h)
}
if !handler.unauthenticated {
h = &httpcontext.AuthHandler{
NextHandler: h,
Authenticator: httpAuthenticator,
Authorizer: handler.authorizer,
}
}
if !handler.noModelUUID {
if strings.HasPrefix(handler.pattern, modelRoutePrefix) {
h = &httpcontext.QueryModelHandler{
Handler: h,
Query: ":modeluuid",
}
} else if strings.HasPrefix(handler.pattern, charmsObjectsRoutePrefix) {
h = &httpcontext.BucketModelHandler{
Handler: h,
Query: ":modeluuid",
}
} else {
h = &httpcontext.ImpliedModelHandler{
Handler: h,
ModelUUID: controllerModelUUID,
}
}
}
for _, method := range methods {
endpoints = append(endpoints, apihttp.Endpoint{
Pattern: handler.pattern,
Method: method,
Handler: h,
})
}
}
httpCtxt := httpContext{srv: srv}
mainAPIHandler := http.HandlerFunc(srv.apiHandler)
healthHandler := http.HandlerFunc(srv.healthHandler)
logStreamHandler := newLogStreamEndpointHandler(httpCtxt)
embeddedCLIHandler := newEmbeddedCLIHandler(httpCtxt)
debugLogHandler := newDebugLogDBHandler(
httpCtxt,
httpAuthenticator,
tagKindAuthorizer{
names.MachineTagKind,
names.ControllerAgentTagKind,
names.UserTagKind,
names.ApplicationTagKind,
},
)
pubsubHandler := newPubSubHandler(httpCtxt, srv.shared.centralHub)
logSinkHandler := logsink.NewHTTPHandler(
newAgentLogWriteCloserFunc(httpCtxt, srv.logSinkWriter, &srv.apiServerLoggers),
httpCtxt.stop(),
&srv.logsinkRateLimitConfig,
logsinkMetricsCollectorWrapper{collector: srv.metricsCollector},
controllerModelUUID,
)
logSinkAuthorizer := tagKindAuthorizer(stateauthenticator.AgentTags)
logTransferHandler := logsink.NewHTTPHandler(
// We don't need to save the migrated logs
// to a logfile as well as to the DB.
newMigrationLogWriteCloserFunc(httpCtxt, &srv.apiServerLoggers),
httpCtxt.stop(),
nil, // no rate-limiting
logsinkMetricsCollectorWrapper{collector: srv.metricsCollector},
controllerModelUUID,
)
modelRestHandler := &modelRestHandler{
ctxt: httpCtxt,
dataDir: srv.dataDir,
}
modelRestServer := &RestHTTPHandler{
GetHandler: modelRestHandler.ServeGet,
}
modelCharmsHandler := &charmsHandler{
ctxt: httpCtxt,
dataDir: srv.dataDir,
stateAuthFunc: httpCtxt.stateForRequestAuthenticatedUser,
}
modelCharmsHTTPHandler := &CharmsHTTPHandler{
PostHandler: modelCharmsHandler.ServePost,
GetHandler: modelCharmsHandler.ServeGet,
}
modelCharmsUploadAuthorizer := tagKindAuthorizer{names.UserTagKind}
modelObjectsCharmsHandler := &objectsCharmHandler{
ctxt: httpCtxt,
stateAuthFunc: httpCtxt.stateForRequestAuthenticatedUser,
}
modelObjectsCharmsHTTPHandler := &objectsCharmHTTPHandler{
GetHandler: modelObjectsCharmsHandler.ServeGet,
PutHandler: modelObjectsCharmsHandler.ServePut,
LegacyCharmsHandler: modelCharmsHTTPHandler,
}
modelToolsUploadHandler := &toolsUploadHandler{
ctxt: httpCtxt,
stateAuthFunc: httpCtxt.stateForRequestAuthenticatedUser,
}
modelToolsUploadAuthorizer := tagKindAuthorizer{names.UserTagKind}
modelToolsDownloadHandler := newToolsDownloadHandler(httpCtxt)
resourcesHandler := &ResourcesHandler{
StateAuthFunc: func(req *http.Request, tagKinds ...string) (ResourcesBackend, state.PoolHelper, names.Tag,
error) {
st, entity, err := httpCtxt.stateForRequestAuthenticatedTag(req, tagKinds...)
if err != nil {
return nil, nil, nil, errors.Trace(err)
}
rst := st.Resources()
return rst, st, entity.Tag(), nil
},
ChangeAllowedFunc: func(req *http.Request) error {
st, err := httpCtxt.stateForRequestUnauthenticated(req)
if err != nil {
return errors.Trace(err)
}
defer st.Release()
blockChecker := common.NewBlockChecker(st)
if err := blockChecker.ChangeAllowed(); err != nil {
return errors.Trace(err)
}
return nil
},
}
unitResourcesHandler := &UnitResourcesHandler{
NewOpener: func(req *http.Request, tagKinds ...string) (resources.Opener, state.PoolHelper, error) {
st, _, err := httpCtxt.stateForRequestAuthenticatedTag(req, tagKinds...)
if err != nil {
return nil, nil, errors.Trace(err)
}
tagStr := req.URL.Query().Get(":unit")
tag, err := names.ParseUnitTag(tagStr)
if err != nil {
return nil, nil, errors.Trace(err)
}
opener, err := resource.NewResourceOpener(st.State, srv.getResourceDownloadLimiter, tag.Id())
if err != nil {
return nil, nil, errors.Trace(err)
}
return opener, st, nil
},
}
controllerAdminAuthorizer := controllerAdminAuthorizer{
controllerTag: systemState.ControllerTag(),
}
migrateCharmsHandler := &charmsHandler{
ctxt: httpCtxt,
dataDir: srv.dataDir,
stateAuthFunc: httpCtxt.stateForMigrationImporting,
}
migrateCharmsHTTPHandler := &CharmsHTTPHandler{
PostHandler: migrateCharmsHandler.ServePost,
GetHandler: migrateCharmsHandler.ServeUnsupported,
}
migrateObjectsCharmsHandler := &objectsCharmHandler{
ctxt: httpCtxt,
stateAuthFunc: httpCtxt.stateForMigrationImporting,
}
migrateObjectsCharmsHTTPHandler := &objectsCharmHTTPHandler{
PutHandler: migrateObjectsCharmsHandler.ServePut,
GetHandler: migrateObjectsCharmsHandler.ServeUnsupported,
LegacyCharmsHandler: migrateCharmsHTTPHandler,
}
migrateToolsUploadHandler := &toolsUploadHandler{
ctxt: httpCtxt,
stateAuthFunc: httpCtxt.stateForMigrationImporting,
}
resourcesMigrationUploadHandler := &resourcesMigrationUploadHandler{
ctxt: httpCtxt,
stateAuthFunc: httpCtxt.stateForMigrationImporting,
}
backupHandler := &backupHandler{ctxt: httpCtxt}
registerHandler := ®isterUserHandler{ctxt: httpCtxt}
// HTTP handler for application offer macaroon authentication.
addOfferAuthHandlers(srv.offerAuthCtxt, srv.mux)
handlers := []handler{{
// This handler is model specific even though it only
// ever makes sense for a controller because the API
// caller that is handed to the worker that is forwarding
// the messages between controllers is bound to the
// /model/:modeluuid namespace.
pattern: modelRoutePrefix + "/pubsub",
handler: pubsubHandler,
tracked: true,
authorizer: controllerAuthorizer{},
}, {
pattern: modelRoutePrefix + "/logstream",
handler: logStreamHandler,
tracked: true,
}, {
pattern: modelRoutePrefix + "/log",
handler: debugLogHandler,
tracked: true,
// The authentication is handled within the debugLogHandler in order
// for discharge required errors to be handled correctly.
unauthenticated: true,
}, {
pattern: modelRoutePrefix + "/logsink",
handler: logSinkHandler,
tracked: true,
authorizer: logSinkAuthorizer,
}, {
pattern: modelRoutePrefix + "/api",
handler: mainAPIHandler,
tracked: true,
unauthenticated: true,
}, {
pattern: modelRoutePrefix + "/commands",
handler: embeddedCLIHandler,
tracked: true,
unauthenticated: true,
}, {
pattern: modelRoutePrefix + "/rest/1.0/:entity/:name/:attribute",
handler: modelRestServer,
}, {
// GET /charms has no authorizer
pattern: modelRoutePrefix + "/charms",
methods: []string{"GET"},
handler: modelCharmsHTTPHandler,
}, {
pattern: modelRoutePrefix + "/charms",
methods: []string{"POST"},
handler: modelCharmsHTTPHandler,
authorizer: modelCharmsUploadAuthorizer,
}, {
pattern: modelRoutePrefix + "/tools",
handler: modelToolsUploadHandler,
authorizer: modelToolsUploadAuthorizer,
}, {
pattern: modelRoutePrefix + "/tools/:version",
handler: modelToolsDownloadHandler,
unauthenticated: true,
}, {
pattern: modelRoutePrefix + "/applications/:application/resources/:resource",
handler: resourcesHandler,
}, {
pattern: modelRoutePrefix + "/units/:unit/resources/:resource",
handler: unitResourcesHandler,
}, {
pattern: modelRoutePrefix + "/backups",
handler: backupHandler,
authorizer: controllerAdminAuthorizer,
}, {
// Legacy migration endpoint. Used by Juju 3.3 and prior
pattern: "/migrate/charms",
handler: migrateCharmsHTTPHandler,
authorizer: controllerAdminAuthorizer,
}, {
pattern: "/migrate/charms/:object",
handler: migrateObjectsCharmsHTTPHandler,
authorizer: controllerAdminAuthorizer,
}, {
pattern: "/migrate/tools",
handler: migrateToolsUploadHandler,
authorizer: controllerAdminAuthorizer,
}, {
pattern: "/migrate/resources",
handler: resourcesMigrationUploadHandler,
authorizer: controllerAdminAuthorizer,
}, {
pattern: "/migrate/logtransfer",
handler: logTransferHandler,
tracked: true,
authorizer: controllerAdminAuthorizer,
}, {
pattern: "/api",
handler: mainAPIHandler,
tracked: true,
unauthenticated: true,
noModelUUID: true,
}, {
pattern: "/commands",
handler: embeddedCLIHandler,
unauthenticated: true,
noModelUUID: true,
}, {
// Serve the API at / for backward compatibility. Note that the
// pat muxer special-cases / so that it does not serve all
// possible endpoints, but only / itself.
pattern: "/",
handler: mainAPIHandler,
tracked: true,
unauthenticated: true,
noModelUUID: true,
}, {
pattern: "/health",
methods: []string{"GET"},
handler: healthHandler,
unauthenticated: true,
noModelUUID: true,
}, {
pattern: "/register",
handler: registerHandler,
unauthenticated: true,
}, {
pattern: "/tools",
handler: modelToolsUploadHandler,
authorizer: modelToolsUploadAuthorizer,
}, {
pattern: "/tools/:version",
handler: modelToolsDownloadHandler,
unauthenticated: true,
}, {
pattern: "/log",
handler: debugLogHandler,
tracked: true,
// The authentication is handled within the debugLogHandler in order
// for discharge required errors to be handled correctly.
unauthenticated: true,
}, {
// GET /charms has no authorizer
pattern: "/charms",
methods: []string{"GET"},
handler: modelCharmsHTTPHandler,
}, {
pattern: "/charms",
methods: []string{"POST"},
handler: modelCharmsHTTPHandler,
authorizer: modelCharmsUploadAuthorizer,