diff --git a/utils/coreutils/coreconsts.go b/utils/coreutils/coreconsts.go index ad13dae05..33f74e697 100644 --- a/utils/coreutils/coreconsts.go +++ b/utils/coreutils/coreconsts.go @@ -74,6 +74,7 @@ var ( ApplicationKey = "JFROG_CLI_APPLICATION_KEY" SigningKey = "JFROG_CLI_SIGNING_KEY" KeyAlias = "JFROG_CLI_KEY_ALIAS" + CertsDir = "JFROG_CLI_CERTS_DIR" //#nosec G101 EncryptionKey = "JFROG_CLI_ENCRYPTION_KEY" // For CI runs diff --git a/utils/coreutils/utils.go b/utils/coreutils/utils.go index 6679bdb5a..b0b0a1b55 100644 --- a/utils/coreutils/utils.go +++ b/utils/coreutils/utils.go @@ -341,6 +341,17 @@ func GetJfrogSecurityDir() (string, error) { } func GetJfrogCertsDir() (string, error) { + if dir := os.Getenv(CertsDir); dir != "" { + dir = filepath.Clean(dir) + + absoluteDir, err := filepath.Abs(dir) + if err != nil { + return "", errorutils.CheckError(err) + } + + return absoluteDir, nil + } + securityDir, err := GetJfrogSecurityDir() if err != nil { return "", err diff --git a/utils/coreutils/utils_test.go b/utils/coreutils/utils_test.go index 8ddffbee4..ce1580012 100644 --- a/utils/coreutils/utils_test.go +++ b/utils/coreutils/utils_test.go @@ -279,3 +279,48 @@ func TestGetMaskedCommandString(t *testing.T) { "pip -i ***@someurl.com/repo --access-token=***", GetMaskedCommandString(exec.Command("pip", "-i", "https://user:pass@someurl.com/repo", "--access-token=123"))) } + +func TestGetJfrogCertsDirFromEnv(t *testing.T) { + certsDirPath, err := filepath.Abs(filepath.Join("/", "custom", "certs")) + assert.NoError(t, err) + + t.Setenv(CertsDir, certsDirPath) + defer func() { + assert.NoError(t, os.Unsetenv(CertsDir)) + }() + + certsDir, err := GetJfrogCertsDir() + + assert.NoError(t, err) + assert.Equal(t, certsDirPath, certsDir) +} + +func TestGetJfrogCertsDirFallsBackToHomeDir(t *testing.T) { + certsDirPath, err := filepath.Abs(filepath.Join("tmp", "jfrog")) + assert.NoError(t, err) + + t.Setenv(HomeDir, certsDirPath) + defer func() { + assert.NoError(t, os.Unsetenv(HomeDir)) + }() + + certsDir, err := GetJfrogCertsDir() + + assert.NoError(t, err) + assert.Equal(t, filepath.Join(certsDirPath, JfrogSecurityDirName, JfrogCertsDirName), certsDir) +} + +func TestGetJfrogCertsDirNormalizesPath(t *testing.T) { + t.Setenv(CertsDir, filepath.Join("test", "..", "custom", "certs")) + defer func() { + assert.NoError(t, os.Unsetenv(CertsDir)) + }() + + expectedDir, err := filepath.Abs(filepath.Join("custom", "certs")) + assert.NoError(t, err) + + certsDir, err := GetJfrogCertsDir() + + assert.NoError(t, err) + assert.Equal(t, expectedDir, certsDir) +}