-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreds.go
76 lines (64 loc) · 1.86 KB
/
creds.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package rksync
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"github.com/pkg/errors"
"github.com/rkcloudchain/rksync/config"
"github.com/rkcloudchain/rksync/logging"
"github.com/rkcloudchain/rksync/util"
"google.golang.org/grpc/credentials"
)
func clientTransportCredentials(cfg *config.Config) (credentials.TransportCredentials, error) {
clientCert, err := clientCertificate(cfg)
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{clientCert},
}
certPool := x509.NewCertPool()
for _, root := range cfg.Server.SecOpts.ServerRootCAs {
pemCerts, err := ioutil.ReadFile(root)
if err != nil {
logging.Errorf("Failed read root ca file: %s (%s)", root, err)
return nil, err
}
err = addPemToCertPool(pemCerts, certPool)
if err != nil {
logging.Errorf("Failed adding certificates to peer's client TLS trust pool: %s", err)
return nil, err
}
}
tlsConfig.RootCAs = certPool
return credentials.NewTLS(tlsConfig), nil
}
func addPemToCertPool(pemCerts []byte, pool *x509.CertPool) error {
certs, _, err := util.PEMToX509Certs(pemCerts)
if err != nil {
return err
}
for _, cert := range certs {
pool.AddCert(cert)
}
return nil
}
func clientCertificate(cfg *config.Config) (tls.Certificate, error) {
clientKey, err := ioutil.ReadFile(cfg.Server.SecOpts.Key)
if err != nil {
return tls.Certificate{}, errors.WithMessage(err, "error loading client TLS key")
}
clientCert, err := ioutil.ReadFile(cfg.Server.SecOpts.Certificate)
if err != nil {
return tls.Certificate{}, errors.WithMessage(err, "error loading client TLS certificate")
}
cert, err := tls.X509KeyPair(clientCert, clientKey)
if err != nil {
return tls.Certificate{}, errors.WithMessage(err, "error parsing client TLS key pair")
}
return cert, nil
}