Skip to content

Commit

Permalink
Merge pull request #266 from jglick/NPE-JENKINS-67132
Browse files Browse the repository at this point in the history
[JENKINS-67132] Default `UsernamePasswordCredentialsImpl.usernameSecret` without `readResolve`
  • Loading branch information
jglick authored Jan 5, 2022
2 parents 1346ba4 + 15110e9 commit 7328cda
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,6 @@ public UsernamePasswordCredentialsImpl(@CheckForNull CredentialsScope scope,
this.password = Secret.fromString(password);
}

private Object readResolve() {
if (usernameSecret == null) {
usernameSecret = true;
}
return this;
}

/**
* {@inheritDoc}
*/
Expand All @@ -104,7 +97,7 @@ public String getUsername() {

@Override
public boolean isUsernameSecret() {
return usernameSecret;
return usernameSecret != null ? usernameSecret : true;
}

@DataBoundSetter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@
package com.cloudbees.plugins.credentials.impl;

import com.cloudbees.plugins.credentials.CredentialsNameProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import java.util.logging.Level;
import jenkins.model.Jenkins;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.LoggerRule;

Expand All @@ -48,4 +51,28 @@ public class UsernamePasswordCredentialsImplTest {
assertEquals("abc123", CredentialsNameProvider.name(creds));
}

@Issue("JENKINS-67132")
@Test public void subclassDeserialization() {
SpecialUsernamePasswordCredentialsImpl c = (SpecialUsernamePasswordCredentialsImpl) Jenkins.XSTREAM2.fromXML(
"<com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImplTest_-SpecialUsernamePasswordCredentialsImpl>\n" +
" <scope>GLOBAL</scope>\n" +
" <id>xxx</id>\n" +
" <username>bob</username>\n" +
" <password>s3cr3t</password>\n" +
"</com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImplTest_-SpecialUsernamePasswordCredentialsImpl>");
assertTrue(c.initialized);
assertEquals("bob", c.getUsername());
assertTrue(c.isUsernameSecret());
}
public static final class SpecialUsernamePasswordCredentialsImpl extends UsernamePasswordCredentialsImpl {
public SpecialUsernamePasswordCredentialsImpl(CredentialsScope scope, String id, String description, String username, String password) {
super(scope, id, description, username, password);
}
transient boolean initialized;
private Object readResolve() {
initialized = true;
return this;
}
}

}

0 comments on commit 7328cda

Please sign in to comment.