-
Notifications
You must be signed in to change notification settings - Fork 5
/
dial_opts.go
149 lines (129 loc) · 4.38 KB
/
dial_opts.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Package grpcutil implements various utilities to simplify common gRPC APIs.
package grpcutil
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"io/fs"
"os"
"github.com/certifi/gocertifi"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
type verification int
const (
// SkipVerifyCA is a constant that improves the readability of functions
// with the insecureSkipVerify parameter.
SkipVerifyCA verification = iota
// VerifyCA is a constant that improves the readability of functions
// with the insecureSkipVerify parameter.
VerifyCA
)
func (v verification) asInsecureSkipVerify() bool {
switch v {
case SkipVerifyCA:
return true
case VerifyCA:
return false
default:
panic("unknown verification")
}
}
// WithSystemCerts returns a grpc.DialOption that uses the system-provided
// certificate authority chain to verify the connection.
//
// If one cannot be found, this falls back to using a vendored version of
// Mozilla's collection of root certificate authorities.
func WithSystemCerts(v verification) (grpc.DialOption, error) {
certPool, err := x509.SystemCertPool()
if err != nil {
// Fall back to Mozilla collection of root CAs.
certPool, err = gocertifi.CACerts()
if err != nil {
// This library promises that this should never occur.
return nil, fmt.Errorf("gocertifi returned an error: %w", err)
}
}
return grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
RootCAs: certPool,
InsecureSkipVerify: v.asInsecureSkipVerify(), // nolint
})), nil
}
func forEachFileContents(dirPath string, fn func(contents []byte)) error {
dirFS := os.DirFS(dirPath)
return fs.WalkDir(dirFS, ".", func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
contents, err := fs.ReadFile(dirFS, d.Name())
if err != nil {
return err
}
fn(contents)
}
return nil
})
}
// WithCustomCerts returns a grpc.DialOption for requiring TLS that is
// authenticated using a certificate authority chain provided as a path on disk.
//
// If the path is a directory, all files are loaded.
func WithCustomCerts(v verification, certPaths ...string) (grpc.DialOption, error) {
var caFiles [][]byte
for _, certPath := range certPaths {
fi, err := os.Stat(certPath)
if err != nil {
return nil, fmt.Errorf("failed to find certificate: %w", err)
}
if fi.IsDir() {
if err = forEachFileContents(certPath, func(contents []byte) {
caFiles = append(caFiles, contents)
}); err != nil {
return nil, err
}
} else {
contents, err := os.ReadFile(certPath)
if err != nil {
return nil, err
}
caFiles = append(caFiles, contents)
}
}
return WithCustomCertBytes(v, caFiles...)
}
// WithCustomCertBytes returns a grpc.DialOption for requiring TLS that is
// authenticated using a certificate authority chain provided in bytes.
func WithCustomCertBytes(v verification, certsContents ...[]byte) (grpc.DialOption, error) {
certPool := x509.NewCertPool()
for _, certContents := range certsContents {
if ok := certPool.AppendCertsFromPEM(certContents); !ok {
return nil, errors.New("failed to append certs from CA PEM")
}
}
return grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{
RootCAs: certPool,
InsecureSkipVerify: v.asInsecureSkipVerify(), // nolint:gosec
})), nil
}
type secureMetadataCreds map[string]string
func (c secureMetadataCreds) RequireTransportSecurity() bool { return true }
func (c secureMetadataCreds) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
return c, nil
}
// WithBearerToken returns a grpc.DialOption that adds a standard HTTP Bearer
// token to all requests sent from a client.
func WithBearerToken(token string) grpc.DialOption {
return grpc.WithPerRPCCredentials(secureMetadataCreds{"authorization": "Bearer " + token})
}
type insecureMetadataCreds map[string]string
func (c insecureMetadataCreds) RequireTransportSecurity() bool { return false }
func (c insecureMetadataCreds) GetRequestMetadata(_ context.Context, _ ...string) (map[string]string, error) {
return c, nil
}
// WithInsecureBearerToken returns a grpc.DialOption that adds a standard HTTP
// Bearer token to all requests sent from an insecure client.
//
// Must be used in conjunction with `insecure.NewCredentials()`.
func WithInsecureBearerToken(token string) grpc.DialOption {
return grpc.WithPerRPCCredentials(insecureMetadataCreds{"authorization": "Bearer " + token})
}