Skip to content

Commit e7a8b4a

Browse files
committedOct 22, 2016
[JENKINS-18211] Encrypt db password and API key using Secret class
Secret class is recommended way to store sensitive data in Jenkins. Previously, the values was stored in plain text.
1 parent 26890af commit e7a8b4a

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed
 

‎src/main/java/hudson/plugins/redmine/RedmineMetricsPublisher.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import hudson.Extension;
44
import hudson.Launcher;
5+
import hudson.Util;
56
import hudson.model.Action;
67
import hudson.model.BuildListener;
78
import hudson.model.AbstractBuild;
@@ -10,6 +11,7 @@
1011
import hudson.tasks.BuildStepMonitor;
1112
import hudson.tasks.Publisher;
1213
import hudson.util.FormValidation;
14+
import hudson.util.Secret;
1315

1416
import java.io.IOException;
1517
import java.io.PrintStream;
@@ -22,7 +24,7 @@
2224

2325
public class RedmineMetricsPublisher extends Publisher {
2426

25-
private String apiKey;
27+
private Secret apiKey;
2628
private String targetVersion;
2729
private String ignoreTicketTracker;
2830
private String ignoreTicketStatus;
@@ -31,7 +33,7 @@ public class RedmineMetricsPublisher extends Publisher {
3133
@DataBoundConstructor
3234
public RedmineMetricsPublisher(String apiKey, String targetVersion, String ignoreTicketTracker,
3335
String ignoreTicketStatus) {
34-
this.apiKey = apiKey;
36+
this.apiKey = Secret.fromString(Util.fixEmptyAndTrim(apiKey));
3537
this.targetVersion = targetVersion;
3638
this.ignoreTicketTracker = ignoreTicketTracker;
3739
this.ignoreTicketStatus = ignoreTicketStatus;
@@ -48,7 +50,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
4850
PrintStream logger = listener.getLogger();
4951

5052
RedmineMetricsCalculator calculator = new RedmineMetricsCalculator(rpp.getRedmineWebsite().baseUrl,
51-
apiKey, rpp.projectName, targetVersion, ignoreTicketTracker,
53+
apiKey.getPlainText(), rpp.projectName, targetVersion, ignoreTicketTracker,
5254
ignoreTicketStatus);
5355
try {
5456
List<MetricsResult> metricsList = calculator.calc();
@@ -66,7 +68,7 @@ public BuildStepMonitor getRequiredMonitorService() {
6668
return BuildStepMonitor.NONE;
6769
}
6870

69-
public String getApiKey() {
71+
public Secret getApiKey() {
7072
return apiKey;
7173
}
7274

‎src/main/java/hudson/plugins/redmine/RedmineSecurityRealm.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package hudson.plugins.redmine;
22

33
import hudson.Extension;
4+
import hudson.Util;
45
import hudson.model.Descriptor;
56
import hudson.plugins.redmine.dao.*;
67
import hudson.plugins.redmine.util.CipherUtil;
78
import hudson.plugins.redmine.util.Constants;
89
import hudson.security.AbstractPasswordBasedSecurityRealm;
910
import hudson.security.GroupDetails;
1011
import hudson.security.SecurityRealm;
12+
import hudson.util.Secret;
1113

1214
import java.util.HashSet;
1315
import java.util.Set;
@@ -47,7 +49,7 @@ public class RedmineSecurityRealm extends AbstractPasswordBasedSecurityRealm {
4749
private final String dbUserName;
4850

4951
/** Database Password */
50-
private final String dbPassword;
52+
private final Secret dbPassword;
5153

5254
/** Redmine Version */
5355
private final String version;
@@ -92,7 +94,7 @@ public RedmineSecurityRealm(String dbms, String dbServer, String databaseName, S
9294
this.port = port;
9395

9496
this.dbUserName = dbUserName;
95-
this.dbPassword = dbPassword;
97+
this.dbPassword = Secret.fromString(Util.fixEmptyAndTrim(dbPassword));
9698
this.version = StringUtils.isBlank(version) ? Constants.VERSION_1_2_0 : version;
9799

98100
this.loginTable = StringUtils.isBlank(loginTable) ? Constants.DEFAULT_LOGIN_TABLE : loginTable;
@@ -152,7 +154,7 @@ protected UserDetails authenticate(String username, String password) throws Auth
152154
LOGGER.info("DB Port : " + this.port);
153155
LOGGER.info("Database Name : " + this.databaseName);
154156

155-
dao.open(this.dbServer, this.port, this.databaseName, this.dbUserName, this.dbPassword);
157+
dao.open(this.dbServer, this.port, this.databaseName, this.dbUserName, this.dbPassword.getPlainText());
156158

157159
if (!dao.isTable(this.loginTable))
158160
throw new RedmineAuthenticationException("RedmineSecurity: Invalid Login Table");
@@ -200,7 +202,7 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
200202
try {
201203
dao = createAuthDao(this.dbms);
202204

203-
dao.open(this.dbServer, this.port, this.databaseName, this.dbUserName, this.dbPassword);
205+
dao.open(this.dbServer, this.port, this.databaseName, this.dbUserName, this.dbPassword.getPlainText());
204206

205207
if (!dao.isTable(this.loginTable))
206208
throw new RedmineAuthenticationException("RedmineSecurity: Invalid Login Table");
@@ -301,7 +303,7 @@ public String getDbUserName() {
301303
*
302304
* @return
303305
*/
304-
public String getDbPassword() {
306+
public Secret getDbPassword() {
305307
return dbPassword;
306308
}
307309

0 commit comments

Comments
 (0)
Please sign in to comment.