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

spiffe: fix a data race in writing trust domain. #13343

Merged
merged 1 commit into from Apr 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 11 additions & 6 deletions pkg/spiffe/spiffe.go
Expand Up @@ -17,6 +17,7 @@ package spiffe
import (
"fmt"
"strings"
"sync"

"istio.io/istio/pkg/log"
)
Expand All @@ -31,15 +32,21 @@ const (
)

var (
trustDomain = defaultTrustDomain
trustDomain = defaultTrustDomain
trustDomainMutex sync.RWMutex
)

func SetTrustDomain(value string) {
// Replace special characters in spiffe
trustDomain = strings.Replace(value, "@", ".", -1)
v := strings.Replace(value, "@", ".", -1)
trustDomainMutex.Lock()
trustDomain = v
trustDomainMutex.Unlock()
}

func GetTrustDomain() string {
trustDomainMutex.RLock()
defer trustDomainMutex.RUnlock()
return trustDomain
}

Expand All @@ -60,7 +67,7 @@ func GenSpiffeURI(ns, serviceAccount string) (string, error) {
err = fmt.Errorf(
"namespace or service account empty for SPIFFEE uri ns=%v serviceAccount=%v", ns, serviceAccount)
}
return URIPrefix + trustDomain + "/ns/" + ns + "/sa/" + serviceAccount, err
return URIPrefix + GetTrustDomain() + "/ns/" + ns + "/sa/" + serviceAccount, err
}

// MustGenSpiffeURI returns the formatted uri(SPIFFEE format for now) for the certificate and logs if there was an error.
Expand All @@ -79,7 +86,5 @@ func GenCustomSpiffe(identity string) string {
return ""
}

// replace special character in spiffe
trustDomain = strings.Replace(trustDomain, "@", ".", -1)
return URIPrefix + trustDomain + "/" + identity
return URIPrefix + GetTrustDomain() + "/" + identity
}