Make shell username and password configuration properties consistent with general security properties

Now simple authentication for the crsh shell can we configured using shell.auth.simple.user.name and shell.auth.simple.user.password. This is consistent with security.user.name and security.user.password.

fixes #113
This commit is contained in:
Christian Dupuis 2013-11-22 20:37:37 +01:00
parent 86334403ed
commit 1b49605749
3 changed files with 50 additions and 28 deletions

View File

@ -298,39 +298,61 @@ public class ShellProperties {
private static Log logger = LogFactory private static Log logger = LogFactory
.getLog(SimpleAuthenticationProperties.class); .getLog(SimpleAuthenticationProperties.class);
private String username = "user"; private User user = new User();
private String password = UUID.randomUUID().toString();
private boolean defaultPassword = true;
@Override @Override
protected void applyToCrshShellConfig(Properties config) { protected void applyToCrshShellConfig(Properties config) {
config.put("crash.auth", "simple"); config.put("crash.auth", "simple");
config.put("crash.auth.simple.username", this.username); config.put("crash.auth.simple.username", this.user.getName());
config.put("crash.auth.simple.password", this.password); config.put("crash.auth.simple.password", this.user.getPassword());
if (this.defaultPassword) { if (this.user.isDefaultPassword()) {
logger.info("\n\nUsing default password for shell access: " logger.info("\n\nUsing default password for shell access: "
+ this.password + "\n\n"); + this.user.getPassword() + "\n\n");
} }
} }
boolean isDefaultPassword() { public User getUser() {
return this.defaultPassword; return this.user;
} }
public void setUsername(String username) { public void setUser(User user) {
Assert.hasLength(username, "username must have text"); this.user = user;
this.username = username;
} }
public void setPassword(String password) { public static class User {
if (password.startsWith("${") && password.endsWith("}")
|| !StringUtils.hasLength(password)) { private String name = "user";
return;
private String password = UUID.randomUUID().toString();
private boolean defaultPassword = true;
boolean isDefaultPassword() {
return this.defaultPassword;
} }
this.password = password;
this.defaultPassword = false; public String getName() {
return this.name;
}
public String getPassword() {
return this.password;
}
public void setName(String name) {
Assert.hasLength(name, "name must have text");
this.name = name;
}
public void setPassword(String password) {
if (password.startsWith("${") && password.endsWith("}")
|| !StringUtils.hasLength(password)) {
return;
}
this.password = password;
this.defaultPassword = false;
}
} }
} }

View File

@ -236,8 +236,8 @@ public class CrshAutoConfigurationTests {
public void testSimpleAuthenticationProvider() throws Exception { public void testSimpleAuthenticationProvider() throws Exception {
MockEnvironment env = new MockEnvironment(); MockEnvironment env = new MockEnvironment();
env.setProperty("shell.auth", "simple"); env.setProperty("shell.auth", "simple");
env.setProperty("shell.auth.simple.username", "user"); env.setProperty("shell.auth.simple.user.name", "user");
env.setProperty("shell.auth.simple.password", "password"); env.setProperty("shell.auth.simple.user.password", "password");
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
this.context.setEnvironment(env); this.context.setEnvironment(env);
this.context.setServletContext(new MockServletContext()); this.context.setServletContext(new MockServletContext());

View File

@ -243,8 +243,8 @@ public class ShellPropertiesTests {
RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell.auth.simple"); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell.auth.simple");
binder.setConversionService(new DefaultConversionService()); binder.setConversionService(new DefaultConversionService());
Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();
map.put("shell.auth.simple.username", "username123"); map.put("shell.auth.simple.user.name", "username123");
map.put("shell.auth.simple.password", "password123"); map.put("shell.auth.simple.user.password", "password123");
binder.bind(new MutablePropertyValues(map)); binder.bind(new MutablePropertyValues(map));
assertFalse(binder.getBindingResult().hasErrors()); assertFalse(binder.getBindingResult().hasErrors());
@ -260,9 +260,9 @@ public class ShellPropertiesTests {
SimpleAuthenticationProperties security = new SimpleAuthenticationProperties(); SimpleAuthenticationProperties security = new SimpleAuthenticationProperties();
RelaxedDataBinder binder = new RelaxedDataBinder(security, "security"); RelaxedDataBinder binder = new RelaxedDataBinder(security, "security");
binder.bind(new MutablePropertyValues(Collections.singletonMap( binder.bind(new MutablePropertyValues(Collections.singletonMap(
"shell.auth.simple.password", "${ADMIN_PASSWORD}"))); "shell.auth.simple.user.password", "${ADMIN_PASSWORD}")));
assertFalse(binder.getBindingResult().hasErrors()); assertFalse(binder.getBindingResult().hasErrors());
assertTrue(security.isDefaultPassword()); assertTrue(security.getUser().isDefaultPassword());
} }
@Test @Test
@ -270,9 +270,9 @@ public class ShellPropertiesTests {
SimpleAuthenticationProperties security = new SimpleAuthenticationProperties(); SimpleAuthenticationProperties security = new SimpleAuthenticationProperties();
RelaxedDataBinder binder = new RelaxedDataBinder(security, "security"); RelaxedDataBinder binder = new RelaxedDataBinder(security, "security");
binder.bind(new MutablePropertyValues(Collections.singletonMap( binder.bind(new MutablePropertyValues(Collections.singletonMap(
"shell.auth.simple.password", ""))); "shell.auth.simple.user.password", "")));
assertFalse(binder.getBindingResult().hasErrors()); assertFalse(binder.getBindingResult().hasErrors());
assertTrue(security.isDefaultPassword()); assertTrue(security.getUser().isDefaultPassword());
} }
@Test @Test