Commit 46e6d24a by Dave Syer

Add basic authentication for config server

Fixes gh-42
parent 8ff3462a
...@@ -35,32 +35,42 @@ import com.netflix.discovery.DiscoveryClient; ...@@ -35,32 +35,42 @@ import com.netflix.discovery.DiscoveryClient;
* @author Dave Syer * @author Dave Syer
* *
*/ */
@ConditionalOnClass({DiscoveryClient.class, ConfigServicePropertySourceLocator.class}) @ConditionalOnClass({ DiscoveryClient.class, ConfigServicePropertySourceLocator.class })
@ConditionalOnExpression("${spring.cloud.bootstrap.config.useDiscovery:false}") @ConditionalOnExpression("${spring.cloud.bootstrap.useDiscovery:false}")
@Configuration @Configuration
@EnableEurekaClient @EnableEurekaClient
@Import(EurekaClientAutoConfiguration.class) @Import(EurekaClientAutoConfiguration.class)
@Slf4j @Slf4j
public class DiscoveryClientConfigServiceBootstrapConfiguration implements ApplicationListener<ContextRefreshedEvent> { public class DiscoveryClientConfigServiceBootstrapConfiguration implements
ApplicationListener<ContextRefreshedEvent> {
private static final String DEFAULT_CONFIG_SERVER = "CONFIGSERVER"; private static final String DEFAULT_CONFIG_SERVER = "CONFIGSERVER";
@Autowired @Autowired
private DiscoveryClient client; private DiscoveryClient client;
@Autowired @Autowired
private ConfigServicePropertySourceLocator delegate; private ConfigServicePropertySourceLocator delegate;
@Override @Override
public void onApplicationEvent(ContextRefreshedEvent event) { public void onApplicationEvent(ContextRefreshedEvent event) {
try { try {
log.info("Locating configserver via discovery"); log.info("Locating configserver via discovery");
InstanceInfo server = client.getNextServerFromEureka(DEFAULT_CONFIG_SERVER, false); InstanceInfo server = client.getNextServerFromEureka(DEFAULT_CONFIG_SERVER,
delegate.setUri(server.getHomePageUrl()); false);
} catch (Exception e) { String url = server.getHomePageUrl();
if (server.getMetadata().containsKey("password")) {
String user = server.getMetadata().get("user");
user = user == null ? "user" : user;
delegate.setUsername(user);
String password = server.getMetadata().get("password");
delegate.setPassword(password);
}
delegate.setUri(url);
}
catch (Exception e) {
log.warn("Could not locate configserver via discovery", e); log.warn("Could not locate configserver via discovery", e);
} }
} }
} }
...@@ -131,6 +131,7 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered { ...@@ -131,6 +131,7 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered {
@Bean @Bean
@Lazy @Lazy
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS)
@ConditionalOnMissingBean(com.netflix.discovery.DiscoveryClient.class)
public com.netflix.discovery.DiscoveryClient eurekaDiscoveryClient() { public com.netflix.discovery.DiscoveryClient eurekaDiscoveryClient() {
return DiscoveryManager.getInstance().getDiscoveryClient(); return DiscoveryManager.getInstance().getDiscoveryClient();
} }
......
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.netflix.config;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.DiscoveryClient;
/**
* @author Dave Syer
*
*/
public class DiscoveryClientConfigServiceBootstrapConfigurationTests {
private AnnotationConfigApplicationContext context;
private DiscoveryClient client = Mockito.mock(DiscoveryClient.class);
private InstanceInfo info = InstanceInfo.Builder.newBuilder().setAppName("app")
.setHostName("foo").setHomePageUrl("/", null).build();
@After
public void close() {
if (context != null) {
context.close();
}
}
@Test
public void offByDefault() throws Exception {
context = new AnnotationConfigApplicationContext(
DiscoveryClientConfigServiceBootstrapConfiguration.class);
assertEquals(0, context.getBeanNamesForType(DiscoveryClient.class).length);
assertEquals(
0,
context.getBeanNamesForType(DiscoveryClientConfigServiceBootstrapConfiguration.class).length);
}
@Test
public void onWhenRequested() throws Exception {
Mockito.when(client.getNextServerFromEureka("CONFIGSERVER", false)).thenReturn(
info);
setup("spring.cloud.bootstrap.useDiscovery=true");
assertEquals(
1,
context.getBeanNamesForType(DiscoveryClientConfigServiceBootstrapConfiguration.class).length);
Mockito.verify(client).getNextServerFromEureka("CONFIGSERVER", false);
ConfigServicePropertySourceLocator locator = context
.getBean(ConfigServicePropertySourceLocator.class);
assertEquals("http://foo:7001/", locator.getUri());
}
@Test
public void setsPasssword() throws Exception {
info.getMetadata().put("password", "bar");
Mockito.when(client.getNextServerFromEureka("CONFIGSERVER", false)).thenReturn(
info);
setup("spring.cloud.bootstrap.useDiscovery=true");
ConfigServicePropertySourceLocator locator = context
.getBean(ConfigServicePropertySourceLocator.class);
assertEquals("http://foo:7001/", locator.getUri());
assertEquals("bar", locator.getPassword());
assertEquals("user", locator.getUsername());
}
private void setup(String... env) {
context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, env);
context.getDefaultListableBeanFactory().registerSingleton("mockDiscoveryClient",
client);
context.register(PropertyPlaceholderAutoConfiguration.class,
DiscoveryClientConfigServiceBootstrapConfiguration.class,
ConfigServicePropertySourceLocator.class);
context.refresh();
}
}
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