Search in sources :

Example 16 with XXServiceConfigMap

use of org.apache.ranger.entity.XXServiceConfigMap in project ranger by apache.

the class TestRangerServiceService method test4GetPopulatedViewObject.

@Test
public void test4GetPopulatedViewObject() {
    XXServiceConfigMapDao xServiceConfigMapDao = Mockito.mock(XXServiceConfigMapDao.class);
    XXPortalUserDao xPortalUserDao = Mockito.mock(XXPortalUserDao.class);
    XXServiceDefDao xServiceDefDao = Mockito.mock(XXServiceDefDao.class);
    XXService xService = xService();
    String name = "fdfdfds";
    List<XXServiceConfigMap> svcConfigMapList = new ArrayList<XXServiceConfigMap>();
    XXServiceConfigMap xConfMap = new XXServiceConfigMap();
    xConfMap.setAddedByUserId(null);
    xConfMap.setConfigkey(name);
    xConfMap.setConfigvalue(name);
    xConfMap.setCreateTime(new Date());
    xConfMap.setServiceId(null);
    xConfMap.setUpdatedByUserId(null);
    xConfMap.setUpdateTime(new Date());
    svcConfigMapList.add(xConfMap);
    XXPortalUser tUser = new XXPortalUser();
    tUser.setAddedByUserId(userId);
    tUser.setCreateTime(new Date());
    tUser.setEmailAddress("test@gmail.com");
    tUser.setFirstName(name);
    tUser.setId(userId);
    tUser.setLastName(name);
    XXServiceDef xServiceDef = new XXServiceDef();
    xServiceDef.setAddedByUserId(userId);
    xServiceDef.setCreateTime(new Date());
    xServiceDef.setDescription("test");
    xServiceDef.setGuid("1427365526516_835_0");
    xServiceDef.setId(userId);
    XXServiceVersionInfoDao xServiceVersionInfoDao = Mockito.mock(XXServiceVersionInfoDao.class);
    XXServiceVersionInfo serviceVersionInfo = new XXServiceVersionInfo();
    serviceVersionInfo.setServiceId(xService.getId());
    serviceVersionInfo.setPolicyVersion(xService.getPolicyVersion());
    serviceVersionInfo.setPolicyUpdateTime(xService.getPolicyUpdateTime());
    serviceVersionInfo.setTagVersion(xService.getTagVersion());
    serviceVersionInfo.setTagUpdateTime(xService.getTagUpdateTime());
    Mockito.when(daoManager.getXXServiceVersionInfo()).thenReturn(xServiceVersionInfoDao);
    Mockito.when(xServiceVersionInfoDao.findByServiceId(xService.getId())).thenReturn(serviceVersionInfo);
    Mockito.when(daoManager.getXXPortalUser()).thenReturn(xPortalUserDao);
    Mockito.when(xPortalUserDao.getById(userId)).thenReturn(tUser);
    Mockito.when(daoManager.getXXServiceDef()).thenReturn(xServiceDefDao);
    Mockito.when(xServiceDefDao.getById(xService.getType())).thenReturn(xServiceDef);
    Mockito.when(daoManager.getXXServiceConfigMap()).thenReturn(xServiceConfigMapDao);
    Mockito.when(xServiceConfigMapDao.findByServiceId(xService.getId())).thenReturn(svcConfigMapList);
    RangerService dbService = serviceService.getPopulatedViewObject(xService);
    Assert.assertNotNull(dbService);
    Assert.assertEquals(userId, dbService.getId());
    Assert.assertEquals(xService.getAddedByUserId(), dbService.getId());
    Assert.assertEquals(xService.getId(), dbService.getId());
    Assert.assertEquals(xService.getDescription(), dbService.getDescription());
    Assert.assertEquals(xService.getGuid(), dbService.getGuid());
    Assert.assertEquals(xService.getName(), dbService.getName());
    Assert.assertEquals(xService.getPolicyUpdateTime(), dbService.getPolicyUpdateTime());
    Assert.assertEquals(xService.getPolicyVersion(), dbService.getPolicyVersion());
    Assert.assertEquals(xService.getVersion(), dbService.getVersion());
    Mockito.verify(daoManager).getXXServiceDef();
    Mockito.verify(daoManager).getXXServiceConfigMap();
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) ArrayList(java.util.ArrayList) XXServiceConfigMapDao(org.apache.ranger.db.XXServiceConfigMapDao) Date(java.util.Date) XXServiceConfigMap(org.apache.ranger.entity.XXServiceConfigMap) XXPortalUser(org.apache.ranger.entity.XXPortalUser) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) XXPortalUserDao(org.apache.ranger.db.XXPortalUserDao) RangerService(org.apache.ranger.plugin.model.RangerService) XXService(org.apache.ranger.entity.XXService) XXServiceVersionInfo(org.apache.ranger.entity.XXServiceVersionInfo) XXServiceVersionInfoDao(org.apache.ranger.db.XXServiceVersionInfoDao) Test(org.junit.Test)

Example 17 with XXServiceConfigMap

use of org.apache.ranger.entity.XXServiceConfigMap in project ranger by apache.

the class ServiceDBStore method createService.

@Override
public RangerService createService(RangerService service) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceDBStore.createService(" + service + ")");
    }
    if (service == null) {
        throw restErrorUtil.createRESTException("Service object cannot be null.", MessageEnums.ERROR_CREATING_OBJECT);
    }
    boolean createDefaultPolicy = true;
    Map<String, String> configs = service.getConfigs();
    Map<String, String> validConfigs = validateRequiredConfigParams(service, configs);
    if (validConfigs == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("==> ConfigParams cannot be null, ServiceDBStore.createService(" + service + ")");
        }
        throw restErrorUtil.createRESTException("ConfigParams cannot be null.", MessageEnums.ERROR_CREATING_OBJECT);
    }
    // While creating, value of version should be 1.
    service.setVersion(Long.valueOf(1));
    service.setTagVersion(Long.valueOf(1));
    if (populateExistingBaseFields) {
        svcServiceWithAssignedId.setPopulateExistingBaseFields(true);
        daoMgr.getXXService().setIdentityInsert(true);
        service = svcServiceWithAssignedId.create(service);
        daoMgr.getXXService().setIdentityInsert(false);
        daoMgr.getXXService().updateSequence();
        svcServiceWithAssignedId.setPopulateExistingBaseFields(false);
        createDefaultPolicy = false;
    } else {
        service = svcService.create(service);
    }
    XXService xCreatedService = daoMgr.getXXService().getById(service.getId());
    VXUser vXUser = null;
    XXServiceConfigMapDao xConfMapDao = daoMgr.getXXServiceConfigMap();
    for (Entry<String, String> configMap : validConfigs.entrySet()) {
        String configKey = configMap.getKey();
        String configValue = configMap.getValue();
        if (StringUtils.equalsIgnoreCase(configKey, "username")) {
            String userName = stringUtil.getValidUserName(configValue);
            XXUser xxUser = daoMgr.getXXUser().findByUserName(userName);
            if (xxUser != null) {
                vXUser = xUserService.populateViewBean(xxUser);
            } else {
                UserSessionBase usb = ContextUtil.getCurrentUserSession();
                if (usb != null && !usb.isUserAdmin() && !usb.isSpnegoEnabled()) {
                    throw restErrorUtil.createRESTException("User does not exist with given username: [" + userName + "] please use existing user", MessageEnums.OPER_NO_PERMISSION);
                }
                vXUser = xUserMgr.createServiceConfigUser(userName);
            }
        }
        if (StringUtils.equalsIgnoreCase(configKey, CONFIG_KEY_PASSWORD)) {
            Joiner joiner = Joiner.on(",").skipNulls();
            String iv = PasswordUtils.generateIvIfNeeded(CRYPT_ALGO);
            String cryptConfigString = joiner.join(CRYPT_ALGO, ENCRYPT_KEY, SALT, ITERATION_COUNT, iv, configValue);
            String encryptedPwd = PasswordUtils.encryptPassword(cryptConfigString);
            String paddedEncryptedPwd = joiner.join(CRYPT_ALGO, ENCRYPT_KEY, SALT, ITERATION_COUNT, iv, encryptedPwd);
            String decryptedPwd = PasswordUtils.decryptPassword(paddedEncryptedPwd);
            if (StringUtils.equals(decryptedPwd, configValue)) {
                configValue = paddedEncryptedPwd;
            }
        }
        XXServiceConfigMap xConfMap = new XXServiceConfigMap();
        xConfMap = rangerAuditFields.populateAuditFields(xConfMap, xCreatedService);
        xConfMap.setServiceId(xCreatedService.getId());
        xConfMap.setConfigkey(configKey);
        if (StringUtils.equalsIgnoreCase(configKey, "username")) {
            configValue = stringUtil.getValidUserName(configValue);
        }
        xConfMap.setConfigvalue(configValue);
        xConfMap = xConfMapDao.create(xConfMap);
    }
    updateTabPermissions(service.getType(), validConfigs);
    if (LOG.isDebugEnabled()) {
        LOG.debug("vXUser:[" + vXUser + "]");
    }
    RangerService createdService = svcService.getPopulatedViewObject(xCreatedService);
    if (createdService == null) {
        throw restErrorUtil.createRESTException("Could not create service - Internal error ", MessageEnums.ERROR_CREATING_OBJECT);
    }
    dataHistService.createObjectDataHistory(createdService, RangerDataHistService.ACTION_CREATE);
    List<XXTrxLog> trxLogList = svcService.getTransactionLog(createdService, RangerServiceService.OPERATION_CREATE_CONTEXT);
    bizUtil.createTrxLog(trxLogList);
    if (createDefaultPolicy) {
        createDefaultPolicies(createdService);
    }
    return createdService;
}
Also used : XXUser(org.apache.ranger.entity.XXUser) Joiner(com.google.common.base.Joiner) VXString(org.apache.ranger.view.VXString) XXTrxLog(org.apache.ranger.entity.XXTrxLog) VXUser(org.apache.ranger.view.VXUser) XXServiceConfigMapDao(org.apache.ranger.db.XXServiceConfigMapDao) UserSessionBase(org.apache.ranger.common.UserSessionBase) XXServiceConfigMap(org.apache.ranger.entity.XXServiceConfigMap) RangerService(org.apache.ranger.plugin.model.RangerService) XXService(org.apache.ranger.entity.XXService)

Example 18 with XXServiceConfigMap

use of org.apache.ranger.entity.XXServiceConfigMap in project ranger by apache.

the class RangerServiceService method populateViewBean.

@Override
protected RangerService populateViewBean(XXService xService) {
    RangerService vService = super.populateViewBean(xService);
    HashMap<String, String> configs = new HashMap<String, String>();
    List<XXServiceConfigMap> svcConfigMapList = daoMgr.getXXServiceConfigMap().findByServiceId(xService.getId());
    for (XXServiceConfigMap svcConfMap : svcConfigMapList) {
        String configValue = svcConfMap.getConfigvalue();
        if (StringUtils.equalsIgnoreCase(svcConfMap.getConfigkey(), ServiceDBStore.CONFIG_KEY_PASSWORD)) {
            configValue = ServiceDBStore.HIDDEN_PASSWORD_STR;
        }
        configs.put(svcConfMap.getConfigkey(), configValue);
    }
    vService.setConfigs(configs);
    return vService;
}
Also used : XXServiceConfigMap(org.apache.ranger.entity.XXServiceConfigMap) HashMap(java.util.HashMap) RangerService(org.apache.ranger.plugin.model.RangerService)

Aggregations

XXServiceConfigMap (org.apache.ranger.entity.XXServiceConfigMap)18 RangerService (org.apache.ranger.plugin.model.RangerService)9 XXServiceConfigMapDao (org.apache.ranger.db.XXServiceConfigMapDao)8 XXService (org.apache.ranger.entity.XXService)7 VXString (org.apache.ranger.view.VXString)6 ArrayList (java.util.ArrayList)5 Date (java.util.Date)4 HashMap (java.util.HashMap)4 XXPortalUser (org.apache.ranger.entity.XXPortalUser)4 Test (org.junit.Test)4 XXPortalUserDao (org.apache.ranger.db.XXPortalUserDao)3 XXServiceDefDao (org.apache.ranger.db.XXServiceDefDao)3 XXServiceVersionInfoDao (org.apache.ranger.db.XXServiceVersionInfoDao)3 XXServiceDef (org.apache.ranger.entity.XXServiceDef)3 XXServiceVersionInfo (org.apache.ranger.entity.XXServiceVersionInfo)3 XXTrxLog (org.apache.ranger.entity.XXTrxLog)3 LinkedHashMap (java.util.LinkedHashMap)2 UserSessionBase (org.apache.ranger.common.UserSessionBase)2 XXUser (org.apache.ranger.entity.XXUser)2 PasswordUtils (org.apache.ranger.plugin.util.PasswordUtils)2