Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-cloud-netflix
Commits
46e6d24a
Commit
46e6d24a
authored
Oct 25, 2014
by
Dave Syer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add basic authentication for config server
Fixes gh-42
parent
8ff3462a
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
117 additions
and
7 deletions
+117
-7
DiscoveryClientConfigServiceBootstrapConfiguration.java
...g/DiscoveryClientConfigServiceBootstrapConfiguration.java
+17
-7
EurekaClientConfiguration.java
...ework/cloud/netflix/eureka/EurekaClientConfiguration.java
+1
-0
DiscoveryClientConfigServiceBootstrapConfigurationTests.java
...coveryClientConfigServiceBootstrapConfigurationTests.java
+99
-0
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/config/DiscoveryClientConfigServiceBootstrapConfiguration.java
View file @
46e6d24a
...
...
@@ -35,13 +35,14 @@ import com.netflix.discovery.DiscoveryClient;
* @author Dave Syer
*
*/
@ConditionalOnClass
({
DiscoveryClient
.
class
,
ConfigServicePropertySourceLocator
.
class
})
@ConditionalOnExpression
(
"${spring.cloud.bootstrap.
config.
useDiscovery:false}"
)
@ConditionalOnClass
({
DiscoveryClient
.
class
,
ConfigServicePropertySourceLocator
.
class
})
@ConditionalOnExpression
(
"${spring.cloud.bootstrap.useDiscovery:false}"
)
@Configuration
@EnableEurekaClient
@Import
(
EurekaClientAutoConfiguration
.
class
)
@Slf4j
public
class
DiscoveryClientConfigServiceBootstrapConfiguration
implements
ApplicationListener
<
ContextRefreshedEvent
>
{
public
class
DiscoveryClientConfigServiceBootstrapConfiguration
implements
ApplicationListener
<
ContextRefreshedEvent
>
{
private
static
final
String
DEFAULT_CONFIG_SERVER
=
"CONFIGSERVER"
;
...
...
@@ -51,14 +52,23 @@ public class DiscoveryClientConfigServiceBootstrapConfiguration implements Appli
@Autowired
private
ConfigServicePropertySourceLocator
delegate
;
@Override
public
void
onApplicationEvent
(
ContextRefreshedEvent
event
)
{
try
{
log
.
info
(
"Locating configserver via discovery"
);
InstanceInfo
server
=
client
.
getNextServerFromEureka
(
DEFAULT_CONFIG_SERVER
,
false
);
delegate
.
setUri
(
server
.
getHomePageUrl
());
}
catch
(
Exception
e
)
{
InstanceInfo
server
=
client
.
getNextServerFromEureka
(
DEFAULT_CONFIG_SERVER
,
false
);
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
);
}
}
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientConfiguration.java
View file @
46e6d24a
...
...
@@ -131,6 +131,7 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered {
@Bean
@Lazy
@Scope
(
proxyMode
=
ScopedProxyMode
.
TARGET_CLASS
)
@ConditionalOnMissingBean
(
com
.
netflix
.
discovery
.
DiscoveryClient
.
class
)
public
com
.
netflix
.
discovery
.
DiscoveryClient
eurekaDiscoveryClient
()
{
return
DiscoveryManager
.
getInstance
().
getDiscoveryClient
();
}
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/config/DiscoveryClientConfigServiceBootstrapConfigurationTests.java
0 → 100644
View file @
46e6d24a
/*
* 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
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment