Skip to content

Commit

Permalink
Merge pull request #119 from gympass/trunc-str
Browse files Browse the repository at this point in the history
fix: generating short name for OAC to avoid bad requests from AWS API
  • Loading branch information
caiofralmeida authored Sep 16, 2024
2 parents 06132b8 + bfae0f2 commit 056138f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
20 changes: 19 additions & 1 deletion internal/cloudfront/oac.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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]
}
48 changes: 48 additions & 0 deletions internal/cloudfront/oac_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 056138f

Please sign in to comment.