Skip to content

Commit 7e5a279

Browse files
authored
feat: add a WithDefaultJWTSVIDPicker source option (#301)
Signed-off-by: Nick Stott <[email protected]>
1 parent 1b87745 commit 7e5a279

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

v2/workloadapi/jwtsource.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var jwtsourceErr = errs.Class("jwtsource")
1616
// Workload API.
1717
type JWTSource struct {
1818
watcher *watcher
19+
picker func([]*jwtsvid.SVID) *jwtsvid.SVID
1920

2021
mtx sync.RWMutex
2122
bundles *jwtbundle.Set
@@ -33,7 +34,9 @@ func NewJWTSource(ctx context.Context, options ...JWTSourceOption) (_ *JWTSource
3334
option.configureJWTSource(config)
3435
}
3536

36-
s := &JWTSource{}
37+
s := &JWTSource{
38+
picker: config.picker,
39+
}
3740

3841
s.watcher, err = newWatcher(ctx, config.watcher, nil, s.setJWTBundles)
3942
if err != nil {
@@ -61,7 +64,22 @@ func (s *JWTSource) FetchJWTSVID(ctx context.Context, params jwtsvid.Params) (*j
6164
if err := s.checkClosed(); err != nil {
6265
return nil, err
6366
}
64-
return s.watcher.client.FetchJWTSVID(ctx, params)
67+
68+
var (
69+
svid *jwtsvid.SVID
70+
err error
71+
)
72+
if s.picker == nil {
73+
svid, err = s.watcher.client.FetchJWTSVID(ctx, params)
74+
} else {
75+
svids, err := s.watcher.client.FetchJWTSVIDs(ctx, params)
76+
if err != nil {
77+
return svid, err
78+
}
79+
svid = s.picker(svids)
80+
}
81+
82+
return svid, err
6583
}
6684

6785
// FetchJWTSVIDs fetches all JWT-SVIDs from the source with the given parameters.

v2/workloadapi/option.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package workloadapi
22

33
import (
44
"github.com/spiffe/go-spiffe/v2/logger"
5+
"github.com/spiffe/go-spiffe/v2/svid/jwtsvid"
56
"github.com/spiffe/go-spiffe/v2/svid/x509svid"
67
"google.golang.org/grpc"
78
)
@@ -68,12 +69,12 @@ type X509SourceOption interface {
6869
configureX509Source(*x509SourceConfig)
6970
}
7071

71-
// WithDefaultX509SVIDPicker provides a function that is used to determine the
72-
// default X509-SVID when more than one is provided by the Workload API. By
73-
// default, the first X509-SVID in the list returned by the Workload API is
72+
// WithDefaultJWTSVIDPicker provides a function that is used to determine the
73+
// default JWT-SVID when more than one is provided by the Workload API. By
74+
// default, the first JWT-SVID in the list returned by the Workload API is
7475
// used.
75-
func WithDefaultX509SVIDPicker(picker func([]*x509svid.SVID) *x509svid.SVID) X509SourceOption {
76-
return withDefaultX509SVIDPicker{picker: picker}
76+
func WithDefaultJWTSVIDPicker(picker func([]*jwtsvid.SVID) *jwtsvid.SVID) JWTSourceOption {
77+
return withDefaultJWTSVIDPicker{picker: picker}
7778
}
7879

7980
// JWTSourceOption is an option for the JWTSource. A SourceOption is also a
@@ -82,6 +83,14 @@ type JWTSourceOption interface {
8283
configureJWTSource(*jwtSourceConfig)
8384
}
8485

86+
// WithDefaultX509SVIDPicker provides a function that is used to determine the
87+
// default X509-SVID when more than one is provided by the Workload API. By
88+
// default, the first X509-SVID in the list returned by the Workload API is
89+
// used.
90+
func WithDefaultX509SVIDPicker(picker func([]*x509svid.SVID) *x509svid.SVID) X509SourceOption {
91+
return withDefaultX509SVIDPicker{picker: picker}
92+
}
93+
8594
// BundleSourceOption is an option for the BundleSource. A SourceOption is also
8695
// a BundleSourceOption.
8796
type BundleSourceOption interface {
@@ -109,6 +118,7 @@ type x509SourceConfig struct {
109118

110119
type jwtSourceConfig struct {
111120
watcher watcherConfig
121+
picker func([]*jwtsvid.SVID) *jwtsvid.SVID
112122
}
113123

114124
type bundleSourceConfig struct {
@@ -154,3 +164,11 @@ type withDefaultX509SVIDPicker struct {
154164
func (o withDefaultX509SVIDPicker) configureX509Source(config *x509SourceConfig) {
155165
config.picker = o.picker
156166
}
167+
168+
type withDefaultJWTSVIDPicker struct {
169+
picker func([]*jwtsvid.SVID) *jwtsvid.SVID
170+
}
171+
172+
func (o withDefaultJWTSVIDPicker) configureJWTSource(config *jwtSourceConfig) {
173+
config.picker = o.picker
174+
}

0 commit comments

Comments
 (0)