Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a WithDefaultJWTSVIDPicker source option #301

Merged
merged 2 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions v2/workloadapi/jwtsource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var jwtsourceErr = errs.Class("jwtsource")
// Workload API.
type JWTSource struct {
watcher *watcher
picker func([]*jwtsvid.SVID) *jwtsvid.SVID

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

s := &JWTSource{}
s := &JWTSource{
picker: config.picker,
}

s.watcher, err = newWatcher(ctx, config.watcher, nil, s.setJWTBundles)
if err != nil {
Expand Down Expand Up @@ -61,7 +64,22 @@ func (s *JWTSource) FetchJWTSVID(ctx context.Context, params jwtsvid.Params) (*j
if err := s.checkClosed(); err != nil {
return nil, err
}
return s.watcher.client.FetchJWTSVID(ctx, params)

var (
svid *jwtsvid.SVID
err error
)
if s.picker == nil {
svid, err = s.watcher.client.FetchJWTSVID(ctx, params)
} else {
svids, err := s.watcher.client.FetchJWTSVIDs(ctx, params)
if err != nil {
return svid, err
}
svid = s.picker(svids)
}

return svid, err
}

// FetchJWTSVIDs fetches all JWT-SVIDs from the source with the given parameters.
Expand Down
28 changes: 23 additions & 5 deletions v2/workloadapi/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package workloadapi

import (
"github.com/spiffe/go-spiffe/v2/logger"
"github.com/spiffe/go-spiffe/v2/svid/jwtsvid"
"github.com/spiffe/go-spiffe/v2/svid/x509svid"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -68,12 +69,12 @@ type X509SourceOption interface {
configureX509Source(*x509SourceConfig)
}

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

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

// WithDefaultX509SVIDPicker provides a function that is used to determine the
// default X509-SVID when more than one is provided by the Workload API. By
// default, the first X509-SVID in the list returned by the Workload API is
// used.
func WithDefaultX509SVIDPicker(picker func([]*x509svid.SVID) *x509svid.SVID) X509SourceOption {
return withDefaultX509SVIDPicker{picker: picker}
}

// BundleSourceOption is an option for the BundleSource. A SourceOption is also
// a BundleSourceOption.
type BundleSourceOption interface {
Expand Down Expand Up @@ -109,6 +118,7 @@ type x509SourceConfig struct {

type jwtSourceConfig struct {
watcher watcherConfig
picker func([]*jwtsvid.SVID) *jwtsvid.SVID
}

type bundleSourceConfig struct {
Expand Down Expand Up @@ -154,3 +164,11 @@ type withDefaultX509SVIDPicker struct {
func (o withDefaultX509SVIDPicker) configureX509Source(config *x509SourceConfig) {
config.picker = o.picker
}

type withDefaultJWTSVIDPicker struct {
picker func([]*jwtsvid.SVID) *jwtsvid.SVID
}

func (o withDefaultJWTSVIDPicker) configureJWTSource(config *jwtSourceConfig) {
config.picker = o.picker
}