Search in sources :

Example 51 with VXResponse

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);
// }
}
Also used : VXResponse(org.apache.ranger.view.VXResponse) IOException(java.io.IOException)

Example 52 with VXResponse

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);
}
Also used : XXSecurityZoneRefGroup(org.apache.ranger.entity.XXSecurityZoneRefGroup) VXPermMap(org.apache.ranger.view.VXPermMap) WebApplicationException(javax.ws.rs.WebApplicationException) ArrayList(java.util.ArrayList) XXSecurityZoneRefGroupDao(org.apache.ranger.db.XXSecurityZoneRefGroupDao) XXPolicy(org.apache.ranger.entity.XXPolicy) XXRoleRefGroupDao(org.apache.ranger.db.XXRoleRefGroupDao) XXRoleRefGroup(org.apache.ranger.entity.XXRoleRefGroup) VXAuditMap(org.apache.ranger.view.VXAuditMap) VXPermMapList(org.apache.ranger.view.VXPermMapList) VXResponse(org.apache.ranger.view.VXResponse) XXGroupPermission(org.apache.ranger.entity.XXGroupPermission) XXResource(org.apache.ranger.entity.XXResource) VXAuditMapList(org.apache.ranger.view.VXAuditMapList) XXRoleDao(org.apache.ranger.db.XXRoleDao) VXGroup(org.apache.ranger.view.VXGroup) XXGroupGroup(org.apache.ranger.entity.XXGroupGroup) XXRole(org.apache.ranger.entity.XXRole) Test(org.junit.Test)

Example 53 with VXResponse

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());
}
Also used : VXResponse(org.apache.ranger.view.VXResponse) XXResource(org.apache.ranger.entity.XXResource) VXResource(org.apache.ranger.view.VXResource) ArrayList(java.util.ArrayList) VXAuditMap(org.apache.ranger.view.VXAuditMap) VXAuditMapList(org.apache.ranger.view.VXAuditMapList) XXResourceDao(org.apache.ranger.db.XXResourceDao) SearchCriteria(org.apache.ranger.common.SearchCriteria) Test(org.junit.Test)

Example 54 with VXResponse

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);
}
Also used : VXResponse(org.apache.ranger.view.VXResponse) XXPortalUser(org.apache.ranger.entity.XXPortalUser) WebApplicationException(javax.ws.rs.WebApplicationException) VXPasswordChange(org.apache.ranger.view.VXPasswordChange) XXPortalUserDao(org.apache.ranger.db.XXPortalUserDao) VXPortalUser(org.apache.ranger.view.VXPortalUser) VXString(org.apache.ranger.view.VXString) Test(org.junit.Test)

Example 55 with VXResponse

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));
}
Also used : VXResponse(org.apache.ranger.view.VXResponse) XXPortalUser(org.apache.ranger.entity.XXPortalUser) VXPasswordChange(org.apache.ranger.view.VXPasswordChange) XXPortalUserDao(org.apache.ranger.db.XXPortalUserDao) VXPortalUser(org.apache.ranger.view.VXPortalUser) VXString(org.apache.ranger.view.VXString) Test(org.junit.Test)

Aggregations

VXResponse (org.apache.ranger.view.VXResponse)57 Test (org.junit.Test)25 ArrayList (java.util.ArrayList)20 WebApplicationException (javax.ws.rs.WebApplicationException)17 XXPortalUser (org.apache.ranger.entity.XXPortalUser)13 VXString (org.apache.ranger.view.VXString)11 XXPortalUserDao (org.apache.ranger.db.XXPortalUserDao)10 UserSessionBase (org.apache.ranger.common.UserSessionBase)9 XXResource (org.apache.ranger.entity.XXResource)9 VXPortalUser (org.apache.ranger.view.VXPortalUser)8 VXResource (org.apache.ranger.view.VXResource)8 VXMessage (org.apache.ranger.view.VXMessage)7 IOException (java.io.IOException)6 VXPasswordChange (org.apache.ranger.view.VXPasswordChange)6 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 RESTResponse (org.apache.ranger.admin.client.datatype.RESTResponse)5 VXAuditMap (org.apache.ranger.view.VXAuditMap)5 VXAuditMapList (org.apache.ranger.view.VXAuditMapList)5 VXPermMap (org.apache.ranger.view.VXPermMap)5