Skip to content

Commit 31aa1a5

Browse files
authored
xds: add env var protection for client-side security (#4247) (#4296)
1 parent b96b7d4 commit 31aa1a5

File tree

3 files changed

+82
-6
lines changed

3 files changed

+82
-6
lines changed

xds/internal/client/cds_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,65 @@ func (s) TestValidateCluster_Success(t *testing.T) {
223223
}
224224
}
225225

226+
func (s) TestValidateClusterWithSecurityConfig_EnvVarOff(t *testing.T) {
227+
// Turn off the env var protection for client-side security.
228+
origClientSideSecurityEnvVar := env.ClientSideSecuritySupport
229+
env.ClientSideSecuritySupport = false
230+
defer func() { env.ClientSideSecuritySupport = origClientSideSecurityEnvVar }()
231+
232+
cluster := &v3clusterpb.Cluster{
233+
ClusterDiscoveryType: &v3clusterpb.Cluster_Type{Type: v3clusterpb.Cluster_EDS},
234+
EdsClusterConfig: &v3clusterpb.Cluster_EdsClusterConfig{
235+
EdsConfig: &v3corepb.ConfigSource{
236+
ConfigSourceSpecifier: &v3corepb.ConfigSource_Ads{
237+
Ads: &v3corepb.AggregatedConfigSource{},
238+
},
239+
},
240+
ServiceName: serviceName,
241+
},
242+
LbPolicy: v3clusterpb.Cluster_ROUND_ROBIN,
243+
TransportSocket: &v3corepb.TransportSocket{
244+
Name: "envoy.transport_sockets.tls",
245+
ConfigType: &v3corepb.TransportSocket_TypedConfig{
246+
TypedConfig: &anypb.Any{
247+
TypeUrl: version.V3UpstreamTLSContextURL,
248+
Value: func() []byte {
249+
tls := &v3tlspb.UpstreamTlsContext{
250+
CommonTlsContext: &v3tlspb.CommonTlsContext{
251+
ValidationContextType: &v3tlspb.CommonTlsContext_ValidationContextCertificateProviderInstance{
252+
ValidationContextCertificateProviderInstance: &v3tlspb.CommonTlsContext_CertificateProviderInstance{
253+
InstanceName: "rootInstance",
254+
CertificateName: "rootCert",
255+
},
256+
},
257+
},
258+
}
259+
mtls, _ := proto.Marshal(tls)
260+
return mtls
261+
}(),
262+
},
263+
},
264+
},
265+
}
266+
wantUpdate := ClusterUpdate{
267+
ServiceName: serviceName,
268+
EnableLRS: false,
269+
}
270+
gotUpdate, err := validateCluster(cluster)
271+
if err != nil {
272+
t.Errorf("validateCluster() failed: %v", err)
273+
}
274+
if diff := cmp.Diff(wantUpdate, gotUpdate); diff != "" {
275+
t.Errorf("validateCluster() returned unexpected diff (-want, got):\n%s", diff)
276+
}
277+
}
278+
226279
func (s) TestValidateClusterWithSecurityConfig(t *testing.T) {
280+
// Turn on the env var protection for client-side security.
281+
origClientSideSecurityEnvVar := env.ClientSideSecuritySupport
282+
env.ClientSideSecuritySupport = true
283+
defer func() { env.ClientSideSecuritySupport = origClientSideSecurityEnvVar }()
284+
227285
const (
228286
identityPluginInstance = "identityPluginInstance"
229287
identityCertName = "identityCert"

xds/internal/client/xds.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,16 @@ func validateCluster(cluster *v3clusterpb.Cluster) (ClusterUpdate, error) {
410410
return emptyUpdate, fmt.Errorf("xds: unexpected lbPolicy %v in response: %+v", cluster.GetLbPolicy(), cluster)
411411
}
412412

413-
sc, err := securityConfigFromCluster(cluster)
414-
if err != nil {
415-
return emptyUpdate, err
413+
// Process security configuration received from the control plane iff the
414+
// corresponding environment variable is set.
415+
var sc *SecurityConfig
416+
if env.ClientSideSecuritySupport {
417+
var err error
418+
if sc, err = securityConfigFromCluster(cluster); err != nil {
419+
return emptyUpdate, err
420+
}
416421
}
422+
417423
return ClusterUpdate{
418424
ServiceName: cluster.GetEdsClusterConfig().GetServiceName(),
419425
EnableLRS: cluster.GetLrsServer().GetSelf() != nil,

xds/internal/env/env.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ const (
3737
// and kept in variable BootstrapFileName.
3838
//
3939
// When both bootstrap FileName and FileContent are set, FileName is used.
40-
BootstrapFileContentEnv = "GRPC_XDS_BOOTSTRAP_CONFIG"
41-
circuitBreakingSupportEnv = "GRPC_XDS_EXPERIMENTAL_CIRCUIT_BREAKING"
42-
timeoutSupportEnv = "GRPC_XDS_EXPERIMENTAL_ENABLE_TIMEOUT"
40+
BootstrapFileContentEnv = "GRPC_XDS_BOOTSTRAP_CONFIG"
41+
circuitBreakingSupportEnv = "GRPC_XDS_EXPERIMENTAL_CIRCUIT_BREAKING"
42+
timeoutSupportEnv = "GRPC_XDS_EXPERIMENTAL_ENABLE_TIMEOUT"
43+
faultInjectionSupportEnv = "GRPC_XDS_EXPERIMENTAL_FAULT_INJECTION"
44+
clientSideSecuritySupportEnv = "GRPC_XDS_EXPERIMENTAL_SECURITY_SUPPORT"
4345
)
4446

4547
var (
@@ -63,4 +65,14 @@ var (
6365
// route actions is enabled. This can be enabled by setting the
6466
// environment variable "GRPC_XDS_EXPERIMENTAL_ENABLE_TIMEOUT" to "true".
6567
TimeoutSupport = strings.EqualFold(os.Getenv(timeoutSupportEnv), "true")
68+
// FaultInjectionSupport is used to control both fault injection and HTTP
69+
// filter support.
70+
FaultInjectionSupport = strings.EqualFold(os.Getenv(faultInjectionSupportEnv), "true")
71+
// ClientSideSecuritySupport is used to control processing of security
72+
// configuration on the client-side.
73+
//
74+
// Note that there is no env var protection for the server-side because we
75+
// have a brand new API on the server-side and users explicitly need to use
76+
// the new API to get security integration on the server.
77+
ClientSideSecuritySupport = strings.EqualFold(os.Getenv(clientSideSecuritySupportEnv), "true")
6678
)

0 commit comments

Comments
 (0)