Skip to content

Commit

Permalink
fix: replace tmp by cache directory
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterSchafer committed Dec 27, 2022
1 parent c22f2c6 commit e72e1d1
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 89 deletions.
6 changes: 1 addition & 5 deletions cliv2/internal/embedded/cliv1/cliv1.go
Expand Up @@ -21,11 +21,7 @@ func CLIV1Version() string {
func GetFullCLIV1TargetPath(cacheDir string) (string, error) {
cliv1Filename := getCLIv1Filename()
versionTag := CLIV1Version()
relPath := path.Join(versionTag, cliv1Filename)
fullPath, err := utils.FullPathInSnykCacheDir(cacheDir, relPath)
if err != nil {
return "", err
}
fullPath := path.Join(utils.GetVersionCacheDirectory(cacheDir, versionTag), cliv1Filename)
return fullPath, nil
}

Expand Down
9 changes: 2 additions & 7 deletions cliv2/internal/proxy/proxy.go
Expand Up @@ -58,13 +58,8 @@ func NewWrapperProxy(insecureSkipVerify bool, cacheDirectory string, cliVersion
return nil, err
}

tempDir, err := utils.SnykTempDirectory(p.DebugLogger)
if err != nil {
p.DebugLogger.Println("failed to create system temp directory:", tempDir)
return nil, err
}

certFile, err := os.CreateTemp(tempDir, "snyk-cli-cert-*.crt")
tmpDirectory := utils.GetTemporaryDirectory(cacheDirectory, cliVersion)
certFile, err := os.CreateTemp(tmpDirectory, "snyk-cli-cert-*.crt")
if err != nil {
fmt.Println("failed to create temp cert file")
return nil, err
Expand Down
10 changes: 5 additions & 5 deletions cliv2/internal/proxy/proxy_test.go
Expand Up @@ -65,7 +65,7 @@ func helper_getHttpClient(gateway *proxy.WrapperProxy, useProxyAuth bool) (*http
}

func Test_closingProxyDeletesTempCert(t *testing.T) {
wp, err := proxy.NewWrapperProxy(false, "", "", debugLogger)
wp, err := proxy.NewWrapperProxy(false, "", "1.1.1", debugLogger)
assert.Nil(t, err)

err = wp.Start()
Expand All @@ -84,7 +84,7 @@ func basicAuthValue(username string, password string) string {
}

func Test_canGoThroughProxy(t *testing.T) {
wp, err := proxy.NewWrapperProxy(false, "", "", debugLogger)
wp, err := proxy.NewWrapperProxy(false, "", "1.1.2", debugLogger)
assert.Nil(t, err)

err = wp.Start()
Expand All @@ -108,7 +108,7 @@ func Test_canGoThroughProxy(t *testing.T) {
}

func Test_proxyRejectsWithoutBasicAuthHeader(t *testing.T) {
wp, err := proxy.NewWrapperProxy(false, "", "", debugLogger)
wp, err := proxy.NewWrapperProxy(false, "", "1.1.1", debugLogger)
assert.Nil(t, err)

err = wp.Start()
Expand Down Expand Up @@ -192,7 +192,7 @@ func Test_SetUpstreamProxy(t *testing.T) {
httpauth.UnknownMechanism,
}

objectUnderTest, err = proxy.NewWrapperProxy(false, "", "", debugLogger)
objectUnderTest, err = proxy.NewWrapperProxy(false, "", "1.1.1", debugLogger)
assert.Nil(t, err)

// running different cases
Expand Down Expand Up @@ -228,7 +228,7 @@ func Test_appendExtraCaCert(t *testing.T) {

os.Setenv(constants.SNYK_CA_CERTIFICATE_LOCATION_ENV, file.Name())

wp, err := proxy.NewWrapperProxy(false, "", "", debugLogger)
wp, err := proxy.NewWrapperProxy(false, "", "1.1.1", debugLogger)
assert.Nil(t, err)

certsPem, err := os.ReadFile(wp.CertificateLocation)
Expand Down
25 changes: 0 additions & 25 deletions cliv2/internal/utils/cache-dir.go

This file was deleted.

36 changes: 36 additions & 0 deletions cliv2/internal/utils/directories.go
@@ -0,0 +1,36 @@
package utils

import (
"os"
"path"

"github.com/pkg/errors"
)

// The directory structure used to cache things into
// - Base cache directory (user definable, default depends on OS, exmple: /Users/username/Library/Caches/snyk/)
// |- Version cache directory (example: /Users/username/Library/Caches/snyk/1.1075.0/)
// |- Temp directory (example: /Users/username/Library/Caches/snyk/1.1075.0/tmp/)

func GetTemporaryDirectory(baseCacheDirectory string, versionNumber string) string {
return path.Join(GetVersionCacheDirectory(baseCacheDirectory, versionNumber), "tmp")
}

func GetVersionCacheDirectory(baseCacheDirectory string, versionNumber string) string {
return path.Join(baseCacheDirectory, versionNumber)
}

func CreateAllDirectories(baseCacheDirectory string, versionNumber string) error {
directoryList := []string{
GetTemporaryDirectory(baseCacheDirectory, versionNumber),
}

for _, dir := range directoryList {
err := os.MkdirAll(dir, 0755)
if err != nil {
return errors.Wrap(err, "failed to create all directories.")
}
}

return nil
}
47 changes: 0 additions & 47 deletions cliv2/internal/utils/temp-dir.go

This file was deleted.

6 changes: 6 additions & 0 deletions cliv2/pkg/basic_workflows/legacycli.go
Expand Up @@ -68,6 +68,12 @@ func legacycliWorkflow(invocation workflow.InvocationContext, input []workflow.D
debugLogger.Println("Insecure HTTPS:", insecure)
debugLogger.Println("Use StdIO:", useStdIo)

// prepare environment by creating all required folders in advance
err = utils.CreateAllDirectories(cacheDirectory, cliv2.GetFullVersion())
if err != nil {
return output, err
}

// init cli object
var cli *cliv2.CLI
cli, err = cliv2.NewCLIv2(cacheDirectory, debugLogger)
Expand Down

0 comments on commit e72e1d1

Please sign in to comment.