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 (84 lines) | stat: -rw-r--r-- 2,120 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
package spiffetls

import (
	"crypto/tls"
	"net"

	"github.com/spiffe/go-spiffe/v2/spiffetls/tlsconfig"
	"github.com/zeebo/errs"
)

var spiffetlsErr = errs.Class("spiffetls")

// DialOption is an option for dialing. Option's are also DialOption's.
type DialOption interface {
	apply(*dialConfig)
}

type dialOption func(*dialConfig)

func (fn dialOption) apply(c *dialConfig) {
	fn(c)
}

type dialConfig struct {
	baseTLSConf *tls.Config
	dialer      *net.Dialer
	tlsOptions  []tlsconfig.Option
}

type listenOption func(*listenConfig)

type listenConfig struct {
	baseTLSConf *tls.Config
	tlsOptions  []tlsconfig.Option
}

func (fn listenOption) apply(c *listenConfig) {
	fn(c)
}

// WithDialTLSConfigBase provides a base TLS configuration to use. Fields
// related to certificates and verification will be overwritten by this package
// as necessary to facilitate SPIFFE authentication.
func WithDialTLSConfigBase(base *tls.Config) DialOption {
	return dialOption(func(c *dialConfig) {
		c.baseTLSConf = base
	})
}

// WithDialTLSOptions provides options to use for the TLS config.
func WithDialTLSOptions(opts ...tlsconfig.Option) DialOption {
	return dialOption(func(c *dialConfig) {
		c.tlsOptions = opts
	})
}

// WithDialer provides a net dialer to use. If unset, the standard net dialer
// will be used.
func WithDialer(dialer *net.Dialer) DialOption {
	return dialOption(func(c *dialConfig) {
		c.dialer = dialer
	})
}

// ListenOption is an option for listening. Option's are also ListenOption's.
type ListenOption interface {
	apply(*listenConfig)
}

// WithListenTLSConfigBase provides a base TLS configuration to use. Fields
// related to certificates and verification will be overwritten by this package
// as necessary to facilitate SPIFFE authentication.
func WithListenTLSConfigBase(base *tls.Config) ListenOption {
	return listenOption(func(c *listenConfig) {
		c.baseTLSConf = base
	})
}

// WithListenTLSOptions provides options to use when doing Server mTLS.
func WithListenTLSOptions(opts ...tlsconfig.Option) ListenOption {
	return listenOption(func(c *listenConfig) {
		c.tlsOptions = opts
	})
}