make cscli use crowdsec version for hub (#194)

This commit is contained in:
AlteredCoder 2020-09-01 14:32:45 +02:00 committed by GitHub
parent b81c735d81
commit b7286d6a85
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 89 additions and 7 deletions

View file

@ -18,8 +18,10 @@ jobs:
id: go
- name: Check out code into the Go module directory
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Build release
run: BUILD_VERSION=xxx make release
run: make release
# - name: Cache release
# uses: actions/cache@v2
# id: cache
@ -31,21 +33,26 @@ jobs:
git clone https://github.com/crowdsecurity/hub-tests.git
cd hub-tests
make
- uses: oprypin/find-latest-tag@v1
with:
repository: crowdsecurity/crowdsec # The repository to scan.
releases-only: false # We know that all relevant tags have a GitHub release for them.
id: crowdsec # The step ID to refer to later.
- name: Create crowdsec test env with all parsers from the release
run: |
cd crowdsec-xxx
cd crowdsec-${{ steps.crowdsec.outputs.tag }}
./test_env.sh
cd tests
for i in `./cscli -c dev.yaml list parsers -a -o json | jq -r ".[].name" ` ; do
./cscli -c dev.yaml install parser $i ;
done
- name: Setup hub ci in crowdsec
working-directory: ./crowdsec-xxx/tests/
working-directory: ./crowdsec-${{ steps.crowdsec.outputs.tag }}/tests/
run: |
cp -R ../../hub-tests/tests .
cp ../../hub-tests/main .
- name: Run the HUB CI
working-directory: ./crowdsec-xxx/tests/
working-directory: ./crowdsec-${{ steps.crowdsec.outputs.tag }}/tests/
run: |
for i in `find ./tests -mindepth 1 -maxdepth 1 -type d` ; do
echo "::group::Test-${i}" ;

5
.gitignore vendored
View file

@ -13,3 +13,8 @@
# Dependency directories (remove the comment below to include it)
# vendor/
# crowdsec binaries
cmd/crowdsec-cli/cscli
cmd/crowdsec/crowdsec

View file

@ -17,7 +17,7 @@ REQUIRE_GOVERSION="1.13"
#Current versioning information from env
BUILD_VERSION?="$(shell git for-each-ref --sort=-v:refname --count=1 --format '%(refname)' | cut -d '/' -f3)"
BUILD_VERSION?="$(shell git describe --tags `git rev-list --tags --max-count=1`)"
BUILD_GOVERSION="$(shell go version | cut -d " " -f3 | sed -r 's/[go]+//g')"
BUILD_CODENAME=$(shell cat RELEASE.json | jq -r .CodeName)
BUILD_TIMESTAMP=$(shell date +%F"_"%T)

View file

@ -63,6 +63,10 @@ you should [update cscli](./cscli_update.md).
if !config.configured {
return fmt.Errorf("you must configure cli before interacting with hub")
}
if err := setHubBranch(); err != nil {
return fmt.Errorf("error while setting hub branch: %s", err)
}
return nil
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {

View file

@ -72,6 +72,7 @@ cscli list -a # List all local and remote configurations
if !config.configured {
return fmt.Errorf("you must configure cli before interacting with hub")
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {

View file

@ -122,7 +122,8 @@ API interaction:
rootCmd.PersistentFlags().BoolVar(&nfo_lvl, "info", false, "Set logging to info.")
rootCmd.PersistentFlags().BoolVar(&wrn_lvl, "warning", false, "Set logging to warning.")
rootCmd.PersistentFlags().BoolVar(&err_lvl, "error", false, "Set logging to error.")
rootCmd.PersistentFlags().StringVar(&cwhub.HubBranch, "branch", "master", "Override hub branch on github")
rootCmd.PersistentFlags().StringVar(&cwhub.HubBranch, "branch", "", "Override hub branch on github")
if err := rootCmd.PersistentFlags().MarkHidden("branch"); err != nil {
log.Fatalf("failed to make branch hidden : %s", err)
}

View file

@ -20,7 +20,11 @@ Fetches the [.index.json](https://github.com/crowdsecurity/hub/blob/master/.inde
Args: cobra.ExactArgs(0),
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if !config.configured {
return fmt.Errorf("you must configure cli before interacting with hub.")
return fmt.Errorf("you must configure cli before interacting with hub")
}
if err := setHubBranch(); err != nil {
return fmt.Errorf("error while setting hub branch: %s", err)
}
return nil
},

View file

@ -90,6 +90,10 @@ cscli upgrade --force # Overwrite tainted configuration
if !config.configured {
return fmt.Errorf("you must configure cli before interacting with hub")
}
if err := setHubBranch(); err != nil {
return fmt.Errorf("error while setting hub branch: %s", err)
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {

View file

@ -1,5 +1,11 @@
package main
import (
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
log "github.com/sirupsen/logrus"
)
func inSlice(s string, slice []string) bool {
for _, str := range slice {
if s == str {
@ -17,3 +23,27 @@ func indexOf(s string, slice []string) int {
}
return -1
}
func setHubBranch() error {
/*
if no branch has been specified in flags for the hub, then use the one corresponding to crowdsec version
*/
if cwhub.HubBranch == "" {
latest, err := cwversion.Latest()
if err != nil {
cwhub.HubBranch = "master"
return err
}
if cwversion.Version == latest {
cwhub.HubBranch = "master"
} else {
log.Warnf("Crowdsec is not the latest version. Current version is '%s' and latest version is '%s'. Please update it!", cwversion.Version, latest)
log.Warnf("As a result, you will not be able to use parsers/scenarios/collections added to Crowdsec Hub after CrowdSec %s", latest)
cwhub.HubBranch = cwversion.Version
}
log.Debugf("Using branch '%s' for the hub", cwhub.HubBranch)
}
return nil
}

View file

@ -1,5 +1,8 @@
{{cli.bin}} allows you install, list, upgrade and remove configurations : parsers, enrichment, scenarios.
!!! warning
If you're not running the latest CrowdSec version, configurations might not be the latest available. `cscli` will use the branch of the corresponding CrowdSec version to download and install configurations from the hub (it will use the `master` branch if you are on the latest CrowdSec version).
The various parsers, enrichers and scenarios installed on your machine makes a coherent ensemble to provide detection capabilities.
_Parsers, Scenarios and Enrichers are often bundled together in "collections" to facilitate configuration._

View file

@ -1,8 +1,10 @@
package cwversion
import (
"encoding/json"
"fmt"
"log"
"net/http"
version "github.com/hashicorp/go-version"
)
@ -60,3 +62,24 @@ func Statisfies(strvers string, constraint string) (bool, error) {
}
return true, nil
}
// Latest return latest crowdsec version based on github
func Latest() (string, error) {
latest := make(map[string]interface{})
resp, err := http.Get("https://api.github.com/repos/crowdsecurity/crowdsec/releases/latest")
if err != nil {
return "", err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&latest)
if err != nil {
return "", err
}
if _, ok := latest["name"]; !ok {
return "", fmt.Errorf("unable to find latest release name from github api")
}
return latest["name"].(string), nil
}