Create Client and update Errors for client daemon commands (#4011)
* Create a client cache daemon client * Fix error formatting for the client cache cmds
This commit is contained in:
1 parent
8af20349e7
commit
b71d134e06
17 files changed
+500
-273
No files matched your search
@@ -5,14 +5,15 @@ package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/internal/clientcache/internal/client"
|
||||
"github.com/hashicorp/boundary/internal/clientcache/internal/daemon"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/version"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
@@ -76,7 +77,7 @@ func (c *AddTokenCommand) Run(args []string) int {
|
||||
return base.CommandCliError
|
||||
}
|
||||
|
||||
apiErr, err := c.Add(ctx, client, keyringType, tokenName)
|
||||
resp, apiErr, err := c.Add(ctx, client, keyringType, tokenName)
|
||||
if err != nil {
|
||||
c.PrintCliError(err)
|
||||
return base.CommandCliError
|
||||
@@ -84,30 +85,35 @@ func (c *AddTokenCommand) Run(args []string) int {
|
||||
if apiErr != nil {
|
||||
c.PrintApiError(apiErr, "Error from daemon when adding a token")
|
||||
return base.CommandApiError
|
||||
|
||||
}
|
||||
switch base.Format(c.UI) {
|
||||
case "json":
|
||||
if ok := c.PrintJsonItem(resp); !ok {
|
||||
return base.CommandCliError
|
||||
}
|
||||
case "table":
|
||||
c.UI.Output("The daemon add-token operation completed successfully.")
|
||||
}
|
||||
return base.CommandSuccess
|
||||
}
|
||||
|
||||
func (c *AddTokenCommand) Add(ctx context.Context, client *api.Client, keyringType, tokenName string) (*api.Error, error) {
|
||||
const op = "daemon.(AddTokenCommand).Add"
|
||||
|
||||
func (c *AddTokenCommand) Add(ctx context.Context, apiClient *api.Client, keyringType, tokenName string) (*api.Response, *api.Error, error) {
|
||||
pa := daemon.UpsertTokenRequest{
|
||||
BoundaryAddr: client.Addr(),
|
||||
BoundaryAddr: apiClient.Addr(),
|
||||
}
|
||||
switch keyringType {
|
||||
case "", base.NoneKeyring:
|
||||
token := client.Token()
|
||||
token := apiClient.Token()
|
||||
if parts := strings.SplitN(token, "_", 4); len(parts) == 3 {
|
||||
pa.AuthTokenId = strings.Join(parts[:2], "_")
|
||||
} else {
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "found auth token is not in the proper format")
|
||||
return nil, nil, errors.New("The found auth token is not in the proper format.")
|
||||
}
|
||||
pa.AuthToken = token
|
||||
default:
|
||||
at := c.ReadTokenFromKeyring(keyringType, tokenName)
|
||||
if at == nil {
|
||||
return nil, errors.New(ctx, errors.Conflict, op, "no auth token available to send to daemon")
|
||||
return nil, nil, errors.New("No auth token could be read from the keyring to send to daemon.")
|
||||
}
|
||||
pa.Keyring = &daemon.KeyringToken{
|
||||
KeyringType: keyringType,
|
||||
@@ -118,42 +124,29 @@ func (c *AddTokenCommand) Add(ctx context.Context, client *api.Client, keyringTy
|
||||
|
||||
dotPath, err := DefaultDotDirectory(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
return addToken(ctx, dotPath, &pa)
|
||||
}
|
||||
|
||||
func addToken(ctx context.Context, daemonPath string, p *daemon.UpsertTokenRequest) (*api.Error, error) {
|
||||
const op = "daemon.addToken"
|
||||
client, err := api.NewClient(nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
func addToken(ctx context.Context, daemonPath string, p *daemon.UpsertTokenRequest) (*api.Response, *api.Error, error) {
|
||||
addr, err := daemon.SocketAddress(daemonPath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
return nil, nil, fmt.Errorf("Error when retrieving the socket address: %w", err)
|
||||
}
|
||||
_, err = os.Stat(addr.Path)
|
||||
if strings.EqualFold(addr.Scheme, "unix") && err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if err := client.SetAddr(addr.String()); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
// Because this is using the real lib it can pick up from stored locations
|
||||
// like the system keychain. Explicitly clear the token for now
|
||||
client.SetToken("")
|
||||
|
||||
req, err := client.NewRequest(ctx, "POST", "/tokens", p)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(daemon.VersionHeaderKey, version.Get().VersionNumber())
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if addr.Scheme == "unix" && err != nil {
|
||||
return nil, nil, fmt.Errorf("Error when detecting if the domain socket is present: %w.", err)
|
||||
}
|
||||
|
||||
return resp.Decode(&struct{}{})
|
||||
c, err := client.New(ctx, addr)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Error when making a new client: %w.", err)
|
||||
}
|
||||
resp, err := c.Post(ctx, "/v1/tokens", p)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("Error when sending request to the daemon: %w.", err)
|
||||
}
|
||||
apiErr, err := resp.Decode(nil)
|
||||
return resp, apiErr, err
|
||||
}
|
||||
@@ -103,7 +103,7 @@ func (w *CommandWrapper) addTokenToCache(ctx context.Context, token string) bool
|
||||
return false
|
||||
}
|
||||
|
||||
apiErr, err := com.Add(ctx, client, keyringType, tokName)
|
||||
_, apiErr, err := com.Add(ctx, client, keyringType, tokName)
|
||||
return err == nil && apiErr == nil
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
stderrors "errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
@@ -136,7 +137,6 @@ func (c *StartCommand) AutocompleteFlags() complete.Flags {
|
||||
}
|
||||
|
||||
func (c *StartCommand) Run(args []string) int {
|
||||
const op = "daemon.(StartCommand).Run"
|
||||
ctx, cancel := context.WithCancel(c.Context)
|
||||
defer cancel()
|
||||
|
||||
@@ -265,9 +265,9 @@ func makeBackground(ctx context.Context, dotDir string, runBackgroundFlag bool)
|
||||
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")
|
||||
return false, writers, noopPidCleanup, stderrors.New("The daemon is already running.")
|
||||
} else if err != nil && !errors.Match(errors.T(errors.NotFound), err) {
|
||||
return false, writers, noopPidCleanup, errors.Wrap(ctx, err, op)
|
||||
return false, writers, noopPidCleanup, fmt.Errorf("Error when checking if the daemon pid is in use: %w.", err)
|
||||
}
|
||||
|
||||
if !runBackgroundFlag && os.Getenv(backgroundEnvName) != backgroundEnvVal {
|
||||
|
||||
@@ -12,9 +12,10 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/internal/clientcache/internal/client"
|
||||
"github.com/hashicorp/boundary/internal/clientcache/internal/daemon"
|
||||
"github.com/hashicorp/boundary/internal/cmd/base"
|
||||
"github.com/hashicorp/boundary/version"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/mitchellh/cli"
|
||||
"github.com/posener/complete"
|
||||
)
|
||||
@@ -69,14 +70,7 @@ func (c *StatusCommand) Run(args []string) int {
|
||||
return base.CommandUserError
|
||||
}
|
||||
|
||||
resp, err := c.Status(ctx)
|
||||
if err != nil {
|
||||
c.PrintCliError(err)
|
||||
return base.CommandCliError
|
||||
}
|
||||
|
||||
res := &daemon.StatusResult{}
|
||||
apiErr, err := resp.Decode(res)
|
||||
resp, result, apiErr, err := c.Status(ctx)
|
||||
if err != nil {
|
||||
c.PrintCliError(err)
|
||||
return base.CommandCliError
|
||||
@@ -84,56 +78,57 @@ func (c *StatusCommand) Run(args []string) int {
|
||||
if apiErr != nil {
|
||||
c.PrintApiError(apiErr, "Error from daemon when getting the status")
|
||||
return base.CommandApiError
|
||||
|
||||
}
|
||||
|
||||
switch base.Format(c.UI) {
|
||||
case "json":
|
||||
c.UI.Output(resp.Body.String())
|
||||
if ok := c.PrintJsonItem(resp); !ok {
|
||||
return base.CommandCliError
|
||||
}
|
||||
default:
|
||||
c.UI.Output(printStatusTable(res))
|
||||
c.UI.Output(printStatusTable(result))
|
||||
}
|
||||
return base.CommandSuccess
|
||||
}
|
||||
|
||||
func (c *StatusCommand) Status(ctx context.Context) (*api.Response, error) {
|
||||
func (c *StatusCommand) Status(ctx context.Context) (*api.Response, *daemon.StatusResult, *api.Error, error) {
|
||||
dotPath, err := DefaultDotDirectory(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
||||
return status(ctx, dotPath)
|
||||
}
|
||||
|
||||
func status(ctx context.Context, daemonPath string) (*api.Response, error) {
|
||||
client, err := api.NewClient(nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error creating a new API client: %w", err)
|
||||
}
|
||||
func status(ctx context.Context, daemonPath string) (*api.Response, *daemon.StatusResult, *api.Error, error) {
|
||||
const op = "daemon.status"
|
||||
addr, err := daemon.SocketAddress(daemonPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Error getting socket address: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("Error getting socket address: %w", err)
|
||||
}
|
||||
_, err = os.Stat(addr.Path)
|
||||
if addr.Scheme == "unix" && err != nil {
|
||||
return nil, errDaemonNotRunning
|
||||
return nil, nil, nil, errDaemonNotRunning
|
||||
}
|
||||
if err := client.SetAddr(addr.String()); err != nil {
|
||||
return nil, fmt.Errorf("Error when setting the client's address: %w", err)
|
||||
c, err := client.New(ctx, addr)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
// Because this is using the real lib it can pick up from stored locations
|
||||
// like the system keychain. Explicitly clear the token for now
|
||||
client.SetToken("")
|
||||
|
||||
req, err := client.NewRequest(ctx, "GET", "/status", nil)
|
||||
resp, err := c.Get(ctx, "/v1/status", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, nil, errors.Wrap(ctx, err, op, errors.WithMsg("client do failed"))
|
||||
}
|
||||
req.Header.Add(daemon.VersionHeaderKey, version.Get().VersionNumber())
|
||||
resp, err := client.Do(req)
|
||||
|
||||
res := &daemon.StatusResult{}
|
||||
apiErr, err := resp.Decode(&res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, nil, nil, fmt.Errorf("Error when sending request to the daemon: %w.", err)
|
||||
}
|
||||
return resp, nil
|
||||
if apiErr != nil {
|
||||
return resp, nil, apiErr, nil
|
||||
}
|
||||
return resp, res, nil, nil
|
||||
}
|
||||
|
||||
func printStatusTable(status *daemon.StatusResult) string {
|
||||
|
||||
@@ -60,7 +60,7 @@ func (c *StopCommand) Run(args []string) int {
|
||||
}
|
||||
if err := c.stop(c.Context); err != nil {
|
||||
if errors.Match(errors.T(errors.NotFound), err) {
|
||||
c.PrintCliError(stderr.New("deamon process is not running"))
|
||||
c.PrintCliError(stderr.New("The deamon process is not running."))
|
||||
} else {
|
||||
c.PrintCliError(err)
|
||||
}
|
||||
|
||||
@@ -8,35 +8,35 @@ package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"syscall"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/util"
|
||||
)
|
||||
|
||||
// stop will send a term signal to the daemon to shut down.
|
||||
func (s *StopCommand) stop(ctx context.Context) error {
|
||||
const op = "daemon.(StopCommand).stop"
|
||||
switch {
|
||||
case util.IsNil(ctx):
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "context is missing")
|
||||
return errors.New("Invalid parameter provided to stop: context is missing")
|
||||
}
|
||||
|
||||
dotPath, err := DefaultDotDirectory(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
return fmt.Errorf("Error when getting default dot directory: %w", err)
|
||||
}
|
||||
pidPath := filepath.Join(dotPath, pidFileName)
|
||||
p, err := pidFileInUse(ctx, pidPath)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
return fmt.Errorf("Error when checking if the daemon's pid file is in use: %w", err)
|
||||
}
|
||||
if p == nil {
|
||||
return errors.New(ctx, errors.NotFound, op, "daemon not running")
|
||||
return errors.New("The daemon is not running.")
|
||||
}
|
||||
if err := p.Signal(syscall.SIGTERM); err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("sending sigterm to process"))
|
||||
return fmt.Errorf("Error when sending sigterm to daemon process: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -8,28 +8,27 @@ package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/internal/clientcache/internal/client"
|
||||
"github.com/hashicorp/boundary/internal/clientcache/internal/daemon"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/util"
|
||||
"github.com/hashicorp/boundary/version"
|
||||
)
|
||||
|
||||
// stop will send a term signal to the daemon to shut down.
|
||||
func (s *StopCommand) stop(ctx context.Context) error {
|
||||
const op = "daemon.(StopCommand).stop"
|
||||
switch {
|
||||
case util.IsNil(ctx):
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "context is missing")
|
||||
return errors.New("Invalid parameter provided to stop: context is missing")
|
||||
}
|
||||
|
||||
dotPath, err := DefaultDotDirectory(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
return fmt.Errorf("Error when getting default dot directory: %w", err)
|
||||
}
|
||||
|
||||
apiErr, err := stopThroughHandler(ctx, dotPath)
|
||||
@@ -41,7 +40,7 @@ func (s *StopCommand) stop(ctx context.Context) error {
|
||||
} else if apiErr != nil {
|
||||
errMsg = apiErr.Message
|
||||
}
|
||||
s.UI.Warn(fmt.Sprintf("failed stopping the daemon through the handler: %q, now killing the process", errMsg))
|
||||
s.UI.Warn(fmt.Sprintf("Failed stopping the daemon through the handler: %q, now killing the process", errMsg))
|
||||
default:
|
||||
// there wasn't an error stopping it through the handler. No need to
|
||||
// force kill the process
|
||||
@@ -52,46 +51,34 @@ func (s *StopCommand) stop(ctx context.Context) error {
|
||||
p, err := pidFileInUse(ctx, pidPath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return errors.Wrap(ctx, err, op, errors.WithCode(errors.NotFound))
|
||||
return errors.New("Unable to stop the daemon: pid file not found.")
|
||||
}
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("Unable to stop the daemon"))
|
||||
return fmt.Errorf("Error when trying to stop the daemon: %w", err)
|
||||
}
|
||||
if p == nil {
|
||||
return errors.New(ctx, errors.NotFound, op, "daemon process was not found")
|
||||
return errors.New("Daemon process was not found.")
|
||||
}
|
||||
|
||||
if err := p.Kill(); err != nil {
|
||||
return errors.Wrap(ctx, err, op, errors.WithMsg("killing the daemon"))
|
||||
return fmt.Errorf("Error when killing the process: %w.", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func stopThroughHandler(ctx context.Context, dotPath string) (*api.Error, error) {
|
||||
const op = "daemon.stopThroughHandler"
|
||||
|
||||
sockAddr, err := daemon.SocketAddress(dotPath)
|
||||
addr, err := daemon.SocketAddress(dotPath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
return nil, fmt.Errorf("Error when retrieving the socket address: %w", err)
|
||||
}
|
||||
client, err := api.NewClient(nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if err := client.SetAddr(sockAddr.String()); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
// Because this is using the real lib it can pick up from stored locations
|
||||
// like the system keychain. Explicitly clear the token for now
|
||||
client.SetToken("")
|
||||
|
||||
req, err := client.NewRequest(ctx, "POST", "/stop", nil)
|
||||
c, err := client.New(ctx, addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Add(daemon.VersionHeaderKey, version.Get().VersionNumber())
|
||||
resp, err := client.Do(req)
|
||||
resp, err := c.Post(ctx, "/v1/stop", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("Error when sending request to the daemon: %w.", err)
|
||||
}
|
||||
return resp.Decode(&struct{}{})
|
||||
apiErr, err := resp.Decode(nil)
|
||||
return apiErr, err
|
||||
}
|
||||
Reference in new issue
Block a user