Search in sources :

Example 1 with UsersMetadata

use of io.crate.user.metadata.UsersMetadata in project crate by crate.

the class TransportCreateUserAction method putUser.

/**
 * Puts a user into the meta data and creates an empty privileges set.
 *
 * @return boolean true if the user already exists, otherwise false
 */
@VisibleForTesting
static boolean putUser(Metadata.Builder mdBuilder, String name, @Nullable SecureHash secureHash) {
    UsersMetadata oldMetadata = (UsersMetadata) mdBuilder.getCustom(UsersMetadata.TYPE);
    if (oldMetadata != null && oldMetadata.contains(name)) {
        return true;
    }
    // create a new instance of the metadata, to guarantee the cluster changed action.
    UsersMetadata newMetadata = UsersMetadata.newInstance(oldMetadata);
    newMetadata.put(name, secureHash);
    assert !newMetadata.equals(oldMetadata) : "must not be equal to guarantee the cluster change action";
    mdBuilder.putCustom(UsersMetadata.TYPE, newMetadata);
    // create empty privileges for this user
    UsersPrivilegesMetadata privilegesMetadata = UsersPrivilegesMetadata.copyOf((UsersPrivilegesMetadata) mdBuilder.getCustom(UsersPrivilegesMetadata.TYPE));
    privilegesMetadata.createPrivileges(name, Collections.emptySet());
    mdBuilder.putCustom(UsersPrivilegesMetadata.TYPE, privilegesMetadata);
    return false;
}
Also used : UsersPrivilegesMetadata(io.crate.user.metadata.UsersPrivilegesMetadata) UsersMetadata(io.crate.user.metadata.UsersMetadata) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 2 with UsersMetadata

use of io.crate.user.metadata.UsersMetadata in project crate by crate.

the class TransportDropUserAction method dropUser.

@VisibleForTesting
static boolean dropUser(Metadata.Builder mdBuilder, @Nullable UsersMetadata oldMetadata, String name) {
    if (oldMetadata == null || oldMetadata.contains(name) == false) {
        return false;
    }
    // create a new instance of the metadata, to guarantee the cluster changed action.
    UsersMetadata newMetadata = UsersMetadata.newInstance(oldMetadata);
    newMetadata.remove(name);
    assert !newMetadata.equals(oldMetadata) : "must not be equal to guarantee the cluster change action";
    mdBuilder.putCustom(UsersMetadata.TYPE, newMetadata);
    // removes all privileges for this user
    UsersPrivilegesMetadata privilegesMetadata = UsersPrivilegesMetadata.copyOf((UsersPrivilegesMetadata) mdBuilder.getCustom(UsersPrivilegesMetadata.TYPE));
    privilegesMetadata.dropPrivileges(name);
    mdBuilder.putCustom(UsersPrivilegesMetadata.TYPE, privilegesMetadata);
    return true;
}
Also used : UsersPrivilegesMetadata(io.crate.user.metadata.UsersPrivilegesMetadata) UsersMetadata(io.crate.user.metadata.UsersMetadata) VisibleForTesting(io.crate.common.annotations.VisibleForTesting)

Example 3 with UsersMetadata

use of io.crate.user.metadata.UsersMetadata in project crate by crate.

the class TransportPrivilegesActionTest method testValidateUserNamesAllExists.

@Test
public void testValidateUserNamesAllExists() throws Exception {
    Metadata metadata = Metadata.builder().putCustom(UsersMetadata.TYPE, new UsersMetadata(UserDefinitions.DUMMY_USERS)).build();
    List<String> unknownUserNames = TransportPrivilegesAction.validateUserNames(metadata, List.of("Ford", "Arthur"));
    assertThat(unknownUserNames.size(), is(0));
}
Also used : UsersMetadata(io.crate.user.metadata.UsersMetadata) UsersMetadata(io.crate.user.metadata.UsersMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) UsersPrivilegesMetadata(io.crate.user.metadata.UsersPrivilegesMetadata) Test(org.junit.Test)

Example 4 with UsersMetadata

use of io.crate.user.metadata.UsersMetadata in project crate by crate.

the class TransportPrivilegesActionTest method testValidateUserNamesMissingUser.

@Test
public void testValidateUserNamesMissingUser() throws Exception {
    Metadata metadata = Metadata.builder().putCustom(UsersMetadata.TYPE, new UsersMetadata(UserDefinitions.SINGLE_USER_ONLY)).build();
    List<String> userNames = List.of("Ford", "Arthur");
    List<String> unknownUserNames = TransportPrivilegesAction.validateUserNames(metadata, userNames);
    assertThat(unknownUserNames, contains("Ford"));
}
Also used : UsersMetadata(io.crate.user.metadata.UsersMetadata) UsersMetadata(io.crate.user.metadata.UsersMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) UsersPrivilegesMetadata(io.crate.user.metadata.UsersPrivilegesMetadata) Test(org.junit.Test)

Example 5 with UsersMetadata

use of io.crate.user.metadata.UsersMetadata in project crate by crate.

the class TransportUserActionTest method testCreateUser.

@Test
public void testCreateUser() throws Exception {
    Metadata.Builder mdBuilder = new Metadata.Builder().putCustom(UsersMetadata.TYPE, new UsersMetadata(UserDefinitions.SINGLE_USER_ONLY));
    TransportCreateUserAction.putUser(mdBuilder, "Trillian", null);
    UsersMetadata newMetadata = (UsersMetadata) mdBuilder.getCustom(UsersMetadata.TYPE);
    assertThat(newMetadata.userNames(), containsInAnyOrder("Trillian", "Arthur"));
}
Also used : UsersMetadata(io.crate.user.metadata.UsersMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) UsersMetadata(io.crate.user.metadata.UsersMetadata) UsersPrivilegesMetadata(io.crate.user.metadata.UsersPrivilegesMetadata) Test(org.junit.Test)

Aggregations

UsersMetadata (io.crate.user.metadata.UsersMetadata)13 UsersPrivilegesMetadata (io.crate.user.metadata.UsersPrivilegesMetadata)10 Test (org.junit.Test)8 Metadata (org.elasticsearch.cluster.metadata.Metadata)7 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)4 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)1 ArrayList (java.util.ArrayList)1