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;
}
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;
}
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));
}
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"));
}
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"));
}
Aggregations