Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set encryption key as Jenkins Secret #104

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 org.apache.commons.lang3.StringUtils;

/**
Expand Down Expand Up @@ -43,8 +44,9 @@ static void configureCliEnv(EnvVars env, String jfrogHomeTempDir, JFrogCliConfig
setupProxy(env);
}
if (encryptionKey.shouldEncrypt()) {
// Set up a random encryption key to make sure no raw text secrets are stored in the file system
env.putIfAbsent(JFROG_CLI_ENCRYPTION_KEY, encryptionKey.getKey());
// Set up a random encryption key and set as a Jenkins secret
JenkinsSecretManager js = new JenkinsSecretManager();
js.createSecret(JFROG_CLI_ENCRYPTION_KEY, encryptionKey.getKey(), "JFrog CLI encryption key");
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/io/jenkins/plugins/jfrog/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import hudson.model.Job;
import hudson.model.TaskListener;
import io.jenkins.plugins.jfrog.callables.TempDirCreator;
import io.jenkins.plugins.jfrog.configuration.JenkinsSecretManager;
import org.apache.commons.lang3.exception.ExceptionUtils;

import java.io.IOException;
Expand Down Expand Up @@ -62,4 +63,9 @@ public static FilePath createAndGetTempDir(final FilePath ws) throws IOException
public static FilePath createAndGetJfrogCliHomeTempDir(final FilePath ws, String buildNumber) throws IOException, InterruptedException {
return createAndGetTempDir(ws).child(buildNumber).child(".jfrog");
}

public static void deleteEncryptionKeySecret() {
JenkinsSecretManager js = new JenkinsSecretManager();
js.deleteSecret(CliEnvConfigurator.JFROG_CLI_ENCRYPTION_KEY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void onCompleted(@NonNull FlowExecution execution) {
try {
WorkflowRun build = getWorkflowRun(execution);
Utils.deleteBuildJfrogHomeDir(getWorkspace(build.getParent()), String.valueOf(build.getNumber()), getTaskListener(execution));
Utils.deleteEncryptionKeySecret();
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package io.jenkins.plugins.jfrog.configuration;

import com.cloudbees.plugins.credentials.Credentials;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.SystemCredentialsProvider;
import hudson.util.Secret;
import org.jenkinsci.plugins.plaincredentials.impl.StringCredentialsImpl;


import java.io.IOException;
import java.util.List;


public class JenkinsSecretManager {

public void createSecret(String name, String value, String description) {
if (secretExists(name)) {
System.out.println("Secret with name " + name + " already exists.");
return;
}
StringCredentialsImpl secret = new StringCredentialsImpl(
CredentialsScope.USER,
name,
description,
Secret.fromString(value)
);
try {
SystemCredentialsProvider.getInstance().getCredentials().add(secret);
SystemCredentialsProvider.getInstance().save();
} catch (IOException e) {
e.printStackTrace();
}
}

public void deleteSecret(String id) {
List<Credentials> credentials = SystemCredentialsProvider.getInstance().getCredentials();
Credentials toRemove = null;
for (Credentials credential : credentials) {
if (credential instanceof StringCredentialsImpl && ((StringCredentialsImpl) credential).getId().equals(id)) {
toRemove = credential;
break;
}
}
if (toRemove != null) {
try {
SystemCredentialsProvider.getInstance().getCredentials().remove(toRemove);
SystemCredentialsProvider.getInstance().save();
} catch (IOException e) {
e.printStackTrace();
}
}

}

public Secret getSecret(String name) {
List<Credentials> 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> credentials = SystemCredentialsProvider.getInstance().getCredentials();
for (Credentials credential : credentials) {
if (credential instanceof StringCredentialsImpl && ((StringCredentialsImpl) credential).getId().equals(name)) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,7 +47,7 @@ public void configEncryptionTest() {
assertEquals(32, configEncryption.getKey().length());

invokeConfigureCliEnv("a/b/c", configEncryption);
assertEnv(envVars, JFROG_CLI_ENCRYPTION_KEY, configEncryption.getKey());
assertEquals(configEncryption.getKey(), new JenkinsSecretManager().getSecret(JFROG_CLI_ENCRYPTION_KEY).getPlainText());
}

@Test
Expand All @@ -57,7 +58,7 @@ public void configEncryptionWithHomeDirTest() {
invokeConfigureCliEnv("", configEncryption);

assertFalse(configEncryption.shouldEncrypt());
assertFalse(envVars.containsKey(JFROG_CLI_ENCRYPTION_KEY));
assertNull(new JenkinsSecretManager().getSecret(JFROG_CLI_ENCRYPTION_KEY));
}

void assertEnv(EnvVars envVars, String key, String expectedValue) {
Expand Down
Loading