Search in sources :

Example 36 with RangerService

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

the class TestTagREST method test52getSecureServiceTagsIfUpdatedIsKeyAdminTrue.

@Test
public void test52getSecureServiceTagsIfUpdatedIsKeyAdminTrue() {
    boolean isAdmin = false;
    boolean isKeyAdmin = true;
    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);
    xServiceDef.setImplclassname("org.apache.ranger.services.kms.RangerServiceKMS");
    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 37 with RangerService

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

the class TestRangerServiceValidator method test_isValid_happyPath.

@Test
public void test_isValid_happyPath() throws Exception {
    // create a service def with some required parameters
    Object[][] serviceDefInput = new Object[][] { { "param1", true }, { "param2", true }, { "param3", false }, { "param4", false }, { "param5", true } };
    List<RangerServiceConfigDef> configDefs = _utils.createServiceConditionDefs(serviceDefInput);
    RangerServiceDef serviceDef = mock(RangerServiceDef.class);
    when(serviceDef.getConfigs()).thenReturn(configDefs);
    // create a service with some parameters on it
    RangerService service = mock(RangerService.class);
    when(service.getName()).thenReturn("aName");
    when(service.getType()).thenReturn("aType");
    // contains an extra parameter (param6) and one optional is missing(param4)
    String[] configs = new String[] { "param1", "param2", "param3", "param5", "param6" };
    Map<String, String> configMap = _utils.createMap(configs);
    when(service.getConfigs()).thenReturn(configMap);
    // wire then into the store
    // service does not exists
    when(_store.getServiceByName("aName")).thenReturn(null);
    // service def exists
    when(_store.getServiceDefByName("aType")).thenReturn(serviceDef);
    Assert.assertTrue(_validator.isValid(service, Action.CREATE, _failures));
    // for update to work the only additional requirement is that id is required and service should exist
    // if name is not null and it points to a service then it should match the id
    when(service.getId()).thenReturn(7L);
    RangerService existingService = mock(RangerService.class);
    when(existingService.getId()).thenReturn(Long.valueOf(7L));
    when(_store.getService(7L)).thenReturn(existingService);
    when(_store.getServiceByName("aName")).thenReturn(existingService);
    Assert.assertTrue(_validator.isValid(service, Action.UPDATE, _failures));
    // name need not point to a service for update to work, of course.
    when(_store.getServiceByName("aName")).thenReturn(null);
    Assert.assertTrue(_validator.isValid(service, Action.UPDATE, _failures));
}
Also used : RangerServiceConfigDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerServiceConfigDef) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) RangerService(org.apache.ranger.plugin.model.RangerService) Test(org.junit.Test)

Example 38 with RangerService

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

the class TestRangerServiceValidator method test_isValid_missingRequiredParameter.

@Test
public void test_isValid_missingRequiredParameter() throws Exception {
    // Create/Update: simulate a condition where required parameters are missing
    Object[][] input = new Object[][] { { "param1", true }, { "param2", true }, { "param3", false }, { "param4", false } };
    List<RangerServiceConfigDef> configDefs = _utils.createServiceConditionDefs(input);
    RangerServiceDef serviceDef = mock(RangerServiceDef.class);
    when(serviceDef.getConfigs()).thenReturn(configDefs);
    // wire this service def into store
    when(_store.getServiceDefByName("aType")).thenReturn(serviceDef);
    // create a service with some require parameters missing
    RangerService service = mock(RangerService.class);
    when(service.getType()).thenReturn("aType");
    when(service.getName()).thenReturn("aName");
    // required parameters param2 is missing
    String[] params = new String[] { "param1", "param3", "param4", "param5" };
    Map<String, String> paramMap = _utils.createMap(params);
    when(service.getConfigs()).thenReturn(paramMap);
    // service does not exist in the store
    when(_store.getServiceByName("aService")).thenReturn(null);
    for (Action action : cu) {
        // it should be invalid
        checkFailure_isValid(_validator, service, action, _failures, "missing", "configuration", "param2");
    }
}
Also used : Action(org.apache.ranger.plugin.model.validation.RangerValidator.Action) RangerServiceConfigDef(org.apache.ranger.plugin.model.RangerServiceDef.RangerServiceConfigDef) RangerServiceDef(org.apache.ranger.plugin.model.RangerServiceDef) RangerService(org.apache.ranger.plugin.model.RangerService) Test(org.junit.Test)

Example 39 with RangerService

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

the class TestRangerServiceValidator method test_isValid_withId_happyPath.

@Test
public void test_isValid_withId_happyPath() throws Exception {
    _validator = new RangerServiceValidator(_store);
    RangerService service = mock(RangerService.class);
    when(_store.getService(1L)).thenReturn(service);
    Assert.assertTrue(_validator.isValid(1L, Action.DELETE, _failures));
}
Also used : RangerService(org.apache.ranger.plugin.model.RangerService) Test(org.junit.Test)

Example 40 with RangerService

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

the class TestRangerValidator method test_getService_byId.

@Test
public void test_getService_byId() throws Exception {
    // if service store returns null or throws an exception then service is deemed invalid
    when(_store.getService(1L)).thenReturn(null);
    when(_store.getService(2L)).thenThrow(new Exception());
    RangerService service = mock(RangerService.class);
    when(_store.getService(3L)).thenReturn(service);
    Assert.assertNull(_validator.getService(1L));
    Assert.assertNull(_validator.getService(2L));
    Assert.assertTrue(_validator.getService(3L) != null);
}
Also used : RangerService(org.apache.ranger.plugin.model.RangerService) Test(org.junit.Test)

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