-
Notifications
You must be signed in to change notification settings - Fork 12
/
tls.go
77 lines (65 loc) · 2.03 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
package tls
import (
"crypto/tls"
"code.cloudfoundry.org/tlsconfig"
)
type TLS struct {
CAPath string `env:"CA_PATH, required, report"`
CertPath string `env:"CERT_PATH, required, report"`
KeyPath string `env:"KEY_PATH, required, report"`
}
func NewMutualTLSClientConfig(caPath, certPath, keyPath, commonName string) (*tls.Config, error) {
return tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
tlsconfig.WithIdentityFromFile(certPath, keyPath),
).Client(
tlsconfig.WithAuthorityFromFile(caPath),
tlsconfig.WithServerName(commonName),
)
}
func NewMutualTLSServerConfig(caPath, certPath, keyPath string) (*tls.Config, error) {
return tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
tlsconfig.WithIdentityFromFile(certPath, keyPath),
).Server(
tlsconfig.WithClientAuthenticationFromFile(caPath),
)
}
func NewTLSClientConfig(caPath, commonName string) (*tls.Config, error) {
return tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
).Client(
tlsconfig.WithAuthorityFromFile(caPath),
tlsconfig.WithServerName(commonName),
)
}
func NewTLSServerConfig(certPath, keyPath string) (*tls.Config, error) {
return tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
tlsconfig.WithIdentityFromFile(certPath, keyPath),
).Server()
}
func NewGenericTLSConfig() (*tls.Config, error) {
return tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
).Server()
}
func NewUAATLSConfig(caPath string, skipCertVerify bool) (*tls.Config, error) {
tlsConfig, err := tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
).Client(
tlsconfig.WithAuthorityFromFile(caPath),
)
tlsConfig.InsecureSkipVerify = skipCertVerify
return tlsConfig, err
}
func NewCAPITLSConfig(caPath string, skipCertVerify bool, commonName string) (*tls.Config, error) {
tlsConfig, err := tlsconfig.Build(
tlsconfig.WithInternalServiceDefaults(),
).Client(
tlsconfig.WithAuthorityFromFile(caPath),
tlsconfig.WithServerName(commonName),
)
tlsConfig.InsecureSkipVerify = skipCertVerify
return tlsConfig, err
}