From 1da2ae279cc8bd01cb402115d22940752eb5b939 Mon Sep 17 00:00:00 2001 From: Eric Chiang Date: Fri, 24 Feb 2017 12:55:04 -0800 Subject: [PATCH] storage/kubernetes: fix hash initialization bug --- storage/kubernetes/client.go | 7 ++++--- storage/kubernetes/client_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/storage/kubernetes/client.go b/storage/kubernetes/client.go index 9f5d0d3f..55e319d7 100644 --- a/storage/kubernetes/client.go +++ b/storage/kubernetes/client.go @@ -72,9 +72,10 @@ func idToName(s string, h func() hash.Hash) string { } func offlineTokenName(userID string, connID string, h func() hash.Hash) string { - h().Write([]byte(userID)) - h().Write([]byte(connID)) - return strings.TrimRight(encoding.EncodeToString(h().Sum(nil)), "=") + hash := h() + hash.Write([]byte(userID)) + hash.Write([]byte(connID)) + return strings.TrimRight(encoding.EncodeToString(hash.Sum(nil)), "=") } func (c *client) urlFor(apiVersion, namespace, resource, name string) string { diff --git a/storage/kubernetes/client_test.go b/storage/kubernetes/client_test.go index 01b4eb7a..5793d7d1 100644 --- a/storage/kubernetes/client_test.go +++ b/storage/kubernetes/client_test.go @@ -29,6 +29,19 @@ func TestIDToName(t *testing.T) { wg.Wait() } +func TestOfflineTokenName(t *testing.T) { + h := func() hash.Hash { return fnv.New64() } + + userID1 := "john" + userID2 := "jane" + + id1 := offlineTokenName(userID1, "local", h) + id2 := offlineTokenName(userID2, "local", h) + if id1 == id2 { + t.Errorf("expected offlineTokenName to produce different hashes") + } +} + func TestNamespaceFromServiceAccountJWT(t *testing.T) { namespace, err := namespaceFromServiceAccountJWT(serviceAccountToken) if err != nil {