提交 5ae67f41 编写于 作者: K kohsuke

Added Secret.toString(Secret) that hides the handling against NPE.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@30276 71c3de6d-444a-0410-be80-ed276b4c234a
上级 82dba60b
......@@ -63,7 +63,7 @@ public class PasswordParameterDefinition extends SimpleParameterDefinition {
}
public String getDefaultValue() {
return defaultValue != null ? defaultValue.toString() : null;
return Secret.toString(defaultValue);
}
// kept for backward compatibility
......
......@@ -50,7 +50,7 @@ public class PasswordParameterValue extends ParameterValue {
@Override
public void buildEnvVars(AbstractBuild<?,?> build, EnvVars env) {
String v = value != null ? value.toString() : null;
String v = Secret.toString(value);
env.put(name, v);
env.put(name.toUpperCase(Locale.ENGLISH),v); // backward compatibility pre 1.345
}
......@@ -59,7 +59,7 @@ public class PasswordParameterValue extends ParameterValue {
public VariableResolver<String> createVariableResolver(AbstractBuild<?, ?> build) {
return new VariableResolver<String>() {
public String resolve(String name) {
return PasswordParameterValue.this.name.equals(name) ? (value != null ? value.toString() : null) : null;
return PasswordParameterValue.this.name.equals(name) ? Secret.toString(value) : null;
}
};
}
......
......@@ -94,8 +94,8 @@ public class ManagedWindowsServiceLauncher extends ComputerLauncher {
private JIDefaultAuthInfoImpl createAuth() {
String[] tokens = userName.split("\\\\");
if(tokens.length==2)
return new JIDefaultAuthInfoImpl(tokens[0], tokens[1], password.toString());
return new JIDefaultAuthInfoImpl("", userName, password.toString());
return new JIDefaultAuthInfoImpl(tokens[0], tokens[1], Secret.toString(password));
return new JIDefaultAuthInfoImpl("", userName, Secret.toString(password));
}
private NtlmPasswordAuthentication createSmbAuth() {
......
......@@ -258,8 +258,7 @@ public class Mailer extends Notifier {
props.put("mail.smtp.timeout","60000");
props.put("mail.smtp.connectiontimeout","60000");
return Session.getInstance(props,getAuthenticator(smtpAuthUserName,
smtpAuthPassword!=null ? smtpAuthPassword.toString() : null));
return Session.getInstance(props,getAuthenticator(smtpAuthUserName,Secret.toString(smtpAuthPassword)));
}
private static Authenticator getAuthenticator(final String smtpAuthUserName, final String smtpAuthPassword) {
......@@ -326,7 +325,7 @@ public class Mailer extends Notifier {
}
public String getSmtpAuthPassword() {
return smtpAuthPassword!=null ? smtpAuthPassword.toString() : null;
return Secret.toString(smtpAuthPassword);
}
public boolean getUseSsl() {
......
......@@ -65,12 +65,24 @@ public final class Secret {
* Obtains the secret in a plain text.
*
* @see #getEncryptedValue()
* @deprecated as of 1.356
* Use {@link #toString(Secret)} to avoid NPE in case Secret is null.
* Or if you really know what you are doing, use the {@link #getPlainText()} method.
*/
@Override
public String toString() {
return value;
}
/**
* Obtains the plain text password.
* Before using this method, ask yourself if you'd be better off using {@link Secret#toString(Secret)}
* to avoid NPE.
*/
public String getPlainText() {
return value;
}
@Override
public boolean equals(Object that) {
return that instanceof Secret && value.equals(((Secret)that).value);
......@@ -140,11 +152,21 @@ public final class Secret {
* @return never null
*/
public static Secret fromString(String data) {
data = Util.fixNull(data);
Secret s = decrypt(data);
if(s==null) s=new Secret(data);
return s;
}
/**
* Works just like {@link Secret#toString()} but avoids NPE when the secret is null.
* To be consistent with {@link #fromString(String)}, this method doesn't distinguish
* empty password and null password.
*/
public static String toString(Secret s) {
return s==null ? "" : s.value;
}
public static final class ConverterImpl implements Converter {
public ConverterImpl() {
}
......
......@@ -48,7 +48,7 @@ public class SecretTest extends TestCase {
public void testEncrypt() {
Secret secret = Secret.fromString("abc");
assertEquals("abc",secret.toString());
assertEquals("abc",secret.getPlainText());
// make sure we got some encryption going
System.out.println(secret.getEncryptedValue());
......@@ -59,13 +59,13 @@ public class SecretTest extends TestCase {
}
public void testDecrypt() {
assertEquals("abc",Secret.fromString("abc").toString());
assertEquals("abc",Secret.toString(Secret.fromString("abc")));
}
public void testSerialization() {
Secret s = Secret.fromString("Mr.Hudson");
String xml = Hudson.XSTREAM.toXML(s);
assertTrue(xml, !xml.contains(s.toString()));
assertTrue(xml, !xml.contains(s.getPlainText()));
assertTrue(xml, xml.contains(s.getEncryptedValue()));
Object o = Hudson.XSTREAM.fromXML(xml);
assertEquals(xml, o, s);
......@@ -83,6 +83,6 @@ public class SecretTest extends TestCase {
String xml = "<"+tagName+"><password>secret</password></"+tagName+">";
Foo foo = new Foo();
Hudson.XSTREAM.fromXML(xml, foo);
assertEquals("secret",foo.password.toString());
assertEquals("secret",Secret.toString(foo.password));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册