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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
package spiffetls
import (
"crypto/tls"
"crypto/x509"
"github.com/spiffe/go-spiffe/v2/bundle/x509bundle"
"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
"github.com/spiffe/go-spiffe/v2/svid/x509svid"
"github.com/spiffe/go-spiffe/v2/workloadapi"
)
type clientMode int
const (
tlsClientMode clientMode = iota
mtlsClientMode
mtlsWebClientMode
)
type serverMode int
const (
tlsServerMode serverMode = iota
mtlsServerMode
mtlsWebServerMode
)
// DialMode is a SPIFFE TLS dialing mode.
type DialMode interface {
get() *dialMode
}
type dialMode struct {
mode clientMode
// sourceUnneeded is true when a X509Source is not required since
// raw sources have already been provided, i.e. when the mode comes from
// a *WithRawConfig method.
sourceUnneeded bool
authorizer tlsconfig.Authorizer
source *workloadapi.X509Source
options []workloadapi.X509SourceOption
bundle x509bundle.Source
svid x509svid.Source
roots *x509.CertPool
}
type listenMode struct {
mode serverMode
// sourceUnneeded is true when a X509Source is not required since
// raw sources have already been provided, i.e. when the mode comes from
// a *WithRawConfig method.
sourceUnneeded bool
authorizer tlsconfig.Authorizer
source *workloadapi.X509Source
options []workloadapi.X509SourceOption
bundle x509bundle.Source
svid x509svid.Source
cert *tls.Certificate
}
func (l *listenMode) get() *listenMode {
return l
}
func (d *dialMode) get() *dialMode {
return d
}
// TLSClient configures the dialing for TLS. The server X509-SVID is
// authenticated using X.509 bundles obtained via the Workload API. The
// authorizer is used to authorize the server X509-SVID.
func TLSClient(authorizer tlsconfig.Authorizer) DialMode {
return &dialMode{
mode: tlsClientMode,
authorizer: authorizer,
}
}
// TLSClientWithSource configures the dialing for TLS. The server X509-SVID is
// authenticated using X.509 bundles obtained via the provided Workload API
// X.509 source. The source must remain valid for the lifetime of the
// connection. The authorizer is used to authorize the server X509-SVID.
func TLSClientWithSource(authorizer tlsconfig.Authorizer, source *workloadapi.X509Source) DialMode {
return &dialMode{
mode: tlsClientMode,
authorizer: authorizer,
source: source,
}
}
// TLSClientWithSourceOptions configures the dialing for TLS. The server
// X509-SVID is authenticated using X.509 bundles obtained via a new Workload
// API X.509 source created with the provided source options. The authorizer is
// used to authorize the server X509-SVID.
func TLSClientWithSourceOptions(authorizer tlsconfig.Authorizer, options ...workloadapi.X509SourceOption) DialMode {
return &dialMode{
mode: tlsClientMode,
authorizer: authorizer,
options: options,
}
}
// TLSClientWithRawConfig configures the dialing for TLS. The server X509-SVID is
// authenticated using X.509 bundles obtained via the provided X.509 bundle
// source. The source must remain valid for the lifetime of the connection. The
// authorizer is used to authorize the server X509-SVID.
func TLSClientWithRawConfig(authorizer tlsconfig.Authorizer, bundle x509bundle.Source) DialMode {
return &dialMode{
mode: tlsClientMode,
sourceUnneeded: true,
authorizer: authorizer,
bundle: bundle,
}
}
// MTLSClient configures the dialing for mutually authenticated TLS (mTLS). The
// client X509-SVID and the X.509 bundles used to authenticate the server
// X509-SVID are obtained via the Workload API. The authorizer is used to
// authorize the server X509-SVID.
func MTLSClient(authorizer tlsconfig.Authorizer) DialMode {
return &dialMode{
mode: mtlsClientMode,
authorizer: authorizer,
}
}
// MTLSClientWithSource configures the dialing for mutually authenticated TLS
// (mTLS). The client X509-SVID and the X.509 bundles used to authenticate the
// server X509-SVID are obtained via the provided Workload API X.509 source.
// The source must remain valid for the lifetime of the connection. The
// authorizer is used to authorize the server X509-SVID.
func MTLSClientWithSource(authorizer tlsconfig.Authorizer, source *workloadapi.X509Source) DialMode {
return &dialMode{
mode: mtlsClientMode,
authorizer: authorizer,
source: source,
}
}
// MTLSClientWithSourceOptions configures the dialing for mutually
// authenticated TLS (mTLS). The client X509-SVID and the X.509 bundles used to
// authenticate the server X509-SVID are obtained via a new Workload API X.509
// source created with the provided source options. The authorizer is used to
// authorize the server X509-SVID.
func MTLSClientWithSourceOptions(authorizer tlsconfig.Authorizer, options ...workloadapi.X509SourceOption) DialMode {
return &dialMode{
mode: mtlsClientMode,
authorizer: authorizer,
options: options,
}
}
// MTLSClientWithRawConfig configures the dialing for mutually authenticated TLS
// (mTLS). The client X509-SVID and the X.509 bundles used to authenticate the
// server X509-SVID are obtained via the provided X509-SVID and X.509 bundle
// sources. The sources must remain valid for the lifetime of the connection.
// The authorizer is used to authorize the server X509-SVID.
func MTLSClientWithRawConfig(authorizer tlsconfig.Authorizer, svid x509svid.Source, bundle x509bundle.Source) DialMode {
return &dialMode{
mode: mtlsClientMode,
sourceUnneeded: true,
authorizer: authorizer,
svid: svid,
bundle: bundle,
}
}
// MTLSWebClient configures the dialing for mutually authenticated TLS (mTLS).
// The client X509-SVID is obtained via the Workload API. The roots (or the
// system roots if nil) are used to authenticate the server certificate.
func MTLSWebClient(roots *x509.CertPool) DialMode {
return &dialMode{
mode: mtlsWebClientMode,
roots: roots,
}
}
// MTLSWebClientWithSource configures the dialing for mutually authenticated
// TLS (mTLS). The client X509-SVID is obtained via the provided Workload API
// X.509 source. The source must remain valid for the lifetime of the
// connection. The roots (or the system roots if nil) are used to authenticate
// the server certificate.
func MTLSWebClientWithSource(roots *x509.CertPool, source *workloadapi.X509Source) DialMode {
return &dialMode{
mode: mtlsWebClientMode,
source: source,
roots: roots,
}
}
// MTLSWebClientWithSourceOptions configures the dialing for mutually
// authenticated TLS (mTLS). The client X509-SVID is obtained via a new
// Workload API X.509 source created with the provided source options. The
// roots (or the system roots if nil) are used to authenticate the server
// certificate.
func MTLSWebClientWithSourceOptions(roots *x509.CertPool, options ...workloadapi.X509SourceOption) DialMode {
return &dialMode{
mode: mtlsWebClientMode,
options: options,
roots: roots,
}
}
// MTLSWebClientWithRawConfig configures the dialing for mutually authenticated
// TLS (mTLS). The client X509-SVID is obtained via the provided X509-SVID
// source. The source must remain valid for the lifetime of the connection. The
// roots (or the system roots if nil) are used to authenticate the server
// certificate.
func MTLSWebClientWithRawConfig(roots *x509.CertPool, svid x509svid.Source) DialMode {
return &dialMode{
mode: mtlsWebClientMode,
sourceUnneeded: true,
svid: svid,
roots: roots,
}
}
// ListenMode is a SPIFFE TLS listening mode.
type ListenMode interface {
get() *listenMode
}
// TLSServer configures the listener for TLS. The listener presents an
// X509-SVID obtained via the Workload API.
func TLSServer() ListenMode {
return &listenMode{
mode: tlsServerMode,
}
}
// TLSServerWithSource configures the listener for TLS. The listener presents
// an X509-SVID obtained via the provided Workload API X.509 source. The source
// must remain valid for the lifetime of the listener.
func TLSServerWithSource(source *workloadapi.X509Source) ListenMode {
return &listenMode{
mode: tlsServerMode,
source: source,
}
}
// TLSServerWithSourceOptions configures the listener for TLS. The listener
// presents an X509-SVID obtained via a new Workload API X.509 source created
// with the provided source options.
func TLSServerWithSourceOptions(options ...workloadapi.X509SourceOption) ListenMode {
return &listenMode{
mode: tlsServerMode,
options: options,
}
}
// TLSServerWithRawConfig configures the listener for TLS. The listener presents
// an X509-SVID obtained via the provided X509-SVID source. The source must
// remain valid for the lifetime of the listener.
func TLSServerWithRawConfig(svid x509svid.Source) ListenMode {
return &listenMode{
mode: tlsServerMode,
sourceUnneeded: true,
svid: svid,
}
}
// MTLSServer configures the listener for mutually authenticated TLS (mTLS).
// The listener presents an X509-SVID and authenticates client X509-SVIDs using
// X.509 bundles, both obtained via the Workload API. The authorizer is used to
// authorize client X509-SVIDs.
func MTLSServer(authorizer tlsconfig.Authorizer) ListenMode {
return &listenMode{
mode: mtlsServerMode,
authorizer: authorizer,
}
}
// MTLSServerWithSource configures the listener for mutually authenticated TLS
// (mTLS). The listener presents an X509-SVID and authenticates client
// X509-SVIDs using X.509 bundles, both obtained via the provided Workload API
// X.509 source. The source must remain valid for the lifetime of the listener.
// The authorizer is used to authorize client X509-SVIDs.
func MTLSServerWithSource(authorizer tlsconfig.Authorizer, source *workloadapi.X509Source) ListenMode {
return &listenMode{
mode: mtlsServerMode,
authorizer: authorizer,
source: source,
}
}
// MTLSServerWithSourceOptions configures the listener for mutually
// authenticated TLS (mTLS). The listener presents an X509-SVID and
// authenticates client X509-SVIDs using X.509 bundles, both obtained via a new
// Workload API X.509 source created with the provided source options. The
// authorizer is used to authorize client X509-SVIDs.
func MTLSServerWithSourceOptions(authorizer tlsconfig.Authorizer, options ...workloadapi.X509SourceOption) ListenMode {
return &listenMode{
mode: mtlsServerMode,
authorizer: authorizer,
options: options,
}
}
// MTLSServerWithRawConfig configures the listener for mutually authenticated TLS
// (mTLS). The listener presents an X509-SVID and authenticates client
// X509-SVIDs using X.509 bundles, both obtained via the provided X509-SVID and
// X.509 bundle sources. The sources must remain valid for the lifetime of the
// listener. The authorizer is used to authorize client X509-SVIDs.
func MTLSServerWithRawConfig(authorizer tlsconfig.Authorizer, svid x509svid.Source, bundle x509bundle.Source) ListenMode {
return &listenMode{
mode: mtlsServerMode,
sourceUnneeded: true,
authorizer: authorizer,
svid: svid,
bundle: bundle,
}
}
// MTLSWebServer configures the listener for mutually authenticated TLS (mTLS).
// The listener presents an X.509 certificate and authenticates client
// X509-SVIDs using X.509 bundles obtained via the Workload API. The authorizer
// is used to authorize client X509-SVIDs.
func MTLSWebServer(authorizer tlsconfig.Authorizer, cert *tls.Certificate) ListenMode {
return &listenMode{
mode: mtlsWebServerMode,
cert: cert,
authorizer: authorizer,
}
}
// MTLSWebServerWithSource configures the listener for mutually authenticated
// TLS (mTLS). The listener presents an X.509 certificate and authenticates
// client X509-SVIDs using X.509 bundles obtained via the provided Workload API
// X.509 source. The source must remain valid for the lifetime of the listener.
// The authorizer is used to authorize client X509-SVIDs.
func MTLSWebServerWithSource(authorizer tlsconfig.Authorizer, cert *tls.Certificate, source *workloadapi.X509Source) ListenMode {
return &listenMode{
mode: mtlsWebServerMode,
cert: cert,
source: source,
authorizer: authorizer,
}
}
// MTLSWebServerWithSourceOptions configures the listener for mutually
// authenticated TLS (mTLS). The listener presents an X.509 certificate and
// authenticates client X509-SVIDs using X.509 bundles, both obtained via a new
// Workload API X.509 source created with the provided source options. The
// authorizer is used to authorize client X509-SVIDs.
func MTLSWebServerWithSourceOptions(authorizer tlsconfig.Authorizer, cert *tls.Certificate, options ...workloadapi.X509SourceOption) ListenMode {
return &listenMode{
mode: mtlsWebServerMode,
cert: cert,
options: options,
authorizer: authorizer,
}
}
// MTLSWebServerWithRawConfig configures the listener for mutually authenticated
// TLS (mTLS). The listener presents an X.509 certificate and authenticates
// client X509-SVIDs using X.509 bundles, both obtained via the provided X.509
// bundle source. The source must remain valid for the lifetime of the
// listener. The authorizer is used to authorize client X509-SVIDs.
func MTLSWebServerWithRawConfig(authorizer tlsconfig.Authorizer, cert *tls.Certificate, bundle x509bundle.Source) ListenMode {
return &listenMode{
mode: mtlsWebServerMode,
sourceUnneeded: true,
authorizer: authorizer,
cert: cert,
bundle: bundle,
}
}
|