Skip to content

Commit a6b61ed

Browse files
authored
option api.server.disable_usage_metrics_export (#4021)
1 parent 24931d7 commit a6b61ed

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

pkg/apiserver/apiserver.go

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ import (
3434
const keyLength = 32
3535

3636
type APIServer struct {
37-
URL string
38-
UnixSocket string
39-
TLS *csconfig.TLSCfg
37+
cfg *csconfig.LocalApiServerCfg
4038
dbClient *database.Client
4139
controller *controllers.Controller
4240
flushScheduler *gocron.Scheduler
@@ -281,9 +279,7 @@ func NewServer(ctx context.Context, config *csconfig.LocalApiServerCfg) (*APISer
281279
controller.TrustedIPs = trustedIPs
282280

283281
return &APIServer{
284-
URL: config.ListenURI,
285-
UnixSocket: config.ListenSocket,
286-
TLS: config.TLS,
282+
cfg: config,
287283
dbClient: dbClient,
288284
controller: controller,
289285
flushScheduler: flushScheduler,
@@ -357,22 +353,24 @@ func (s *APIServer) initAPIC(ctx context.Context) {
357353
return nil
358354
})
359355

360-
s.apic.metricsTomb.Go(func() error {
361-
s.apic.SendUsageMetrics(ctx)
362-
return nil
363-
})
356+
if !s.cfg.DisableUsageMetricsExport {
357+
s.apic.metricsTomb.Go(func() error {
358+
s.apic.SendUsageMetrics(ctx)
359+
return nil
360+
})
361+
}
364362
}
365363

366364
func (s *APIServer) Run(ctx context.Context, apiReady chan bool) error {
367365
defer trace.CatchPanic("lapi/runServer")
368366

369-
tlsCfg, err := s.TLS.GetTLSConfig()
367+
tlsCfg, err := s.cfg.TLS.GetTLSConfig()
370368
if err != nil {
371369
return fmt.Errorf("while creating TLS config: %w", err)
372370
}
373371

374372
s.httpServer = &http.Server{
375-
Addr: s.URL,
373+
Addr: s.cfg.ListenURI,
376374
Handler: s.router,
377375
TLSConfig: tlsCfg,
378376
Protocols: &http.Protocols{},
@@ -408,18 +406,18 @@ func (s *APIServer) listenAndServeLAPI(ctx context.Context, apiReady chan bool)
408406
startServer := func(listener net.Listener, canTLS bool) {
409407
var err error
410408

411-
if canTLS && s.TLS != nil && (s.TLS.CertFilePath != "" || s.TLS.KeyFilePath != "") {
412-
if s.TLS.KeyFilePath == "" {
409+
if canTLS && s.cfg.TLS != nil && (s.cfg.TLS.CertFilePath != "" || s.cfg.TLS.KeyFilePath != "") {
410+
if s.cfg.TLS.KeyFilePath == "" {
413411
serverError <- errors.New("missing TLS key file")
414412
return
415413
}
416414

417-
if s.TLS.CertFilePath == "" {
415+
if s.cfg.TLS.CertFilePath == "" {
418416
serverError <- errors.New("missing TLS cert file")
419417
return
420418
}
421419

422-
err = s.httpServer.ServeTLS(listener, s.TLS.CertFilePath, s.TLS.KeyFilePath)
420+
err = s.httpServer.ServeTLS(listener, s.cfg.TLS.CertFilePath, s.cfg.TLS.KeyFilePath)
423421
} else {
424422
err = s.httpServer.Serve(listener)
425423
}
@@ -446,7 +444,7 @@ func (s *APIServer) listenAndServeLAPI(ctx context.Context, apiReady chan bool)
446444

447445
log.Infof("CrowdSec Local API listening on %s", url)
448446
startServer(listener, true)
449-
}(s.URL)
447+
}(s.cfg.ListenURI)
450448

451449
// Starting Unix socket listener
452450
go func(socket string) {
@@ -468,7 +466,7 @@ func (s *APIServer) listenAndServeLAPI(ctx context.Context, apiReady chan bool)
468466

469467
log.Infof("CrowdSec Local API listening on Unix socket %s", socket)
470468
startServer(listener, false)
471-
}(s.UnixSocket)
469+
}(s.cfg.ListenSocket)
472470

473471
apiReady <- true
474472

@@ -485,10 +483,10 @@ func (s *APIServer) listenAndServeLAPI(ctx context.Context, apiReady chan bool)
485483
log.Errorf("while shutting down http server: %v", err)
486484
}
487485

488-
if s.UnixSocket != "" {
489-
if err := os.Remove(s.UnixSocket); err != nil {
486+
if s.cfg.ListenSocket != "" {
487+
if err := os.Remove(s.cfg.ListenSocket); err != nil {
490488
if !errors.Is(err, fs.ErrNotExist) {
491-
log.Errorf("can't remove socket %s: %s", s.UnixSocket, err)
489+
log.Errorf("can't remove socket %s: %s", s.cfg.ListenSocket, err)
492490
}
493491
}
494492
}
@@ -555,7 +553,7 @@ func hasPlugins(profiles []*csconfig.ProfileCfg) bool {
555553
}
556554

557555
func (s *APIServer) InitPlugins(ctx context.Context, cConfig *csconfig.Config, pluginBroker *csplugin.PluginBroker) error {
558-
if hasPlugins(cConfig.API.Server.Profiles) {
556+
if hasPlugins(s.cfg.Profiles) {
559557
log.Info("initiating plugin broker")
560558
// On windows, the plugins are always run as medium-integrity processes, so we don't care about plugin_config
561559
if cConfig.PluginConfig == nil && runtime.GOOS != "windows" {
@@ -566,7 +564,7 @@ func (s *APIServer) InitPlugins(ctx context.Context, cConfig *csconfig.Config, p
566564
return errors.New("plugins are enabled, but config_paths.plugin_dir is not defined")
567565
}
568566

569-
err := pluginBroker.Init(ctx, cConfig.PluginConfig, cConfig.API.Server.Profiles, cConfig.ConfigPaths)
567+
err := pluginBroker.Init(ctx, cConfig.PluginConfig, s.cfg.Profiles, cConfig.ConfigPaths)
570568
if err != nil {
571569
return fmt.Errorf("plugin broker: %w", err)
572570
}
@@ -584,18 +582,18 @@ func (s *APIServer) InitController() error {
584582
return fmt.Errorf("controller init: %w", err)
585583
}
586584

587-
if s.TLS == nil {
585+
if s.cfg.TLS == nil {
588586
return nil
589587
}
590588

591589
// TLS is configured: create the TLSAuth middleware for agents and bouncers
592590

593591
cacheExpiration := time.Hour
594-
if s.TLS.CacheExpiration != nil {
595-
cacheExpiration = *s.TLS.CacheExpiration
592+
if s.cfg.TLS.CacheExpiration != nil {
593+
cacheExpiration = *s.cfg.TLS.CacheExpiration
596594
}
597595

598-
s.controller.HandlerV1.Middlewares.JWT.TlsAuth, err = v1.NewTLSAuth(s.TLS.AllowedAgentsOU, s.TLS.CRLPath,
596+
s.controller.HandlerV1.Middlewares.JWT.TlsAuth, err = v1.NewTLSAuth(s.cfg.TLS.AllowedAgentsOU, s.cfg.TLS.CRLPath,
599597
cacheExpiration,
600598
log.WithFields(log.Fields{
601599
"component": "tls-auth",
@@ -605,7 +603,7 @@ func (s *APIServer) InitController() error {
605603
return fmt.Errorf("while creating TLS auth for agents: %w", err)
606604
}
607605

608-
s.controller.HandlerV1.Middlewares.APIKey.TlsAuth, err = v1.NewTLSAuth(s.TLS.AllowedBouncersOU, s.TLS.CRLPath,
606+
s.controller.HandlerV1.Middlewares.APIKey.TlsAuth, err = v1.NewTLSAuth(s.cfg.TLS.AllowedBouncersOU, s.cfg.TLS.CRLPath,
609607
cacheExpiration,
610608
log.WithFields(log.Fields{
611609
"component": "tls-auth",

pkg/csconfig/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ type LocalApiServerCfg struct {
247247
CapiWhitelistsPath string `yaml:"capi_whitelists_path,omitempty"`
248248
CapiWhitelists *CapiWhitelist `yaml:"-"`
249249
AutoRegister *LocalAPIAutoRegisterCfg `yaml:"auto_registration,omitempty"`
250+
DisableUsageMetricsExport bool `yaml:"disable_usage_metrics_export"`
250251
}
251252

252253
func (c *LocalApiServerCfg) GetTrustedIPs() ([]net.IPNet, error) {

0 commit comments

Comments
 (0)