diff --git a/internal/clientcache/internal/cache/refresh_test.go b/internal/clientcache/internal/cache/refresh_test.go index c9af8da4e..871bb8b2c 100644 --- a/internal/clientcache/internal/cache/refresh_test.go +++ b/internal/clientcache/internal/cache/refresh_test.go @@ -940,11 +940,13 @@ func TestFullFetch(t *testing.T) { 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", + Id: fmt.Sprintf("target_%s", suffix), + Name: fmt.Sprintf("name_%s", suffix), + Description: fmt.Sprintf("description_%s", suffix), + Address: fmt.Sprintf("address_%s", suffix), + ScopeId: fmt.Sprintf("p_%s", suffix), + Type: "tcp", + SessionMaxSeconds: 1234, } } diff --git a/internal/clientcache/internal/cache/repository_targets_test.go b/internal/clientcache/internal/cache/repository_targets_test.go index ab441d4c4..b3387b0b2 100644 --- a/internal/clientcache/internal/cache/repository_targets_test.go +++ b/internal/clientcache/internal/cache/repository_targets_test.go @@ -8,6 +8,7 @@ import ( "sync" "testing" + "github.com/hashicorp/boundary/api" "github.com/hashicorp/boundary/api/authtokens" "github.com/hashicorp/boundary/api/targets" "github.com/hashicorp/boundary/globals" @@ -137,6 +138,66 @@ func TestRepository_refreshTargets(t *testing.T) { } } +func TestRepository_RefreshTargets_InvalidListTokenError(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)) + + ts := [][]*targets.Target{ + { + target("1"), + target("2"), + target("3"), + }, + } + + var withRefreshToken int + var withoutRefreshToken int + // invalidAuthTokenFunc returns an invalid auth token error if an auth token + // is provided, otherwise it returns the tokens with a new auth token. + invalidAuthTokenFunc := func(ctx context.Context, addr, authTok string, refreshTok RefreshTokenValue) (ret []*targets.Target, removedIds []string, refreshToken RefreshTokenValue, err error) { + if refreshTok != "" { + withRefreshToken += 1 + return nil, nil, "", api.ErrInvalidListToken + } + withoutRefreshToken += 1 + return testStaticResourceRetrievalFunc(t, ts, [][]string{nil})(ctx, addr, authTok, refreshTok) + } + + require.NoError(t, r.refreshTargets(ctx, &u, map[AuthToken]string{{Id: "id"}: "something"}, + WithTargetRetrievalFunc(invalidAuthTokenFunc))) + + // This time an invalid auth token should be returned, and refreshTargets should fall back + // to requesting without one. + require.NoError(t, r.refreshTargets(ctx, &u, map[AuthToken]string{{Id: "id"}: "something"}, + WithTargetRetrievalFunc(invalidAuthTokenFunc))) + + assert.Equal(t, 1, withRefreshToken) + assert.Equal(t, 2, withoutRefreshToken) +} + func TestRepository_RefreshTargets_withRefreshTokens(t *testing.T) { ctx := context.Background() s, err := cachedb.Open(ctx) @@ -166,31 +227,10 @@ func TestRepository_RefreshTargets_withRefreshTokens(t *testing.T) { ts := [][]*targets.Target{ { - { - Id: "ttcp_1", - Name: "name1", - Address: "address1", - Type: "tcp", - ScopeId: "p_123", - SessionMaxSeconds: 111, - }, - { - Id: "ttcp_2", - Name: "name2", - Address: "address2", - Type: "tcp", - ScopeId: "p_123", - SessionMaxSeconds: 222, - }, + target("1"), + target("2"), }, { - { - Id: "ttcp_3", - Name: "name3", - Address: "address3", - Type: "tcp", - ScopeId: "p_123", - SessionMaxSeconds: 333, - }, + target("3"), }, } @@ -274,30 +314,9 @@ func TestRepository_ListTargets(t *testing.T) { }) ts := []*targets.Target{ - { - Id: "ttcp_1", - Name: "name1", - Address: "address1", - Type: "tcp", - ScopeId: "p_123", - SessionMaxSeconds: 111, - }, - { - Id: "ttcp_2", - Name: "name2", - Address: "address2", - Type: "tcp", - ScopeId: "p_123", - SessionMaxSeconds: 222, - }, - { - Id: "ttcp_3", - Name: "name3", - Address: "address3", - Type: "tcp", - ScopeId: "p_123", - SessionMaxSeconds: 333, - }, + target("1"), + target("2"), + target("3"), } require.NoError(t, r.refreshTargets(ctx, u1, map[AuthToken]string{{Id: "id"}: "something"}, WithTargetRetrievalFunc(testStaticResourceRetrievalFunc(t, [][]*targets.Target{ts}, [][]string{nil})))) diff --git a/internal/daemon/controller/handlers/errors.go b/internal/daemon/controller/handlers/errors.go index ec13358c0..ff47066f0 100644 --- a/internal/daemon/controller/handlers/errors.go +++ b/internal/daemon/controller/handlers/errors.go @@ -153,13 +153,13 @@ func InvalidArgumentErrorf(msg string, fields map[string]string) *ApiError { return apiErr } -func invalidRefreshTokenError(err error) *ApiError { - const op = "handlers.invalidRefreshTokenError" +func invalidListTokenError(err error) *ApiError { + const op = "handlers.invalidListTokenError" ctx := context.TODO() var domainErr *errors.Err if !errors.As(err, &domainErr) { - event.WriteError(ctx, op, err, event.WithInfoMsg("Unable to build invalid argument api error.")) + event.WriteError(ctx, op, err, event.WithInfoMsg("Unable to build invalid list token api error.")) } return &ApiError{ @@ -219,7 +219,7 @@ func backendErrorToApiError(inErr error) *ApiError { case errors.Match(errors.T(errors.AccountAlreadyAssociated), inErr): return InvalidArgumentErrorf(inErr.Error(), nil) case errors.Match(errors.T(errors.InvalidListToken), inErr): - return invalidRefreshTokenError(inErr) + return invalidListTokenError(inErr) case errors.Match(errors.T(errors.InvalidFieldMask), inErr), errors.Match(errors.T(errors.EmptyFieldMask), inErr): return InvalidArgumentErrorf("Error in provided request", map[string]string{"update_mask": "Invalid update mask provided."}) case errors.IsUniqueError(inErr): diff --git a/internal/listtoken/list_token.go b/internal/listtoken/list_token.go index 8ad657902..16710ae11 100644 --- a/internal/listtoken/list_token.go +++ b/internal/listtoken/list_token.go @@ -353,58 +353,58 @@ func (tk *Token) Validate( const op = "listtoken.Validate" switch { case tk == nil: - return errors.New(ctx, errors.InvalidParameter, op, "list token was missing") + return errors.New(ctx, errors.InvalidListToken, op, "list token was missing") case len(tk.GrantsHash) == 0: - return errors.New(ctx, errors.InvalidParameter, op, "list token was missing its grants hash") + return errors.New(ctx, errors.InvalidListToken, op, "list token was missing its grants hash") case !bytes.Equal(tk.GrantsHash, expectedGrantsHash): - return errors.New(ctx, errors.InvalidParameter, op, "grants have changed since list token was issued") + return errors.New(ctx, errors.InvalidListToken, op, "grants have changed since list token was issued") case tk.CreateTime.After(time.Now()): - return errors.New(ctx, errors.InvalidParameter, op, "list token was created in the future") + return errors.New(ctx, errors.InvalidListToken, op, "list token was created in the future") // Tokens older than 30 days have expired case tk.CreateTime.Before(time.Now().AddDate(0, 0, -30)): - return errors.New(ctx, errors.InvalidParameter, op, "list token was expired") + return errors.New(ctx, errors.InvalidListToken, op, "list token was expired") case tk.ResourceType != expectedResourceType: - return errors.New(ctx, errors.InvalidParameter, op, "list token resource type does not match expected resource type") + return errors.New(ctx, errors.InvalidListToken, op, "list token resource type does not match expected resource type") } switch st := tk.Subtype.(type) { case *RefreshToken: switch { case st.PhaseUpperBound.Before(tk.CreateTime): - return errors.New(ctx, errors.InvalidParameter, op, "list token's refresh component's phase upper bound was before its creation time") + return errors.New(ctx, errors.InvalidListToken, op, "list token's refresh component's phase upper bound was before its creation time") case st.PhaseUpperBound.After(time.Now()): - return errors.New(ctx, errors.InvalidParameter, op, "list token's refresh component's phase upper bound was in the future") + return errors.New(ctx, errors.InvalidListToken, op, "list token's refresh component's phase upper bound was in the future") case st.PhaseLowerBound.Before(tk.CreateTime): - return errors.New(ctx, errors.InvalidParameter, op, "list token's refresh component's phase lower bound was before its creation time") + return errors.New(ctx, errors.InvalidListToken, op, "list token's refresh component's phase lower bound was before its creation time") case st.PhaseLowerBound.After(time.Now()): - return errors.New(ctx, errors.InvalidParameter, op, "list token's refresh component's phase lower bound was in the future") + return errors.New(ctx, errors.InvalidListToken, op, "list token's refresh component's phase lower bound was in the future") case st.PhaseUpperBound.Before(st.PhaseLowerBound): - return errors.New(ctx, errors.InvalidParameter, op, "list token's refresh component's phase upper bound was before the phase lower bound") + return errors.New(ctx, errors.InvalidListToken, op, "list token's refresh component's phase upper bound was before the phase lower bound") case st.PreviousDeletedIdsTime.Before(tk.CreateTime): - return errors.New(ctx, errors.InvalidParameter, op, "list token's refresh component previous deleted ids time was before its creation time") + return errors.New(ctx, errors.InvalidListToken, op, "list token's refresh component previous deleted ids time was before its creation time") case st.PreviousDeletedIdsTime.After(time.Now()): - return errors.New(ctx, errors.InvalidParameter, op, "list token's refresh component previous deleted ids time was in the future") + return errors.New(ctx, errors.InvalidListToken, op, "list token's refresh component previous deleted ids time was in the future") case st.LastItemId == "": - return errors.New(ctx, errors.InvalidParameter, op, "list token's refresh component missing last item ID") + return errors.New(ctx, errors.InvalidListToken, op, "list token's refresh component missing last item ID") case st.LastItemUpdateTime.After(time.Now()): - return errors.New(ctx, errors.InvalidParameter, op, "list token's refresh component's last item was updated in the future") + return errors.New(ctx, errors.InvalidListToken, op, "list token's refresh component's last item was updated in the future") } case *PaginationToken: switch { case st.LastItemId == "": - return errors.New(ctx, errors.InvalidParameter, op, "list tokens's pagination component missing last item ID") + return errors.New(ctx, errors.InvalidListToken, op, "list tokens's pagination component missing last item ID") case st.LastItemCreateTime.After(time.Now()): - return errors.New(ctx, errors.InvalidParameter, op, "list token's pagination component's last item was created in the future") + return errors.New(ctx, errors.InvalidListToken, op, "list token's pagination component's last item was created in the future") } case *StartRefreshToken: switch { case st.PreviousPhaseUpperBound.Before(tk.CreateTime): - return errors.New(ctx, errors.InvalidParameter, op, "list token's start refresh component's previous phase upper bound was before its creation time") + return errors.New(ctx, errors.InvalidListToken, op, "list token's start refresh component's previous phase upper bound was before its creation time") case st.PreviousPhaseUpperBound.After(time.Now()): - return errors.New(ctx, errors.InvalidParameter, op, "list token's start refresh component's previous phase upper bound was in the future") + return errors.New(ctx, errors.InvalidListToken, op, "list token's start refresh component's previous phase upper bound was in the future") case st.PreviousDeletedIdsTime.Before(tk.CreateTime): - return errors.New(ctx, errors.InvalidParameter, op, "list token's start refresh component previous deleted ids time was before its creation time") + return errors.New(ctx, errors.InvalidListToken, op, "list token's start refresh component previous deleted ids time was before its creation time") case st.PreviousDeletedIdsTime.After(time.Now()): - return errors.New(ctx, errors.InvalidParameter, op, "list token's start refresh component previous deleted ids time was in the future") + return errors.New(ctx, errors.InvalidListToken, op, "list token's start refresh component previous deleted ids time was in the future") } } diff --git a/internal/listtoken/list_token_test.go b/internal/listtoken/list_token_test.go index 08bf864ba..844bf2521 100644 --- a/internal/listtoken/list_token_test.go +++ b/internal/listtoken/list_token_test.go @@ -437,7 +437,7 @@ func Test_ValidateListToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token was missing", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "no grants hash", @@ -449,7 +449,7 @@ func Test_ValidateListToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token was missing its grants hash", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "changed grants hash", @@ -461,7 +461,7 @@ func Test_ValidateListToken(t *testing.T) { grantsHash: []byte("some other hash"), resourceType: resource.Target, wantErrString: "grants have changed since list token was issued", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "created in the future", @@ -473,7 +473,7 @@ func Test_ValidateListToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token was created in the future", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "expired", @@ -485,7 +485,7 @@ func Test_ValidateListToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token was expired", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "resource type mismatch", @@ -497,7 +497,7 @@ func Test_ValidateListToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.SessionRecording, wantErrString: "list token resource type does not match expected resource type", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, } for _, tt := range tests { @@ -554,7 +554,7 @@ func Test_ValidatePaginationToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list tokens's pagination component missing last item ID", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "updated in the future", @@ -570,7 +570,7 @@ func Test_ValidatePaginationToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's pagination component's last item was created in the future", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, } for _, tt := range tests { @@ -627,7 +627,7 @@ func Test_ValidateStartRefreshToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's start refresh component's previous phase upper bound was before its creation time", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "previous phase upper bound in future", @@ -643,7 +643,7 @@ func Test_ValidateStartRefreshToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's start refresh component's previous phase upper bound was in the future", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "previous deleted ids time before create time", @@ -659,7 +659,7 @@ func Test_ValidateStartRefreshToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's start refresh component previous deleted ids time was before its creation time", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "previous deleted ids time in future", @@ -675,7 +675,7 @@ func Test_ValidateStartRefreshToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's start refresh component previous deleted ids time was in the future", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, } for _, tt := range tests { @@ -738,7 +738,7 @@ func Test_ValidateRefreshToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's refresh component's phase upper bound was before its creation time", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "phase upper bound before phase lower bound", @@ -757,7 +757,7 @@ func Test_ValidateRefreshToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's refresh component's phase upper bound was before the phase lower bound", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "phase upper bound in future", @@ -776,7 +776,7 @@ func Test_ValidateRefreshToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's refresh component's phase upper bound was in the future", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "phase lower bound before create time", @@ -795,7 +795,7 @@ func Test_ValidateRefreshToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's refresh component's phase lower bound was before its creation time", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "phase lower bound in future", @@ -814,7 +814,7 @@ func Test_ValidateRefreshToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's refresh component's phase lower bound was in the future", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "previous deleted ids time before create time", @@ -833,7 +833,7 @@ func Test_ValidateRefreshToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's refresh component previous deleted ids time was before its creation time", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "previous deleted ids time in future", @@ -852,7 +852,7 @@ func Test_ValidateRefreshToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's refresh component previous deleted ids time was in the future", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "emtpy last item id", @@ -871,7 +871,7 @@ func Test_ValidateRefreshToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's refresh component missing last item ID", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, { name: "last item update in future", @@ -890,7 +890,7 @@ func Test_ValidateRefreshToken(t *testing.T) { grantsHash: []byte("some hash"), resourceType: resource.Target, wantErrString: "list token's refresh component's last item was updated in the future", - wantErrCode: errors.InvalidParameter, + wantErrCode: errors.InvalidListToken, }, } for _, tt := range tests {