From 1b496057499e3f550499dfc807f1524df45c5228 Mon Sep 17 00:00:00 2001 From: Christian Dupuis Date: Fri, 22 Nov 2013 20:37:37 +0100 Subject: [PATCH] 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 --- .../actuate/properties/ShellProperties.java | 62 +++++++++++++------ .../CrshAutoConfigurationTests.java | 4 +- .../properties/ShellPropertiesTests.java | 12 ++-- 3 files changed, 50 insertions(+), 28 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/properties/ShellProperties.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/properties/ShellProperties.java index a81da13302..95e0bd5d5b 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/properties/ShellProperties.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/properties/ShellProperties.java @@ -298,39 +298,61 @@ public class ShellProperties { private static Log logger = LogFactory .getLog(SimpleAuthenticationProperties.class); - private String username = "user"; - - private String password = UUID.randomUUID().toString(); - - private boolean defaultPassword = true; + private User user = new User(); @Override protected void applyToCrshShellConfig(Properties config) { config.put("crash.auth", "simple"); - config.put("crash.auth.simple.username", this.username); - config.put("crash.auth.simple.password", this.password); - if (this.defaultPassword) { + config.put("crash.auth.simple.username", this.user.getName()); + config.put("crash.auth.simple.password", this.user.getPassword()); + if (this.user.isDefaultPassword()) { logger.info("\n\nUsing default password for shell access: " - + this.password + "\n\n"); + + this.user.getPassword() + "\n\n"); } } - boolean isDefaultPassword() { - return this.defaultPassword; + public User getUser() { + return this.user; } - public void setUsername(String username) { - Assert.hasLength(username, "username must have text"); - this.username = username; + public void setUser(User user) { + this.user = user; } - public void setPassword(String password) { - if (password.startsWith("${") && password.endsWith("}") - || !StringUtils.hasLength(password)) { - return; + public static class User { + + private String name = "user"; + + 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; + } + } } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/CrshAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/CrshAutoConfigurationTests.java index 35295d28fc..41d67aabf5 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/CrshAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/CrshAutoConfigurationTests.java @@ -236,8 +236,8 @@ public class CrshAutoConfigurationTests { public void testSimpleAuthenticationProvider() throws Exception { MockEnvironment env = new MockEnvironment(); env.setProperty("shell.auth", "simple"); - env.setProperty("shell.auth.simple.username", "user"); - env.setProperty("shell.auth.simple.password", "password"); + env.setProperty("shell.auth.simple.user.name", "user"); + env.setProperty("shell.auth.simple.user.password", "password"); this.context = new AnnotationConfigWebApplicationContext(); this.context.setEnvironment(env); this.context.setServletContext(new MockServletContext()); diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/properties/ShellPropertiesTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/properties/ShellPropertiesTests.java index 04349e0d84..7121a3f490 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/properties/ShellPropertiesTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/properties/ShellPropertiesTests.java @@ -243,8 +243,8 @@ public class ShellPropertiesTests { RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell.auth.simple"); binder.setConversionService(new DefaultConversionService()); Map map = new HashMap(); - map.put("shell.auth.simple.username", "username123"); - map.put("shell.auth.simple.password", "password123"); + map.put("shell.auth.simple.user.name", "username123"); + map.put("shell.auth.simple.user.password", "password123"); binder.bind(new MutablePropertyValues(map)); assertFalse(binder.getBindingResult().hasErrors()); @@ -260,9 +260,9 @@ public class ShellPropertiesTests { SimpleAuthenticationProperties security = new SimpleAuthenticationProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(security, "security"); binder.bind(new MutablePropertyValues(Collections.singletonMap( - "shell.auth.simple.password", "${ADMIN_PASSWORD}"))); + "shell.auth.simple.user.password", "${ADMIN_PASSWORD}"))); assertFalse(binder.getBindingResult().hasErrors()); - assertTrue(security.isDefaultPassword()); + assertTrue(security.getUser().isDefaultPassword()); } @Test @@ -270,9 +270,9 @@ public class ShellPropertiesTests { SimpleAuthenticationProperties security = new SimpleAuthenticationProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(security, "security"); binder.bind(new MutablePropertyValues(Collections.singletonMap( - "shell.auth.simple.password", ""))); + "shell.auth.simple.user.password", ""))); assertFalse(binder.getBindingResult().hasErrors()); - assertTrue(security.isDefaultPassword()); + assertTrue(security.getUser().isDefaultPassword()); } @Test