-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
120 lines (98 loc) · 2.78 KB
/
client.go
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
package cert
import (
"crypto/tls"
"fmt"
"os"
)
type ClientTLSConfigOptions struct {
CaPem []byte
CaFilePath string
ClientCertPem []byte
ClientCertFile string
ClientKeyPem []byte
ClientKeyFile string
ClientKeyPassword string
ServerName string // override default server name verification
Insecure bool
}
// TLSConfigOptions
//
// Deprecated: use ClientTLSConfigOptions instead
type TLSConfigOptions = ClientTLSConfigOptions
func (o *ClientTLSConfigOptions) WithCAPem(pem []byte) *ClientTLSConfigOptions {
o.CaPem = pem
return o
}
func (o *ClientTLSConfigOptions) WithCA(caFilePath string) *ClientTLSConfigOptions {
o.CaFilePath = caFilePath
return nil
}
func (o *ClientTLSConfigOptions) WithClientCertificatePem(clientCertPem, clientKeyPem []byte) *ClientTLSConfigOptions {
o.ClientCertPem = clientCertPem
o.ClientKeyPem = clientKeyPem
return o
}
func (o *ClientTLSConfigOptions) WithClientCertificate(clientCertFile string, clientKeyFile string) *ClientTLSConfigOptions {
o.ClientCertFile = clientCertFile
o.ClientKeyFile = clientKeyFile
return o
}
func (o *ClientTLSConfigOptions) WithClientKeyPassword(password string) *ClientTLSConfigOptions {
o.ClientKeyPassword = password
return o
}
func (o *ClientTLSConfigOptions) WithServerName(serverName string) *ClientTLSConfigOptions {
o.ServerName = serverName
return o
}
func (o *ClientTLSConfigOptions) WithInsecure(insecure bool) *ClientTLSConfigOptions {
o.Insecure = insecure
return o
}
func (o *ClientTLSConfigOptions) TLSConfig() (*tls.Config, error) {
config := &tls.Config{}
if len(o.CaPem) != 0 || o.CaFilePath != "" {
caPem := o.CaPem
var err error
if len(o.CaPem) == 0 && o.CaFilePath != "" {
caPem, err = os.ReadFile(o.CaFilePath)
if err != nil {
return nil, err
}
}
pool, ok := PoolFromPem(caPem)
if !ok {
return nil, fmt.Errorf("cannot parse pem")
}
config.RootCAs = pool
}
if len(o.ClientCertPem) != 0 || len(o.ClientKeyPem) != 0 || o.ClientCertFile != "" || o.ClientKeyFile != "" {
clientCertPem := o.ClientCertPem
clientKeyPem := o.ClientKeyPem
var err error
if len(o.ClientCertPem) == 0 && o.ClientCertFile != "" {
clientCertPem, err = os.ReadFile(o.ClientCertFile)
if err != nil {
return nil, err
}
}
if len(o.ClientKeyPem) == 0 && o.ClientKeyFile != "" {
clientKeyPem, err = os.ReadFile(o.ClientKeyFile)
if err != nil {
return nil, err
}
}
cert, err := ParseCertificate(clientCertPem, clientKeyPem, o.ClientKeyPassword)
if err != nil {
return nil, err
}
config.GetClientCertificate = func(info *tls.CertificateRequestInfo) (*tls.Certificate, error) {
return &cert, nil
}
}
config.ServerName = o.ServerName
if o.Insecure {
config.InsecureSkipVerify = true
}
return config, nil
}