Skip to content

Commit

Permalink
Handle empty cluster name in sslcert namer
Browse files Browse the repository at this point in the history
  • Loading branch information
prameshj committed Jun 12, 2018
1 parent 96bd52f commit a248f76
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/utils/namer.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func truncate(key string) string {

func (n *Namer) decorateName(name string) string {
clusterName := n.UID()
// clusterName might be empty for legacy clusters
if clusterName == "" {
return name
}
Expand Down Expand Up @@ -342,7 +343,7 @@ func (n *Namer) IsLegacySSLCert(lbName string, resourceName string) bool {
func (n *Namer) SSLCertName(lbName string, secretHash string) string {
lbNameHash := n.lbNameToHash(lbName)
// k8s-ssl-[lbNameHash]-[certhash]--[clusterUID]
return truncate(fmt.Sprintf("%s-%s-%s-%s%s%s", n.prefix, sslCertPrefix, lbNameHash, secretHash, clusterNameDelimiter, n.UID()))
return n.decorateName(fmt.Sprintf("%s-%s-%s-%s", n.prefix, sslCertPrefix, lbNameHash, secretHash))
}

// ForwardingRule returns the name of the forwarding rule prefix.
Expand Down
12 changes: 12 additions & 0 deletions pkg/utils/namer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package utils
import (
"crypto/sha256"
"fmt"
"strings"
"testing"
)

Expand Down Expand Up @@ -336,6 +337,17 @@ func TestNamerLoadBalancer(t *testing.T) {
}
}

// Ensure that a valid cert name is created if clusterName is empty.
func TestNamerSSLCertName(t *testing.T) {
secretHash := fmt.Sprintf("%x", sha256.Sum256([]byte("test123")))[:16]
namer := NewNamerWithPrefix("k8s", "", "fw1")
lbName := namer.LoadBalancer("key1")
certName := namer.SSLCertName(lbName, secretHash)
if strings.HasSuffix(certName, clusterNameDelimiter) {
t.Errorf("Invalid Cert name %s ending with %s", certName, clusterNameDelimiter)
}
}

func TestNamerIsCertUsedForLB(t *testing.T) {
cases := map[string]struct {
prefix string
Expand Down

0 comments on commit a248f76

Please sign in to comment.