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:
1 parent
6ba3676c37
commit
fba9d1cdf1
71 files changed
+8980
-972
No files matched your search
+7
@@ -0,0 +1,7 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
/*
|
||||
Package cache contains the domain logic for the client cache.
|
||||
*/
|
||||
package cache
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"github.com/hashicorp/go-dbw"
|
||||
)
|
||||
|
||||
type options struct {
|
||||
withUpdateLastAccessedTime bool
|
||||
withDbType dbw.DbType
|
||||
withTargetRetrievalFunc TargetRetrievalFunc
|
||||
withSessionRetrievalFunc SessionRetrievalFunc
|
||||
}
|
||||
|
||||
// Option - how options are passed as args
|
||||
type Option func(*options) error
|
||||
|
||||
func getDefaultOptions() options {
|
||||
return options{
|
||||
withDbType: dbw.Sqlite,
|
||||
}
|
||||
}
|
||||
|
||||
func getOpts(opt ...Option) (options, error) {
|
||||
opts := getDefaultOptions()
|
||||
|
||||
for _, o := range opt {
|
||||
if err := o(&opts); err != nil {
|
||||
return opts, err
|
||||
}
|
||||
}
|
||||
return opts, nil
|
||||
}
|
||||
|
||||
// WithUpdateLastAccessedTime provides an option for updating the last access time
|
||||
func WithUpdateLastAccessedTime(b bool) Option {
|
||||
return func(o *options) error {
|
||||
o.withUpdateLastAccessedTime = b
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithTargetRetrievalFunc provides an option for specifying a targetRetrievalFunc
|
||||
func WithTargetRetrievalFunc(fn TargetRetrievalFunc) Option {
|
||||
return func(o *options) error {
|
||||
o.withTargetRetrievalFunc = fn
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// WithSessionRetrievalFunc provides an option for specifying a sessionRetrievalFunc
|
||||
func WithSessionRetrievalFunc(fn SessionRetrievalFunc) Option {
|
||||
return func(o *options) error {
|
||||
o.withSessionRetrievalFunc = fn
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/api/sessions"
|
||||
"github.com/hashicorp/boundary/api/targets"
|
||||
"github.com/hashicorp/go-dbw"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func Test_GetOpts(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
t.Run("default", func(t *testing.T) {
|
||||
opts, err := getOpts()
|
||||
require.NoError(t, err)
|
||||
testOpts := options{
|
||||
withDbType: dbw.Sqlite,
|
||||
}
|
||||
assert.Equal(t, opts, testOpts)
|
||||
})
|
||||
t.Run("WithUpdateLastAccessedTime", func(t *testing.T) {
|
||||
opts, err := getOpts(WithUpdateLastAccessedTime(true))
|
||||
require.NoError(t, err)
|
||||
testOpts := getDefaultOptions()
|
||||
testOpts.withUpdateLastAccessedTime = true
|
||||
assert.Equal(t, opts, testOpts)
|
||||
})
|
||||
t.Run("WithTargetRetrievalFunc", func(t *testing.T) {
|
||||
var f TargetRetrievalFunc = func(ctx context.Context, keyringstring, tokenName string) ([]*targets.Target, error) { return nil, nil }
|
||||
opts, err := getOpts(WithTargetRetrievalFunc(f))
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.NotNil(t, opts.withTargetRetrievalFunc)
|
||||
opts.withTargetRetrievalFunc = nil
|
||||
|
||||
testOpts := getDefaultOptions()
|
||||
assert.Equal(t, opts, testOpts)
|
||||
})
|
||||
t.Run("WithSessionRetrievalFunc", func(t *testing.T) {
|
||||
var f SessionRetrievalFunc = func(ctx context.Context, keyringstring, tokenName string) ([]*sessions.Session, error) {
|
||||
return nil, nil
|
||||
}
|
||||
opts, err := getOpts(WithSessionRetrievalFunc(f))
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.NotNil(t, opts.withSessionRetrievalFunc)
|
||||
opts.withSessionRetrievalFunc = nil
|
||||
|
||||
testOpts := getDefaultOptions()
|
||||
assert.Equal(t, opts, testOpts)
|
||||
})
|
||||
}
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
stderrors "errors"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/authtokens"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/event"
|
||||
"github.com/hashicorp/boundary/internal/util"
|
||||
)
|
||||
|
||||
type RefreshService struct {
|
||||
repo *Repository
|
||||
}
|
||||
|
||||
func NewRefreshService(ctx context.Context, r *Repository) (*RefreshService, error) {
|
||||
return &RefreshService{repo: r}, nil
|
||||
}
|
||||
|
||||
// cleanAndPickAuthTokens removes from the cache all auth tokens which are
|
||||
// evicted from the cache or no longer stored in a keyring and returns the
|
||||
// remaining ones.
|
||||
// The returned auth tokens have not been validated against boundary
|
||||
func (r *RefreshService) cleanAndPickAuthTokens(ctx context.Context, u *user) (map[AuthToken]string, error) {
|
||||
const op = "cache.(RefreshService).cleanAndPickAuthTokens"
|
||||
switch {
|
||||
case util.IsNil(u):
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "user is nil")
|
||||
}
|
||||
ret := make(map[AuthToken]string)
|
||||
|
||||
tokens, err := r.repo.listTokens(ctx, u)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
for _, t := range tokens {
|
||||
keyringTokens, err := r.repo.listKeyringTokens(ctx, t)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("for user %v, auth token %q", u, t.Id))
|
||||
}
|
||||
for _, kt := range keyringTokens {
|
||||
at := r.repo.tokenKeyringFn(kt.KeyringType, kt.TokenName)
|
||||
switch {
|
||||
case at == nil, at.Id != kt.AuthTokenId, at.UserId != t.UserId:
|
||||
// delete the keyring token if the auth token in the keyring
|
||||
// has changed since it was stored in the cache.
|
||||
if err := r.repo.deleteKeyringToken(ctx, *kt); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
case at != nil:
|
||||
_, err := r.repo.tokenReadFromBoundaryFn(ctx, u.Address, at.Token)
|
||||
var apiErr *api.Error
|
||||
switch {
|
||||
case err != nil && api.ErrUnauthorized.Is(err):
|
||||
if err := r.repo.deleteKeyringToken(ctx, *kt); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
continue
|
||||
case err != nil && !errors.Is(err, apiErr):
|
||||
event.WriteError(ctx, op, err, event.WithInfoMsg("validating keyring stored token against boundary", "auth token id", at.Id))
|
||||
continue
|
||||
}
|
||||
ret[*t] = at.Token
|
||||
}
|
||||
}
|
||||
if atv, ok := r.repo.idToKeyringlessAuthToken.Load(t.Id); ok {
|
||||
if at, ok := atv.(*authtokens.AuthToken); ok {
|
||||
_, err := r.repo.tokenReadFromBoundaryFn(ctx, u.Address, at.Token)
|
||||
var apiErr *api.Error
|
||||
switch {
|
||||
case err != nil && api.ErrUnauthorized.Is(err):
|
||||
r.repo.idToKeyringlessAuthToken.Delete(t.Id)
|
||||
continue
|
||||
case err != nil && !errors.Is(err, apiErr):
|
||||
event.WriteError(ctx, op, err, event.WithInfoMsg("validating in memory stored token against boundary", "auth token id", at.Id))
|
||||
continue
|
||||
}
|
||||
|
||||
ret[*t] = at.Token
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// Refresh iterates over all tokens in the cache, attempts to read the
|
||||
// resources for those tokens from boundary and updates the cache with
|
||||
// the values retrieved there. Refresh accepts the options
|
||||
// WithTarget which overwrites the default function used to retrieve the
|
||||
// targets from a boundary address.
|
||||
func (r *RefreshService) Refresh(ctx context.Context, opt ...Option) error {
|
||||
const op = "cache.(RefreshService).Refresh"
|
||||
if err := r.repo.cleanExpiredOrOrphanedAuthTokens(ctx); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if err := r.repo.syncKeyringlessTokensWithDb(ctx); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
us, err := r.repo.listUsers(ctx)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
var retErr error
|
||||
for _, u := range us {
|
||||
tokens, err := r.cleanAndPickAuthTokens(ctx, u)
|
||||
if err != nil {
|
||||
retErr = stderrors.Join(retErr, errors.Wrap(ctx, err, op))
|
||||
continue
|
||||
}
|
||||
|
||||
if err := r.repo.refreshTargets(ctx, u, tokens, opt...); err != nil {
|
||||
retErr = stderrors.Join(retErr, errors.Wrap(ctx, err, op))
|
||||
}
|
||||
if err := r.repo.refreshSessions(ctx, u, tokens, opt...); err != nil {
|
||||
retErr = stderrors.Join(retErr, errors.Wrap(ctx, err, op))
|
||||
}
|
||||
|
||||
}
|
||||
return retErr
|
||||
}
|
||||
+417
@@ -0,0 +1,417 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/authtokens"
|
||||
"github.com/hashicorp/boundary/api/sessions"
|
||||
"github.com/hashicorp/boundary/api/targets"
|
||||
"github.com/hashicorp/boundary/internal/clientcache/internal/db"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
// testStaticResourceRetrievalFunc returns a function that always returns the
|
||||
// provided slice and a nil error. The returned function can be passed into the
|
||||
// options that provide a resource retrieval func such as
|
||||
// WithTargetRetrievalFunc and WithSessionRetrievalFunc.
|
||||
func testStaticResourceRetrievalFunc[T any](ret []T) func(context.Context, string, string) ([]T, error) {
|
||||
return func(ctx context.Context, s1, s2 string) ([]T, error) {
|
||||
return ret, nil
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanAndPickTokens(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := db.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
boundaryAddr := "address"
|
||||
u1 := &user{Id: "u1", Address: boundaryAddr}
|
||||
at1a := &authtokens.AuthToken{
|
||||
Id: "at_1a",
|
||||
Token: "at_1a_token",
|
||||
UserId: u1.Id,
|
||||
ExpirationTime: time.Now().Add(time.Minute),
|
||||
}
|
||||
at1b := &authtokens.AuthToken{
|
||||
Id: "at_1b",
|
||||
Token: "at_1b_token",
|
||||
UserId: u1.Id,
|
||||
ExpirationTime: time.Now().Add(time.Minute),
|
||||
}
|
||||
|
||||
keyringOnlyUser := &user{Id: "keyringUser", Address: boundaryAddr}
|
||||
keyringAuthToken1 := &authtokens.AuthToken{
|
||||
Id: "at_2a",
|
||||
Token: "at_2a_token",
|
||||
UserId: keyringOnlyUser.Id,
|
||||
ExpirationTime: time.Now().Add(time.Minute),
|
||||
}
|
||||
keyringAuthToken2 := &authtokens.AuthToken{
|
||||
Id: "at_2b",
|
||||
Token: "at_2b_token",
|
||||
UserId: keyringOnlyUser.Id,
|
||||
ExpirationTime: time.Now().Add(time.Minute),
|
||||
}
|
||||
|
||||
boundaryAuthTokens := []*authtokens.AuthToken{at1a, keyringAuthToken1, at1b, keyringAuthToken2}
|
||||
unauthorizedAuthTokens := []*authtokens.AuthToken{}
|
||||
randomErrorAuthTokens := []*authtokens.AuthToken{}
|
||||
fakeBoundaryLookupFn := func(ctx context.Context, addr, at string) (*authtokens.AuthToken, error) {
|
||||
for _, v := range randomErrorAuthTokens {
|
||||
if at == v.Token {
|
||||
return nil, errors.New("test error")
|
||||
}
|
||||
}
|
||||
for _, v := range unauthorizedAuthTokens {
|
||||
if at == v.Token {
|
||||
return nil, api.ErrUnauthorized
|
||||
}
|
||||
}
|
||||
for _, v := range boundaryAuthTokens {
|
||||
if at == v.Token {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return nil, errors.New("not found")
|
||||
}
|
||||
|
||||
atMap := make(map[ringToken]*authtokens.AuthToken)
|
||||
r, err := NewRepository(ctx, s, &sync.Map{},
|
||||
mapBasedAuthTokenKeyringLookup(atMap),
|
||||
fakeBoundaryLookupFn)
|
||||
require.NoError(t, err)
|
||||
rs, err := NewRefreshService(ctx, r)
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("unknown user", func(t *testing.T) {
|
||||
got, err := rs.cleanAndPickAuthTokens(ctx, &user{Id: "unknownuser", Address: "unknown"})
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, got)
|
||||
})
|
||||
|
||||
t.Run("both memory and keyring stored token", func(t *testing.T) {
|
||||
key := ringToken{"k1", "t1"}
|
||||
atMap[key] = at1a
|
||||
require.NoError(t, r.AddKeyringToken(ctx, boundaryAddr, KeyringToken{
|
||||
KeyringType: key.k,
|
||||
TokenName: key.t,
|
||||
AuthTokenId: at1a.Id,
|
||||
}))
|
||||
require.NoError(t, r.AddRawToken(ctx, boundaryAddr, at1b.Token))
|
||||
|
||||
got, err := rs.cleanAndPickAuthTokens(ctx, u1)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, maps.Values(got), []string{at1a.Token, at1b.Token})
|
||||
|
||||
// delete the keyringToken from the keyring and see it get removed from the response
|
||||
delete(atMap, key)
|
||||
got, err = rs.cleanAndPickAuthTokens(ctx, u1)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, maps.Values(got), []string{at1b.Token})
|
||||
})
|
||||
|
||||
t.Run("2 memory tokens", func(t *testing.T) {
|
||||
require.NoError(t, r.AddRawToken(ctx, boundaryAddr, at1a.Token))
|
||||
require.NoError(t, r.AddRawToken(ctx, boundaryAddr, at1b.Token))
|
||||
|
||||
got, err := rs.cleanAndPickAuthTokens(ctx, u1)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, maps.Values(got), []string{at1a.Token, at1b.Token})
|
||||
})
|
||||
|
||||
t.Run("boundary in memory auth token expires", func(t *testing.T) {
|
||||
require.NoError(t, r.AddRawToken(ctx, boundaryAddr, at1a.Token))
|
||||
require.NoError(t, r.AddRawToken(ctx, boundaryAddr, at1b.Token))
|
||||
|
||||
got, err := rs.cleanAndPickAuthTokens(ctx, u1)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, maps.Values(got), []string{at1a.Token, at1b.Token})
|
||||
|
||||
t.Cleanup(func() {
|
||||
unauthorizedAuthTokens = nil
|
||||
})
|
||||
|
||||
unauthorizedAuthTokens = []*authtokens.AuthToken{at1b}
|
||||
got, err = rs.cleanAndPickAuthTokens(ctx, u1)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, maps.Values(got), []string{at1a.Token})
|
||||
})
|
||||
|
||||
t.Run("boundary keyring auths token expires", func(t *testing.T) {
|
||||
key1 := ringToken{"k1", "t1"}
|
||||
atMap[key1] = keyringAuthToken1
|
||||
require.NoError(t, r.AddKeyringToken(ctx, boundaryAddr, KeyringToken{
|
||||
KeyringType: key1.k,
|
||||
TokenName: key1.t,
|
||||
AuthTokenId: keyringAuthToken1.Id,
|
||||
}))
|
||||
key2 := ringToken{"k2", "t2"}
|
||||
atMap[key2] = keyringAuthToken2
|
||||
require.NoError(t, r.AddKeyringToken(ctx, boundaryAddr, KeyringToken{
|
||||
KeyringType: key2.k,
|
||||
TokenName: key2.t,
|
||||
AuthTokenId: keyringAuthToken2.Id,
|
||||
}))
|
||||
|
||||
got, err := rs.cleanAndPickAuthTokens(ctx, keyringOnlyUser)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, maps.Values(got), []string{keyringAuthToken1.Token, keyringAuthToken2.Token})
|
||||
|
||||
t.Cleanup(func() {
|
||||
unauthorizedAuthTokens = nil
|
||||
})
|
||||
|
||||
unauthorizedAuthTokens = []*authtokens.AuthToken{keyringAuthToken2}
|
||||
got, err = rs.cleanAndPickAuthTokens(ctx, keyringOnlyUser)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, maps.Values(got), []string{keyringAuthToken1.Token})
|
||||
})
|
||||
|
||||
t.Run("boundary in memory auth token check errors", func(t *testing.T) {
|
||||
require.NoError(t, r.AddRawToken(ctx, boundaryAddr, at1a.Token))
|
||||
require.NoError(t, r.AddRawToken(ctx, boundaryAddr, at1b.Token))
|
||||
|
||||
got, err := rs.cleanAndPickAuthTokens(ctx, u1)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, maps.Values(got), []string{at1a.Token, at1b.Token})
|
||||
|
||||
t.Cleanup(func() {
|
||||
randomErrorAuthTokens = nil
|
||||
})
|
||||
|
||||
randomErrorAuthTokens = []*authtokens.AuthToken{at1b}
|
||||
got, err = rs.cleanAndPickAuthTokens(ctx, u1)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, maps.Values(got), []string{at1a.Token})
|
||||
})
|
||||
|
||||
t.Run("boundary keyring auths token check errors", func(t *testing.T) {
|
||||
key1 := ringToken{"k1", "t1"}
|
||||
atMap[key1] = keyringAuthToken1
|
||||
require.NoError(t, r.AddKeyringToken(ctx, boundaryAddr, KeyringToken{
|
||||
KeyringType: key1.k,
|
||||
TokenName: key1.t,
|
||||
AuthTokenId: keyringAuthToken1.Id,
|
||||
}))
|
||||
key2 := ringToken{"k2", "t2"}
|
||||
atMap[key2] = keyringAuthToken2
|
||||
require.NoError(t, r.AddKeyringToken(ctx, boundaryAddr, KeyringToken{
|
||||
KeyringType: key2.k,
|
||||
TokenName: key2.t,
|
||||
AuthTokenId: keyringAuthToken2.Id,
|
||||
}))
|
||||
|
||||
got, err := rs.cleanAndPickAuthTokens(ctx, keyringOnlyUser)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, maps.Values(got), []string{keyringAuthToken1.Token, keyringAuthToken2.Token})
|
||||
|
||||
t.Cleanup(func() {
|
||||
randomErrorAuthTokens = nil
|
||||
})
|
||||
|
||||
randomErrorAuthTokens = []*authtokens.AuthToken{keyringAuthToken2}
|
||||
got, err = rs.cleanAndPickAuthTokens(ctx, keyringOnlyUser)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, maps.Values(got), []string{keyringAuthToken1.Token})
|
||||
})
|
||||
|
||||
t.Run("2 keyring tokens", func(t *testing.T) {
|
||||
key1 := ringToken{"k1", "t1"}
|
||||
atMap[key1] = keyringAuthToken1
|
||||
require.NoError(t, r.AddKeyringToken(ctx, boundaryAddr, KeyringToken{
|
||||
KeyringType: key1.k,
|
||||
TokenName: key1.t,
|
||||
AuthTokenId: keyringAuthToken1.Id,
|
||||
}))
|
||||
key2 := ringToken{"k2", "t2"}
|
||||
atMap[key2] = keyringAuthToken2
|
||||
require.NoError(t, r.AddKeyringToken(ctx, boundaryAddr, KeyringToken{
|
||||
KeyringType: key2.k,
|
||||
TokenName: key2.t,
|
||||
AuthTokenId: keyringAuthToken2.Id,
|
||||
}))
|
||||
|
||||
got, err := rs.cleanAndPickAuthTokens(ctx, keyringOnlyUser)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, maps.Values(got), []string{keyringAuthToken1.Token, keyringAuthToken2.Token})
|
||||
|
||||
// Removing all keyring references and then cleaning auth tokens
|
||||
// removes all auth tokens, along with the user
|
||||
gotU, err := r.listUsers(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, gotU, keyringOnlyUser)
|
||||
|
||||
delete(atMap, key1)
|
||||
delete(atMap, key2)
|
||||
got, err = rs.cleanAndPickAuthTokens(ctx, keyringOnlyUser)
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, got)
|
||||
|
||||
gotT, err := r.listTokens(ctx, keyringOnlyUser)
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, gotT)
|
||||
gotU, err = r.listUsers(ctx)
|
||||
assert.NoError(t, err)
|
||||
assert.NotContains(t, gotU, keyringOnlyUser)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRefresh(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := db.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
boundaryAddr := "address"
|
||||
u := &user{Id: "u1", Address: boundaryAddr}
|
||||
at := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: u.Id,
|
||||
ExpirationTime: time.Now().Add(time.Minute),
|
||||
}
|
||||
|
||||
boundaryAuthTokens := []*authtokens.AuthToken{at}
|
||||
atMap := make(map[ringToken]*authtokens.AuthToken)
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(boundaryAuthTokens))
|
||||
require.NoError(t, err)
|
||||
rs, err := NewRefreshService(ctx, r)
|
||||
require.NoError(t, err)
|
||||
|
||||
atMap[ringToken{"k", "t"}] = at
|
||||
require.NoError(t, r.AddKeyringToken(ctx, boundaryAddr, KeyringToken{KeyringType: "k", TokenName: "t", AuthTokenId: at.Id}))
|
||||
|
||||
t.Run("set targets", func(t *testing.T) {
|
||||
retTargets := []*targets.Target{
|
||||
target("1"),
|
||||
target("2"),
|
||||
target("3"),
|
||||
}
|
||||
assert.NoError(t, rs.Refresh(ctx,
|
||||
WithSessionRetrievalFunc(testStaticResourceRetrievalFunc[*sessions.Session](nil)),
|
||||
WithTargetRetrievalFunc(func(ctx context.Context, addr, token string) ([]*targets.Target, error) {
|
||||
require.Equal(t, boundaryAddr, addr)
|
||||
require.Equal(t, at.Token, token)
|
||||
return retTargets, nil
|
||||
})))
|
||||
|
||||
cachedTargets, err := r.ListTargets(ctx, at.Id)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, retTargets, cachedTargets)
|
||||
|
||||
t.Run("empty response clears it out", func(t *testing.T) {
|
||||
assert.NoError(t, rs.Refresh(ctx,
|
||||
WithSessionRetrievalFunc(testStaticResourceRetrievalFunc[*sessions.Session](nil)),
|
||||
WithTargetRetrievalFunc(func(ctx context.Context, addr, token string) ([]*targets.Target, error) {
|
||||
require.Equal(t, boundaryAddr, addr)
|
||||
require.Equal(t, at.Token, token)
|
||||
return nil, nil
|
||||
})))
|
||||
|
||||
cachedTargets, err := r.ListTargets(ctx, at.Id)
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, cachedTargets)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("set sessions", func(t *testing.T) {
|
||||
retSess := []*sessions.Session{
|
||||
session("1"),
|
||||
session("2"),
|
||||
session("3"),
|
||||
}
|
||||
assert.NoError(t, rs.Refresh(ctx,
|
||||
WithTargetRetrievalFunc(testStaticResourceRetrievalFunc[*targets.Target](nil)),
|
||||
WithSessionRetrievalFunc(testStaticResourceRetrievalFunc(retSess))))
|
||||
|
||||
cachedSessions, err := r.ListSessions(ctx, at.Id)
|
||||
assert.NoError(t, err)
|
||||
assert.ElementsMatch(t, retSess, cachedSessions)
|
||||
|
||||
t.Run("empty response clears it out", func(t *testing.T) {
|
||||
assert.NoError(t, rs.Refresh(ctx,
|
||||
WithTargetRetrievalFunc(testStaticResourceRetrievalFunc[*targets.Target](nil)),
|
||||
WithSessionRetrievalFunc(testStaticResourceRetrievalFunc[*sessions.Session](nil))))
|
||||
|
||||
cachedTargets, err := r.ListSessions(ctx, at.Id)
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, cachedTargets)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("error propogates up", func(t *testing.T) {
|
||||
innerErr := errors.New("test error")
|
||||
err := rs.Refresh(ctx,
|
||||
WithSessionRetrievalFunc(testStaticResourceRetrievalFunc[*sessions.Session](nil)),
|
||||
WithTargetRetrievalFunc(func(ctx context.Context, addr, token string) ([]*targets.Target, error) {
|
||||
require.Equal(t, boundaryAddr, addr)
|
||||
require.Equal(t, at.Token, token)
|
||||
return nil, innerErr
|
||||
}))
|
||||
assert.ErrorContains(t, err, innerErr.Error())
|
||||
err = rs.Refresh(ctx,
|
||||
WithTargetRetrievalFunc(testStaticResourceRetrievalFunc[*targets.Target](nil)),
|
||||
WithSessionRetrievalFunc(func(ctx context.Context, addr, token string) ([]*sessions.Session, error) {
|
||||
require.Equal(t, boundaryAddr, addr)
|
||||
require.Equal(t, at.Token, token)
|
||||
return nil, innerErr
|
||||
}))
|
||||
assert.ErrorContains(t, err, innerErr.Error())
|
||||
})
|
||||
|
||||
t.Run("tokens that are no longer in the ring is deleted", func(t *testing.T) {
|
||||
// Remove the token from the keyring, see that we can still see the
|
||||
// token and then user until a Refresh happens which causes them to be
|
||||
// cleaned up.
|
||||
delete(atMap, ringToken{"k", "t"})
|
||||
|
||||
ps, err := r.listTokens(ctx, u)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, ps, 1)
|
||||
|
||||
us, err := r.listUsers(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, us, 1)
|
||||
|
||||
rs.Refresh(ctx,
|
||||
WithSessionRetrievalFunc(testStaticResourceRetrievalFunc[*sessions.Session](nil)),
|
||||
WithTargetRetrievalFunc(testStaticResourceRetrievalFunc[*targets.Target](nil)))
|
||||
|
||||
ps, err = r.listTokens(ctx, u)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, ps)
|
||||
|
||||
// And since the last token was deleted, the user also was deleted
|
||||
us, err = r.listUsers(ctx)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, us)
|
||||
})
|
||||
}
|
||||
|
||||
func target(suffix string) *targets.Target {
|
||||
return &targets.Target{
|
||||
Id: fmt.Sprintf("target_%s", suffix),
|
||||
Name: fmt.Sprintf("name_%s", suffix),
|
||||
Description: fmt.Sprintf("description_%s", suffix),
|
||||
Address: fmt.Sprintf("address_%s", suffix),
|
||||
Type: "tcp",
|
||||
}
|
||||
}
|
||||
|
||||
func session(suffix string) *sessions.Session {
|
||||
return &sessions.Session{
|
||||
Id: fmt.Sprintf("session_%s", suffix),
|
||||
Type: "tcp",
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/api/authtokens"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/util"
|
||||
)
|
||||
|
||||
const (
|
||||
usersLimit = 50
|
||||
tokenStalenessLimit = 36 * time.Hour
|
||||
)
|
||||
|
||||
// KeyringTokenLookupFn takes a token name and returns the token from the keyring
|
||||
type KeyringTokenLookupFn func(keyring string, tokenName string) *authtokens.AuthToken
|
||||
|
||||
// BoundaryTokenReaderFn reads an auth token's resource information from boundary
|
||||
type BoundaryTokenReaderFn func(ctx context.Context, addr string, authToken string) (*authtokens.AuthToken, error)
|
||||
|
||||
type Repository struct {
|
||||
rw *db.Db
|
||||
tokenKeyringFn KeyringTokenLookupFn
|
||||
tokenReadFromBoundaryFn BoundaryTokenReaderFn
|
||||
// idToKeyringlessAuthToken maps an auth token id to an *authtokens.AuthToken
|
||||
idToKeyringlessAuthToken *sync.Map
|
||||
}
|
||||
|
||||
// NewRepository returns a cache repository. The provided Store must be
|
||||
func NewRepository(ctx context.Context, conn *db.DB, idToAuthToken *sync.Map, keyringFn KeyringTokenLookupFn, atReadFn BoundaryTokenReaderFn, opt ...Option) (*Repository, error) {
|
||||
const op = "cache.NewRepository"
|
||||
switch {
|
||||
case util.IsNil(conn):
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing store connection")
|
||||
case util.IsNil(idToAuthToken):
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing keyringless auth token map")
|
||||
case util.IsNil(keyringFn):
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing token keyring function")
|
||||
case util.IsNil(atReadFn):
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing auth token read function")
|
||||
}
|
||||
return &Repository{
|
||||
rw: db.New(conn),
|
||||
tokenKeyringFn: keyringFn,
|
||||
tokenReadFromBoundaryFn: atReadFn,
|
||||
// This is passed in instead of being fully owned by the repo so multiple
|
||||
// instances of the repo can operate on the same backing data
|
||||
idToKeyringlessAuthToken: idToAuthToken,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *Repository) SaveError(ctx context.Context, u *user, resourceType string, err error) error {
|
||||
const op = "cache.(Repository).StoreError"
|
||||
switch {
|
||||
case resourceType == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "resource type is empty")
|
||||
case err == nil:
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "error is nil")
|
||||
case u == nil:
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "user is nil")
|
||||
case u.Id == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "user id is empty")
|
||||
}
|
||||
apiErr := &ApiError{
|
||||
UserId: u.Id,
|
||||
ResourceType: resourceType,
|
||||
Error: err.Error(),
|
||||
}
|
||||
onConflict := db.OnConflict{
|
||||
Target: db.Columns{"user_id", "resource_type"},
|
||||
Action: db.SetColumns([]string{"error", "create_time"}),
|
||||
}
|
||||
if err := r.rw.Create(ctx, apiErr, db.WithOnConflict(&onConflict)); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type ApiError struct {
|
||||
UserId string `gorm:"primaryKey"`
|
||||
ResourceType string `gorm:"primaryKey"`
|
||||
Error string
|
||||
CreateTime time.Time
|
||||
}
|
||||
|
||||
func (*ApiError) TableName() string {
|
||||
return "api_error"
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
stderrors "errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/sessions"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/event"
|
||||
"github.com/hashicorp/boundary/internal/types/resource"
|
||||
"github.com/hashicorp/boundary/internal/util"
|
||||
"github.com/hashicorp/mql"
|
||||
)
|
||||
|
||||
// SessionRetrievalFunc is a function that retrieves sessions
|
||||
// from the provided boundary addr using the provided token.
|
||||
type SessionRetrievalFunc func(ctx context.Context, addr, token string) ([]*sessions.Session, error)
|
||||
|
||||
func defaultSessionFunc(ctx context.Context, addr, token string) ([]*sessions.Session, error) {
|
||||
const op = "cache.defaultSessionFunc"
|
||||
client, err := api.NewClient(&api.Config{
|
||||
Addr: addr,
|
||||
Token: token,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
sClient := sessions.NewClient(client)
|
||||
l, err := sClient.List(ctx, "global", sessions.WithRecursive(true))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return l.Items, nil
|
||||
}
|
||||
|
||||
func (r *Repository) refreshSessions(ctx context.Context, u *user, tokens map[AuthToken]string, opt ...Option) error {
|
||||
const op = "cache.(Repository).refreshSessions"
|
||||
switch {
|
||||
case util.IsNil(u):
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "user is nil")
|
||||
case u.Id == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "user id is missing")
|
||||
case u.Address == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "user boundary address is missing")
|
||||
}
|
||||
|
||||
opts, err := getOpts(opt...)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if opts.withSessionRetrievalFunc == nil {
|
||||
opts.withSessionRetrievalFunc = defaultSessionFunc
|
||||
}
|
||||
|
||||
// Find and use a token for retrieving sessions
|
||||
var gotResponse bool
|
||||
var resp []*sessions.Session
|
||||
var retErr error
|
||||
for at, t := range tokens {
|
||||
resp, err = opts.withSessionRetrievalFunc(ctx, u.Address, t)
|
||||
if err != nil {
|
||||
// TODO: If we get an error about the token no longer having
|
||||
// permissions, remove it.
|
||||
retErr = stderrors.Join(retErr, errors.Wrap(ctx, err, op, errors.WithMsg("for token %q", at.Id)))
|
||||
continue
|
||||
}
|
||||
gotResponse = true
|
||||
break
|
||||
}
|
||||
|
||||
if retErr != nil {
|
||||
if saveErr := r.SaveError(ctx, u, resource.Session.String(), retErr); saveErr != nil {
|
||||
return stderrors.Join(err, errors.Wrap(ctx, saveErr, op))
|
||||
}
|
||||
}
|
||||
if !gotResponse {
|
||||
return retErr
|
||||
}
|
||||
|
||||
event.WriteSysEvent(ctx, op, fmt.Sprintf("updating %d sessions for user %v", len(resp), u))
|
||||
_, err = r.rw.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(r db.Reader, w db.Writer) error {
|
||||
// TODO: Instead of deleting everything, use refresh tokens and apply the delta
|
||||
if _, err := w.Exec(ctx, "delete from session where user_id = @user_id",
|
||||
[]any{sql.Named("user_id", u.Id)}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, s := range resp {
|
||||
item, err := json.Marshal(s)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newSession := &Session{
|
||||
UserId: u.Id,
|
||||
Id: s.Id,
|
||||
Type: s.Type,
|
||||
Status: s.Status,
|
||||
Endpoint: s.Endpoint,
|
||||
Item: string(item),
|
||||
}
|
||||
onConflict := db.OnConflict{
|
||||
Target: db.Columns{"user_id", "id"},
|
||||
Action: db.UpdateAll(true),
|
||||
}
|
||||
if err := w.Create(ctx, newSession, db.WithOnConflict(&onConflict)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) ListSessions(ctx context.Context, authTokenId string) ([]*sessions.Session, error) {
|
||||
const op = "cache.(Repository).ListSessions"
|
||||
switch {
|
||||
case authTokenId == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "auth token id is missing")
|
||||
}
|
||||
ret, err := r.searchSessions(ctx, authTokenId, "true", nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (r *Repository) QuerySessions(ctx context.Context, authTokenId, query string) ([]*sessions.Session, error) {
|
||||
const op = "cache.(Repository).QuerySessions"
|
||||
switch {
|
||||
case authTokenId == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "auth token id is missing")
|
||||
case query == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "query is missing")
|
||||
}
|
||||
|
||||
w, err := mql.Parse(query, Session{}, mql.WithIgnoredFields("UserId", "Item"))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.InvalidParameter))
|
||||
}
|
||||
ret, err := r.searchSessions(ctx, authTokenId, w.Condition, w.Args)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (r *Repository) searchSessions(ctx context.Context, authTokenId, condition string, searchArgs []any) ([]*sessions.Session, error) {
|
||||
const op = "cache.(Repository).searchSessions"
|
||||
switch {
|
||||
case authTokenId == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "auth token id is missing")
|
||||
case condition == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "condition is missing")
|
||||
}
|
||||
|
||||
condition = fmt.Sprintf("%s and user_id in (select user_id from auth_token where id = ?)", condition)
|
||||
args := append(searchArgs, authTokenId)
|
||||
var cachedSessions []*Session
|
||||
if err := r.rw.SearchWhere(ctx, &cachedSessions, condition, args, db.WithLimit(-1)); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
retSessions := make([]*sessions.Session, 0, len(cachedSessions))
|
||||
for _, cachedSess := range cachedSessions {
|
||||
var sess sessions.Session
|
||||
if err := json.Unmarshal([]byte(cachedSess.Item), &sess); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
retSessions = append(retSessions, &sess)
|
||||
}
|
||||
return retSessions, nil
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
UserId string `gorm:"primaryKey"`
|
||||
Id string `gorm:"primaryKey"`
|
||||
Type string `gorm:"default:null"`
|
||||
Endpoint string `gorm:"default:null"`
|
||||
Status string `gorm:"default:null"`
|
||||
Item string `gorm:"default:null"`
|
||||
}
|
||||
|
||||
func (*Session) TableName() string {
|
||||
return "session"
|
||||
}
|
||||
@@ -0,0 +1,343 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/api/authtokens"
|
||||
"github.com/hashicorp/boundary/api/sessions"
|
||||
"github.com/hashicorp/boundary/api/targets"
|
||||
cachedb "github.com/hashicorp/boundary/internal/clientcache/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller"
|
||||
"github.com/hashicorp/boundary/internal/daemon/worker"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/exp/maps"
|
||||
|
||||
_ "github.com/hashicorp/boundary/internal/daemon/controller/handlers/targets/tcp"
|
||||
)
|
||||
|
||||
func TestRepository_refreshSessions(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
u := user{
|
||||
Id: "u1",
|
||||
Address: addr,
|
||||
}
|
||||
at := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: u.Id,
|
||||
}
|
||||
kt := KeyringToken{
|
||||
KeyringType: "keyring",
|
||||
TokenName: "token",
|
||||
AuthTokenId: at.Id,
|
||||
}
|
||||
atMap := map[ringToken]*authtokens.AuthToken{
|
||||
{kt.KeyringType, kt.TokenName}: at,
|
||||
}
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(maps.Values(atMap)))
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, r.AddKeyringToken(ctx, addr, kt))
|
||||
|
||||
ss := []*sessions.Session{
|
||||
{
|
||||
Id: "ttcp_1",
|
||||
Status: "status1",
|
||||
Endpoint: "address1",
|
||||
Type: "tcp",
|
||||
},
|
||||
{
|
||||
Id: "ttcp_2",
|
||||
Status: "status2",
|
||||
Endpoint: "address2",
|
||||
Type: "tcp",
|
||||
},
|
||||
{
|
||||
Id: "ttcp_3",
|
||||
Status: "status3",
|
||||
Endpoint: "address3",
|
||||
Type: "tcp",
|
||||
},
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
u *user
|
||||
sess []*sessions.Session
|
||||
wantCount int
|
||||
errorContains string
|
||||
}{
|
||||
{
|
||||
name: "Success",
|
||||
u: &user{
|
||||
Address: addr,
|
||||
Id: at.UserId,
|
||||
},
|
||||
sess: ss,
|
||||
wantCount: len(ss),
|
||||
},
|
||||
{
|
||||
name: "repeated session with different values",
|
||||
u: &user{
|
||||
Address: addr,
|
||||
Id: at.UserId,
|
||||
},
|
||||
sess: append(ss, &sessions.Session{
|
||||
Id: ss[0].Id,
|
||||
Status: "a different status",
|
||||
}),
|
||||
wantCount: len(ss),
|
||||
},
|
||||
{
|
||||
name: "nil user",
|
||||
u: nil,
|
||||
sess: ss,
|
||||
errorContains: "user is nil",
|
||||
},
|
||||
{
|
||||
name: "missing user Id",
|
||||
u: &user{
|
||||
Address: addr,
|
||||
},
|
||||
sess: ss,
|
||||
errorContains: "user id is missing",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := r.refreshSessions(ctx, tc.u, map[AuthToken]string{{Id: "id"}: "something"},
|
||||
WithSessionRetrievalFunc(testStaticResourceRetrievalFunc(tc.sess)))
|
||||
if tc.errorContains == "" {
|
||||
assert.NoError(t, err)
|
||||
rw := db.New(s)
|
||||
var got []*Session
|
||||
require.NoError(t, rw.SearchWhere(ctx, &got, "true", nil))
|
||||
assert.Len(t, got, tc.wantCount)
|
||||
} else {
|
||||
assert.ErrorContains(t, err, tc.errorContains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_ListSessions(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
u1 := &user{
|
||||
Id: "u1",
|
||||
Address: addr,
|
||||
}
|
||||
at1 := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: u1.Id,
|
||||
}
|
||||
kt1 := KeyringToken{
|
||||
KeyringType: "k1",
|
||||
TokenName: "t1",
|
||||
AuthTokenId: at1.Id,
|
||||
}
|
||||
u2 := &user{
|
||||
Id: "u2",
|
||||
Address: addr,
|
||||
}
|
||||
at2 := &authtokens.AuthToken{
|
||||
Id: "at_2",
|
||||
Token: "at_2_token",
|
||||
UserId: u2.Id,
|
||||
}
|
||||
kt2 := KeyringToken{
|
||||
KeyringType: "k2",
|
||||
TokenName: "t2",
|
||||
AuthTokenId: at2.Id,
|
||||
}
|
||||
atMap := map[ringToken]*authtokens.AuthToken{
|
||||
{"k1", "t1"}: at1,
|
||||
{"k2", "t2"}: at2,
|
||||
}
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(maps.Values(atMap)))
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, r.AddKeyringToken(ctx, addr, kt1))
|
||||
require.NoError(t, r.AddKeyringToken(ctx, addr, kt2))
|
||||
|
||||
t.Run("auth token id is missing", func(t *testing.T) {
|
||||
l, err := r.ListSessions(ctx, "")
|
||||
assert.Nil(t, l)
|
||||
assert.ErrorContains(t, err, "auth token id is missing")
|
||||
})
|
||||
|
||||
ss := []*sessions.Session{
|
||||
{
|
||||
Id: "ttcp_1",
|
||||
Status: "status1",
|
||||
Endpoint: "address1",
|
||||
Type: "tcp",
|
||||
},
|
||||
{
|
||||
Id: "ttcp_2",
|
||||
Status: "status2",
|
||||
Endpoint: "address2",
|
||||
Type: "tcp",
|
||||
},
|
||||
{
|
||||
Id: "ttcp_3",
|
||||
Status: "status3",
|
||||
Endpoint: "address3",
|
||||
Type: "tcp",
|
||||
},
|
||||
}
|
||||
require.NoError(t, r.refreshSessions(ctx, u1, map[AuthToken]string{{Id: "id"}: "something"},
|
||||
WithSessionRetrievalFunc(testStaticResourceRetrievalFunc(ss))))
|
||||
|
||||
t.Run("wrong user gets no sessions", func(t *testing.T) {
|
||||
l, err := r.ListSessions(ctx, kt2.AuthTokenId)
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, l)
|
||||
})
|
||||
t.Run("correct token gets sessions", func(t *testing.T) {
|
||||
l, err := r.ListSessions(ctx, kt1.AuthTokenId)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, l, len(ss))
|
||||
assert.ElementsMatch(t, l, ss)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRepository_QuerySessions(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
u1 := &user{
|
||||
Id: "u1",
|
||||
Address: addr,
|
||||
}
|
||||
at1 := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: u1.Id,
|
||||
}
|
||||
kt1 := KeyringToken{
|
||||
KeyringType: "k1",
|
||||
TokenName: "t1",
|
||||
AuthTokenId: at1.Id,
|
||||
}
|
||||
u2 := &user{
|
||||
Id: "u2",
|
||||
Address: addr,
|
||||
}
|
||||
at2 := &authtokens.AuthToken{
|
||||
Id: "at_2",
|
||||
Token: "at_2_token",
|
||||
UserId: u2.Id,
|
||||
}
|
||||
kt2 := KeyringToken{
|
||||
KeyringType: "k2",
|
||||
TokenName: "t2",
|
||||
AuthTokenId: at2.Id,
|
||||
}
|
||||
atMap := map[ringToken]*authtokens.AuthToken{
|
||||
{"k1", "t1"}: at1,
|
||||
{"k2", "t2"}: at2,
|
||||
}
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(maps.Values(atMap)))
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, r.AddKeyringToken(ctx, addr, kt1))
|
||||
require.NoError(t, r.AddKeyringToken(ctx, addr, kt2))
|
||||
|
||||
query := "status % status1 or status % status2"
|
||||
|
||||
errorCases := []struct {
|
||||
name string
|
||||
t string
|
||||
query string
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "auth token id is missing",
|
||||
t: "",
|
||||
query: query,
|
||||
errContains: "auth token id is missing",
|
||||
},
|
||||
{
|
||||
name: "query is missing",
|
||||
t: "token id",
|
||||
errContains: "query is missing",
|
||||
},
|
||||
}
|
||||
for _, tc := range errorCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
l, err := r.QuerySessions(ctx, tc.t, tc.query)
|
||||
assert.Nil(t, l)
|
||||
assert.ErrorContains(t, err, tc.errContains)
|
||||
})
|
||||
}
|
||||
|
||||
ss := []*sessions.Session{
|
||||
{
|
||||
Id: "ttcp_1",
|
||||
Status: "status1",
|
||||
Endpoint: "address1",
|
||||
Type: "tcp",
|
||||
},
|
||||
{
|
||||
Id: "ttcp_2",
|
||||
Status: "status2",
|
||||
Endpoint: "address2",
|
||||
Type: "tcp",
|
||||
},
|
||||
{
|
||||
Id: "ttcp_3",
|
||||
Status: "status3",
|
||||
Endpoint: "address3",
|
||||
Type: "tcp",
|
||||
},
|
||||
}
|
||||
require.NoError(t, r.refreshSessions(ctx, u1, map[AuthToken]string{{Id: "id"}: "something"},
|
||||
WithSessionRetrievalFunc(testStaticResourceRetrievalFunc(ss))))
|
||||
|
||||
t.Run("wrong token gets no sessions", func(t *testing.T) {
|
||||
l, err := r.QuerySessions(ctx, kt2.AuthTokenId, query)
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, l)
|
||||
})
|
||||
t.Run("correct token gets sessions", func(t *testing.T) {
|
||||
l, err := r.QuerySessions(ctx, kt1.AuthTokenId, query)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, l, 2)
|
||||
assert.ElementsMatch(t, l, ss[0:2])
|
||||
})
|
||||
}
|
||||
|
||||
func TestDefaultSessionRetrievalFunc(t *testing.T) {
|
||||
tc := controller.NewTestController(t, nil)
|
||||
tc.Client().SetToken(tc.Token().Token)
|
||||
tarClient := targets.NewClient(tc.Client())
|
||||
_ = worker.NewTestWorker(t, &worker.TestWorkerOpts{
|
||||
InitialUpstreams: tc.ClusterAddrs(),
|
||||
WorkerAuthKms: tc.Config().WorkerAuthKms,
|
||||
})
|
||||
|
||||
tar1, err := tarClient.Create(tc.Context(), "tcp", "p_1234567890", targets.WithName("tar1"), targets.WithTcpTargetDefaultPort(1), targets.WithAddress("address"))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, tar1)
|
||||
_, err = tarClient.AuthorizeSession(tc.Context(), tar1.Item.Id)
|
||||
assert.NoError(t, err)
|
||||
|
||||
got, err := defaultSessionFunc(tc.Context(), tc.ApiAddrs()[0], tc.Token().Token)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, got, 1)
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
stderrors "errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/hashicorp/boundary/api"
|
||||
"github.com/hashicorp/boundary/api/targets"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/event"
|
||||
"github.com/hashicorp/boundary/internal/types/resource"
|
||||
"github.com/hashicorp/boundary/internal/util"
|
||||
"github.com/hashicorp/mql"
|
||||
)
|
||||
|
||||
// TargetRetrievalFunc is a function that retrieves targets
|
||||
// from the provided boundary addr using the provided token.
|
||||
type TargetRetrievalFunc func(ctx context.Context, addr, token string) ([]*targets.Target, error)
|
||||
|
||||
func defaultTargetFunc(ctx context.Context, addr, token string) ([]*targets.Target, error) {
|
||||
const op = "cache.defaultTargetFunc"
|
||||
client, err := api.NewClient(&api.Config{
|
||||
Addr: addr,
|
||||
Token: token,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
tarClient := targets.NewClient(client)
|
||||
l, err := tarClient.List(ctx, "global", targets.WithRecursive(true))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return l.Items, nil
|
||||
}
|
||||
|
||||
func (r *Repository) refreshTargets(ctx context.Context, u *user, tokens map[AuthToken]string, opt ...Option) error {
|
||||
const op = "cache.(Repository).refreshTargets"
|
||||
switch {
|
||||
case util.IsNil(u):
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "user is nil")
|
||||
case u.Id == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "user id is missing")
|
||||
}
|
||||
|
||||
opts, err := getOpts(opt...)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if opts.withTargetRetrievalFunc == nil {
|
||||
opts.withTargetRetrievalFunc = defaultTargetFunc
|
||||
}
|
||||
|
||||
// Find and use a token for retrieving targets
|
||||
var gotResponse bool
|
||||
var resp []*targets.Target
|
||||
var retErr error
|
||||
for at, t := range tokens {
|
||||
resp, err = opts.withTargetRetrievalFunc(ctx, u.Address, t)
|
||||
if err != nil {
|
||||
// TODO: If we get an error about the token no longer having
|
||||
// permissions, remove it.
|
||||
retErr = stderrors.Join(retErr, errors.Wrap(ctx, err, op, errors.WithMsg("for token %q", at.Id)))
|
||||
continue
|
||||
}
|
||||
gotResponse = true
|
||||
break
|
||||
}
|
||||
if retErr != nil {
|
||||
if saveErr := r.SaveError(ctx, u, resource.Target.String(), retErr); saveErr != nil {
|
||||
return stderrors.Join(err, errors.Wrap(ctx, saveErr, op))
|
||||
}
|
||||
}
|
||||
if !gotResponse {
|
||||
return retErr
|
||||
}
|
||||
|
||||
event.WriteSysEvent(ctx, op, fmt.Sprintf("updating %d targets for user %v", len(resp), u))
|
||||
_, err = r.rw.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(r db.Reader, w db.Writer) error {
|
||||
// TODO: Instead of deleting everything, use refresh tokens and apply the delta
|
||||
if _, err := w.Exec(ctx, "delete from target where user_id = @user_id",
|
||||
[]any{sql.Named("user_id", u.Id)}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, t := range resp {
|
||||
item, err := json.Marshal(t)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
newTarget := &Target{
|
||||
UserId: u.Id,
|
||||
Id: t.Id,
|
||||
Name: t.Name,
|
||||
Description: t.Description,
|
||||
Address: t.Address,
|
||||
Item: string(item),
|
||||
}
|
||||
onConflict := db.OnConflict{
|
||||
Target: db.Columns{"user_id", "id"},
|
||||
Action: db.UpdateAll(true),
|
||||
}
|
||||
if err := w.Create(ctx, newTarget, db.WithOnConflict(&onConflict)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) ListTargets(ctx context.Context, authTokenId string) ([]*targets.Target, error) {
|
||||
const op = "cache.(Repository).ListTargets"
|
||||
switch {
|
||||
case authTokenId == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "auth token id is missing")
|
||||
}
|
||||
ret, err := r.searchTargets(ctx, authTokenId, "true", nil)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (r *Repository) QueryTargets(ctx context.Context, authTokenId, query string) ([]*targets.Target, error) {
|
||||
const op = "cache.(Repository).QueryTargets"
|
||||
switch {
|
||||
case authTokenId == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "auth token id is missing")
|
||||
case query == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "query is missing")
|
||||
}
|
||||
|
||||
w, err := mql.Parse(query, Target{}, mql.WithIgnoredFields("UserId", "Item"))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.InvalidParameter))
|
||||
}
|
||||
ret, err := r.searchTargets(ctx, authTokenId, w.Condition, w.Args)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (r *Repository) searchTargets(ctx context.Context, authTokenId, condition string, searchArgs []any) ([]*targets.Target, error) {
|
||||
const op = "cache.(Repository).searchTargets"
|
||||
switch {
|
||||
case authTokenId == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "user id is missing")
|
||||
case condition == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "condition is missing")
|
||||
}
|
||||
|
||||
condition = fmt.Sprintf("%s and user_id in (select user_id from auth_token where id = ?)", condition)
|
||||
args := append(searchArgs, authTokenId)
|
||||
var cachedTargets []*Target
|
||||
if err := r.rw.SearchWhere(ctx, &cachedTargets, condition, args, db.WithLimit(-1)); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
retTargets := make([]*targets.Target, 0, len(cachedTargets))
|
||||
for _, cachedTar := range cachedTargets {
|
||||
var tar targets.Target
|
||||
if err := json.Unmarshal([]byte(cachedTar.Item), &tar); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
retTargets = append(retTargets, &tar)
|
||||
}
|
||||
return retTargets, nil
|
||||
}
|
||||
|
||||
type Target struct {
|
||||
UserId string `gorm:"primaryKey"`
|
||||
Id string `gorm:"primaryKey"`
|
||||
Name string `gorm:"default:null"`
|
||||
Description string `gorm:"default:null"`
|
||||
Address string `gorm:"default:null"`
|
||||
Item string `gorm:"default:null"`
|
||||
}
|
||||
|
||||
func (*Target) TableName() string {
|
||||
return "target"
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/api/authtokens"
|
||||
"github.com/hashicorp/boundary/api/targets"
|
||||
cachedb "github.com/hashicorp/boundary/internal/clientcache/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/daemon/controller"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
func TestRepository_refreshTargets(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
u := user{
|
||||
Id: "u1",
|
||||
Address: addr,
|
||||
}
|
||||
at := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: u.Id,
|
||||
}
|
||||
kt := KeyringToken{KeyringType: "k", TokenName: "t", AuthTokenId: at.Id}
|
||||
atMap := map[ringToken]*authtokens.AuthToken{
|
||||
{"k", "t"}: at,
|
||||
}
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(maps.Values(atMap)))
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, r.AddKeyringToken(ctx, addr, kt))
|
||||
|
||||
ts := []*targets.Target{
|
||||
{
|
||||
Id: "ttcp_1",
|
||||
Name: "name1",
|
||||
Type: "tcp",
|
||||
SessionMaxSeconds: 111,
|
||||
},
|
||||
{
|
||||
Id: "ttcp_2",
|
||||
Name: "name2",
|
||||
Address: "address2",
|
||||
Type: "tcp",
|
||||
SessionMaxSeconds: 222,
|
||||
},
|
||||
{
|
||||
Id: "ttcp_3",
|
||||
Name: "name3",
|
||||
Address: "address3",
|
||||
Type: "tcp",
|
||||
SessionMaxSeconds: 333,
|
||||
},
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
u *user
|
||||
targets []*targets.Target
|
||||
wantCount int
|
||||
errorContains string
|
||||
}{
|
||||
{
|
||||
name: "Success",
|
||||
u: &user{
|
||||
Id: at.UserId,
|
||||
Address: addr,
|
||||
},
|
||||
targets: ts,
|
||||
wantCount: len(ts),
|
||||
},
|
||||
{
|
||||
name: "repeated target with different values",
|
||||
u: &user{
|
||||
Address: addr,
|
||||
Id: at.UserId,
|
||||
},
|
||||
targets: append(ts, &targets.Target{
|
||||
Id: ts[0].Id,
|
||||
Name: "a different name",
|
||||
}),
|
||||
wantCount: len(ts),
|
||||
},
|
||||
{
|
||||
name: "nil user",
|
||||
u: nil,
|
||||
targets: ts,
|
||||
errorContains: "user is nil",
|
||||
},
|
||||
{
|
||||
name: "missing user Id",
|
||||
u: &user{
|
||||
Address: addr,
|
||||
},
|
||||
targets: ts,
|
||||
errorContains: "user id is missing",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := r.refreshTargets(ctx, tc.u, map[AuthToken]string{{Id: "id"}: "something"},
|
||||
WithTargetRetrievalFunc(testStaticResourceRetrievalFunc(tc.targets)))
|
||||
if tc.errorContains == "" {
|
||||
assert.NoError(t, err)
|
||||
rw := db.New(s)
|
||||
var got []*Target
|
||||
require.NoError(t, rw.SearchWhere(ctx, &got, "true", nil))
|
||||
assert.Len(t, got, tc.wantCount)
|
||||
} else {
|
||||
assert.ErrorContains(t, err, tc.errorContains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_ListTargets(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
u1 := &user{
|
||||
Id: "u1",
|
||||
Address: addr,
|
||||
}
|
||||
at1 := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: u1.Id,
|
||||
}
|
||||
kt1 := KeyringToken{KeyringType: "k1", TokenName: "t1", AuthTokenId: at1.Id}
|
||||
|
||||
u2 := &user{
|
||||
Id: "u2",
|
||||
Address: addr,
|
||||
}
|
||||
at2 := &authtokens.AuthToken{
|
||||
Id: "at_2",
|
||||
Token: "at_2_token",
|
||||
UserId: u2.Id,
|
||||
}
|
||||
kt2 := KeyringToken{KeyringType: "k2", TokenName: "t2", AuthTokenId: at2.Id}
|
||||
atMap := map[ringToken]*authtokens.AuthToken{
|
||||
{"k1", "t1"}: at1,
|
||||
{"k2", "t2"}: at2,
|
||||
}
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(maps.Values(atMap)))
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, r.AddKeyringToken(ctx, addr, kt1))
|
||||
require.NoError(t, r.AddKeyringToken(ctx, addr, kt2))
|
||||
|
||||
t.Run("token is missing", func(t *testing.T) {
|
||||
l, err := r.ListTargets(ctx, "")
|
||||
assert.Nil(t, l)
|
||||
assert.ErrorContains(t, err, "auth token id is missing")
|
||||
})
|
||||
|
||||
ts := []*targets.Target{
|
||||
{
|
||||
Id: "ttcp_1",
|
||||
Name: "name1",
|
||||
Address: "address1",
|
||||
Type: "tcp",
|
||||
SessionMaxSeconds: 111,
|
||||
},
|
||||
{
|
||||
Id: "ttcp_2",
|
||||
Name: "name2",
|
||||
Address: "address2",
|
||||
Type: "tcp",
|
||||
SessionMaxSeconds: 222,
|
||||
},
|
||||
{
|
||||
Id: "ttcp_3",
|
||||
Name: "name3",
|
||||
Address: "address3",
|
||||
Type: "tcp",
|
||||
SessionMaxSeconds: 333,
|
||||
},
|
||||
}
|
||||
require.NoError(t, r.refreshTargets(ctx, u1, map[AuthToken]string{{Id: "id"}: "something"},
|
||||
WithTargetRetrievalFunc(testStaticResourceRetrievalFunc(ts))))
|
||||
|
||||
t.Run("wrong user gets no targets", func(t *testing.T) {
|
||||
l, err := r.ListTargets(ctx, kt2.AuthTokenId)
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, l)
|
||||
})
|
||||
t.Run("correct token gets targets", func(t *testing.T) {
|
||||
l, err := r.ListTargets(ctx, kt1.AuthTokenId)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, l, len(ts))
|
||||
assert.ElementsMatch(t, l, ts)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRepository_QueryTargets(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
u1 := &user{
|
||||
Id: "u1",
|
||||
Address: addr,
|
||||
}
|
||||
at1 := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: u1.Id,
|
||||
}
|
||||
kt1 := KeyringToken{KeyringType: "k1", TokenName: "t1", AuthTokenId: at1.Id}
|
||||
|
||||
u2 := &user{
|
||||
Id: "u2",
|
||||
Address: addr,
|
||||
}
|
||||
at2 := &authtokens.AuthToken{
|
||||
Id: "at_2",
|
||||
Token: "at_2_token",
|
||||
UserId: u2.Id,
|
||||
}
|
||||
kt2 := KeyringToken{KeyringType: "k2", TokenName: "t2", AuthTokenId: at2.Id}
|
||||
atMap := map[ringToken]*authtokens.AuthToken{
|
||||
{"k1", "t1"}: at1,
|
||||
{"k2", "t2"}: at2,
|
||||
}
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(maps.Values(atMap)))
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, r.AddKeyringToken(ctx, addr, kt1))
|
||||
require.NoError(t, r.AddKeyringToken(ctx, addr, kt2))
|
||||
|
||||
query := "name % name1 or name % name2"
|
||||
|
||||
errorCases := []struct {
|
||||
name string
|
||||
p string
|
||||
query string
|
||||
errContains string
|
||||
}{
|
||||
{
|
||||
name: "auth token id is missing",
|
||||
p: "",
|
||||
query: query,
|
||||
errContains: "auth token id is missing",
|
||||
},
|
||||
{
|
||||
name: "query is missing",
|
||||
p: "authtokenid",
|
||||
errContains: "query is missing",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range errorCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
l, err := r.QueryTargets(ctx, tc.p, tc.query)
|
||||
assert.Nil(t, l)
|
||||
assert.ErrorContains(t, err, tc.errContains)
|
||||
})
|
||||
}
|
||||
|
||||
ts := []*targets.Target{
|
||||
{
|
||||
Id: "ttcp_1",
|
||||
Name: "name1",
|
||||
Address: "address1",
|
||||
Type: "tcp",
|
||||
SessionMaxSeconds: 111,
|
||||
},
|
||||
{
|
||||
Id: "ttcp_2",
|
||||
Name: "name2",
|
||||
Address: "address2",
|
||||
Type: "tcp",
|
||||
SessionMaxSeconds: 222,
|
||||
},
|
||||
{
|
||||
Id: "ttcp_3",
|
||||
Name: "name3",
|
||||
Address: "address3",
|
||||
Type: "tcp",
|
||||
SessionMaxSeconds: 333,
|
||||
},
|
||||
}
|
||||
require.NoError(t, r.refreshTargets(ctx, u1, map[AuthToken]string{{Id: "id"}: "something"},
|
||||
WithTargetRetrievalFunc(testStaticResourceRetrievalFunc(ts))))
|
||||
|
||||
t.Run("wrong token gets no targets", func(t *testing.T) {
|
||||
l, err := r.QueryTargets(ctx, kt2.AuthTokenId, query)
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, l)
|
||||
})
|
||||
t.Run("correct token gets targets", func(t *testing.T) {
|
||||
l, err := r.QueryTargets(ctx, kt1.AuthTokenId, query)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, l, 2)
|
||||
assert.ElementsMatch(t, l, ts[0:2])
|
||||
})
|
||||
}
|
||||
|
||||
func TestDefaultTargetRetrievalFunc(t *testing.T) {
|
||||
tc := controller.NewTestController(t, nil)
|
||||
tc.Client().SetToken(tc.Token().Token)
|
||||
tarClient := targets.NewClient(tc.Client())
|
||||
|
||||
tar1, err := tarClient.Create(tc.Context(), "tcp", "p_1234567890", targets.WithName("tar1"), targets.WithTcpTargetDefaultPort(1))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, tar1)
|
||||
tar2, err := tarClient.Create(tc.Context(), "tcp", "p_1234567890", targets.WithName("tar2"), targets.WithTcpTargetDefaultPort(2))
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, tar2)
|
||||
|
||||
got, err := defaultTargetFunc(tc.Context(), tc.ApiAddrs()[0], tc.Token().Token)
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, got, tar1.Item)
|
||||
assert.Contains(t, got, tar2.Item)
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
stdErrors "errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/api/authtokens"
|
||||
cachedb "github.com/hashicorp/boundary/internal/clientcache/internal/db"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// ringToken is a test struct used to group a keyring type and token name
|
||||
// so it can be used in an authtoken lookup function.
|
||||
type ringToken struct {
|
||||
k string
|
||||
t string
|
||||
}
|
||||
|
||||
// mapBasedAuthTokenKeyringLookup provides a fake KeyringTokenLookupFn that uses
|
||||
// the provided map to perform lookups for the tokens
|
||||
func mapBasedAuthTokenKeyringLookup(m map[ringToken]*authtokens.AuthToken) KeyringTokenLookupFn {
|
||||
return func(k, t string) *authtokens.AuthToken {
|
||||
return m[ringToken{k, t}]
|
||||
}
|
||||
}
|
||||
|
||||
// sliceBasedAuthTokenBoundaryReader provides a fake BoundaryTokenReaderFn that uses
|
||||
// the provided map to lookup an auth tokens information.
|
||||
func sliceBasedAuthTokenBoundaryReader(s []*authtokens.AuthToken) BoundaryTokenReaderFn {
|
||||
return func(ctx context.Context, addr, at string) (*authtokens.AuthToken, error) {
|
||||
for _, v := range s {
|
||||
if at == v.Token {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
return nil, stdErrors.New("not found")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_SaveError(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
r, err := NewRepository(ctx, s, &sync.Map{},
|
||||
mapBasedAuthTokenKeyringLookup(map[ringToken]*authtokens.AuthToken{}),
|
||||
sliceBasedAuthTokenBoundaryReader(nil))
|
||||
require.NoError(t, err)
|
||||
|
||||
testResource := "test_resource_type"
|
||||
testErr := fmt.Errorf("test error for %q", testResource)
|
||||
|
||||
u := &user{
|
||||
Id: "u1",
|
||||
Address: "addr",
|
||||
}
|
||||
require.NoError(t, r.rw.Create(ctx, u))
|
||||
|
||||
t.Run("empty resource type", func(t *testing.T) {
|
||||
assert.ErrorContains(t, r.SaveError(ctx, u, "", testErr), "resource type is empty")
|
||||
})
|
||||
t.Run("nil error", func(t *testing.T) {
|
||||
assert.ErrorContains(t, r.SaveError(ctx, u, testResource, nil), "error is nil")
|
||||
})
|
||||
t.Run("nil user", func(t *testing.T) {
|
||||
assert.ErrorContains(t, r.SaveError(ctx, nil, testResource, testErr), "user is nil")
|
||||
})
|
||||
t.Run("success", func(t *testing.T) {
|
||||
assert.NoError(t, r.SaveError(ctx, u, testResource, testErr))
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,527 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/api/authtokens"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/util"
|
||||
)
|
||||
|
||||
// AuthTokenIdSegmentCount are the number of segments, delineated by "_", that
|
||||
// make up the auth token id inside an auth token.
|
||||
// For example, an authtoken format should look something like at_1234567890_sometokenpayload
|
||||
const AuthTokenIdSegmentCount = 2
|
||||
|
||||
// upsertUserAndAuthToken upserts a user and authToken using the data in the provided authtoken.
|
||||
// If creating this user results in the number of users exceeding the limit of
|
||||
// allowed users it deletes the oldest one.
|
||||
func upsertUserAndAuthToken(ctx context.Context, reader db.Reader, writer db.Writer, bAddr string, at *authtokens.AuthToken) error {
|
||||
const op = "cache.upsertUserAndAuthToken"
|
||||
switch {
|
||||
case util.IsNil(reader):
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "reader is nil")
|
||||
case util.IsNil(writer):
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "writer is nil")
|
||||
case !writer.IsTx(ctx):
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "writer isn't part of an inflight transaction")
|
||||
case util.IsNil(at):
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "auth token is nil")
|
||||
case bAddr == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "boundary address is empty")
|
||||
case at.Id == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "auth token id is empty")
|
||||
case at.UserId == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "auth token user id is empty")
|
||||
}
|
||||
{
|
||||
// always make sure the user exists when adding a token
|
||||
u := &user{
|
||||
Id: at.UserId,
|
||||
Address: bAddr,
|
||||
}
|
||||
onConflict := &db.OnConflict{
|
||||
Target: db.Columns{"id"},
|
||||
Action: db.DoNothing(true),
|
||||
}
|
||||
if err := writer.Create(ctx, u, db.WithOnConflict(onConflict)); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
st := &AuthToken{
|
||||
Id: at.Id,
|
||||
UserId: at.UserId,
|
||||
LastAccessedTime: time.Now(),
|
||||
ExpirationTime: at.ExpirationTime,
|
||||
}
|
||||
onConflict := &db.OnConflict{
|
||||
Target: db.Columns{"id"},
|
||||
Action: db.SetColumns([]string{"last_accessed_time", "expiration_time"}),
|
||||
}
|
||||
if err := writer.Create(ctx, st, db.WithOnConflict(onConflict)); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
}
|
||||
|
||||
var users []*user
|
||||
if err := reader.SearchWhere(ctx, &users, "true", []any{}, db.WithLimit(-1)); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
if len(users) <= usersLimit {
|
||||
return nil
|
||||
}
|
||||
|
||||
var oldestUser *user
|
||||
var oldestUsersTime *time.Time
|
||||
for _, u := range users {
|
||||
ats, err := listTokens(ctx, reader, u)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
for _, at := range ats {
|
||||
if oldestUsersTime == nil || oldestUsersTime.After(at.LastAccessedTime) {
|
||||
oldestUser = u
|
||||
oldestUsersTime = &at.LastAccessedTime
|
||||
}
|
||||
}
|
||||
}
|
||||
if oldestUser != nil {
|
||||
if _, err := deleteUser(ctx, writer, oldestUser); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddRawToken upserts the auth token's user and auth token in the db and
|
||||
// stores the actual raw auth token in the repositories in memory storage.
|
||||
// The raw token must be valid and present in boundary and be for a user that
|
||||
// has permission to send a Read request for itself to boundary.
|
||||
func (r *Repository) AddRawToken(ctx context.Context, bAddr string, rawToken string) error {
|
||||
const op = "cache.(Repository).AddRawToken"
|
||||
switch {
|
||||
case rawToken == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "boundary auth token is empty")
|
||||
case bAddr == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "boundary address is empty")
|
||||
}
|
||||
|
||||
// rawToken should look something like at_1234567890_someencryptedpayload
|
||||
atIdParts := strings.SplitN(rawToken, "_", 4)
|
||||
if len(atIdParts) != 3 {
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "boundary auth token is is malformed")
|
||||
}
|
||||
atId := strings.Join(atIdParts[:AuthTokenIdSegmentCount], "_")
|
||||
|
||||
var at *authtokens.AuthToken
|
||||
{
|
||||
var inMemAuthToken *authtokens.AuthToken
|
||||
atV, inMem := r.idToKeyringlessAuthToken.Load(atId)
|
||||
if inMem {
|
||||
var ok bool
|
||||
inMemAuthToken, ok = atV.(*authtokens.AuthToken)
|
||||
if !ok {
|
||||
return errors.New(ctx, errors.Internal, op, "unable to cast in memory auth token to *authtoken.AuthToken")
|
||||
}
|
||||
}
|
||||
t := &AuthToken{
|
||||
Id: atId,
|
||||
}
|
||||
err := r.rw.LookupById(ctx, t)
|
||||
switch {
|
||||
case err != nil && !errors.IsNotFoundError(err):
|
||||
return errors.Wrap(ctx, err, op)
|
||||
case errors.IsNotFoundError(err) || !inMem:
|
||||
// if we don't know about it in the cache or we don't know about
|
||||
// this auth token in memory, get it from boundary to sure up the
|
||||
// cache information about this auth token.
|
||||
at, err = r.tokenReadFromBoundaryFn(ctx, bAddr, rawToken)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
case t.UserId != inMemAuthToken.UserId:
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "user id doesn't match what is specified in the stored auth token")
|
||||
case inMem:
|
||||
at = inMemAuthToken
|
||||
}
|
||||
}
|
||||
|
||||
if at != nil {
|
||||
// The token is never returned from boundary except in the original auth
|
||||
// request so we must set the token.
|
||||
at.Token = rawToken
|
||||
}
|
||||
|
||||
_, err := r.rw.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(reader db.Reader, writer db.Writer) error {
|
||||
if err := upsertUserAndAuthToken(ctx, reader, writer, bAddr, at); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
r.idToKeyringlessAuthToken.Store(at.Id, at)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddKeyringToken adds a token to the repository. If the token's id in the
|
||||
// keyring doesn't match the id provided an error is returned.
|
||||
// The token must be valid and present in boundary and be for a user that
|
||||
// has permission to send a self-Read request to boundary. The user id
|
||||
// stored in the keyring must also match the user id returned from boundary.
|
||||
func (r *Repository) AddKeyringToken(ctx context.Context, bAddr string, token KeyringToken) error {
|
||||
const op = "cache.(Repository).AddKeyringToken"
|
||||
switch {
|
||||
case token.TokenName == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "token name is empty")
|
||||
case token.KeyringType == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "keyring type is empty")
|
||||
case token.AuthTokenId == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "boundary auth token id is empty")
|
||||
case bAddr == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "boundary address is empty")
|
||||
}
|
||||
kt := token.clone()
|
||||
keyringStoredAt := r.tokenKeyringFn(kt.KeyringType, kt.TokenName)
|
||||
if keyringStoredAt == nil {
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "unable to find token in the keyring specified")
|
||||
}
|
||||
if kt.AuthTokenId != keyringStoredAt.Id {
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "provided auth token id doesn't match the one stored")
|
||||
}
|
||||
|
||||
var at *authtokens.AuthToken
|
||||
{
|
||||
cachedAt := &AuthToken{
|
||||
Id: kt.AuthTokenId,
|
||||
}
|
||||
err := r.rw.LookupById(ctx, cachedAt)
|
||||
switch {
|
||||
case err != nil && !errors.IsNotFoundError(err):
|
||||
return errors.Wrap(ctx, err, op)
|
||||
case errors.IsNotFoundError(err):
|
||||
at, err = r.tokenReadFromBoundaryFn(ctx, bAddr, keyringStoredAt.Token)
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
case cachedAt.UserId != keyringStoredAt.UserId:
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "user id doesn't match what is specified in the stored auth token")
|
||||
default:
|
||||
at = keyringStoredAt
|
||||
}
|
||||
}
|
||||
_, err := r.rw.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(reader db.Reader, writer db.Writer) error {
|
||||
if err := upsertUserAndAuthToken(ctx, reader, writer, bAddr, at); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
onConflict := &db.OnConflict{
|
||||
Target: db.Columns{"keyring_type", "token_name"},
|
||||
Action: db.SetColumns([]string{"auth_token_id"}),
|
||||
}
|
||||
if err := writer.Create(ctx, kt, db.WithOnConflict(onConflict)); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// LookupToken returns the Token in the cache if one exists.
|
||||
// Accepts withUpdateLastAccessedTime options. If withUpdateLastAccessedTime
|
||||
// is provided, the last update time of the returned token will be updated to
|
||||
// the current time and reflected in the db. The returned AuthToken will not
|
||||
// have the updated time.
|
||||
func (r *Repository) LookupToken(ctx context.Context, authTokenId string, opt ...Option) (*AuthToken, error) {
|
||||
const op = "cache.(Repository).LookupToken"
|
||||
switch {
|
||||
case authTokenId == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "auth token id is empty")
|
||||
}
|
||||
|
||||
at := &AuthToken{
|
||||
Id: authTokenId,
|
||||
}
|
||||
if err := r.rw.LookupById(ctx, at); err != nil {
|
||||
if errors.IsNotFoundError(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
opts, err := getOpts(opt...)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
if opts.withUpdateLastAccessedTime {
|
||||
updatedT := &AuthToken{
|
||||
Id: authTokenId,
|
||||
LastAccessedTime: time.Now(),
|
||||
}
|
||||
if _, err := r.rw.Update(ctx, updatedT, []string{"LastAccessedTime"}, nil); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
}
|
||||
return at, nil
|
||||
}
|
||||
|
||||
// deleteKeyringToken deletes a keyring token
|
||||
func (r *Repository) deleteKeyringToken(ctx context.Context, kt KeyringToken) error {
|
||||
const op = "cache.(Repository).deleteKeyringToken"
|
||||
switch {
|
||||
case kt.KeyringType == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "missing keyring type")
|
||||
case kt.TokenName == "":
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "token name type")
|
||||
}
|
||||
|
||||
_, err := r.rw.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(reader db.Reader, writer db.Writer) error {
|
||||
// TODO(https://github.com/go-gorm/gorm/issues/4879): Use the
|
||||
// writer.Delete() function once the gorm bug is fixed. Until then
|
||||
// the gorm driver for sqlite has an error which wont execute a
|
||||
// delete correctly. as a work around we manually execute the
|
||||
// query here.
|
||||
n, err := writer.Exec(ctx, "delete from keyring_token where (keyring_type, token_name) = (?, ?)", []any{kt.KeyringType, kt.TokenName})
|
||||
if err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
switch n {
|
||||
case 1:
|
||||
if err := cleanExpiredOrOrphanedAuthTokens(ctx, writer, r.idToKeyringlessAuthToken); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
case 0:
|
||||
return errors.New(ctx, errors.RecordNotFound, op, "token not found when attempting deletion")
|
||||
default:
|
||||
return errors.New(ctx, errors.MultipleRecords, op, "multiple tokens deleted when one was requested")
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// cleanExpiredOrOrphanedAuthTokens removes all tokens which are older than the staleness limit
|
||||
// or does not have either a keyring or keyringless reference to it.
|
||||
func (r *Repository) cleanExpiredOrOrphanedAuthTokens(ctx context.Context) error {
|
||||
const op = "cache.Repository.cleanExpiredOrOrphanedAuthTokens"
|
||||
_, err := r.rw.DoTx(ctx, db.StdRetryCnt, db.ExpBackoff{}, func(reader db.Reader, writer db.Writer) error {
|
||||
if err := cleanExpiredOrOrphanedAuthTokens(ctx, writer, r.idToKeyringlessAuthToken); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
// cleanExpiredOrOrphanedAuthTokens removes all tokens which are older than the staleness limit
|
||||
// or does not have either a keyring or keyringless reference to it.
|
||||
func cleanExpiredOrOrphanedAuthTokens(ctx context.Context, writer db.Writer, idToKeyringlessAuthToken *sync.Map) error {
|
||||
const op = "cache.cleanExpiredOrOrphanedAuthTokens"
|
||||
switch {
|
||||
case util.IsNil(writer):
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "writer is nil")
|
||||
case idToKeyringlessAuthToken == nil:
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "keyringless auth token map is nil")
|
||||
case !writer.IsTx(ctx):
|
||||
return errors.New(ctx, errors.InvalidParameter, op, "writer isn't part of an inflight transaction")
|
||||
}
|
||||
|
||||
var keyringlessAuthTokens []string
|
||||
idToKeyringlessAuthToken.Range(func(key, _ any) bool {
|
||||
keyringlessAuthTokens = append(keyringlessAuthTokens, key.(string))
|
||||
return true
|
||||
})
|
||||
|
||||
deleteOrphanedAuthTokens := `
|
||||
delete from auth_token
|
||||
where
|
||||
last_accessed_time < @last_accessed_time
|
||||
or
|
||||
-- sqlite stores expiration_time as a string in a format that might not match
|
||||
-- what is being used by current_timestamp. Using datetime() makes the
|
||||
-- formats match which allow the string comparison performed here to work
|
||||
-- the same as a time comparison.
|
||||
datetime(expiration_time) < current_timestamp
|
||||
or
|
||||
%s
|
||||
`
|
||||
args := []any{sql.Named("last_accessed_time", time.Now().Add(-tokenStalenessLimit))}
|
||||
|
||||
idInSection := "id not in (select auth_token_id from keyring_token)"
|
||||
if len(keyringlessAuthTokens) > 0 {
|
||||
// Note: We have to build the statement like this because if the slice of string
|
||||
// is empty this gets converted to a query that says " and id not in (NULL)"
|
||||
idInSection = fmt.Sprintf("(%s and id not in @keyringless_token_ids)", idInSection)
|
||||
args = append(args, sql.Named("keyringless_token_ids", keyringlessAuthTokens))
|
||||
}
|
||||
|
||||
if _, err := writer.Exec(ctx, fmt.Sprintf(deleteOrphanedAuthTokens, idInSection), args); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// listUsers returns all known tokens in the cache
|
||||
func (r *Repository) listUsers(ctx context.Context) ([]*user, error) {
|
||||
const op = "cache.(Repository).listUsers"
|
||||
var ret []*user
|
||||
if err := r.rw.SearchWhere(ctx, &ret, "true", nil); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// listTokens returns all known tokens in the cache for the provided user
|
||||
func (r *Repository) listTokens(ctx context.Context, u *user) ([]*AuthToken, error) {
|
||||
const op = "cache.(Repository).listTokens"
|
||||
ret, err := listTokens(ctx, r.rw, u)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// listTokens returns all known tokens in the cache for the provided user using the provided reader
|
||||
func listTokens(ctx context.Context, reader db.Reader, u *user) ([]*AuthToken, error) {
|
||||
const op = "cache.listTokens"
|
||||
switch {
|
||||
case util.IsNil(u):
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "user is nil")
|
||||
case u.Id == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "user id is missing")
|
||||
}
|
||||
var ret []*AuthToken
|
||||
if err := reader.SearchWhere(ctx, &ret, "user_id = ?", []any{u.Id}); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// listKeyringTokens returns all known keyring tokens in the cache for the provided auth token
|
||||
func (r *Repository) listKeyringTokens(ctx context.Context, at *AuthToken) ([]*KeyringToken, error) {
|
||||
const op = "cache.(Repository).listTokens"
|
||||
switch {
|
||||
case util.IsNil(at):
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "auth token is nil")
|
||||
case at.Id == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "auth token id is empty")
|
||||
}
|
||||
var ret []*KeyringToken
|
||||
if err := r.rw.SearchWhere(ctx, &ret, "auth_token_id = ?", []any{at.Id}); err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// syncKeyringlessTokensWithDb removes the in memory storage of auth tokens if
|
||||
// they are no longer represented in the db.
|
||||
func (r *Repository) syncKeyringlessTokensWithDb(ctx context.Context) error {
|
||||
const op = "cache.(Repository).syncKeyringlessTokensWithDb"
|
||||
var ret []*AuthToken
|
||||
if err := r.rw.SearchWhere(ctx, &ret, "true", nil); err != nil {
|
||||
return errors.Wrap(ctx, err, op)
|
||||
}
|
||||
authTokenIds := make(map[string]struct{})
|
||||
for _, at := range ret {
|
||||
authTokenIds[at.Id] = struct{}{}
|
||||
}
|
||||
r.idToKeyringlessAuthToken.Range(func(key, value any) bool {
|
||||
k := key.(string)
|
||||
if _, ok := authTokenIds[k]; !ok {
|
||||
r.idToKeyringlessAuthToken.Delete(key)
|
||||
}
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// deleteUser executes a delete command using the provided db.Writer for the provided user.
|
||||
func deleteUser(ctx context.Context, w db.Writer, u *user) (int, error) {
|
||||
const op = "cache.deleteUser"
|
||||
switch {
|
||||
case util.IsNil(w):
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "writer is nil")
|
||||
case u.Id == "":
|
||||
return db.NoRowsAffected, errors.New(ctx, errors.InvalidParameter, op, "missing id")
|
||||
}
|
||||
// TODO(https://github.com/go-gorm/gorm/issues/4879): Use the
|
||||
// writer.Delete() function once the gorm bug is fixed. Until then
|
||||
// the gorm driver for sqlite has an error which wont execute a
|
||||
// delete correctly. as a work around we manually execute the
|
||||
// query here.
|
||||
n, err := w.Exec(ctx, "delete from user where id = ?", []any{u.Id})
|
||||
if err != nil {
|
||||
err = errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
// user is a gorm model for the user table. It represents a user
|
||||
type user struct {
|
||||
Id string `gorm:"primaryKey"`
|
||||
Address string `gorm:"default:null"`
|
||||
}
|
||||
|
||||
func (*user) TableName() string {
|
||||
return "user"
|
||||
}
|
||||
|
||||
func (u *user) clone() *user {
|
||||
return &user{
|
||||
Id: u.Id,
|
||||
Address: u.Address,
|
||||
}
|
||||
}
|
||||
|
||||
// AuthToken is a gorm model for the token table and is an auth token known by the
|
||||
// boundary client side cache daemon.
|
||||
type AuthToken struct {
|
||||
Id string `gorm:"primaryKey"`
|
||||
UserId string `gorm:"default:null"`
|
||||
LastAccessedTime time.Time `gorm:"default:(strftime('%Y-%m-%d %H:%M:%f','now'))"`
|
||||
ExpirationTime time.Time
|
||||
}
|
||||
|
||||
func (*AuthToken) TableName() string {
|
||||
return "auth_token"
|
||||
}
|
||||
|
||||
// KeyringToken is a gorm model for the keyring stored token
|
||||
type KeyringToken struct {
|
||||
KeyringType string `gorm:"primaryKey"`
|
||||
TokenName string `gorm:"primaryKey"`
|
||||
AuthTokenId string `gorm:"default:null"`
|
||||
}
|
||||
|
||||
func (*KeyringToken) TableName() string {
|
||||
return "keyring_token"
|
||||
}
|
||||
|
||||
func (kt *KeyringToken) clone() *KeyringToken {
|
||||
return &KeyringToken{
|
||||
KeyringType: kt.KeyringType,
|
||||
TokenName: kt.TokenName,
|
||||
AuthTokenId: kt.AuthTokenId,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,822 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/api/authtokens"
|
||||
cachedb "github.com/hashicorp/boundary/internal/clientcache/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/exp/maps"
|
||||
)
|
||||
|
||||
func TestRepository_AddKeyringToken(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
u := user{
|
||||
Id: "u1",
|
||||
Address: addr,
|
||||
}
|
||||
at := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: u.Id,
|
||||
}
|
||||
// used to test mismatched user ids between keyring and cache
|
||||
mismatchingAt := &authtokens.AuthToken{
|
||||
Id: "at_mismatch",
|
||||
Token: "at_mismatch_token",
|
||||
UserId: u.Id,
|
||||
}
|
||||
boundaryAuthTokens := []*authtokens.AuthToken{at, mismatchingAt}
|
||||
keyring := "k"
|
||||
tokenName := "t"
|
||||
atMap := make(map[ringToken]*authtokens.AuthToken)
|
||||
atMap[ringToken{keyring, tokenName}] = at
|
||||
atMap[ringToken{"mismatch", "mismatch"}] = mismatchingAt
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(boundaryAuthTokens))
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("userid mismatch between db and keyring", func(t *testing.T) {
|
||||
require.NoError(t, r.AddKeyringToken(ctx, "address", KeyringToken{
|
||||
KeyringType: "mismatch",
|
||||
TokenName: "mismatch",
|
||||
AuthTokenId: mismatchingAt.Id,
|
||||
}))
|
||||
|
||||
mismatchingAt.UserId = "changedToMismatch"
|
||||
assert.ErrorContains(t, r.AddKeyringToken(ctx, "address", KeyringToken{
|
||||
KeyringType: "mismatch",
|
||||
TokenName: "mismatch",
|
||||
AuthTokenId: mismatchingAt.Id,
|
||||
}), "user id doesn't match what is specified in the stored auth token")
|
||||
})
|
||||
|
||||
errCases := []struct {
|
||||
name string
|
||||
addr string
|
||||
kt KeyringToken
|
||||
errorContains string
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
addr: "address",
|
||||
kt: KeyringToken{
|
||||
KeyringType: keyring,
|
||||
TokenName: tokenName,
|
||||
AuthTokenId: at.Id,
|
||||
},
|
||||
errorContains: "",
|
||||
},
|
||||
{
|
||||
name: "not in keyring",
|
||||
addr: "address",
|
||||
kt: KeyringToken{
|
||||
KeyringType: keyring,
|
||||
TokenName: "unknowntokenname",
|
||||
AuthTokenId: at.Id,
|
||||
},
|
||||
errorContains: "unable to find token in the keyring specified",
|
||||
},
|
||||
{
|
||||
name: "missing address",
|
||||
kt: KeyringToken{
|
||||
KeyringType: keyring,
|
||||
TokenName: tokenName,
|
||||
AuthTokenId: at.Id,
|
||||
},
|
||||
errorContains: "boundary address is empty",
|
||||
},
|
||||
{
|
||||
name: "missing token",
|
||||
addr: "address",
|
||||
kt: KeyringToken{
|
||||
KeyringType: keyring,
|
||||
AuthTokenId: at.Id,
|
||||
},
|
||||
errorContains: "token name is empty",
|
||||
},
|
||||
{
|
||||
name: "missing keyring type",
|
||||
addr: "address",
|
||||
kt: KeyringToken{
|
||||
TokenName: tokenName,
|
||||
AuthTokenId: at.Id,
|
||||
},
|
||||
errorContains: "keyring type is empty",
|
||||
},
|
||||
{
|
||||
name: "missing auth token id",
|
||||
addr: "address",
|
||||
kt: KeyringToken{
|
||||
KeyringType: keyring,
|
||||
TokenName: tokenName,
|
||||
},
|
||||
errorContains: "boundary auth token id is empty",
|
||||
},
|
||||
{
|
||||
name: "unmatching auth token id",
|
||||
addr: "address",
|
||||
kt: KeyringToken{
|
||||
KeyringType: keyring,
|
||||
TokenName: tokenName,
|
||||
AuthTokenId: "wrong auth token id",
|
||||
},
|
||||
errorContains: "provided auth token id doesn't match",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range errCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := r.AddKeyringToken(ctx, tc.addr, tc.kt)
|
||||
if tc.errorContains == "" {
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
assert.ErrorContains(t, err, tc.errorContains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_AddRawToken(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
at := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: "u1",
|
||||
}
|
||||
existingAt := &authtokens.AuthToken{
|
||||
Id: "at_existing",
|
||||
Token: "at_existing_token",
|
||||
UserId: "u2",
|
||||
}
|
||||
boundaryAuthTokens := []*authtokens.AuthToken{at, existingAt}
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(map[ringToken]*authtokens.AuthToken{}), sliceBasedAuthTokenBoundaryReader(boundaryAuthTokens))
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("mismatched userid between memory and db", func(t *testing.T) {
|
||||
require.NoError(t, r.AddRawToken(ctx, "address", existingAt.Token))
|
||||
loadedExistingV, loaded := r.idToKeyringlessAuthToken.Load(existingAt.Id)
|
||||
require.True(t, loaded)
|
||||
loadedExistingAt := loadedExistingV.(*authtokens.AuthToken)
|
||||
loadedExistingAt.UserId = "mismatchingUserId"
|
||||
r.idToKeyringlessAuthToken.Store(existingAt.Id, loadedExistingAt)
|
||||
|
||||
err := r.AddRawToken(ctx, "address", loadedExistingAt.Token)
|
||||
assert.Error(t, err)
|
||||
assert.ErrorContains(t, err, "user id doesn't match what is specified in the stored auth token")
|
||||
})
|
||||
|
||||
errCases := []struct {
|
||||
name string
|
||||
addr string
|
||||
rawAt string
|
||||
errorContains string
|
||||
}{
|
||||
{
|
||||
name: "success",
|
||||
addr: "address",
|
||||
rawAt: at.Token,
|
||||
errorContains: "",
|
||||
},
|
||||
{
|
||||
name: "missing address",
|
||||
rawAt: at.Token,
|
||||
errorContains: "boundary address is empty",
|
||||
},
|
||||
{
|
||||
name: "missing token",
|
||||
addr: "address",
|
||||
errorContains: "auth token is empty",
|
||||
},
|
||||
{
|
||||
name: "malformed auth token",
|
||||
addr: "address",
|
||||
rawAt: fmt.Sprintf("%s_extraunderscore", at.Token),
|
||||
errorContains: "boundary auth token is is malformed",
|
||||
},
|
||||
{
|
||||
name: "not found in boundary",
|
||||
addr: "address",
|
||||
rawAt: "at_123_notfoundinboundary",
|
||||
errorContains: "not found",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range errCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := r.AddRawToken(ctx, tc.addr, tc.rawAt)
|
||||
if tc.errorContains == "" {
|
||||
require.NoError(t, err)
|
||||
} else {
|
||||
assert.ErrorContains(t, err, tc.errorContains)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepository_AddToken_EvictsOverLimitUsers(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
boundaryAuthTokens := []*authtokens.AuthToken{
|
||||
{
|
||||
UserId: "user_base",
|
||||
Id: "at_base",
|
||||
Token: "at_base_token",
|
||||
},
|
||||
}
|
||||
for i := 0; i < usersLimit; i++ {
|
||||
iAt := &authtokens.AuthToken{
|
||||
UserId: fmt.Sprintf("user%d", i),
|
||||
Id: fmt.Sprintf("at_%d", i),
|
||||
}
|
||||
iAt.Token = fmt.Sprintf("%s_token", iAt.Id)
|
||||
boundaryAuthTokens = append(boundaryAuthTokens, iAt)
|
||||
}
|
||||
|
||||
atMap := make(map[ringToken]*authtokens.AuthToken)
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(boundaryAuthTokens))
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
kt := KeyringToken{
|
||||
KeyringType: "keyring",
|
||||
TokenName: "token",
|
||||
AuthTokenId: boundaryAuthTokens[0].Id,
|
||||
}
|
||||
atMap[ringToken{kt.KeyringType, kt.TokenName}] = boundaryAuthTokens[0]
|
||||
|
||||
assert.NoError(t, r.AddKeyringToken(ctx, addr, kt))
|
||||
assert.NoError(t, r.AddKeyringToken(ctx, addr, kt))
|
||||
|
||||
lastKtAdded := kt
|
||||
for i, at := range boundaryAuthTokens[1:] {
|
||||
kr := fmt.Sprintf("%s%d", kt.KeyringType, i)
|
||||
tn := fmt.Sprintf("%s%d", kt.TokenName, i)
|
||||
ikt := KeyringToken{KeyringType: kr, TokenName: tn, AuthTokenId: at.Id}
|
||||
|
||||
atMap[ringToken{ikt.KeyringType, ikt.TokenName}] = at
|
||||
assert.NoError(t, r.AddKeyringToken(ctx, addr, ikt))
|
||||
lastKtAdded = ikt
|
||||
}
|
||||
|
||||
// Lookup the first token added. It should have been deleted when the
|
||||
// associated user was evicted for being used the least recently.
|
||||
gotP, err := r.LookupToken(ctx, kt.AuthTokenId)
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, gotP)
|
||||
|
||||
gotP, err = r.LookupToken(ctx, lastKtAdded.AuthTokenId)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, gotP)
|
||||
}
|
||||
|
||||
func TestRepository_AddToken_EvictsOverLimit_Keyringless(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
boundaryAuthTokens := []*authtokens.AuthToken{
|
||||
{
|
||||
UserId: "user_base",
|
||||
Id: "at_base",
|
||||
Token: "at_base_token",
|
||||
},
|
||||
}
|
||||
for i := 0; i < usersLimit; i++ {
|
||||
iAt := &authtokens.AuthToken{
|
||||
UserId: fmt.Sprintf("user%d", i),
|
||||
Id: fmt.Sprintf("at_%d", i),
|
||||
}
|
||||
iAt.Token = fmt.Sprintf("%s_token", iAt.Id)
|
||||
boundaryAuthTokens = append(boundaryAuthTokens, iAt)
|
||||
}
|
||||
|
||||
atMap := make(map[ringToken]*authtokens.AuthToken)
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(boundaryAuthTokens))
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
|
||||
assert.NoError(t, r.AddRawToken(ctx, addr, boundaryAuthTokens[0].Token))
|
||||
assert.NoError(t, r.AddRawToken(ctx, addr, boundaryAuthTokens[0].Token))
|
||||
for _, at := range boundaryAuthTokens[1:] {
|
||||
assert.NoError(t, r.AddRawToken(ctx, addr, at.Token))
|
||||
}
|
||||
// Lookup the first persona added. It should have been evicted from the db
|
||||
// for being used the least recently. It is only removed from the db once
|
||||
// cleanAuthTokens is called.
|
||||
gotP, err := r.LookupToken(ctx, boundaryAuthTokens[0].Id)
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, gotP)
|
||||
_, ok := r.idToKeyringlessAuthToken.Load(boundaryAuthTokens[0].Id)
|
||||
assert.True(t, ok)
|
||||
|
||||
gotP, err = r.LookupToken(ctx, boundaryAuthTokens[len(boundaryAuthTokens)-1].Id)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, gotP)
|
||||
_, ok = r.idToKeyringlessAuthToken.Load(boundaryAuthTokens[len(boundaryAuthTokens)-1].Id)
|
||||
assert.True(t, ok)
|
||||
|
||||
assert.NoError(t, r.syncKeyringlessTokensWithDb(ctx))
|
||||
_, ok = r.idToKeyringlessAuthToken.Load(boundaryAuthTokens[0].Id)
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
func TestRepository_CleanAuthTokens(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
at := &authtokens.AuthToken{
|
||||
UserId: "user_base",
|
||||
Id: "at_base",
|
||||
Token: "at_base_token",
|
||||
}
|
||||
boundaryAuthTokens := []*authtokens.AuthToken{at}
|
||||
|
||||
atMap := make(map[ringToken]*authtokens.AuthToken)
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(boundaryAuthTokens))
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, r.AddRawToken(ctx, "baddr", at.Token))
|
||||
_, present := r.idToKeyringlessAuthToken.Load(at.Id)
|
||||
assert.True(t, present)
|
||||
|
||||
_, err = r.rw.Delete(ctx, &user{Id: at.UserId})
|
||||
require.NoError(t, err)
|
||||
|
||||
_, present = r.idToKeyringlessAuthToken.Load(at.Id)
|
||||
assert.True(t, present)
|
||||
|
||||
assert.NoError(t, r.syncKeyringlessTokensWithDb(ctx))
|
||||
|
||||
_, present = r.idToKeyringlessAuthToken.Load(at.Id)
|
||||
assert.False(t, present)
|
||||
}
|
||||
|
||||
func TestRepository_AddKeyringToken_AddingExistingUpdatesLastAccessedTime(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
at1 := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: "u_1",
|
||||
}
|
||||
kt1 := KeyringToken{
|
||||
TokenName: "t1",
|
||||
KeyringType: "k1",
|
||||
AuthTokenId: at1.Id,
|
||||
}
|
||||
at2 := &authtokens.AuthToken{
|
||||
Id: "at_2",
|
||||
Token: "at_2_token",
|
||||
UserId: "u_2",
|
||||
}
|
||||
kt2 := KeyringToken{
|
||||
TokenName: "t2",
|
||||
KeyringType: "k2",
|
||||
AuthTokenId: "at_2",
|
||||
}
|
||||
|
||||
boundaryAuthTokens := []*authtokens.AuthToken{at1, at2}
|
||||
atMap := map[ringToken]*authtokens.AuthToken{
|
||||
{kt1.KeyringType, kt1.TokenName}: at1,
|
||||
{kt2.KeyringType, kt2.TokenName}: at2,
|
||||
}
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(boundaryAuthTokens))
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.NoError(t, r.AddKeyringToken(ctx, addr, kt1))
|
||||
assert.NoError(t, r.AddKeyringToken(ctx, addr, kt2))
|
||||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
assert.NoError(t, r.AddKeyringToken(ctx, addr, kt1))
|
||||
|
||||
gotP1, err := r.LookupToken(ctx, kt1.AuthTokenId)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, gotP1)
|
||||
gotP2, err := r.LookupToken(ctx, kt2.AuthTokenId)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, gotP2)
|
||||
|
||||
assert.Greater(t, gotP1.LastAccessedTime, gotP2.LastAccessedTime)
|
||||
}
|
||||
|
||||
func TestRepository_AddRawToken_AddingExistingUpdatesLastAccessedTime(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
at1 := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: "u_1",
|
||||
}
|
||||
at2 := &authtokens.AuthToken{
|
||||
Id: "at_2",
|
||||
Token: "at_2_token",
|
||||
UserId: "u_2",
|
||||
}
|
||||
|
||||
boundaryAuthTokens := []*authtokens.AuthToken{at1, at2}
|
||||
atMap := map[ringToken]*authtokens.AuthToken{}
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(boundaryAuthTokens))
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.NoError(t, r.AddRawToken(ctx, addr, at1.Token))
|
||||
assert.NoError(t, r.AddRawToken(ctx, addr, at2.Token))
|
||||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
assert.NoError(t, r.AddRawToken(ctx, addr, at1.Token))
|
||||
|
||||
gotP1, err := r.LookupToken(ctx, at1.Id)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, gotP1)
|
||||
gotP2, err := r.LookupToken(ctx, at2.Id)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, gotP2)
|
||||
|
||||
assert.Greater(t, gotP1.LastAccessedTime, gotP2.LastAccessedTime)
|
||||
}
|
||||
|
||||
func TestRepository_ListTokens(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
u := &user{
|
||||
Id: "u1",
|
||||
Address: addr,
|
||||
}
|
||||
at := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: u.Id,
|
||||
}
|
||||
|
||||
atMap := make(map[ringToken]*authtokens.AuthToken)
|
||||
|
||||
ktTokenCount := 15
|
||||
for i := 0; i < ktTokenCount; i++ {
|
||||
k := fmt.Sprintf("k%d", i)
|
||||
t := fmt.Sprintf("t%d", i)
|
||||
atMap[ringToken{k, t}] = at
|
||||
}
|
||||
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(maps.Values(atMap)))
|
||||
require.NoError(t, err)
|
||||
|
||||
for k, v := range atMap {
|
||||
require.NoError(t, r.AddKeyringToken(ctx, addr, KeyringToken{
|
||||
KeyringType: k.k,
|
||||
TokenName: k.t,
|
||||
AuthTokenId: v.Id,
|
||||
}))
|
||||
}
|
||||
|
||||
t.Run("no token", func(t *testing.T) {
|
||||
gotP, err := r.listTokens(ctx, &user{Id: "tokenless"})
|
||||
assert.NoError(t, err)
|
||||
assert.Empty(t, gotP)
|
||||
})
|
||||
|
||||
t.Run("many tokens", func(t *testing.T) {
|
||||
gotAt, err := r.listTokens(ctx, u)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, gotAt, 1)
|
||||
|
||||
gotKrt, err := r.listKeyringTokens(ctx, gotAt[0])
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, gotKrt, ktTokenCount)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRepository_DeleteKeyringToken(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
at1 := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: "u_1",
|
||||
}
|
||||
kt1 := KeyringToken{
|
||||
TokenName: "t1",
|
||||
KeyringType: "k1",
|
||||
AuthTokenId: at1.Id,
|
||||
}
|
||||
|
||||
boundaryAuthTokens := []*authtokens.AuthToken{at1}
|
||||
atMap := map[ringToken]*authtokens.AuthToken{
|
||||
{kt1.KeyringType, kt1.TokenName}: at1,
|
||||
}
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(boundaryAuthTokens))
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("delete non existing", func(t *testing.T) {
|
||||
assert.ErrorContains(t, r.deleteKeyringToken(ctx, KeyringToken{KeyringType: "Unknown", TokenName: "Unknown"}), "not found")
|
||||
})
|
||||
|
||||
t.Run("delete existing", func(t *testing.T) {
|
||||
assert.NoError(t, r.AddKeyringToken(ctx, addr, kt1))
|
||||
p, err := r.LookupToken(ctx, kt1.AuthTokenId)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, p)
|
||||
|
||||
assert.NoError(t, r.deleteKeyringToken(ctx, kt1))
|
||||
|
||||
got, err := r.LookupToken(ctx, kt1.AuthTokenId)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, got)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRepository_LookupToken(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr := "address"
|
||||
at := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: "u_1",
|
||||
}
|
||||
kt := KeyringToken{
|
||||
TokenName: "t1",
|
||||
KeyringType: "k1",
|
||||
AuthTokenId: at.Id,
|
||||
}
|
||||
|
||||
boundaryAuthTokens := []*authtokens.AuthToken{at}
|
||||
atMap := map[ringToken]*authtokens.AuthToken{
|
||||
{kt.KeyringType, kt.TokenName}: at,
|
||||
}
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(boundaryAuthTokens))
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Run("empty token id", func(t *testing.T) {
|
||||
p, err := r.LookupToken(ctx, "")
|
||||
assert.ErrorContains(t, err, "auth token id is empty")
|
||||
assert.Nil(t, p)
|
||||
})
|
||||
t.Run("not found", func(t *testing.T) {
|
||||
p, err := r.LookupToken(ctx, "token")
|
||||
assert.NoError(t, err)
|
||||
assert.Nil(t, p)
|
||||
})
|
||||
t.Run("found", func(t *testing.T) {
|
||||
assert.NoError(t, r.AddKeyringToken(ctx, addr, kt))
|
||||
p, err := r.LookupToken(ctx, kt.AuthTokenId)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, p)
|
||||
})
|
||||
|
||||
t.Run("withUpdateLastAccessedTime", func(t *testing.T) {
|
||||
assert.NoError(t, r.AddKeyringToken(ctx, addr, kt))
|
||||
time.Sleep(1 * time.Millisecond)
|
||||
|
||||
beforeP, err := r.LookupToken(ctx, kt.AuthTokenId, WithUpdateLastAccessedTime(true))
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, beforeP)
|
||||
|
||||
afterP, err := r.LookupToken(ctx, kt.AuthTokenId)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, afterP)
|
||||
assert.Greater(t, afterP.LastAccessedTime, beforeP.LastAccessedTime)
|
||||
})
|
||||
}
|
||||
|
||||
func TestRepository_RemoveStaleTokens(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
u := &user{
|
||||
Id: "user",
|
||||
Address: "address",
|
||||
}
|
||||
at1 := &authtokens.AuthToken{
|
||||
Id: "at_1",
|
||||
Token: "at_1_token",
|
||||
UserId: u.Id,
|
||||
ExpirationTime: time.Now().Add(1 * time.Minute),
|
||||
}
|
||||
kt1 := KeyringToken{
|
||||
TokenName: "t1",
|
||||
KeyringType: "k1",
|
||||
AuthTokenId: at1.Id,
|
||||
}
|
||||
at2 := &authtokens.AuthToken{
|
||||
Id: "at_2",
|
||||
Token: "at_2_token",
|
||||
UserId: u.Id,
|
||||
ExpirationTime: time.Now().Add(1 * time.Minute),
|
||||
}
|
||||
kt2 := KeyringToken{
|
||||
TokenName: "t2",
|
||||
KeyringType: "k2",
|
||||
AuthTokenId: "at_2",
|
||||
}
|
||||
// this auth token expired a minute ago
|
||||
at3 := &authtokens.AuthToken{
|
||||
Id: "at_3",
|
||||
Token: "at_3_token",
|
||||
UserId: u.Id,
|
||||
ExpirationTime: time.Now().Add(-(1 * time.Minute)),
|
||||
}
|
||||
kt3 := KeyringToken{
|
||||
TokenName: "t3",
|
||||
KeyringType: "k3",
|
||||
AuthTokenId: "at_3",
|
||||
}
|
||||
|
||||
boundaryAuthTokens := []*authtokens.AuthToken{at1, at2, at3}
|
||||
atMap := map[ringToken]*authtokens.AuthToken{
|
||||
{kt1.KeyringType, kt1.TokenName}: at1,
|
||||
{kt2.KeyringType, kt2.TokenName}: at2,
|
||||
{kt3.KeyringType, kt3.TokenName}: at3,
|
||||
}
|
||||
r, err := NewRepository(ctx, s, &sync.Map{}, mapBasedAuthTokenKeyringLookup(atMap), sliceBasedAuthTokenBoundaryReader(boundaryAuthTokens))
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, r.AddKeyringToken(ctx, u.Address, kt1))
|
||||
assert.NoError(t, r.AddKeyringToken(ctx, u.Address, kt2))
|
||||
assert.NoError(t, r.AddKeyringToken(ctx, u.Address, kt3))
|
||||
|
||||
staleTime := time.Now().Add(-(tokenStalenessLimit + 1*time.Hour))
|
||||
freshTime := time.Now().Add(-(tokenStalenessLimit - 1*time.Hour))
|
||||
|
||||
freshAt := &AuthToken{
|
||||
Id: at1.Id,
|
||||
LastAccessedTime: freshTime,
|
||||
}
|
||||
_, err = r.rw.Update(ctx, freshAt, []string{"LastAccessedTime"}, nil)
|
||||
require.NoError(t, err)
|
||||
anotherFreshAt := &AuthToken{
|
||||
Id: at3.Id,
|
||||
LastAccessedTime: freshTime,
|
||||
}
|
||||
_, err = r.rw.Update(ctx, anotherFreshAt, []string{"LastAccessedTime"}, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
staleAt := &AuthToken{
|
||||
Id: at2.Id,
|
||||
LastAccessedTime: staleTime,
|
||||
}
|
||||
_, err = r.rw.Update(ctx, staleAt, []string{"LastAccessedTime"}, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
lAt, err := r.listTokens(ctx, u)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, lAt, 3)
|
||||
|
||||
assert.NoError(t, r.cleanExpiredOrOrphanedAuthTokens(ctx))
|
||||
|
||||
lAt, err = r.listTokens(ctx, u)
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, lAt, 1)
|
||||
assert.Equal(t, lAt[0].Id, at1.Id)
|
||||
}
|
||||
|
||||
func TestCleanExpiredOrOrphanedAuthTokens_Errors(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
err := cleanExpiredOrOrphanedAuthTokens(ctx, nil, &sync.Map{})
|
||||
assert.Error(t, err)
|
||||
assert.ErrorContains(t, err, "writer is nil")
|
||||
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
rw := db.New(s)
|
||||
|
||||
err = cleanExpiredOrOrphanedAuthTokens(ctx, rw, &sync.Map{})
|
||||
assert.Error(t, err)
|
||||
assert.ErrorContains(t, err, "writer isn't part of an inflight transaction")
|
||||
|
||||
rw.DoTx(ctx, 1, db.ExpBackoff{}, func(_ db.Reader, writer db.Writer) error {
|
||||
err := cleanExpiredOrOrphanedAuthTokens(ctx, writer, nil)
|
||||
assert.Error(t, err)
|
||||
assert.ErrorContains(t, err, "keyringless auth token map is nil")
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func TestUpsertUserAndAuthToken(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
rw := db.New(s)
|
||||
|
||||
defaultAt := &authtokens.AuthToken{
|
||||
Id: "at_123",
|
||||
Token: "at_123_token",
|
||||
UserId: "u_123",
|
||||
}
|
||||
|
||||
rw.DoTx(ctx, 1, db.ExpBackoff{}, func(txReader db.Reader, txWriter db.Writer) error {
|
||||
errorCases := []struct {
|
||||
name string
|
||||
reader db.Reader
|
||||
writer db.Writer
|
||||
addr string
|
||||
at *authtokens.AuthToken
|
||||
errorContains string
|
||||
}{
|
||||
{
|
||||
name: "nil reader",
|
||||
reader: nil,
|
||||
writer: txWriter,
|
||||
addr: "address",
|
||||
at: defaultAt,
|
||||
errorContains: "reader is nil",
|
||||
},
|
||||
{
|
||||
name: "nil writer",
|
||||
reader: txReader,
|
||||
writer: nil,
|
||||
addr: "address",
|
||||
at: defaultAt,
|
||||
errorContains: "writer is nil",
|
||||
},
|
||||
{
|
||||
name: "writer not in tx",
|
||||
reader: txReader,
|
||||
writer: rw,
|
||||
addr: "address",
|
||||
at: defaultAt,
|
||||
errorContains: "writer isn't part of an inflight transaction",
|
||||
},
|
||||
{
|
||||
name: "empty address",
|
||||
reader: txReader,
|
||||
writer: txWriter,
|
||||
addr: "",
|
||||
at: defaultAt,
|
||||
errorContains: "boundary address is empty",
|
||||
},
|
||||
{
|
||||
name: "auth token is nil",
|
||||
reader: txReader,
|
||||
writer: txWriter,
|
||||
addr: "address",
|
||||
at: nil,
|
||||
errorContains: "auth token is nil",
|
||||
},
|
||||
{
|
||||
name: "auth token missing id",
|
||||
reader: txReader,
|
||||
writer: txWriter,
|
||||
addr: "address",
|
||||
at: &authtokens.AuthToken{
|
||||
Token: "at_123_token",
|
||||
UserId: "u_123",
|
||||
},
|
||||
errorContains: "auth token id is empty",
|
||||
},
|
||||
{
|
||||
name: "auth token missing user id",
|
||||
reader: txReader,
|
||||
writer: txWriter,
|
||||
addr: "address",
|
||||
at: &authtokens.AuthToken{
|
||||
Id: "at_123",
|
||||
Token: "at_123_token",
|
||||
},
|
||||
errorContains: "auth token user id is empty",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range errorCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := upsertUserAndAuthToken(ctx, tc.reader, tc.writer, tc.addr, tc.at)
|
||||
assert.Error(t, err)
|
||||
assert.ErrorContains(t, err, tc.errorContains)
|
||||
})
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
+196
@@ -0,0 +1,196 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/boundary/api/sessions"
|
||||
"github.com/hashicorp/boundary/api/targets"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/util"
|
||||
"github.com/hashicorp/go-bexpr"
|
||||
)
|
||||
|
||||
type SearchableResource string
|
||||
|
||||
const (
|
||||
Unknown SearchableResource = "unknown"
|
||||
Targets SearchableResource = "targets"
|
||||
Sessions SearchableResource = "sessions"
|
||||
)
|
||||
|
||||
func (r SearchableResource) Valid() bool {
|
||||
switch r {
|
||||
case Targets, Sessions:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func ToSearchableResource(s string) SearchableResource {
|
||||
switch {
|
||||
case strings.EqualFold(s, string(Targets)):
|
||||
return Targets
|
||||
case strings.EqualFold(s, string(Sessions)):
|
||||
return Sessions
|
||||
}
|
||||
return Unknown
|
||||
}
|
||||
|
||||
// SearchParams contains the parameters for searching in the cache
|
||||
type SearchParams struct {
|
||||
// the name of the resource. eg. "targets" or "sessions"
|
||||
Resource SearchableResource
|
||||
// the auth token id for the user id that has resources synced to the cache
|
||||
AuthTokenId string
|
||||
// the optional mql query to use when searching the resources.
|
||||
Query string
|
||||
// the optional bexpr filter string that all results will be filtered by
|
||||
Filter string
|
||||
}
|
||||
|
||||
// SearchResult returns the results from searching the cache.
|
||||
type SearchResult struct {
|
||||
Targets []*targets.Target
|
||||
Sessions []*sessions.Session
|
||||
}
|
||||
|
||||
// SearchService is a domain service that can search across all resources in the
|
||||
// cache.
|
||||
type SearchService struct {
|
||||
searchableResources map[SearchableResource]resourceSearcher
|
||||
}
|
||||
|
||||
// QueryAndLister defines the methods needed for listing an querying the
|
||||
// resources supported by the boundary frontend.
|
||||
type QueryAndLister interface {
|
||||
QueryTargets(context.Context, string, string) ([]*targets.Target, error)
|
||||
ListTargets(context.Context, string) ([]*targets.Target, error)
|
||||
QuerySessions(context.Context, string, string) ([]*sessions.Session, error)
|
||||
ListSessions(context.Context, string) ([]*sessions.Session, error)
|
||||
}
|
||||
|
||||
func NewSearchService(ctx context.Context, repo QueryAndLister) (*SearchService, error) {
|
||||
const op = "cache.NewSearchService"
|
||||
switch {
|
||||
case util.IsNil(repo):
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "repo is nil")
|
||||
}
|
||||
return &SearchService{
|
||||
searchableResources: map[SearchableResource]resourceSearcher{
|
||||
Targets: &resourceSearchFns[*targets.Target]{
|
||||
list: repo.ListTargets,
|
||||
query: repo.QueryTargets,
|
||||
searchResult: func(t []*targets.Target) *SearchResult {
|
||||
return &SearchResult{Targets: t}
|
||||
},
|
||||
},
|
||||
Sessions: &resourceSearchFns[*sessions.Session]{
|
||||
list: repo.ListSessions,
|
||||
query: repo.QuerySessions,
|
||||
searchResult: func(s []*sessions.Session) *SearchResult {
|
||||
return &SearchResult{Sessions: s}
|
||||
},
|
||||
},
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Search returns a SearchResult based on the provided SearchParams. If the
|
||||
// SearchParams doesn't have a valid searchable resource or an auth token id
|
||||
// an error is returned. If the auth token id is unrecognized or is associated
|
||||
// with a user id which doesn't have any resources associated with it an empty
|
||||
// SearchResult is returned. SearchResult will only have at most one field
|
||||
// populated.
|
||||
func (s *SearchService) Search(ctx context.Context, params SearchParams) (*SearchResult, error) {
|
||||
const op = "cache.(SearchService).Search"
|
||||
switch {
|
||||
case !params.Resource.Valid():
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "invalid resource")
|
||||
case params.AuthTokenId == "":
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, "missing auth token id")
|
||||
}
|
||||
rSearcher, ok := s.searchableResources[params.Resource]
|
||||
if !ok {
|
||||
return nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("resource name %q is not recognized", params.Resource))
|
||||
}
|
||||
resp, err := rSearcher.search(ctx, params)
|
||||
if err != nil {
|
||||
err = errors.Wrap(ctx, err, op)
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// resourceSearchFns is a struct that collects all the functions needed to
|
||||
// perform a search on a specific resource type.
|
||||
type resourceSearchFns[T any] struct {
|
||||
// list takes a context and an auth token and returns all resources for the
|
||||
// user of that auth token. If the provided auth token is not in the cache
|
||||
// an empty slice and no error is returned.
|
||||
list func(context.Context, string) ([]T, error)
|
||||
// query takes a context, an auth token, and a query string and returns all
|
||||
// resources for that auth token that matches the provided query parameter.
|
||||
// If the provided auth token is not in the cache an empty slice and no
|
||||
// error is returned.
|
||||
query func(context.Context, string, string) ([]T, error)
|
||||
// searchResult is a function which provides a SearchResult based on the
|
||||
// type of T. SearchResult contains different fields for the different
|
||||
// resource types returned, so for example if T is *targets.Target the
|
||||
// returned SearchResult will have it's "Targets" field populated so the
|
||||
// searchResult should take the passed in paramater and assign it to the
|
||||
// appropriate field in the SearchResult.
|
||||
searchResult func([]T) *SearchResult
|
||||
}
|
||||
|
||||
// resourceSearcher is an interface that only resourceSearchFns[T] is expected
|
||||
// to satisfy. Specifying this interface allows the code to have a map with
|
||||
// resourceSearchFns values which have different bound generic types.
|
||||
type resourceSearcher interface {
|
||||
search(ctx context.Context, p SearchParams) (*SearchResult, error)
|
||||
}
|
||||
|
||||
// search will perform a query using the provided query string or a list if the
|
||||
// provided query string is empty and filter than based on the provided filter.
|
||||
// The results are tied to the user id associated with the provided auth token id.
|
||||
// If the auth token id or the associated user are not in the cache no error
|
||||
// is returned and the returned SearchResults will be empty.
|
||||
// search implements searcher.
|
||||
func (l *resourceSearchFns[T]) search(ctx context.Context, p SearchParams) (*SearchResult, error) {
|
||||
const op = "daemon.(resourceSearchFns).search"
|
||||
|
||||
var found []T
|
||||
var err error
|
||||
switch p.Query {
|
||||
case "":
|
||||
found, err = l.list(ctx, p.AuthTokenId)
|
||||
default:
|
||||
found, err = l.query(ctx, p.AuthTokenId, p.Query)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op)
|
||||
}
|
||||
|
||||
if p.Filter == "" {
|
||||
return l.searchResult(found), nil
|
||||
}
|
||||
|
||||
e, err := bexpr.CreateEvaluator(p.Filter, bexpr.WithTagName("json"))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("couldn't build filter"), errors.WithCode(errors.InvalidParameter))
|
||||
}
|
||||
finalResults := make([]T, 0, len(found))
|
||||
for _, item := range found {
|
||||
if m, err := e.Evaluate(filterItem{item}); err == nil && m {
|
||||
finalResults = append(finalResults, item)
|
||||
}
|
||||
}
|
||||
return l.searchResult(finalResults), nil
|
||||
}
|
||||
|
||||
type filterItem struct {
|
||||
Item any `json:"item"`
|
||||
}
|
||||
+234
@@ -0,0 +1,234 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/api/sessions"
|
||||
"github.com/hashicorp/boundary/api/targets"
|
||||
cachedb "github.com/hashicorp/boundary/internal/clientcache/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestNewSearchService(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
t.Run("nil repo", func(t *testing.T) {
|
||||
ss, err := NewSearchService(ctx, nil)
|
||||
assert.Error(t, err)
|
||||
assert.ErrorContains(t, err, "repo is nil")
|
||||
assert.Nil(t, ss)
|
||||
})
|
||||
|
||||
t.Run("success", func(t *testing.T) {
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
r, err := NewRepository(ctx, s, &sync.Map{},
|
||||
mapBasedAuthTokenKeyringLookup(nil),
|
||||
sliceBasedAuthTokenBoundaryReader(nil))
|
||||
assert.NoError(t, err)
|
||||
|
||||
ss, err := NewSearchService(ctx, r)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, ss)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSearch_Errors(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
r, err := NewRepository(ctx, s, &sync.Map{},
|
||||
mapBasedAuthTokenKeyringLookup(nil),
|
||||
sliceBasedAuthTokenBoundaryReader(nil))
|
||||
assert.NoError(t, err)
|
||||
|
||||
ss, err := NewSearchService(ctx, r)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, ss)
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
params SearchParams
|
||||
errorContains string
|
||||
}{
|
||||
{
|
||||
name: "missing resource",
|
||||
params: SearchParams{
|
||||
Resource: "",
|
||||
AuthTokenId: "at_1",
|
||||
},
|
||||
errorContains: "invalid resource",
|
||||
},
|
||||
{
|
||||
name: "missing auth token id",
|
||||
params: SearchParams{
|
||||
Resource: "targets",
|
||||
AuthTokenId: "",
|
||||
},
|
||||
errorContains: "missing auth token id",
|
||||
},
|
||||
{
|
||||
name: "unrecognized resource",
|
||||
params: SearchParams{
|
||||
Resource: "unknown",
|
||||
AuthTokenId: "at_1",
|
||||
},
|
||||
errorContains: "invalid resource",
|
||||
},
|
||||
{
|
||||
name: "bad filter",
|
||||
params: SearchParams{
|
||||
Resource: "targets",
|
||||
AuthTokenId: "at_1",
|
||||
Filter: "unknown=filter?syntax!",
|
||||
},
|
||||
errorContains: "couldn't build filter",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
res, err := ss.Search(ctx, tc.params)
|
||||
assert.Error(t, err)
|
||||
assert.ErrorContains(t, err, tc.errorContains)
|
||||
assert.Nil(t, res)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearch(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
at := &AuthToken{
|
||||
Id: "at_1",
|
||||
UserId: "u_1",
|
||||
}
|
||||
{
|
||||
u := &user{Id: at.UserId, Address: "address"}
|
||||
rw := db.New(s)
|
||||
require.NoError(t, rw.Create(ctx, u))
|
||||
require.NoError(t, rw.Create(ctx, at))
|
||||
|
||||
targets := []any{
|
||||
&Target{UserId: u.Id, Id: "t_1", Name: "one", Item: `{"id": "t_1", "name": "one"}`},
|
||||
&Target{UserId: u.Id, Id: "t_2", Name: "two", Item: `{"id": "t_2", "name": "two"}`},
|
||||
}
|
||||
require.NoError(t, rw.CreateItems(ctx, targets))
|
||||
|
||||
sessions := []any{
|
||||
&Session{UserId: u.Id, Id: "s_1", Endpoint: "one", Item: `{"id": "s_1", "endpoint": "one"}`},
|
||||
&Session{UserId: u.Id, Id: "s_2", Endpoint: "two", Item: `{"id": "s_2", "endpoint": "two"}`},
|
||||
}
|
||||
require.NoError(t, rw.CreateItems(ctx, sessions))
|
||||
}
|
||||
|
||||
r, err := NewRepository(ctx, s, &sync.Map{},
|
||||
mapBasedAuthTokenKeyringLookup(nil),
|
||||
sliceBasedAuthTokenBoundaryReader(nil))
|
||||
assert.NoError(t, err)
|
||||
|
||||
ss, err := NewSearchService(ctx, r)
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, ss)
|
||||
|
||||
t.Run("List targets", func(t *testing.T) {
|
||||
got, err := ss.Search(ctx, SearchParams{
|
||||
Resource: "targets",
|
||||
AuthTokenId: at.Id,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, &SearchResult{Targets: []*targets.Target{
|
||||
{Id: "t_1", Name: "one"},
|
||||
{Id: "t_2", Name: "two"},
|
||||
}}, got)
|
||||
})
|
||||
|
||||
t.Run("query targets", func(t *testing.T) {
|
||||
got, err := ss.Search(ctx, SearchParams{
|
||||
Resource: "targets",
|
||||
AuthTokenId: at.Id,
|
||||
Query: `name="one"`,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, &SearchResult{Targets: []*targets.Target{
|
||||
{Id: "t_1", Name: "one"},
|
||||
}}, got)
|
||||
})
|
||||
|
||||
t.Run("query targets bad column", func(t *testing.T) {
|
||||
got, err := ss.Search(ctx, SearchParams{
|
||||
Resource: "targets",
|
||||
AuthTokenId: at.Id,
|
||||
Query: `item % "one"`,
|
||||
})
|
||||
assert.Error(t, err)
|
||||
assert.ErrorContains(t, err, `invalid column "item"`)
|
||||
assert.Nil(t, got)
|
||||
})
|
||||
|
||||
t.Run("Filter targets", func(t *testing.T) {
|
||||
got, err := ss.Search(ctx, SearchParams{
|
||||
Resource: "targets",
|
||||
AuthTokenId: at.Id,
|
||||
Filter: `"/item/name" matches "one"`,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, &SearchResult{Targets: []*targets.Target{
|
||||
{Id: "t_1", Name: "one"},
|
||||
}}, got)
|
||||
})
|
||||
|
||||
t.Run("List sessions", func(t *testing.T) {
|
||||
got, err := ss.Search(ctx, SearchParams{
|
||||
Resource: "sessions",
|
||||
AuthTokenId: at.Id,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, &SearchResult{Sessions: []*sessions.Session{
|
||||
{Id: "s_1", Endpoint: "one"},
|
||||
{Id: "s_2", Endpoint: "two"},
|
||||
}}, got)
|
||||
})
|
||||
|
||||
t.Run("query sessions", func(t *testing.T) {
|
||||
got, err := ss.Search(ctx, SearchParams{
|
||||
Resource: "sessions",
|
||||
AuthTokenId: at.Id,
|
||||
Query: `endpoint="one"`,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, &SearchResult{Sessions: []*sessions.Session{
|
||||
{Id: "s_1", Endpoint: "one"},
|
||||
}}, got)
|
||||
})
|
||||
|
||||
t.Run("Filter sessions", func(t *testing.T) {
|
||||
got, err := ss.Search(ctx, SearchParams{
|
||||
Resource: "sessions",
|
||||
AuthTokenId: at.Id,
|
||||
Filter: `"/item/endpoint" matches "one"`,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, &SearchResult{Sessions: []*sessions.Session{
|
||||
{Id: "s_1", Endpoint: "one"},
|
||||
}}, got)
|
||||
})
|
||||
|
||||
t.Run("unrecognized auth token", func(t *testing.T) {
|
||||
got, err := ss.Search(ctx, SearchParams{
|
||||
Resource: "targets",
|
||||
AuthTokenId: "unrecognized",
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, &SearchResult{Targets: []*targets.Target{}}, got)
|
||||
})
|
||||
}
|
||||
+508
@@ -0,0 +1,508 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
cachedb "github.com/hashicorp/boundary/internal/clientcache/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestUser(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
conn, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
rw := db.New(conn)
|
||||
|
||||
t.Run("missing address", func(t *testing.T) {
|
||||
u := &user{
|
||||
Id: "u1",
|
||||
}
|
||||
assert.ErrorContains(t, rw.Create(ctx, &u), "constraint failed")
|
||||
})
|
||||
|
||||
t.Run("missing id", func(t *testing.T) {
|
||||
u := &user{
|
||||
Address: "address",
|
||||
}
|
||||
assert.ErrorContains(t, rw.Create(ctx, &u), "constraint failed")
|
||||
})
|
||||
|
||||
t.Run("create success", func(t *testing.T) {
|
||||
u := &user{
|
||||
Id: "created",
|
||||
Address: "address",
|
||||
}
|
||||
assert.NoError(t, rw.Create(ctx, &u))
|
||||
})
|
||||
|
||||
t.Run("update", func(t *testing.T) {
|
||||
u := &user{
|
||||
Id: "update",
|
||||
Address: "address",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, &u))
|
||||
updatedU := u.clone()
|
||||
updatedU.Address = "updated"
|
||||
_, err := rw.Update(ctx, updatedU, []string{"address"}, nil)
|
||||
require.NoError(t, err)
|
||||
|
||||
lookedUp := u.clone()
|
||||
require.NoError(t, rw.LookupById(ctx, lookedUp))
|
||||
assert.Equal(t, updatedU.Address, lookedUp.Address)
|
||||
})
|
||||
}
|
||||
|
||||
func TestUser_NoMoreTokens(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
conn, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
rw := db.New(conn)
|
||||
|
||||
u := &user{
|
||||
Id: "userId",
|
||||
Address: "address",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, u))
|
||||
|
||||
tok1 := &AuthToken{
|
||||
UserId: u.Id,
|
||||
Id: "at_1",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, tok1))
|
||||
tok2 := &AuthToken{
|
||||
UserId: u.Id,
|
||||
Id: "at_2",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, tok2))
|
||||
assert.NoError(t, rw.LookupById(ctx, u))
|
||||
|
||||
// deleting a single token doesn't remove the user
|
||||
_, err = rw.Exec(ctx, "delete from auth_token where id = ?", []any{tok1.Id})
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, rw.LookupById(ctx, u))
|
||||
|
||||
// deleting both tokens _does_ remove the user
|
||||
_, err = rw.Exec(ctx, "delete from auth_token", nil)
|
||||
require.NoError(t, err)
|
||||
assert.True(t, errors.IsNotFoundError(rw.LookupById(ctx, u)))
|
||||
}
|
||||
|
||||
func TestAuthToken_NoMoreKeyringTokens(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
rw := db.New(s)
|
||||
|
||||
u := &user{
|
||||
Id: "userId",
|
||||
Address: "address",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, u))
|
||||
|
||||
at := &AuthToken{
|
||||
UserId: u.Id,
|
||||
Id: "at_1234567890",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, at))
|
||||
|
||||
kt1 := &KeyringToken{
|
||||
KeyringType: "k1",
|
||||
TokenName: "n1",
|
||||
AuthTokenId: at.Id,
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, kt1))
|
||||
kt2 := &KeyringToken{
|
||||
KeyringType: "k2",
|
||||
TokenName: "n2",
|
||||
AuthTokenId: at.Id,
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, kt2))
|
||||
assert.NoError(t, rw.LookupById(ctx, u))
|
||||
|
||||
// deleting the keyring tokens doesn't remove the user
|
||||
_, err = rw.Exec(ctx, "delete from keyring_token", nil)
|
||||
require.NoError(t, err)
|
||||
assert.NoError(t, rw.LookupById(ctx, at))
|
||||
assert.NoError(t, rw.LookupById(ctx, u))
|
||||
}
|
||||
|
||||
func TestAuthToken(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
rw := db.New(s)
|
||||
|
||||
u := &user{
|
||||
Id: "userId",
|
||||
Address: "address",
|
||||
}
|
||||
|
||||
t.Run("no user foreign key constraint", func(t *testing.T) {
|
||||
tok := &AuthToken{
|
||||
UserId: u.Id,
|
||||
Id: "at_1234567890",
|
||||
}
|
||||
require.ErrorContains(t, rw.Create(ctx, tok), "constraint failed")
|
||||
})
|
||||
|
||||
require.NoError(t, rw.Create(ctx, u))
|
||||
|
||||
t.Run("no user id", func(t *testing.T) {
|
||||
tok := &AuthToken{
|
||||
Id: "at_1234567890",
|
||||
}
|
||||
require.ErrorContains(t, rw.Create(ctx, tok), "constraint failed")
|
||||
})
|
||||
|
||||
t.Run("create", func(t *testing.T) {
|
||||
tok := &AuthToken{
|
||||
UserId: u.Id,
|
||||
Id: "at_create",
|
||||
}
|
||||
before := time.Now().Truncate(1 * time.Millisecond)
|
||||
require.NoError(t, rw.Create(ctx, tok))
|
||||
require.NoError(t, rw.LookupById(ctx, tok))
|
||||
assert.GreaterOrEqual(t, tok.LastAccessedTime, before)
|
||||
})
|
||||
|
||||
t.Run("update", func(t *testing.T) {
|
||||
tok := &AuthToken{
|
||||
UserId: u.Id,
|
||||
Id: "at_update",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, tok))
|
||||
|
||||
tok.LastAccessedTime = time.Now().Add(-(24 * 365 * time.Hour))
|
||||
n, err := rw.Update(ctx, tok, []string{"LastAccessedTime"}, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, n)
|
||||
})
|
||||
|
||||
t.Run("delete user deletes token", func(t *testing.T) {
|
||||
u := &user{
|
||||
Id: "deletethis",
|
||||
Address: "deleted",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, u))
|
||||
|
||||
tok := &AuthToken{
|
||||
UserId: u.Id,
|
||||
Id: "at_deleted",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, tok))
|
||||
|
||||
_, err = rw.Exec(ctx, "delete from user where id = ?", []any{u.Id})
|
||||
|
||||
require.True(t, errors.IsNotFoundError(rw.LookupById(ctx, tok)))
|
||||
})
|
||||
|
||||
// TODO: When gorm sqlite driver fixes it's delete, use rw.Delete instead of the Exec.
|
||||
// n, err := rw.Delete(ctx, p)
|
||||
_, err = rw.Exec(ctx, "delete from auth_token", nil)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestKeyringToken(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
rw := db.New(s)
|
||||
|
||||
u := &user{
|
||||
Id: "userId",
|
||||
Address: "address",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, u))
|
||||
|
||||
at := &AuthToken{
|
||||
Id: "at_1",
|
||||
UserId: u.Id,
|
||||
}
|
||||
|
||||
t.Run("no token foreign key constraint", func(t *testing.T) {
|
||||
kt := &KeyringToken{
|
||||
KeyringType: "keyring",
|
||||
TokenName: "token",
|
||||
AuthTokenId: at.Id,
|
||||
}
|
||||
require.ErrorContains(t, rw.Create(ctx, kt), "constraint failed")
|
||||
})
|
||||
|
||||
require.NoError(t, rw.Create(ctx, at))
|
||||
|
||||
t.Run("no auth token id", func(t *testing.T) {
|
||||
tok := &KeyringToken{
|
||||
KeyringType: "keyring",
|
||||
TokenName: "token",
|
||||
}
|
||||
require.ErrorContains(t, rw.Create(ctx, tok), "constraint failed")
|
||||
})
|
||||
|
||||
t.Run("create", func(t *testing.T) {
|
||||
kt := &KeyringToken{
|
||||
KeyringType: "keyring",
|
||||
TokenName: "create",
|
||||
AuthTokenId: at.Id,
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, kt))
|
||||
require.NoError(t, rw.LookupById(ctx, kt))
|
||||
})
|
||||
|
||||
t.Run("delete user deletes keyring token", func(t *testing.T) {
|
||||
u := &user{
|
||||
Id: "deletethis",
|
||||
Address: "deleted",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, u))
|
||||
|
||||
at := &AuthToken{
|
||||
UserId: u.Id,
|
||||
Id: "at_deleted",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, at))
|
||||
|
||||
kt := &KeyringToken{
|
||||
KeyringType: "keyring",
|
||||
TokenName: "deleted",
|
||||
AuthTokenId: at.Id,
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, kt))
|
||||
require.NoError(t, rw.LookupById(ctx, kt))
|
||||
|
||||
_, err = rw.Exec(ctx, "delete from user where id = ?", []any{u.Id})
|
||||
require.True(t, errors.IsNotFoundError(rw.LookupById(ctx, kt)))
|
||||
})
|
||||
|
||||
t.Run("delete auth token deletes keyring token", func(t *testing.T) {
|
||||
u := &user{
|
||||
Id: "deletethis",
|
||||
Address: "deleted",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, u))
|
||||
|
||||
at := &AuthToken{
|
||||
UserId: u.Id,
|
||||
Id: "at_deleted",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, at))
|
||||
|
||||
kt := &KeyringToken{
|
||||
KeyringType: "keyring",
|
||||
TokenName: "deleted",
|
||||
AuthTokenId: at.Id,
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, kt))
|
||||
require.NoError(t, rw.LookupById(ctx, kt))
|
||||
|
||||
_, err = rw.Exec(ctx, "delete from auth_token where id = ?", []any{at.Id})
|
||||
require.True(t, errors.IsNotFoundError(rw.LookupById(ctx, kt)))
|
||||
})
|
||||
|
||||
// TODO: When gorm sqlite driver fixes it's delete, use rw.Delete instead of the Exec.
|
||||
// n, err := rw.Delete(ctx, p)
|
||||
_, err = rw.Exec(ctx, "delete from keyring_token", nil)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestTarget(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
rw := db.New(s)
|
||||
|
||||
addr := "boundary"
|
||||
userId := "u_12345"
|
||||
u := &user{
|
||||
Id: userId,
|
||||
Address: addr,
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, u))
|
||||
|
||||
t.Run("target without user id", func(t *testing.T) {
|
||||
unknownTarget := &Target{
|
||||
Id: "tssh_1234567890",
|
||||
Name: "target",
|
||||
Description: "target desc",
|
||||
Address: "some address",
|
||||
Item: "{id:'tssh_1234567890'}",
|
||||
}
|
||||
require.ErrorContains(t, rw.Create(ctx, unknownTarget), "constraint failed")
|
||||
})
|
||||
|
||||
t.Run("target actions", func(t *testing.T) {
|
||||
target := &Target{
|
||||
UserId: u.Id,
|
||||
Id: "tssh_1234567890",
|
||||
Name: "target",
|
||||
Description: "target desc",
|
||||
Address: "some address",
|
||||
Item: "{id:'tssh_1234567890'}",
|
||||
}
|
||||
|
||||
require.NoError(t, rw.Create(ctx, target))
|
||||
|
||||
require.NoError(t, rw.LookupById(ctx, target))
|
||||
|
||||
target.Address = "new address"
|
||||
n, err := rw.Update(ctx, target, []string{"address"}, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, n)
|
||||
|
||||
// TODO: Once the sqlite driver properly builds the delete query call
|
||||
// n, err = rw.Delete(ctx, target) instead of the Exec call
|
||||
n, err = rw.Exec(ctx, "delete from target where (user_id, id) IN (values (?, ?))",
|
||||
[]any{target.UserId, target.Id})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, n)
|
||||
})
|
||||
|
||||
t.Run("lookup a target", func(t *testing.T) {
|
||||
target := &Target{
|
||||
UserId: u.Id,
|
||||
Id: "tssh_1234567890",
|
||||
Name: "target",
|
||||
Description: "target desc",
|
||||
Address: "some address",
|
||||
Item: "{id:'tssh_1234567890'}",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, target))
|
||||
|
||||
lookTar := &Target{
|
||||
UserId: target.UserId,
|
||||
Id: target.Id,
|
||||
}
|
||||
assert.NoError(t, rw.LookupById(ctx, lookTar))
|
||||
assert.NotNil(t, lookTar)
|
||||
|
||||
// cleanup the targets
|
||||
_, err := rw.Exec(ctx, "delete from target", nil)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("deleting the user deletes the target", func(t *testing.T) {
|
||||
target := &Target{
|
||||
UserId: u.Id,
|
||||
Id: "tssh_1234567890",
|
||||
Name: "target",
|
||||
Description: "target desc",
|
||||
Address: "some address",
|
||||
Item: "{id:'tssh_1234567890'}",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, target))
|
||||
// Deleting the user deletes the target
|
||||
// TODO: Once the sqlite driver supports proper deletes change from the
|
||||
// Exec call to .Delete
|
||||
// n, err := rw.Delete(ctx, &u)
|
||||
n, err := rw.Exec(ctx, "delete from user where id = ?", []any{userId})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, n)
|
||||
|
||||
lookTar := &Target{
|
||||
UserId: target.UserId,
|
||||
Id: target.Id,
|
||||
}
|
||||
assert.ErrorContains(t, rw.LookupById(ctx, lookTar), "not found")
|
||||
})
|
||||
}
|
||||
|
||||
func TestSession(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
s, err := cachedb.Open(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
rw := db.New(s)
|
||||
|
||||
addr := "boundary"
|
||||
userId := "u_12345"
|
||||
u := &user{
|
||||
Id: userId,
|
||||
Address: addr,
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, u))
|
||||
|
||||
t.Run("session without user id", func(t *testing.T) {
|
||||
unknownSess := &Session{
|
||||
Id: "sess_1234567890",
|
||||
Item: "{id:'sess_1234567890'}",
|
||||
}
|
||||
require.ErrorContains(t, rw.Create(ctx, unknownSess), "FOREIGN KEY constraint")
|
||||
})
|
||||
t.Run("session actions", func(t *testing.T) {
|
||||
session := &Session{
|
||||
UserId: u.Id,
|
||||
Id: "s_1234567890",
|
||||
Endpoint: "endpoint",
|
||||
Item: "{id:'s_1234567890'}",
|
||||
}
|
||||
|
||||
require.NoError(t, rw.Create(ctx, session))
|
||||
|
||||
require.NoError(t, rw.LookupById(ctx, session))
|
||||
|
||||
session.Endpoint = "new endpoint"
|
||||
n, err := rw.Update(ctx, session, []string{"endpoint"}, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, n)
|
||||
|
||||
// TODO: Once the sqlite driver properly builds the delete query call
|
||||
// n, err = rw.Delete(ctx, session) instead of the Exec call
|
||||
n, err = rw.Exec(ctx, "delete from session where (user_id, id) IN (values (?, ?))",
|
||||
[]any{session.UserId, session.Id})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, n)
|
||||
})
|
||||
|
||||
t.Run("lookup a session", func(t *testing.T) {
|
||||
session := &Session{
|
||||
UserId: u.Id,
|
||||
Id: "s_1234567890",
|
||||
Endpoint: "endpoint",
|
||||
Item: "{id:'s_1234567890'}",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, session))
|
||||
|
||||
lookSess := &Session{
|
||||
UserId: u.Id,
|
||||
Id: session.Id,
|
||||
}
|
||||
assert.NoError(t, rw.LookupById(ctx, lookSess))
|
||||
assert.NotNil(t, lookSess)
|
||||
|
||||
// cleanup the sessions
|
||||
_, err := rw.Exec(ctx, "delete from session", nil)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("deleting the user deletes the session", func(t *testing.T) {
|
||||
session := &Session{
|
||||
UserId: u.Id,
|
||||
Id: "s_1234567890",
|
||||
Endpoint: "endpoint",
|
||||
Item: "{id:'s_1234567890'}",
|
||||
}
|
||||
require.NoError(t, rw.Create(ctx, session))
|
||||
// Deleting the user deletes the session
|
||||
// TODO: Once the sqlite driver supports proper deletes change from the
|
||||
// Exec call to .Delete
|
||||
// n, err := rw.Delete(ctx, &u)
|
||||
n, err := rw.Exec(ctx, "delete from user where id = ?", []any{userId})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, n)
|
||||
|
||||
lookSess := &Session{
|
||||
UserId: session.UserId,
|
||||
Id: session.Id,
|
||||
}
|
||||
assert.ErrorContains(t, rw.LookupById(ctx, lookSess), "not found")
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user