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

feat(cscli): add env variable flag for dashboard setup #3110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 7 additions & 5 deletions cmd/crowdsec-cli/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
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 @@ -154,7 +155,7 @@
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)

Check failure on line 158 in cmd/crowdsec-cli/dashboard.go

View workflow job for this annotation

GitHub Actions / Build + tests

line-length-limit: line is 259 characters, out of limit 221 (revive)

Check warning on line 158 in cmd/crowdsec-cli/dashboard.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L158

Added line #L158 was not covered by tests
if err != nil {
return err
}
Expand All @@ -181,6 +182,7 @@
flags.BoolVarP(&forceYes, "yes", "y", false, "force yes")
// flags.StringVarP(&metabaseUser, "user", "u", "[email protected]", "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 @@
)

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) {

Check warning on line 31 in pkg/metabase/container.go

View check run for this annotation

Codecov / codecov/patch

pkg/metabase/container.go#L31

Added line #L31 was not covered by tests
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,

Check warning on line 45 in pkg/metabase/container.go

View check run for this annotation

Codecov / codecov/patch

pkg/metabase/container.go#L37-L45

Added lines #L37 - L45 were not covered by tests
}, nil
}

Expand Down Expand Up @@ -79,9 +81,9 @@
},
}

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))

Check warning on line 86 in pkg/metabase/container.go

View check run for this annotation

Codecov / codecov/patch

pkg/metabase/container.go#L84-L86

Added lines #L84 - L86 were not covered by tests
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 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 @@
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)

Check warning on line 92 in pkg/metabase/metabase.go

View check run for this annotation

Codecov / codecov/patch

pkg/metabase/metabase.go#L92

Added line #L92 was not covered by tests
if err != nil {
return fmt.Errorf("container init: %w", err)
}
Expand Down Expand Up @@ -137,21 +138,21 @@
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) {

Check warning on line 143 in pkg/metabase/metabase.go

View check run for this annotation

Codecov / codecov/patch

pkg/metabase/metabase.go#L143

Added line #L143 was not covered by tests
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,

Check warning on line 155 in pkg/metabase/metabase.go

View check run for this annotation

Codecov / codecov/patch

pkg/metabase/metabase.go#L146-L155

Added lines #L146 - L155 were not covered by tests
},
}
if err := metabase.Init(containerName, image); err != nil {
Expand Down
Loading