From af4e690837bbd939c38bfafb9e27d7ad424dce61 Mon Sep 17 00:00:00 2001 From: Thomas Stromberg Date: Wed, 11 Dec 2019 09:14:20 -0800 Subject: [PATCH] Use sha1 for a larger hash, truncated to 40 chars --- pkg/util/lock/lock.go | 13 ++++++------- pkg/util/lock/lock_test.go | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/pkg/util/lock/lock.go b/pkg/util/lock/lock.go index d5c945b9d8..6c5eee6ac8 100644 --- a/pkg/util/lock/lock.go +++ b/pkg/util/lock/lock.go @@ -17,12 +17,11 @@ limitations under the License. package lock import ( - "crypto/md5" + "crypto/sha1" "fmt" "io/ioutil" "os" "os/user" - "regexp" "time" "github.com/golang/glog" @@ -32,8 +31,6 @@ import ( ) var ( - // nonString is characters to strip from lock names - nonString = regexp.MustCompile(`[\W_]+`) // forceID is a user id for consistent testing forceID = "" ) @@ -64,9 +61,10 @@ func UserMutexSpec(path string) mutex.Spec { id = u.Uid } } - + name := getMutexNameForPath(fmt.Sprintf("%s-%s", path, id)) + glog.Infof("mutex name for %s: %s", path, name) s := mutex.Spec{ - Name: getMutexNameForPath(fmt.Sprintf("%s-%s", path, id)), + Name: name, Clock: clock.WallClock, // Poll the lock twice a second Delay: 500 * time.Millisecond, @@ -78,5 +76,6 @@ func UserMutexSpec(path string) mutex.Spec { func getMutexNameForPath(path string) string { // juju requires that names match ^[a-zA-Z][a-zA-Z0-9-]*$", and be under 40 chars long. - return fmt.Sprintf("m%x", md5.Sum([]byte(path))) + name := fmt.Sprintf("mk%x", sha1.Sum([]byte(path))) + return name[0:40] } diff --git a/pkg/util/lock/lock_test.go b/pkg/util/lock/lock_test.go index d7d7a22fe9..43656fa943 100644 --- a/pkg/util/lock/lock_test.go +++ b/pkg/util/lock/lock_test.go @@ -62,8 +62,8 @@ func TestUserMutexSpec(t *testing.T) { for _, tc := range tests { t.Run(tc.description, func(t *testing.T) { got := UserMutexSpec(tc.path) - if len(got.Name) > 40 { - t.Errorf("%s mutex name is too long", got.Name) + if len(got.Name) != 40 { + t.Errorf("%s is not 40 chars long", got.Name) } }) }