-
Notifications
You must be signed in to change notification settings - Fork 84
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
Add option to filter certificates by tag before adding it to LB #658
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package aws | |
|
||
import ( | ||
"crypto/x509" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/acm" | ||
|
@@ -10,16 +11,17 @@ import ( | |
) | ||
|
||
type acmCertificateProvider struct { | ||
api acmiface.ACMAPI | ||
api acmiface.ACMAPI | ||
filterTag string | ||
} | ||
|
||
func newACMCertProvider(api acmiface.ACMAPI) certs.CertificatesProvider { | ||
return &acmCertificateProvider{api: api} | ||
func newACMCertProvider(api acmiface.ACMAPI, certFilterTag string) certs.CertificatesProvider { | ||
return &acmCertificateProvider{api: api, filterTag: certFilterTag} | ||
} | ||
|
||
// GetCertificates returns a list of AWS ACM certificates | ||
func (p *acmCertificateProvider) GetCertificates() ([]*certs.CertificateSummary, error) { | ||
acmSummaries, err := getACMCertificateSummaries(p.api) | ||
acmSummaries, err := getACMCertificateSummaries(p.api, p.filterTag) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
@@ -34,20 +36,47 @@ func (p *acmCertificateProvider) GetCertificates() ([]*certs.CertificateSummary, | |
return result, nil | ||
} | ||
|
||
func getACMCertificateSummaries(api acmiface.ACMAPI) ([]*acm.CertificateSummary, error) { | ||
func getACMCertificateSummaries(api acmiface.ACMAPI, filterTag string) ([]*acm.CertificateSummary, error) { | ||
params := &acm.ListCertificatesInput{ | ||
CertificateStatuses: []*string{ | ||
aws.String(acm.CertificateStatusIssued), | ||
}, | ||
} | ||
acmSummaries := make([]*acm.CertificateSummary, 0) | ||
|
||
err := api.ListCertificatesPages(params, func(page *acm.ListCertificatesOutput, lastPage bool) bool { | ||
acmSummaries = append(acmSummaries, page.CertificateSummaryList...) | ||
return true | ||
}) | ||
|
||
if tag := strings.Split(filterTag, "="); filterTag != "=" && len(tag) == 2 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor: I think for readability it's better to create two if's with the same return here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see how There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah https://go.dev/play/p/oGRkFVn_zu9 now I got it :D |
||
return filterCertificatesByTag(api, acmSummaries, tag[0], tag[1]) | ||
} | ||
|
||
return acmSummaries, err | ||
} | ||
|
||
func filterCertificatesByTag(api acmiface.ACMAPI, allSummaries []*acm.CertificateSummary, key, value string) ([]*acm.CertificateSummary, error) { | ||
prodSummaries := make([]*acm.CertificateSummary, 0) | ||
RomanZavodskikh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for _, summary := range allSummaries { | ||
in := &acm.ListTagsForCertificateInput{ | ||
CertificateArn: summary.CertificateArn, | ||
} | ||
out, err := api.ListTagsForCertificate(in) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, tag := range out.Tags { | ||
if *tag.Key == key && *tag.Value == value { | ||
prodSummaries = append(prodSummaries, summary) | ||
} | ||
} | ||
} | ||
|
||
return prodSummaries, nil | ||
} | ||
|
||
func getCertificateSummaryFromACM(api acmiface.ACMAPI, arn *string) (*certs.CertificateSummary, error) { | ||
params := &acm.GetCertificateInput{CertificateArn: arn} | ||
resp, err := api.GetCertificate(params) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,16 +15,18 @@ type acmExpect struct { | |
DomainNames []string | ||
Chain int | ||
Error error | ||
EmptyList bool | ||
} | ||
|
||
func TestACM(t *testing.T) { | ||
cert := mustRead("acm.txt") | ||
chain := mustRead("chain.txt") | ||
|
||
for _, ti := range []struct { | ||
msg string | ||
api acmiface.ACMAPI | ||
expect acmExpect | ||
msg string | ||
api acmiface.ACMAPI | ||
filterTag string | ||
expect acmExpect | ||
}{ | ||
{ | ||
msg: "Found ACM Cert foobar and a chain", | ||
|
@@ -69,9 +71,64 @@ func TestACM(t *testing.T) { | |
Error: nil, | ||
}, | ||
}, | ||
{ | ||
msg: "Found ACM Cert with correct filter tag", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you like to add tests with several certificates in the list. |
||
api: fake.NewACMClientWithTags( | ||
acm.ListCertificatesOutput{ | ||
CertificateSummaryList: []*acm.CertificateSummary{ | ||
{ | ||
CertificateArn: aws.String("foobar"), | ||
DomainName: aws.String("foobar.de"), | ||
}, | ||
}, | ||
}, | ||
acm.GetCertificateOutput{ | ||
Certificate: aws.String(cert), | ||
}, | ||
map[string]*acm.ListTagsForCertificateOutput{ | ||
"foobar": { | ||
Tags: []*acm.Tag{{Key: aws.String("production"), Value: aws.String("true")}}, | ||
}, | ||
}, | ||
), | ||
filterTag: "production=true", | ||
expect: acmExpect{ | ||
ARN: "foobar", | ||
DomainNames: []string{"foobar.de"}, | ||
Error: nil, | ||
}, | ||
}, | ||
{ | ||
msg: "ACM Cert with incorrect filter tag should not be found", | ||
api: fake.NewACMClientWithTags( | ||
acm.ListCertificatesOutput{ | ||
CertificateSummaryList: []*acm.CertificateSummary{ | ||
{ | ||
CertificateArn: aws.String("foobar"), | ||
DomainName: aws.String("foobar.de"), | ||
}, | ||
}, | ||
}, | ||
acm.GetCertificateOutput{ | ||
Certificate: aws.String(cert), | ||
}, | ||
map[string]*acm.ListTagsForCertificateOutput{ | ||
"foobar": { | ||
Tags: []*acm.Tag{{Key: aws.String("production"), Value: aws.String("false")}}, | ||
}, | ||
}, | ||
), | ||
filterTag: "production=true", | ||
expect: acmExpect{ | ||
EmptyList: true, | ||
ARN: "foobar", | ||
DomainNames: []string{"foobar.de"}, | ||
Error: nil, | ||
}, | ||
}, | ||
} { | ||
t.Run(ti.msg, func(t *testing.T) { | ||
provider := newACMCertProvider(ti.api) | ||
provider := newACMCertProvider(ti.api, ti.filterTag) | ||
list, err := provider.GetCertificates() | ||
|
||
if ti.expect.Error != nil { | ||
|
@@ -80,11 +137,16 @@ func TestACM(t *testing.T) { | |
require.NoError(t, err) | ||
} | ||
|
||
require.Equal(t, 1, len(list)) | ||
if ti.expect.EmptyList { | ||
require.Equal(t, 0, len(list)) | ||
|
||
cert := list[0] | ||
require.Equal(t, ti.expect.ARN, cert.ID()) | ||
require.Equal(t, ti.expect.DomainNames, cert.DomainNames()) | ||
} else { | ||
require.Equal(t, 1, len(list)) | ||
|
||
cert := list[0] | ||
require.Equal(t, ti.expect.ARN, cert.ID()) | ||
require.Equal(t, ti.expect.DomainNames, cert.DomainNames()) | ||
} | ||
}) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe we should add a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could but I believe this case is covered in the if in line 52, because if
filterTag == ""
thenlen(tag) != 2
, and in this case we will just returnacmSummaries
in line 56, which is just after the if.