Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cscli hub update: option --with-content to keep embedded items in index; use it in docker #3195

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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) 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 @@
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 @@
import (
"context"
"fmt"
"net/url"

"github.com/sirupsen/logrus"

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

// 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 @@
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 @@
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