diff --git a/src/main/java/io/jenkins/plugins/jfrog/configuration/JenkinsSecretManager.java b/src/main/java/io/jenkins/plugins/jfrog/configuration/JenkinsSecretManager.java index 3c9b617e..ee24e82f 100644 --- a/src/main/java/io/jenkins/plugins/jfrog/configuration/JenkinsSecretManager.java +++ b/src/main/java/io/jenkins/plugins/jfrog/configuration/JenkinsSecretManager.java @@ -52,6 +52,16 @@ public void deleteSecret(String id) { } + public Secret getSecret(String name) { + List credentials = SystemCredentialsProvider.getInstance().getCredentials(); + for (Credentials credential : credentials) { + if (credential instanceof StringCredentialsImpl && ((StringCredentialsImpl) credential).getId().equals(name)) { + return ((StringCredentialsImpl) credential).getSecret(); + } + } + return null; + } + public boolean secretExists(String name) { List credentials = SystemCredentialsProvider.getInstance().getCredentials(); for (Credentials credential : credentials) { diff --git a/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorTest.java b/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorTest.java index a485094d..b4eb3b31 100644 --- a/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorTest.java +++ b/src/test/java/io/jenkins/plugins/jfrog/CliEnvConfiguratorTest.java @@ -3,6 +3,7 @@ import hudson.EnvVars; import io.jenkins.plugins.jfrog.actions.JFrogCliConfigEncryption; import io.jenkins.plugins.jfrog.configuration.JenkinsProxyConfiguration; +import io.jenkins.plugins.jfrog.configuration.JenkinsSecretManager; import jenkins.model.Jenkins; import org.junit.Before; import org.junit.Rule; @@ -39,26 +40,26 @@ public void configureCliEnvBasicTest() { assertEnv(envVars, JFROG_CLI_HOME_DIR, "a/b/c"); } -// @Test -// public void configEncryptionTest() { -// JFrogCliConfigEncryption configEncryption = new JFrogCliConfigEncryption(envVars); -// assertTrue(configEncryption.shouldEncrypt()); -// assertEquals(32, configEncryption.getKey().length()); -// -// invokeConfigureCliEnv("a/b/c", configEncryption); -// assertEnv(envVars, JFROG_CLI_ENCRYPTION_KEY, configEncryption.getKey()); -// } + @Test + public void configEncryptionTest() { + JFrogCliConfigEncryption configEncryption = new JFrogCliConfigEncryption(envVars); + assertTrue(configEncryption.shouldEncrypt()); + assertEquals(32, configEncryption.getKey().length()); + + invokeConfigureCliEnv("a/b/c", configEncryption); + assertEquals(configEncryption.getKey(), new JenkinsSecretManager().getSecret(JFROG_CLI_ENCRYPTION_KEY).getPlainText()); + } -// @Test -// public void configEncryptionWithHomeDirTest() { -// // Config JFROG_CLI_HOME_DIR to disable key encryption -// envVars.put(JFROG_CLI_HOME_DIR, "/a/b/c"); -// JFrogCliConfigEncryption configEncryption = new JFrogCliConfigEncryption(envVars); -// invokeConfigureCliEnv("", configEncryption); -// -// assertFalse(configEncryption.shouldEncrypt()); -// assertFalse(envVars.containsKey(JFROG_CLI_ENCRYPTION_KEY)); -// } + @Test + public void configEncryptionWithHomeDirTest() { + // Config JFROG_CLI_HOME_DIR to disable key encryption + envVars.put(JFROG_CLI_HOME_DIR, "/a/b/c"); + JFrogCliConfigEncryption configEncryption = new JFrogCliConfigEncryption(envVars); + invokeConfigureCliEnv("", configEncryption); + + assertFalse(configEncryption.shouldEncrypt()); + assertNull(new JenkinsSecretManager().getSecret(JFROG_CLI_ENCRYPTION_KEY)); + } void assertEnv(EnvVars envVars, String key, String expectedValue) { assertEquals(expectedValue, envVars.get(key));