From 8dd9f1bf42cdb2bf3d9b055f1be7bd2908c91bf1 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Sun, 4 Oct 2009 14:52:21 +0000 Subject: [PATCH] Secret class should be serialization forward compatible with Strnig. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@22381 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/util/Secret.java | 6 +++++- core/src/test/java/hudson/util/SecretTest.java | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/util/Secret.java b/core/src/main/java/hudson/util/Secret.java index 81ca5914b6..d4c55449f8 100644 --- a/core/src/main/java/hudson/util/Secret.java +++ b/core/src/main/java/hudson/util/Secret.java @@ -45,6 +45,10 @@ import java.security.GeneralSecurityException; * Note that since the cryptography relies on {@link Hudson#getSecretKey()}, this is not meant as a protection * against code running in the same VM, nor against an attacker who has local file system access. * + *

+ * {@link Secret}s can correctly read-in plain text password, so this allows the existing + * String field to be updated to {@link Secret}. + * * @author Kohsuke Kawaguchi */ public final class Secret { @@ -155,7 +159,7 @@ public final class Secret { } public Object unmarshal(HierarchicalStreamReader reader, final UnmarshallingContext context) { - return Secret.decrypt(reader.getValue()); + return fromString(reader.getValue()); } } diff --git a/core/src/test/java/hudson/util/SecretTest.java b/core/src/test/java/hudson/util/SecretTest.java index 3f1d7a950d..ef52bf6d61 100644 --- a/core/src/test/java/hudson/util/SecretTest.java +++ b/core/src/test/java/hudson/util/SecretTest.java @@ -71,4 +71,19 @@ public class SecretTest extends TestCase { Object o = Hudson.XSTREAM.fromXML(xml); assertEquals(o,s); } + + public static class Foo { + Secret password; + } + + /** + * Makes sure the serialization form is backward compatible with String. + */ + public void testCompatibilityFromString() { + String tagName = Foo.class.getName().replace("$","-"); + String xml = "<"+tagName+">secret"; + Foo foo = new Foo(); + Hudson.XSTREAM.fromXML(xml, foo); + assertEquals("secret",foo.password.toString()); + } }