Search in sources :

Example 21 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class FileTagSource method synchUp.

public void synchUp() {
    if (isChanged()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Begin: update tags from source==>sink");
        }
        ServiceTags serviceTags = readFromFile();
        updateSink(serviceTags);
        if (LOG.isDebugEnabled()) {
            LOG.debug("End: update tags from source==>sink");
        }
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("FileTagSource: no change found for synchronization.");
        }
    }
}
Also used : ServiceTags(org.apache.ranger.plugin.util.ServiceTags)

Example 22 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class TestTagREST method test57getSecureServiceTagsIfUpdated.

@Test
public void test57getSecureServiceTagsIfUpdated() {
    boolean isAdmin = false;
    boolean isKeyAdmin = false;
    boolean isAllowed = true;
    ServiceTags oldServiceTag = null;
    XXService xService = new XXService();
    xService.setId(id);
    xService.setName(serviceName);
    xService.setType(5L);
    XXServiceDef xServiceDef = new XXServiceDef();
    xServiceDef.setId(id);
    xServiceDef.setVersion(5L);
    RangerService rangerService = new RangerService();
    rangerService.setId(id);
    rangerService.setName(serviceName);
    XXServiceDao xXServiceDao = Mockito.mock(XXServiceDao.class);
    XXServiceDefDao xXServiceDefDao = Mockito.mock(XXServiceDefDao.class);
    Mockito.when(bizUtil.isAdmin()).thenReturn(isAdmin);
    Mockito.when(bizUtil.isKeyAdmin()).thenReturn(isKeyAdmin);
    Mockito.when(daoManager.getXXService()).thenReturn(xXServiceDao);
    Mockito.when(xXServiceDao.findByName(serviceName)).thenReturn(xService);
    Mockito.when(daoManager.getXXServiceDef()).thenReturn(xXServiceDefDao);
    Mockito.when(xXServiceDefDao.getById(xService.getType())).thenReturn(xServiceDef);
    try {
        Mockito.when(svcStore.getServiceByName(serviceName)).thenReturn(rangerService);
    } catch (Exception e) {
    }
    Mockito.when(bizUtil.isUserAllowed(rangerService, Allowed_User_List_For_Tag_Download)).thenReturn(isAllowed);
    try {
        Mockito.when(tagStore.getServiceTagsIfUpdated(serviceName, lastKnownVersion)).thenReturn(oldServiceTag);
    } catch (Exception e) {
    }
    Mockito.when(restErrorUtil.createRESTException(Mockito.anyInt(), Mockito.anyString(), Mockito.anyBoolean())).thenThrow(new WebApplicationException());
    thrown.expect(WebApplicationException.class);
    tagREST.getSecureServiceTagsIfUpdated(serviceName, lastKnownVersion, 0L, pluginId, null);
    Mockito.verify(bizUtil).isAdmin();
    Mockito.verify(bizUtil).isKeyAdmin();
    Mockito.verify(daoManager).getXXService();
    Mockito.verify(xXServiceDao).findByName(serviceName);
    Mockito.verify(daoManager).getXXServiceDef();
    Mockito.verify(xXServiceDefDao).getById(xService.getType());
    try {
        Mockito.verify(svcStore).getServiceByName(serviceName);
    } catch (Exception e) {
    }
    Mockito.verify(bizUtil).isUserAllowed(rangerService, Allowed_User_List_For_Tag_Download);
    try {
        Mockito.verify(tagStore).getServiceTagsIfUpdated(serviceName, lastKnownVersion);
    } catch (Exception e) {
    }
    Mockito.verify(restErrorUtil).createRESTException(Mockito.anyInt(), Mockito.anyString(), Mockito.anyBoolean());
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) WebApplicationException(javax.ws.rs.WebApplicationException) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) RangerService(org.apache.ranger.plugin.model.RangerService) XXService(org.apache.ranger.entity.XXService) XXServiceDao(org.apache.ranger.db.XXServiceDao) ExpectedException(org.junit.rules.ExpectedException) WebApplicationException(javax.ws.rs.WebApplicationException) Test(org.junit.Test)

Example 23 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class TestTagREST method test53getSecureServiceTagsIfUpdatedIsAdminTrue.

@Test
public void test53getSecureServiceTagsIfUpdatedIsAdminTrue() {
    boolean isAdmin = true;
    boolean isKeyAdmin = false;
    ServiceTags oldServiceTag = new ServiceTags();
    oldServiceTag.setServiceName(serviceName);
    oldServiceTag.setTagVersion(5L);
    XXService xService = new XXService();
    xService.setId(id);
    xService.setName(serviceName);
    xService.setType(5L);
    XXServiceDef xServiceDef = new XXServiceDef();
    xServiceDef.setId(id);
    xServiceDef.setVersion(5L);
    RangerService rangerService = new RangerService();
    rangerService.setId(id);
    rangerService.setName(serviceName);
    XXServiceDao xXServiceDao = Mockito.mock(XXServiceDao.class);
    XXServiceDefDao xXServiceDefDao = Mockito.mock(XXServiceDefDao.class);
    Mockito.when(bizUtil.isAdmin()).thenReturn(isAdmin);
    Mockito.when(bizUtil.isKeyAdmin()).thenReturn(isKeyAdmin);
    Mockito.when(daoManager.getXXService()).thenReturn(xXServiceDao);
    Mockito.when(xXServiceDao.findByName(serviceName)).thenReturn(xService);
    Mockito.when(daoManager.getXXServiceDef()).thenReturn(xXServiceDefDao);
    Mockito.when(xXServiceDefDao.getById(xService.getType())).thenReturn(xServiceDef);
    try {
        Mockito.when(svcStore.getServiceByName(serviceName)).thenReturn(rangerService);
    } catch (Exception e) {
    }
    try {
        Mockito.when(tagStore.getServiceTagsIfUpdated(serviceName, lastKnownVersion)).thenReturn(oldServiceTag);
    } catch (Exception e) {
    }
    ServiceTags result = tagREST.getSecureServiceTagsIfUpdated(serviceName, lastKnownVersion, 0L, pluginId, null);
    Assert.assertNotNull(result.getServiceName());
    Assert.assertEquals(result.getServiceName(), oldServiceTag.getServiceName());
    Assert.assertEquals(result.getTagVersion(), oldServiceTag.getTagVersion());
    Mockito.verify(bizUtil).isAdmin();
    Mockito.verify(bizUtil).isKeyAdmin();
    Mockito.verify(daoManager).getXXService();
    Mockito.verify(xXServiceDao).findByName(serviceName);
    Mockito.verify(daoManager).getXXServiceDef();
    Mockito.verify(xXServiceDefDao).getById(xService.getType());
    try {
        Mockito.verify(svcStore).getServiceByName(serviceName);
    } catch (Exception e) {
    }
    try {
        Mockito.verify(tagStore).getServiceTagsIfUpdated(serviceName, lastKnownVersion);
    } catch (Exception e) {
    }
}
Also used : XXServiceDef(org.apache.ranger.entity.XXServiceDef) XXServiceDefDao(org.apache.ranger.db.XXServiceDefDao) ServiceTags(org.apache.ranger.plugin.util.ServiceTags) RangerService(org.apache.ranger.plugin.model.RangerService) XXService(org.apache.ranger.entity.XXService) XXServiceDao(org.apache.ranger.db.XXServiceDao) ExpectedException(org.junit.rules.ExpectedException) WebApplicationException(javax.ws.rs.WebApplicationException) Test(org.junit.Test)

Example 24 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class TestTagREST method test51getServiceTagsIfUpdated.

@Test
public void test51getServiceTagsIfUpdated() {
    ServiceTags oldServiceTag = new ServiceTags();
    oldServiceTag.setServiceName(serviceName);
    oldServiceTag.setTagVersion(5L);
    try {
        Mockito.when(tagStore.getServiceTagsIfUpdated(serviceName, lastKnownVersion)).thenReturn(oldServiceTag);
    } catch (Exception e) {
    }
    ServiceTags serviceTags = tagREST.getServiceTagsIfUpdated(serviceName, lastKnownVersion, 0L, pluginId, null);
    Assert.assertEquals(serviceTags.getServiceName(), oldServiceTag.getServiceName());
    Assert.assertEquals(serviceTags.getTagVersion(), oldServiceTag.getTagVersion());
    try {
        Mockito.verify(tagStore).getServiceTagsIfUpdated(serviceName, lastKnownVersion);
    } catch (Exception e) {
    }
}
Also used : ServiceTags(org.apache.ranger.plugin.util.ServiceTags) ExpectedException(org.junit.rules.ExpectedException) WebApplicationException(javax.ws.rs.WebApplicationException) Test(org.junit.Test)

Example 25 with ServiceTags

use of org.apache.ranger.plugin.util.ServiceTags in project ranger by apache.

the class RangerServiceTagsCache method getServiceTags.

public ServiceTags getServiceTags(String serviceName, Long serviceId, TagStore tagStore) throws Exception {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> RangerServiceTagsCache.getServiceTags(" + serviceName + ", " + serviceId + ")");
    }
    ServiceTags ret = null;
    if (StringUtils.isNotBlank(serviceName) && serviceId != null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("useServiceTagsCache=" + useServiceTagsCache);
        }
        ServiceTags serviceTags = null;
        if (!useServiceTagsCache) {
            if (tagStore != null) {
                try {
                    serviceTags = tagStore.getServiceTags(serviceName);
                } catch (Exception exception) {
                    LOG.error("getServiceTags(" + serviceName + "): failed to get latest tags from tag-store", exception);
                }
            } else {
                LOG.error("getServiceTags(" + serviceName + "): failed to get latest tags as tag-store is null!");
            }
        } else {
            ServiceTagsWrapper serviceTagsWrapper = null;
            synchronized (this) {
                serviceTagsWrapper = serviceTagsMap.get(serviceName);
                if (serviceTagsWrapper != null) {
                    if (!serviceId.equals(serviceTagsWrapper.getServiceId())) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Service [" + serviceName + "] changed service-id from " + serviceTagsWrapper.getServiceId() + " to " + serviceId);
                            LOG.debug("Recreating serviceTagsWrapper for serviceName [" + serviceName + "]");
                        }
                        serviceTagsMap.remove(serviceName);
                        serviceTagsWrapper = null;
                    }
                }
                if (serviceTagsWrapper == null) {
                    serviceTagsWrapper = new ServiceTagsWrapper(serviceId);
                    serviceTagsMap.put(serviceName, serviceTagsWrapper);
                }
            }
            if (tagStore != null) {
                boolean refreshed = serviceTagsWrapper.getLatestOrCached(serviceName, tagStore);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("getLatestOrCached returned " + refreshed);
                }
            } else {
                LOG.error("getServiceTags(" + serviceName + "): failed to get latest tags as tag-store is null!");
            }
            serviceTags = serviceTagsWrapper.getServiceTags();
        }
        ret = serviceTags;
    } else {
        LOG.error("getServiceTags() failed to get tags as serviceName is null or blank and/or serviceId is null!");
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("<== RangerServiceTagsCache.getServiceTags(" + serviceName + ", " + serviceId + "): count=" + ((ret == null || ret.getTags() == null) ? 0 : ret.getTags().size()));
    }
    return ret;
}
Also used : ServiceTags(org.apache.ranger.plugin.util.ServiceTags)

Aggregations

ServiceTags (org.apache.ranger.plugin.util.ServiceTags)25 WebApplicationException (javax.ws.rs.WebApplicationException)10 XXService (org.apache.ranger.entity.XXService)9 Test (org.junit.Test)8 ExpectedException (org.junit.rules.ExpectedException)8 XXServiceDef (org.apache.ranger.entity.XXServiceDef)7 RangerService (org.apache.ranger.plugin.model.RangerService)7 XXServiceDao (org.apache.ranger.db.XXServiceDao)6 XXServiceDefDao (org.apache.ranger.db.XXServiceDefDao)6 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 RangerAtlasEntity (org.apache.ranger.tagsync.source.atlasrest.RangerAtlasEntity)3 Gson (com.google.gson.Gson)2 IOException (java.io.IOException)2 Map (java.util.Map)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 UserGroupInformation (org.apache.hadoop.security.UserGroupInformation)2 XXServiceVersionInfo (org.apache.ranger.entity.XXServiceVersionInfo)2