-
Notifications
You must be signed in to change notification settings - Fork 75
/
generator.go
333 lines (304 loc) · 11.8 KB
/
generator.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package bootstrap
import (
"context"
"crypto/x509"
"encoding/pem"
"net"
"os"
"sort"
"strings"
)
import (
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)
import (
mesh_proto "github.com/apache/dubbo-kubernetes/api/mesh/v1alpha1"
xds_config "github.com/apache/dubbo-kubernetes/pkg/config/xds"
bootstrap_config "github.com/apache/dubbo-kubernetes/pkg/config/xds/bootstrap"
core_mesh "github.com/apache/dubbo-kubernetes/pkg/core/resources/apis/mesh"
core_manager "github.com/apache/dubbo-kubernetes/pkg/core/resources/manager"
"github.com/apache/dubbo-kubernetes/pkg/core/resources/model/rest"
core_store "github.com/apache/dubbo-kubernetes/pkg/core/resources/store"
core_xds "github.com/apache/dubbo-kubernetes/pkg/core/xds"
"github.com/apache/dubbo-kubernetes/pkg/xds/bootstrap/types"
)
type BootstrapGenerator interface {
Generate(ctx context.Context, request types.BootstrapRequest) (proto.Message, DubboDpBootstrap, error)
}
func NewDefaultBootstrapGenerator(
resManager core_manager.ResourceManager,
serverConfig *bootstrap_config.BootstrapServerConfig,
proxyConfig xds_config.Proxy,
dpServerCertFile string,
authEnabledForProxyType map[string]bool,
enableReloadableTokens bool,
hdsEnabled bool,
defaultAdminPort uint32,
) (BootstrapGenerator, error) {
hostsAndIps, err := hostsAndIPs()
if err != nil {
return nil, err
}
if serverConfig.Params.XdsHost != "" && !hostsAndIps[serverConfig.Params.XdsHost] {
return nil, errors.Errorf("hostname: %s set by DUBBO_BOOTSTRAP_SERVER_PARAMS_XDS_HOST is not available in the DP Server certificate. Available hostnames: %q. Change the hostname or generate certificate with proper hostname.", serverConfig.Params.XdsHost, hostsAndIps.slice())
}
return &bootstrapGenerator{
resManager: resManager,
config: serverConfig,
proxyConfig: proxyConfig,
xdsCertFile: dpServerCertFile,
authEnabledForProxyType: authEnabledForProxyType,
enableReloadableTokens: enableReloadableTokens,
hostsAndIps: hostsAndIps,
hdsEnabled: hdsEnabled,
defaultAdminPort: defaultAdminPort,
}, nil
}
func hostsAndIPs() (SANSet, error) {
hostsAndIps := map[string]bool{}
interfaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, iface := range interfaces {
addrs, err := iface.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if ok {
hostsAndIps[ipNet.IP.String()] = true
}
}
}
hostname, err := os.Hostname()
if err != nil {
return nil, err
}
hostsAndIps[hostname] = true
hostsAndIps["localhost"] = true
return hostsAndIps, nil
}
type bootstrapGenerator struct {
resManager core_manager.ResourceManager
config *bootstrap_config.BootstrapServerConfig
proxyConfig xds_config.Proxy
authEnabledForProxyType map[string]bool
enableReloadableTokens bool
xdsCertFile string
hostsAndIps SANSet
hdsEnabled bool
defaultAdminPort uint32
}
type SANSet map[string]bool
func (s SANSet) slice() []string {
sans := []string{}
for san := range s {
sans = append(sans, san)
}
sort.Strings(sans)
return sans
}
func hostsAndIPsFromCertFile(dpServerCertFile string) (SANSet, error) {
certBytes, err := os.ReadFile(dpServerCertFile)
if err != nil {
return nil, errors.Wrap(err, "could not read certificate")
}
pemCert, _ := pem.Decode(certBytes)
if pemCert == nil {
return nil, errors.New("could not parse certificate")
}
cert, err := x509.ParseCertificate(pemCert.Bytes)
if err != nil {
return nil, errors.Wrap(err, "could not parse certificate")
}
hostsAndIps := map[string]bool{}
for _, dnsName := range cert.DNSNames {
hostsAndIps[dnsName] = true
}
for _, ip := range cert.IPAddresses {
hostsAndIps[ip.String()] = true
}
return hostsAndIps, nil
}
func (b *bootstrapGenerator) Generate(ctx context.Context, request types.BootstrapRequest) (proto.Message, DubboDpBootstrap, error) {
if request.ProxyType == "" {
request.ProxyType = string(mesh_proto.DataplaneProxyType)
}
dubboDpBootstrap := DubboDpBootstrap{}
//if err := b.validateRequest(request); err != nil {
// return nil, dubboDpBootstrap, err
//}
accessLogSocketPath := request.AccessLogSocketPath
if accessLogSocketPath == "" {
accessLogSocketPath = core_xds.AccessLogSocketName(os.TempDir(), request.Name, request.Mesh)
}
metricsSocketPath := request.MetricsResources.SocketPath
if metricsSocketPath == "" {
metricsSocketPath = core_xds.MetricsHijackerSocketName(os.TempDir(), request.Name, request.Mesh)
}
proxyId := core_xds.BuildProxyId(request.Mesh, request.Name)
params := configParameters{
Id: proxyId.String(),
AdminAddress: b.config.Params.AdminAddress,
AdminAccessLogPath: b.adminAccessLogPath(request.OperatingSystem),
XdsHost: b.xdsHost(request),
XdsPort: b.config.Params.XdsPort,
XdsConnectTimeout: b.config.Params.XdsConnectTimeout.Duration,
DataplaneToken: request.DataplaneToken,
DataplaneTokenPath: request.DataplaneTokenPath,
DataplaneResource: request.DataplaneResource,
Version: &mesh_proto.Version{
DubboDp: &mesh_proto.DubboDpVersion{
Version: request.Version.DubboDp.Version,
GitTag: request.Version.DubboDp.GitTag,
GitCommit: request.Version.DubboDp.GitCommit,
BuildDate: request.Version.DubboDp.BuildDate,
},
Envoy: &mesh_proto.EnvoyVersion{
Version: request.Version.Envoy.Version,
Build: request.Version.Envoy.Build,
DubboDpCompatible: request.Version.Envoy.DubboDpCompatible,
},
},
DynamicMetadata: request.DynamicMetadata,
DNSPort: request.DNSPort,
EmptyDNSPort: request.EmptyDNSPort,
ProxyType: request.ProxyType,
Features: request.Features,
Resources: request.Resources,
Workdir: request.Workdir,
AccessLogSocketPath: accessLogSocketPath,
MetricsSocketPath: metricsSocketPath,
MetricsCertPath: request.MetricsResources.CertPath,
MetricsKeyPath: request.MetricsResources.KeyPath,
}
setAdminPort := func(adminPortFromResource uint32) {
if adminPortFromResource != 0 {
params.AdminPort = adminPortFromResource
} else {
params.AdminPort = b.defaultAdminPort
}
}
switch mesh_proto.ProxyType(params.ProxyType) {
case mesh_proto.IngressProxyType:
zoneIngress, err := b.zoneIngressFor(ctx, request, proxyId)
if err != nil {
return nil, dubboDpBootstrap, err
}
params.Service = "ingress"
setAdminPort(zoneIngress.Spec.GetNetworking().GetAdmin().GetPort())
case mesh_proto.EgressProxyType:
zoneEgress, err := b.zoneEgressFor(ctx, request, proxyId)
if err != nil {
return nil, dubboDpBootstrap, err
}
params.Service = "egress"
setAdminPort(zoneEgress.Spec.GetNetworking().GetAdmin().GetPort())
default:
return nil, dubboDpBootstrap, errors.Errorf("unknown proxy type %v", params.ProxyType)
}
var err error
config, err := genConfig(params, b.proxyConfig, b.enableReloadableTokens)
if err != nil {
return nil, dubboDpBootstrap, errors.Wrap(err, "failed creating bootstrap conf")
}
if err = config.Validate(); err != nil {
return nil, dubboDpBootstrap, errors.Wrap(err, "Envoy bootstrap config is not valid")
}
return config, dubboDpBootstrap, nil
}
func (b *bootstrapGenerator) xdsHost(request types.BootstrapRequest) string {
if b.config.Params.XdsHost != "" { // XdsHost from config takes precedence over Host from request
return b.config.Params.XdsHost
} else {
return request.Host
}
}
var DpTokenRequired = errors.New("Dataplane Token is required. Generate token using 'dubboctl generate dataplane-token > /path/file' and provide it via --dataplane-token-file=/path/file argument to Dubbo DP")
var NotCA = errors.New("A data plane proxy is trying to verify the control plane using the certificate which is not a certificate authority (basic constraint 'CA' is set to 'false').\n" +
"Provide CA that was used to sign a certificate used in the control plane by using 'dubbo-dp run --ca-cert-file=file' or via DUBBO_CONTROL_PLANE_CA_CERT_FILE")
func SANMismatchErr(host string, sans []string) error {
return errors.Errorf("A data plane proxy is trying to connect to the control plane using %q address, but the certificate in the control plane has the following SANs %q. "+
"Either change the --cp-address in dubbo-dp to one of those or execute the following steps:\n"+
"1) Generate a new certificate with the address you are trying to use. It is recommended to use trusted Certificate Authority, but you can also generate self-signed certificates using 'dubboctl generate tls-certificate --type=server --cp-hostname=%s'\n"+
"2) Set DUBBO_GENERAL_TLS_CERT_FILE and DUBBO_GENERAL_TLS_KEY_FILE or the equivalent in Dubbo CP config file to the new certificate.\n"+
"3) Restart the control plane to read the new certificate and start dubbo-dp.", host, sans, host)
}
func ISSANMismatchErr(err error) bool {
if err == nil {
return false
}
return strings.HasPrefix(err.Error(), "A data plane proxy is trying to connect to the control plane using")
}
// caCert gets CA cert that was used to signed cert that DP server is protected with.
// Technically result of this function does not have to be a valid CA.
// When user provides custom cert + key and does not provide --ca-cert-file to dubbo-dp run, this can return just a regular cert
func (b *bootstrapGenerator) adminAccessLogPath(operatingSystem string) string {
if operatingSystem == "" { // backwards compatibility
return b.config.Params.AdminAccessLogPath
}
if b.config.Params.AdminAccessLogPath == os.DevNull && operatingSystem == "windows" {
// when AdminAccessLogPath was not explicitly set and DPP OS is Windows we need to set window specific DevNull.
// otherwise when CP is on Linux, we would set /dev/null which is not valid on Windows.
return "NUL"
}
return b.config.Params.AdminAccessLogPath
}
func (b *bootstrapGenerator) validateRequest(request types.BootstrapRequest) error {
if b.authEnabledForProxyType[request.ProxyType] && request.DataplaneToken == "" && request.DataplaneTokenPath == "" {
return DpTokenRequired
}
if b.config.Params.XdsHost == "" { // XdsHost takes precedence over Host in the request, so validate only when it is not set
if !b.hostsAndIps[request.Host] {
return SANMismatchErr(request.Host, b.hostsAndIps.slice())
}
}
return nil
}
func (b *bootstrapGenerator) zoneEgressFor(ctx context.Context, request types.BootstrapRequest, proxyId *core_xds.ProxyId) (*core_mesh.ZoneEgressResource, error) {
if request.DataplaneResource != "" {
res, err := rest.YAML.UnmarshalCore([]byte(request.DataplaneResource))
if err != nil {
return nil, err
}
zoneEgress, ok := res.(*core_mesh.ZoneEgressResource)
if !ok {
return nil, errors.Errorf("invalid resource")
}
if err := zoneEgress.Validate(); err != nil {
return nil, err
}
return zoneEgress, nil
} else {
zoneEgress := core_mesh.NewZoneEgressResource()
if err := b.resManager.Get(ctx, zoneEgress, core_store.GetBy(proxyId.ToResourceKey())); err != nil {
return nil, err
}
return zoneEgress, nil
}
}
func (b *bootstrapGenerator) zoneIngressFor(ctx context.Context, request types.BootstrapRequest, proxyId *core_xds.ProxyId) (*core_mesh.ZoneIngressResource, error) {
if request.DataplaneResource != "" {
res, err := rest.YAML.UnmarshalCore([]byte(request.DataplaneResource))
if err != nil {
return nil, err
}
zoneIngress, ok := res.(*core_mesh.ZoneIngressResource)
if !ok {
return nil, errors.Errorf("invalid resource")
}
if err := zoneIngress.Validate(); err != nil {
return nil, err
}
return zoneIngress, nil
} else {
zoneIngress := core_mesh.NewZoneIngressResource()
if err := b.resManager.Get(ctx, zoneIngress, core_store.GetBy(proxyId.ToResourceKey())); err != nil {
return nil, err
}
return zoneIngress, nil
}
}