-
Notifications
You must be signed in to change notification settings - Fork 25
/
tls.go
118 lines (102 loc) · 2.84 KB
/
tls.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
package plumbing
import (
"crypto/tls"
"log"
"net"
"net/http"
"time"
"code.cloudfoundry.org/tlsconfig"
"google.golang.org/grpc/credentials"
)
var cipherMap = map[string]uint16{
"TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256": tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
"TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384": tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
}
// ConfigOption is used when configuring a new tls.Config.
type ConfigOption func(*tls.Config)
// WithCipherSuites is used to override the default cipher suites.
func WithCipherSuites(ciphers []string) ConfigOption {
return func(c *tls.Config) {
var configuredCiphers []uint16
for _, c := range ciphers {
cipher, ok := cipherMap[c]
if !ok {
continue
}
configuredCiphers = append(configuredCiphers, cipher)
}
c.CipherSuites = configuredCiphers
if len(c.CipherSuites) == 0 {
log.Panic("no valid ciphers provided for TLS configuration")
}
}
}
// NewClientCredentials returns gRPC credentials for dialing.
func NewClientCredentials(
certFile string,
keyFile string,
caCertFile string,
serverName string,
) (credentials.TransportCredentials, error) {
tlsConfig, err := tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
tlsconfig.WithIdentityFromFile(certFile, keyFile),
).Client(
tlsconfig.WithAuthorityFromFile(caCertFile),
tlsconfig.WithServerName(serverName),
)
if err != nil {
return nil, err
}
return credentials.NewTLS(tlsConfig), nil
}
// NewServerCredentials returns gRPC credentials for a server.
func NewServerCredentials(
certFile string,
keyFile string,
caCertFile string,
opts ...ConfigOption,
) (credentials.TransportCredentials, error) {
tlsConfig, err := tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
tlsconfig.WithIdentityFromFile(certFile, keyFile),
).Server(
tlsconfig.WithClientAuthenticationFromFile(caCertFile),
)
for _, opt := range opts {
opt(tlsConfig)
}
if err != nil {
return nil, err
}
return credentials.NewTLS(tlsConfig), nil
}
func NewTLSHTTPClient(cert, key, ca, commonName string, disableKeepAlives bool) *http.Client {
tlsConfig, err := tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
tlsconfig.WithIdentityFromFile(cert, key),
).Client(
tlsconfig.WithAuthorityFromFile(ca),
tlsconfig.WithServerName(commonName),
)
if err != nil {
log.Panicf("failed to load API client certificates: %s", err)
}
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: tlsConfig,
DisableKeepAlives: disableKeepAlives,
}
return &http.Client{
Transport: transport,
}
}