From 81f7df892a74dc8501f860fab584d23fecd9cf14 Mon Sep 17 00:00:00 2001 From: Alexander Matyushentsev Date: Fri, 11 Feb 2022 09:05:12 -0800 Subject: [PATCH] more reviewer notes Signed-off-by: Alexander Matyushentsev --- reposerver/repository/repository.go | 5 +++-- util/helm/client.go | 25 +++++++++++++++++++++---- util/io/paths.go | 4 ++-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/reposerver/repository/repository.go b/reposerver/repository/repository.go index 783ead446a7ca..dba971b7583d3 100644 --- a/reposerver/repository/repository.go +++ b/reposerver/repository/repository.go @@ -123,6 +123,9 @@ func NewService(metricsServer *metrics.MetricsServer, cache *reposervercache.Cac func (s *Service) Init() error { _, err := os.Stat(s.rootDir) + if os.IsNotExist(err) { + return os.MkdirAll(s.rootDir, 0300) + } if err == nil { // give itself read permissions to list previously written directories err = os.Chmod(s.rootDir, 0700) @@ -130,8 +133,6 @@ func (s *Service) Init() error { var files []fs.FileInfo if err == nil { files, err = ioutil.ReadDir(s.rootDir) - } else if !os.IsNotExist(err) { - return os.MkdirAll(s.rootDir, 0300) } if err != nil { log.Warnf("Failed to restore cloned repositories paths: %v", err) diff --git a/util/helm/client.go b/util/helm/client.go index ba50f471cb601..c3c9ed7e7e11b 100644 --- a/util/helm/client.go +++ b/util/helm/client.go @@ -4,6 +4,7 @@ import ( "bytes" "crypto/tls" "crypto/x509" + "encoding/json" "errors" "fmt" "io/ioutil" @@ -117,6 +118,18 @@ func (c *nativeHelmChart) CleanChartCache(chart string, version string) error { return os.RemoveAll(cachePath) } +func createTempDir() (string, error) { + newUUID, err := uuid.NewRandom() + if err != nil { + return "", err + } + tempDir := path.Join(os.TempDir(), newUUID.String()) + if err := os.Mkdir(tempDir, 0755); err != nil { + return "", err + } + return tempDir, nil +} + func (c *nativeHelmChart) ExtractChart(chart string, version string, passCredentials bool) (string, io.Closer, error) { // always use Helm V3 since we don't have chart content to determine correct Helm version helmCmd, err := NewCmdWithVersion("", HelmV3, c.enableOci, c.proxy) @@ -132,7 +145,7 @@ func (c *nativeHelmChart) ExtractChart(chart string, version string, passCredent } // throw away temp directory that stores extracted chart and should be deleted as soon as no longer needed by returned closer - tempDir, err := ioutil.TempDir("", "helm") + tempDir, err := createTempDir() if err != nil { return "", nil, err } @@ -153,8 +166,8 @@ func (c *nativeHelmChart) ExtractChart(chart string, version string, passCredent if !exists { // create empty temp directory to extract chart from the registry - tempDest := path.Join(os.TempDir(), uuid.New().String()) - if err := os.Mkdir(tempDest, 0755); err != nil { + tempDest, err := createTempDir() + if err != nil { return "", nil, err } defer func() { _ = os.RemoveAll(tempDest) }() @@ -354,7 +367,11 @@ func normalizeChartName(chart string) string { } func (c *nativeHelmChart) getCachedChartPath(chart string, version string) (string, error) { - return c.chartCachePaths.GetPath(fmt.Sprintf("%s/%s:%s", c.repoURL, strings.ReplaceAll(chart, "/", "_"), version)) + keyData, err := json.Marshal(map[string]string{"url": c.repoURL, "chart": chart, "version": version}) + if err != nil { + return "", err + } + return c.chartCachePaths.GetPath(string(keyData)) } // Ensures that given OCI registries URL does not have protocol diff --git a/util/io/paths.go b/util/io/paths.go index 8b11966e21405..b9c20283bcdcd 100644 --- a/util/io/paths.go +++ b/util/io/paths.go @@ -7,7 +7,7 @@ import ( "github.com/google/uuid" ) -// TempPaths allows generating and memoizing random paths for a given URL. +// TempPaths allows generating and memoizing random paths, each path being mapped to a specific key. type TempPaths struct { root string paths map[string]string @@ -27,7 +27,7 @@ func (p *TempPaths) Add(key string, value string) { p.paths[key] = value } -// GetPath generates a path for the given URL or returns previously generated one. +// GetPath generates a path for the given key or returns previously generated one. func (p *TempPaths) GetPath(key string) (string, error) { p.lock.Lock() defer p.lock.Unlock()