Commit d784cb6a authored by Phillip Webb's avatar Phillip Webb

Apply eclipse-formatter conventions

parent 0def4477
...@@ -74,25 +74,27 @@ import org.springframework.util.ClassUtils; ...@@ -74,25 +74,27 @@ import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for embedding an extensible shell into a Spring * {@link EnableAutoConfiguration Auto-configuration} for embedding an extensible shell
* Boot enabled application. By default a SSH daemon is started on port 2000 with a default username * into a Spring Boot enabled application. By default a SSH daemon is started on port 2000
* <code>user</code> and password (default password is logged during application startup). * with a default username <code>user</code> and password (default password is logged
* during application startup).
* *
* <p> * <p>
* This configuration will auto detect the existence of a Spring Security {@link AuthenticationManager} * This configuration will auto detect the existence of a Spring Security
* and will delegate authentication requests for shell access to this detected instance. * {@link AuthenticationManager} and will delegate authentication requests for shell
* access to this detected instance.
* *
* <p> * <p>
* To add customizations to the shell simply define beans of type {@link CRaSHPlugin} in the * To add customizations to the shell simply define beans of type {@link CRaSHPlugin} in
* application context. Those beans will get auto detected during startup and registered with the * the application context. Those beans will get auto detected during startup and
* underlying shell infrastructure. * registered with the underlying shell infrastructure.
* *
* <p> * <p>
* Additional shell commands can be implemented using the guide and documentation at * Additional shell commands can be implemented using the guide and documentation at <a
* <a href="http://www.crashub.org">crashub.org</a>. By default Boot will search for commands using * href="http://www.crashub.org">crashub.org</a>. By default Boot will search for commands
* the following classpath scanning pattern <code>classpath*:/commands/**</code>. To add different * using the following classpath scanning pattern <code>classpath*:/commands/**</code>. To
* locations or override the default use <code>shell.command_path_patterns</code> in your application * add different locations or override the default use
* configuration. * <code>shell.command_path_patterns</code> in your application configuration.
* *
* @author Christian Dupuis * @author Christian Dupuis
*/ */
...@@ -105,7 +107,6 @@ public class CrshAutoConfiguration { ...@@ -105,7 +107,6 @@ public class CrshAutoConfiguration {
@Autowired @Autowired
private CrshProperties properties; private CrshProperties properties;
@Bean @Bean
@ConditionalOnExpression("#{environment['shell.auth'] == 'jaas'}") @ConditionalOnExpression("#{environment['shell.auth'] == 'jaas'}")
@ConditionalOnMissingBean({ AuthenticationProperties.class }) @ConditionalOnMissingBean({ AuthenticationProperties.class })
...@@ -144,11 +145,10 @@ public class CrshAutoConfiguration { ...@@ -144,11 +145,10 @@ public class CrshAutoConfiguration {
@ConditionalOnMissingBean({ PluginLifeCycle.class }) @ConditionalOnMissingBean({ PluginLifeCycle.class })
public PluginLifeCycle shellBootstrap() { public PluginLifeCycle shellBootstrap() {
CrshBootstrap bs = new CrshBootstrap(); CrshBootstrap bs = new CrshBootstrap();
bs.setConfig(properties.mergeProperties(new Properties())); bs.setConfig(this.properties.mergeProperties(new Properties()));
return bs; return bs;
} }
public static class CrshBootstrap extends PluginLifeCycle { public static class CrshBootstrap extends PluginLifeCycle {
@Autowired @Autowired
...@@ -160,7 +160,6 @@ public class CrshAutoConfiguration { ...@@ -160,7 +160,6 @@ public class CrshAutoConfiguration {
@Autowired @Autowired
private ResourcePatternResolver resourceLoader; private ResourcePatternResolver resourceLoader;
@PreDestroy @PreDestroy
public void destroy() { public void destroy() {
stop(); stop();
...@@ -168,54 +167,58 @@ public class CrshAutoConfiguration { ...@@ -168,54 +167,58 @@ public class CrshAutoConfiguration {
@PostConstruct @PostConstruct
public void init() throws Exception { public void init() throws Exception {
FS commandFileSystem = createFileSystem(properties.getCommandPathPatterns()); FS commandFileSystem = createFileSystem(this.properties
FS confFileSystem = createFileSystem(properties.getConfigPathPatterns()); .getCommandPathPatterns());
FS confFileSystem = createFileSystem(this.properties.getConfigPathPatterns());
PluginDiscovery discovery = new BeanFactoryFilteringPluginDiscovery(resourceLoader.getClassLoader(), PluginDiscovery discovery = new BeanFactoryFilteringPluginDiscovery(
beanFactory, properties.getDisabledPlugins()); this.resourceLoader.getClassLoader(), this.beanFactory,
this.properties.getDisabledPlugins());
PluginContext context = new PluginContext(discovery, createPluginContextAttributes(), PluginContext context = new PluginContext(discovery,
commandFileSystem, confFileSystem, resourceLoader.getClassLoader()); createPluginContextAttributes(), commandFileSystem, confFileSystem,
this.resourceLoader.getClassLoader());
context.refresh(); context.refresh();
start(context); start(context);
} }
protected FS createFileSystem(String[] pathPatterns) throws IOException,
protected FS createFileSystem(String[] pathPatterns) throws IOException, URISyntaxException { URISyntaxException {
Assert.notNull(pathPatterns); Assert.notNull(pathPatterns);
FS cmdFS = new FS(); FS cmdFS = new FS();
for (String pathPattern : pathPatterns) { for (String pathPattern : pathPatterns) {
cmdFS.mount(new SimpleFileSystemDriver(new DirectoryHandle(pathPattern, resourceLoader))); cmdFS.mount(new SimpleFileSystemDriver(new DirectoryHandle(pathPattern,
this.resourceLoader)));
} }
return cmdFS; return cmdFS;
} }
protected Map<String, Object> createPluginContextAttributes() { protected Map<String, Object> createPluginContextAttributes() {
Map<String, Object> attributes = new HashMap<String, Object>(); Map<String, Object> attributes = new HashMap<String, Object>();
String bootVersion = CrshAutoConfiguration.class.getPackage().getImplementationVersion(); String bootVersion = CrshAutoConfiguration.class.getPackage()
.getImplementationVersion();
if (bootVersion != null) { if (bootVersion != null) {
attributes.put("spring.boot.version", bootVersion); attributes.put("spring.boot.version", bootVersion);
} }
attributes.put("spring.version", SpringVersion.getVersion()); attributes.put("spring.version", SpringVersion.getVersion());
if (beanFactory != null) { if (this.beanFactory != null) {
attributes.put("spring.beanfactory", beanFactory); attributes.put("spring.beanfactory", this.beanFactory);
} }
return attributes; return attributes;
} }
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private static class AuthenticationManagerAdapter extends CRaSHPlugin<AuthenticationPlugin> implements private static class AuthenticationManagerAdapter extends
AuthenticationPlugin<String> { CRaSHPlugin<AuthenticationPlugin> implements AuthenticationPlugin<String> {
private static final PropertyDescriptor<String> ROLES = PropertyDescriptor.create(
"auth.spring.roles", "ADMIN", "Comma separated list of roles required to access the shell");
private static final PropertyDescriptor<String> ROLES = PropertyDescriptor
.create("auth.spring.roles", "ADMIN",
"Comma separated list of roles required to access the shell");
@Autowired(required=false) @Autowired(required = false)
private AccessDecisionManager accessDecisionManager; private AccessDecisionManager accessDecisionManager;
@Autowired @Autowired
...@@ -223,22 +226,24 @@ public class CrshAutoConfiguration { ...@@ -223,22 +226,24 @@ public class CrshAutoConfiguration {
private String[] roles = new String[] { "ROLE_ADMIN" }; private String[] roles = new String[] { "ROLE_ADMIN" };
@Override @Override
public boolean authenticate(String username, String password) throws Exception { public boolean authenticate(String username, String password) throws Exception {
// Authenticate first to make credentials are valid // Authenticate first to make credentials are valid
Authentication token = new UsernamePasswordAuthenticationToken(username, password); Authentication token = new UsernamePasswordAuthenticationToken(username,
password);
try { try {
token = authenticationManager.authenticate(token); token = this.authenticationManager.authenticate(token);
} }
catch (AuthenticationException ae) { catch (AuthenticationException ae) {
return false; return false;
} }
// Test access rights if a Spring Security AccessDecisionManager is installed // Test access rights if a Spring Security AccessDecisionManager is installed
if (accessDecisionManager != null && token.isAuthenticated() && roles != null) { if (this.accessDecisionManager != null && token.isAuthenticated()
&& this.roles != null) {
try { try {
accessDecisionManager.decide(token, this, SecurityConfig.createList(roles)); this.accessDecisionManager.decide(token, this,
SecurityConfig.createList(this.roles));
} }
catch (AccessDeniedException e) { catch (AccessDeniedException e) {
return false; return false;
...@@ -266,35 +271,33 @@ public class CrshAutoConfiguration { ...@@ -266,35 +271,33 @@ public class CrshAutoConfiguration {
public void init() { public void init() {
String rolesPropertyValue = getContext().getProperty(ROLES); String rolesPropertyValue = getContext().getProperty(ROLES);
if (rolesPropertyValue != null) { if (rolesPropertyValue != null) {
this.roles = StringUtils.commaDelimitedListToStringArray(rolesPropertyValue); this.roles = StringUtils
.commaDelimitedListToStringArray(rolesPropertyValue);
} }
} }
@Override @Override
protected Iterable<PropertyDescriptor<?>> createConfigurationCapabilities() { protected Iterable<PropertyDescriptor<?>> createConfigurationCapabilities() {
return Arrays.<PropertyDescriptor<?>>asList(ROLES); return Arrays.<PropertyDescriptor<?>> asList(ROLES);
} }
} }
private static class BeanFactoryFilteringPluginDiscovery extends
private static class BeanFactoryFilteringPluginDiscovery extends ServiceLoaderDiscovery { ServiceLoaderDiscovery {
private ListableBeanFactory beanFactory; private ListableBeanFactory beanFactory;
private String[] disabledPlugins; private String[] disabledPlugins;
public BeanFactoryFilteringPluginDiscovery(ClassLoader classLoader,
public BeanFactoryFilteringPluginDiscovery(ClassLoader classLoader, ListableBeanFactory beanFactory, ListableBeanFactory beanFactory, String[] disabledPlugins)
String[] disabledPlugins)
throws NullPointerException { throws NullPointerException {
super(classLoader); super(classLoader);
this.beanFactory = beanFactory; this.beanFactory = beanFactory;
this.disabledPlugins = disabledPlugins; this.disabledPlugins = disabledPlugins;
} }
@Override @Override
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public Iterable<CRaSHPlugin<?>> getPlugins() { public Iterable<CRaSHPlugin<?>> getPlugins() {
...@@ -306,7 +309,8 @@ public class CrshAutoConfiguration { ...@@ -306,7 +309,8 @@ public class CrshAutoConfiguration {
} }
} }
Collection<CRaSHPlugin> springPlugins = beanFactory.getBeansOfType(CRaSHPlugin.class).values(); Collection<CRaSHPlugin> springPlugins = this.beanFactory.getBeansOfType(
CRaSHPlugin.class).values();
for (CRaSHPlugin<?> p : springPlugins) { for (CRaSHPlugin<?> p : springPlugins) {
if (!shouldFilter(p)) { if (!shouldFilter(p)) {
plugins.add(p); plugins.add(p);
...@@ -316,7 +320,6 @@ public class CrshAutoConfiguration { ...@@ -316,7 +320,6 @@ public class CrshAutoConfiguration {
return plugins; return plugins;
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
protected boolean shouldFilter(CRaSHPlugin<?> plugin) { protected boolean shouldFilter(CRaSHPlugin<?> plugin) {
Assert.notNull(plugin); Assert.notNull(plugin);
...@@ -325,10 +328,12 @@ public class CrshAutoConfiguration { ...@@ -325,10 +328,12 @@ public class CrshAutoConfiguration {
classes.add(plugin.getClass()); classes.add(plugin.getClass());
for (Class<?> clazz : classes) { for (Class<?> clazz : classes) {
if (disabledPlugins != null && disabledPlugins.length > 0) { if (this.disabledPlugins != null && this.disabledPlugins.length > 0) {
for (String disabledPlugin : disabledPlugins) { for (String disabledPlugin : this.disabledPlugins) {
if (ClassUtils.getShortName(clazz).equalsIgnoreCase(disabledPlugin) if (ClassUtils.getShortName(clazz).equalsIgnoreCase(
|| ClassUtils.getQualifiedName(clazz).equalsIgnoreCase(disabledPlugin)) { disabledPlugin)
|| ClassUtils.getQualifiedName(clazz).equalsIgnoreCase(
disabledPlugin)) {
return true; return true;
} }
} }
...@@ -339,19 +344,17 @@ public class CrshAutoConfiguration { ...@@ -339,19 +344,17 @@ public class CrshAutoConfiguration {
} }
private static class SimpleFileSystemDriver extends AbstractFSDriver<ResourceHandle> { private static class SimpleFileSystemDriver extends AbstractFSDriver<ResourceHandle> {
private ResourceHandle root; private ResourceHandle root;
public SimpleFileSystemDriver(ResourceHandle handle) { public SimpleFileSystemDriver(ResourceHandle handle) {
this.root = handle; this.root = handle;
} }
@Override @Override
public Iterable<ResourceHandle> children(ResourceHandle handle) throws IOException { public Iterable<ResourceHandle> children(ResourceHandle handle)
throws IOException {
if (handle instanceof DirectoryHandle) { if (handle instanceof DirectoryHandle) {
return ((DirectoryHandle) handle).members(); return ((DirectoryHandle) handle).members();
} }
...@@ -379,32 +382,30 @@ public class CrshAutoConfiguration { ...@@ -379,32 +382,30 @@ public class CrshAutoConfiguration {
@Override @Override
public Iterator<InputStream> open(ResourceHandle handle) throws IOException { public Iterator<InputStream> open(ResourceHandle handle) throws IOException {
if (handle instanceof FileHandle) { if (handle instanceof FileHandle) {
return Collections.singletonList(((FileHandle) handle).openStream()).iterator(); return Collections.singletonList(((FileHandle) handle).openStream())
.iterator();
} }
return Collections.<InputStream>emptyList().iterator(); return Collections.<InputStream> emptyList().iterator();
} }
@Override @Override
public ResourceHandle root() throws IOException { public ResourceHandle root() throws IOException {
return root; return this.root;
} }
} }
private static class DirectoryHandle extends ResourceHandle { private static class DirectoryHandle extends ResourceHandle {
private ResourcePatternResolver resourceLoader; private ResourcePatternResolver resourceLoader;
public DirectoryHandle(String name, ResourcePatternResolver resourceLoader) { public DirectoryHandle(String name, ResourcePatternResolver resourceLoader) {
super(name); super(name);
this.resourceLoader = resourceLoader; this.resourceLoader = resourceLoader;
} }
public List<ResourceHandle> members() throws IOException { public List<ResourceHandle> members() throws IOException {
Resource[] resources = resourceLoader.getResources(getName()); Resource[] resources = this.resourceLoader.getResources(getName());
List<ResourceHandle> files = new ArrayList<ResourceHandle>(); List<ResourceHandle> files = new ArrayList<ResourceHandle>();
for (Resource resource : resources) { for (Resource resource : resources) {
if (!resource.getURL().getPath().endsWith("/")) { if (!resource.getURL().getPath().endsWith("/")) {
...@@ -416,18 +417,15 @@ public class CrshAutoConfiguration { ...@@ -416,18 +417,15 @@ public class CrshAutoConfiguration {
} }
private static class FileHandle extends ResourceHandle { private static class FileHandle extends ResourceHandle {
private Resource resource; private Resource resource;
public FileHandle(String name, Resource resource) { public FileHandle(String name, Resource resource) {
super(name); super(name);
this.resource = resource; this.resource = resource;
} }
public InputStream openStream() throws IOException { public InputStream openStream() throws IOException {
return this.resource.getInputStream(); return this.resource.getInputStream();
} }
...@@ -436,25 +434,23 @@ public class CrshAutoConfiguration { ...@@ -436,25 +434,23 @@ public class CrshAutoConfiguration {
try { try {
return this.resource.lastModified(); return this.resource.lastModified();
} }
catch (IOException e) {} catch (IOException e) {
}
return -1; return -1;
} }
} }
private abstract static class ResourceHandle { private abstract static class ResourceHandle {
private String name; private String name;
public ResourceHandle(String name) { public ResourceHandle(String name) {
this.name = name; this.name = name;
} }
public String getName() { public String getName() {
return name; return this.name;
} }
} }
......
...@@ -66,7 +66,6 @@ public class CrshProperties { ...@@ -66,7 +66,6 @@ public class CrshProperties {
private Telnet telnet = new Telnet(); private Telnet telnet = new Telnet();
public String getAuth() { public String getAuth() {
return this.auth; return this.auth;
} }
...@@ -100,24 +99,25 @@ public class CrshProperties { ...@@ -100,24 +99,25 @@ public class CrshProperties {
} }
public Properties mergeProperties(Properties properties) { public Properties mergeProperties(Properties properties) {
properties = ssh.mergeProperties(properties); properties = this.ssh.mergeProperties(properties);
properties = telnet.mergeProperties(properties); properties = this.telnet.mergeProperties(properties);
properties.put(CRASH_AUTH, auth); properties.put(CRASH_AUTH, this.auth);
if (authenticationProperties != null) { if (this.authenticationProperties != null) {
properties = authenticationProperties.mergeProperties(properties); properties = this.authenticationProperties.mergeProperties(properties);
} }
if (this.commandRefreshInterval > 0) { if (this.commandRefreshInterval > 0) {
properties.put(CRASH_VFS_REFRESH_PERIOD, String.valueOf(this.commandRefreshInterval)); properties.put(CRASH_VFS_REFRESH_PERIOD,
String.valueOf(this.commandRefreshInterval));
} }
// special handling for disabling Ssh and Telnet support // special handling for disabling Ssh and Telnet support
List<String> dp = new ArrayList<String>(Arrays.asList(this.disabledPlugins)); List<String> dp = new ArrayList<String>(Arrays.asList(this.disabledPlugins));
if (!ssh.isEnabled()) { if (!this.ssh.isEnabled()) {
dp.add("org.crsh.ssh.SSHPlugin"); dp.add("org.crsh.ssh.SSHPlugin");
} }
if (!telnet.isEnabled()) { if (!this.telnet.isEnabled()) {
dp.add("org.crsh.telnet.TelnetPlugin"); dp.add("org.crsh.telnet.TelnetPlugin");
} }
this.disabledPlugins = dp.toArray(new String[dp.size()]); this.disabledPlugins = dp.toArray(new String[dp.size()]);
...@@ -130,7 +130,8 @@ public class CrshProperties { ...@@ -130,7 +130,8 @@ public class CrshProperties {
this.auth = auth; this.auth = auth;
} }
public void setAuthenticationProperties(AuthenticationProperties authenticationProperties) { public void setAuthenticationProperties(
AuthenticationProperties authenticationProperties) {
Assert.notNull(authenticationProperties); Assert.notNull(authenticationProperties);
this.authenticationProperties = authenticationProperties; this.authenticationProperties = authenticationProperties;
} }
...@@ -164,17 +165,14 @@ public class CrshProperties { ...@@ -164,17 +165,14 @@ public class CrshProperties {
this.telnet = telnet; this.telnet = telnet;
} }
public interface AuthenticationProperties extends PropertiesProvider { public interface AuthenticationProperties extends PropertiesProvider {
} }
@ConfigurationProperties(name = "shell.auth.jaas", ignoreUnknownFields = false) @ConfigurationProperties(name = "shell.auth.jaas", ignoreUnknownFields = false)
public static class JaasAuthenticationProperties implements AuthenticationProperties { public static class JaasAuthenticationProperties implements AuthenticationProperties {
private String domain = "my-domain"; private String domain = "my-domain";
@Override @Override
public Properties mergeProperties(Properties properties) { public Properties mergeProperties(Properties properties) {
properties.put(CRASH_AUTH_JAAS_DOMAIN, this.domain); properties.put(CRASH_AUTH_JAAS_DOMAIN, this.domain);
...@@ -188,13 +186,11 @@ public class CrshProperties { ...@@ -188,13 +186,11 @@ public class CrshProperties {
} }
@ConfigurationProperties(name = "shell.auth.key", ignoreUnknownFields = false) @ConfigurationProperties(name = "shell.auth.key", ignoreUnknownFields = false)
public static class KeyAuthenticationProperties implements AuthenticationProperties { public static class KeyAuthenticationProperties implements AuthenticationProperties {
private String path; private String path;
@Override @Override
public Properties mergeProperties(Properties properties) { public Properties mergeProperties(Properties properties) {
if (this.path != null) { if (this.path != null) {
...@@ -210,17 +206,16 @@ public class CrshProperties { ...@@ -210,17 +206,16 @@ public class CrshProperties {
} }
public interface PropertiesProvider { public interface PropertiesProvider {
Properties mergeProperties(Properties properties); Properties mergeProperties(Properties properties);
} }
@ConfigurationProperties(name = "shell.auth.simple", ignoreUnknownFields = false) @ConfigurationProperties(name = "shell.auth.simple", ignoreUnknownFields = false)
public static class SimpleAuthenticationProperties implements AuthenticationProperties { public static class SimpleAuthenticationProperties implements
AuthenticationProperties {
private static Log logger = LogFactory.getLog(SimpleAuthenticationProperties.class);
private static Log logger = LogFactory
.getLog(SimpleAuthenticationProperties.class);
private String username = "user"; private String username = "user";
...@@ -228,7 +223,6 @@ public class CrshProperties { ...@@ -228,7 +223,6 @@ public class CrshProperties {
private boolean defaultPassword = true; private boolean defaultPassword = true;
public boolean isDefaultPassword() { public boolean isDefaultPassword() {
return this.defaultPassword; return this.defaultPassword;
} }
...@@ -244,7 +238,8 @@ public class CrshProperties { ...@@ -244,7 +238,8 @@ public class CrshProperties {
} }
public void setPassword(String password) { public void setPassword(String password) {
if (password.startsWith("${") && password.endsWith("}") || !StringUtils.hasLength(password)) { if (password.startsWith("${") && password.endsWith("}")
|| !StringUtils.hasLength(password)) {
return; return;
} }
this.password = password; this.password = password;
...@@ -258,17 +253,17 @@ public class CrshProperties { ...@@ -258,17 +253,17 @@ public class CrshProperties {
} }
@ConfigurationProperties(name = "shell.auth.spring", ignoreUnknownFields = false) @ConfigurationProperties(name = "shell.auth.spring", ignoreUnknownFields = false)
public static class SpringAuthenticationProperties implements AuthenticationProperties { public static class SpringAuthenticationProperties implements
AuthenticationProperties {
private String[] roles = new String[] { "ROLE_ADMIN" }; private String[] roles = new String[] { "ROLE_ADMIN" };
@Override @Override
public Properties mergeProperties(Properties properties) { public Properties mergeProperties(Properties properties) {
if (this.roles != null) { if (this.roles != null) {
properties.put(CRASH_AUTH_SPRING_ROLES, StringUtils.arrayToCommaDelimitedString(this.roles)); properties.put(CRASH_AUTH_SPRING_ROLES,
StringUtils.arrayToCommaDelimitedString(this.roles));
} }
return properties; return properties;
} }
...@@ -280,7 +275,6 @@ public class CrshProperties { ...@@ -280,7 +275,6 @@ public class CrshProperties {
} }
public static class Ssh implements PropertiesProvider { public static class Ssh implements PropertiesProvider {
private boolean enabled = true; private boolean enabled = true;
...@@ -289,7 +283,6 @@ public class CrshProperties { ...@@ -289,7 +283,6 @@ public class CrshProperties {
private String port = "2000"; private String port = "2000";
public boolean isEnabled() { public boolean isEnabled() {
return this.enabled; return this.enabled;
} }
...@@ -321,14 +314,12 @@ public class CrshProperties { ...@@ -321,14 +314,12 @@ public class CrshProperties {
} }
public static class Telnet implements PropertiesProvider { public static class Telnet implements PropertiesProvider {
private boolean enabled = false; private boolean enabled = false;
private String port = "5000"; private String port = "5000";
public boolean isEnabled() { public boolean isEnabled() {
return this.enabled; return this.enabled;
} }
......
...@@ -16,13 +16,6 @@ ...@@ -16,13 +16,6 @@
package org.springframework.boot.actuate.autoconfigure; package org.springframework.boot.actuate.autoconfigure;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
...@@ -56,6 +49,13 @@ import org.springframework.security.core.AuthenticationException; ...@@ -56,6 +49,13 @@ import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/** /**
* Tests for {@link CrshAutoConfiguration}. * Tests for {@link CrshAutoConfiguration}.
* *
...@@ -77,7 +77,8 @@ public class CrshAutoConfigurationTests { ...@@ -77,7 +77,8 @@ public class CrshAutoConfigurationTests {
@Test @Test
public void testDisabledPlugins() throws Exception { public void testDisabledPlugins() throws Exception {
MockEnvironment env = new MockEnvironment(); MockEnvironment env = new MockEnvironment();
env.setProperty("shell.disabled_plugins", "GroovyREPL, termIOHandler, org.crsh.auth.AuthenticationPlugin"); env.setProperty("shell.disabled_plugins",
"GroovyREPL, termIOHandler, org.crsh.auth.AuthenticationPlugin");
this.context = new AnnotationConfigWebApplicationContext(); this.context = new AnnotationConfigWebApplicationContext();
this.context.setEnvironment(env); this.context.setEnvironment(env);
this.context.register(CrshAutoConfiguration.class); this.context.register(CrshAutoConfiguration.class);
...@@ -132,7 +133,8 @@ public class CrshAutoConfigurationTests { ...@@ -132,7 +133,8 @@ public class CrshAutoConfigurationTests {
PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class); PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class);
assertEquals(lifeCycle.getConfig().getProperty("crash.ssh.keypath"), "~/.ssh/id.pem"); assertEquals(lifeCycle.getConfig().getProperty("crash.ssh.keypath"),
"~/.ssh/id.pem");
} }
@Test @Test
...@@ -144,7 +146,8 @@ public class CrshAutoConfigurationTests { ...@@ -144,7 +146,8 @@ public class CrshAutoConfigurationTests {
PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class); PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class);
int count = 0; int count = 0;
Iterator<Resource> resources = lifeCycle.getContext().loadResources("login", ResourceKind.LIFECYCLE).iterator(); Iterator<Resource> resources = lifeCycle.getContext()
.loadResources("login", ResourceKind.LIFECYCLE).iterator();
while (resources.hasNext()) { while (resources.hasNext()) {
count++; count++;
resources.next(); resources.next();
...@@ -152,7 +155,8 @@ public class CrshAutoConfigurationTests { ...@@ -152,7 +155,8 @@ public class CrshAutoConfigurationTests {
assertEquals(1, count); assertEquals(1, count);
count = 0; count = 0;
resources = lifeCycle.getContext().loadResources("help.java", ResourceKind.COMMAND).iterator(); resources = lifeCycle.getContext()
.loadResources("help.java", ResourceKind.COMMAND).iterator();
while (resources.hasNext()) { while (resources.hasNext()) {
count++; count++;
resources.next(); resources.next();
...@@ -172,7 +176,8 @@ public class CrshAutoConfigurationTests { ...@@ -172,7 +176,8 @@ public class CrshAutoConfigurationTests {
PluginContext pluginContext = lifeCycle.getContext(); PluginContext pluginContext = lifeCycle.getContext();
int count = 0; int count = 0;
Iterator<AuthenticationPlugin> plugins = pluginContext.getPlugins(AuthenticationPlugin.class).iterator(); Iterator<AuthenticationPlugin> plugins = pluginContext.getPlugins(
AuthenticationPlugin.class).iterator();
while (plugins.hasNext()) { while (plugins.hasNext()) {
count++; count++;
plugins.next(); plugins.next();
...@@ -194,7 +199,8 @@ public class CrshAutoConfigurationTests { ...@@ -194,7 +199,8 @@ public class CrshAutoConfigurationTests {
PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class); PluginLifeCycle lifeCycle = this.context.getBean(PluginLifeCycle.class);
assertEquals(lifeCycle.getConfig().get("crash.auth"), "jaas"); assertEquals(lifeCycle.getConfig().get("crash.auth"), "jaas");
assertEquals(lifeCycle.getConfig().get("crash.auth.jaas.domain"), "my-test-domain"); assertEquals(lifeCycle.getConfig().get("crash.auth.jaas.domain"),
"my-test-domain");
} }
@Test @Test
...@@ -233,7 +239,8 @@ public class CrshAutoConfigurationTests { ...@@ -233,7 +239,8 @@ public class CrshAutoConfigurationTests {
AuthenticationPlugin<String> authenticationPlugin = null; AuthenticationPlugin<String> authenticationPlugin = null;
String authentication = lifeCycle.getConfig().getProperty("crash.auth"); String authentication = lifeCycle.getConfig().getProperty("crash.auth");
assertNotNull(authentication); assertNotNull(authentication);
for (AuthenticationPlugin plugin : lifeCycle.getContext().getPlugins(AuthenticationPlugin.class)) { for (AuthenticationPlugin plugin : lifeCycle.getContext().getPlugins(
AuthenticationPlugin.class)) {
if (authentication.equals(plugin.getName())) { if (authentication.equals(plugin.getName())) {
authenticationPlugin = plugin; authenticationPlugin = plugin;
break; break;
...@@ -272,7 +279,8 @@ public class CrshAutoConfigurationTests { ...@@ -272,7 +279,8 @@ public class CrshAutoConfigurationTests {
AuthenticationPlugin<String> authenticationPlugin = null; AuthenticationPlugin<String> authenticationPlugin = null;
String authentication = lifeCycle.getConfig().getProperty("crash.auth"); String authentication = lifeCycle.getConfig().getProperty("crash.auth");
assertNotNull(authentication); assertNotNull(authentication);
for (AuthenticationPlugin plugin : lifeCycle.getContext().getPlugins(AuthenticationPlugin.class)) { for (AuthenticationPlugin plugin : lifeCycle.getContext().getPlugins(
AuthenticationPlugin.class)) {
if (authentication.equals(plugin.getName())) { if (authentication.equals(plugin.getName())) {
authenticationPlugin = plugin; authenticationPlugin = plugin;
break; break;
...@@ -308,10 +316,15 @@ public class CrshAutoConfigurationTests { ...@@ -308,10 +316,15 @@ public class CrshAutoConfigurationTests {
return new AuthenticationManager() { return new AuthenticationManager() {
@Override @Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException { public Authentication authenticate(Authentication authentication)
if (authentication.getName().equals(USERNAME) && authentication.getCredentials().equals(PASSWORD)) { throws AuthenticationException {
authentication = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(), if (authentication.getName().equals(USERNAME)
authentication.getCredentials(), Collections.singleton(new SimpleGrantedAuthority("ROLE_ADMIN"))); && authentication.getCredentials().equals(PASSWORD)) {
authentication = new UsernamePasswordAuthenticationToken(
authentication.getPrincipal(),
authentication.getCredentials(),
Collections.singleton(new SimpleGrantedAuthority(
"ROLE_ADMIN")));
} }
else { else {
throw new BadCredentialsException("Invalid username and password"); throw new BadCredentialsException("Invalid username and password");
......
...@@ -16,12 +16,6 @@ ...@@ -16,12 +16,6 @@
package org.springframework.boot.actuate.properties; package org.springframework.boot.actuate.properties;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -37,6 +31,12 @@ import org.springframework.boot.actuate.properties.CrshProperties.SpringAuthenti ...@@ -37,6 +31,12 @@ import org.springframework.boot.actuate.properties.CrshProperties.SpringAuthenti
import org.springframework.boot.bind.RelaxedDataBinder; import org.springframework.boot.bind.RelaxedDataBinder;
import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.convert.support.DefaultConversionService;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/** /**
* Tests for {@link CrshProperties}. * Tests for {@link CrshProperties}.
* *
...@@ -48,7 +48,8 @@ public class CrshPropertiesTests { ...@@ -48,7 +48,8 @@ public class CrshPropertiesTests {
public void testBindingAuth() { public void testBindingAuth() {
CrshProperties props = new CrshProperties(); CrshProperties props = new CrshProperties();
RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell");
binder.bind(new MutablePropertyValues(Collections.singletonMap("shell.auth", "spring"))); binder.bind(new MutablePropertyValues(Collections.singletonMap("shell.auth",
"spring")));
assertFalse(binder.getBindingResult().hasErrors()); assertFalse(binder.getBindingResult().hasErrors());
assertEquals("spring", props.getAuth()); assertEquals("spring", props.getAuth());
} }
...@@ -67,7 +68,8 @@ public class CrshPropertiesTests { ...@@ -67,7 +68,8 @@ public class CrshPropertiesTests {
CrshProperties props = new CrshProperties(); CrshProperties props = new CrshProperties();
RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell");
binder.setConversionService(new DefaultConversionService()); binder.setConversionService(new DefaultConversionService());
binder.bind(new MutablePropertyValues(Collections.singletonMap("shell.command_refresh_interval", "1"))); binder.bind(new MutablePropertyValues(Collections.singletonMap(
"shell.command_refresh_interval", "1")));
assertFalse(binder.getBindingResult().hasErrors()); assertFalse(binder.getBindingResult().hasErrors());
assertEquals(1, props.getCommandRefreshInterval()); assertEquals(1, props.getCommandRefreshInterval());
} }
...@@ -77,11 +79,12 @@ public class CrshPropertiesTests { ...@@ -77,11 +79,12 @@ public class CrshPropertiesTests {
CrshProperties props = new CrshProperties(); CrshProperties props = new CrshProperties();
RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell");
binder.setConversionService(new DefaultConversionService()); binder.setConversionService(new DefaultConversionService());
binder.bind(new MutablePropertyValues(Collections.singletonMap("shell.command_path_patterns", binder.bind(new MutablePropertyValues(Collections.singletonMap(
"pattern1, pattern2"))); "shell.command_path_patterns", "pattern1, pattern2")));
assertFalse(binder.getBindingResult().hasErrors()); assertFalse(binder.getBindingResult().hasErrors());
assertEquals(2, props.getCommandPathPatterns().length); assertEquals(2, props.getCommandPathPatterns().length);
Assert.assertArrayEquals(new String[] { "pattern1", "pattern2" }, props.getCommandPathPatterns()); Assert.assertArrayEquals(new String[] { "pattern1", "pattern2" },
props.getCommandPathPatterns());
} }
@Test @Test
...@@ -89,11 +92,12 @@ public class CrshPropertiesTests { ...@@ -89,11 +92,12 @@ public class CrshPropertiesTests {
CrshProperties props = new CrshProperties(); CrshProperties props = new CrshProperties();
RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell");
binder.setConversionService(new DefaultConversionService()); binder.setConversionService(new DefaultConversionService());
binder.bind(new MutablePropertyValues(Collections.singletonMap("shell.config_path_patterns", binder.bind(new MutablePropertyValues(Collections.singletonMap(
"pattern1, pattern2"))); "shell.config_path_patterns", "pattern1, pattern2")));
assertFalse(binder.getBindingResult().hasErrors()); assertFalse(binder.getBindingResult().hasErrors());
assertEquals(2, props.getConfigPathPatterns().length); assertEquals(2, props.getConfigPathPatterns().length);
Assert.assertArrayEquals(new String[] { "pattern1", "pattern2" }, props.getConfigPathPatterns()); Assert.assertArrayEquals(new String[] { "pattern1", "pattern2" },
props.getConfigPathPatterns());
} }
@Test @Test
...@@ -101,11 +105,12 @@ public class CrshPropertiesTests { ...@@ -101,11 +105,12 @@ public class CrshPropertiesTests {
CrshProperties props = new CrshProperties(); CrshProperties props = new CrshProperties();
RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell");
binder.setConversionService(new DefaultConversionService()); binder.setConversionService(new DefaultConversionService());
binder.bind(new MutablePropertyValues(Collections.singletonMap("shell.disabled_plugins", binder.bind(new MutablePropertyValues(Collections.singletonMap(
"pattern1, pattern2"))); "shell.disabled_plugins", "pattern1, pattern2")));
assertFalse(binder.getBindingResult().hasErrors()); assertFalse(binder.getBindingResult().hasErrors());
assertEquals(2, props.getDisabledPlugins().length); assertEquals(2, props.getDisabledPlugins().length);
assertArrayEquals(new String[] { "pattern1", "pattern2" }, props.getDisabledPlugins()); assertArrayEquals(new String[] { "pattern1", "pattern2" },
props.getDisabledPlugins());
} }
@Test @Test
...@@ -269,7 +274,8 @@ public class CrshPropertiesTests { ...@@ -269,7 +274,8 @@ public class CrshPropertiesTests {
public void testBindingSpring() { public void testBindingSpring() {
SpringAuthenticationProperties props = new SpringAuthenticationProperties(); SpringAuthenticationProperties props = new SpringAuthenticationProperties();
RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell.auth.spring"); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell.auth.spring");
binder.bind(new MutablePropertyValues(Collections.singletonMap("shell.auth.spring.roles", "role1, role2"))); binder.bind(new MutablePropertyValues(Collections.singletonMap(
"shell.auth.spring.roles", "role1, role2")));
assertFalse(binder.getBindingResult().hasErrors()); assertFalse(binder.getBindingResult().hasErrors());
Properties p = new Properties(); Properties p = new Properties();
...@@ -278,5 +284,4 @@ public class CrshPropertiesTests { ...@@ -278,5 +284,4 @@ public class CrshPropertiesTests {
assertEquals("role1, role2", p.get(CrshProperties.CRASH_AUTH_SPRING_ROLES)); assertEquals("role1, role2", p.get(CrshProperties.CRASH_AUTH_SPRING_ROLES));
} }
} }
...@@ -16,11 +16,6 @@ ...@@ -16,11 +16,6 @@
package org.springframework.boot.autoconfigure.jdbc; package org.springframework.boot.autoconfigure.jdbc;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.sql.Connection; import java.sql.Connection;
import java.sql.Driver; import java.sql.Driver;
import java.sql.DriverPropertyInfo; import java.sql.DriverPropertyInfo;
...@@ -47,6 +42,10 @@ import org.springframework.jdbc.core.JdbcTemplate; ...@@ -47,6 +42,10 @@ import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/** /**
* Tests for {@link DataSourceAutoConfiguration}. * Tests for {@link DataSourceAutoConfiguration}.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment