Search in sources :

Example 86 with RangerService

use of org.apache.ranger.plugin.model.RangerService 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)) {
            String cryptConfigString = CRYPT_ALGO + "," + ENCRYPT_KEY + "," + SALT + "," + ITERATION_COUNT + "," + configValue;
            String encryptedPwd = PasswordUtils.encryptPassword(cryptConfigString);
            encryptedPwd = CRYPT_ALGO + "," + ENCRYPT_KEY + "," + SALT + "," + ITERATION_COUNT + "," + encryptedPwd;
            String decryptedPwd = PasswordUtils.decryptPassword(encryptedPwd);
            if (StringUtils.equals(decryptedPwd, configValue)) {
                configValue = encryptedPwd;
            }
        }
        XXServiceConfigMap xConfMap = new XXServiceConfigMap();
        xConfMap = rangerAuditFields.populateAuditFields(xConfMap, xCreatedService);
        xConfMap.setServiceId(xCreatedService.getId());
        xConfMap.setConfigkey(configKey);
        xConfMap.setConfigvalue(configValue);
        xConfMapDao.create(xConfMap);
    }
    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) 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 87 with RangerService

use of org.apache.ranger.plugin.model.RangerService in project ranger by apache.

the class ServiceMgr method getRangerServiceByName.

public RangerBaseService getRangerServiceByName(String serviceName, ServiceStore svcStore) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceMgr.getRangerServiceByName(" + serviceName + ")");
    }
    RangerBaseService ret = null;
    RangerService service = svcStore == null ? null : svcStore.getServiceByName(serviceName);
    if (service != null) {
        ret = getRangerServiceByService(service, svcStore);
    } else {
        LOG.warn("ServiceMgr.getRangerServiceByName(" + serviceName + "): could not find the service");
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== ServiceMgr.getRangerServiceByName(" + serviceName + "): " + ret);
    }
    return ret;
}
Also used : RangerBaseService(org.apache.ranger.plugin.service.RangerBaseService) RangerService(org.apache.ranger.plugin.model.RangerService)

Example 88 with RangerService

use of org.apache.ranger.plugin.model.RangerService in project ranger by apache.

the class ServiceMgr method lookupResource.

public List<String> lookupResource(String serviceName, ResourceLookupContext context, ServiceStore svcStore) throws Exception {
    List<String> ret = null;
    rangerBizUtil.blockAuditorRoleUser();
    RangerService service = svcDBStore.getServiceByName(serviceName);
    String authType = PropertiesUtil.getProperty(AUTHENTICATION_TYPE);
    String lookupPrincipal = SecureClientLogin.getPrincipal(PropertiesUtil.getProperty(LOOKUP_PRINCIPAL), PropertiesUtil.getProperty(HOST_NAME));
    String lookupKeytab = PropertiesUtil.getProperty(LOOKUP_KEYTAB);
    String nameRules = PropertiesUtil.getProperty(NAME_RULES);
    String rangerPrincipal = SecureClientLogin.getPrincipal(PropertiesUtil.getProperty(ADMIN_USER_PRINCIPAL), PropertiesUtil.getProperty(HOST_NAME));
    String rangerkeytab = PropertiesUtil.getProperty(ADMIN_USER_KEYTAB);
    if (!StringUtils.isEmpty(authType) && KERBEROS_TYPE.equalsIgnoreCase(authType.trim()) && SecureClientLogin.isKerberosCredentialExists(lookupPrincipal, lookupKeytab)) {
        if (service != null && service.getConfigs() != null) {
            service.getConfigs().put(HadoopConfigHolder.RANGER_LOOKUP_PRINCIPAL, lookupPrincipal);
            service.getConfigs().put(HadoopConfigHolder.RANGER_LOOKUP_KEYTAB, lookupKeytab);
            service.getConfigs().put(HadoopConfigHolder.RANGER_NAME_RULES, nameRules);
            service.getConfigs().put(HadoopConfigHolder.RANGER_AUTH_TYPE, authType);
        }
    }
    if (!StringUtils.isEmpty(authType) && KERBEROS_TYPE.equalsIgnoreCase(authType.trim()) && SecureClientLogin.isKerberosCredentialExists(rangerPrincipal, rangerkeytab)) {
        if (service != null && service.getConfigs() != null) {
            service.getConfigs().put(HadoopConfigHolder.RANGER_PRINCIPAL, rangerPrincipal);
            service.getConfigs().put(HadoopConfigHolder.RANGER_KEYTAB, rangerkeytab);
            service.getConfigs().put(HadoopConfigHolder.RANGER_NAME_RULES, nameRules);
            service.getConfigs().put(HadoopConfigHolder.RANGER_AUTH_TYPE, authType);
        }
    }
    Map<String, String> newConfigs = rangerSvcService.getConfigsWithDecryptedPassword(service);
    service.setConfigs(newConfigs);
    RangerBaseService svc = getRangerServiceByService(service, svcStore);
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceMgr.lookupResource for Service: (" + svc + "Context: " + context + ")");
    }
    if (svc != null) {
        if (StringUtils.equals(svc.getServiceDef().getName(), EmbeddedServiceDefsUtil.EMBEDDED_SERVICEDEF_TAG_NAME)) {
            ret = svc.lookupResource(context);
        } else {
            LookupCallable callable = new LookupCallable(svc, context);
            long time = getTimeoutValueForLookupInMilliSeconds(svc);
            ret = timedExecutor.timedTask(callable, time, TimeUnit.MILLISECONDS);
        }
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> ServiceMgr.lookupResource for Response: (" + ret + ")");
    }
    return ret;
}
Also used : RangerBaseService(org.apache.ranger.plugin.service.RangerBaseService) RangerService(org.apache.ranger.plugin.model.RangerService)

Example 89 with RangerService

use of org.apache.ranger.plugin.model.RangerService in project ranger by apache.

the class KmsKeyMgr method getKMSUserName.

private String getKMSUserName(String srvName) throws Exception {
    RangerService rangerService = null;
    rangerService = svcStore.getServiceByName(srvName);
    return rangerService.getConfigs().get(KMS_USERNAME);
}
Also used : RangerService(org.apache.ranger.plugin.model.RangerService)

Example 90 with RangerService

use of org.apache.ranger.plugin.model.RangerService in project ranger by apache.

the class KmsKeyMgr method getKMSURL.

private String[] getKMSURL(String name) throws Exception {
    String[] providers = null;
    RangerService rangerService = null;
    try {
        rangerService = svcStore.getServiceByName(name);
        if (rangerService != null) {
            String kmsUrl = rangerService.getConfigs().get(KMS_URL_CONFIG);
            String dbKmsUrl = kmsUrl;
            if (providerList.containsKey(kmsUrl)) {
                kmsUrl = providerList.get(kmsUrl);
            } else {
                providerList.put(kmsUrl, kmsUrl);
            }
            providers = createProvider(dbKmsUrl, kmsUrl);
        } else {
            throw new Exception("Service " + name + " not found");
        }
    } catch (Exception excp) {
        logger.error("getServiceByName(" + name + ") failed", excp);
        throw new Exception("getServiceByName(" + name + ") failed", excp);
    }
    if (providers == null) {
        throw new Exception("Providers for service " + name + " not found");
    }
    return providers;
}
Also used : RangerService(org.apache.ranger.plugin.model.RangerService) URISyntaxException(java.net.URISyntaxException) UniformInterfaceException(com.sun.jersey.api.client.UniformInterfaceException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Aggregations

RangerService (org.apache.ranger.plugin.model.RangerService)163 Test (org.junit.Test)85 RangerPolicy (org.apache.ranger.plugin.model.RangerPolicy)52 ArrayList (java.util.ArrayList)39 WebApplicationException (javax.ws.rs.WebApplicationException)30 XXServiceDef (org.apache.ranger.entity.XXServiceDef)26 SearchFilter (org.apache.ranger.plugin.util.SearchFilter)24 VXString (org.apache.ranger.view.VXString)24 XXService (org.apache.ranger.entity.XXService)23 Path (javax.ws.rs.Path)22 Produces (javax.ws.rs.Produces)21 Date (java.util.Date)20 HashMap (java.util.HashMap)16 XXServiceDefDao (org.apache.ranger.db.XXServiceDefDao)16 HttpServletRequest (javax.servlet.http.HttpServletRequest)15 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)14 IOException (java.io.IOException)12 GET (javax.ws.rs.GET)12 XXTrxLog (org.apache.ranger.entity.XXTrxLog)12 RangerServiceDef (org.apache.ranger.plugin.model.RangerServiceDef)12