Search in sources :

Example 11 with AuthConfig

use of io.druid.server.security.AuthConfig in project druid by druid-io.

the class DatasourcesResourceTest method testGetSegmentDataSourceIntervals.

@Test
public void testGetSegmentDataSourceIntervals() {
    server = new DruidServer("who", "host", 1234, "historical", "tier1", 0);
    server.addDataSegment(dataSegmentList.get(0).getIdentifier(), dataSegmentList.get(0));
    server.addDataSegment(dataSegmentList.get(1).getIdentifier(), dataSegmentList.get(1));
    server.addDataSegment(dataSegmentList.get(2).getIdentifier(), dataSegmentList.get(2));
    EasyMock.expect(inventoryView.getInventory()).andReturn(ImmutableList.of(server)).atLeastOnce();
    EasyMock.replay(inventoryView);
    List<Interval> expectedIntervals = new ArrayList<>();
    expectedIntervals.add(new Interval("2010-01-22T00:00:00.000Z/2010-01-23T00:00:00.000Z"));
    expectedIntervals.add(new Interval("2010-01-01T00:00:00.000Z/2010-01-02T00:00:00.000Z"));
    DatasourcesResource datasourcesResource = new DatasourcesResource(inventoryView, null, null, new AuthConfig());
    Response response = datasourcesResource.getSegmentDataSourceIntervals("invalidDataSource", null, null);
    Assert.assertEquals(response.getEntity(), null);
    response = datasourcesResource.getSegmentDataSourceIntervals("datasource1", null, null);
    TreeSet<Interval> actualIntervals = (TreeSet) response.getEntity();
    Assert.assertEquals(2, actualIntervals.size());
    Assert.assertEquals(expectedIntervals.get(0), actualIntervals.first());
    Assert.assertEquals(expectedIntervals.get(1), actualIntervals.last());
    response = datasourcesResource.getSegmentDataSourceIntervals("datasource1", "simple", null);
    TreeMap<Interval, Map<String, Object>> results = (TreeMap) response.getEntity();
    Assert.assertEquals(2, results.size());
    Assert.assertEquals(expectedIntervals.get(0), results.firstKey());
    Assert.assertEquals(expectedIntervals.get(1), results.lastKey());
    Assert.assertEquals(1, results.firstEntry().getValue().get("count"));
    Assert.assertEquals(1, results.lastEntry().getValue().get("count"));
    response = datasourcesResource.getSegmentDataSourceIntervals("datasource1", null, "full");
    results = ((TreeMap<Interval, Map<String, Object>>) response.getEntity());
    int i = 1;
    for (Map.Entry<Interval, Map<String, Object>> entry : results.entrySet()) {
        Assert.assertEquals(dataSegmentList.get(i).getInterval(), entry.getKey());
        Assert.assertEquals(dataSegmentList.get(i), ((Map<String, Object>) entry.getValue().get(dataSegmentList.get(i).getIdentifier())).get("metadata"));
        i--;
    }
    EasyMock.verify(inventoryView);
}
Also used : ArrayList(java.util.ArrayList) DruidServer(io.druid.client.DruidServer) AuthConfig(io.druid.server.security.AuthConfig) TreeMap(java.util.TreeMap) Response(javax.ws.rs.core.Response) TreeSet(java.util.TreeSet) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Interval(org.joda.time.Interval) Test(org.junit.Test)

Example 12 with AuthConfig

use of io.druid.server.security.AuthConfig in project druid by druid-io.

the class DatasourcesResourceTest method testDeleteDataSource.

@Test
public void testDeleteDataSource() {
    IndexingServiceClient indexingServiceClient = EasyMock.createStrictMock(IndexingServiceClient.class);
    EasyMock.replay(indexingServiceClient, server);
    DatasourcesResource datasourcesResource = new DatasourcesResource(inventoryView, null, indexingServiceClient, new AuthConfig());
    Response response = datasourcesResource.deleteDataSource("datasource", "true", "???");
    Assert.assertEquals(400, response.getStatus());
    Assert.assertNotNull(response.getEntity());
    Assert.assertTrue(response.getEntity().toString().contains("java.lang.IllegalArgumentException"));
    EasyMock.verify(indexingServiceClient, server);
}
Also used : Response(javax.ws.rs.core.Response) IndexingServiceClient(io.druid.client.indexing.IndexingServiceClient) AuthConfig(io.druid.server.security.AuthConfig) Test(org.junit.Test)

Example 13 with AuthConfig

use of io.druid.server.security.AuthConfig in project druid by druid-io.

the class QueryResourceTest method testDenySecuredGetServer.

@Test(timeout = 60_000L)
public void testDenySecuredGetServer() throws Exception {
    final CountDownLatch waitForCancellationLatch = new CountDownLatch(1);
    final CountDownLatch waitFinishLatch = new CountDownLatch(2);
    final CountDownLatch startAwaitLatch = new CountDownLatch(1);
    EasyMock.expect(testServletRequest.getAttribute(EasyMock.anyString())).andReturn(new AuthorizationInfo() {

        @Override
        public Access isAuthorized(Resource resource, Action action) {
            // WRITE corresponds to cancellation of query
            if (action.equals(Action.READ)) {
                try {
                    waitForCancellationLatch.await();
                } catch (InterruptedException e) {
                    Throwables.propagate(e);
                }
                return new Access(true);
            } else {
                // Deny access to cancel the query
                return new Access(false);
            }
        }
    }).times(2);
    EasyMock.replay(testServletRequest);
    queryResource = new QueryResource(warehouse, serverConfig, jsonMapper, jsonMapper, testSegmentWalker, new NoopServiceEmitter(), new NoopRequestLogger(), queryManager, new AuthConfig(true));
    final String queryString = "{\"queryType\":\"timeBoundary\", \"dataSource\":\"allow\"," + "\"context\":{\"queryId\":\"id_1\"}}";
    ObjectMapper mapper = new DefaultObjectMapper();
    Query query = mapper.readValue(queryString, Query.class);
    ListenableFuture future = MoreExecutors.listeningDecorator(Execs.singleThreaded("test_query_resource_%s")).submit(new Runnable() {

        @Override
        public void run() {
            try {
                startAwaitLatch.countDown();
                Response response = queryResource.doPost(new ByteArrayInputStream(queryString.getBytes("UTF-8")), null, testServletRequest);
                Assert.assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
            } catch (IOException e) {
                Throwables.propagate(e);
            }
            waitFinishLatch.countDown();
        }
    });
    queryManager.registerQuery(query, future);
    startAwaitLatch.await();
    Executors.newSingleThreadExecutor().submit(new Runnable() {

        @Override
        public void run() {
            Response response = queryResource.getServer("id_1", testServletRequest);
            Assert.assertEquals(Response.Status.FORBIDDEN.getStatusCode(), response.getStatus());
            waitForCancellationLatch.countDown();
            waitFinishLatch.countDown();
        }
    });
    waitFinishLatch.await();
}
Also used : Action(io.druid.server.security.Action) Query(io.druid.query.Query) Resource(io.druid.server.security.Resource) Access(io.druid.server.security.Access) NoopRequestLogger(io.druid.server.log.NoopRequestLogger) NoopServiceEmitter(io.druid.server.metrics.NoopServiceEmitter) AuthConfig(io.druid.server.security.AuthConfig) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AuthorizationInfo(io.druid.server.security.AuthorizationInfo) Response(javax.ws.rs.core.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 14 with AuthConfig

use of io.druid.server.security.AuthConfig in project druid by druid-io.

the class QueryResourceTest method testSecuredGetServer.

@Test(timeout = 60_000L)
public void testSecuredGetServer() throws Exception {
    final CountDownLatch waitForCancellationLatch = new CountDownLatch(1);
    final CountDownLatch waitFinishLatch = new CountDownLatch(2);
    final CountDownLatch startAwaitLatch = new CountDownLatch(1);
    final CountDownLatch cancelledCountDownLatch = new CountDownLatch(1);
    EasyMock.expect(testServletRequest.getAttribute(EasyMock.anyString())).andReturn(new AuthorizationInfo() {

        @Override
        public Access isAuthorized(Resource resource, Action action) {
            // WRITE corresponds to cancellation of query
            if (action.equals(Action.READ)) {
                try {
                    // Countdown startAwaitLatch as we want query cancellation to happen
                    // after we enter isAuthorized method so that we can handle the
                    // InterruptedException here because of query cancellation
                    startAwaitLatch.countDown();
                    waitForCancellationLatch.await();
                } catch (InterruptedException e) {
                    // When the query is cancelled the control will reach here,
                    // countdown the latch and rethrow the exception so that error response is returned for the query
                    cancelledCountDownLatch.countDown();
                    Throwables.propagate(e);
                }
                return new Access(true);
            } else {
                return new Access(true);
            }
        }
    }).times(2);
    EasyMock.replay(testServletRequest);
    queryResource = new QueryResource(warehouse, serverConfig, jsonMapper, jsonMapper, testSegmentWalker, new NoopServiceEmitter(), new NoopRequestLogger(), queryManager, new AuthConfig(true));
    final String queryString = "{\"queryType\":\"timeBoundary\", \"dataSource\":\"allow\"," + "\"context\":{\"queryId\":\"id_1\"}}";
    ObjectMapper mapper = new DefaultObjectMapper();
    Query query = mapper.readValue(queryString, Query.class);
    ListenableFuture future = MoreExecutors.listeningDecorator(Execs.singleThreaded("test_query_resource_%s")).submit(new Runnable() {

        @Override
        public void run() {
            try {
                Response response = queryResource.doPost(new ByteArrayInputStream(queryString.getBytes("UTF-8")), null, testServletRequest);
                Assert.assertEquals(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus());
            } catch (IOException e) {
                Throwables.propagate(e);
            }
            waitFinishLatch.countDown();
        }
    });
    queryManager.registerQuery(query, future);
    startAwaitLatch.await();
    Executors.newSingleThreadExecutor().submit(new Runnable() {

        @Override
        public void run() {
            Response response = queryResource.getServer("id_1", testServletRequest);
            Assert.assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus());
            waitForCancellationLatch.countDown();
            waitFinishLatch.countDown();
        }
    });
    waitFinishLatch.await();
    cancelledCountDownLatch.await();
}
Also used : Action(io.druid.server.security.Action) Query(io.druid.query.Query) Resource(io.druid.server.security.Resource) Access(io.druid.server.security.Access) NoopRequestLogger(io.druid.server.log.NoopRequestLogger) NoopServiceEmitter(io.druid.server.metrics.NoopServiceEmitter) AuthConfig(io.druid.server.security.AuthConfig) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) AuthorizationInfo(io.druid.server.security.AuthorizationInfo) Response(javax.ws.rs.core.Response) ByteArrayInputStream(java.io.ByteArrayInputStream) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) DefaultObjectMapper(io.druid.jackson.DefaultObjectMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 15 with AuthConfig

use of io.druid.server.security.AuthConfig in project druid by druid-io.

the class OverlordResourceTest method setUp.

@Before
public void setUp() throws Exception {
    taskRunner = EasyMock.createMock(TaskRunner.class);
    taskMaster = EasyMock.createStrictMock(TaskMaster.class);
    tsqa = EasyMock.createStrictMock(TaskStorageQueryAdapter.class);
    req = EasyMock.createStrictMock(HttpServletRequest.class);
    EasyMock.expect(taskMaster.getTaskRunner()).andReturn(Optional.of(taskRunner)).anyTimes();
    overlordResource = new OverlordResource(taskMaster, tsqa, null, null, null, new AuthConfig(true));
    EasyMock.expect(req.getAttribute(AuthConfig.DRUID_AUTH_TOKEN)).andReturn(new AuthorizationInfo() {

        @Override
        public Access isAuthorized(Resource resource, Action action) {
            if (resource.getName().equals("allow")) {
                return new Access(true);
            } else {
                return new Access(false);
            }
        }
    });
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Action(io.druid.server.security.Action) Resource(io.druid.server.security.Resource) Access(io.druid.server.security.Access) AuthConfig(io.druid.server.security.AuthConfig) TaskMaster(io.druid.indexing.overlord.TaskMaster) AuthorizationInfo(io.druid.server.security.AuthorizationInfo) TaskStorageQueryAdapter(io.druid.indexing.overlord.TaskStorageQueryAdapter) TaskRunner(io.druid.indexing.overlord.TaskRunner) Before(org.junit.Before)

Aggregations

AuthConfig (io.druid.server.security.AuthConfig)21 Test (org.junit.Test)19 Response (javax.ws.rs.core.Response)18 Map (java.util.Map)10 TreeMap (java.util.TreeMap)9 ArrayList (java.util.ArrayList)8 Interval (org.joda.time.Interval)7 HashMap (java.util.HashMap)6 ImmutableList (com.google.common.collect.ImmutableList)5 Access (io.druid.server.security.Access)5 Action (io.druid.server.security.Action)5 AuthorizationInfo (io.druid.server.security.AuthorizationInfo)5 Resource (io.druid.server.security.Resource)5 List (java.util.List)5 DruidDataSource (io.druid.client.DruidDataSource)4 DruidServer (io.druid.client.DruidServer)3 NoopRequestLogger (io.druid.server.log.NoopRequestLogger)3 NoopServiceEmitter (io.druid.server.metrics.NoopServiceEmitter)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 TreeSet (java.util.TreeSet)3