-
Notifications
You must be signed in to change notification settings - Fork 240
/
tls.go
48 lines (40 loc) · 1.24 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
package transport
import (
"crypto/tls"
"crypto/x509"
"fmt"
"os"
"google.golang.org/grpc/credentials"
)
const (
serverCertPEMFilename = "tls.crt"
serverKeyPEMFilename = "tls.key"
caCertPEMFilename = "ca.crt"
path = "/usr/local/npm"
)
func serverTLSCreds() (credentials.TransportCredentials, error) {
certFilepath := path + "/" + serverCertPEMFilename
keyFilepath := path + "/" + serverKeyPEMFilename
creds, err := credentials.NewServerTLSFromFile(certFilepath, keyFilepath)
if err != nil {
return nil, fmt.Errorf("failed to create creds from cert/key files : %w", err)
}
return creds, nil
}
func clientTLSConfig() (*tls.Config, error) {
caCertFilepath := path + "/" + caCertPEMFilename
// Load certificate of the CA who signed server's certificate
pemServerCA, err := os.ReadFile(caCertFilepath)
if err != nil {
return nil, fmt.Errorf("Failed to read the CA cert : %w", err)
}
certPool := x509.NewCertPool()
if !certPool.AppendCertsFromPEM(pemServerCA) {
return nil, fmt.Errorf("failed to append ca cert to cert pool : %w", ErrTLSCerts)
}
// Create the credentials and return it
return &tls.Config{ //nolint // setting tls min version to 3
RootCAs: certPool,
InsecureSkipVerify: false,
}, nil
}