2023-11-24 14:57:32 +00:00
|
|
|
package cwhub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2024-03-08 09:55:30 +00:00
|
|
|
"errors"
|
2023-11-24 14:57:32 +00:00
|
|
|
"fmt"
|
2023-12-19 16:20:09 +00:00
|
|
|
"io"
|
2023-11-24 14:57:32 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2024-01-11 09:28:58 +00:00
|
|
|
"slices"
|
2023-11-24 14:57:32 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Hub is the main structure for the package.
|
|
|
|
type Hub struct {
|
2023-12-01 08:36:38 +00:00
|
|
|
items HubItems // Items read from HubDir and InstallDir
|
2023-11-24 14:57:32 +00:00
|
|
|
local *csconfig.LocalHubCfg
|
|
|
|
remote *RemoteHubCfg
|
|
|
|
Warnings []string // Warnings encountered during sync
|
2023-12-19 16:20:09 +00:00
|
|
|
logger *logrus.Logger
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDataDir returns the data directory, where data sets are installed.
|
|
|
|
func (h *Hub) GetDataDir() string {
|
|
|
|
return h.local.InstallDataDir
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewHub returns a new Hub instance with local and (optionally) remote configuration, and syncs the local state.
|
|
|
|
// If updateIndex is true, the local index file is updated from the remote before reading the state of the items.
|
|
|
|
// All download operations (including updateIndex) return ErrNilRemoteHub if the remote configuration is not set.
|
2023-12-19 16:20:09 +00:00
|
|
|
func NewHub(local *csconfig.LocalHubCfg, remote *RemoteHubCfg, updateIndex bool, logger *logrus.Logger) (*Hub, error) {
|
2023-11-24 14:57:32 +00:00
|
|
|
if local == nil {
|
2024-03-08 09:55:30 +00:00
|
|
|
return nil, errors.New("no hub configuration found")
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
if logger == nil {
|
|
|
|
logger = logrus.New()
|
|
|
|
logger.SetOutput(io.Discard)
|
|
|
|
}
|
|
|
|
|
2023-11-24 14:57:32 +00:00
|
|
|
hub := &Hub{
|
|
|
|
local: local,
|
|
|
|
remote: remote,
|
2023-12-19 16:20:09 +00:00
|
|
|
logger: logger,
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if updateIndex {
|
|
|
|
if err := hub.updateIndex(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
logger.Debugf("loading hub idx %s", local.HubIndexFile)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
if err := hub.parseIndex(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to load index: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := hub.localSync(); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to sync items: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return hub, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseIndex takes the content of an index file and fills the map of associated parsers/scenarios/collections.
|
|
|
|
func (h *Hub) parseIndex() error {
|
|
|
|
bidx, err := os.ReadFile(h.local.HubIndexFile)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to read index file: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
if err := json.Unmarshal(bidx, &h.items); err != nil {
|
2023-11-24 14:57:32 +00:00
|
|
|
return fmt.Errorf("failed to unmarshal index: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Debugf("%d item types in hub index", len(ItemTypes))
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
// Iterate over the different types to complete the struct
|
|
|
|
for _, itemType := range ItemTypes {
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Tracef("%s: %d items", itemType, len(h.GetItemMap(itemType)))
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
for name, item := range h.GetItemMap(itemType) {
|
2023-11-24 14:57:32 +00:00
|
|
|
item.hub = h
|
|
|
|
item.Name = name
|
|
|
|
|
|
|
|
// if the item has no (redundant) author, take it from the json key
|
|
|
|
if item.Author == "" && strings.Contains(name, "/") {
|
|
|
|
item.Author = strings.Split(name, "/")[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
item.Type = itemType
|
|
|
|
item.FileName = path.Base(item.RemotePath)
|
|
|
|
|
|
|
|
item.logMissingSubItems()
|
2024-01-11 09:28:58 +00:00
|
|
|
|
|
|
|
if item.latestHash() == "" {
|
|
|
|
h.logger.Errorf("invalid hub item %s: latest version missing from index", item.FQName())
|
|
|
|
}
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ItemStats returns total counts of the hub items, including local and tainted.
|
|
|
|
func (h *Hub) ItemStats() []string {
|
|
|
|
loaded := ""
|
|
|
|
local := 0
|
|
|
|
tainted := 0
|
|
|
|
|
|
|
|
for _, itemType := range ItemTypes {
|
2023-12-01 08:36:38 +00:00
|
|
|
if len(h.GetItemMap(itemType)) == 0 {
|
2023-11-24 14:57:32 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
loaded += fmt.Sprintf("%d %s, ", len(h.GetItemMap(itemType)), itemType)
|
2023-11-24 14:57:32 +00:00
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
for _, item := range h.GetItemMap(itemType) {
|
2023-12-05 12:38:52 +00:00
|
|
|
if item.State.IsLocal() {
|
2023-11-24 14:57:32 +00:00
|
|
|
local++
|
|
|
|
}
|
|
|
|
|
|
|
|
if item.State.Tainted {
|
|
|
|
tainted++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loaded = strings.Trim(loaded, ", ")
|
|
|
|
if loaded == "" {
|
|
|
|
loaded = "0 items"
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := []string{
|
|
|
|
fmt.Sprintf("Loaded: %s", loaded),
|
|
|
|
}
|
|
|
|
|
|
|
|
if local > 0 || tainted > 0 {
|
|
|
|
ret = append(ret, fmt.Sprintf("Unmanaged items: %d local, %d tainted", local, tainted))
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateIndex downloads the latest version of the index and writes it to disk if it changed.
|
|
|
|
func (h *Hub) updateIndex() error {
|
|
|
|
body, err := h.remote.fetchIndex()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
oldContent, err := os.ReadFile(h.local.HubIndexFile)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Warningf("failed to read hub index: %s", err)
|
2023-11-24 14:57:32 +00:00
|
|
|
}
|
|
|
|
} else if bytes.Equal(body, oldContent) {
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Info("hub index is up to date")
|
2023-11-24 14:57:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.WriteFile(h.local.HubIndexFile, body, 0o644); err != nil {
|
|
|
|
return fmt.Errorf("failed to write hub index: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
h.logger.Infof("Wrote index to %s, %d bytes", h.local.HubIndexFile, len(body))
|
2023-11-24 14:57:32 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-11-28 22:51:51 +00:00
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
func (h *Hub) addItem(item *Item) {
|
|
|
|
if h.items[item.Type] == nil {
|
|
|
|
h.items[item.Type] = make(map[string]*Item)
|
|
|
|
}
|
|
|
|
|
|
|
|
h.items[item.Type][item.Name] = item
|
|
|
|
}
|
|
|
|
|
2023-11-28 22:51:51 +00:00
|
|
|
// GetItemMap returns the map of items for a given type.
|
|
|
|
func (h *Hub) GetItemMap(itemType string) map[string]*Item {
|
2023-12-01 08:36:38 +00:00
|
|
|
return h.items[itemType]
|
2023-11-28 22:51:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetItem returns an item from hub based on its type and full name (author/name).
|
|
|
|
func (h *Hub) GetItem(itemType string, itemName string) *Item {
|
|
|
|
return h.GetItemMap(itemType)[itemName]
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
// GetItemFQ returns an item from hub based on its type and name (type:author/name).
|
|
|
|
func (h *Hub) GetItemFQ(itemFQName string) (*Item, error) {
|
|
|
|
// type and name are separated by a colon
|
|
|
|
parts := strings.Split(itemFQName, ":")
|
|
|
|
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return nil, fmt.Errorf("invalid item name %s", itemFQName)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := h.GetItemMap(parts[0])
|
|
|
|
if m == nil {
|
|
|
|
return nil, fmt.Errorf("invalid item type %s", parts[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
i := m[parts[1]]
|
|
|
|
if i == nil {
|
|
|
|
return nil, fmt.Errorf("item %s not found", parts[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
return i, nil
|
|
|
|
}
|
|
|
|
|
2024-04-24 09:09:37 +00:00
|
|
|
// GetNamesByType returns a slice of (full) item names for a given type
|
2023-11-28 22:51:51 +00:00
|
|
|
// (eg. for collections: crowdsecurity/apache2 crowdsecurity/nginx).
|
2024-04-24 09:09:37 +00:00
|
|
|
func (h *Hub) GetNamesByType(itemType string) []string {
|
2023-11-28 22:51:51 +00:00
|
|
|
m := h.GetItemMap(itemType)
|
|
|
|
if m == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
names := make([]string, 0, len(m))
|
|
|
|
for k := range m {
|
|
|
|
names = append(names, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
return names
|
|
|
|
}
|
|
|
|
|
2024-04-24 09:09:37 +00:00
|
|
|
// GetItemsByType returns a slice of all the items of a given type, installed or not.
|
|
|
|
func (h *Hub) GetItemsByType(itemType string) ([]*Item, error) {
|
2023-12-01 08:36:38 +00:00
|
|
|
if !slices.Contains(ItemTypes, itemType) {
|
|
|
|
return nil, fmt.Errorf("invalid item type %s", itemType)
|
2023-11-28 22:51:51 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
items := h.items[itemType]
|
|
|
|
|
2023-11-28 22:51:51 +00:00
|
|
|
ret := make([]*Item, len(items))
|
|
|
|
|
|
|
|
idx := 0
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
ret[idx] = item
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2024-04-24 09:09:37 +00:00
|
|
|
// GetInstalledItemsByType returns a slice of the installed items of a given type.
|
|
|
|
func (h *Hub) GetInstalledItemsByType(itemType string) ([]*Item, error) {
|
2023-12-01 08:36:38 +00:00
|
|
|
if !slices.Contains(ItemTypes, itemType) {
|
|
|
|
return nil, fmt.Errorf("invalid item type %s", itemType)
|
2023-11-28 22:51:51 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 08:36:38 +00:00
|
|
|
items := h.items[itemType]
|
|
|
|
|
2023-11-28 22:51:51 +00:00
|
|
|
retItems := make([]*Item, 0)
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
if item.State.Installed {
|
|
|
|
retItems = append(retItems, item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retItems, nil
|
|
|
|
}
|
|
|
|
|
2024-04-24 09:09:37 +00:00
|
|
|
// GetInstalledNamesByType returns the names of the installed items of a given type.
|
|
|
|
func (h *Hub) GetInstalledNamesByType(itemType string) ([]string, error) {
|
|
|
|
items, err := h.GetInstalledItemsByType(itemType)
|
2023-11-28 22:51:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
retStr := make([]string, len(items))
|
|
|
|
|
|
|
|
for idx, it := range items {
|
|
|
|
retStr[idx] = it.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
return retStr, nil
|
|
|
|
}
|