github.com/crowdsecurity/crowdsec@v1.6.1/pkg/apiserver/controllers/v1/controller.go (about)

     1  package v1
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net"
     7  
     8  	middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1"
     9  	"github.com/crowdsecurity/crowdsec/pkg/csconfig"
    10  	"github.com/crowdsecurity/crowdsec/pkg/csplugin"
    11  	"github.com/crowdsecurity/crowdsec/pkg/csprofiles"
    12  	"github.com/crowdsecurity/crowdsec/pkg/database"
    13  	"github.com/crowdsecurity/crowdsec/pkg/models"
    14  )
    15  
    16  type Controller struct {
    17  	Ectx         context.Context
    18  	DBClient     *database.Client
    19  	APIKeyHeader string
    20  	Middlewares  *middlewares.Middlewares
    21  	Profiles     []*csprofiles.Runtime
    22  
    23  	AlertsAddChan      chan []*models.Alert
    24  	DecisionDeleteChan chan []*models.Decision
    25  
    26  	PluginChannel chan csplugin.ProfileAlert
    27  	ConsoleConfig csconfig.ConsoleConfig
    28  	TrustedIPs    []net.IPNet
    29  }
    30  
    31  type ControllerV1Config struct {
    32  	DbClient    *database.Client
    33  	Ctx         context.Context
    34  	ProfilesCfg []*csconfig.ProfileCfg
    35  
    36  	AlertsAddChan      chan []*models.Alert
    37  	DecisionDeleteChan chan []*models.Decision
    38  
    39  	PluginChannel chan csplugin.ProfileAlert
    40  	ConsoleConfig csconfig.ConsoleConfig
    41  	TrustedIPs    []net.IPNet
    42  }
    43  
    44  func New(cfg *ControllerV1Config) (*Controller, error) {
    45  	var err error
    46  
    47  	profiles, err := csprofiles.NewProfile(cfg.ProfilesCfg)
    48  	if err != nil {
    49  		return &Controller{}, fmt.Errorf("failed to compile profiles: %w", err)
    50  	}
    51  
    52  	v1 := &Controller{
    53  		Ectx:               cfg.Ctx,
    54  		DBClient:           cfg.DbClient,
    55  		APIKeyHeader:       middlewares.APIKeyHeader,
    56  		Profiles:           profiles,
    57  		AlertsAddChan:      cfg.AlertsAddChan,
    58  		DecisionDeleteChan: cfg.DecisionDeleteChan,
    59  		PluginChannel:      cfg.PluginChannel,
    60  		ConsoleConfig:      cfg.ConsoleConfig,
    61  		TrustedIPs:         cfg.TrustedIPs,
    62  	}
    63  	v1.Middlewares, err = middlewares.NewMiddlewares(cfg.DbClient)
    64  
    65  	if err != nil {
    66  		return v1, err
    67  	}
    68  
    69  	return v1, nil
    70  }