* fix #842 and move preflight checks tgth * handle new container name Co-authored-by: AlteredCoder <AlteredCoder>
This commit is contained in:
parent
1b39893fcf
commit
033c8e17e8
|
@ -6,7 +6,6 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
@ -29,7 +28,7 @@ var (
|
||||||
/**/
|
/**/
|
||||||
metabaseListenAddress = "127.0.0.1"
|
metabaseListenAddress = "127.0.0.1"
|
||||||
metabaseListenPort = "3000"
|
metabaseListenPort = "3000"
|
||||||
metabaseContainerID = "/crowdsec-metabase"
|
metabaseContainerID = "crowdsec-metabase"
|
||||||
crowdsecGroup = "crowdsec"
|
crowdsecGroup = "crowdsec"
|
||||||
|
|
||||||
forceYes bool
|
forceYes bool
|
||||||
|
@ -54,9 +53,8 @@ cscli dashboard stop
|
||||||
cscli dashboard remove
|
cscli dashboard remove
|
||||||
`,
|
`,
|
||||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||||
|
if err := metabase.TestAvailability(); err != nil {
|
||||||
if runtime.GOARCH != "amd64" {
|
log.Fatalf("%s", err)
|
||||||
log.Fatalf("cscli dashboard is only available on amd64, but you are running %s", runtime.GOARCH)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
||||||
|
@ -73,6 +71,17 @@ cscli dashboard remove
|
||||||
log.Fatalf(err.Error())
|
log.Fatalf(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
Old container name was "/crowdsec-metabase" but podman doesn't
|
||||||
|
allow '/' in container name. We do this check to not break
|
||||||
|
existing dashboard setup.
|
||||||
|
*/
|
||||||
|
if !metabase.IsContainerExist(metabaseContainerID) {
|
||||||
|
oldContainerID := fmt.Sprintf("/%s", metabaseContainerID)
|
||||||
|
if metabase.IsContainerExist(oldContainerID) {
|
||||||
|
metabaseContainerID = oldContainerID
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -140,7 +149,7 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
|
||||||
log.Fatalf("unable to chown sqlite db file '%s': %s", csConfig.DbConfig.DbPath, err)
|
log.Fatalf("unable to chown sqlite db file '%s': %s", csConfig.DbConfig.DbPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
mb, err := metabase.SetupMetabase(csConfig.API.Server.DbConfig, metabaseListenAddress, metabaseListenPort, metabaseUser, metabasePassword, metabaseDbPath, dockerGroup.Gid)
|
mb, err := metabase.SetupMetabase(csConfig.API.Server.DbConfig, metabaseListenAddress, metabaseListenPort, metabaseUser, metabasePassword, metabaseDbPath, dockerGroup.Gid, metabaseContainerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf(err.Error())
|
log.Fatalf(err.Error())
|
||||||
}
|
}
|
||||||
|
@ -172,7 +181,7 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
|
||||||
Long: `Stats the metabase container using docker.`,
|
Long: `Stats the metabase container using docker.`,
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
mb, err := metabase.NewMetabase(metabaseConfigPath)
|
mb, err := metabase.NewMetabase(metabaseConfigPath, metabaseContainerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf(err.Error())
|
log.Fatalf(err.Error())
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,7 +27,7 @@ type Container struct {
|
||||||
DockerGroupID string
|
DockerGroupID string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewContainer(listenAddr string, listenPort string, sharedFolder string, name string, image string, mbDBURI string, dockerGroupID string) (*Container, error) {
|
func NewContainer(listenAddr string, listenPort string, sharedFolder string, containerName string, image string, mbDBURI string, dockerGroupID string) (*Container, error) {
|
||||||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to create docker client : %s", err)
|
return nil, fmt.Errorf("failed to create docker client : %s", err)
|
||||||
|
@ -37,7 +37,7 @@ func NewContainer(listenAddr string, listenPort string, sharedFolder string, nam
|
||||||
ListenPort: listenPort,
|
ListenPort: listenPort,
|
||||||
SharedFolder: sharedFolder,
|
SharedFolder: sharedFolder,
|
||||||
Image: image,
|
Image: image,
|
||||||
Name: name,
|
Name: containerName,
|
||||||
CLI: cli,
|
CLI: cli,
|
||||||
MBDBUri: mbDBURI,
|
MBDBUri: mbDBURI,
|
||||||
DockerGroupID: dockerGroupID,
|
DockerGroupID: dockerGroupID,
|
||||||
|
|
|
@ -3,15 +3,18 @@ package metabase
|
||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/docker/docker/client"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
|
@ -41,14 +44,27 @@ type Config struct {
|
||||||
var (
|
var (
|
||||||
metabaseDefaultUser = "crowdsec@crowdsec.net"
|
metabaseDefaultUser = "crowdsec@crowdsec.net"
|
||||||
metabaseDefaultPassword = "!!Cr0wdS3c_M3t4b4s3??"
|
metabaseDefaultPassword = "!!Cr0wdS3c_M3t4b4s3??"
|
||||||
containerName = "/crowdsec-metabase"
|
|
||||||
metabaseImage = "metabase/metabase:v0.37.0.2"
|
metabaseImage = "metabase/metabase:v0.37.0.2"
|
||||||
containerSharedFolder = "/metabase-data"
|
containerSharedFolder = "/metabase-data"
|
||||||
|
metabaseSQLiteDBURL = "https://crowdsec-statics-assets.s3-eu-west-1.amazonaws.com/metabase_sqlite.zip"
|
||||||
metabaseSQLiteDBURL = "https://crowdsec-statics-assets.s3-eu-west-1.amazonaws.com/metabase_sqlite.zip"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (m *Metabase) Init() error {
|
func TestAvailability() error {
|
||||||
|
if runtime.GOARCH != "amd64" {
|
||||||
|
return fmt.Errorf("cscli dashboard is only available on amd64, but you are running %s", runtime.GOARCH)
|
||||||
|
}
|
||||||
|
|
||||||
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create docker client : %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = cli.Ping(context.TODO())
|
||||||
|
return err
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *Metabase) Init(containerName string) error {
|
||||||
var err error
|
var err error
|
||||||
var DBConnectionURI string
|
var DBConnectionURI string
|
||||||
var remoteDBAddr string
|
var remoteDBAddr string
|
||||||
|
@ -81,12 +97,12 @@ func (m *Metabase) Init() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMetabase(configPath string) (*Metabase, error) {
|
func NewMetabase(configPath string, containerName string) (*Metabase, error) {
|
||||||
m := &Metabase{}
|
m := &Metabase{}
|
||||||
if err := m.LoadConfig(configPath); err != nil {
|
if err := m.LoadConfig(configPath); err != nil {
|
||||||
return m, err
|
return m, err
|
||||||
}
|
}
|
||||||
if err := m.Init(); err != nil {
|
if err := m.Init(containerName); err != nil {
|
||||||
return m, err
|
return m, err
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
|
@ -118,15 +134,11 @@ func (m *Metabase) LoadConfig(configPath string) error {
|
||||||
|
|
||||||
m.Config = config
|
m.Config = config
|
||||||
|
|
||||||
if err := m.Init(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func SetupMetabase(dbConfig *csconfig.DatabaseCfg, listenAddr string, listenPort string, username string, password string, mbDBPath string, dockerGroupID string) (*Metabase, error) {
|
func SetupMetabase(dbConfig *csconfig.DatabaseCfg, listenAddr string, listenPort string, username string, password string, mbDBPath string, dockerGroupID string, containerName string) (*Metabase, error) {
|
||||||
metabase := &Metabase{
|
metabase := &Metabase{
|
||||||
Config: &Config{
|
Config: &Config{
|
||||||
Database: dbConfig,
|
Database: dbConfig,
|
||||||
|
@ -139,7 +151,7 @@ func SetupMetabase(dbConfig *csconfig.DatabaseCfg, listenAddr string, listenPort
|
||||||
DockerGroupID: dockerGroupID,
|
DockerGroupID: dockerGroupID,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
if err := metabase.Init(); err != nil {
|
if err := metabase.Init(containerName); err != nil {
|
||||||
return nil, errors.Wrap(err, "metabase setup init")
|
return nil, errors.Wrap(err, "metabase setup init")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue