github.com/crowdsecurity/crowdsec@v1.6.1/cmd/crowdsec-cli/require/require.go (about)

     1  package require
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/sirupsen/logrus"
     8  
     9  	"github.com/crowdsecurity/crowdsec/pkg/csconfig"
    10  	"github.com/crowdsecurity/crowdsec/pkg/cwhub"
    11  )
    12  
    13  func LAPI(c *csconfig.Config) error {
    14  	if err := c.LoadAPIServer(true); err != nil {
    15  		return fmt.Errorf("failed to load Local API: %w", err)
    16  	}
    17  
    18  	if c.DisableAPI {
    19  		return fmt.Errorf("local API is disabled -- this command must be run on the local API machine")
    20  	}
    21  
    22  	return nil
    23  }
    24  
    25  func CAPI(c *csconfig.Config) error {
    26  	if c.API.Server.OnlineClient == nil {
    27  		return fmt.Errorf("no configuration for Central API (CAPI) in '%s'", *c.FilePath)
    28  	}
    29  
    30  	return nil
    31  }
    32  
    33  func PAPI(c *csconfig.Config) error {
    34  	if c.API.Server.OnlineClient.Credentials.PapiURL == "" {
    35  		return fmt.Errorf("no PAPI URL in configuration")
    36  	}
    37  
    38  	return nil
    39  }
    40  
    41  func CAPIRegistered(c *csconfig.Config) error {
    42  	if c.API.Server.OnlineClient.Credentials == nil {
    43  		return fmt.Errorf("the Central API (CAPI) must be configured with 'cscli capi register'")
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func DB(c *csconfig.Config) error {
    50  	if err := c.LoadDBConfig(true); err != nil {
    51  		return fmt.Errorf("this command requires direct database access (must be run on the local API machine): %w", err)
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  func Notifications(c *csconfig.Config) error {
    58  	if c.ConfigPaths.NotificationDir == "" {
    59  		return fmt.Errorf("config_paths.notification_dir is not set in crowdsec config")
    60  	}
    61  
    62  	return nil
    63  }
    64  
    65  // RemoteHub returns the configuration required to download hub index and items: url, branch, etc.
    66  func RemoteHub(c *csconfig.Config) *cwhub.RemoteHubCfg {
    67  	// set branch in config, and log if necessary
    68  	branch := HubBranch(c)
    69  	urlTemplate := HubURLTemplate(c)
    70  	remote := &cwhub.RemoteHubCfg{
    71  		Branch:      branch,
    72  		URLTemplate: urlTemplate,
    73  		IndexPath: ".index.json",
    74  	}
    75  
    76  	return remote
    77  }
    78  
    79  // Hub initializes the hub. If a remote configuration is provided, it can be used to download the index and items.
    80  // If no remote parameter is provided, the hub can only be used for local operations.
    81  func Hub(c *csconfig.Config, remote *cwhub.RemoteHubCfg, logger *logrus.Logger) (*cwhub.Hub, error) {
    82  	local := c.Hub
    83  
    84  	if local == nil {
    85  		return nil, fmt.Errorf("you must configure cli before interacting with hub")
    86  	}
    87  
    88  	if logger == nil {
    89  		logger = logrus.New()
    90  		logger.SetOutput(io.Discard)
    91  	}
    92  
    93  	hub, err := cwhub.NewHub(local, remote, false, logger)
    94  	if err != nil {
    95  		return nil, fmt.Errorf("failed to read Hub index: %w. Run 'sudo cscli hub update' to download the index again", err)
    96  	}
    97  
    98  	return hub, nil
    99  }