Skip to content

Commit 6416d37

Browse files
committed
1.Change hashmap traverse to java 8 code style. 2.Add security feature in readme.
1 parent 766d6ae commit 6416d37

File tree

7 files changed

+44
-33
lines changed

7 files changed

+44
-33
lines changed

README.adoc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Kafka/ZK REST API is to provide the production-ready endpoints to perform some a
1515
* Consumer Group Lag check
1616
* Collect JMX metrics from brokers that expose JMX metrics +
1717
More details refer to https://github.com/gnuhpc/Kafka-zk-restapi/blob/master/docs/JMXCollector.adoc[JMXCollector API Specification]
18+
* Secure the REST API with Spring Security
1819
// end::base-t[]
1920

2021
image::https://raw.githubusercontent.com/gnuhpc/Kafka-zk-restapi/master/pics/ShowApi.png[API]
@@ -38,12 +39,46 @@ Change the following settings of application-home.yml in src/main/resources to v
3839
kafka.brokers
3940
zookeeper.uris
4041

42+
Change security related parameters in application config file:
43+
See below: <<_security,'How to config security'>>
44+
45+
server.security.check
46+
server.security.checkInitDelay
47+
server.security.checkSecurityInterval
48+
49+
If you want to use JMX Query Filter function, you can add your own custom filter files to JMXFilterTemplate directory in project root folder.
50+
More details refer to https://github.com/gnuhpc/Kafka-zk-restapi/blob/master/docs/JMXCollector.adoc[JMXCollector API Specification]
51+
4152
#mvn clean package -Dmaven.test.skip=true+
4253

4354
You will find zip/tar under directory: Kafka-zk-restapi/target
4455

4556
You can get it running by unzip/untaring the distribution package and run bin/start.sh
4657

58+
[[_security]]
59+
=== Security
60+
Public REST services without access control make the sensitive data under risk.Then we provide a simple authentication mechanism using Spring Security.
61+
In order to make the project lighter, we use yml file to store user information, not using database.
62+
63+
Follow the steps to enable security feature:
64+
65+
Step 1:Modify the application config file and set server.security.check to true. +
66+
67+
* server.security.check:
68+
** True: Add security for the API. Clients can access the API with valid username and password stored in security.yml, or the Swagger UI(http://127.0.0.1:8121/api) is only
69+
allowed to access.
70+
** False: All the endpoints can be accessed without authentication.
71+
* server.security.checkInitDelay: The number of seconds of init delay for the timing thread to check the security file.
72+
* server.security.checkSecurityInterval: The number of seconds of check interval for the timing thread to check the security file.
73+
74+
Step 2: Make sure security/security.yml exist in application root folder.
75+
76+
Step 3: Use user controller API to add user to security file security/security.yml. +
77+
**Notice**:
78+
79+
* The first user should be added manually. Password need to be encoded using bcrypt before saving to the yml file.For convenience, we provide CommonUtils to encode the password.
80+
* No need to restart server after adding new user or update user info. Timing thread introduced in Step 1 will refresh the user list according to your settings.
81+
4782
=== Support Kafka Version Information
4883
Currently, this rest api (master branch) supports Kafka 0.10.x brokers. The master branch is the most active branch.
4984

@@ -66,6 +101,7 @@ You can access Swagger-UI by accessing http://127.0.0.1:8121/api
66101
* kafka-controller : Kafka Api
67102
* zookeeper-controller : Zookeeper Api
68103
* collector-controller : JMX Metric Collector Api
104+
* user-controller : User management Api
69105

70106

71107
=== https://github.com/gnuhpc/Kafka-zk-restapi/blob/master/docs/definitions.adoc[Data Model Definitions for 0.10]

docs/JMXCollector.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ The response is as follows:
302302
----
303303

304304
==== How to add filter template
305-
You can add filter template yml file in the resources/JMXFilterTempalte directory. The fields of the file are the same with the query filter that noticed above.
305+
You can add filter template yml files to JMXFilterTemplate directory in project root folder. The fields of the file are the same with the query filter that noticed above.
306306

307307
Sample filter template is as follows:
308308
[source, yml]

docs/security.adoc

Lines changed: 0 additions & 2 deletions
This file was deleted.

pics/ShowApi.png

-80.7 KB
Loading

security/security.yml

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,4 @@
11
---
2-
test4:
3-
password: "$2a$10$M9g/YgjQUuEH05RZklnm3u1JWoyRApv/Bfxwe0EiOFMKl2ghQvDK2"
4-
role: "user"
5-
test2:
6-
password: "$2a$10$xUisqGbg1lC.F62bwAZ0KuIJ/ltIg.0erANK1rz/gi1qVbRzkb.KC"
7-
role: "user"
8-
test3:
9-
password: "$2a$10$5L.EQBqNKBPLsbflg3mYUuZGmH7jh6IUjB6nyecLPo1rGKLu5LXAW"
10-
role: "user"
11-
test:
12-
password: "$2a$10$tXq/tF9d5COfuRbS0eyfdOrsgv2mn.xhKT3TdWY8007dsIEhDpItS"
13-
role: "user"
142
admin:
153
password: "$2a$10$cwkLeAFbPSNWEvjnL.w2FeoEPIv.MMEb0Pk541TiuqGRHP.x8ReoK"
164
role: "admin"
17-
test1:
18-
password: "$2a$10$knZ1h7KIDJeJuJdpmspt9utL.UajESzTlox7X/DFA4YcINbb3TFdC"
19-
role: "user"
20-
tina:
21-
password: "$2a$10$e6UbRpWtOP43A/QCOmNCGuJ3BN9OoToiF0HAiZgMW3ktnzvQkjQfC"
22-
role: "string"

src/main/java/org/gnuhpc/bigdata/security/UserDetailsServiceImp.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,11 @@ private ArrayList<User> fetchUserListFromSecurtiyFile() {
6868
String securityFilePath = WebSecurityConfig.SECURITY_FILE_PATH;
6969
try {
7070
HashMap<Object, Object> accounts = CommonUtils.yamlParse(securityFilePath);
71-
Iterator iter = accounts.entrySet().iterator();
72-
while (iter.hasNext()) {
73-
HashMap.Entry entry = (HashMap.Entry) iter.next();
74-
String username = (String)entry.getKey();
75-
Map<String, String> userInfo = (Map)entry.getValue();
71+
accounts.forEach((key, value)->{
72+
String username = (String)key;
73+
Map<String, String> userInfo = (Map)value;
7674
userList.add(new User(username, userInfo.get("password"), userInfo.get("role")));
77-
}
75+
});
7876
} catch (IOException ioException) {
7977
log.error("Security file process exception.", ioException);
8078
}

src/main/java/org/gnuhpc/bigdata/service/UserService.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,9 @@ public List<String> listUser() {
2424
List<String> userList = new ArrayList<>();
2525
try {
2626
accounts = CommonUtils.yamlParse(WebSecurityConfig.SECURITY_FILE_PATH);
27-
Iterator iterator = accounts.entrySet().iterator();
28-
while (iterator.hasNext()) {
29-
Map.Entry entry = (Map.Entry) iterator.next();
30-
String username = (String)entry.getKey();
31-
userList.add(username);
32-
}
27+
accounts.forEach((username, value)->{
28+
userList.add((String)username);
29+
});
3330
} catch (IOException ioException) {
3431
log.error("Failed to get user list. Reason : " + ioException.getLocalizedMessage());
3532
}

0 commit comments

Comments
 (0)