use of org.apache.ranger.entity.XXService 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;
}
use of org.apache.ranger.entity.XXService in project ranger by apache.
the class ServiceDBStore method getServicePolicies.
@Override
public List<RangerPolicy> getServicePolicies(Long serviceId, SearchFilter filter) throws Exception {
if (LOG.isDebugEnabled()) {
LOG.debug("==> ServiceDBStore.getServicePolicies(" + serviceId + ")");
}
XXService service = daoMgr.getXXService().getById(serviceId);
if (service == null) {
throw new Exception("service does not exist - id='" + serviceId);
}
List<RangerPolicy> ret = getServicePolicies(service, filter);
if (LOG.isDebugEnabled()) {
LOG.debug("<== ServiceDBStore.getServicePolicies(" + serviceId + ") : policy-count=" + (ret == null ? 0 : ret.size()));
}
return ret;
}
use of org.apache.ranger.entity.XXService in project ranger by apache.
the class ServiceDBStore method getServicePolicies.
@Override
public ServicePolicies getServicePolicies(String serviceName) throws Exception {
if (LOG.isDebugEnabled()) {
LOG.debug("==> ServiceDBStore.getServicePolicies(" + serviceName + ")");
}
ServicePolicies ret = null;
XXService serviceDbObj = daoMgr.getXXService().findByName(serviceName);
if (serviceDbObj == null) {
throw new Exception("service does not exist. name=" + serviceName);
}
XXServiceVersionInfo serviceVersionInfoDbObj = daoMgr.getXXServiceVersionInfo().findByServiceName(serviceName);
if (serviceVersionInfoDbObj == null) {
LOG.warn("serviceVersionInfo does not exist. name=" + serviceName);
}
RangerServiceDef serviceDef = getServiceDef(serviceDbObj.getType());
if (serviceDef == null) {
throw new Exception("service-def does not exist. id=" + serviceDbObj.getType());
}
List<RangerPolicy> policies = null;
ServicePolicies.TagPolicies tagPolicies = null;
String auditMode = getAuditMode(serviceDef.getName(), serviceName);
if (serviceDbObj.getIsenabled()) {
if (serviceDbObj.getTagService() != null) {
XXService tagServiceDbObj = daoMgr.getXXService().getById(serviceDbObj.getTagService());
if (tagServiceDbObj != null && tagServiceDbObj.getIsenabled()) {
RangerServiceDef tagServiceDef = getServiceDef(tagServiceDbObj.getType());
if (tagServiceDef == null) {
throw new Exception("service-def does not exist. id=" + tagServiceDbObj.getType());
}
XXServiceVersionInfo tagServiceVersionInfoDbObj = daoMgr.getXXServiceVersionInfo().findByServiceId(serviceDbObj.getTagService());
if (tagServiceVersionInfoDbObj == null) {
LOG.warn("serviceVersionInfo does not exist. name=" + tagServiceDbObj.getName());
}
tagPolicies = new ServicePolicies.TagPolicies();
tagPolicies.setServiceId(tagServiceDbObj.getId());
tagPolicies.setServiceName(tagServiceDbObj.getName());
tagPolicies.setPolicyVersion(tagServiceVersionInfoDbObj == null ? null : tagServiceVersionInfoDbObj.getPolicyVersion());
tagPolicies.setPolicyUpdateTime(tagServiceVersionInfoDbObj == null ? null : tagServiceVersionInfoDbObj.getPolicyUpdateTime());
tagPolicies.setPolicies(getServicePoliciesFromDb(tagServiceDbObj));
tagPolicies.setServiceDef(tagServiceDef);
tagPolicies.setAuditMode(auditMode);
}
}
policies = getServicePoliciesFromDb(serviceDbObj);
} else {
policies = new ArrayList<RangerPolicy>();
}
ret = new ServicePolicies();
ret.setServiceId(serviceDbObj.getId());
ret.setServiceName(serviceDbObj.getName());
ret.setPolicyVersion(serviceVersionInfoDbObj == null ? null : serviceVersionInfoDbObj.getPolicyVersion());
ret.setPolicyUpdateTime(serviceVersionInfoDbObj == null ? null : serviceVersionInfoDbObj.getPolicyUpdateTime());
ret.setPolicies(policies);
ret.setServiceDef(serviceDef);
ret.setAuditMode(auditMode);
ret.setTagPolicies(tagPolicies);
if (LOG.isDebugEnabled()) {
LOG.debug("<== ServiceDBStore.getServicePolicies(" + serviceName + "): count=" + ((ret == null || ret.getPolicies() == null) ? 0 : ret.getPolicies().size()));
}
return ret;
}
use of org.apache.ranger.entity.XXService in project ranger by apache.
the class TagDBStore method getServiceTagsIfUpdated.
@Override
public ServiceTags getServiceTagsIfUpdated(String serviceName, Long lastKnownVersion) throws Exception {
if (LOG.isDebugEnabled()) {
LOG.debug("==> TagDBStore.getServiceTagsIfUpdated(" + serviceName + ", " + lastKnownVersion + ")");
}
ServiceTags ret = null;
XXService xxService = daoManager.getXXService().findByName(serviceName);
if (xxService == null) {
LOG.error("Requested Service not found. serviceName=" + serviceName);
throw restErrorUtil.createRESTException(HttpServletResponse.SC_NOT_FOUND, RangerServiceNotFoundException.buildExceptionMsg(serviceName), false);
}
XXServiceVersionInfo serviceVersionInfoDbObj = daoManager.getXXServiceVersionInfo().findByServiceName(serviceName);
if (serviceVersionInfoDbObj == null) {
LOG.warn("serviceVersionInfo does not exist. name=" + serviceName);
}
if (lastKnownVersion == null || serviceVersionInfoDbObj == null || serviceVersionInfoDbObj.getTagVersion() == null || !lastKnownVersion.equals(serviceVersionInfoDbObj.getTagVersion())) {
ret = RangerServiceTagsCache.getInstance().getServiceTags(serviceName, xxService.getId(), this);
}
if (ret != null && lastKnownVersion != null && lastKnownVersion.equals(ret.getTagVersion())) {
// ServiceTags are not changed
ret = null;
}
if (LOG.isDebugEnabled()) {
RangerServiceTagsCache.getInstance().dump();
}
if (LOG.isDebugEnabled()) {
LOG.debug("<== TagDBStore.getServiceTagsIfUpdated(" + serviceName + ", " + lastKnownVersion + "): count=" + ((ret == null || ret.getTags() == null) ? 0 : ret.getTags().size()));
}
return ret;
}
use of org.apache.ranger.entity.XXService in project ranger by apache.
the class TagDBStore method deleteAllTagObjectsForService.
@Override
public void deleteAllTagObjectsForService(String serviceName) throws Exception {
if (LOG.isDebugEnabled()) {
LOG.debug("==> TagDBStore.deleteAllTagObjectsForService(" + serviceName + ")");
}
XXService service = daoManager.getXXService().findByName(serviceName);
if (service != null) {
Long serviceId = service.getId();
List<XXTagAttribute> xxTagAttributes = daoManager.getXXTagAttribute().findByServiceIdAndOwner(serviceId, RangerTag.OWNER_SERVICERESOURCE);
List<XXTag> xxTags = daoManager.getXXTag().findByServiceIdAndOwner(serviceId, RangerTag.OWNER_SERVICERESOURCE);
List<XXTagResourceMap> xxTagResourceMaps = daoManager.getXXTagResourceMap().findByServiceId(serviceId);
if (CollectionUtils.isNotEmpty(xxTagResourceMaps)) {
for (XXTagResourceMap xxTagResourceMap : xxTagResourceMaps) {
try {
daoManager.getXXTagResourceMap().remove(xxTagResourceMap);
} catch (Exception e) {
LOG.error("Error deleting RangerTagResourceMap with id=" + xxTagResourceMap.getId(), e);
throw e;
}
}
}
if (CollectionUtils.isNotEmpty(xxTagAttributes)) {
for (XXTagAttribute xxTagAttribute : xxTagAttributes) {
try {
daoManager.getXXTagAttribute().remove(xxTagAttribute);
} catch (Exception e) {
LOG.error("Error deleting RangerTagAttribute with id=" + xxTagAttribute.getId(), e);
throw e;
}
}
}
if (CollectionUtils.isNotEmpty(xxTags)) {
for (XXTag xxTag : xxTags) {
try {
daoManager.getXXTag().remove(xxTag);
} catch (Exception e) {
LOG.error("Error deleting RangerTag with id=" + xxTag.getId(), e);
throw e;
}
}
}
List<XXServiceResourceElementValue> xxServiceResourceElementValues = daoManager.getXXServiceResourceElementValue().findByServiceId(serviceId);
if (CollectionUtils.isNotEmpty(xxServiceResourceElementValues)) {
for (XXServiceResourceElementValue xxServiceResourceElementValue : xxServiceResourceElementValues) {
try {
daoManager.getXXServiceResourceElementValue().remove(xxServiceResourceElementValue);
} catch (Exception e) {
LOG.error("Error deleting ServiceResourceElementValue with id=" + xxServiceResourceElementValue.getId(), e);
throw e;
}
}
}
List<XXServiceResourceElement> xxServiceResourceElements = daoManager.getXXServiceResourceElement().findByServiceId(serviceId);
if (CollectionUtils.isNotEmpty(xxServiceResourceElements)) {
for (XXServiceResourceElement xxServiceResourceElement : xxServiceResourceElements) {
try {
daoManager.getXXServiceResourceElement().remove(xxServiceResourceElement);
} catch (Exception e) {
LOG.error("Error deleting ServiceResourceElement with id=" + xxServiceResourceElement.getId(), e);
throw e;
}
}
}
List<XXServiceResource> xxServiceResources = daoManager.getXXServiceResource().findByServiceId(serviceId);
if (CollectionUtils.isNotEmpty(xxServiceResources)) {
for (XXServiceResource xxServiceResource : xxServiceResources) {
try {
daoManager.getXXServiceResource().remove(xxServiceResource);
} catch (Exception e) {
LOG.error("Error deleting RangerServiceResource with id=" + xxServiceResource.getId(), e);
throw e;
}
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("<== TagDBStore.deleteAllTagObjectsForService(" + serviceName + ")");
}
}
Aggregations