Skip to content

Commit

Permalink
feat(cscli): add env variable flag for dashboard setup (#3110)
Browse files Browse the repository at this point in the history
Signed-off-by: Nathanael DEMACON <quantumsheep@users.noreply.github.com>
Co-authored-by: Nathanael DEMACON <quantumsheep@users.noreply.github.com>
Co-authored-by: Laurence Jones <laurence.jones@live.co.uk>
  • Loading branch information
3 people authored Jan 13, 2025
1 parent 303ce8e commit e9c1ed0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 47 deletions.
12 changes: 7 additions & 5 deletions cmd/crowdsec-cli/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ var (
metabaseConfigFile = "metabase.yaml"
metabaseImage = "metabase/metabase:v0.46.6.1"
/**/
metabaseListenAddress = "127.0.0.1"
metabaseListenPort = "3000"
metabaseContainerID = "crowdsec-metabase"
crowdsecGroup = "crowdsec"
metabaseListenAddress = "127.0.0.1"
metabaseListenPort = "3000"
metabaseContainerID = "crowdsec-metabase"
metabaseContainerEnvironmentVariables []string
crowdsecGroup = "crowdsec"

forceYes bool

Expand Down Expand Up @@ -166,7 +167,7 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
if err = cli.chownDatabase(dockerGroup.Gid); err != nil {
return err
}
mb, err := metabase.SetupMetabase(cli.cfg().API.Server.DbConfig, metabaseListenAddress, metabaseListenPort, metabaseUser, metabasePassword, metabaseDBPath, dockerGroup.Gid, metabaseContainerID, metabaseImage)
mb, err := metabase.SetupMetabase(cli.cfg().API.Server.DbConfig, metabaseListenAddress, metabaseListenPort, metabaseUser, metabasePassword, metabaseDBPath, dockerGroup.Gid, metabaseContainerID, metabaseImage, metabaseContainerEnvironmentVariables)
if err != nil {
return err
}
Expand All @@ -193,6 +194,7 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
flags.BoolVarP(&forceYes, "yes", "y", false, "force yes")
// flags.StringVarP(&metabaseUser, "user", "u", "crowdsec@crowdsec.net", "metabase user")
flags.StringVar(&metabasePassword, "password", "", "metabase password")
flags.StringSliceVarP(&metabaseContainerEnvironmentVariables, "env", "e", nil, "Additional environment variables to pass to the metabase container")

return cmd
}
Expand Down
44 changes: 23 additions & 21 deletions pkg/metabase/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,33 @@ import (
)

type Container struct {
ListenAddr string
ListenPort string
SharedFolder string
Image string
Name string
ID string
CLI *client.Client
MBDBUri string
DockerGroupID string
ListenAddr string
ListenPort string
SharedFolder string
Image string
Name string
ID string
CLI *client.Client
MBDBUri string
DockerGroupID string
EnvironmentVariables []string
}

func NewContainer(listenAddr string, listenPort string, sharedFolder string, containerName 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, environmentVariables []string) (*Container, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, fmt.Errorf("failed to create docker client : %s", err)
}
return &Container{
ListenAddr: listenAddr,
ListenPort: listenPort,
SharedFolder: sharedFolder,
Image: image,
Name: containerName,
CLI: cli,
MBDBUri: mbDBURI,
DockerGroupID: dockerGroupID,
ListenAddr: listenAddr,
ListenPort: listenPort,
SharedFolder: sharedFolder,
Image: image,
Name: containerName,
CLI: cli,
MBDBUri: mbDBURI,
DockerGroupID: dockerGroupID,
EnvironmentVariables: environmentVariables,
}, nil
}

Expand Down Expand Up @@ -79,9 +81,9 @@ func (c *Container) Create() error {
},
}

env := []string{
fmt.Sprintf("MB_DB_FILE=%s/metabase.db", containerSharedFolder),
}
env := c.EnvironmentVariables

env = append(env, fmt.Sprintf("MB_DB_FILE=%s/metabase.db", containerSharedFolder))
if c.MBDBUri != "" {
env = append(env, c.MBDBUri)
}
Expand Down
43 changes: 22 additions & 21 deletions pkg/metabase/metabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ type Metabase struct {
}

type Config struct {
Database *csconfig.DatabaseCfg `yaml:"database"`
ListenAddr string `yaml:"listen_addr"`
ListenPort string `yaml:"listen_port"`
ListenURL string `yaml:"listen_url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
DBPath string `yaml:"metabase_db_path"`
DockerGroupID string `yaml:"-"`
Image string `yaml:"image"`
Database *csconfig.DatabaseCfg `yaml:"database"`
ListenAddr string `yaml:"listen_addr"`
ListenPort string `yaml:"listen_port"`
ListenURL string `yaml:"listen_url"`
Username string `yaml:"username"`
Password string `yaml:"password"`
DBPath string `yaml:"metabase_db_path"`
DockerGroupID string `yaml:"-"`
Image string `yaml:"image"`
EnvironmentVariables []string `yaml:"environment_variables"`
}

var (
Expand Down Expand Up @@ -88,7 +89,7 @@ func (m *Metabase) Init(containerName string, image string) error {
if err != nil {
return err
}
m.Container, err = NewContainer(m.Config.ListenAddr, m.Config.ListenPort, m.Config.DBPath, containerName, image, DBConnectionURI, m.Config.DockerGroupID)
m.Container, err = NewContainer(m.Config.ListenAddr, m.Config.ListenPort, m.Config.DBPath, containerName, image, DBConnectionURI, m.Config.DockerGroupID, m.Config.EnvironmentVariables)
if err != nil {
return fmt.Errorf("container init: %w", err)
}
Expand Down Expand Up @@ -137,21 +138,21 @@ func (m *Metabase) LoadConfig(configPath string) error {
m.Config = config

return nil

}

func SetupMetabase(dbConfig *csconfig.DatabaseCfg, listenAddr string, listenPort string, username string, password string, mbDBPath string, dockerGroupID string, containerName string, image string) (*Metabase, error) {
func SetupMetabase(dbConfig *csconfig.DatabaseCfg, listenAddr string, listenPort string, username string, password string, mbDBPath string, dockerGroupID string, containerName string, image string, environmentVariables []string) (*Metabase, error) {
metabase := &Metabase{
Config: &Config{
Database: dbConfig,
ListenAddr: listenAddr,
ListenPort: listenPort,
Username: username,
Password: password,
ListenURL: fmt.Sprintf("http://%s:%s", listenAddr, listenPort),
DBPath: mbDBPath,
DockerGroupID: dockerGroupID,
Image: image,
Database: dbConfig,
ListenAddr: listenAddr,
ListenPort: listenPort,
Username: username,
Password: password,
ListenURL: fmt.Sprintf("http://%s:%s", listenAddr, listenPort),
DBPath: mbDBPath,
DockerGroupID: dockerGroupID,
Image: image,
EnvironmentVariables: environmentVariables,
},
}
if err := metabase.Init(containerName, image); err != nil {
Expand Down

0 comments on commit e9c1ed0

Please sign in to comment.