Skip to content

Commit a1bebc5

Browse files
fix(sdk): newGranter nil check (#2729)
This pull request makes a targeted fix to the error handling logic in the `newGranter` function within `sdk/tdf.go`. The change ensures that if an error occurs while creating the granter, the function immediately returns the error, preventing further operations on a potentially invalid object. Error handling improvement: * Added an early return in the `newGranter` function to immediately return if `err` is not nil, improving reliability and preventing assignment to `g.keyInfoFetcher` when an error occurs. (`sdk/tdf.go`, [sdk/tdf.goR458-R460](diffhunk://#diff-04aae07cffd4333871c74efd45d47c4e6739b11bf4ebd9a9b5e90376cbf46d08R458-R460) --------- Co-authored-by: David Mihalcik <dmihalcik@virtru.com>
1 parent 11e6201 commit a1bebc5

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

sdk/tdf.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,7 @@ func (tdfConfig *TDFConfig) initKAOTemplate(ctx context.Context, s SDK) error {
374374

375375
// * Get base key before autoconfigure to condition off of.
376376
if tdfConfig.autoconfigure {
377-
var g granter
378-
var err error
379-
g, err = s.newGranter(ctx, tdfConfig, err)
377+
g, err := s.newGranter(ctx, tdfConfig)
380378
if err != nil {
381379
return err
382380
}
@@ -448,15 +446,19 @@ func (tdfConfig *TDFConfig) initKAOTemplate(ctx context.Context, s SDK) error {
448446
return nil
449447
}
450448

451-
func (s SDK) newGranter(ctx context.Context, tdfConfig *TDFConfig, err error) (granter, error) {
449+
func (s SDK) newGranter(ctx context.Context, tdfConfig *TDFConfig) (granter, error) {
452450
var g granter
451+
var err error
453452
if len(tdfConfig.attributeValues) > 0 {
454453
g, err = newGranterFromAttributes(s.kasKeyCache, tdfConfig.attributeValues...)
455454
} else if len(tdfConfig.attributes) > 0 {
456455
g, err = newGranterFromService(ctx, s.kasKeyCache, s.Attributes, tdfConfig.attributes...)
457456
}
457+
if err != nil {
458+
return g, err
459+
}
458460
g.keyInfoFetcher = s
459-
return g, err
461+
return g, nil
460462
}
461463

462464
func (t *TDFObject) Manifest() Manifest {

0 commit comments

Comments
 (0)