-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from gympass/PE1-1796/cert-auto-discovery
[PE1-1796] feat: discover ACM certificates automatically
- Loading branch information
Showing
17 changed files
with
386 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) 2023 GPBR Participacoes LTDA. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
// this software and associated documentation files (the "Software"), to deal in | ||
// the Software without restriction, including without limitation the rights to | ||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
// the Software, and to permit persons to whom the Software is furnished to do so, | ||
// subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
package certificate | ||
|
||
// New creates a Certificate | ||
func New(arn, domainName string, alternativeNames []string /*, renewalEligibility string*/) Certificate { | ||
return Certificate{ | ||
arn: arn, | ||
domainName: domainName, | ||
alternativeNames: alternativeNames, | ||
} | ||
} | ||
|
||
// Certificate represents a basic certificate | ||
type Certificate struct { | ||
arn string | ||
domainName string | ||
alternativeNames []string | ||
} | ||
|
||
// DomainName returns the main certificate domain name | ||
func (c Certificate) DomainName() string { | ||
return c.domainName | ||
} | ||
|
||
// AlternativeNames returns a list of certificate subject alternative names | ||
func (c Certificate) AlternativeNames() []string { | ||
return c.alternativeNames | ||
} | ||
|
||
// ARN returns the certificate identifier | ||
func (c Certificate) ARN() string { | ||
return c.arn | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright (c) 2023 GPBR Participacoes LTDA. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
// this software and associated documentation files (the "Software"), to deal in | ||
// the Software without restriction, including without limitation the rights to | ||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
// the Software, and to permit persons to whom the Software is furnished to do so, | ||
// subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
package certificate | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/acm" | ||
"github.com/aws/aws-sdk-go/service/acm/acmiface" | ||
) | ||
|
||
var ( | ||
errFindCert = errors.New("finding certificate") | ||
) | ||
|
||
// CertFilter type represents a certificate filter interface | ||
type CertFilter func(c Certificate) bool | ||
|
||
// Repository provides methods for manipulating Custom domain names on AWS | ||
type Repository interface { | ||
FindByFilter(CertFilter) ([]Certificate, error) | ||
} | ||
|
||
type acmCertRepository struct { | ||
client acmiface.ACMAPI | ||
} | ||
|
||
// NewRepository creates a new Repository | ||
func NewRepository(c acmiface.ACMAPI) Repository { | ||
return acmCertRepository{client: c} | ||
} | ||
|
||
// FindByFilter find a certificate given a filter | ||
func (r acmCertRepository) FindByFilter(filter CertFilter) ([]Certificate, error) { | ||
|
||
input := &acm.ListCertificatesInput{ | ||
CertificateStatuses: aws.StringSlice([]string{acm.CertificateStatusIssued}), | ||
} | ||
|
||
var certs []Certificate | ||
var certDiscoveryErr error | ||
|
||
err := r.client.ListCertificatesPages(input, func(output *acm.ListCertificatesOutput, _ bool) bool { | ||
for _, acmCertSummary := range output.CertificateSummaryList { | ||
acmCert, err := r.client.DescribeCertificate(&acm.DescribeCertificateInput{ | ||
CertificateArn: acmCertSummary.CertificateArn, | ||
}) | ||
|
||
if err != nil { | ||
certDiscoveryErr = fmt.Errorf("describing certificate (ARN: %s): %v", *acmCertSummary.CertificateArn, err) | ||
return false | ||
} | ||
|
||
certDetails := acmCert.Certificate | ||
dnCert := New(*certDetails.CertificateArn, | ||
*certDetails.DomainName, | ||
aws.StringValueSlice(acmCert.Certificate.SubjectAlternativeNames), | ||
) | ||
if filter(dnCert) { | ||
certs = append(certs, dnCert) | ||
} | ||
} | ||
return true | ||
}) | ||
|
||
if certDiscoveryErr != nil { | ||
err = certDiscoveryErr | ||
} | ||
if err != nil { | ||
return []Certificate{}, fmt.Errorf("%w: %v", errFindCert, err) | ||
} | ||
|
||
return certs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright (c) 2023 GPBR Participacoes LTDA. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
// this software and associated documentation files (the "Software"), to deal in | ||
// the Software without restriction, including without limitation the rights to | ||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
// the Software, and to permit persons to whom the Software is furnished to do so, | ||
// subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
package certificate | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
var ( | ||
// ErrNoMatchingCert any matching certificate error | ||
ErrNoMatchingCert = errors.New("could not find any matching certificate") | ||
) | ||
|
||
// Service handle the certificate actions as discovery | ||
type Service interface { | ||
DiscoverByHost(string) (Certificate, error) | ||
} | ||
|
||
// NewService creates a new Certificate Service | ||
func NewService(c Repository) Service { | ||
return acmCertService{repo: c} | ||
} | ||
|
||
type acmCertService struct { | ||
repo Repository | ||
} | ||
|
||
// DiscoverByHost tries to discover a certificate given a host | ||
func (a acmCertService) DiscoverByHost(host string) (Certificate, error) { | ||
|
||
certs, err := a.repo.FindByFilter(matchingDomainFilter(host)) | ||
|
||
if err != nil { | ||
return Certificate{}, fmt.Errorf("discovery certificate: %v", err) | ||
} | ||
|
||
if len(certs) == 0 { | ||
return Certificate{}, ErrNoMatchingCert | ||
} | ||
|
||
return certs[0], nil | ||
} | ||
|
||
func matchingDomainFilter(host string) CertFilter { | ||
return func(c Certificate) bool { | ||
if host == c.DomainName() { | ||
return true | ||
} | ||
|
||
for _, alterName := range c.AlternativeNames() { | ||
hs := strings.Split(host, ".") | ||
hostDomain := strings.Join(hs[1:], ".") | ||
|
||
if strings.HasPrefix(alterName, "*.") { | ||
alterName = strings.ReplaceAll(alterName, "*.", "") | ||
} | ||
|
||
if alterName == hostDomain { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) 2023 GPBR Participacoes LTDA. | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
// this software and associated documentation files (the "Software"), to deal in | ||
// the Software without restriction, including without limitation the rights to | ||
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
// the Software, and to permit persons to whom the Software is furnished to do so, | ||
// subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
package certificate | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
func TestRunCertificateServiceSuite(t *testing.T) { | ||
t.Parallel() | ||
suite.Run(t, &CertificateServiceTestSuite{}) | ||
} | ||
|
||
type CertificateServiceTestSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func (s *CertificateServiceTestSuite) TestMatchDomainFilter_MainDomain() { | ||
cert := New( | ||
"arn:xpto", | ||
"foo.xpto.com", | ||
[]string{"foo.xpto.com", "*.foo.xpto.com"}, | ||
) | ||
|
||
filter := matchingDomainFilter("foo.xpto.com") | ||
s.True(filter(cert)) | ||
} | ||
|
||
func (s *CertificateServiceTestSuite) TestMatchDomainFilter_SubDomain() { | ||
cert := New( | ||
"arn:xpto", | ||
"foo.xpto.com", | ||
[]string{"foo.xpto.com", "*.foo.xpto.com"}, | ||
) | ||
|
||
filter := matchingDomainFilter("baz.foo.xpto.com") | ||
s.True(filter(cert)) | ||
} |
Oops, something went wrong.