diff --git a/registry/storage/driver/swift/swift.go b/registry/storage/driver/swift/swift.go index adbe8aa9..b64e3b66 100644 --- a/registry/storage/driver/swift/swift.go +++ b/registry/storage/driver/swift/swift.go @@ -667,13 +667,16 @@ func (d *driver) swiftPath(path string) string { return strings.TrimLeft(strings.TrimRight(d.Prefix+"/files"+path, "/"), "/") } +// swiftSegmentPath returns a randomly generated path in the segments directory. func (d *driver) swiftSegmentPath(path string) (string, error) { checksum := sha1.New() - random := make([]byte, 32) - if _, err := rand.Read(random); err != nil { + checksum.Write([]byte(path)) + + if _, err := io.CopyN(checksum, rand.Reader, 32); err != nil { return "", err } - path = hex.EncodeToString(checksum.Sum(append([]byte(path), random...))) + + path = hex.EncodeToString(checksum.Sum(nil)) return strings.TrimLeft(strings.TrimRight(d.Prefix+"/segments/"+path[0:3]+"/"+path[3:], "/"), "/"), nil } diff --git a/registry/storage/driver/swift/swift_test.go b/registry/storage/driver/swift/swift_test.go index 00288cba..17007c19 100644 --- a/registry/storage/driver/swift/swift_test.go +++ b/registry/storage/driver/swift/swift_test.go @@ -243,3 +243,39 @@ func TestFilenameChunking(t *testing.T) { t.Fatal("expected error for size = -1") } } + +func TestSwiftSegmentPath(t *testing.T) { + d := &driver{ + Prefix: "/test/segment/path", + } + + s1, err := d.swiftSegmentPath("foo-baz") + if err != nil { + t.Fatalf("unexpected error generating segment path: %v", err) + } + + s2, err := d.swiftSegmentPath("foo-baz") + if err != nil { + t.Fatalf("unexpected error generating segment path: %v", err) + } + + if !strings.HasPrefix(s1, "test/segment/path/segments/") { + t.Fatalf("expected to be prefixed: %s", s1) + } + + if !strings.HasPrefix(s1, "test/segment/path/segments/") { + t.Fatalf("expected to be prefixed: %s", s2) + } + + if len(s1) != 68 { + t.Fatalf("unexpected segment path length, %d != %d", len(s1), 68) + } + + if len(s2) != 68 { + t.Fatalf("unexpected segment path length, %d != %d", len(s2), 68) + } + + if s1 == s2 { + t.Fatalf("expected segment paths to differ, %s == %s", s1, s2) + } +}