Skip to content

Commit 756cc13

Browse files
Merge pull request #121 from gympass/fix-oac-short-name
Fix oac short name
2 parents bc93bda + 97cf1ad commit 756cc13

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/aws/aws-sdk-go v1.44.271
77
github.com/creasty/defaults v1.7.0
88
github.com/go-logr/logr v1.2.4
9-
github.com/google/uuid v1.6.0
109
github.com/hashicorp/go-multierror v1.1.1
1110
github.com/joho/godotenv v1.5.1
1211
github.com/spf13/viper v1.15.0
@@ -38,6 +37,7 @@ require (
3837
github.com/google/gnostic v0.5.7-v3refs // indirect
3938
github.com/google/go-cmp v0.5.9 // indirect
4039
github.com/google/gofuzz v1.1.0 // indirect
40+
github.com/google/uuid v1.6.0 // indirect
4141
github.com/hashicorp/errwrap v1.0.0 // indirect
4242
github.com/hashicorp/hcl v1.0.0 // indirect
4343
github.com/imdario/mergo v0.3.12 // indirect

internal/cloudfront/oac.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
package cloudfront
2121

2222
import (
23+
"crypto/md5"
2324
"fmt"
2425
"strings"
2526

2627
awscloudfront "github.com/aws/aws-sdk-go/service/cloudfront"
27-
"github.com/google/uuid"
2828
)
2929

3030
const (
@@ -62,14 +62,19 @@ func oacName(distributionName, s3Host string) string {
6262

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

6868
func oacDescription(originName string) string {
6969
return fmt.Sprintf("OAC for %s, managed by cdn-origin-controller", originName)
7070
}
7171

72-
func generateShortID() string {
73-
id := uuid.New().String()
74-
return strings.Split(id, "-")[0]
72+
func generateShortID(distrName, domainName string) string {
73+
md5 := generateMD5(fmt.Sprintf("%s-%s", distrName, domainName))
74+
return md5[:8]
75+
}
76+
77+
func generateMD5(text string) string {
78+
data := []byte(text)
79+
return fmt.Sprintf("%x", md5.Sum(data))
7580
}

internal/cloudfront/oac_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package cloudfront
2121

2222
import (
23-
"strings"
2423
"testing"
2524

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

4443
func (s *OACTestSuite) TestNewOACWithShortNamePattern() {
4544
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")
46-
s.True(strings.HasPrefix(oac.Name, "wellz-accounts-fe-develop-nv-"))
45+
s.Equal("wellz-accounts-fe-develop-nv-51d6956b", oac.Name)
4746
s.True(len(oac.Name) <= 63)
4847
}

0 commit comments

Comments
 (0)