-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
140 lines (106 loc) · 3.82 KB
/
config.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
package aserto
import (
"github.com/pkg/errors"
"google.golang.org/grpc"
)
var ErrInvalidConfig = errors.New("invalid configuration")
// gRPC Client Configuration.
type Config struct {
// Address of the service to connect to.
//
// Address is typically in the form "hostname:port" but may also be a Unix socket or DNS URI.
// See https://github.com/grpc/grpc/blob/master/doc/naming.md#name-syntax for more details.
Address string `json:"address"`
// A JWT to be used for authentication with the service.
//
// Token and APIKey are mutually exclusive.
Token string `json:"token"`
// An API key to be used for authentication with the service.
APIKey string `json:"api_key"`
// An Aserto tenant ID.
TenantID string `json:"tenant_id"`
// An Aserto account ID.
AccountID string `json:"account_id"`
// In mTLS connections, ClientCertPath is the path of the client's
// certificate file.
ClientCertPath string `json:"client_cert_path"`
// In mTLS connections, ClientKeyPath is the path of the client's
// private key file.
ClientKeyPath string `json:"client_key_path"`
// In TLS connections, CACertPath is the path of a CA certificate to
// validate the server's certificate against.
CACertPath string `json:"ca_cert_path"`
// In TLS connections, skip verification of the server certificate.
Insecure bool `json:"insecure"`
// Disable TLS and use a plaintext connection.
NoTLS bool `json:"no_tls"`
// NoProxy bypasses any configured HTTP proxy.
NoProxy bool `json:"no_proxy"`
// Additional headers to include in requests to the service.
Headers map[string]string `json:"headers"`
// Deprecated: no longer used. Timeouts are controlled on a per-call basis
// by the provided context.
TimeoutInSeconds int `json:"timeout_in_seconds"`
}
// Connects to the service specified in Config, possibly with additional
// connection options.
func (cfg *Config) Connect(opts ...ConnectionOption) (*grpc.ClientConn, error) {
if cfg.APIKey != "" {
opts = append(opts, WithAPIKeyAuth(cfg.APIKey))
}
if cfg.Token != "" {
opts = append(opts, WithTokenAuth(cfg.Token))
}
connOpts := &ConnectionOptions{Config: *cfg}
if err := connOpts.Apply(opts...); err != nil {
return nil, err
}
return Connect(connOpts)
}
// Converts the Config into a ConnectionOption slice that can be passed to NewConnection().
func (cfg *Config) ToConnectionOptions() ([]ConnectionOption, error) {
if err := cfg.validate(); err != nil {
return nil, err
}
options := []ConnectionOption{
WithInsecure(cfg.Insecure),
WithNoTLS(cfg.NoTLS),
}
if cfg.Token != "" {
options = append(options, WithTokenAuth(cfg.Token))
}
if cfg.APIKey != "" {
options = append(options, WithAPIKeyAuth(cfg.APIKey))
}
if cfg.Address != "" {
options = append(options, WithAddr(cfg.Address))
}
if cfg.CACertPath != "" {
options = append(options, WithCACertPath(cfg.CACertPath))
}
if cfg.TenantID != "" {
options = append(options, WithTenantID(cfg.TenantID))
}
if cfg.ClientCertPath != "" {
options = append(options, WithClientCert(cfg.ClientCertPath, cfg.ClientKeyPath))
}
for key, value := range cfg.Headers {
options = append(options, WithHeader(key, value))
}
return options, nil
}
func (cfg *Config) validate() error {
if cfg.APIKey != "" && cfg.Token != "" {
return errors.Wrap(ErrInvalidConfig, "api_key and token are mutually exclusive")
}
if cfg.Insecure && cfg.NoTLS {
return errors.Wrap(ErrInvalidConfig, "insecure and no_tls are mutually exclusive")
}
if cfg.NoTLS && (cfg.ClientCertPath != "" || cfg.ClientKeyPath != "") {
return errors.Wrap(ErrInvalidConfig, "mtls (client_cert_path and client_cert_key) and no_tls are mutually exclusive")
}
if !cfg.NoTLS && ((cfg.ClientCertPath == "") != (cfg.ClientKeyPath == "")) {
return errors.Wrap(ErrInvalidConfig, "client_cert_path and client_key_path must be specified together")
}
return nil
}