The commands added are: boundary daemon start boundary daemon stop boundary daemon add-token boundary daemon status boundary search If these actions are performed on a platform that doesn't support sqlite natively in go an error is returned. Besides some hooks into the cmd package all the significant logic resides in internal/clientcache feat (cmds/authenticate): output the token name when storing token feat: update go-dbw which now uses pure go sqlite driver refact (base.Command): export FlagOutputCurlString Exporting FlagOutputCurlString allows other commands that embed the base.Command to access the flag as needed feat: add a cache Repository to manage cache storage feat: add a "cache" CLI command This new command will support both managing and searching the local CLI cache daemon. feat: start cache daemon as needed when issue CLI commands Move files to new directories and add persona table (#3526) * Add persona table and move files to new directories Client daemon now uses unix sockets and update pathing (#3535) * Add unix socket listen and dialers with file permissions applied * Update the pathing to match RFC Cache refreshing now uses all stored personas. (#3545) Updating headers for LLB files Decouple daemon and cache server (#3566) * Separate daemon from server * Add persona handler to daemon server (#3579) Moved search to use mql and added tests for the search command (#3589) * search uses hashicorp/mql Windows support for client side daemon (#3618) * exec.Command for background process * Log to stderr when in foreground and file always * Use pid files across platform for start and stop Requests with version info must match the daemon's version (#3640) * requests with version info must match daemons Update dependencies and breakout cgo/pure go sqlite drivers (#3657) upgrade go-dbw deps and add cgo build tags Enable graceful shutdown in windows (#3695) * Enable graceful shutdown in windows Co-authored-by: Jim <jlambert@hashicorp.com> Targets delete when a persona changes address or user id (#3673) * Handle cache refresh in repo Add sessions to the client side cache (#3682) * Add sessions to repository * Search command supports sessions * adding session related tests to db and refresh Change cache schema from personas to tokens+users (#3737) * Change from personas to tokens+users * Separate keyring tokens from auth tokens Add support for keyringless auth tokens (#3765) * Add support for keyringless auth tokens Remove expired auth tokens from cache (#3768) * Remove expired auth tokens from cache Complete the TODO of checking for inflight tx (#3779) Delete tokens that the controller says are invalid (#3782) fix search test with boundary token check (#3799) Collect resource info in repository_<resource>s.go (#3769) * Collect resource info in repository_<resource>s.go * Move search logic into the domain (#3794) Daemon and search restricted to supported platforms (#3801) * Daemon and search restricted to supported platforms Reorganize the clientcache packages (#3814) * Refactor for self contained domain logic * Put clientcache stuff under internal
274 lines
7.4 KiB
Go
274 lines
7.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package daemon
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/hashicorp/boundary/internal/clientcache/internal/daemon"
|
|
"github.com/hashicorp/boundary/internal/cmd/base"
|
|
"github.com/hashicorp/boundary/internal/errors"
|
|
"github.com/mitchellh/cli"
|
|
"github.com/mitchellh/go-homedir"
|
|
"github.com/posener/complete"
|
|
)
|
|
|
|
const (
|
|
dotDirname = ".boundary"
|
|
pidFileName = "cache.pid"
|
|
logFileName = "cache.log"
|
|
|
|
// Mark of process as having been started in the background
|
|
backgroundEnvName = "_BOUNDARY_DAEMON_BACKGROUND"
|
|
backgroundEnvVal = "1"
|
|
)
|
|
|
|
var (
|
|
_ cli.Command = (*StartCommand)(nil)
|
|
_ cli.CommandAutocomplete = (*StartCommand)(nil)
|
|
)
|
|
|
|
type server interface {
|
|
setupLogging(context.Context, io.Writer) error
|
|
serve(context.Context, daemon.Commander, net.Listener) error
|
|
shutdown() error
|
|
}
|
|
|
|
type StartCommand struct {
|
|
*base.Command
|
|
|
|
flagRefreshIntervalSeconds int64
|
|
flagDatabaseUrl string
|
|
flagLogLevel string
|
|
flagLogFormat string
|
|
flagStoreDebug bool
|
|
flagBackground bool
|
|
}
|
|
|
|
func (c *StartCommand) Synopsis() string {
|
|
return "Start a Boundary daemon"
|
|
}
|
|
|
|
func (c *StartCommand) Help() string {
|
|
helpText := `
|
|
Usage: boundary daemon start [options]
|
|
|
|
Start a daemon:
|
|
|
|
$ boundary daemon start
|
|
|
|
For a full list of examples, please see the documentation.
|
|
|
|
` + c.Flags().Help()
|
|
return strings.TrimSpace(helpText)
|
|
}
|
|
|
|
func (c *StartCommand) Flags() *base.FlagSets {
|
|
set := c.FlagSet(base.FlagSetHTTP | base.FlagSetClient)
|
|
|
|
f := set.NewFlagSet("Command Options")
|
|
f.StringVar(&base.StringVar{
|
|
Name: "log-level",
|
|
Target: &c.flagLogLevel,
|
|
EnvVar: "BOUNDARY_LOG_LEVEL",
|
|
Completion: complete.PredictSet("trace", "debug", "info", "warn", "err"),
|
|
Usage: "Log verbosity level, mostly as a fallback for events. Supported values (in order of more detail to less) are " +
|
|
"\"trace\", \"debug\", \"info\", \"warn\", and \"err\".",
|
|
})
|
|
f.StringVar(&base.StringVar{
|
|
Name: "log-format",
|
|
Target: &c.flagLogFormat,
|
|
Completion: complete.PredictSet("standard", "json"),
|
|
Usage: `Log format, mostly as a fallback for events. Supported values are "standard" and "json".`,
|
|
})
|
|
f.StringVar(&base.StringVar{
|
|
Name: "database-url",
|
|
Target: &c.flagDatabaseUrl,
|
|
Usage: `If set, specifies the URL used to connect to the sqlite database (store) for caching. This can refer to a file on disk (file://) from which a URL will be read; an env var (env://) from which the URL will be read; or a direct database URL.`,
|
|
})
|
|
f.Int64Var(&base.Int64Var{
|
|
Name: "refresh-interval-seconds",
|
|
Target: &c.flagRefreshIntervalSeconds,
|
|
Usage: `If set, specifies the number of seconds between cache refreshes. Default: 5 minutes`,
|
|
Aliases: []string{"r"},
|
|
Default: daemon.DefaultRefreshIntervalSeconds,
|
|
})
|
|
f.BoolVar(&base.BoolVar{
|
|
Name: "store-debug",
|
|
Target: &c.flagStoreDebug,
|
|
Default: false,
|
|
Usage: `Turn on store debugging`,
|
|
Aliases: []string{"d"},
|
|
})
|
|
f.BoolVar(&base.BoolVar{
|
|
Name: "background",
|
|
Target: &c.flagBackground,
|
|
Default: false,
|
|
Usage: `Turn on store debugging`,
|
|
})
|
|
|
|
return set
|
|
}
|
|
|
|
func (c *StartCommand) AutocompleteArgs() complete.Predictor {
|
|
return complete.PredictNothing
|
|
}
|
|
|
|
func (c *StartCommand) AutocompleteFlags() complete.Flags {
|
|
return c.Flags().Completions()
|
|
}
|
|
|
|
func (c *StartCommand) Run(args []string) int {
|
|
const op = "daemon.(StartCommand).Run"
|
|
ctx, cancel := context.WithCancel(c.Context)
|
|
defer cancel()
|
|
|
|
var err error
|
|
f := c.Flags()
|
|
if err := f.Parse(args); err != nil {
|
|
c.PrintCliError(err)
|
|
return base.CommandUserError
|
|
}
|
|
|
|
dotDir, err := DefaultDotDirectory(ctx)
|
|
if err != nil {
|
|
return base.CommandCliError
|
|
}
|
|
|
|
continueRun, writers, cleanup, err := makeBackground(ctx, dotDir, c.flagBackground)
|
|
defer func() {
|
|
if cleanup != nil {
|
|
cleanup()
|
|
}
|
|
}()
|
|
if err != nil {
|
|
c.PrintCliError(err)
|
|
return base.CommandCliError
|
|
}
|
|
if !continueRun {
|
|
return base.CommandSuccess
|
|
}
|
|
|
|
// TODO: print something out for the spawner to consume in case they can easily
|
|
// report if the daemon started or not.
|
|
|
|
logFilePath := filepath.Join(dotDir, logFileName)
|
|
logFile, err := os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY, 0o600)
|
|
if err != nil {
|
|
c.PrintCliError(err)
|
|
return base.CommandCliError
|
|
}
|
|
defer logFile.Close()
|
|
if _, err := logFile.Seek(0, io.SeekEnd); err != nil {
|
|
c.PrintCliError(err)
|
|
return base.CommandCliError
|
|
}
|
|
writers = append(writers, logFile)
|
|
|
|
cfg := &daemon.Config{
|
|
ContextCancel: cancel,
|
|
RefreshIntervalSeconds: c.flagRefreshIntervalSeconds,
|
|
DatabaseUrl: c.flagDatabaseUrl,
|
|
StoreDebug: c.flagStoreDebug,
|
|
LogLevel: c.flagLogLevel,
|
|
LogFormat: c.flagLogFormat,
|
|
LogWriter: io.MultiWriter(writers...),
|
|
DotDirectory: dotDir,
|
|
}
|
|
|
|
srv, err := daemon.New(ctx, cfg)
|
|
if err != nil {
|
|
c.UI.Error(err.Error())
|
|
return base.CommandUserError
|
|
}
|
|
|
|
var srvErr error
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
srvErr = srv.Serve(ctx, c)
|
|
}()
|
|
|
|
// This is a blocking call. We rely on the c.ShutdownCh to cancel this
|
|
// context when sigterm or sigint is received.
|
|
<-ctx.Done()
|
|
if err := srv.Shutdown(ctx); err != nil {
|
|
c.PrintCliError(err)
|
|
return base.CommandCliError
|
|
}
|
|
wg.Wait()
|
|
if srvErr != nil {
|
|
c.PrintCliError(srvErr)
|
|
return base.CommandCliError
|
|
}
|
|
|
|
return base.CommandSuccess
|
|
}
|
|
|
|
// DefaultDotDirectory returns the default path to the boundary dot directory.
|
|
func DefaultDotDirectory(ctx context.Context) (string, error) {
|
|
const op = "daemon.DefaultDotDirectory"
|
|
homeDir, err := homedir.Dir()
|
|
if err != nil {
|
|
return "", errors.Wrap(ctx, err, op)
|
|
}
|
|
return filepath.Join(homeDir, dotDirname), nil
|
|
}
|
|
|
|
func makeBackground(ctx context.Context, dotDir string, runBackgroundFlag bool) (bool, []io.Writer, pidCleanup, error) {
|
|
const op = "daemon.makeBackground"
|
|
|
|
writers := []io.Writer{}
|
|
pidPath := filepath.Join(dotDir, pidFileName)
|
|
if running, err := pidFileInUse(ctx, pidPath); running != nil {
|
|
return false, writers, noopPidCleanup, errors.New(ctx, errors.Conflict, op, "daemon already running")
|
|
} else if err != nil && !errors.Match(errors.T(errors.NotFound), err) {
|
|
return false, writers, noopPidCleanup, errors.Wrap(ctx, err, op)
|
|
}
|
|
|
|
if !runBackgroundFlag && os.Getenv(backgroundEnvName) != backgroundEnvVal {
|
|
writers = append(writers, os.Stderr)
|
|
}
|
|
|
|
if !runBackgroundFlag || os.Getenv(backgroundEnvName) == backgroundEnvVal {
|
|
// We are either already running in the background or background was
|
|
// not requested. Write the pid file and continue.
|
|
cleanup, err := writePidFile(ctx, pidPath)
|
|
if err != nil {
|
|
return false, writers, noopPidCleanup, errors.Wrap(ctx, err, op)
|
|
}
|
|
return true, writers, cleanup, nil
|
|
}
|
|
|
|
absPath, err := os.Executable()
|
|
if err != nil {
|
|
return false, writers, noopPidCleanup, errors.Wrap(ctx, err, op)
|
|
}
|
|
|
|
env := os.Environ()
|
|
env = append(env, fmt.Sprintf("%s=%s", backgroundEnvName, backgroundEnvVal))
|
|
cmd := exec.Command(absPath, "daemon", "start")
|
|
cmd.Env = env
|
|
if err = cmd.Start(); err != nil {
|
|
return false, writers, noopPidCleanup, errors.Wrap(ctx, err, op)
|
|
}
|
|
|
|
// TODO: Read the output from the child process for a brief time
|
|
// to see if we can identify any errors that might arise.
|
|
return false, writers, noopPidCleanup, nil
|
|
}
|
|
|
|
type pidCleanup func() error
|
|
|
|
var noopPidCleanup pidCleanup = func() error { return nil }
|