Skip to content

Commit

Permalink
Fix: nil secret in resourceversiontable (#2982)
Browse files Browse the repository at this point in the history
* fix nil secret in resourceversiontable

Signed-off-by: huabing zhao <[email protected]>

* check secrets in the xds result

Signed-off-by: huabing zhao <[email protected]>

---------

Signed-off-by: huabing zhao <[email protected]>
  • Loading branch information
zhaohuabing authored Mar 25, 2024
1 parent e58bb22 commit e880439
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
16 changes: 11 additions & 5 deletions internal/xds/translator/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,14 @@ func processTLSSocket(tlsConfig *ir.TLSUpstreamConfig, tCtx *types.ResourceVersi
if tlsConfig == nil {
return nil, nil
}
CaSecret := buildXdsUpstreamTLSCASecret(tlsConfig)
if CaSecret != nil {
// Create a secret for the CA certificate only if it's not using the system trust store
if !tlsConfig.UseSystemTrustStore {
CaSecret := buildXdsUpstreamTLSCASecret(tlsConfig)
if err := tCtx.AddXdsResource(resourcev3.SecretType, CaSecret); err != nil {
return nil, err
}
}

// for upstreamTLS , a fixed sni can be used. use auto_sni otherwise
// https://www.envoyproxy.io/docs/envoy/latest/faq/configuration/sni#faq-how-to-setup-sni:~:text=For%20clusters%2C%20a,for%20trust%20anchor.
tlsSocket, err := buildXdsUpstreamTLSSocketWthCert(tlsConfig)
Expand Down Expand Up @@ -574,9 +576,12 @@ func addXdsCluster(tCtx *types.ResourceVersionTable, args *xdsClusterArgs) error
xdsEndpoints := buildXdsClusterLoadAssignment(args.name, args.settings)
for _, ds := range args.settings {
if ds.TLS != nil {
secret := buildXdsUpstreamTLSCASecret(ds.TLS)
if err := tCtx.AddXdsResource(resourcev3.SecretType, secret); err != nil {
return err
// Create a secret for the CA certificate only if it's not using the system trust store
if !ds.TLS.UseSystemTrustStore {
secret := buildXdsUpstreamTLSCASecret(ds.TLS)
if err := tCtx.AddXdsResource(resourcev3.SecretType, secret); err != nil {
return err
}
}
}
}
Expand All @@ -602,6 +607,7 @@ const (

func buildXdsUpstreamTLSCASecret(tlsConfig *ir.TLSUpstreamConfig) *tlsv3.Secret {
// Build the tls secret
// It's just a sanity check, we shouldn't call this function if the system trust store is used
if tlsConfig.UseSystemTrustStore {
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion internal/xds/translator/translator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func TestTranslateXds(t *testing.T) {
name: "http-route-dns-cluster",
},
{
name: "http-route-with-tls-system-truststore",
name: "http-route-with-tls-system-truststore",
requireSecrets: true,
},
{
name: "http-route-with-tlsbundle",
Expand Down
5 changes: 5 additions & 0 deletions internal/xds/types/resourceversiontable.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func (t *ResourceVersionTable) GetXdsResources() XdsResources {
}

func (t *ResourceVersionTable) AddXdsResource(rType resourcev3.Type, xdsResource types.Resource) error {
// It's a sanity check to make sure the xdsResource is not nil
if xdsResource == nil {
return fmt.Errorf("xds resource is nil")
}

// Perform type switch to handle different types of xdsResource
switch rType {
case resourcev3.ListenerType:
Expand Down

0 comments on commit e880439

Please sign in to comment.