Skip to content

Commit

Permalink
cscli hub update: option --with-content to keep embedded items in ind…
Browse files Browse the repository at this point in the history
…ex; use it in docker
  • Loading branch information
mmetc committed Aug 27, 2024
1 parent c4431b6 commit 99cb522
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ RUN make clean release DOCKER_BUILD=1 BUILD_STATIC=1 CGO_CFLAGS="-D_LARGEFILE64_
cd crowdsec-v* && \
./wizard.sh --docker-mode && \
cd - >/dev/null && \
cscli hub update && \
./docker/preload-hub-items && \
cscli hub update --with-content && \
cscli collections install crowdsecurity/linux && \
cscli parsers install crowdsecurity/whitelists

Expand Down
3 changes: 1 addition & 2 deletions Dockerfile.debian
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ RUN make clean release DOCKER_BUILD=1 BUILD_STATIC=1 && \
cd crowdsec-v* && \
./wizard.sh --docker-mode && \
cd - >/dev/null && \
cscli hub update && \
./docker/preload-hub-items && \
cscli hub update --with-content && \
cscli collections install crowdsecurity/linux && \
cscli parsers install crowdsecurity/whitelists

Expand Down
10 changes: 8 additions & 2 deletions cmd/crowdsec-cli/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ func (cli *cliHub) newListCmd() *cobra.Command {
return cmd
}

func (cli *cliHub) update(ctx context.Context) error {
func (cli *cliHub) update(ctx context.Context, withContent bool) error {
local := cli.cfg().Hub
remote := require.RemoteHub(ctx, cli.cfg())
remote.EmbedItemContent = withContent

// don't use require.Hub because if there is no index file, it would fail
hub, err := cwhub.NewHub(local, remote, log.StandardLogger())
Expand All @@ -125,6 +126,8 @@ func (cli *cliHub) update(ctx context.Context) error {
}

func (cli *cliHub) newUpdateCmd() *cobra.Command {
withContent := false

Check warning on line 130 in cmd/crowdsec-cli/hub.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hub.go#L130

Added line #L130 was not covered by tests
cmd := &cobra.Command{
Use: "update",
Short: "Download the latest index (catalog of available configurations)",
Expand All @@ -134,10 +137,13 @@ Fetches the .index.json file from the hub, containing the list of available conf
Args: cobra.ExactArgs(0),
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, _ []string) error {
return cli.update(cmd.Context())
return cli.update(cmd.Context(), withContent)
},
}

flags := cmd.Flags()
flags.BoolVar(&withContent, "with-content", false, "Download index with embedded item content")

Check warning on line 146 in cmd/crowdsec-cli/hub.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/hub.go#L146

Added line #L146 was not covered by tests
return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion docker/docker_start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ run_hub_update() {
index_modification_time=$(stat -c %Y /etc/crowdsec/hub/.index.json 2>/dev/null)
# Run cscli hub update if no date or if the index file is older than 24h
if [ -z "$index_modification_time" ] || [ $(( $(date +%s) - index_modification_time )) -gt 86400 ]; then
cscli hub update
cscli hub update --with-content
else
echo "Skipping hub update, index file is recent"
fi
Expand Down
33 changes: 30 additions & 3 deletions pkg/cwhub/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cwhub
import (
"context"
"fmt"
"net/url"

"github.com/sirupsen/logrus"

Expand All @@ -11,9 +12,10 @@ import (

// RemoteHubCfg is used to retrieve index and items from the remote hub.
type RemoteHubCfg struct {
Branch string
URLTemplate string
IndexPath string
Branch string
URLTemplate string
IndexPath string
EmbedItemContent bool
}

// urlTo builds the URL to download a file from the remote hub.
Expand All @@ -30,6 +32,24 @@ func (r *RemoteHubCfg) urlTo(remotePath string) (string, error) {
return fmt.Sprintf(r.URLTemplate, r.Branch, remotePath), nil
}

// addURLParam adds the "with_content=true" parameter to the URL if it's not already present.
func addURLParam(rawURL string, param string, value string) (string, error) {
parsedURL, err := url.Parse(rawURL)
if err != nil {
return "", fmt.Errorf("failed to parse URL: %w", err)
}

Check warning on line 40 in pkg/cwhub/remote.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/remote.go#L39-L40

Added lines #L39 - L40 were not covered by tests

query := parsedURL.Query()

Check warning on line 43 in pkg/cwhub/remote.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/remote.go#L43

Added line #L43 was not covered by tests
if _, exists := query[param]; !exists {
query.Add(param, value)
}

Check warning on line 46 in pkg/cwhub/remote.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/remote.go#L46

Added line #L46 was not covered by tests

parsedURL.RawQuery = query.Encode()

Check warning on line 49 in pkg/cwhub/remote.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/remote.go#L49

Added line #L49 was not covered by tests
return parsedURL.String(), nil
}

// fetchIndex downloads the index from the hub and returns the content.
func (r *RemoteHubCfg) fetchIndex(ctx context.Context, destPath string) (bool, error) {
if r == nil {
Expand All @@ -41,6 +61,13 @@ func (r *RemoteHubCfg) fetchIndex(ctx context.Context, destPath string) (bool, e
return false, fmt.Errorf("failed to build hub index request: %w", err)
}

if r.EmbedItemContent {
url, err = addURLParam(url, "with_content", "true")
if err != nil {
return false, fmt.Errorf("failed to add 'with_content' parameter to URL: %w", err)
}

Check warning on line 68 in pkg/cwhub/remote.go

View check run for this annotation

Codecov / codecov/patch

pkg/cwhub/remote.go#L67-L68

Added lines #L67 - L68 were not covered by tests
}

downloaded, err := downloader.
New().
WithHTTPClient(hubClient).
Expand Down
2 changes: 1 addition & 1 deletion test/lib/config/config-local
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ make_init_data() {
./instance-db config-yaml
./instance-db setup

"$CSCLI" --warning hub update
"$CSCLI" --warning hub update --with-content

# preload some content and data files
"$CSCLI" collections install crowdsecurity/linux --download-only
Expand Down

0 comments on commit 99cb522

Please sign in to comment.