me.prettyprint.cassandra.serializers.CompositeSerializer Java Examples

The following examples show how to use me.prettyprint.cassandra.serializers.CompositeSerializer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CassandraUserStoreManager.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if the role is existing the role store.
 */
@Override
protected boolean doCheckExistingRole(String roleNameWithTenantDomain) throws UserStoreException {

    RoleContext roleContext = createRoleContext(roleNameWithTenantDomain);
    boolean isExisting = false;

    String roleName = roleContext.getRoleName();

    Composite key = new Composite();
    key.addComponent(roleName, stringSerializer);
    key.addComponent(tenantIdString, stringSerializer);

    ColumnQuery<Composite, String, String> getCredentialQuery = HFactory.createColumnQuery(keyspace,
            CompositeSerializer.get(), stringSerializer, stringSerializer);

    getCredentialQuery.setColumnFamily(CFConstants.UM_ROLES).setKey(key).setName(CFConstants.UM_ROLE_NAME);

    HColumn<String, String> result = getCredentialQuery.execute().get();
    if (result != null && result.getValue() != null) {
        isExisting = true;
    }

    return isExisting;
}
 
Example #2
Source File: CassandraUserStoreManager.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Checks if the user is existing in the user store.
 */
@Override
protected boolean doCheckExistingUser(String userName) throws UserStoreException {

    Boolean isExist = false;

    Composite key = new Composite();
    key.addComponent(userName, stringSerializer);
    key.addComponent(tenantIdString, stringSerializer);

    ColumnQuery<Composite, String, String> getCredentialQuery = HFactory.createColumnQuery(keyspace,
            CompositeSerializer.get(), stringSerializer, stringSerializer);

    getCredentialQuery.setColumnFamily(CFConstants.UM_USER).setKey(key).setName(CFConstants.UM_USER_NAME);

    HColumn<String, String> result = getCredentialQuery.execute().get();
    if (result != null && result.getValue() != null) {
        isExist = true;
    }

    return isExist;

}
 
Example #3
Source File: CassandraUserStoreManager.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Adds a role to the role store.
 */
@Override
public void doAddRole(String roleName, String[] userList, boolean shared) throws UserStoreException {

    Mutator<Composite> mutator = HFactory.createMutator(keyspace, CompositeSerializer.get());
    Composite composite = new Composite();
    composite.addComponent(roleName, stringSerializer);
    composite.addComponent(tenantIdString, stringSerializer);

    mutator.addInsertion(composite, CFConstants.UM_ROLES,
            HFactory.createColumn(CFConstants.UM_ROLE_NAME, roleName, stringSerializer, stringSerializer));
    mutator.addInsertion(composite, CFConstants.UM_ROLES,
            HFactory.createColumn(CFConstants.UM_TENANT_ID, tenantIdString, stringSerializer, stringSerializer));

    if (userList != null && userList.length > 0) {
        addRoleToUsersList(userList, roleName, mutator);
    }

    mutator.execute();
}
 
Example #4
Source File: CassandraUserStoreManager.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Maps the users to a role list. Adds the (username, tenantId) -> roleList
 * and (role, tenantId) -> userName
 *
 * @param userName The username of the user the roles need to be added to.
 * @param roleList The list of roles that needs to be mapped against the user.
 */
private void addUserToRoleList(String userName, String[] roleList) {

    Mutator<Composite> mutator = HFactory.createMutator(keyspace, CompositeSerializer.get());

    if (roleList != null) {
        for (String role : roleList) {
            Composite key = new Composite();
            key.addComponent(userName, stringSerializer);
            key.addComponent(tenantIdString, stringSerializer);

            mutator.addInsertion(key, CFConstants.UM_USER_ROLE, HFactory.createColumn(role, role));

            Composite keyRole = new Composite();
            keyRole.addComponent(role, stringSerializer);
            keyRole.addComponent(tenantIdString, stringSerializer);

            mutator.addInsertion(keyRole, CFConstants.UM_ROLE_USER_INDEX, HFactory.createColumn(userName, userName));

        }
        mutator.execute();
    }
}
 
Example #5
Source File: CassandraUserStoreManager.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Get the list of users mapped to a role.
 */
@Override
public String[] doGetUserListOfRole(String roleName, String filter) throws UserStoreException {

    List<String> usersList = new ArrayList<String>();
    Composite key = new Composite();
    key.addComponent(roleName, stringSerializer);
    key.addComponent(tenantIdString, stringSerializer);
    SliceQuery<Composite, String, String> query = HFactory
            .createSliceQuery(keyspace, CompositeSerializer.get(), StringSerializer.get(), StringSerializer.get())
            .setKey(key).setColumnFamily(CFConstants.UM_ROLE_USER_INDEX);

    ColumnSliceIterator<Composite, String, String> iterator = new ColumnSliceIterator<Composite, String, String>(
            query, null, "\uFFFF", false);

    while (iterator.hasNext()) {
        HColumn<String, String> column = iterator.next();
        usersList.add(column.getValue());
    }
    return usersList.toArray(new String[usersList.size()]);
}
 
Example #6
Source File: CassandraUserStoreManager.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the external role list of a user.
 */
@Override
public String[] doGetExternalRoleListOfUser(String userName, String filter) throws UserStoreException {

    List<String> roles = new ArrayList<String>();
    int arrayLength = 0;
    Composite key = new Composite();
    key.addComponent(userName, stringSerializer);
    key.addComponent(tenantIdString, stringSerializer);
    SliceQuery<Composite, String, String> query = HFactory
            .createSliceQuery(keyspace, CompositeSerializer.get(), StringSerializer.get(), StringSerializer.get())
            .setKey(key).setColumnFamily(CFConstants.UM_USER_ROLE);

    ColumnSliceIterator<Composite, String, String> iterator = new ColumnSliceIterator<Composite, String, String>(
            query, null, "\uFFFF", false);

    while (iterator.hasNext()) {
        HColumn<String, String> column = iterator.next();
        roles.add(column.getValue());
    }
    return roles.toArray(new String[arrayLength]);
}
 
Example #7
Source File: CassandraUserStoreManager.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes a user by userName.
 */
@Override
public void doDeleteUser(String userName) throws UserStoreException {

    Mutator<Composite> mutator = HFactory.createMutator(keyspace, CompositeSerializer.get());
    String[] roles = doGetExternalRoleListOfUser(userName, "");
    for (String role : roles) {
        Composite key = new Composite();
        key.addComponent(role, stringSerializer);
        key.addComponent(tenantIdString, stringSerializer);
        ColumnFamilyTemplate<Composite, String> userCFTemplate = new ThriftColumnFamilyTemplate<Composite, String>(
                keyspace, CFConstants.UM_ROLE_USER_INDEX, CompositeSerializer.get(), StringSerializer.get());
        try {
            userCFTemplate.deleteColumn(key, userName);
        } catch (HectorException e) {
            log.error("Error during deletion ", e);
        }
    }

    Composite userKey = new Composite();
    userKey.addComponent(userName, stringSerializer);
    userKey.addComponent(tenantIdString, stringSerializer);
    mutator.addDeletion(userKey, CFConstants.UM_USER_ROLE, null, CompositeSerializer.get());
    mutator.addDeletion(userKey, CFConstants.UM_USER, null, CompositeSerializer.get());
    mutator.execute();

    if (log.isDebugEnabled()) {
        log.debug("Deleted user " + userName + " successfully");
    }
}
 
Example #8
Source File: CassandraUserStoreManager.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
@Override
public void doUpdateCredentialByAdmin(String userName, Object newCredential) throws UserStoreException {
    if (!checkUserPasswordValid(newCredential)) {
        throw new UserStoreException(
                "Credential not valid. Credential must be a non null string with following format, "
                        + realmConfig.getUserStoreProperty(UserCoreConstants.RealmConfig.PROPERTY_JAVA_REG_EX));

    }

    String saltValue = null;
    if (TRUE.equalsIgnoreCase(realmConfig.getUserStoreProperties().get(JDBCRealmConstants.STORE_SALTED_PASSWORDS))) {
        saltValue = Util.getSaltValue();
    }
    String password = Util.preparePassword((String) newCredential, saltValue);
    Mutator<Composite> mutator = HFactory.createMutator(keyspace, CompositeSerializer.get());
    Composite key = new Composite();
    key.addComponent(userName, stringSerializer);
    key.addComponent(tenantIdString, stringSerializer);
    mutator.addInsertion(key, CFConstants.UM_USER,
            HFactory.createColumn(CFConstants.UM_SECRET, password, stringSerializer, stringSerializer));
    mutator.addInsertion(key, CFConstants.UM_USER,
            HFactory.createColumn(CFConstants.UM_SALT_VALUE, saltValue, stringSerializer, stringSerializer));
    try {
        mutator.execute();
        if (log.isDebugEnabled()) {
            log.debug("Changed password for user " + userName + "successfully");
        }
    } catch (HectorException e) {
        throw new UserStoreException("Change Password failed.", e);
    }
}
 
Example #9
Source File: CassandraUserStoreManager.java    From carbon-identity with Apache License 2.0 5 votes vote down vote up
/**
 * Authenticates a user given the user name and password against the user
 * store.
 */
@Override
public boolean doAuthenticate(String userName, Object credential) throws UserStoreException {

    String password = (String) credential;
    boolean isAuthed = false;
    if (!checkUserNameValid(userName)) {
        log.error("Invalid Username");
        return false;
    }

    if (!checkUserPasswordValid(credential)) {
        log.error("Invalid password");
        return false;
    }

    if (UserCoreUtil.isRegistryAnnonymousUser(userName)) {
        log.error("Anonnymous user trying to login");
        return false;
    }

    Composite key = new Composite();
    key.addComponent(userName, stringSerializer);
    key.addComponent(tenantIdString, stringSerializer);

    ColumnFamilyTemplate<Composite, String> userCFTemplate = new ThriftColumnFamilyTemplate<Composite, String>(
            keyspace, CFConstants.UM_USER, CompositeSerializer.get(), StringSerializer.get());

    ColumnFamilyResult<Composite, String> result = userCFTemplate.queryColumns(key);
    String saltVallue = result.getString(CFConstants.UM_SALT_VALUE);
    String storedPassword = result.getString(CFConstants.UM_SECRET);

    if (TRUE.equalsIgnoreCase(realmConfig.getUserStoreProperty(JDBCRealmConstants.STORE_SALTED_PASSWORDS))) {
        password = Util.preparePassword(password, saltVallue);
        if ((storedPassword != null) && (storedPassword.equals(password))) {
            isAuthed = true;
        }
    }
    return isAuthed;
}
 
Example #10
Source File: BackplaneConfiguration.java    From elasticactors with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void initialize() {
    String cassandraHosts = env.getProperty("ea.cassandra.hosts","localhost:9160");
    CassandraHostConfigurator hostConfigurator = new CassandraHostConfigurator(cassandraHosts);
    hostConfigurator.setAutoDiscoverHosts(false);
    hostConfigurator.setMaxActive(env.getProperty("ea.cassandra.maxActive",Integer.class,Runtime.getRuntime().availableProcessors() * 3));
    hostConfigurator.setRetryDownedHosts(true);
    hostConfigurator.setRetryDownedHostsDelayInSeconds(env.getProperty("ea.cassandra.retryDownedHostsDelayInSeconds",Integer.class,1));
    hostConfigurator.setMaxWaitTimeWhenExhausted(2000L);
    String cassandraClusterName = env.getProperty("ea.cassandra.cluster","ElasticActorsCluster");
    // it seems that there are issues with the CassandraHostRetryService and retrying downed hosts
    // if we don't let the HFactory manage the cluster then CassandraHostRetryService doesn't try to
    // be smart about finding out if a host was removed from the ring and so it will keep on retrying
    // all configured hosts (and ultimately fail-back when the host comes back online)
    // the default is TRUE, which will let HFactory manage the cluster
    Boolean manageClusterThroughHFactory = env.getProperty("ea.cassandra.hfactory.manageCluster", Boolean.class, Boolean.TRUE);
    Cluster cluster;
    if(manageClusterThroughHFactory) {
        cluster = HFactory.getOrCreateCluster(cassandraClusterName, hostConfigurator);
    } else {
        cluster = new ThriftCluster(cassandraClusterName, hostConfigurator, null);
    }
    String cassandraKeyspaceName = env.getProperty("ea.cassandra.keyspace","ElasticActors");
    Keyspace keyspace = HFactory.createKeyspace(cassandraKeyspaceName,cluster);
    persistentActorsColumnFamilyTemplate =
        new ThriftColumnFamilyTemplate<>(keyspace,"PersistentActors", CompositeSerializer.get(),StringSerializer.get());
    scheduledMessagesColumnFamilyTemplate =
        new ThriftColumnFamilyTemplate<>(keyspace,"ScheduledMessages",CompositeSerializer.get(), CompositeSerializer.get());
    actorSystemEventListenersColumnFamilyTemplate =
            new ThriftColumnFamilyTemplate<>(keyspace,"ActorSystemEventListeners", CompositeSerializer.get(),StringSerializer.get());
    // return
    // @TODO: make this configurable and use the ColumnSliceIterator
    scheduledMessagesColumnFamilyTemplate.setCount(Integer.MAX_VALUE);
    actorSystemEventListenersColumnFamilyTemplate.setCount(Integer.MAX_VALUE);
}
 
Example #11
Source File: CassandraUserStoreManager.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Adds the user to the user store.
 */
@Override
public void doAddUser(String userName, Object credential, String[] roleList, Map<String, String> claims,
                      String profileName, boolean requirePasswordChange) throws UserStoreException {

    String userId = UUID.randomUUID().toString();
    String saltValue = null;

    if (TRUE.equalsIgnoreCase(realmConfig.getUserStoreProperties().get(JDBCRealmConstants.STORE_SALTED_PASSWORDS))) {
        saltValue = Util.getSaltValue();
    }

    String password = Util.preparePassword((String) credential, saltValue);

    if (doCheckExistingUser(userName)) {

        String message = "User with credentials " + userName + "exists";

        UserStoreException userStoreException = new UserStoreException(message);
        log.error(message, userStoreException);
        throw userStoreException;
    } else {

        Mutator<Composite> mutator = HFactory.createMutator(keyspace, CompositeSerializer.get());

        Composite key = new Composite();
        key.addComponent(userName, stringSerializer);
        key.addComponent(tenantIdString, stringSerializer);

        // add user ID
        mutator.addInsertion(key, CFConstants.UM_USER,
                HFactory.createColumn(CFConstants.UM_USER_ID, userId, stringSerializer, stringSerializer));
        mutator.addInsertion(key, CFConstants.UM_USER,
                HFactory.createColumn(CFConstants.UM_USER_NAME, userName, stringSerializer, stringSerializer));
        mutator.addInsertion(key, CFConstants.UM_USER,
                HFactory.createColumn(CFConstants.UM_SECRET, password, stringSerializer, stringSerializer));
        mutator.addInsertion(key, CFConstants.UM_USER,
                HFactory.createColumn(CFConstants.UM_SALT_VALUE, saltValue, stringSerializer, stringSerializer));
        mutator.addInsertion(key, CFConstants.UM_USER, HFactory.createColumn(CFConstants.UM_REQUIRE_CHANGE_BOOLEAN,
                "false", stringSerializer, stringSerializer));
        mutator.addInsertion(key, CFConstants.UM_USER,
                HFactory.createColumn(CFConstants.UM_TENANT_ID, tenantIdString, stringSerializer, stringSerializer));
        mutator = addUserToRoleList(userName, roleList, mutator);

        if (claims != null) {
            mutator = addClaimsForUser(userId, claims, mutator);
        }

        try {
            mutator.execute();
            if (log.isDebugEnabled()) {
                log.debug("Added user " + userName + " successfully");
            }
        } catch (HectorException e) {
            // TODO- research and check how to identify cassandra failure
            // and handle it efficiently.
            throw new UserStoreException("Adding user failed.", e);
        }
        mutator.execute();

    }
}
 
Example #12
Source File: CassandraUserStoreManager.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * Update the user list mapped to a role.
 */
@Override
public void doUpdateUserListOfRole(String roleName, String[] deletedUsers, String[] newUsers)
        throws UserStoreException {

    Mutator<Composite> mutator = HFactory.createMutator(keyspace, CompositeSerializer.get());
    RoleContext ctx = createRoleContext(roleName);
    roleName = ctx.getRoleName();
    boolean isShared = ctx.isShared();
    if (!isShared) {
        //TODO TO BE Implemented
    }
    if (deletedUsers != null && deletedUsers.length > 0) {
        if (isShared) {
            //TODO TO BE Implemented
        } else {
            if (deletedUsers.length > 0) {
                Composite key = new Composite();
                key.addComponent(roleName, stringSerializer);
                key.addComponent(tenantIdString, stringSerializer);

                for (String user : deletedUsers) {

                    Composite userKey = new Composite();
                    userKey.addComponent(user, stringSerializer);
                    userKey.addComponent(tenantIdString, stringSerializer);

                    ColumnFamilyTemplate<Composite, String> userCFTemplate = new ThriftColumnFamilyTemplate<Composite, String>(
                            keyspace, CFConstants.UM_USER_ROLE, CompositeSerializer.get(), StringSerializer.get());
                    ColumnFamilyTemplate<Composite, String> roleCFTemplate = new ThriftColumnFamilyTemplate<Composite, String>(
                            keyspace, CFConstants.UM_ROLE_USER_INDEX, CompositeSerializer.get(),
                            StringSerializer.get());
                    try {
                        roleCFTemplate.deleteColumn(mutator, key, user);
                        userCFTemplate.deleteColumn(mutator, userKey, roleName);
                    } catch (HectorException e) {
                        log.error(e.getMessage(), e);
                        throw new UserStoreException("Error during the updating of a user's role list");
                    }
                }
            }

        }
    }
    // need to clear user roles cache upon roles update
    clearUserRolesCacheByTenant(this.tenantId);

    if (newUsers != null && newUsers.length > 0) {
        if (isShared) {
            //TODO TO BE Implemented
        } else {
            addRoleToUsersList(newUsers, roleName, mutator);
        }
    }
    mutator.execute();

}