Add client daemon repo, server, and cmd

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
This commit is contained in:
Jim authored and Johan Brandhorst-Satzkorn committed 2024-01-05 07:43:20 -08:00
1 parent 6ba3676c37
commit fba9d1cdf1
71 files changed
+8980 -972

No files matched your search

@@ -0,0 +1,133 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package daemon
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/hashicorp/boundary/internal/clientcache/internal/cache"
"github.com/hashicorp/boundary/internal/cmd/base"
"github.com/hashicorp/boundary/internal/errors"
"github.com/hashicorp/boundary/internal/util"
)
type refresher interface {
refresh()
}
// KeyringToken has keyring held auth token information.
type KeyringToken struct {
// The keyring type used by boundary to access the auth token
KeyringType string
// The token identifier for the provided keyring type that holds the auth token
TokenName string
}
// userTokenToAdd is the request body to this handler.
type UpsertTokenRequest struct {
// BoundaryAddr is a required field for all requests
BoundaryAddr string
// The id of the auth token asserted to be attempted to be added
AuthTokenId string
// The raw auth token for this user. Either this field or the Keyring field
// must be set but not both.
AuthToken string
// Keyring is the keyring info used when adding an auth token held in
// keyring to the daemon.
Keyring *KeyringToken
}
func newTokenHandlerFunc(ctx context.Context, repo *cache.Repository, refresher refresher) (http.HandlerFunc, error) {
const op = "daemon.newPersonaHandlerFunc"
switch {
case util.IsNil(repo):
return nil, errors.New(ctx, errors.InvalidParameter, op, "repository is nil")
case util.IsNil(refresher):
return nil, errors.New(ctx, errors.InvalidParameter, op, "refresher is nil")
}
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
if r.Method != http.MethodPost {
writeError(w, "method not allowed", http.StatusMethodNotAllowed)
return
}
var perReq UpsertTokenRequest
data, err := io.ReadAll(r.Body)
if err != nil {
writeError(w, "unable to read request body", http.StatusBadRequest)
return
}
if err := json.Unmarshal(data, &perReq); err != nil {
writeError(w, "unable to parse request body", http.StatusBadRequest)
return
}
switch {
case perReq.BoundaryAddr == "":
writeError(w, "BoundaryAddr is a required field but was empty", http.StatusBadRequest)
return
case perReq.AuthTokenId == "":
writeError(w, "AuthTokenId is a required field but was empty", http.StatusBadRequest)
return
case perReq.Keyring == nil && perReq.AuthToken == "":
writeError(w, "Either keyring info or the authtoken must be provided but were empty", http.StatusBadRequest)
return
case perReq.Keyring != nil:
switch {
case perReq.Keyring.TokenName == "":
writeError(w, "TokenName is a required field but was empty", http.StatusBadRequest)
return
case perReq.Keyring.KeyringType == "":
writeError(w, "KeyringType is a required field but was empty", http.StatusBadRequest)
return
case perReq.Keyring.KeyringType == base.NoneKeyring:
writeError(w, fmt.Sprintf("KeyringType is set to %s which is not supported", perReq.Keyring.KeyringType), http.StatusBadRequest)
return
}
case perReq.AuthToken != "":
switch {
case !strings.HasPrefix(perReq.AuthToken, perReq.AuthTokenId):
writeError(w, "The auth token id doesn't match the auth token's prefix", http.StatusBadRequest)
return
}
}
tok, err := repo.LookupToken(ctx, perReq.AuthTokenId)
if err != nil {
writeError(w, "error performing auth token lookup", http.StatusInternalServerError)
return
}
switch {
case perReq.Keyring != nil:
kt := cache.KeyringToken{
KeyringType: perReq.Keyring.KeyringType,
TokenName: perReq.Keyring.TokenName,
AuthTokenId: perReq.AuthTokenId,
}
if err = repo.AddKeyringToken(ctx, perReq.BoundaryAddr, kt); err != nil {
writeError(w, "Failed to add a keyring stored token", http.StatusInternalServerError)
return
}
case perReq.AuthToken != "":
if err = repo.AddRawToken(ctx, perReq.BoundaryAddr, perReq.AuthToken); err != nil {
writeError(w, "Failed to add a raw token", http.StatusInternalServerError)
return
}
}
w.WriteHeader(http.StatusNoContent)
if tok == nil {
refresher.refresh()
}
}, nil
}