-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.go
166 lines (132 loc) · 4.09 KB
/
options.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package aserto
import (
"context"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/samber/lo"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
)
// ConnectionOptions holds settings used to establish a connection to the authorizer service.
type ConnectionOptions struct {
Config
// Credentials used to authenticate with the authorizer service. Either API Key or OAuth Token.
Creds credentials.PerRPCCredentials
// UnaryClientInterceptors passed to the grpc client.
UnaryClientInterceptors []grpc.UnaryClientInterceptor
// StreamClientInterceptors passed to the grpc client.
StreamClientInterceptors []grpc.StreamClientInterceptor
// DialOptions passed to the grpc client.
DialOptions []grpc.DialOption
}
// NewConnectionOptions creates a ConnectionOptions object from a collection of ConnectionOption functions.
func NewConnectionOptions(opts ...ConnectionOption) (*ConnectionOptions, error) {
options := &ConnectionOptions{}
if err := options.Apply(opts...); err != nil {
return nil, err
}
return options, nil
}
// Apply additional options.
func (o *ConnectionOptions) Apply(opts ...ConnectionOption) error {
var errs error
for _, opt := range opts {
if err := opt(o); err != nil {
errs = multierror.Append(errs, err)
}
}
return errs
}
func (o *ConnectionOptions) ToDialOptions() ([]grpc.DialOption, error) {
if o.Insecure && o.NoTLS {
return nil, errors.Wrap(ErrInvalidOptions, "insecure and no_tls options are mutually exclusive")
}
transportCreds, err := o.transportCredentials()
if err != nil {
return nil, err
}
opts := []grpc.DialOption{
transportCreds,
grpc.WithChainStreamInterceptor(o.StreamClientInterceptors...),
grpc.WithChainUnaryInterceptor(o.UnaryClientInterceptors...),
}
opts = append(opts, o.DialOptions...)
if o.Creds != nil {
opts = append(opts, grpc.WithPerRPCCredentials(o.Creds))
}
if o.TenantID != "" {
opts = append(opts, contextWrapperInterceptor(o.tenantContext)...)
}
if o.AccountID != "" {
opts = append(opts, contextWrapperInterceptor(o.accountContext)...)
}
if o.NoProxy {
opts = append(opts, grpc.WithNoProxy())
}
if len(o.Headers) > 0 {
opts = append(opts, o.outgoingHeaders()...)
}
return opts, nil
}
func (o *ConnectionOptions) transportCredentials() (grpc.DialOption, error) {
if o.NoTLS {
return grpc.WithTransportCredentials(insecure.NewCredentials()), nil
}
cfg := &TLSConfig{
Cert: o.ClientCertPath,
Key: o.ClientKeyPath,
CA: o.CACertPath,
}
creds, err := cfg.ClientCredentials(o.Insecure)
if err != nil {
return nil, errors.Wrap(err, "failed to create transport credentials")
}
return grpc.WithTransportCredentials(creds), nil
}
func (o *ConnectionOptions) tenantContext(ctx context.Context) context.Context {
return SetTenantContext(ctx, o.TenantID)
}
func (o *ConnectionOptions) accountContext(ctx context.Context) context.Context {
return SetAccountContext(ctx, o.AccountID)
}
func (o *ConnectionOptions) outgoingHeaders() []grpc.DialOption {
pairs := lo.Reduce(
lo.Entries(o.Headers),
func(acc []string, entry lo.Entry[string, string], _ int) []string {
return append(acc, entry.Key, entry.Value)
},
nil,
)
appendOutgoing := func(ctx context.Context) context.Context {
return metadata.AppendToOutgoingContext(ctx, pairs...)
}
return contextWrapperInterceptor(appendOutgoing)
}
func contextWrapperInterceptor(wrap func(ctx context.Context) context.Context) []grpc.DialOption {
unary := func(
ctx context.Context,
method string,
req, reply interface{},
cc *grpc.ClientConn,
invoker grpc.UnaryInvoker,
opts ...grpc.CallOption,
) error {
return invoker(wrap(ctx), method, req, reply, cc, opts...)
}
stream := func(
ctx context.Context,
desc *grpc.StreamDesc,
cc *grpc.ClientConn,
method string,
streamer grpc.Streamer,
opts ...grpc.CallOption,
) (grpc.ClientStream, error) {
return streamer(wrap(ctx), desc, cc, method, opts...)
}
return []grpc.DialOption{
grpc.WithChainUnaryInterceptor(unary),
grpc.WithChainStreamInterceptor(stream),
}
}