use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class TestRangerTagDefService method test10getPopulatedViewObject.
@Test
public void test10getPopulatedViewObject() {
XXTagDef xxTagDef = new XXTagDef();
xxTagDef.setId(id);
xxTagDef.setName(name);
xxTagDef.setUpdateTime(new Date());
List<XXTagAttributeDef> tagAttrDefList = new ArrayList<XXTagAttributeDef>();
XXTagAttributeDef xxTagAttributeDef = new XXTagAttributeDef();
xxTagAttributeDef.setId(id);
xxTagAttributeDef.setName(name);
tagAttrDefList.add(xxTagAttributeDef);
RangerTagDef result = rangerTagDefService.getPopulatedViewObject(xxTagDef);
Assert.assertEquals(result.getId(), tagAttrDefList.get(0).getId());
Assert.assertEquals(result.getName(), tagAttrDefList.get(0).getName());
}
use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class TestRangerTagDefService method test5getTagDefByGuid.
@Test
public void test5getTagDefByGuid() {
XXTagDef xxTagDef = null;
XXTagDefDao xXTagDefDao = Mockito.mock(XXTagDefDao.class);
Mockito.when(daoMgr.getXXTagDef()).thenReturn(xXTagDefDao);
Mockito.when(xXTagDefDao.findByGuid(guid)).thenReturn(xxTagDef);
RangerTagDef result = rangerTagDefService.getTagDefByGuid(guid);
Assert.assertNull(result);
Mockito.verify(daoMgr).getXXTagDef();
Mockito.verify(xXTagDefDao).findByGuid(guid);
}
use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class TestRangerTagDefServiceBase method test1mapViewToEntityBean.
@Test
public void test1mapViewToEntityBean() {
RangerTagDef rangerTagDef = new RangerTagDef();
rangerTagDef.setId(id);
rangerTagDef.setGuid(guid);
XXTagDef xxTagDef = new XXTagDef();
xxTagDef.setId(id);
xxTagDef.setGuid(guid);
xxTagDef.setName(name);
xxTagDef.setVersion(version);
int operationContext = 1;
XXTagDef result = rangerTagDefService.mapViewToEntityBean(rangerTagDef, xxTagDef, operationContext);
Assert.assertNotNull(result);
Assert.assertEquals(result, xxTagDef);
Assert.assertEquals(result.getGuid(), xxTagDef.getGuid());
Assert.assertEquals(result.getName(), xxTagDef.getName());
Assert.assertEquals(result.getId(), xxTagDef.getId());
Assert.assertEquals(result.getVersion(), xxTagDef.getVersion());
}
use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class ServiceTagsProcessor method delete.
private void delete(ServiceTags serviceTags) throws Exception {
if (LOG.isDebugEnabled()) {
LOG.debug("==> ServiceTagsProcessor.delete()");
}
// We dont expect any resourceId->tagId mappings in delete operation, so ignoring them if specified
List<RangerServiceResource> serviceResources = serviceTags.getServiceResources();
if (CollectionUtils.isNotEmpty(serviceResources)) {
for (RangerServiceResource serviceResource : serviceResources) {
RangerServiceResource objToDelete = null;
try {
if (StringUtils.isNotBlank(serviceResource.getGuid())) {
objToDelete = tagStore.getServiceResourceByGuid(serviceResource.getGuid());
}
if (objToDelete == null) {
if (MapUtils.isNotEmpty(serviceResource.getResourceElements())) {
RangerServiceResourceSignature serializer = new RangerServiceResourceSignature(serviceResource);
String serviceResourceSignature = serializer.getSignature();
objToDelete = tagStore.getServiceResourceByServiceAndResourceSignature(serviceResource.getServiceName(), serviceResourceSignature);
}
}
if (objToDelete != null) {
List<RangerTagResourceMap> tagResourceMaps = tagStore.getTagResourceMapsForResourceGuid(objToDelete.getGuid());
if (CollectionUtils.isNotEmpty(tagResourceMaps)) {
for (RangerTagResourceMap tagResourceMap : tagResourceMaps) {
tagStore.deleteTagResourceMap(tagResourceMap.getId());
}
}
tagStore.deleteServiceResource(objToDelete.getId());
}
} catch (Exception exception) {
LOG.error("deleteServiceResourceByGuid failed, guid=" + serviceResource.getGuid(), exception);
throw exception;
}
}
}
Map<Long, RangerTag> tagsMap = serviceTags.getTags();
if (MapUtils.isNotEmpty(tagsMap)) {
for (Map.Entry<Long, RangerTag> entry : tagsMap.entrySet()) {
RangerTag tag = entry.getValue();
try {
RangerTag objToDelete = tagStore.getTagByGuid(tag.getGuid());
if (objToDelete != null) {
tagStore.deleteTag(objToDelete.getId());
}
} catch (Exception exception) {
LOG.error("deleteTag failed, guid=" + tag.getGuid(), exception);
throw exception;
}
}
}
Map<Long, RangerTagDef> tagDefsMap = serviceTags.getTagDefinitions();
if (MapUtils.isNotEmpty(tagDefsMap)) {
for (Map.Entry<Long, RangerTagDef> entry : tagDefsMap.entrySet()) {
RangerTagDef tagDef = entry.getValue();
try {
RangerTagDef objToDelete = tagStore.getTagDefByGuid(tagDef.getGuid());
if (objToDelete != null) {
tagStore.deleteTagDef(objToDelete.getId());
}
} catch (Exception exception) {
LOG.error("deleteTagDef failed, guid=" + tagDef.getGuid(), exception);
throw exception;
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("<== ServiceTagsProcessor.delete()");
}
}
use of org.apache.ranger.plugin.model.RangerTagDef in project ranger by apache.
the class ServiceTagsProcessor method addOrUpdate.
// Map tagdef, tag, serviceResource ids to created ids and use them in tag-resource-mapping
private void addOrUpdate(ServiceTags serviceTags) throws Exception {
if (LOG.isDebugEnabled()) {
LOG.debug("==> ServiceTagsProcessor.createOrUpdate()");
}
RangerPerfTracer perfTotal = null;
RangerPerfTracer perf = null;
Map<Long, RangerTagDef> tagDefsInStore = new HashMap<Long, RangerTagDef>();
Map<Long, RangerServiceResource> resourcesInStore = new HashMap<Long, RangerServiceResource>();
if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG_ADD_OR_UPDATE)) {
perfTotal = RangerPerfTracer.getPerfTracer(PERF_LOG_ADD_OR_UPDATE, "tags.addOrUpdate()");
}
if (MapUtils.isNotEmpty(serviceTags.getTagDefinitions())) {
RangerTagDef tagDef = null;
try {
for (Map.Entry<Long, RangerTagDef> entry : serviceTags.getTagDefinitions().entrySet()) {
tagDef = entry.getValue();
RangerTagDef existing = null;
if (StringUtils.isNotEmpty(tagDef.getGuid())) {
existing = tagStore.getTagDefByGuid(tagDef.getGuid());
}
if (existing == null && StringUtils.isNotEmpty(tagDef.getName())) {
existing = tagStore.getTagDefByName(tagDef.getName());
}
RangerTagDef tagDefInStore = null;
if (existing == null) {
tagDefInStore = tagStore.createTagDef(tagDef);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("tagDef for name:" + tagDef.getName() + " exists, will not update it");
}
tagDefInStore = existing;
}
tagDefsInStore.put(entry.getKey(), tagDefInStore);
}
} catch (Exception exception) {
LOG.error("createTagDef failed, tagDef=" + tagDef, exception);
throw exception;
}
}
List<RangerServiceResource> resources = serviceTags.getServiceResources();
if (CollectionUtils.isNotEmpty(resources)) {
RangerServiceResource resource = null;
try {
for (int i = 0; i < resources.size(); i++) {
resource = resources.get(i);
RangerServiceResource existing = null;
String resourceSignature = null;
Long resourceId = resource.getId();
if (StringUtils.isNotEmpty(resource.getGuid())) {
if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG_ADD_OR_UPDATE)) {
perf = RangerPerfTracer.getPerfTracer(PERF_LOG_ADD_OR_UPDATE, "tags.search_service_resource_by_guid(" + resourceId + ")");
}
existing = tagStore.getServiceResourceByGuid(resource.getGuid());
RangerPerfTracer.logAlways(perf);
} else {
if (MapUtils.isNotEmpty(resource.getResourceElements())) {
if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG_ADD_OR_UPDATE)) {
perf = RangerPerfTracer.getPerfTracer(PERF_LOG_ADD_OR_UPDATE, "tags.search_service_resource_by_signature(" + resourceId + ")");
}
RangerServiceResourceSignature serializer = new RangerServiceResourceSignature(resource);
resourceSignature = serializer.getSignature();
resource.setResourceSignature(resourceSignature);
existing = tagStore.getServiceResourceByServiceAndResourceSignature(resource.getServiceName(), resourceSignature);
RangerPerfTracer.logAlways(perf);
}
}
RangerServiceResource resourceInStore = null;
if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG_ADD_OR_UPDATE)) {
perf = RangerPerfTracer.getPerfTracer(PERF_LOG_ADD_OR_UPDATE, "tags.createOrUpdate_service_resource(" + resourceId + ")");
}
if (existing == null) {
resourceInStore = tagStore.createServiceResource(resource);
} else if (StringUtils.isEmpty(resource.getServiceName()) || MapUtils.isEmpty(resource.getResourceElements())) {
resourceInStore = existing;
} else {
resource.setId(existing.getId());
resource.setGuid(existing.getGuid());
resourceInStore = tagStore.updateServiceResource(resource);
}
resourcesInStore.put(resourceId, resourceInStore);
RangerPerfTracer.logAlways(perf);
}
} catch (Exception exception) {
LOG.error("createServiceResource failed, resource=" + resource, exception);
throw exception;
}
}
if (MapUtils.isNotEmpty(serviceTags.getResourceToTagIds())) {
for (Map.Entry<Long, List<Long>> entry : serviceTags.getResourceToTagIds().entrySet()) {
Long resourceId = entry.getKey();
RangerServiceResource resourceInStore = resourcesInStore.get(resourceId);
if (resourceInStore == null) {
LOG.error("Resource (id=" + resourceId + ") not found. Skipping tags update");
continue;
}
if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG_ADD_OR_UPDATE)) {
perf = RangerPerfTracer.getPerfTracer(PERF_LOG_ADD_OR_UPDATE, "tags.get_tags_for_service_resource(" + resourceInStore.getId() + ")");
}
// Get all tags associated with this resourceId
List<RangerTag> associatedTags = null;
try {
associatedTags = tagStore.getTagsForResourceId(resourceInStore.getId());
} catch (Exception exception) {
LOG.error("RangerTags cannot be retrieved for resource with guid=" + resourceInStore.getGuid());
throw exception;
} finally {
RangerPerfTracer.logAlways(perf);
}
List<RangerTag> tagsToRetain = new ArrayList<RangerTag>();
boolean isAnyTagUpdated = false;
List<Long> tagIds = entry.getValue();
try {
for (Long tagId : tagIds) {
RangerTag incomingTag = MapUtils.isNotEmpty(serviceTags.getTags()) ? serviceTags.getTags().get(tagId) : null;
if (incomingTag == null) {
LOG.error("Tag (id=" + tagId + ") not found. Skipping addition of this tag for resource (id=" + resourceId + ")");
continue;
}
RangerTag matchingTag = findMatchingTag(incomingTag, associatedTags);
if (matchingTag == null) {
if (LOG.isDebugEnabled()) {
LOG.debug("Did not find matching tag for tagId=" + tagId);
}
// create new tag from incoming tag and associate it with service-resource
if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG_ADD_OR_UPDATE)) {
perf = RangerPerfTracer.getPerfTracer(PERF_LOG_ADD_OR_UPDATE, "tags.create_tag(" + tagId + ")");
}
RangerTag newTag = tagStore.createTag(incomingTag);
RangerPerfTracer.logAlways(perf);
RangerTagResourceMap tagResourceMap = new RangerTagResourceMap();
tagResourceMap.setTagId(newTag.getId());
tagResourceMap.setResourceId(resourceInStore.getId());
if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG_ADD_OR_UPDATE)) {
perf = RangerPerfTracer.getPerfTracer(PERF_LOG_ADD_OR_UPDATE, "tags.create_tagResourceMap(" + tagId + ")");
}
tagResourceMap = tagStore.createTagResourceMap(tagResourceMap);
RangerPerfTracer.logAlways(perf);
associatedTags.add(newTag);
tagsToRetain.add(newTag);
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("Found matching tag for tagId=" + tagId + ", matchingTag=" + matchingTag);
}
if (isResourcePrivateTag(incomingTag)) {
if (!isResourcePrivateTag(matchingTag)) {
// create new tag from incoming tag and associate it with service-resource
RangerTag newTag = tagStore.createTag(incomingTag);
RangerTagResourceMap tagResourceMap = new RangerTagResourceMap();
tagResourceMap.setTagId(newTag.getId());
tagResourceMap.setResourceId(resourceInStore.getId());
tagResourceMap = tagStore.createTagResourceMap(tagResourceMap);
associatedTags.add(newTag);
tagsToRetain.add(newTag);
} else {
tagsToRetain.add(matchingTag);
boolean isTagUpdateNeeded = false;
// not matching is to check if both old and new tags have empty validityPeriods
if (matchingTag.getGuid() != null && matchingTag.getGuid().equals(incomingTag.getGuid())) {
if (isMatch(incomingTag, matchingTag) && CollectionUtils.isEmpty(incomingTag.getValidityPeriods()) && CollectionUtils.isEmpty(matchingTag.getValidityPeriods())) {
if (LOG.isDebugEnabled()) {
LOG.debug("No need to update existing-tag:[" + matchingTag + "] with incoming-tag:[" + incomingTag + "]");
}
} else {
isTagUpdateNeeded = true;
}
} else {
if (CollectionUtils.isEmpty(incomingTag.getValidityPeriods()) && CollectionUtils.isEmpty(matchingTag.getValidityPeriods())) {
// Completely matched tags. No need to update
if (LOG.isDebugEnabled()) {
LOG.debug("No need to update existing-tag:[" + matchingTag + "] with incoming-tag:[" + incomingTag + "]");
}
} else {
isTagUpdateNeeded = true;
}
}
if (isTagUpdateNeeded) {
// Keep this tag, and update it with attribute-values and validity schedules from incoming tag
if (LOG.isDebugEnabled()) {
LOG.debug("Updating existing private tag with id=" + matchingTag.getId());
}
incomingTag.setId(matchingTag.getId());
tagStore.updateTag(incomingTag);
isAnyTagUpdated = true;
}
}
} else {
// shared model
if (isResourcePrivateTag(matchingTag)) {
// create new tag from incoming tag and associate it with service-resource
RangerTag newTag = tagStore.createTag(incomingTag);
RangerTagResourceMap tagResourceMap = new RangerTagResourceMap();
tagResourceMap.setTagId(newTag.getId());
tagResourceMap.setResourceId(resourceInStore.getId());
tagResourceMap = tagStore.createTagResourceMap(tagResourceMap);
associatedTags.add(newTag);
tagsToRetain.add(newTag);
} else {
// Keep this tag, but update it with attribute-values from incoming tag
tagsToRetain.add(matchingTag);
// Update shared tag with new values
incomingTag.setId(matchingTag.getId());
tagStore.updateTag(incomingTag);
// associate with service-resource if not already associated
if (findTagInList(matchingTag, associatedTags) == null) {
RangerTagResourceMap tagResourceMap = new RangerTagResourceMap();
tagResourceMap.setTagId(matchingTag.getId());
tagResourceMap.setResourceId(resourceInStore.getId());
tagResourceMap = tagStore.createTagResourceMap(tagResourceMap);
} else {
isAnyTagUpdated = true;
}
}
}
}
}
} catch (Exception exception) {
LOG.error("createRangerTagResourceMap failed", exception);
throw exception;
}
if (CollectionUtils.isNotEmpty(associatedTags)) {
Long tagId = null;
try {
for (RangerTag associatedTag : associatedTags) {
if (findTagInList(associatedTag, tagsToRetain) == null) {
tagId = associatedTag.getId();
RangerTagResourceMap tagResourceMap = tagStore.getTagResourceMapForTagAndResourceId(tagId, resourceInStore.getId());
if (tagResourceMap != null) {
tagStore.deleteTagResourceMap(tagResourceMap.getId());
}
if (LOG.isDebugEnabled()) {
LOG.debug("Deleted tagResourceMap(tagId=" + tagId + ", resourceId=" + resourceInStore.getId());
}
}
}
} catch (Exception exception) {
LOG.error("deleteTagResourceMap failed, tagId=" + tagId + ", resourceId=" + resourceInStore.getId());
throw exception;
}
}
if (isAnyTagUpdated) {
if (RangerPerfTracer.isPerfTraceEnabled(PERF_LOG_ADD_OR_UPDATE)) {
perf = RangerPerfTracer.getPerfTracer(PERF_LOG_ADD_OR_UPDATE, "tags.refreshServiceResource(" + resourceInStore.getId() + ")");
}
tagStore.refreshServiceResource(resourceInStore.getId());
RangerPerfTracer.logAlways(perf);
}
}
}
RangerPerfTracer.logAlways(perfTotal);
if (LOG.isDebugEnabled()) {
LOG.debug("<== ServiceTagsProcessor.createOrUpdate()");
}
}
Aggregations