Skip to content

Commit 6ae389f

Browse files
committed
Enable login authentication for eureka
1 parent c523af6 commit 6ae389f

File tree

7 files changed

+106
-2
lines changed

7 files changed

+106
-2
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Apollo 2.1.0
3939
* [feat: use can change spring.profiles.active's value without rebuild project](https://github.com/apolloconfig/apollo/pull/4616)
4040
* [refactor: remove app.properties and move some config file's location](https://github.com/apolloconfig/apollo/pull/4637)
4141
* [Fix the problem of deleting blank items appear at the end](https://github.com/apolloconfig/apollo/pull/4662)
42+
* [Enable login authentication for eureka](https://github.com/apolloconfig/apollo/pull/4663)
4243

4344
------------------
4445
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/11?closed=1)

apollo-configservice/src/main/java/com/ctrip/framework/apollo/configservice/ConfigServerEurekaServerConfigure.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@
1616
*/
1717
package com.ctrip.framework.apollo.configservice;
1818

19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.beans.factory.annotation.Value;
1921
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2022
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
2123
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.core.annotation.Order;
25+
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
26+
import org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer;
27+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
28+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
2229

2330
/**
2431
* Start Eureka Server annotations according to configuration
@@ -29,4 +36,43 @@
2936
@EnableEurekaServer
3037
@ConditionalOnProperty(name = "apollo.eureka.server.enabled", havingValue = "true", matchIfMissing = true)
3138
public class ConfigServerEurekaServerConfigure {
39+
40+
@Order(99)
41+
@Configuration
42+
static class EurekaServerSecurityConfigurer extends WebSecurityConfigurerAdapter {
43+
44+
private static final String EUREKA_ROLE = "EUREKA";
45+
46+
@Value("${apollo.eureka.server.security.enabled:false}")
47+
private boolean eurekaSecurityEnabled;
48+
@Value("${apollo.eureka.server.security.username:}")
49+
private String username;
50+
@Value("${apollo.eureka.server.security.password:}")
51+
private String password;
52+
53+
@Override
54+
protected void configure(HttpSecurity http) throws Exception {
55+
http.csrf().disable();
56+
http.httpBasic();
57+
if (eurekaSecurityEnabled) {
58+
http.authorizeRequests()
59+
.antMatchers("/eureka/apps/**", "/eureka/instances/**", "/eureka/peerreplication/**")
60+
.hasRole(EUREKA_ROLE)
61+
.antMatchers("/**").permitAll();
62+
}
63+
}
64+
65+
@Autowired
66+
public void configureEurekaUser(AuthenticationManagerBuilder auth) throws Exception {
67+
if (!eurekaSecurityEnabled) {
68+
return;
69+
}
70+
InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder> configurer = auth
71+
.getConfigurer(InMemoryUserDetailsManagerConfigurer.class);
72+
if (configurer == null) {
73+
configurer = auth.inMemoryAuthentication();
74+
}
75+
configurer.withUser(username).password(password).roles(EUREKA_ROLE);
76+
}
77+
}
3278
}

apollo-configservice/src/test/java/com/ctrip/framework/apollo/configservice/controller/TestWebSecurityConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
2323

2424
@Configuration
25-
@Order(99)
25+
@Order(98)
2626
public class TestWebSecurityConfig extends WebSecurityConfigurerAdapter {
2727

2828
@Override

docs/en/deployment/distributed-deployment-guide.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,3 +1528,31 @@ admin-services.access.tokens=098f6bcd4621d373cade4e832627b4f6,ad0234829205b90331
15281528
> For version 2.0.0 and above
15291529
15301530
The default value is 60, in seconds. Since the key authentication needs to verify the time, there may be time deviation between the time of the client and the time of the server, if the deviation is too large, the authentication will fail, this configuration can configure the tolerated time deviation size, the default is 60 seconds.
1531+
1532+
### 3.2.9 apollo.eureka.server.security.enabled - Configure whether to enable Eureka login authentication
1533+
1534+
> For version 2.1.0 and above
1535+
1536+
The default value is false, if you want to improve security (such as when apollo is exposed to the public network), you can enable login authentication for eureka by setting this configuration to true.
1537+
1538+
Note that if eureka login authentication is enabled, the addresses in [eureka.service.url](#_321-eurekaserviceurl-eureka-service-url) needs to be configured with a user name and password, such as:
1539+
1540+
```
1541+
http://some-user-name:some-password@1.1.1.1:8080/eureka/, http://some-user-name:some-password@2.2.2.2:8080/eureka/
1542+
```
1543+
1544+
Among them, `some-user-name` and `some-password` need to be consistent with the configuration items of `apollo.eureka.server.security.username` and `apollo.eureka.server.security.password`.
1545+
1546+
### 3.2.10 apollo.eureka.server.security.username - Configure the username of Eureka server
1547+
1548+
> For version 2.1.0 and above
1549+
1550+
Configure the login username of eureka server, which needs to be used together with [apollo.eureka.server.security.enabled](#_329-apolloeurekaserversecurityenabled-configure-whether-to-enable-eureka-login-authentication).
1551+
1552+
> Note that the username cannot be configured as apollo.
1553+
1554+
### 3.2.11 apollo.eureka.server.security.password - Configure the password of Eureka server
1555+
1556+
> For version 2.1.0 and above
1557+
1558+
Configure the login password of eureka server, which needs to be used together with [apollo.eureka.server.security.enabled](#_329-apolloeurekaserversecurityenabled-configure-whether-to-enable-eureka-login-authentication).

docs/en/usage/apollo-user-guide.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,4 +504,5 @@ In addition to user permissions, system access also needs to be considered in te
504504

505505
1. `apollo-configservice` and `apollo-adminservice` are designed based on the intranet trusted network, so for security reasons, `apollo-configservice` and `apollo-adminservice` are prohibited from being exposed directly to the public network
506506
2. For sensitive configurations, consider enabling [access secret key](en/usage/apollo-user-guide?id=_62-configuring-access-keys) so that only authenticated clients can access sensitive configurations
507-
3. 1.7.1 and above can consider enabling [access control](en/deployment/distributed-deployment-guide?id=_326-admin-servicesaccesscontrolenabled-configure-whether-apollo-adminservice-has-access-control-enabled) for `apollo-adminservice`, so that only [controlled](en/deployment/distributed-deployment-guide?id=_3112-admin-servicesaccesstokens-set-the-access-token-required-by-apollo-portal-to-access-the-apollo-adminservice-for-each-environment) `apollo-portal` can access the corresponding interface to enhance security
507+
3. version 1.7.1 and above can consider enabling [access control](en/deployment/distributed-deployment-guide?id=_326-admin-servicesaccesscontrolenabled-configure-whether-apollo-adminservice-has-access-control-enabled) for `apollo-adminservice`, so that only [controlled](en/deployment/distributed-deployment-guide?id=_3112-admin-servicesaccesstokens-set-the-access-token-required-by-apollo-portal-to-access-the-apollo-adminservice-for-each-environment) `apollo-portal` can access the corresponding interface to enhance security
508+
4. version 2.1.0 and above can consider enabling [access control](en/deployment/distributed-deployment-guide?id=_329-apolloeurekaserversecurityenabled-configure-whether-to-enable-eureka-login-authentication) for `eureka`, so that only controlled `apollo-configservice` and `apollo-adminservice` can be registered to `eureka` to enhance security

docs/zh/deployment/distributed-deployment-guide.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,3 +1470,30 @@ admin-service.access.tokens=098f6bcd4621d373cade4e832627b4f6,ad0234829205b903319
14701470
> 适用于2.0.0及以上版本
14711471
14721472
默认值为60,单位为秒。由于密钥认证时需要校验时间,客户端与服务端的时间可能存在时间偏差,如果偏差太大会导致认证失败,此配置可以配置容忍的时间偏差大小,默认为60秒。
1473+
1474+
### 3.2.9 apollo.eureka.server.security.enabled - 配置是否开启eureka server的登录认证
1475+
1476+
> 适用于2.1.0及以上版本
1477+
1478+
默认为false,如果希望提升安全性(比如公网可访问的场景),可以设置该配置项为true启用登录认证。
1479+
1480+
需要注意的是,开启登录认证后,[eureka.service.url](#_321-eurekaserviceurl-eureka服务url)中的地址需要配置用户名和密码,如:
1481+
1482+
```
1483+
http://some-user-name:some-password@1.1.1.1:8080/eureka/,http://some-user-name:some-password@2.2.2.2:8080/eureka/
1484+
```
1485+
其中`some-user-name``some-password`需要和`apollo.eureka.server.security.username`以及`apollo.eureka.server.security.password`的配置项一致。
1486+
1487+
### 3.2.10 apollo.eureka.server.security.username - 配置eureka server的登录用户名
1488+
1489+
> 适用于2.1.0及以上版本
1490+
1491+
配置eureka server的登录用户名,需要和[apollo.eureka.server.security.enabled](#_329-apolloeurekaserversecurityenabled-配置是否开启eureka-server的登录认证)一起使用。
1492+
1493+
> 注意用户名不能配置为apollo
1494+
1495+
### 3.2.11 apollo.eureka.server.security.password - 配置eureka server的登录密码
1496+
1497+
> 适用于2.1.0及以上版本
1498+
1499+
配置eureka server的登录密码,需要和[apollo.eureka.server.security.enabled](#_329-apolloeurekaserversecurityenabled-配置是否开启eureka-server的登录认证)一起使用。

docs/zh/usage/apollo-user-guide.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,3 +477,4 @@ Apollo 支持细粒度的权限控制,请务必根据实际情况做好权限
477477
1. `apollo-configservice``apollo-adminservice`是基于内网可信网络设计的,所以出于安全考虑,禁止`apollo-configservice``apollo-adminservice`直接暴露在公网
478478
2. 对敏感配置可以考虑开启[访问秘钥](#_62-%e9%85%8d%e7%bd%ae%e8%ae%bf%e9%97%ae%e5%af%86%e9%92%a5),从而只有经过身份验证的客户端才能访问敏感配置
479479
3. 1.7.1及以上版本可以考虑为`apollo-adminservice`开启[访问控制](zh/deployment/distributed-deployment-guide?id=_326-admin-serviceaccesscontrolenabled-配置apollo-adminservice是否开启访问控制),从而只有[受控的](zh/deployment/distributed-deployment-guide?id=_3112-admin-serviceaccesstokens-设置apollo-portal访问各环境apollo-adminservice所需的access-token)`apollo-portal`才能访问对应接口,增强安全性
480+
4. 2.1.0及以上版本可以考虑为`eureka`开启[访问控制](zh/deployment/distributed-deployment-guide?id=_329-apolloeurekaserversecurityenabled-配置是否开启eureka-server的登录认证),从而只有受控的`apollo-configservice``apollo-adminservice`可以注册到`eureka`,增强安全性

0 commit comments

Comments
 (0)