Debian Codegraph @

exporter/tls.go

raw symbols SCIP
1 package exporter
2
3 import (
4 "crypto/tls"
5 "crypto/x509"
6 "fmt"
7 "os"
8
9 log "github.com/sirupsen/logrus"
10 )
11
12 // CreateClientTLSConfig verifies configured files and return a prepared tls.Config
13 func (e *Exporter) CreateClientTLSConfig() (*tls.Config, error) {
14 tlsConfig := tls.Config{
15 InsecureSkipVerify: e.options.SkipTLSVerification,
16 }
17
18 if e.options.ClientCertFile != "" && e.options.ClientKeyFile != "" {
19 cert, err := LoadKeyPair(e.options.ClientCertFile, e.options.ClientKeyFile)
20 if err != nil {
21 return nil, err
22 }
23 tlsConfig.Certificates = []tls.Certificate{*cert}
24 }
25
26 if e.options.CaCertFile != "" {
27 certificates, err := LoadCAFile(e.options.CaCertFile)
28 if err != nil {
29 return nil, err
30 }
31 tlsConfig.RootCAs = certificates
32 } else {
33 // Load the system certificate pool
34 rootCAs, err := x509.SystemCertPool()
35 if err != nil {
36 return nil, err
37 }
38
39 tlsConfig.RootCAs = rootCAs
40 }
41
42 return &tlsConfig, nil
43 }
44
45 var tlsVersions = map[string]uint16{
46 "TLS1.3": tls.VersionTLS13,
47 "TLS1.2": tls.VersionTLS12,
48 "TLS1.1": tls.VersionTLS11,
49 "TLS1.0": tls.VersionTLS10,
50 }
51
52 // CreateServerTLSConfig verifies configuration and return a prepared tls.Config
53 func (e *Exporter) CreateServerTLSConfig(certFile, keyFile, caCertFile, minVersionString string) (*tls.Config, error) {
54 // Verify that the initial key pair is accepted
55 _, err := LoadKeyPair(certFile, keyFile)
56 if err != nil {
57 return nil, err
58 }
59
60 // Get minimum acceptable TLS version from the config string
61 minVersion, ok := tlsVersions[minVersionString]
62 if !ok {
63 return nil, fmt.Errorf("configured minimum TLS version unknown: '%s'", minVersionString)
64 }
65
66 tlsConfig := tls.Config{
67 MinVersion: minVersion,
68 GetCertificate: GetServerCertificateFunc(certFile, keyFile),
69 }
70
71 if caCertFile != "" {
72 // Verify that the initial CA file is accepted when configured
73 _, err := LoadCAFile(caCertFile)
74 if err != nil {
75 return nil, err
76 }
77 tlsConfig.GetConfigForClient = GetConfigForClientFunc(certFile, keyFile, caCertFile)
78 }
79
80 return &tlsConfig, nil
81 }
82
83 // GetServerCertificateFunc returns a function for tls.Config.GetCertificate
84 func GetServerCertificateFunc(certFile, keyFile string) func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
85 return func(*tls.ClientHelloInfo) (*tls.Certificate, error) {
86 return LoadKeyPair(certFile, keyFile)
87 }
88 }
89
90 // GetConfigForClientFunc returns a function for tls.Config.GetConfigForClient
91 func GetConfigForClientFunc(certFile, keyFile, caCertFile string) func(*tls.ClientHelloInfo) (*tls.Config, error) {
92 return func(*tls.ClientHelloInfo) (*tls.Config, error) {
93 certificates, err := LoadCAFile(caCertFile)
94 if err != nil {
95 return nil, err
96 }
97
98 tlsConfig := tls.Config{
99 ClientAuth: tls.RequireAndVerifyClientCert,
100 ClientCAs: certificates,
101 GetCertificate: GetServerCertificateFunc(certFile, keyFile),
102 }
103 return &tlsConfig, nil
104 }
105 }
106
107 // LoadKeyPair reads and parses a public/private key pair from a pair of files.
108 // The files must contain PEM encoded data.
109 func LoadKeyPair(certFile, keyFile string) (*tls.Certificate, error) {
110 log.Debugf("Load key pair: %s %s", certFile, keyFile)
111 cert, err := tls.LoadX509KeyPair(certFile, keyFile)
112 if err != nil {
113 return nil, err
114 }
115 return &cert, nil
116 }
117
118 // LoadCAFile reads and parses CA certificates from a file into a pool.
119 // The file must contain PEM encoded data.
120 func LoadCAFile(caFile string) (*x509.CertPool, error) {
121 log.Debugf("Load CA cert file: %s", caFile)
122 pemCerts, err := os.ReadFile(caFile)
123 if err != nil {
124 return nil, err
125 }
126 pool := x509.NewCertPool()
127 pool.AppendCertsFromPEM(pemCerts)
128 return pool, nil
129 }