Skip to content

fix: new ca-filter causing expontentially more api-calls #3608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions pkg/ingress/cert_discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ingress

import (
"context"
"slices"
"strings"
"sync"
"time"
Expand All @@ -16,6 +15,7 @@ import (
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/cache"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/utils/strings/slices"
"sigs.k8s.io/aws-load-balancer-controller/pkg/aws/services"
)

Expand Down Expand Up @@ -153,18 +153,18 @@ func (d *acmCertDiscovery) loadDomainsForCertificate(ctx context.Context, certAR
certDetail := resp.Certificate

// check if cert is issued from an allowed CA
// otherwise empty-out the list of domains
domains := sets.String{}
if len(d.allowedCAARNs) == 0 || slices.Contains(d.allowedCAARNs, awssdk.StringValue(certDetail.CertificateAuthorityArn)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just for my understanding, the originally PR didn't introduce any regression right?
And you faced this issue due to you used the new "allowedCAARNs" feature?
If so, this fix looks good to me. However, i'd like to change this code to be like

domains := sets.NewString(aws.StringValueSlice(certDetail.SubjectAlternativeNames)...)
switch aws.StringValue(certDetail.Type) {
case acm.CertificateTypeImported:
	d.certDomainsCache.Set(certARN, domains, d.importedCertDomainsCacheTTL)
case acm.CertificateTypeAmazonIssued, acm.CertificateTypePrivate:
	d.certDomainsCache.Set(certARN, domains, d.privateCertDomainsCacheTTL)
}
if len(d.allowedCAARNs) == 0 || slices.Contains(d.allowedCAARNs, awssdk.StringValue(certDetail.CertificateAuthorityArn)) {
   return domains, nil
}
return sets.String{}, nil

technically there is no functional difference since allowedCAARNs is a controller-level flag which is immutable given the controller's lifetime. However, from coding perspective, the cache shall be for the "domains" before the "CA filter logic" and this make the code more robust(e.g. works even allowedCAARNs can be dynamically updated somehow).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our PR introduce this behavior only if both conditions are met:

  1. use the --allowed-certificate-authority-arns=... parameter
  2. there exists certificates inside AWS ACM which are not issued from a authority in the allowed list.

I/we can test your proposal tomorrow in our staging environment. But I assume that your proposed code change does not work properly as we then also cache the domain(s) of a certificate which is not issued from an allowed CA.

And the function first tries to load the domains from the cache before it reaches this filtering code block:

func (d *acmCertDiscovery) loadDomainsForCertificate(ctx context.Context, certARN string) (sets.String, error) {
	if rawCacheItem, ok := d.certDomainsCache.Get(certARN); ok {
		return rawCacheItem.(sets.String), nil
	}
	// only continues in case we didn't find it inside the cache

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkilchhofer
You are right, the cache shall be the domains after the filter.
ideally we should refactor the code such that the cache stores certificate details from AWS. (but for now it only caches domains and your filter logic is on cert CA), thus filter logic has to be run before the cache.

I'll approve this, and maybe refactor this in the future if we ever need to make the ca list mutable.

domains := sets.NewString(aws.StringValueSlice(certDetail.SubjectAlternativeNames)...)
switch aws.StringValue(certDetail.Type) {
case acm.CertificateTypeImported:
d.certDomainsCache.Set(certARN, domains, d.importedCertDomainsCacheTTL)
case acm.CertificateTypeAmazonIssued, acm.CertificateTypePrivate:
d.certDomainsCache.Set(certARN, domains, d.privateCertDomainsCacheTTL)
}
return domains, nil
domains = sets.NewString(aws.StringValueSlice(certDetail.SubjectAlternativeNames)...)
}
return sets.String{}, nil

switch aws.StringValue(certDetail.Type) {
case acm.CertificateTypeImported:
d.certDomainsCache.Set(certARN, domains, d.importedCertDomainsCacheTTL)
case acm.CertificateTypeAmazonIssued, acm.CertificateTypePrivate:
d.certDomainsCache.Set(certARN, domains, d.privateCertDomainsCacheTTL)
}
return domains, nil
}
Copy link

@mkilchhofer mkilchhofer Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Real resp. resulting diff for this feature (#3565) can be viewed here:
v2.7.1...the-technat:aws-load-balancer-controller:main

Or on this screenshot:

image


func (d *acmCertDiscovery) domainMatchesHost(domainName string, tlsHost string) bool {
Expand Down