Search in sources :

Example 1 with UsersPrivilegesMetadata

use of io.crate.user.metadata.UsersPrivilegesMetadata 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 UsersPrivilegesMetadata

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

the class UserManagerDDLModifier method transferTablePrivileges.

private static boolean transferTablePrivileges(Metadata.Builder mdBuilder, RelationName sourceRelationName, RelationName targetRelationName) {
    UsersPrivilegesMetadata oldMetadata = (UsersPrivilegesMetadata) mdBuilder.getCustom(UsersPrivilegesMetadata.TYPE);
    if (oldMetadata == null) {
        return false;
    }
    // create a new instance of the metadata if privileges were changed, to guarantee the cluster changed action.
    UsersPrivilegesMetadata newMetadata = UsersPrivilegesMetadata.maybeCopyAndReplaceTableIdents(oldMetadata, sourceRelationName.fqn(), targetRelationName.fqn());
    if (newMetadata != null) {
        mdBuilder.putCustom(UsersPrivilegesMetadata.TYPE, newMetadata);
        return true;
    }
    return false;
}
Also used : UsersPrivilegesMetadata(io.crate.user.metadata.UsersPrivilegesMetadata)

Example 3 with UsersPrivilegesMetadata

use of io.crate.user.metadata.UsersPrivilegesMetadata 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 4 with UsersPrivilegesMetadata

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

the class TransportPrivilegesActionTest method testApplyPrivilegesCreatesNewPrivilegesInstance.

@Test
public void testApplyPrivilegesCreatesNewPrivilegesInstance() {
    // given
    Metadata.Builder mdBuilder = Metadata.builder();
    Map<String, Set<Privilege>> usersPrivileges = new HashMap<>();
    usersPrivileges.put("Ford", new HashSet<>(PRIVILEGES));
    UsersPrivilegesMetadata initialPrivilegesMetadata = new UsersPrivilegesMetadata(usersPrivileges);
    mdBuilder.putCustom(UsersPrivilegesMetadata.TYPE, initialPrivilegesMetadata);
    PrivilegesRequest denyPrivilegeRequest = new PrivilegesRequest(Collections.singletonList("Ford"), Collections.singletonList(DENY_DQL));
    // when
    TransportPrivilegesAction.applyPrivileges(mdBuilder, denyPrivilegeRequest);
    // then
    UsersPrivilegesMetadata newPrivilegesMetadata = (UsersPrivilegesMetadata) mdBuilder.getCustom(UsersPrivilegesMetadata.TYPE);
    assertNotSame(newPrivilegesMetadata, initialPrivilegesMetadata);
}
Also used : UsersPrivilegesMetadata(io.crate.user.metadata.UsersPrivilegesMetadata) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) 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 UsersPrivilegesMetadata

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

the class TransportUserActionTest method testEmptyPrivilegesAreCreatedForNewUsers.

@Test
public void testEmptyPrivilegesAreCreatedForNewUsers() throws Exception {
    Metadata.Builder mdBuilder = new Metadata.Builder();
    TransportCreateUserAction.putUser(mdBuilder, "root", null);
    UsersPrivilegesMetadata metadata = (UsersPrivilegesMetadata) mdBuilder.getCustom(UsersPrivilegesMetadata.TYPE);
    assertThat(metadata.getUserPrivileges("root"), is(Collections.emptySet()));
}
Also used : UsersPrivilegesMetadata(io.crate.user.metadata.UsersPrivilegesMetadata) Metadata(org.elasticsearch.cluster.metadata.Metadata) UsersMetadata(io.crate.user.metadata.UsersMetadata) UsersPrivilegesMetadata(io.crate.user.metadata.UsersPrivilegesMetadata) Test(org.junit.Test)

Aggregations

UsersPrivilegesMetadata (io.crate.user.metadata.UsersPrivilegesMetadata)10 UsersMetadata (io.crate.user.metadata.UsersMetadata)6 Metadata (org.elasticsearch.cluster.metadata.Metadata)4 VisibleForTesting (io.crate.common.annotations.VisibleForTesting)3 Test (org.junit.Test)3 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1