fallback to master for hub index download if it does not exist (#2210)
This commit is contained in:
parent
0ea1508ff9
commit
6e3ca35941
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
|
@ -98,8 +99,16 @@ Fetches the [.index.json](https://github.com/crowdsecurity/hub/blob/master/.inde
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
if err := cwhub.UpdateHubIdx(csConfig.Hub); err != nil {
|
if err := cwhub.UpdateHubIdx(csConfig.Hub); err != nil {
|
||||||
|
if errors.Is(err, cwhub.ErrIndexNotFound) {
|
||||||
|
log.Warnf("Could not find index file for branch '%s', using 'master'", cwhub.HubBranch)
|
||||||
|
cwhub.HubBranch = "master"
|
||||||
|
if err := cwhub.UpdateHubIdx(csConfig.Hub); err != nil {
|
||||||
|
log.Fatalf("Failed to get Hub index after retry : %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
log.Fatalf("Failed to get Hub index : %v", err)
|
log.Fatalf("Failed to get Hub index : %v", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
//use LocalSync to get warnings about tainted / outdated items
|
//use LocalSync to get warnings about tainted / outdated items
|
||||||
_, warn := cwhub.LocalSync(csConfig.Hub)
|
_, warn := cwhub.LocalSync(csConfig.Hub)
|
||||||
for _, v := range warn {
|
for _, v := range warn {
|
||||||
|
|
|
@ -18,6 +18,8 @@ import (
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrIndexNotFound = fmt.Errorf("index not found")
|
||||||
|
|
||||||
func UpdateHubIdx(hub *csconfig.Hub) error {
|
func UpdateHubIdx(hub *csconfig.Hub) error {
|
||||||
|
|
||||||
bidx, err := DownloadHubIdx(hub)
|
bidx, err := DownloadHubIdx(hub)
|
||||||
|
@ -47,10 +49,13 @@ func DownloadHubIdx(hub *csconfig.Hub) ([]byte, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed http request for hub index")
|
return nil, errors.Wrap(err, "failed http request for hub index")
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
if resp.StatusCode == http.StatusNotFound {
|
||||||
|
return nil, ErrIndexNotFound
|
||||||
|
}
|
||||||
return nil, fmt.Errorf("bad http code %d while requesting %s", resp.StatusCode, req.URL.String())
|
return nil, fmt.Errorf("bad http code %d while requesting %s", resp.StatusCode, req.URL.String())
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to read request answer for hub index")
|
return nil, errors.Wrap(err, "failed to read request answer for hub index")
|
||||||
|
|
|
@ -16,12 +16,14 @@ import (
|
||||||
func chooseHubBranch() (string, error) {
|
func chooseHubBranch() (string, error) {
|
||||||
latest, err := cwversion.Latest()
|
latest, err := cwversion.Latest()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Warningf("Unable to retrieve latest crowdsec version: %s, defaulting to master", err)
|
||||||
//lint:ignore nilerr reason
|
//lint:ignore nilerr reason
|
||||||
return "master", nil // ignore
|
return "master", nil // ignore
|
||||||
}
|
}
|
||||||
|
|
||||||
csVersion := cwversion.VersionStrip()
|
csVersion := cwversion.VersionStrip()
|
||||||
if csVersion == latest {
|
if csVersion == latest {
|
||||||
|
log.Debugf("current version is equal to latest (%s)", csVersion)
|
||||||
return "master", nil
|
return "master", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue