Skip to content

Commit

Permalink
Merge pull request #121 from gympass/fix-oac-short-name
Browse files Browse the repository at this point in the history
Fix oac short name
  • Loading branch information
caiofralmeida authored Sep 17, 2024
2 parents bc93bda + 97cf1ad commit 756cc13
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ 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 @@ -38,6 +37,7 @@ 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.6.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
15 changes: 10 additions & 5 deletions internal/cloudfront/oac.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
package cloudfront

import (
"crypto/md5"
"fmt"
"strings"

awscloudfront "github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/google/uuid"
)

const (
Expand Down Expand Up @@ -62,14 +62,19 @@ func oacName(distributionName, s3Host string) string {

// generates a short name to avoid AWS limits.
hostName := strings.Split(distributionName, ".")[0]
return fmt.Sprintf("%s-%s", hostName, generateShortID())
return fmt.Sprintf("%s-%s", hostName, generateShortID(distributionName, s3Name))
}

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]
func generateShortID(distrName, domainName string) string {
md5 := generateMD5(fmt.Sprintf("%s-%s", distrName, domainName))
return md5[:8]
}

func generateMD5(text string) string {
data := []byte(text)
return fmt.Sprintf("%x", md5.Sum(data))
}
3 changes: 1 addition & 2 deletions internal/cloudfront/oac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package cloudfront

import (
"strings"
"testing"

"github.com/stretchr/testify/suite"
Expand All @@ -43,6 +42,6 @@ func (s *OACTestSuite) TestNewOACWithDefaultNamePattern() {

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.Equal("wellz-accounts-fe-develop-nv-51d6956b", oac.Name)
s.True(len(oac.Name) <= 63)
}

0 comments on commit 756cc13

Please sign in to comment.