Commit 49b5c7e3 authored by Jeff Burke's avatar Jeff Burke
Browse files

ac2 rework: fixes for unit and int tests

parent efd157bb
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -260,6 +260,10 @@ public abstract class LdapDAO
        {
            throw new TransientException("ldap timeout");
        }
        else if (code == ResultCode.INVALID_DN_SYNTAX)
        {
            throw new IllegalArgumentException("Invalid DN syntax");
        }

        throw new RuntimeException("Ldap error (" + code.getName() + ")");
    }
+30 −4
Original line number Diff line number Diff line
@@ -285,7 +285,16 @@ public class LdapUserDAO<T extends Principal> extends LdapDAO
        DN userDN;
        try
        {
            userDN = getUserRequestsDN(userRequest.getUser().getUserID().getName());
            T userID = userRequest.getUser().getUserID();
            try
            {
                getUser(userID, config.getUsersDN(), false);
                throw new UserAlreadyExistsException(userID.getName() + " found in " +
                                                     config.getUsersDN());
            }
            catch (UserNotFoundException ignore) {}

            userDN = getUserRequestsDN(userID.getName());
            addUser(userRequest, userDN);

            // AD: Search results sometimes come incomplete if
@@ -293,7 +302,7 @@ public class LdapUserDAO<T extends Principal> extends LdapDAO
            getConnection().reconnect();
            try
            {
                return getUser(userRequest.getUser().getUserID(), config.getUserRequestsDN());
                return getUser(userID, config.getUserRequestsDN());
            }
            catch (UserNotFoundException e)
            {
@@ -443,7 +452,6 @@ public class LdapUserDAO<T extends Principal> extends LdapDAO
        return getUser(userID, config.getUserRequestsDN());
    }


    /**
     * Get the user specified by userID.
     *
@@ -457,6 +465,24 @@ public class LdapUserDAO<T extends Principal> extends LdapDAO
    private User<T> getUser(final T userID, final String usersDN)
        throws UserNotFoundException, TransientException,
        AccessControlException
    {
        return getUser(userID, usersDN, true);
    }

    /**
     * Get the user specified by userID.
     *
     * @param userID  The userID.
     * @param usersDN The LDAP tree to search.
     * @param proxy   If true proxy the request as the calling user.
     * @return User instance.
     * @throws UserNotFoundException  when the user is not found.
     * @throws TransientException     If an temporary, unexpected problem occurred.
     * @throws AccessControlException If the operation is not permitted.
     */
    private User<T> getUser(final T userID, final String usersDN, boolean proxy)
            throws UserNotFoundException, TransientException,
                   AccessControlException
    {
        String searchField = userLdapAttrib.get(userID.getClass());
        if (searchField == null)
@@ -474,7 +500,7 @@ public class LdapUserDAO<T extends Principal> extends LdapDAO
            SearchRequest searchRequest =
                    new SearchRequest(usersDN, SearchScope.SUB,
                                      searchField, userAttribs);
            if (isSecure(usersDN))
            if (proxy && isSecure(usersDN))
            {
                searchRequest.addControl(
                        new ProxiedAuthorizationV2RequestControl(
+4 −2
Original line number Diff line number Diff line
@@ -97,16 +97,18 @@ public class SyncOutput

    public void setCode(int code)
    {
        log.debug("setting code");
        if (writer != null)
            return;
            throw new IllegalStateException("attempted to set code after writer has been opened");

        response.setStatus(code);
        log.debug("set code " + code);
    }

    public void setHeader(String key, Object value)
    {
        if (writer != null)
            return;
            throw new IllegalStateException("attempted to set header after writer has been opened");

        if (value == null)
            response.setHeader(key, null);
+18 −26
Original line number Diff line number Diff line
@@ -69,7 +69,9 @@
package ca.nrc.cadc.ac.server.web.users;

import ca.nrc.cadc.ac.PersonalDetails;
import ca.nrc.cadc.ac.ReaderException;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.ac.UserAlreadyExistsException;
import ca.nrc.cadc.ac.UserNotFoundException;
import ca.nrc.cadc.ac.UserRequest;
import ca.nrc.cadc.ac.json.JsonUserListWriter;
@@ -158,6 +160,13 @@ public abstract class AbstractUserAction implements PrivilegedExceptionAction<Ob
            this.logInfo.setMessage(message);
            sendError(400, message);
        }
        catch (ReaderException e)
        {
            log.debug(e.getMessage(), e);
            String message = e.getMessage();
            this.logInfo.setMessage(message);
            sendError(400, message);
        }
        catch (UserNotFoundException e)
        {
            log.debug(e.getMessage(), e);
@@ -165,6 +174,13 @@ public abstract class AbstractUserAction implements PrivilegedExceptionAction<Ob
            this.logInfo.setMessage(message);
            sendError(404, message);
        }
        catch (UserAlreadyExistsException e)
        {
            log.debug(e.getMessage(), e);
            String message = "User not found: " + e.getMessage();
            this.logInfo.setMessage(message);
            sendError(409, message);
        }
        catch (UnsupportedOperationException e)
        {
            log.debug(e.getMessage(), e);
@@ -198,6 +214,7 @@ public abstract class AbstractUserAction implements PrivilegedExceptionAction<Ob

    private void sendError(int responseCode, String message)
    {
        syncOut.setCode(responseCode);
        syncOut.setHeader("Content-Type", "text/plain");
        if (message != null)
        {
@@ -210,7 +227,6 @@ public abstract class AbstractUserAction implements PrivilegedExceptionAction<Ob
                log.warn("Could not write error message to output stream");
            }
        }
        syncOut.setCode(responseCode);
    }

    @SuppressWarnings("unchecked")
@@ -344,28 +360,4 @@ public abstract class AbstractUserAction implements PrivilegedExceptionAction<Ob
        }
    }

    void redirectGet(User<?> user) throws Exception
    {
        final Set<Principal> httpPrincipals =  user.getIdentities();

        String id = null;
        String idType = null;
        Iterator<Principal> i = httpPrincipals.iterator();
        Principal next = null;
        while (idType == null && i.hasNext())
        {
            next = i.next();
            idType = AuthenticationUtil.getPrincipalType(next);
            id = next.getName();
        }

        if (idType == null)
        {
            throw new IllegalStateException("No identities found.");
        }

        final String redirectURL = "/" + id + "?idType=" + idType;
        syncOut.setHeader("Location", redirectURL);
        syncOut.setCode(303);
    }
}
+5 −2
Original line number Diff line number Diff line
@@ -84,13 +84,15 @@ import java.util.Set;
public class ModifyUserAction extends AbstractUserAction
{
    private final InputStream inputStream;
    private final String request;


    ModifyUserAction(final InputStream inputStream)
    ModifyUserAction(final InputStream inputStream, final String request)
    {
        super();

        this.inputStream = inputStream;
        this.request = request;
    }


@@ -100,7 +102,8 @@ public class ModifyUserAction extends AbstractUserAction
        final User<Principal> modifiedUser = modifyUser(user);
        logUserInfo(modifiedUser.getUserID().getName());

        redirectGet(modifiedUser);
        syncOut.setHeader("Location", request);
        syncOut.setCode(303);
    }

    /**
Loading