mattermost/server/cmd/mmctl/commands/utils_unix.go
Ben Schumacher 892a7c9c69
Some checks are pending
API / build (push) Waiting to run
Server CI / Compute Go Version (push) Waiting to run
Server CI / Check mocks (push) Blocked by required conditions
Server CI / Check go mod tidy (push) Blocked by required conditions
Server CI / check-style (push) Blocked by required conditions
Server CI / Check serialization methods for hot structs (push) Blocked by required conditions
Server CI / Vet API (push) Blocked by required conditions
Server CI / Check migration files (push) Blocked by required conditions
Server CI / Generate email templates (push) Blocked by required conditions
Server CI / Check store layers (push) Blocked by required conditions
Server CI / Check mmctl docs (push) Blocked by required conditions
Server CI / Postgres with binary parameters (push) Blocked by required conditions
Server CI / Postgres (push) Blocked by required conditions
Server CI / Postgres (FIPS) (push) Blocked by required conditions
Server CI / Generate Test Coverage (push) Blocked by required conditions
Server CI / Run mmctl tests (push) Blocked by required conditions
Server CI / Run mmctl tests (FIPS) (push) Blocked by required conditions
Server CI / Build mattermost server app (push) Blocked by required conditions
Web App CI / check-lint (push) Waiting to run
Web App CI / check-i18n (push) Waiting to run
Web App CI / check-types (push) Waiting to run
Web App CI / test (push) Waiting to run
Web App CI / build (push) Waiting to run
Use golangci-lints's build-in modernize linter (#34341)
2025-11-04 12:09:11 +01:00

80 lines
2.1 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
//go:build linux || darwin || freebsd
package commands
import (
"fmt"
"os"
"os/user"
"strconv"
"syscall"
"github.com/isacikgoz/prompt"
"github.com/pkg/errors"
)
func checkValidSocket(socketPath string) error {
// check file mode and permissions
fi, err := os.Stat(socketPath)
if err != nil && os.IsNotExist(err) {
return fmt.Errorf("socket file %q doesn't exist, please check the server configuration for local mode", socketPath)
} else if err != nil {
return err
}
if fi.Mode() != expectedSocketMode {
return fmt.Errorf("invalid file mode for file %q, it must be a socket with 0600 permissions", socketPath)
}
// check matching user
cUser, err := user.Current()
if err != nil {
return err
}
s, ok := fi.Sys().(*syscall.Stat_t)
if !ok {
return fmt.Errorf("cannot get owner of the file %q", socketPath)
}
// if user id is "0", they are root and we should avoid this check
if strconv.FormatUint(uint64(s.Uid), 10) != cUser.Uid && cUser.Uid != "0" {
return fmt.Errorf("owner of the file %q must be the same user running mmctl", socketPath)
}
return nil
}
func getConfirmation(question string, dbConfirmation bool) error {
if err := checkInteractiveTerminal(); err != nil {
return fmt.Errorf("could not proceed, either enable --confirm flag or use an interactive shell to complete operation: %w", err)
}
if dbConfirmation {
s, err := prompt.NewSelection("Have you performed a database backup?", []string{"no", "yes"}, "", 2)
if err != nil {
return fmt.Errorf("could not initiate prompt: %w", err)
}
ans, err := s.Run()
if err != nil {
return fmt.Errorf("error running prompt: %w", err)
}
if ans != "yes" {
return errors.New("aborted")
}
}
s, err := prompt.NewSelection(question, []string{"no", "yes"}, "WARNING: This operation is not reversible.", 2)
if err != nil {
return fmt.Errorf("could not initiate prompt: %w", err)
}
ans, err := s.Run()
if err != nil {
return fmt.Errorf("error running prompt: %w", err)
}
if ans != "yes" {
return errors.New("aborted")
}
return nil
}