use of org.apache.ranger.view.VXResponse in project ranger by apache.
the class RangerAuthFailureHandler method onAuthenticationFailure.
/*
* (non-Javadoc)
*
* @see org.springframework.security.web.authentication.
* ExceptionMappingAuthenticationFailureHandler
* #onAuthenticationFailure(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse,
* org.springframework.security.core.AuthenticationException)
*/
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
String ajaxRequestHeader = request.getHeader("X-Requested-With");
if (logger.isDebugEnabled()) {
logger.debug("commence() X-Requested-With=" + ajaxRequestHeader);
}
response.setContentType("application/json;charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("X-Frame-Options", "DENY");
String jsonResp = "";
try {
String msg = exception.getMessage();
VXResponse vXResponse = new VXResponse();
if (msg != null && !msg.isEmpty()) {
if (CLIUtil.getMessage("AbstractUserDetailsAuthenticationProvider.badCredentials", request).equalsIgnoreCase(msg)) {
vXResponse.setStatusCode(HttpServletResponse.SC_UNAUTHORIZED);
vXResponse.setMsgDesc("The username or password you entered is incorrect.");
logger.info("Error Message : " + msg);
} else if (msg.contains("Could not get JDBC Connection; nested exception is java.sql.SQLException: Connections could not be acquired from the underlying database!")) {
vXResponse.setStatusCode(HttpServletResponse.SC_UNAUTHORIZED);
vXResponse.setMsgDesc("Unable to connect to DB.");
} else if (msg.contains("Communications link failure")) {
vXResponse.setStatusCode(HttpServletResponse.SC_UNAUTHORIZED);
vXResponse.setMsgDesc("Unable to connect to DB.");
} else if (CLIUtil.getMessage("AbstractUserDetailsAuthenticationProvider.disabled", request).equalsIgnoreCase(msg)) {
vXResponse.setStatusCode(HttpServletResponse.SC_UNAUTHORIZED);
vXResponse.setMsgDesc("The username or password you entered is disabled.");
}
}
jsonResp = jsonUtil.writeObjectAsString(vXResponse);
response.getWriter().write(jsonResp);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
} catch (IOException e) {
logger.info("Error while writing JSON in HttpServletResponse");
}
if (ajaxRequestHeader != null && "XMLHttpRequest".equalsIgnoreCase(ajaxRequestHeader)) {
// response);
if (logger.isDebugEnabled()) {
logger.debug("Sending login failed response : " + jsonResp);
}
}
// else {
// super.onAuthenticationFailure(request, response, exception);
// }
}
use of org.apache.ranger.view.VXResponse in project ranger by apache.
the class TestXUserMgr method test128DeleteXGroup.
@Test
public void test128DeleteXGroup() {
destroySession();
setup();
boolean force = true;
VXGroup vXGroup = vxGroup();
VXPermMapList vXPermMapList = new VXPermMapList();
VXPermMap vXPermMap1 = getVXPermMap();
List<VXPermMap> vXPermMaps = new ArrayList<VXPermMap>();
vXPermMaps.add(vXPermMap1);
vXPermMapList.setVXPermMaps(vXPermMaps);
VXAuditMapList vXAuditMapList = new VXAuditMapList();
List<VXAuditMap> vXAuditMaps = new ArrayList<VXAuditMap>();
VXAuditMap vXAuditMap = getVXAuditMap();
vXAuditMaps.add(vXAuditMap);
vXAuditMapList.setVXAuditMaps(vXAuditMaps);
List<XXGroupGroup> xXGroupGroups = new ArrayList<XXGroupGroup>();
XXGroupGroup xXGroupGroup = xxGroupGroup();
xXGroupGroups.add(xXGroupGroup);
List<XXGroupPermission> xXGroupPermissions = new ArrayList<XXGroupPermission>();
XXGroupPermission xGroupPermissionObj = xxGroupPermission();
xXGroupPermissions.add(xGroupPermissionObj);
List<XXPolicy> xXPolicyList = new ArrayList<XXPolicy>();
XXPolicy xXPolicy = getXXPolicy();
xXPolicyList.add(xXPolicy);
List<XXResource> xResourceList = new ArrayList<XXResource>();
XXResource xXResource = new XXResource();
xXResource.setId(userId);
xXResource.setName("hadoopdev");
xXResource.setIsRecursive(AppConstants.BOOL_TRUE);
xXResource.setResourceStatus(AppConstants.STATUS_ENABLED);
xResourceList.add(xXResource);
List<XXSecurityZoneRefGroup> zoneSecRefGroup = new ArrayList<XXSecurityZoneRefGroup>();
XXSecurityZoneRefGroupDao zoneSecRefGroupDao = Mockito.mock(XXSecurityZoneRefGroupDao.class);
Mockito.when(daoManager.getXXSecurityZoneRefGroup()).thenReturn(zoneSecRefGroupDao);
Mockito.when(zoneSecRefGroupDao.findByGroupId(userId)).thenReturn(zoneSecRefGroup);
List<XXRoleRefGroup> roleRefGroup = new ArrayList<XXRoleRefGroup>();
XXRoleRefGroup xRoleRefGroup = new XXRoleRefGroup();
xRoleRefGroup.setRoleId(userId);
xRoleRefGroup.setGroupId(userId);
xRoleRefGroup.setGroupName(groupName);
xRoleRefGroup.setGroupType(0);
roleRefGroup.add(xRoleRefGroup);
XXRoleRefGroupDao roleRefGroupDao = Mockito.mock(XXRoleRefGroupDao.class);
Mockito.when(daoManager.getXXRoleRefGroup()).thenReturn(roleRefGroupDao);
Mockito.when(roleRefGroupDao.findByGroupId(userId)).thenReturn(roleRefGroup);
XXRole xRole = new XXRole();
xRole.setId(userId);
xRole.setName("Role1");
XXRoleDao roleDao = Mockito.mock(XXRoleDao.class);
Mockito.when(daoManager.getXXRole()).thenReturn(roleDao);
Mockito.when(roleDao.getById(xRoleRefGroup.getRoleId())).thenReturn(xRole);
VXResponse vXResponse = new VXResponse();
vXResponse.setStatusCode(HttpServletResponse.SC_BAD_REQUEST);
vXResponse.setMsgDesc("Can Not Delete Group '" + vXGroup.getName().trim() + "' as its present in " + RangerConstants.ROLE_FIELD);
Mockito.when(restErrorUtil.generateRESTException((VXResponse) Mockito.any())).thenThrow(new WebApplicationException());
thrown.expect(WebApplicationException.class);
xUserMgr.deleteXGroup(vXGroup.getId(), force);
}
use of org.apache.ranger.view.VXResponse in project ranger by apache.
the class TestXUserMgr method test93searchXAuditMap.
@Test
public void test93searchXAuditMap() {
SearchCriteria testSearchCriteria = createsearchCriteria();
testSearchCriteria.addParam("xUserId", userId);
VXResource vxresource = new VXResource();
VXAuditMap vXAuditMap = getVXAuditMap();
List<VXAuditMap> vXAuditMapList = new ArrayList<VXAuditMap>();
vXAuditMapList.add(vXAuditMap);
VXAuditMapList auditMapList = new VXAuditMapList();
auditMapList.setVXAuditMaps(vXAuditMapList);
Mockito.when(xAuditMapService.searchXAuditMaps((SearchCriteria) Mockito.any())).thenReturn(auditMapList);
List<XXResource> xResourceList = new ArrayList<XXResource>();
XXResource xRes = new XXResource();
xRes.setId(userId);
xRes.setName("hadoopdev");
xRes.setIsRecursive(AppConstants.BOOL_TRUE);
xRes.setResourceStatus(AppConstants.STATUS_ENABLED);
xResourceList.add(xRes);
XXResourceDao xxResourceDao = Mockito.mock(XXResourceDao.class);
Mockito.when(daoManager.getXXResource()).thenReturn(xxResourceDao);
Mockito.when(xxResourceDao.getById(Mockito.anyLong())).thenReturn(xRes);
Mockito.when(xResourceService.populateViewBean(xRes)).thenReturn(vxresource);
VXResponse vXResponse = new VXResponse();
vXResponse.setStatusCode(VXResponse.STATUS_SUCCESS);
Mockito.when(msBizUtil.hasPermission(vxresource, AppConstants.XA_PERM_TYPE_ADMIN)).thenReturn(vXResponse);
VXAuditMapList returnList = xUserMgr.searchXAuditMaps(testSearchCriteria);
Assert.assertNotNull(returnList);
Assert.assertEquals(auditMapList.getListSize(), returnList.getListSize());
}
use of org.apache.ranger.view.VXResponse in project ranger by apache.
the class TestUserMgr method test03ChangePasswordAsAdmin.
@Test
public void test03ChangePasswordAsAdmin() {
setup();
XXPortalUserDao userDao = Mockito.mock(XXPortalUserDao.class);
VXPortalUser userProfile = userProfile();
VXPasswordChange pwdChange = new VXPasswordChange();
pwdChange.setId(userProfile.getId());
pwdChange.setLoginId(userProfile.getLoginId());
pwdChange.setOldPassword(userProfile.getPassword());
pwdChange.setEmailAddress(userProfile.getEmailAddress());
pwdChange.setUpdPassword(userProfile.getPassword());
XXPortalUser user = new XXPortalUser();
Mockito.when(daoManager.getXXPortalUser()).thenReturn(userDao);
Mockito.when(userDao.findByLoginId(Mockito.nullable(String.class))).thenReturn(user);
Mockito.when(stringUtil.equals(Mockito.anyString(), Mockito.nullable(String.class))).thenReturn(true);
Mockito.when(daoManager.getXXPortalUser()).thenReturn(userDao);
Mockito.when(stringUtil.validatePassword(Mockito.anyString(), Mockito.any(String[].class))).thenReturn(true);
VXResponse dbVXResponse = userMgr.changePassword(pwdChange);
Assert.assertNotNull(dbVXResponse);
Assert.assertEquals(userProfile.getStatus(), dbVXResponse.getStatusCode());
Mockito.verify(stringUtil).equals(Mockito.anyString(), Mockito.nullable(String.class));
Mockito.verify(stringUtil).validatePassword(Mockito.anyString(), Mockito.any(String[].class));
XXPortalUser user2 = new XXPortalUser();
user2.setId(userId);
Mockito.when(userDao.findByLoginId(Mockito.anyString())).thenReturn(user2);
VXPasswordChange invalidpwdChange = new VXPasswordChange();
invalidpwdChange.setId(userProfile.getId());
invalidpwdChange.setLoginId(userProfile.getLoginId());
invalidpwdChange.setOldPassword("invalidOldPassword");
invalidpwdChange.setEmailAddress(userProfile.getEmailAddress());
invalidpwdChange.setUpdPassword(userProfile.getPassword());
thrown.expect(WebApplicationException.class);
userMgr.changePassword(invalidpwdChange);
XXPortalUser externalUser = new XXPortalUser();
externalUser.setUserSource(RangerCommonEnums.USER_EXTERNAL);
Mockito.when(userDao.findByLoginId(Mockito.anyString())).thenReturn(externalUser);
VXResponse vXResponse = new VXResponse();
vXResponse.setStatusCode(HttpServletResponse.SC_FORBIDDEN);
vXResponse.setMsgDesc("SECURITY:changePassword().Ranger External Users cannot change password. LoginId=" + pwdChange.getLoginId());
Mockito.when(restErrorUtil.generateRESTException((VXResponse) Mockito.any())).thenReturn(new WebApplicationException());
thrown.expect(WebApplicationException.class);
userMgr.changePassword(pwdChange);
}
use of org.apache.ranger.view.VXResponse in project ranger by apache.
the class TestUserMgr method test05ChangePasswordAsUser.
@Test
public void test05ChangePasswordAsUser() {
setupUser();
XXPortalUserDao userDao = Mockito.mock(XXPortalUserDao.class);
VXPortalUser userProfile = userProfile();
VXPasswordChange pwdChange = new VXPasswordChange();
pwdChange.setId(userProfile.getId());
pwdChange.setLoginId(userProfile.getLoginId());
pwdChange.setOldPassword(userProfile.getPassword());
pwdChange.setEmailAddress(userProfile.getEmailAddress());
pwdChange.setUpdPassword(userProfile.getPassword());
XXPortalUser user = new XXPortalUser();
user.setId(userProfile.getId());
user.setLoginId(userProfile.getLoginId());
Mockito.when(daoManager.getXXPortalUser()).thenReturn(userDao);
Mockito.when(userDao.findByLoginId(Mockito.anyString())).thenReturn(user);
Mockito.when(stringUtil.equals(Mockito.anyString(), Mockito.nullable(String.class))).thenReturn(true);
Mockito.when(daoManager.getXXPortalUser()).thenReturn(userDao);
Mockito.when(stringUtil.validatePassword(Mockito.anyString(), Mockito.any(String[].class))).thenReturn(true);
VXResponse dbVXResponse = userMgr.changePassword(pwdChange);
Assert.assertNotNull(dbVXResponse);
Assert.assertEquals(userProfile.getStatus(), dbVXResponse.getStatusCode());
Mockito.verify(stringUtil).equals(Mockito.anyString(), Mockito.nullable(String.class));
Mockito.verify(stringUtil).validatePassword(Mockito.anyString(), Mockito.any(String[].class));
}
Aggregations