add more credential apis.

This commit is contained in:
astroshim 2016-06-17 00:05:42 +09:00
parent cfe677b827
commit 03717018b8
4 changed files with 156 additions and 11 deletions

View file

@ -61,7 +61,33 @@ public class Credentials {
}
public void putUserCredentials(String username, UserCredentials uc) throws IOException {
credentialsMap.put(username, uc);
synchronized (credentialsMap) {
credentialsMap.put(username, uc);
}
saveCredentials();
}
public UserCredentials removeUserCredentials(String username) throws IOException {
UserCredentials uc;
synchronized (credentialsMap) {
uc = credentialsMap.remove(username);
}
saveCredentials();
return uc;
}
public boolean removeCredentialEntity(String username, String entity) throws IOException {
UserCredentials uc = credentialsMap.get(username);
if (uc != null && uc.existUsernamePassword(entity) == false) {
return false;
}
uc.removeUsernamePassword(entity);
saveCredentials();
return true;
}
public void saveCredentials() throws IOException {
if (credentialsPersist) {
saveToFile();
}
@ -118,5 +144,4 @@ public class Credentials {
LOG.error("Error saving credentials file", e);
}
}
}

View file

@ -35,7 +35,21 @@ public class UserCredentials {
}
public void putUsernamePassword(String entity, UsernamePassword up) {
userCredentials.put(entity, up);
synchronized (userCredentials) {
userCredentials.put(entity, up);
}
}
public void removeUsernamePassword(String entity) {
synchronized (userCredentials) {
userCredentials.remove(entity);
}
}
public boolean existUsernamePassword(String entity) {
synchronized (userCredentials) {
return userCredentials.containsKey(entity);
}
}
@Override

View file

@ -50,7 +50,6 @@ public class CredentialRestApi {
private HttpServletRequest servReq;
public CredentialRestApi() {
}
public CredentialRestApi(Credentials credentials) {
@ -58,10 +57,13 @@ public class CredentialRestApi {
}
/**
* Update credentials for current user
* Put User Credentials REST API
* @param message - JSON with entity, username, password.
* @return JSON with status.OK
* @throws IOException, IllegalArgumentException
*/
@PUT
public Response putCredentials(String message) throws IOException {
public Response putCredentials(String message) throws IOException, IllegalArgumentException {
Map<String, String> messageMap = gson.fromJson(message,
new TypeToken<Map<String, String>>(){}.getType());
String entity = messageMap.get("entity");
@ -69,7 +71,7 @@ public class CredentialRestApi {
String password = messageMap.get("password");
if (entity == null || username == null || password == null) {
return new JsonResponse(Status.BAD_REQUEST, "", "").build();
return new JsonResponse(Status.BAD_REQUEST).build();
}
String user = SecurityUtils.getPrincipal();
@ -77,7 +79,57 @@ public class CredentialRestApi {
UserCredentials uc = credentials.getUserCredentials(user);
uc.putUsernamePassword(entity, new UsernamePassword(username, password));
credentials.putUserCredentials(user, uc);
return new JsonResponse(Status.OK, "", "").build();
return new JsonResponse(Status.OK).build();
}
/**
* Get User Credentials list REST API
* @param
* @return JSON with status.OK
* @throws IOException, IllegalArgumentException
*/
@GET
public Response getCredentials(String message) throws
IOException, IllegalArgumentException {
String user = SecurityUtils.getPrincipal();
logger.info("getCredentials credentials for user {} ", user);
UserCredentials uc = credentials.getUserCredentials(user);
return new JsonResponse(Status.OK, uc).build();
}
/**
* Remove User Credentials REST API
* @param
* @return JSON with status.OK
* @throws IOException, IllegalArgumentException
*/
@DELETE
public Response removeCredentials(String message) throws
IOException, IllegalArgumentException {
String user = SecurityUtils.getPrincipal();
logger.info("removeCredentials credentials for user {} ", user);
UserCredentials uc = credentials.removeUserCredentials(user);
if (uc == null) {
return new JsonResponse(Status.NOT_FOUND).build();
}
return new JsonResponse(Status.OK).build();
}
/**
* Remove Entity of User Credential entity REST API
* @param
* @return JSON with status.OK
* @throws IOException, IllegalArgumentException
*/
@DELETE
@Path("{entity}")
public Response removeCredentialEntity(@PathParam("entity") String entity) throws
IOException, IllegalArgumentException {
String user = SecurityUtils.getPrincipal();
logger.info("removeCredentialEntity for user {} entity {}", user, entity);
if (credentials.removeCredentialEntity(user, entity) == false) {
return new JsonResponse(Status.NOT_FOUND).build();
}
return new JsonResponse(Status.OK).build();
}
}

View file

@ -19,19 +19,26 @@ package org.apache.zeppelin.rest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.zeppelin.notebook.Note;
import org.apache.zeppelin.server.ZeppelinServer;
import org.apache.zeppelin.user.UserCredentials;
import org.apache.zeppelin.utils.SecurityUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;
public class CredentialsRestApiTest extends AbstractTestRestApi {
protected static final Logger LOG = LoggerFactory.getLogger(CredentialsRestApiTest.class);
Gson gson = new Gson();
@BeforeClass
@ -72,5 +79,52 @@ public class CredentialsRestApiTest extends AbstractTestRestApi {
allNullPut.releaseConnection();
}
}
public Map<String, UserCredentials> testGetUserCredentials() throws IOException {
GetMethod getMethod = httpGet("/credential");
getMethod.addRequestHeader("Origin", "http://localhost");
Map<String, Object> resp = gson.fromJson(getMethod.getResponseBodyAsString(),
new TypeToken<Map<String, Object>>(){}.getType());
Map<String, Object> body = (Map<String, Object>) resp.get("body");
Map<String, UserCredentials> credentialMap = (Map<String, UserCredentials>)body.get("userCredentials");
getMethod.releaseConnection();
return credentialMap;
}
public void testPutUserCredentials(String requestData) throws IOException {
PutMethod putMethod = httpPut("/credential", requestData);
putMethod.addRequestHeader("Origin", "http://localhost");
assertThat(putMethod, isAllowed());
putMethod.releaseConnection();
}
public void testRemoveUserCredentials() throws IOException {
DeleteMethod deleteMethod = httpDelete("/credential/");
assertThat("Test delete method:", deleteMethod, isAllowed());
deleteMethod.releaseConnection();
}
public void testRemoveCredentialEntity(String entity) throws IOException {
DeleteMethod deleteMethod = httpDelete("/credential/" + entity);
assertThat("Test delete method:", deleteMethod, isAllowed());
deleteMethod.releaseConnection();
}
@Test
public void testCredentialsAPIs() throws IOException {
String requestData1 = "{\"entity\" : \"entityname\", \"username\" : \"myuser\", \"password\" : \"mypass\"}";
String entity = "entityname";
Map<String, UserCredentials> credentialMap;
testPutUserCredentials(requestData1);
credentialMap = testGetUserCredentials();
assertNotNull("CredentialMap should be null", credentialMap);
testRemoveCredentialEntity(entity);
credentialMap = testGetUserCredentials();
assertNull("CredentialMap should be null", credentialMap.get("entity1"));
testRemoveUserCredentials();
credentialMap = testGetUserCredentials();
assertEquals("Compare CredentialMap", credentialMap.toString(), "{}");
}
}