Skip to content

Commit

Permalink
Store encrypted password
Browse files Browse the repository at this point in the history
Now password for SSH authentication file is stored as plain text.

This patch fixes it. Already stored password would be replaced to
encrypted ones if config is saved once.

Fix for JENKINS-23165
  • Loading branch information
rinrinne committed May 27, 2014
1 parent 4775bce commit 7d6bcae
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
Expand Up @@ -141,7 +141,7 @@ public class Config implements IGerritHudsonTriggerConfig {
private String gerritUserName;
private String gerritEMail;
private File gerritAuthKeyFile;
private String gerritAuthKeyFilePassword;
private Secret gerritAuthKeyFilePassword;
private boolean useRestApi;
private String gerritHttpUserName;
private Secret gerritHttpPassword;
Expand Down Expand Up @@ -199,7 +199,7 @@ public Config(IGerritHudsonTriggerConfig config) {
gerritEMail = config.getGerritEMail();
notificationLevel = config.getNotificationLevel();
gerritAuthKeyFile = new File(config.getGerritAuthKeyFile().getPath());
gerritAuthKeyFilePassword = config.getGerritAuthKeyFilePassword();
gerritAuthKeyFilePassword = Secret.fromString(config.getGerritAuthKeyFilePassword());
useRestApi = config.isUseRestApi();
gerritHttpUserName = config.getGerritHttpUserName();
gerritHttpPassword = Secret.fromString(config.getGerritHttpPassword());
Expand Down Expand Up @@ -254,11 +254,11 @@ public void setValues(JSONObject formData) {
} else {
gerritAuthKeyFile = DEFAULT_GERRIT_AUTH_KEY_FILE;
}
gerritAuthKeyFilePassword = formData.optString(
gerritAuthKeyFilePassword = Secret.fromString(formData.optString(
"gerritAuthKeyFilePassword",
DEFAULT_GERRIT_AUTH_KEY_FILE_PASSWORD);
DEFAULT_GERRIT_AUTH_KEY_FILE_PASSWORD));

if (gerritAuthKeyFilePassword != null && gerritAuthKeyFilePassword.length() <= 0) {
if (gerritAuthKeyFilePassword != null && gerritAuthKeyFilePassword.getPlainText().length() <= 0) {
gerritAuthKeyFilePassword = null;
}
gerritBuildCurrentPatchesOnly = formData.optBoolean(
Expand Down Expand Up @@ -477,7 +477,11 @@ public void setGerritAuthKeyFile(File gerritAuthKeyFile) {

@Override
public String getGerritAuthKeyFilePassword() {
return gerritAuthKeyFilePassword;
if (gerritAuthKeyFilePassword == null) {
return "";
} else {
return gerritAuthKeyFilePassword.getPlainText();
}
}

/**
Expand All @@ -487,7 +491,7 @@ public String getGerritAuthKeyFilePassword() {
* @see #getGerritAuthKeyFilePassword()
*/
public void setGerritAuthKeyFilePassword(String gerritAuthKeyFilePassword) {
this.gerritAuthKeyFilePassword = gerritAuthKeyFilePassword;
this.gerritAuthKeyFilePassword = Secret.fromString(gerritAuthKeyFilePassword);
}

/**
Expand Down Expand Up @@ -874,7 +878,14 @@ public void setEnableManualTrigger(boolean enableManualTrigger) {

@Override
public Authentication getGerritAuthentication() {
return new Authentication(gerritAuthKeyFile, gerritUserName, gerritAuthKeyFilePassword);
Authentication authentication;
if (gerritAuthKeyFilePassword == null) {
authentication = new Authentication(gerritAuthKeyFile, gerritUserName, "");
} else {
authentication = new Authentication(
gerritAuthKeyFile, gerritUserName, gerritAuthKeyFilePassword.getPlainText());
}
return authentication;
}

@Override
Expand Down
Expand Up @@ -36,6 +36,8 @@
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import hudson.util.Secret;

import java.io.File;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -253,4 +255,17 @@ public void testGetGerritFrontEndUrlForChangeBasedEventProvider() {
event.getChange().setUrl(null);
assertEquals("http://gerrit/1000", config.getGerritFrontEndUrlFor(event));
}

/**
* Tests {@link Config#getGerritAuthKeyFilePassword()}.
* With a encrypted string as password.
*/
@Test
public void testGetGerritAuthKeyFilePasswordFromEncryptedString() {
Secret pass = Secret.fromString("gerritpass");
String formString = "{\"gerritAuthKeyFilePassword\":\"" + pass.getEncryptedValue() + "\"}";
JSONObject form = (JSONObject)JSONSerializer.toJSON(formString);
Config config = new Config(form);
assertEquals("gerritpass", config.getGerritAuthKeyFilePassword());
}
}

0 comments on commit 7d6bcae

Please sign in to comment.