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

[Metabase] QOL Changes and chown wal files #2627

Merged
79 changes: 48 additions & 31 deletions cmd/crowdsec-cli/dashboard.go
@@ -1,3 +1,5 @@
//go:build linux

package main

import (
Expand All @@ -9,6 +11,7 @@
"path/filepath"
"strconv"
"strings"
"syscall"
"unicode"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -136,6 +139,9 @@
if err != nil {
return err
}
if err = chownDatabase(dockerGroup.Gid); err != nil {
return err
}

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L142-L144

Added lines #L142 - L144 were not covered by tests
mb, err := metabase.SetupMetabase(csConfig.API.Server.DbConfig, metabaseListenAddress, metabaseListenPort, metabaseUser, metabasePassword, metabaseDbPath, dockerGroup.Gid, metabaseContainerID, metabaseImage)
if err != nil {
return err
Expand Down Expand Up @@ -366,45 +372,56 @@
}

func checkGroups(forceYes *bool) (*user.Group, error) {
groupExist := false
dockerGroup, err := user.LookupGroup(crowdsecGroup)
if err == nil {
groupExist = true
return dockerGroup, nil

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L377

Added line #L377 was not covered by tests
}
if !groupExist {
if !*forceYes {
var answer bool
prompt := &survey.Confirm{
Message: fmt.Sprintf("For metabase docker to be able to access SQLite file we need to add a new group called '%s' to the system, is it ok for you ?", crowdsecGroup),
Default: true,
}
if err := survey.AskOne(prompt, &answer); err != nil {
return dockerGroup, fmt.Errorf("unable to ask to question: %s", err)
}
if !answer {
return dockerGroup, fmt.Errorf("unable to continue without creating '%s' group", crowdsecGroup)
}
}
groupAddCmd, err := exec.LookPath("groupadd")
if err != nil {
return dockerGroup, fmt.Errorf("unable to find 'groupadd' command, can't continue")
if !*forceYes {
var answer bool
prompt := &survey.Confirm{
Message: fmt.Sprintf("For metabase docker to be able to access SQLite file we need to add a new group called '%s' to the system, is it ok for you ?", crowdsecGroup),
Default: true,

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L379-L383

Added lines #L379 - L383 were not covered by tests
}

groupAdd := &exec.Cmd{Path: groupAddCmd, Args: []string{groupAddCmd, crowdsecGroup}}
if err := groupAdd.Run(); err != nil {
return dockerGroup, fmt.Errorf("unable to add group '%s': %s", dockerGroup, err)
if err := survey.AskOne(prompt, &answer); err != nil {
return dockerGroup, fmt.Errorf("unable to ask to question: %s", err)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L385-L386

Added lines #L385 - L386 were not covered by tests
}
dockerGroup, err = user.LookupGroup(crowdsecGroup)
if err != nil {
return dockerGroup, fmt.Errorf("unable to lookup '%s' group: %+v", dockerGroup, err)
if !answer {
return dockerGroup, fmt.Errorf("unable to continue without creating '%s' group", crowdsecGroup)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L388-L389

Added lines #L388 - L389 were not covered by tests
}
}
intID, err := strconv.Atoi(dockerGroup.Gid)
groupAddCmd, err := exec.LookPath("groupadd")

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L392

Added line #L392 was not covered by tests
if err != nil {
return dockerGroup, fmt.Errorf("unable to convert group ID to int: %s", err)
return dockerGroup, fmt.Errorf("unable to find 'groupadd' command, can't continue")

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L394

Added line #L394 was not covered by tests
}
if err := os.Chown(csConfig.DbConfig.DbPath, 0, intID); err != nil {
return dockerGroup, fmt.Errorf("unable to chown sqlite db file '%s': %s", csConfig.DbConfig.DbPath, err)

groupAdd := &exec.Cmd{Path: groupAddCmd, Args: []string{groupAddCmd, crowdsecGroup}}
if err := groupAdd.Run(); err != nil {
return dockerGroup, fmt.Errorf("unable to add group '%s': %s", dockerGroup, err)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L397-L399

Added lines #L397 - L399 were not covered by tests
}
return dockerGroup, nil
return user.LookupGroup(crowdsecGroup)

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L401

Added line #L401 was not covered by tests
}

func chownDatabase(gid string) error {
intID, err := strconv.Atoi(gid)
if err != nil {
return fmt.Errorf("unable to convert group ID to int: %s", err)
}
if stat, err := os.Stat(csConfig.DbConfig.DbPath); !os.IsNotExist(err) {
info := stat.Sys()
if err := os.Chown(csConfig.DbConfig.DbPath, int(info.(*syscall.Stat_t).Uid), intID); err != nil {
return fmt.Errorf("unable to chown sqlite db file '%s': %s", csConfig.DbConfig.DbPath, err)
}

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L404-L413

Added lines #L404 - L413 were not covered by tests
}
if csConfig.DbConfig.Type == "sqlite" && csConfig.DbConfig.UseWal != nil && *csConfig.DbConfig.UseWal {
for _, ext := range []string{"-wal", "-shm"} {
file := csConfig.DbConfig.DbPath + ext
if stat, err := os.Stat(file); !os.IsNotExist(err) {
info := stat.Sys()
if err := os.Chown(file, int(info.(*syscall.Stat_t).Uid), intID); err != nil {
return fmt.Errorf("unable to chown sqlite db file '%s': %s", file, err)
}

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L415-L422

Added lines #L415 - L422 were not covered by tests
}
}
}
return nil

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

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/dashboard.go#L426

Added line #L426 was not covered by tests
}
22 changes: 22 additions & 0 deletions cmd/crowdsec-cli/dashboard_unsupported.go
@@ -0,0 +1,22 @@
//go:build !linux

package main

import (
"runtime"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func NewDashboardCmd() *cobra.Command {
var cmdDashboard = &cobra.Command{
Use: "dashboard",
DisableAutoGenTag: true,
Run: func(cmd *cobra.Command, args []string) {
log.Infof("Dashboard command is disabled on %s", runtime.GOOS)
},
}

return cmdDashboard
}
9 changes: 0 additions & 9 deletions pkg/metabase/container.go
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"context"
"fmt"
"runtime"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
Expand Down Expand Up @@ -93,14 +92,6 @@ func (c *Container) Create() error {
Tty: true,
Env: env,
}
os := runtime.GOOS
switch os {
case "linux":
case "windows", "darwin":
return fmt.Errorf("mac and windows are not supported yet")
default:
return fmt.Errorf("OS '%s' is not supported", os)
}

log.Infof("creating container '%s'", c.Name)
resp, err := c.CLI.ContainerCreate(ctx, dockerConfig, hostConfig, nil, nil, c.Name)
Expand Down