Search in sources :

Example 6 with BasicAuthenticatorUser

use of org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorUser in project druid by druid-io.

the class CoordinatorBasicAuthenticatorResourceTest method testGetAllUsersSeparateDatabaseTables.

@Test
public void testGetAllUsersSeparateDatabaseTables() {
    Response response = resource.getAllUsers(req, AUTHENTICATOR_NAME);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(ImmutableSet.of(BasicAuthUtils.ADMIN_NAME, BasicAuthUtils.INTERNAL_USER_NAME), response.getEntity());
    resource.createUser(req, AUTHENTICATOR_NAME, "druid");
    resource.createUser(req, AUTHENTICATOR_NAME, "druid2");
    resource.createUser(req, AUTHENTICATOR_NAME, "druid3");
    resource.createUser(req, AUTHENTICATOR_NAME2, "druid4");
    resource.createUser(req, AUTHENTICATOR_NAME2, "druid5");
    resource.createUser(req, AUTHENTICATOR_NAME2, "druid6");
    Set<String> expectedUsers = ImmutableSet.of(BasicAuthUtils.ADMIN_NAME, BasicAuthUtils.INTERNAL_USER_NAME, "druid", "druid2", "druid3");
    Set<String> expectedUsers2 = ImmutableSet.of(BasicAuthUtils.ADMIN_NAME, BasicAuthUtils.INTERNAL_USER_NAME, "druid4", "druid5", "druid6");
    response = resource.getAllUsers(req, AUTHENTICATOR_NAME);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(expectedUsers, response.getEntity());
    // Verify cached user map for AUTHENTICATOR_NAME authenticator is also getting updated
    response = resource.getCachedSerializedUserMap(req, AUTHENTICATOR_NAME);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getEntity() instanceof byte[]);
    Map<String, BasicAuthenticatorUser> cachedUserMap = BasicAuthUtils.deserializeAuthenticatorUserMap(objectMapper, (byte[]) response.getEntity());
    Assert.assertNotNull(cachedUserMap.get(BasicAuthUtils.ADMIN_NAME));
    Assert.assertEquals(cachedUserMap.get(BasicAuthUtils.ADMIN_NAME).getName(), BasicAuthUtils.ADMIN_NAME);
    Assert.assertNotNull(cachedUserMap.get(BasicAuthUtils.INTERNAL_USER_NAME));
    Assert.assertEquals(cachedUserMap.get(BasicAuthUtils.ADMIN_NAME).getName(), BasicAuthUtils.ADMIN_NAME);
    Assert.assertNotNull(cachedUserMap.get("druid"));
    Assert.assertEquals(cachedUserMap.get("druid").getName(), "druid");
    Assert.assertNotNull(cachedUserMap.get("druid2"));
    Assert.assertEquals(cachedUserMap.get("druid2").getName(), "druid2");
    Assert.assertNotNull(cachedUserMap.get("druid3"));
    Assert.assertEquals(cachedUserMap.get("druid3").getName(), "druid3");
    response = resource.getAllUsers(req, AUTHENTICATOR_NAME2);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(expectedUsers2, response.getEntity());
    // Verify cached user map for each AUTHENTICATOR_NAME2 is also getting updated
    response = resource.getCachedSerializedUserMap(req, AUTHENTICATOR_NAME2);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getEntity() instanceof byte[]);
    cachedUserMap = BasicAuthUtils.deserializeAuthenticatorUserMap(objectMapper, (byte[]) response.getEntity());
    Assert.assertNotNull(cachedUserMap.get(BasicAuthUtils.ADMIN_NAME));
    Assert.assertEquals(cachedUserMap.get(BasicAuthUtils.ADMIN_NAME).getName(), BasicAuthUtils.ADMIN_NAME);
    Assert.assertNotNull(cachedUserMap.get(BasicAuthUtils.INTERNAL_USER_NAME));
    Assert.assertEquals(cachedUserMap.get(BasicAuthUtils.ADMIN_NAME).getName(), BasicAuthUtils.ADMIN_NAME);
    Assert.assertNotNull(cachedUserMap.get("druid4"));
    Assert.assertEquals(cachedUserMap.get("druid4").getName(), "druid4");
    Assert.assertNotNull(cachedUserMap.get("druid5"));
    Assert.assertEquals(cachedUserMap.get("druid5").getName(), "druid5");
    Assert.assertNotNull(cachedUserMap.get("druid6"));
    Assert.assertEquals(cachedUserMap.get("druid6").getName(), "druid6");
}
Also used : Response(javax.ws.rs.core.Response) BasicAuthenticatorUser(org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorUser) Test(org.junit.Test)

Example 7 with BasicAuthenticatorUser

use of org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorUser in project druid by druid-io.

the class CoordinatorBasicAuthenticatorResourceTest method testCreateDeleteUser.

@Test
public void testCreateDeleteUser() {
    Response response = resource.createUser(req, AUTHENTICATOR_NAME, "druid");
    Assert.assertEquals(200, response.getStatus());
    response = resource.getUser(req, AUTHENTICATOR_NAME, "druid");
    Assert.assertEquals(200, response.getStatus());
    BasicAuthenticatorUser expectedUser = new BasicAuthenticatorUser("druid", null);
    Assert.assertEquals(expectedUser, response.getEntity());
    response = resource.deleteUser(req, AUTHENTICATOR_NAME, "druid");
    Assert.assertEquals(200, response.getStatus());
    response = resource.getCachedSerializedUserMap(req, AUTHENTICATOR_NAME);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getEntity() instanceof byte[]);
    Map<String, BasicAuthenticatorUser> cachedUserMap = BasicAuthUtils.deserializeAuthenticatorUserMap(objectMapper, (byte[]) response.getEntity());
    Assert.assertNotNull(cachedUserMap);
    Assert.assertNull(cachedUserMap.get("druid"));
    response = resource.deleteUser(req, AUTHENTICATOR_NAME, "druid");
    Assert.assertEquals(400, response.getStatus());
    Assert.assertEquals(errorMapWithMsg("User [druid] does not exist."), response.getEntity());
    response = resource.getUser(req, AUTHENTICATOR_NAME, "druid");
    Assert.assertEquals(400, response.getStatus());
    Assert.assertEquals(errorMapWithMsg("User [druid] does not exist."), response.getEntity());
}
Also used : Response(javax.ws.rs.core.Response) BasicAuthenticatorUser(org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorUser) Test(org.junit.Test)

Example 8 with BasicAuthenticatorUser

use of org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorUser in project druid by druid-io.

the class CoordinatorBasicAuthenticatorMetadataStorageUpdaterTest method createUser.

@Test
public void createUser() {
    Map<String, BasicAuthenticatorUser> expectedUserMap = ImmutableMap.of("druid", new BasicAuthenticatorUser("druid", null));
    byte[] expectedSerializeUserMap = BasicAuthUtils.serializeAuthenticatorUserMap(objectMapper, expectedUserMap);
    updater.createUser(AUTHENTICATOR_NAME, "druid");
    Assert.assertArrayEquals(expectedSerializeUserMap, updater.getCurrentUserMapBytes(AUTHENTICATOR_NAME));
    Map<String, BasicAuthenticatorUser> actualUserMap = BasicAuthUtils.deserializeAuthenticatorUserMap(objectMapper, updater.getCurrentUserMapBytes(AUTHENTICATOR_NAME));
    Assert.assertEquals(expectedUserMap, actualUserMap);
    // Validate cache user map methods
    Assert.assertEquals(expectedUserMap, updater.getCachedUserMap(AUTHENTICATOR_NAME));
    Assert.assertArrayEquals(expectedSerializeUserMap, updater.getCachedSerializedUserMap(AUTHENTICATOR_NAME));
    // create duplicate should fail
    expectedException.expect(BasicSecurityDBResourceException.class);
    expectedException.expectMessage("User [druid] already exists.");
    updater.createUser(AUTHENTICATOR_NAME, "druid");
}
Also used : BasicAuthenticatorUser(org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorUser) Test(org.junit.Test)

Example 9 with BasicAuthenticatorUser

use of org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorUser in project druid by druid-io.

the class CoordinatorBasicAuthenticatorMetadataStorageUpdaterTest method setCredentials.

@Test
public void setCredentials() {
    updater.createUser(AUTHENTICATOR_NAME, "druid");
    updater.setUserCredentials(AUTHENTICATOR_NAME, "druid", new BasicAuthenticatorCredentialUpdate("helloworld", null));
    Map<String, BasicAuthenticatorUser> userMap = BasicAuthUtils.deserializeAuthenticatorUserMap(objectMapper, updater.getCurrentUserMapBytes(AUTHENTICATOR_NAME));
    BasicAuthenticatorCredentials credentials = userMap.get("druid").getCredentials();
    byte[] recalculatedHash = BasicAuthUtils.hashPassword("helloworld".toCharArray(), credentials.getSalt(), credentials.getIterations());
    Assert.assertArrayEquals(credentials.getHash(), recalculatedHash);
    // Validate cache user map methods
    Map<String, BasicAuthenticatorUser> expectedUserMap = ImmutableMap.of("druid", new BasicAuthenticatorUser("druid", credentials));
    byte[] expectedSerializeUserMap = BasicAuthUtils.serializeAuthenticatorUserMap(objectMapper, expectedUserMap);
    Assert.assertArrayEquals(expectedSerializeUserMap, updater.getCurrentUserMapBytes(AUTHENTICATOR_NAME));
    Assert.assertEquals(expectedUserMap, updater.getCachedUserMap(AUTHENTICATOR_NAME));
    Assert.assertArrayEquals(expectedSerializeUserMap, updater.getCachedSerializedUserMap(AUTHENTICATOR_NAME));
}
Also used : BasicAuthenticatorCredentials(org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorCredentials) BasicAuthenticatorCredentialUpdate(org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorCredentialUpdate) BasicAuthenticatorUser(org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorUser) Test(org.junit.Test)

Example 10 with BasicAuthenticatorUser

use of org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorUser in project druid by druid-io.

the class CoordinatorBasicAuthenticatorResourceTest method testUserCredentials.

@Test
public void testUserCredentials() {
    Response response = resource.createUser(req, AUTHENTICATOR_NAME, "druid");
    Assert.assertEquals(200, response.getStatus());
    response = resource.updateUserCredentials(req, AUTHENTICATOR_NAME, "druid", new BasicAuthenticatorCredentialUpdate("helloworld", null));
    Assert.assertEquals(200, response.getStatus());
    response = resource.getUser(req, AUTHENTICATOR_NAME, "druid");
    Assert.assertEquals(200, response.getStatus());
    BasicAuthenticatorUser actualUser = (BasicAuthenticatorUser) response.getEntity();
    Assert.assertEquals("druid", actualUser.getName());
    BasicAuthenticatorCredentials credentials = actualUser.getCredentials();
    byte[] salt = credentials.getSalt();
    byte[] hash = credentials.getHash();
    int iterations = credentials.getIterations();
    Assert.assertEquals(BasicAuthUtils.SALT_LENGTH, salt.length);
    Assert.assertEquals(BasicAuthUtils.KEY_LENGTH / 8, hash.length);
    Assert.assertEquals(BasicAuthUtils.DEFAULT_KEY_ITERATIONS, iterations);
    byte[] recalculatedHash = BasicAuthUtils.hashPassword("helloworld".toCharArray(), salt, iterations);
    Assert.assertArrayEquals(recalculatedHash, hash);
    response = resource.getCachedSerializedUserMap(req, AUTHENTICATOR_NAME);
    Assert.assertEquals(200, response.getStatus());
    Assert.assertTrue(response.getEntity() instanceof byte[]);
    Map<String, BasicAuthenticatorUser> cachedUserMap = BasicAuthUtils.deserializeAuthenticatorUserMap(objectMapper, (byte[]) response.getEntity());
    Assert.assertNotNull(cachedUserMap);
    Assert.assertNotNull(cachedUserMap.get("druid"));
    Assert.assertEquals("druid", cachedUserMap.get("druid").getName());
    BasicAuthenticatorCredentials cachedUserCredentials = cachedUserMap.get("druid").getCredentials();
    salt = cachedUserCredentials.getSalt();
    hash = cachedUserCredentials.getHash();
    iterations = cachedUserCredentials.getIterations();
    Assert.assertEquals(BasicAuthUtils.SALT_LENGTH, salt.length);
    Assert.assertEquals(BasicAuthUtils.KEY_LENGTH / 8, hash.length);
    Assert.assertEquals(BasicAuthUtils.DEFAULT_KEY_ITERATIONS, iterations);
    recalculatedHash = BasicAuthUtils.hashPassword("helloworld".toCharArray(), salt, iterations);
    Assert.assertArrayEquals(recalculatedHash, hash);
    response = resource.deleteUser(req, AUTHENTICATOR_NAME, "druid");
    Assert.assertEquals(200, response.getStatus());
    response = resource.getUser(req, AUTHENTICATOR_NAME, "druid");
    Assert.assertEquals(400, response.getStatus());
    Assert.assertEquals(errorMapWithMsg("User [druid] does not exist."), response.getEntity());
    response = resource.updateUserCredentials(req, AUTHENTICATOR_NAME, "druid", new BasicAuthenticatorCredentialUpdate("helloworld", null));
    Assert.assertEquals(400, response.getStatus());
    Assert.assertEquals(errorMapWithMsg("User [druid] does not exist."), response.getEntity());
}
Also used : Response(javax.ws.rs.core.Response) BasicAuthenticatorCredentials(org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorCredentials) BasicAuthenticatorCredentialUpdate(org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorCredentialUpdate) BasicAuthenticatorUser(org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorUser) Test(org.junit.Test)

Aggregations

BasicAuthenticatorUser (org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorUser)15 Test (org.junit.Test)6 Response (javax.ws.rs.core.Response)4 BasicHTTPAuthenticator (org.apache.druid.security.basic.authentication.BasicHTTPAuthenticator)4 BasicSecurityDBResourceException (org.apache.druid.security.basic.BasicSecurityDBResourceException)3 BasicAuthenticatorCredentialUpdate (org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorCredentialUpdate)3 BasicAuthenticatorCredentials (org.apache.druid.security.basic.authentication.entity.BasicAuthenticatorCredentials)3 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ISE (org.apache.druid.java.util.common.ISE)2 LifecycleStart (org.apache.druid.java.util.common.lifecycle.LifecycleStart)2 Authenticator (org.apache.druid.server.security.Authenticator)2 Duration (org.joda.time.Duration)2 Nullable (javax.annotation.Nullable)1 IAE (org.apache.druid.java.util.common.IAE)1 Request (org.apache.druid.java.util.http.client.Request)1 BytesFullResponseHandler (org.apache.druid.java.util.http.client.response.BytesFullResponseHandler)1 BytesFullResponseHolder (org.apache.druid.java.util.http.client.response.BytesFullResponseHolder)1 BasicAuthDBConfig (org.apache.druid.security.basic.BasicAuthDBConfig)1 BasicSecurityAuthenticationException (org.apache.druid.security.basic.BasicSecurityAuthenticationException)1