From f4b2bc9ff6cc6229402d7c27e887763154cf0378 Mon Sep 17 00:00:00 2001 From: Maria Shaldybin Date: Sat, 27 Jan 2024 01:06:11 +0000 Subject: [PATCH] Generate 16 byte length cache key for buildpack paths When generated key starts with zeros %x formatting strips first zeros. Add padding to format up to 16 bytes to match the key generated by cloud controller. --- builder/builder_test.go | 22 +++++++++++---------- builder_config.go | 2 +- builder_config_test.go | 40 +++++++++++++++++++++++++-------------- buildpackrunner/runner.go | 2 +- 4 files changed, 40 insertions(+), 26 deletions(-) diff --git a/builder/builder_test.go b/builder/builder_test.go index 5e36db1d..74868666 100644 --- a/builder/builder_test.go +++ b/builder/builder_test.go @@ -60,8 +60,7 @@ var _ = Describe("Building", func() { } cpBuildpack := func(buildpack string) { - hash := fmt.Sprintf("%x", xxhash.Sum64String(buildpack)) - cp(filepath.Join(buildpackFixtures, buildpack), filepath.Join(buildpacksDir, hash)) + cp(filepath.Join(buildpackFixtures, buildpack), filepath.Join(buildpacksDir, buildpackHash(buildpack))) } BeforeEach(func() { @@ -401,8 +400,7 @@ var _ = Describe("Building", func() { Skip("warning is unnecessary on Windows") } - hash := fmt.Sprintf("%x", xxhash.Sum64String("always-detects")) - binDetect := filepath.Join(buildpacksDir, hash, "bin", "detect") + binDetect := filepath.Join(buildpacksDir, buildpackHash("always-detects"), "bin", "detect") Expect(os.Chmod(binDetect, 0644)).To(Succeed()) }) @@ -794,10 +792,10 @@ var _ = Describe("Building", func() { }) It("the supply buildpacks caches supply output as $CACHE_DIR/", func() { - supplyCacheDir := fmt.Sprintf("%x", xxhash.Sum64String("always-detects-creates-build-artifacts")) + supplyCacheDir := buildpackHash("always-detects-creates-build-artifacts") Expect(files).To(ContainElement("./" + supplyCacheDir + "/supplied")) - supplyCacheDir = fmt.Sprintf("%x", xxhash.Sum64String("always-detects")) + supplyCacheDir = buildpackHash("always-detects") Expect(files).To(ContainElement("./" + supplyCacheDir + "/supplied")) content, err := exec.Command("tar", "-xzOf", outputBuildArtifactsCache, "./"+supplyCacheDir+"/supplied").Output() @@ -835,10 +833,10 @@ var _ = Describe("Building", func() { }) It("the supply buildpacks caches supply output as $CACHE_DIR/", func() { - supplyCacheDir := fmt.Sprintf("%x", xxhash.Sum64String("always-detects-creates-build-artifacts")) + supplyCacheDir := buildpackHash("always-detects-creates-build-artifacts") Expect(files).To(ContainElement("./" + supplyCacheDir + "/supplied")) - supplyCacheDir = fmt.Sprintf("%x", xxhash.Sum64String("always-detects")) + supplyCacheDir = buildpackHash("always-detects") Expect(files).To(ContainElement("./" + supplyCacheDir + "/supplied")) content, err := exec.Command("tar", "-xzOf", outputBuildArtifactsCache, "./"+supplyCacheDir+"/supplied").Output() @@ -860,13 +858,13 @@ var _ = Describe("Building", func() { BeforeEach(func() { rand.Seed(time.Now().UnixNano()) cachedSupply = fmt.Sprintf("%d", rand.Int()) - alwaysDetectsHash = fmt.Sprintf("%x", xxhash.Sum64String("always-detects")) + alwaysDetectsHash = buildpackHash("always-detects") err := os.MkdirAll(filepath.Join(buildArtifactsCacheDir, alwaysDetectsHash), 0755) Expect(err).To(BeNil()) err = ioutil.WriteFile(filepath.Join(buildArtifactsCacheDir, alwaysDetectsHash, "old-supply"), []byte(cachedSupply), 0644) Expect(err).To(BeNil()) - notInBuildpackOrderHash = fmt.Sprintf("%x", xxhash.Sum64String("not-in-buildpack-order")) + notInBuildpackOrderHash = buildpackHash("not-in-buildpack-order") err = os.MkdirAll(filepath.Join(buildArtifactsCacheDir, notInBuildpackOrderHash), 0755) Expect(err).To(BeNil()) @@ -1423,3 +1421,7 @@ func removeTrailingSpace(dirty []string) []string { return clean } + +func buildpackHash(key string) string { + return fmt.Sprintf("%016x", xxhash.Sum64String(key)) +} diff --git a/builder_config.go b/builder_config.go index 41b955f9..ffc40d78 100644 --- a/builder_config.go +++ b/builder_config.go @@ -159,7 +159,7 @@ func (s LifecycleBuilderConfig) BuildpackPath(buildpackName string) string { if err == nil && buildpackURL.IsAbs() { baseDir = s.BuildpacksDownloadDir() } - return filepath.Join(baseDir, fmt.Sprintf("%x", xxhash.Sum64String(buildpackName))) + return filepath.Join(baseDir, fmt.Sprintf("%016x", xxhash.Sum64String(buildpackName))) } func (s LifecycleBuilderConfig) LegacyBuildpackPath(buildpackName string) string { diff --git a/builder_config_test.go b/builder_config_test.go index b64acdfa..b8a66f35 100644 --- a/builder_config_test.go +++ b/builder_config_test.go @@ -96,24 +96,36 @@ var _ = Describe("LifecycleBuilderConfig", func() { }) }) - It("returns the path to a given system buildpack using legacy md5", func() { - key := "my-buildpack/key/::" - Expect(builderConfig.LegacyBuildpackPath(key)).To(Equal(filepath.Join(pathPrefix(), "tmp", "buildpacks", "8b2f72a0702aed614f8b5d8f7f5b431b"))) - }) + Describe("LegacyBuildpackPath", func() { + It("returns the path to a given system buildpack using legacy md5", func() { + key := "my-buildpack/key/::" + Expect(builderConfig.LegacyBuildpackPath(key)).To(Equal(filepath.Join(pathPrefix(), "tmp", "buildpacks", "8b2f72a0702aed614f8b5d8f7f5b431b"))) + }) - It("returns the path to a given downloaded buildpack using legacy md5", func() { - key := "https://github.com/cloudfoundry/ruby-buildpack" - Expect(builderConfig.LegacyBuildpackPath(key)).To(Equal(filepath.Join(pathPrefix(), "tmp", "buildpackdownloads", "21de62d118ecb1f46d868d24f00839ef"))) + It("returns the path to a given downloaded buildpack using legacy md5", func() { + key := "https://github.com/cloudfoundry/ruby-buildpack" + Expect(builderConfig.LegacyBuildpackPath(key)).To(Equal(filepath.Join(pathPrefix(), "tmp", "buildpackdownloads", "21de62d118ecb1f46d868d24f00839ef"))) + }) }) - It("returns the path to a given system buildpack", func() { - key := "my-buildpack/key/::" - Expect(builderConfig.BuildpackPath(key)).To(Equal(filepath.Join(pathPrefix(), "tmp", "buildpacks", "dc91f5556d3ae859"))) - }) + Describe("BuildpackPath", func() { + It("returns the path to a given system buildpack", func() { + key := "my-buildpack/key/::" + Expect(builderConfig.BuildpackPath(key)).To(Equal(filepath.Join(pathPrefix(), "tmp", "buildpacks", "dc91f5556d3ae859"))) + }) + + It("returns the path to a given downloaded buildpack", func() { + key := "https://github.com/cloudfoundry/ruby-buildpack" + Expect(builderConfig.BuildpackPath(key)).To(Equal(filepath.Join(pathPrefix(), "tmp", "buildpackdownloads", "0e1df1251578f504"))) + }) - It("returns the path to a given downloaded buildpack", func() { - key := "https://github.com/cloudfoundry/ruby-buildpack" - Expect(builderConfig.BuildpackPath(key)).To(Equal(filepath.Join(pathPrefix(), "tmp", "buildpackdownloads", "e1df1251578f504"))) + Context("when key generates hash that start with zeros", func() { + It("returns the path that contains 16 byte key hash", func() { + // xxhash starts with zeros + key := "a1b4aa82-e5ae-4736-8284-bdfb013a7121_2845c523cf4f4f1045b331982cc557ef761afde4dc9fc8777831563461309455" + Expect(builderConfig.BuildpackPath(key)).To(Equal(filepath.Join(pathPrefix(), "tmp", "buildpacks", "00c84a1da9e0c81e"))) + }) + }) }) It("returns the path to the staging metadata", func() { diff --git a/buildpackrunner/runner.go b/buildpackrunner/runner.go index c10c6392..20f7fd96 100644 --- a/buildpackrunner/runner.go +++ b/buildpackrunner/runner.go @@ -487,7 +487,7 @@ func (runner *Runner) pathHasBinDirectory(pathToTest string) bool { } func (runner *Runner) supplyCachePath(buildpack string) string { - return filepath.Join(runner.config.BuildArtifactsCacheDir(), fmt.Sprintf("%x", xxhash.Sum64String(buildpack))) + return filepath.Join(runner.config.BuildArtifactsCacheDir(), fmt.Sprintf("%016x", xxhash.Sum64String(buildpack))) } func fileExists(file string) (bool, error) {