File: option.go

package info (click to toggle)
golang-github-spiffe-go-spiffe 2.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,116 kB
  • sloc: makefile: 157
file content (174 lines) | stat: -rw-r--r-- 4,993 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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"
)

// ClientOption is an option used when creating a new Client.
type ClientOption interface {
	configureClient(*clientConfig)
}

// WithAddr provides an address for the Workload API. The value of the
// SPIFFE_ENDPOINT_SOCKET environment variable will be used if the option
// is unused.
func WithAddr(addr string) ClientOption {
	return clientOption(func(c *clientConfig) {
		c.address = addr
	})
}

// WithDialOptions provides extra GRPC dialing options when dialing the
// Workload API.
func WithDialOptions(options ...grpc.DialOption) ClientOption {
	return clientOption(func(c *clientConfig) {
		c.dialOptions = append(c.dialOptions, options...)
	})
}

// WithLogger provides a logger to the Client.
func WithLogger(logger logger.Logger) ClientOption {
	return clientOption(func(c *clientConfig) {
		c.log = logger
	})
}

// WithBackoff provides a custom backoff strategy that replaces the
// default backoff strategy (linear backoff).
func WithBackoffStrategy(backoffStrategy BackoffStrategy) ClientOption {
	return clientOption(func(c *clientConfig) {
		c.backoffStrategy = backoffStrategy
	})
}

// SourceOption are options that are shared among all option types.
type SourceOption interface {
	configureX509Source(*x509SourceConfig)
	configureJWTSource(*jwtSourceConfig)
	configureBundleSource(*bundleSourceConfig)
}

// WithClient provides a Client for the source to use. If unset, a new Client
// will be created.
func WithClient(client *Client) SourceOption {
	return withClient{client: client}
}

// WithClientOptions controls the options used to create a new Client for the
// source. This option will be ignored if WithClient is used.
func WithClientOptions(options ...ClientOption) SourceOption {
	return withClientOptions{options: options}
}

// X509SourceOption is an option for the X509Source. A SourceOption is also an
// X509SourceOption.
type X509SourceOption interface {
	configureX509Source(*x509SourceConfig)
}

// 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 WithDefaultJWTSVIDPicker(picker func([]*jwtsvid.SVID) *jwtsvid.SVID) JWTSourceOption {
	return withDefaultJWTSVIDPicker{picker: picker}
}

// JWTSourceOption is an option for the JWTSource. A SourceOption is also a
// JWTSourceOption.
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 {
	configureBundleSource(*bundleSourceConfig)
}

type clientConfig struct {
	address         string
	namedPipeName   string
	dialOptions     []grpc.DialOption
	log             logger.Logger
	backoffStrategy BackoffStrategy
}

type clientOption func(*clientConfig)

func (fn clientOption) configureClient(config *clientConfig) {
	fn(config)
}

type x509SourceConfig struct {
	watcher watcherConfig
	picker  func([]*x509svid.SVID) *x509svid.SVID
}

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

type bundleSourceConfig struct {
	watcher watcherConfig
}

type withClient struct {
	client *Client
}

func (o withClient) configureX509Source(config *x509SourceConfig) {
	config.watcher.client = o.client
}

func (o withClient) configureJWTSource(config *jwtSourceConfig) {
	config.watcher.client = o.client
}

func (o withClient) configureBundleSource(config *bundleSourceConfig) {
	config.watcher.client = o.client
}

type withClientOptions struct {
	options []ClientOption
}

func (o withClientOptions) configureX509Source(config *x509SourceConfig) {
	config.watcher.clientOptions = o.options
}

func (o withClientOptions) configureJWTSource(config *jwtSourceConfig) {
	config.watcher.clientOptions = o.options
}

func (o withClientOptions) configureBundleSource(config *bundleSourceConfig) {
	config.watcher.clientOptions = o.options
}

type withDefaultX509SVIDPicker struct {
	picker func([]*x509svid.SVID) *x509svid.SVID
}

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
}