Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix oac short name #121

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading