Skip to content

Commit 208ed46

Browse files
committed
chore: address comments from PR
1 parent 74b0fb2 commit 208ed46

File tree

7 files changed

+43
-23
lines changed

7 files changed

+43
-23
lines changed

authorization/hasher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func NewAuthorizationHasher(opts ...AuthorizationHasherOption) (*AuthorizationHa
6969
// Create the hasher used for hashing new tokens before storage.
7070
hasher, err := influxdb2_algo.New(influxdb2_algo.WithVariant(options.hasherVariant))
7171
if err != nil {
72-
return nil, fmt.Errorf("creating hasher for AuthorizationHasher: %w", err)
72+
return nil, fmt.Errorf("creating hasher %s for AuthorizationHasher: %w", options.hasherVariant.Prefix(), err)
7373
}
7474

7575
// Create decoder and register all requested decoder variants.

authorization/http_server_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func TestService_handlePostAuthorization(t *testing.T) {
191191
require.Equal(t, tt.wants.contentType, contentType)
192192
}
193193
diff, err := jsonDiff(string(body), tt.wants.body)
194-
require.NoError(t, err)
194+
require.NoError(t, err, "jsonDiff failed")
195195
require.Empty(t, diff, "authorization endpoint returned unexpected result")
196196
})
197197
}
@@ -355,7 +355,7 @@ func TestService_handleGetAuthorization(t *testing.T) {
355355
require.Equal(t, tt.wants.contentType, contentType)
356356
}
357357
diff, err := jsonDiff(string(body), tt.wants.body)
358-
require.NoError(t, err)
358+
require.NoError(t, err, "jsonDiff failed")
359359
require.Empty(t, diff, "authorization endpoint returned unexpected result")
360360
})
361361
}
@@ -740,7 +740,7 @@ func TestService_handleGetAuthorizations(t *testing.T) {
740740
require.Equal(t, tt.wants.contentType, contentType)
741741
}
742742
diff, err := jsonDiff(string(body), tt.wants.body)
743-
require.NoError(t, err)
743+
require.NoError(t, err, "jsonDiff failed")
744744
require.Empty(t, diff, "authorization endpoint returned unexpected results")
745745
})
746746
}
@@ -840,7 +840,7 @@ func TestService_handleDeleteAuthorization(t *testing.T) {
840840

841841
if tt.wants.body != "" {
842842
diff, err := jsonDiff(string(body), tt.wants.body)
843-
require.NoError(t, err)
843+
require.NoError(t, err, "jsonDiff failed")
844844
require.Empty(t, diff, "authorization endpoint returned unexpected results")
845845
}
846846
})

authorization/service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func initAuthService(s kv.Store, f influxdbtesting.AuthorizationFields, useHashe
6464

6565
func TestBoltAuthService(t *testing.T) {
6666
t.Parallel()
67-
for _, useHashedTokens := range []bool{true} {
67+
for _, useHashedTokens := range []bool{false, true} {
6868
init := func(f influxdbtesting.AuthorizationFields, t *testing.T) (influxdb.AuthorizationService, string, func()) {
6969
return initBoltAuthService(f, useHashedTokens, t)
7070
}

authorization/storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func (s *Store) hashedTokenMigration(ctx context.Context) error {
298298
// Now update them. This really seems too simple, but s.UpdateAuthorization() is magical.
299299
for _, a := range batch {
300300
if _, err := s.UpdateAuthorization(ctx, tx, a.ID, a); err != nil {
301-
return err
301+
return fmt.Errorf("failed to update authorization for %d (%s): %w", a.ID, a.Description, err)
302302
}
303303
}
304304
return nil

authorization/storage_authorization.go

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
)
1818

1919
var (
20+
ErrNilAuthorization = goerrors.New("authorization cannot be nil")
2021
ErrHashedTokenMismatch = goerrors.New("HashedToken does not match Token")
2122
ErrIncorrectToken = goerrors.New("token is incorrect for authorization")
2223
ErrNoTokenAvailable = goerrors.New("no token available for authorization")
@@ -108,7 +109,7 @@ func (s *Store) transformToken(a *influxdb.Authorization) error {
108109
// Note that even if a.HashedToken is set, we will regenerate it here. This ensures
109110
// that a.HashedToken will be stored using the currently configured hashing algorithm.
110111
if hashedToken, err := s.hasher.Hash(a.Token); err != nil {
111-
return fmt.Errorf("error hashing token: %w", err)
112+
return fmt.Errorf("error hashing token for token %d (%s): %w", a.ID, a.Description, err)
112113
} else {
113114
a.HashedToken = hashedToken
114115
}
@@ -122,11 +123,17 @@ func (s *Store) transformToken(a *influxdb.Authorization) error {
122123
}
123124

124125
// CreateAuthorization takes an Authorization object and saves it in storage using its token
125-
// using its token property as an index
126+
// using its token property as an index. The contents of a should be considered invalid if an
127+
// error occurs.
126128
func (s *Store) CreateAuthorization(ctx context.Context, tx kv.Tx, a *influxdb.Authorization) (retErr error) {
127129
defer func() {
128130
retErr = errors.ErrInternalServiceError(retErr, errors.WithErrorOp(influxdb.OpCreateAuthorization))
129131
}()
132+
133+
if a == nil {
134+
return ErrNilAuthorization
135+
}
136+
130137
// if the provided ID is invalid, or already maps to an existing Auth, then generate a new one
131138
if !a.ID.Valid() {
132139
id, err := s.generateSafeID(ctx, tx, authBucket)
@@ -193,7 +200,7 @@ func (s *Store) validateToken(auth *influxdb.Authorization, token string) (bool,
193200
if auth.HashedToken != "" {
194201
match, err := s.hasher.Match(auth.HashedToken, token)
195202
if err != nil {
196-
return false, fmt.Errorf("error matching hashed token for validation: %w", err)
203+
return false, fmt.Errorf("error matching hashed token %d (%s) for validation: %w", auth.ID, auth.Description, err)
197204
}
198205
return match, nil
199206
}
@@ -437,6 +444,10 @@ func (s *Store) UpdateAuthorization(ctx context.Context, tx kv.Tx, id platform.I
437444
retErr = errors.ErrInternalServiceError(retErr, errors.WithErrorOp(influxdb.OpUpdateAuthorization))
438445
}()
439446

447+
if a == nil {
448+
return nil, ErrNilAuthorization
449+
}
450+
440451
initialToken := a.Token
441452
initialHashedToken := a.HashedToken
442453

@@ -610,16 +621,28 @@ func (s *Store) authorizationsPredicateFn(f influxdb.AuthorizationFilter) kv.Cur
610621
// but we'll still look at the unhashed Token if it is available.
611622
}
612623
return func(_, value []byte) bool {
613-
// it is assumed that token never has escaped string data
624+
// Check if "token" matches. It is assumed that token never has escaped string data.
614625
if got, _, _, err := jsonparser.Get(value, "token"); err == nil {
615-
return string(got) == token
626+
if len(got) > 0 {
627+
return string(got) == token
628+
}
629+
} else {
630+
return true // predicate must return true on errors
616631
}
632+
633+
// Check if "hashedToken" matches, if applicable.
617634
if len(allHashes) > 0 {
618635
if got, _, _, err := jsonparser.Get(value, "hashedToken"); err == nil {
619-
return slices.Contains(allHashes, string(got))
636+
if len(got) > 0 {
637+
return slices.Contains(allHashes, string(got))
638+
}
639+
} else {
640+
return true // predicate must return true on errors
620641
}
621642
}
622-
return true
643+
644+
// No match on "token" or "hashedToken", do not include this record.
645+
return false
623646
}
624647
}
625648

authorizer/task_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestOnboardingValidation(t *testing.T) {
3838
Bucket: "holder",
3939
RetentionPeriodSeconds: 1,
4040
})
41-
require.NoError(t, err)
41+
require.NoError(t, err, "OnboardInitialUser failed")
4242

4343
ctx := pctx.SetAuthorizer(context.Background(), r.Auth)
4444

@@ -142,18 +142,16 @@ func runTestValidations(useHashedTokens bool, t *testing.T) {
142142
Bucket: "holder",
143143
RetentionPeriodSeconds: 1,
144144
})
145-
require.NoError(t, err)
145+
require.NoError(t, err, "OnboardInitialUser failed")
146146

147-
err = svc.CreateOrganization(context.Background(), otherOrg)
148-
require.NoError(t, err)
147+
require.NoError(t, svc.CreateOrganization(context.Background(), otherOrg))
149148

150149
otherBucket := &influxdb.Bucket{
151150
Name: "other_bucket",
152151
OrgID: otherOrg.ID,
153152
}
154153

155-
err = svc.CreateBucket(context.Background(), otherBucket)
156-
require.NoError(t, err)
154+
require.NoError(t, svc.CreateBucket(context.Background(), otherBucket))
157155

158156
var (
159157
orgID = r.Org.ID
@@ -603,8 +601,7 @@ func newStore(t *testing.T) kv.Store {
603601

604602
store := inmem.NewKVStore()
605603

606-
err := all.Up(context.Background(), zaptest.NewLogger(t), store)
607-
require.NoError(t, err)
604+
require.NoError(t, all.Up(context.Background(), zaptest.NewLogger(t), store))
608605

609606
return store
610607
}

cmd/influxd/launcher/launcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ func (m *Launcher) run(ctx context.Context, opts *InfluxdOpts) (err error) {
295295
hasherVariantName := authorization.DefaultHashVariantName // This value could come from opts in the future.
296296
authStore, err := authorization.NewStore(ctx, m.kvStore, opts.UseHashedTokens, authorization.WithAuthorizationHashVariantName(hasherVariantName), authorization.WithLogger(m.log))
297297
if err != nil {
298-
m.log.Error("Failed creating new authorization store", zap.Error(err))
298+
m.log.Error("Failed creating new authorization store", zap.Error(err), zap.Bool("UseHashedTokens", opts.UseHashedTokens), zap.String("hasherVariant", hasherVariantName))
299299
return err
300300
}
301301
authSvc = authorization.NewService(authStore, ts)

0 commit comments

Comments
 (0)