Skip to content

Commit 0371701

Browse files
author
astroshim
committed
add more credential apis.
1 parent cfe677b commit 0371701

File tree

4 files changed

+156
-11
lines changed

4 files changed

+156
-11
lines changed

zeppelin-interpreter/src/main/java/org/apache/zeppelin/user/Credentials.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,33 @@ public UserCredentials getUserCredentials(String username) {
6161
}
6262

6363
public void putUserCredentials(String username, UserCredentials uc) throws IOException {
64-
credentialsMap.put(username, uc);
64+
synchronized (credentialsMap) {
65+
credentialsMap.put(username, uc);
66+
}
67+
saveCredentials();
68+
}
69+
70+
public UserCredentials removeUserCredentials(String username) throws IOException {
71+
UserCredentials uc;
72+
synchronized (credentialsMap) {
73+
uc = credentialsMap.remove(username);
74+
}
75+
saveCredentials();
76+
return uc;
77+
}
78+
79+
public boolean removeCredentialEntity(String username, String entity) throws IOException {
80+
UserCredentials uc = credentialsMap.get(username);
81+
if (uc != null && uc.existUsernamePassword(entity) == false) {
82+
return false;
83+
}
84+
85+
uc.removeUsernamePassword(entity);
86+
saveCredentials();
87+
return true;
88+
}
89+
90+
public void saveCredentials() throws IOException {
6591
if (credentialsPersist) {
6692
saveToFile();
6793
}
@@ -118,5 +144,4 @@ private void saveToFile() throws IOException {
118144
LOG.error("Error saving credentials file", e);
119145
}
120146
}
121-
122147
}

zeppelin-interpreter/src/main/java/org/apache/zeppelin/user/UserCredentials.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,21 @@ public UsernamePassword getUsernamePassword(String entity) {
3535
}
3636

3737
public void putUsernamePassword(String entity, UsernamePassword up) {
38-
userCredentials.put(entity, up);
38+
synchronized (userCredentials) {
39+
userCredentials.put(entity, up);
40+
}
41+
}
42+
43+
public void removeUsernamePassword(String entity) {
44+
synchronized (userCredentials) {
45+
userCredentials.remove(entity);
46+
}
47+
}
48+
49+
public boolean existUsernamePassword(String entity) {
50+
synchronized (userCredentials) {
51+
return userCredentials.containsKey(entity);
52+
}
3953
}
4054

4155
@Override

zeppelin-server/src/main/java/org/apache/zeppelin/rest/CredentialRestApi.java

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,34 +50,86 @@ public class CredentialRestApi {
5050
private HttpServletRequest servReq;
5151

5252
public CredentialRestApi() {
53-
5453
}
5554

5655
public CredentialRestApi(Credentials credentials) {
5756
this.credentials = credentials;
5857
}
5958

6059
/**
61-
* Update credentials for current user
60+
* Put User Credentials REST API
61+
* @param message - JSON with entity, username, password.
62+
* @return JSON with status.OK
63+
* @throws IOException, IllegalArgumentException
6264
*/
6365
@PUT
64-
public Response putCredentials(String message) throws IOException {
66+
public Response putCredentials(String message) throws IOException, IllegalArgumentException {
6567
Map<String, String> messageMap = gson.fromJson(message,
6668
new TypeToken<Map<String, String>>(){}.getType());
6769
String entity = messageMap.get("entity");
6870
String username = messageMap.get("username");
6971
String password = messageMap.get("password");
7072

7173
if (entity == null || username == null || password == null) {
72-
return new JsonResponse(Status.BAD_REQUEST, "", "").build();
74+
return new JsonResponse(Status.BAD_REQUEST).build();
7375
}
7476

7577
String user = SecurityUtils.getPrincipal();
7678
logger.info("Update credentials for user {} entity {}", user, entity);
7779
UserCredentials uc = credentials.getUserCredentials(user);
7880
uc.putUsernamePassword(entity, new UsernamePassword(username, password));
7981
credentials.putUserCredentials(user, uc);
80-
return new JsonResponse(Status.OK, "", "").build();
82+
return new JsonResponse(Status.OK).build();
83+
}
84+
85+
/**
86+
* Get User Credentials list REST API
87+
* @param
88+
* @return JSON with status.OK
89+
* @throws IOException, IllegalArgumentException
90+
*/
91+
@GET
92+
public Response getCredentials(String message) throws
93+
IOException, IllegalArgumentException {
94+
String user = SecurityUtils.getPrincipal();
95+
logger.info("getCredentials credentials for user {} ", user);
96+
UserCredentials uc = credentials.getUserCredentials(user);
97+
return new JsonResponse(Status.OK, uc).build();
98+
}
99+
100+
/**
101+
* Remove User Credentials REST API
102+
* @param
103+
* @return JSON with status.OK
104+
* @throws IOException, IllegalArgumentException
105+
*/
106+
@DELETE
107+
public Response removeCredentials(String message) throws
108+
IOException, IllegalArgumentException {
109+
String user = SecurityUtils.getPrincipal();
110+
logger.info("removeCredentials credentials for user {} ", user);
111+
UserCredentials uc = credentials.removeUserCredentials(user);
112+
if (uc == null) {
113+
return new JsonResponse(Status.NOT_FOUND).build();
114+
}
115+
return new JsonResponse(Status.OK).build();
81116
}
82117

118+
/**
119+
* Remove Entity of User Credential entity REST API
120+
* @param
121+
* @return JSON with status.OK
122+
* @throws IOException, IllegalArgumentException
123+
*/
124+
@DELETE
125+
@Path("{entity}")
126+
public Response removeCredentialEntity(@PathParam("entity") String entity) throws
127+
IOException, IllegalArgumentException {
128+
String user = SecurityUtils.getPrincipal();
129+
logger.info("removeCredentialEntity for user {} entity {}", user, entity);
130+
if (credentials.removeCredentialEntity(user, entity) == false) {
131+
return new JsonResponse(Status.NOT_FOUND).build();
132+
}
133+
return new JsonResponse(Status.OK).build();
134+
}
83135
}

zeppelin-server/src/test/java/org/apache/zeppelin/rest/CredentialsRestApiTest.java

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,26 @@
1919

2020
import com.google.gson.Gson;
2121
import com.google.gson.reflect.TypeToken;
22+
import org.apache.commons.httpclient.methods.DeleteMethod;
2223
import org.apache.commons.httpclient.methods.GetMethod;
2324
import org.apache.commons.httpclient.methods.PutMethod;
25+
import org.apache.zeppelin.notebook.Note;
26+
import org.apache.zeppelin.server.ZeppelinServer;
27+
import org.apache.zeppelin.user.UserCredentials;
28+
import org.apache.zeppelin.utils.SecurityUtils;
2429
import org.junit.AfterClass;
2530
import org.junit.BeforeClass;
2631
import org.junit.Test;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
2734

2835
import java.io.IOException;
2936
import java.util.Map;
3037

31-
import static org.junit.Assert.assertEquals;
32-
import static org.junit.Assert.assertThat;
38+
import static org.junit.Assert.*;
3339

3440
public class CredentialsRestApiTest extends AbstractTestRestApi {
41+
protected static final Logger LOG = LoggerFactory.getLogger(CredentialsRestApiTest.class);
3542
Gson gson = new Gson();
3643

3744
@BeforeClass
@@ -72,5 +79,52 @@ public void testInvalidRequest() throws IOException {
7279
allNullPut.releaseConnection();
7380
}
7481

75-
}
82+
public Map<String, UserCredentials> testGetUserCredentials() throws IOException {
83+
GetMethod getMethod = httpGet("/credential");
84+
getMethod.addRequestHeader("Origin", "http://localhost");
85+
Map<String, Object> resp = gson.fromJson(getMethod.getResponseBodyAsString(),
86+
new TypeToken<Map<String, Object>>(){}.getType());
87+
Map<String, Object> body = (Map<String, Object>) resp.get("body");
88+
Map<String, UserCredentials> credentialMap = (Map<String, UserCredentials>)body.get("userCredentials");
89+
getMethod.releaseConnection();
90+
return credentialMap;
91+
}
92+
93+
public void testPutUserCredentials(String requestData) throws IOException {
94+
PutMethod putMethod = httpPut("/credential", requestData);
95+
putMethod.addRequestHeader("Origin", "http://localhost");
96+
assertThat(putMethod, isAllowed());
97+
putMethod.releaseConnection();
98+
}
99+
100+
public void testRemoveUserCredentials() throws IOException {
101+
DeleteMethod deleteMethod = httpDelete("/credential/");
102+
assertThat("Test delete method:", deleteMethod, isAllowed());
103+
deleteMethod.releaseConnection();
104+
}
105+
106+
public void testRemoveCredentialEntity(String entity) throws IOException {
107+
DeleteMethod deleteMethod = httpDelete("/credential/" + entity);
108+
assertThat("Test delete method:", deleteMethod, isAllowed());
109+
deleteMethod.releaseConnection();
110+
}
111+
112+
@Test
113+
public void testCredentialsAPIs() throws IOException {
114+
String requestData1 = "{\"entity\" : \"entityname\", \"username\" : \"myuser\", \"password\" : \"mypass\"}";
115+
String entity = "entityname";
116+
Map<String, UserCredentials> credentialMap;
117+
118+
testPutUserCredentials(requestData1);
119+
credentialMap = testGetUserCredentials();
120+
assertNotNull("CredentialMap should be null", credentialMap);
76121

122+
testRemoveCredentialEntity(entity);
123+
credentialMap = testGetUserCredentials();
124+
assertNull("CredentialMap should be null", credentialMap.get("entity1"));
125+
126+
testRemoveUserCredentials();
127+
credentialMap = testGetUserCredentials();
128+
assertEquals("Compare CredentialMap", credentialMap.toString(), "{}");
129+
}
130+
}

0 commit comments

Comments
 (0)