Search in sources :

Example 1 with SyncException

use of org.apache.jackrabbit.oak.spi.security.authentication.external.SyncException in project jackrabbit-oak by apache.

the class ExternalLoginModule method syncUser.

/**
     * Initiates synchronization of the external user.
     * @param user the external user
     * @throws SyncException if an error occurs
     */
private void syncUser(@Nonnull ExternalUser user) throws SyncException {
    Root root = getRoot();
    if (root == null) {
        throw new SyncException("Cannot synchronize user. root == null");
    }
    UserManager userManager = getUserManager();
    if (userManager == null) {
        throw new SyncException("Cannot synchronize user. userManager == null");
    }
    int numAttempt = 0;
    while (numAttempt++ < MAX_SYNC_ATTEMPTS) {
        SyncContext context = null;
        try {
            DebugTimer timer = new DebugTimer();
            context = syncHandler.createContext(idp, userManager, new ValueFactoryImpl(root, NamePathMapper.DEFAULT));
            SyncResult syncResult = context.sync(user);
            timer.mark("sync");
            if (root.hasPendingChanges()) {
                root.commit();
                timer.mark("commit");
            }
            debug("syncUser({}) {}, status: {}", user.getId(), timer.getString(), syncResult.getStatus().toString());
            return;
        } catch (CommitFailedException e) {
            log.warn("User synchronization failed during commit: {}. (attempt {}/{})", e.toString(), numAttempt, MAX_SYNC_ATTEMPTS);
            root.refresh();
        } finally {
            if (context != null) {
                context.close();
            }
        }
    }
    throw new SyncException("User synchronization failed during commit after " + MAX_SYNC_ATTEMPTS + " attempts");
}
Also used : DebugTimer(org.apache.jackrabbit.oak.commons.DebugTimer) Root(org.apache.jackrabbit.oak.api.Root) UserManager(org.apache.jackrabbit.api.security.user.UserManager) ValueFactoryImpl(org.apache.jackrabbit.oak.plugins.value.jcr.ValueFactoryImpl) SyncException(org.apache.jackrabbit.oak.spi.security.authentication.external.SyncException) SyncContext(org.apache.jackrabbit.oak.spi.security.authentication.external.SyncContext) SyncResult(org.apache.jackrabbit.oak.spi.security.authentication.external.SyncResult) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Example 2 with SyncException

use of org.apache.jackrabbit.oak.spi.security.authentication.external.SyncException in project jackrabbit-oak by apache.

the class DefaultSyncContext method sync.

/**
     * {@inheritDoc}
     */
@Nonnull
@Override
public SyncResult sync(@Nonnull ExternalIdentity identity) throws SyncException {
    ExternalIdentityRef ref = identity.getExternalId();
    if (!isSameIDP(ref)) {
        // create result in accordance with sync(String) where status is FOREIGN
        boolean isGroup = (identity instanceof ExternalGroup);
        return new DefaultSyncResultImpl(new DefaultSyncedIdentity(identity.getId(), ref, isGroup, -1), SyncResult.Status.FOREIGN);
    }
    try {
        DebugTimer timer = new DebugTimer();
        DefaultSyncResultImpl ret;
        boolean created = false;
        if (identity instanceof ExternalUser) {
            User user = getAuthorizable(identity, User.class);
            timer.mark("find");
            if (user == null) {
                user = createUser((ExternalUser) identity);
                timer.mark("create");
                created = true;
            }
            ret = syncUser((ExternalUser) identity, user);
            timer.mark("sync");
        } else if (identity instanceof ExternalGroup) {
            Group group = getAuthorizable(identity, Group.class);
            timer.mark("find");
            if (group == null) {
                group = createGroup((ExternalGroup) identity);
                timer.mark("create");
                created = true;
            }
            ret = syncGroup((ExternalGroup) identity, group);
            timer.mark("sync");
        } else {
            throw new IllegalArgumentException("identity must be user or group but was: " + identity);
        }
        if (log.isDebugEnabled()) {
            log.debug("sync({}) -> {} {}", ref.getString(), identity.getId(), timer.getString());
        }
        if (created) {
            ret.setStatus(SyncResult.Status.ADD);
        }
        return ret;
    } catch (RepositoryException e) {
        throw new SyncException(e);
    }
}
Also used : DebugTimer(org.apache.jackrabbit.oak.commons.DebugTimer) Group(org.apache.jackrabbit.api.security.user.Group) ExternalGroup(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalGroup) User(org.apache.jackrabbit.api.security.user.User) ExternalUser(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalUser) ExternalIdentityRef(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityRef) ExternalGroup(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalGroup) ExternalUser(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalUser) RepositoryException(javax.jcr.RepositoryException) SyncException(org.apache.jackrabbit.oak.spi.security.authentication.external.SyncException) Nonnull(javax.annotation.Nonnull)

Example 3 with SyncException

use of org.apache.jackrabbit.oak.spi.security.authentication.external.SyncException in project jackrabbit-oak by apache.

the class Delegatee method syncUser.

@Nonnull
private List<SyncResult> syncUser(@Nonnull ExternalIdentity id, @Nonnull List<SyncResult> results, @Nonnull List<String> list) {
    try {
        SyncResult r = context.sync(id);
        if (r.getIdentity() == null) {
            r = new DefaultSyncResultImpl(new DefaultSyncedIdentity(id.getId(), id.getExternalId(), false, -1), SyncResult.Status.NO_SUCH_IDENTITY);
            log.warn("sync failed. {}", r.getIdentity());
        } else {
            log.info("synced {}", r.getIdentity());
        }
        results.add(r);
    } catch (SyncException e) {
        log.error(ERROR_SYNC_USER, id, e);
        results.add(new ErrorSyncResult(id.getExternalId(), e));
    }
    return commit(list, results, batchSize);
}
Also used : DefaultSyncedIdentity(org.apache.jackrabbit.oak.spi.security.authentication.external.basic.DefaultSyncedIdentity) DefaultSyncResultImpl(org.apache.jackrabbit.oak.spi.security.authentication.external.basic.DefaultSyncResultImpl) SyncException(org.apache.jackrabbit.oak.spi.security.authentication.external.SyncException) SyncResult(org.apache.jackrabbit.oak.spi.security.authentication.external.SyncResult) Nonnull(javax.annotation.Nonnull)

Example 4 with SyncException

use of org.apache.jackrabbit.oak.spi.security.authentication.external.SyncException in project jackrabbit-oak by apache.

the class ExternalLoginModule method validateUser.

/**
     * Initiates synchronization of a possible remove user
     * @param id the user id
     */
private void validateUser(@Nonnull String id) throws SyncException {
    SyncContext context = null;
    try {
        Root root = getRoot();
        if (root == null) {
            throw new SyncException("Cannot synchronize user. root == null");
        }
        UserManager userManager = getUserManager();
        if (userManager == null) {
            throw new SyncException("Cannot synchronize user. userManager == null");
        }
        DebugTimer timer = new DebugTimer();
        context = syncHandler.createContext(idp, userManager, new ValueFactoryImpl(root, NamePathMapper.DEFAULT));
        context.sync(id);
        timer.mark("sync");
        root.commit();
        timer.mark("commit");
        debug("validateUser({}) {}", id, timer.getString());
    } catch (CommitFailedException e) {
        throw new SyncException("User synchronization failed during commit.", e);
    } finally {
        if (context != null) {
            context.close();
        }
    }
}
Also used : DebugTimer(org.apache.jackrabbit.oak.commons.DebugTimer) Root(org.apache.jackrabbit.oak.api.Root) UserManager(org.apache.jackrabbit.api.security.user.UserManager) ValueFactoryImpl(org.apache.jackrabbit.oak.plugins.value.jcr.ValueFactoryImpl) SyncException(org.apache.jackrabbit.oak.spi.security.authentication.external.SyncException) SyncContext(org.apache.jackrabbit.oak.spi.security.authentication.external.SyncContext) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Example 5 with SyncException

use of org.apache.jackrabbit.oak.spi.security.authentication.external.SyncException in project jackrabbit-oak by apache.

the class DefaultSyncContext method sync.

/**
     * {@inheritDoc}
     */
@Nonnull
@Override
public SyncResult sync(@Nonnull String id) throws SyncException {
    try {
        DebugTimer timer = new DebugTimer();
        DefaultSyncResultImpl ret;
        // find authorizable
        Authorizable auth = userManager.getAuthorizable(id);
        if (auth == null) {
            return new DefaultSyncResultImpl(new DefaultSyncedIdentity(id, null, false, -1), SyncResult.Status.NO_SUCH_AUTHORIZABLE);
        }
        // check if we need to deal with this authorizable
        ExternalIdentityRef ref = getIdentityRef(auth);
        if (ref == null || !isSameIDP(ref)) {
            return new DefaultSyncResultImpl(new DefaultSyncedIdentity(id, ref, auth.isGroup(), -1), SyncResult.Status.FOREIGN);
        }
        if (auth.isGroup()) {
            ExternalGroup external = idp.getGroup(id);
            timer.mark("retrieve");
            if (external == null) {
                ret = handleMissingIdentity(id, auth, timer);
            } else {
                ret = syncGroup(external, (Group) auth);
                timer.mark("sync");
            }
        } else {
            ExternalUser external = idp.getUser(id);
            timer.mark("retrieve");
            if (external == null) {
                ret = handleMissingIdentity(id, auth, timer);
            } else {
                ret = syncUser(external, (User) auth);
                timer.mark("sync");
            }
        }
        if (log.isDebugEnabled()) {
            log.debug("sync({}) -> {} {}", id, ref.getString(), timer.getString());
        }
        return ret;
    } catch (RepositoryException e) {
        throw new SyncException(e);
    } catch (ExternalIdentityException e) {
        throw new SyncException(e);
    }
}
Also used : DebugTimer(org.apache.jackrabbit.oak.commons.DebugTimer) Group(org.apache.jackrabbit.api.security.user.Group) ExternalGroup(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalGroup) User(org.apache.jackrabbit.api.security.user.User) ExternalUser(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalUser) ExternalIdentityRef(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityRef) ExternalGroup(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalGroup) ExternalUser(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalUser) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) RepositoryException(javax.jcr.RepositoryException) SyncException(org.apache.jackrabbit.oak.spi.security.authentication.external.SyncException) ExternalIdentityException(org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityException) Nonnull(javax.annotation.Nonnull)

Aggregations

SyncException (org.apache.jackrabbit.oak.spi.security.authentication.external.SyncException)5 DebugTimer (org.apache.jackrabbit.oak.commons.DebugTimer)4 Nonnull (javax.annotation.Nonnull)3 RepositoryException (javax.jcr.RepositoryException)2 Group (org.apache.jackrabbit.api.security.user.Group)2 User (org.apache.jackrabbit.api.security.user.User)2 UserManager (org.apache.jackrabbit.api.security.user.UserManager)2 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)2 Root (org.apache.jackrabbit.oak.api.Root)2 ValueFactoryImpl (org.apache.jackrabbit.oak.plugins.value.jcr.ValueFactoryImpl)2 ExternalGroup (org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalGroup)2 ExternalIdentityRef (org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityRef)2 ExternalUser (org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalUser)2 SyncContext (org.apache.jackrabbit.oak.spi.security.authentication.external.SyncContext)2 SyncResult (org.apache.jackrabbit.oak.spi.security.authentication.external.SyncResult)2 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)1 ExternalIdentityException (org.apache.jackrabbit.oak.spi.security.authentication.external.ExternalIdentityException)1 DefaultSyncResultImpl (org.apache.jackrabbit.oak.spi.security.authentication.external.basic.DefaultSyncResultImpl)1 DefaultSyncedIdentity (org.apache.jackrabbit.oak.spi.security.authentication.external.basic.DefaultSyncedIdentity)1