Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* @author Kohsuke Kawaguchi
*/
public abstract class DefaultCargoContainerAdapterImpl extends CargoContainerAdapter {
@Target(FIELD)
@Target({FIELD, METHOD})
@Retention(RUNTIME)
@Documented
public @interface Property {
Expand All @@ -27,22 +30,39 @@ public abstract class DefaultCargoContainerAdapterImpl extends CargoContainerAda

/**
* Default implementation that fills the configuration by using
* fields annotated with {@link Property}.
* fields and getters annotated with {@link Property}.
*/
public void configure(Configuration config) {
for(Field f : getClass().getFields()) {
Property p = f.getAnnotation(Property.class);
if(p==null) continue;

try {
String v = ConvertUtils.convert(f.get(this));
if(v!=null)
config.setProperty(p.value(), v);
} catch (IllegalAccessException e) {
IllegalAccessError x = new IllegalAccessError();
x.initCause(e);
throw x;
}
setConfiguration(f, config);
}
for (Method m : getClass().getMethods()) {
setConfiguration(m, config);
}
}

private void setConfiguration(AccessibleObject ao, Configuration config) {
Property p = ao.getAnnotation(Property.class);
if(p==null) return;

try {
String v = ConvertUtils.convert(getPropertyValue(ao));
if(v!=null)
config.setProperty(p.value(), v);
} catch (Exception e) {
IllegalAccessError x = new IllegalAccessError();
x.initCause(e);
throw x;
}
}

private Object getPropertyValue(AccessibleObject ao) throws Exception {
if (ao instanceof Field) {
return ((Field)ao).get(this);
} else if (ao instanceof Method) {
return ((Method)ao).invoke(this);
} else {
throw new UnsupportedOperationException();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hudson.plugins.deploy;

import hudson.util.Scrambler;
import org.codehaus.cargo.container.property.RemotePropertySet;

/**
Expand All @@ -8,11 +9,27 @@
public abstract class PasswordProtectedAdapterCargo extends DefaultCargoContainerAdapterImpl {
@Property(RemotePropertySet.USERNAME)
public final String userName;
@Property(RemotePropertySet.PASSWORD)
public final String password;
@Deprecated // backward compatibility
private String password;
private String passwordScrambled;

public PasswordProtectedAdapterCargo(String userName, String password) {
this.password = password;
this.password = null;
this.passwordScrambled = Scrambler.scramble(password);
this.userName = userName;
}

@Property(RemotePropertySet.PASSWORD)
public String getPassword() {
return Scrambler.descramble(passwordScrambled);
}

private Object readResolve() {
// backward compatibility
if(passwordScrambled == null && password != null){
passwordScrambled = Scrambler.scramble(password);
password = null;
}
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected Container getContainer(ConfigurationFactory configFactory, ContainerFa
AbstractRuntimeConfiguration config = (AbstractRuntimeConfiguration) configFactory.createConfiguration(id, ContainerType.REMOTE, ConfigurationType.RUNTIME);
configure(config);
config.setProperty(RemotePropertySet.USERNAME, userName);
config.setProperty(RemotePropertySet.PASSWORD, password);
config.setProperty(RemotePropertySet.PASSWORD, getPassword());
config.setProperty(GeneralPropertySet.HOSTNAME, hostname);

AbstractRemoteContainer container = (AbstractRemoteContainer) containerFactory.createContainer(id, ContainerType.REMOTE, config);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package hudson.plugins.deploy;

import hudson.util.XStream2;
import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;
import org.junit.matchers.JUnitMatchers;

public class PasswordProtectedAdapterCargoTest {
@Test
public void testDeserializeOldPlainPassword () {
String plainPassword = "plain-password";
String oldXml = "<hudson.plugins.deploy.glassfish.GlassFish3xAdapter><userName>manager</userName><password>"
+ plainPassword + "</password><home>/</home><hostname></hostname></hudson.plugins.deploy.glassfish.GlassFish3xAdapter>";
XStream2 xs = new XStream2();

PasswordProtectedAdapterCargo adapter = (PasswordProtectedAdapterCargo)xs.fromXML(oldXml);
Assert.assertEquals(plainPassword, adapter.getPassword());

String newXml = xs.toXML(adapter);
Assert.assertThat("Password should be scrambled", newXml, CoreMatchers.not(JUnitMatchers.containsString(plainPassword)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void testConfigure() {
Assert.assertEquals(adapter.home, home);
// Assert.assertEquals(adapter.adminPort, port);
Assert.assertEquals(adapter.userName, username);
Assert.assertEquals(adapter.password, password);
Assert.assertEquals(adapter.getPassword(), password);

ConfigurationFactory configFactory = new DefaultConfigurationFactory();
ContainerFactory containerFactory = new DefaultContainerFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testConfigure() {
Assert.assertEquals(adapter.home, home);
// Assert.assertEquals(adapter.adminPort, port);
Assert.assertEquals(adapter.userName, username);
Assert.assertEquals(adapter.password, password);
Assert.assertEquals(adapter.getPassword(), password);

ConfigurationFactory configFactory = new DefaultConfigurationFactory();
ContainerFactory containerFactory = new DefaultContainerFactory();
Expand All @@ -59,7 +59,7 @@ public void testConfigureRemote() {
Assert.assertNull("Expexted adapter.home to be null", remoteAdapter.home);
// Assert.assertEquals(remoteAdapter.adminPort, adminPort);
Assert.assertEquals(remoteAdapter.userName, username);
Assert.assertEquals(remoteAdapter.password, password);
Assert.assertEquals(remoteAdapter.getPassword(), password);
Assert.assertEquals(remoteAdapter.hostname, hostname);

ConfigurationFactory configFactory = new DefaultConfigurationFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public void testContainerId() {
public void testConfigure() {
Assert.assertEquals(adapter.url,url);
Assert.assertEquals(adapter.userName,username);
Assert.assertEquals(adapter.password,password);
Assert.assertEquals(adapter.getPassword(),password);
}
}