-
Notifications
You must be signed in to change notification settings - Fork 291
/
grpc_certs.go
84 lines (67 loc) · 1.79 KB
/
grpc_certs.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
//
// (C) Copyright 2019-2021 Intel Corporation.
//
// SPDX-License-Identifier: BSD-2-Clause-Patent
//
package security
import (
"github.com/pkg/errors"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
)
func GetServerTransportCredentials(cfg *TransportConfig) (credentials.TransportCredentials, error) {
if cfg == nil {
return nil, errors.New("nil TransportConfig")
}
if cfg.tlsKeypair == nil || cfg.caPool == nil {
err := cfg.PreLoadCertData()
if err != nil {
return nil, err
}
}
creds := credentials.NewTLS(serverTLSConfig(cfg))
return creds, nil
}
func GetClientTransportCredentials(cfg *TransportConfig) (credentials.TransportCredentials, error) {
if cfg == nil {
return nil, errors.New("nil TransportConfig")
}
if cfg.tlsKeypair == nil || cfg.caPool == nil {
err := cfg.PreLoadCertData()
if err != nil {
return nil, err
}
}
creds := credentials.NewTLS(clientTLSConfig(cfg))
return creds, nil
}
func ServerOptionForTransportConfig(cfg *TransportConfig) (grpc.ServerOption, error) {
if cfg == nil {
return nil, errors.New("nil TransportConfig")
}
if cfg.AllowInsecure {
return grpc.Creds(nil), nil
}
creds, err := GetServerTransportCredentials(cfg)
if err != nil {
return nil, err
}
return grpc.Creds(creds), nil
}
func DialOptionForTransportConfig(cfg *TransportConfig) (grpc.DialOption, error) {
if cfg == nil {
return nil, errors.New("nil TransportConfig")
}
if cfg.AllowInsecure {
return grpc.WithTransportCredentials(insecure.NewCredentials()), nil
}
if cfg.ServerName == "" {
return nil, errors.New("No ServerName set in TransportConfig")
}
creds, err := GetClientTransportCredentials(cfg)
if err != nil {
return nil, err
}
return grpc.WithTransportCredentials(creds), nil
}