1
1
package bootstrap
2
2
3
3
import (
4
+ "path/filepath"
5
+
4
6
xds_accesslog_config "github.com/envoyproxy/go-control-plane/envoy/config/accesslog/v3"
5
7
xds_bootstrap "github.com/envoyproxy/go-control-plane/envoy/config/bootstrap/v3"
6
8
xds_cluster "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
@@ -9,6 +11,7 @@ import (
9
11
xds_accesslog_stream "github.com/envoyproxy/go-control-plane/envoy/extensions/access_loggers/stream/v3"
10
12
xds_transport_sockets "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
11
13
xds_upstream_http "github.com/envoyproxy/go-control-plane/envoy/extensions/upstreams/http/v3"
14
+ xds_discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
12
15
"github.com/golang/protobuf/ptypes/any"
13
16
"google.golang.org/protobuf/types/known/anypb"
14
17
@@ -17,6 +20,98 @@ import (
17
20
"github.com/openservicemesh/osm/pkg/errcode"
18
21
)
19
22
23
+ const (
24
+ envoyTLSCertificateSecretName = "tls_sds"
25
+ envoyValidationContextSecretName = "validation_context_sds"
26
+
27
+ // EnvoyBootstrapConfigFile is the name Envoy bootstrap configuration file
28
+ EnvoyBootstrapConfigFile = "bootstrap.yaml"
29
+
30
+ // EnvoyTLSCertificateSDSSecretFile is the name of the Envoy TLS certificate SDS config file
31
+ EnvoyTLSCertificateSDSSecretFile = "tls_certificate_sds_secret.yaml"
32
+
33
+ // EnvoyValidationContextSDSSecretFile is the name of the Envoy validation context SDS config file
34
+ EnvoyValidationContextSDSSecretFile = "validation_context_sds_secret.yaml"
35
+
36
+ // EnvoyProxyConfigPath is the path where the Envoy bootstrap config info is located
37
+ EnvoyProxyConfigPath = "/etc/envoy"
38
+
39
+ // EnvoyXDSCACertFile is the name of the Envoy XDS CA certificate file
40
+ EnvoyXDSCACertFile = "cacert.pem"
41
+
42
+ // EnvoyXDSCertFile is the name of the Envoy XDS certificate file
43
+ EnvoyXDSCertFile = "sds_cert.pem"
44
+
45
+ // EnvoyXDSKeyFile is the name of the Envoy XDS private key file
46
+ EnvoyXDSKeyFile = "sds_key.pem"
47
+ )
48
+
49
+ var (
50
+ envoyTLSCertificateConfigPath = filepath .Join (EnvoyProxyConfigPath , EnvoyTLSCertificateSDSSecretFile )
51
+ envoyValidationContextConfigPath = filepath .Join (EnvoyProxyConfigPath , EnvoyValidationContextSDSSecretFile )
52
+
53
+ envoyXDSCertPath = filepath .Join (EnvoyProxyConfigPath , EnvoyXDSCertFile )
54
+ envoyXDSKeyPath = filepath .Join (EnvoyProxyConfigPath , EnvoyXDSKeyFile )
55
+ envoyXDSCACertPath = filepath .Join (EnvoyProxyConfigPath , EnvoyXDSCACertFile )
56
+ )
57
+
58
+ // BuildTLSSecret builds and returns an Envoy Discovery Response object for Envoy's xDS TLS
59
+ // Certificate
60
+ func BuildTLSSecret () (* xds_discovery.DiscoveryResponse , error ) {
61
+ secret := & xds_transport_sockets.Secret {
62
+ Name : envoyTLSCertificateSecretName ,
63
+ Type : & xds_transport_sockets.Secret_TlsCertificate {
64
+ TlsCertificate : & xds_transport_sockets.TlsCertificate {
65
+ CertificateChain : & xds_core.DataSource {
66
+ Specifier : & xds_core.DataSource_Filename {
67
+ Filename : envoyXDSCertPath ,
68
+ },
69
+ },
70
+ PrivateKey : & xds_core.DataSource {
71
+ Specifier : & xds_core.DataSource_Filename {
72
+ Filename : envoyXDSKeyPath ,
73
+ },
74
+ },
75
+ },
76
+ },
77
+ }
78
+ marshalledSecret , err := anypb .New (secret )
79
+ if err != nil {
80
+ log .Error ().Err (err ).Msg ("Error marshalling Secret for Envoy's xDS TLS certificate resource" )
81
+ return nil , err
82
+ }
83
+
84
+ return & xds_discovery.DiscoveryResponse {
85
+ Resources : []* any.Any {marshalledSecret },
86
+ }, nil
87
+ }
88
+
89
+ // BuildValidationSecret builds and returns an Envoy Discovery Response object for Envoy's xDS
90
+ // Validation Context
91
+ func BuildValidationSecret () (* xds_discovery.DiscoveryResponse , error ) {
92
+ secret := & xds_transport_sockets.Secret {
93
+ Name : envoyValidationContextSecretName ,
94
+ Type : & xds_transport_sockets.Secret_ValidationContext {
95
+ ValidationContext : & xds_transport_sockets.CertificateValidationContext {
96
+ TrustedCa : & xds_core.DataSource {
97
+ Specifier : & xds_core.DataSource_Filename {
98
+ Filename : envoyXDSCACertPath ,
99
+ },
100
+ },
101
+ },
102
+ },
103
+ }
104
+ marshalledSecret , err := anypb .New (secret )
105
+ if err != nil {
106
+ log .Error ().Err (err ).Msg ("Error marshalling Secret for Envoy's xDS Validation Context resource" )
107
+ return nil , err
108
+ }
109
+
110
+ return & xds_discovery.DiscoveryResponse {
111
+ Resources : []* any.Any {marshalledSecret },
112
+ }, nil
113
+ }
114
+
20
115
// BuildFromConfig builds and returns an Envoy Bootstrap object from the given config
21
116
func BuildFromConfig (config Config ) (* xds_bootstrap.Bootstrap , error ) {
22
117
httpProtocolOptions := & xds_upstream_http.HttpProtocolOptions {
@@ -51,11 +146,12 @@ func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) {
51
146
AlpnProtocols : []string {
52
147
"h2" ,
53
148
},
54
- ValidationContextType : & xds_transport_sockets.CommonTlsContext_ValidationContext {
55
- ValidationContext : & xds_transport_sockets.CertificateValidationContext {
56
- TrustedCa : & xds_core.DataSource {
57
- Specifier : & xds_core.DataSource_InlineBytes {
58
- InlineBytes : config .TrustedCA ,
149
+ ValidationContextType : & xds_transport_sockets.CommonTlsContext_ValidationContextSdsSecretConfig {
150
+ ValidationContextSdsSecretConfig : & xds_transport_sockets.SdsSecretConfig {
151
+ Name : envoyValidationContextSecretName ,
152
+ SdsConfig : & xds_core.ConfigSource {
153
+ ConfigSourceSpecifier : & xds_core.ConfigSource_Path {
154
+ Path : envoyValidationContextConfigPath ,
59
155
},
60
156
},
61
157
},
@@ -66,16 +162,12 @@ func BuildFromConfig(config Config) (*xds_bootstrap.Bootstrap, error) {
66
162
CipherSuites : config .CipherSuites ,
67
163
EcdhCurves : config .ECDHCurves ,
68
164
},
69
- TlsCertificates : []* xds_transport_sockets.TlsCertificate {
165
+ TlsCertificateSdsSecretConfigs : []* xds_transport_sockets.SdsSecretConfig {
70
166
{
71
- CertificateChain : & xds_core.DataSource {
72
- Specifier : & xds_core.DataSource_InlineBytes {
73
- InlineBytes : config .CertificateChain ,
74
- },
75
- },
76
- PrivateKey : & xds_core.DataSource {
77
- Specifier : & xds_core.DataSource_InlineBytes {
78
- InlineBytes : config .PrivateKey ,
167
+ Name : envoyTLSCertificateSecretName ,
168
+ SdsConfig : & xds_core.ConfigSource {
169
+ ConfigSourceSpecifier : & xds_core.ConfigSource_Path {
170
+ Path : envoyTLSCertificateConfigPath ,
79
171
},
80
172
},
81
173
},
0 commit comments