Skip to content

Commit

Permalink
more reviewer notes
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Matyushentsev <AMatyushentsev@gmail.com>
  • Loading branch information
alexmt committed Feb 16, 2022
1 parent 20e365d commit 81f7df8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
5 changes: 3 additions & 2 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,16 @@ 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)
}
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)
Expand Down
25 changes: 21 additions & 4 deletions util/helm/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
Expand All @@ -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) }()
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions util/io/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 81f7df8

Please sign in to comment.