From bfae0f23b60100631fad6e885052390b4e5077ff Mon Sep 17 00:00:00 2001 From: Caio Almeida Date: Mon, 16 Sep 2024 12:55:45 -0300 Subject: [PATCH] fix: generating short name for OAC to avoid bad requests from AWS API --- go.mod | 2 +- go.sum | 4 +-- internal/cloudfront/oac.go | 20 +++++++++++++- internal/cloudfront/oac_test.go | 48 +++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 internal/cloudfront/oac_test.go diff --git a/go.mod b/go.mod index 54ce8bf..d76dead 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/aws/aws-sdk-go v1.44.271 github.com/creasty/defaults v1.7.0 github.com/go-logr/logr v1.2.4 + github.com/google/uuid v1.6.0 github.com/hashicorp/go-multierror v1.1.1 github.com/joho/godotenv v1.5.1 github.com/spf13/viper v1.15.0 @@ -37,7 +38,6 @@ require ( github.com/google/gnostic v0.5.7-v3refs // indirect github.com/google/go-cmp v0.5.9 // indirect github.com/google/gofuzz v1.1.0 // indirect - github.com/google/uuid v1.3.0 // indirect github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/imdario/mergo v0.3.12 // indirect diff --git a/go.sum b/go.sum index b57b5b7..9e39756 100644 --- a/go.sum +++ b/go.sum @@ -159,8 +159,8 @@ github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLe github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 h1:K6RDEckDVWvDI9JAJYCmNdQXq6neHJOYx3V6jnqNEec= github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg= github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk= github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g= diff --git a/internal/cloudfront/oac.go b/internal/cloudfront/oac.go index f696e4c..22fb97a 100644 --- a/internal/cloudfront/oac.go +++ b/internal/cloudfront/oac.go @@ -24,6 +24,11 @@ import ( "strings" awscloudfront "github.com/aws/aws-sdk-go/service/cloudfront" + "github.com/google/uuid" +) + +const ( + oacNameCharLimit = 63 ) type OAC struct { @@ -48,10 +53,23 @@ func NewOAC(distribution, originName string) OAC { } func oacName(distributionName, s3Host string) string { + // keeps the default behavior for already working AOC's. s3Name := strings.Split(s3Host, ".")[0] - return fmt.Sprintf("%s-%s", distributionName, s3Name) + defaultOACName := fmt.Sprintf("%s-%s", distributionName, s3Name) + if len(defaultOACName) <= oacNameCharLimit { + return defaultOACName + } + + // generates a short name to avoid AWS limits. + hostName := strings.Split(distributionName, ".")[0] + return fmt.Sprintf("%s-%s", hostName, generateShortID()) } func oacDescription(originName string) string { return fmt.Sprintf("OAC for %s, managed by cdn-origin-controller", originName) } + +func generateShortID() string { + id := uuid.New().String() + return strings.Split(id, "-")[0] +} diff --git a/internal/cloudfront/oac_test.go b/internal/cloudfront/oac_test.go new file mode 100644 index 0000000..6f27ed3 --- /dev/null +++ b/internal/cloudfront/oac_test.go @@ -0,0 +1,48 @@ +// Copyright (c) 2021 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 cloudfront + +import ( + "strings" + "testing" + + "github.com/stretchr/testify/suite" +) + +func TestRunOACTestSuite(t *testing.T) { + t.Parallel() + suite.Run(t, &OACTestSuite{}) +} + +type OACTestSuite struct { + suite.Suite +} + +func (s *OACTestSuite) TestNewOACWithDefaultNamePattern() { + oac := NewOAC("asylium.gympass.com", "gympass-production-nv-wellz-accounts.s3.us-east-1.amazonaws.com") + s.Equal("asylium.gympass.com-gympass-production-nv-wellz-accounts", oac.Name) + s.True(len(oac.Name) <= 63) +} + +func (s *OACTestSuite) TestNewOACWithShortNamePattern() { + oac := NewOAC("wellz-accounts-fe-develop-nv.nv.dev.us.gympass.cloud", "gympass-develop-nv-wellz-accounts-fe-develop-nv.s3.us-east-1.amazonaws.com") + s.True(strings.HasPrefix(oac.Name, "wellz-accounts-fe-develop-nv-")) + s.True(len(oac.Name) <= 63) +}