Merge pull request #4401 from hashicorp/jefferai-linter-fixes
Fix linter warnings, parts 1 and 2
This commit is contained in:
91 files changed
+472
-411
No files matched your search
@@ -8,7 +8,6 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
"github.com/hashicorp/boundary/internal/auth/password"
|
||||
"github.com/hashicorp/boundary/internal/authtoken/store"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
@@ -18,6 +17,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
// This file contains tests for methods defined in authtoken.go as well as tests which exercise the db
|
||||
@@ -43,8 +43,7 @@ func TestAuthToken_DbUpdate(t *testing.T) {
|
||||
nullMask []string
|
||||
authTok *store.AuthToken
|
||||
}
|
||||
future, err := ptypes.TimestampProto(time.Now().Add(time.Hour))
|
||||
require.NoError(t, err)
|
||||
future := timestamppb.New(time.Now().Add(time.Hour))
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
@@ -9,12 +9,12 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
"github.com/hashicorp/boundary/internal/db/timestamp"
|
||||
"github.com/hashicorp/boundary/internal/errors"
|
||||
"github.com/hashicorp/boundary/internal/iam"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -104,10 +104,7 @@ func (r *Repository) CreateAuthToken(ctx context.Context, withIamUser *iam.User,
|
||||
|
||||
// We truncate the expiration time to the nearest second to make testing in different platforms with
|
||||
// different time resolutions easier.
|
||||
expiration, err := ptypes.TimestampProto(time.Now().Add(r.timeToLiveDuration).Truncate(time.Second))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithCode(errors.InvalidTimeStamp))
|
||||
}
|
||||
expiration := timestamppb.New(time.Now().Add(r.timeToLiveDuration).Truncate(time.Second))
|
||||
at.ExpirationTime = ×tamp.Timestamp{Timestamp: expiration}
|
||||
|
||||
var newAuthToken *AuthToken
|
||||
@@ -215,14 +212,8 @@ func (r *Repository) ValidateToken(ctx context.Context, id, token string, opt ..
|
||||
}
|
||||
|
||||
// If the token is too old or stale invalidate it and return nothing.
|
||||
exp, err := ptypes.Timestamp(retAT.GetExpirationTime().GetTimestamp())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("expiration time"), errors.WithCode(errors.InvalidTimeStamp))
|
||||
}
|
||||
lastAccessed, err := ptypes.Timestamp(retAT.GetApproximateLastAccessTime().GetTimestamp())
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(ctx, err, op, errors.WithMsg("last accessed time"), errors.WithCode(errors.InvalidTimeStamp))
|
||||
}
|
||||
exp := retAT.GetExpirationTime().AsTime()
|
||||
lastAccessed := retAT.GetApproximateLastAccessTime().AsTime()
|
||||
|
||||
now := time.Now()
|
||||
sinceLastAccessed := now.Sub(lastAccessed) + timeSkew
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
"github.com/google/go-cmp/cmp"
|
||||
"github.com/hashicorp/boundary/globals"
|
||||
"github.com/hashicorp/boundary/internal/auth/password"
|
||||
@@ -19,6 +18,7 @@ import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/testing/protocmp"
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/authtoken/store"
|
||||
"github.com/hashicorp/boundary/internal/db"
|
||||
@@ -365,10 +365,8 @@ func TestRepository_LookupAuthToken(t *testing.T) {
|
||||
// is because the resolution of the timestamp in the db does not
|
||||
// match the resolution in Go code. But might be worth checking
|
||||
// into.
|
||||
wantGoTimeExpr, err := ptypes.Timestamp(tt.want.AuthToken.GetExpirationTime().Timestamp)
|
||||
require.NoError(err)
|
||||
gotGoTimeExpr, err := ptypes.Timestamp(got.AuthToken.GetExpirationTime().Timestamp)
|
||||
require.NoError(err)
|
||||
wantGoTimeExpr := tt.want.AuthToken.GetExpirationTime().Timestamp.AsTime()
|
||||
gotGoTimeExpr := got.AuthToken.GetExpirationTime().Timestamp.AsTime()
|
||||
assert.WithinDuration(wantGoTimeExpr, gotGoTimeExpr, time.Millisecond)
|
||||
tt.want.AuthToken.ExpirationTime = got.AuthToken.ExpirationTime
|
||||
assert.Empty(cmp.Diff(tt.want.AuthToken, got.AuthToken, protocmp.Transform()))
|
||||
@@ -397,8 +395,7 @@ func TestRepository_ValidateToken(t *testing.T) {
|
||||
at.Token = ""
|
||||
at.CtToken = nil
|
||||
at.KeyId = ""
|
||||
atTime, err := ptypes.Timestamp(at.GetApproximateLastAccessTime().GetTimestamp())
|
||||
require.NoError(t, err)
|
||||
atTime := at.GetApproximateLastAccessTime()
|
||||
require.NotNil(t, atTime)
|
||||
|
||||
badId, err := NewAuthTokenId(ctx)
|
||||
@@ -465,18 +462,16 @@ func TestRepository_ValidateToken(t *testing.T) {
|
||||
require.NotNil(tt.want, "Got %v but wanted nil", got)
|
||||
|
||||
// NOTE: See comment in LookupAuthToken about this logic
|
||||
wantGoTimeExpr, err := ptypes.Timestamp(tt.want.AuthToken.GetExpirationTime().Timestamp)
|
||||
require.NoError(err)
|
||||
gotGoTimeExpr, err := ptypes.Timestamp(got.AuthToken.GetExpirationTime().Timestamp)
|
||||
require.NoError(err)
|
||||
wantGoTimeExpr := tt.want.AuthToken.GetExpirationTime().AsTime()
|
||||
gotGoTimeExpr := got.AuthToken.GetExpirationTime().AsTime()
|
||||
assert.WithinDuration(wantGoTimeExpr, gotGoTimeExpr, time.Millisecond)
|
||||
tt.want.AuthToken.ExpirationTime = got.AuthToken.ExpirationTime
|
||||
assert.Empty(cmp.Diff(tt.want.AuthToken, got.AuthToken, protocmp.Transform()))
|
||||
|
||||
// preTime1 should be the value prior to the ValidateToken was called so it should equal creation time
|
||||
preTime1, err := ptypes.Timestamp(got.GetApproximateLastAccessTime().GetTimestamp())
|
||||
preTime1 := got.GetApproximateLastAccessTime()
|
||||
require.NoError(err)
|
||||
assert.True(preTime1.Equal(atTime), "Create time %q doesn't match the time from the first call to MaybeUpdateLastAccesssed: %q.", atTime, preTime1)
|
||||
assert.True(preTime1.AsTime().Equal(atTime.AsTime()), "Create time %q doesn't match the time from the first call to MaybeUpdateLastAccesssed: %q.", atTime, preTime1)
|
||||
|
||||
// Enable the duration which limits how frequently a token's approximate last accessed time can be updated
|
||||
// so the next call doesn't cause the last accessed time to be updated.
|
||||
@@ -484,18 +479,16 @@ func TestRepository_ValidateToken(t *testing.T) {
|
||||
|
||||
got2, err := repo.ValidateToken(ctx, tt.id, tt.token)
|
||||
assert.NoError(err)
|
||||
preTime2, err := ptypes.Timestamp(got2.GetApproximateLastAccessTime().GetTimestamp())
|
||||
require.NoError(err)
|
||||
assert.True(preTime2.After(preTime1), "First updated time %q was not after the creation time %q", preTime2, preTime1)
|
||||
preTime2 := got2.GetApproximateLastAccessTime().GetTimestamp()
|
||||
assert.True(preTime2.AsTime().After(preTime1.AsTime()), "First updated time %q was not after the creation time %q", preTime2, preTime1)
|
||||
|
||||
// We should find no oplog since tokens are not replicated, so they don't need oplog entries.
|
||||
assert.Error(db.TestVerifyOplog(t, rw, got.GetPublicId(), db.WithOperation(oplog.OpType_OP_TYPE_UPDATE)))
|
||||
|
||||
got3, err := repo.ValidateToken(ctx, tt.id, tt.token)
|
||||
require.NoError(err)
|
||||
preTime3, err := ptypes.Timestamp(got3.GetApproximateLastAccessTime().GetTimestamp())
|
||||
require.NoError(err)
|
||||
assert.True(preTime3.Equal(preTime2), "The 3rd timestamp %q was not equal to the second time %q", preTime3, preTime2)
|
||||
preTime3 := got3.GetApproximateLastAccessTime()
|
||||
assert.True(preTime3.AsTime().Equal(preTime2.AsTime()), "The 3rd timestamp %q was not equal to the second time %q", preTime3, preTime2)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -880,11 +873,9 @@ func Test_IssuePendingToken(t *testing.T) {
|
||||
}
|
||||
require.NoError(err)
|
||||
assert.NotEmpty(tk)
|
||||
accessTime, err := ptypes.Timestamp(tk.GetApproximateLastAccessTime().GetTimestamp())
|
||||
require.NoError(err)
|
||||
createTime, err := ptypes.Timestamp(tk.GetCreateTime().GetTimestamp())
|
||||
require.NoError(err)
|
||||
assert.True(accessTime.After(createTime), "last access time %q was not after the creation time %q", accessTime, createTime)
|
||||
accessTime := tk.GetApproximateLastAccessTime()
|
||||
createTime := tk.GetCreateTime().GetTimestamp()
|
||||
assert.True(accessTime.AsTime().After(createTime.AsTime()), "last access time %q was not after the creation time %q", accessTime, createTime)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -913,8 +904,7 @@ func Test_CloseExpiredPendingTokens(t *testing.T) {
|
||||
id, err := NewAuthTokenId(ctx)
|
||||
require.NoError(t, err)
|
||||
at.PublicId = id
|
||||
exp, err := ptypes.TimestampProto(time.Now().Add(expIn).Truncate(time.Second))
|
||||
require.NoError(t, err)
|
||||
exp := timestamppb.New(time.Now().Add(expIn).Truncate(time.Second))
|
||||
at.ExpirationTime = ×tamp.Timestamp{Timestamp: exp}
|
||||
at.Status = string(status)
|
||||
at.AuthAccountId = accts[i].PublicId
|
||||
|
||||
Reference in new issue
Block a user