mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-10 20:05:55 +02:00
Some checks are pending
Tests / sqlite (push) Waiting to run
Tests / mariadb (push) Waiting to run
Tests / mysql (push) Waiting to run
Tests / postgres (push) Waiting to run
Tests / hub (push) Waiting to run
Release Drafter / update_release_draft (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Test Docker images / test_flavor (debian) (push) Waiting to run
Test Docker images / test_flavor (slim) (push) Waiting to run
Go tests (windows) / Build + tests (push) Waiting to run
Build / Build + tests (push) Waiting to run
(push-master) Publish latest Docker images / dev-alpine (push) Waiting to run
(push-master) Publish latest Docker images / dev-debian (push) Waiting to run
113 lines
2.5 KiB
Go
113 lines
2.5 KiB
Go
package require
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
|
)
|
|
|
|
func LAPI(c *csconfig.Config) error {
|
|
if err := c.LoadAPIServer(true); err != nil {
|
|
return fmt.Errorf("failed to load Local API: %w", err)
|
|
}
|
|
|
|
if c.DisableAPI {
|
|
return errors.New("local API is disabled -- this command must be run on the local API machine")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func CAPI(c *csconfig.Config) error {
|
|
if c.API.Server.OnlineClient == nil {
|
|
return fmt.Errorf("no configuration for Central API (CAPI) in '%s'", c.FilePath)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func PAPI(c *csconfig.Config) error {
|
|
if err := CAPI(c); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := CAPIRegistered(c); err != nil {
|
|
return err
|
|
}
|
|
|
|
if c.API.Server.OnlineClient.Credentials.PapiURL == "" {
|
|
return errors.New("no PAPI URL in configuration")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func CAPIRegistered(c *csconfig.Config) error {
|
|
if c.API.Server.OnlineClient.Credentials == nil {
|
|
return errors.New("the Central API (CAPI) must be configured with 'cscli capi register'")
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func DBClient(ctx context.Context, dbcfg *csconfig.DatabaseCfg) (*database.Client, error) {
|
|
db, err := database.NewClient(ctx, dbcfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to connect to database: %w", err)
|
|
}
|
|
|
|
return db, nil
|
|
}
|
|
|
|
func DB(c *csconfig.Config) error {
|
|
if err := c.LoadDBConfig(true); err != nil {
|
|
return fmt.Errorf("this command requires direct database access (must be run on the local API machine): %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func HubDownloader(ctx context.Context, c *csconfig.Config) *cwhub.Downloader {
|
|
// set branch in config, and log if necessary
|
|
branch := HubBranch(ctx, c)
|
|
urlTemplate := HubURLTemplate(c)
|
|
remote := &cwhub.Downloader{
|
|
Branch: branch,
|
|
URLTemplate: urlTemplate,
|
|
}
|
|
|
|
return remote
|
|
}
|
|
|
|
// Hub initializes the hub. If a remote configuration is provided, it can be used to download the index and items.
|
|
// If no remote parameter is provided, the hub can only be used for local operations.
|
|
func Hub(c *csconfig.Config, logger *logrus.Logger) (*cwhub.Hub, error) {
|
|
local := c.Hub
|
|
|
|
if local == nil {
|
|
return nil, errors.New("you must configure cli before interacting with hub")
|
|
}
|
|
|
|
if logger == nil {
|
|
logger = logrus.New()
|
|
logger.SetOutput(io.Discard)
|
|
}
|
|
|
|
hub, err := cwhub.NewHub(local, logger)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := hub.Load(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return hub, nil
|
|
}
|