Search in sources :

Example 1 with UserNotFoundException

use of org.apache.archiva.redback.users.UserNotFoundException in project archiva by apache.

the class DefaultRepositoriesService method copyArtifact.

@Override
public Boolean copyArtifact(ArtifactTransferRequest artifactTransferRequest) throws ArchivaRestServiceException {
    // check parameters
    String userName = getAuditInformation().getUser().getUsername();
    if (StringUtils.isBlank(userName)) {
        throw new ArchivaRestServiceException("copyArtifact call: userName not found", null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getRepositoryId())) {
        throw new ArchivaRestServiceException("copyArtifact call: sourceRepositoryId cannot be null", null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getTargetRepositoryId())) {
        throw new ArchivaRestServiceException("copyArtifact call: targetRepositoryId cannot be null", null);
    }
    ManagedRepository source = null;
    try {
        source = managedRepositoryAdmin.getManagedRepository(artifactTransferRequest.getRepositoryId());
    } catch (RepositoryAdminException e) {
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
    if (source == null) {
        throw new ArchivaRestServiceException("cannot find repository with id " + artifactTransferRequest.getRepositoryId(), null);
    }
    ManagedRepository target = null;
    try {
        target = managedRepositoryAdmin.getManagedRepository(artifactTransferRequest.getTargetRepositoryId());
    } catch (RepositoryAdminException e) {
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
    if (target == null) {
        throw new ArchivaRestServiceException("cannot find repository with id " + artifactTransferRequest.getTargetRepositoryId(), null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getGroupId())) {
        throw new ArchivaRestServiceException("groupId is mandatory", null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getArtifactId())) {
        throw new ArchivaRestServiceException("artifactId is mandatory", null);
    }
    if (StringUtils.isBlank(artifactTransferRequest.getVersion())) {
        throw new ArchivaRestServiceException("version is mandatory", null);
    }
    if (VersionUtil.isSnapshot(artifactTransferRequest.getVersion())) {
        throw new ArchivaRestServiceException("copy of SNAPSHOT not supported", null);
    }
    // end check parameters
    User user = null;
    try {
        user = securitySystem.getUserManager().findUser(userName);
    } catch (UserNotFoundException e) {
        throw new ArchivaRestServiceException("user " + userName + " not found", e);
    } catch (UserManagerException e) {
        throw new ArchivaRestServiceException("ArchivaRestServiceException:" + e.getMessage(), e);
    }
    // check karma on source : read
    AuthenticationResult authn = new AuthenticationResult(true, userName, null);
    SecuritySession securitySession = new DefaultSecuritySession(authn, user);
    try {
        boolean authz = securitySystem.isAuthorized(securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_ACCESS, artifactTransferRequest.getRepositoryId());
        if (!authz) {
            throw new ArchivaRestServiceException("not authorized to access repo:" + artifactTransferRequest.getRepositoryId(), null);
        }
    } catch (AuthorizationException e) {
        log.error("error reading permission: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
    // check karma on target: write
    try {
        boolean authz = securitySystem.isAuthorized(securitySession, ArchivaRoleConstants.OPERATION_REPOSITORY_UPLOAD, artifactTransferRequest.getTargetRepositoryId());
        if (!authz) {
            throw new ArchivaRestServiceException("not authorized to write to repo:" + artifactTransferRequest.getTargetRepositoryId(), null);
        }
    } catch (AuthorizationException e) {
        log.error("error reading permission: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
    // sounds good we can continue !
    ArtifactReference artifactReference = new ArtifactReference();
    artifactReference.setArtifactId(artifactTransferRequest.getArtifactId());
    artifactReference.setGroupId(artifactTransferRequest.getGroupId());
    artifactReference.setVersion(artifactTransferRequest.getVersion());
    artifactReference.setClassifier(artifactTransferRequest.getClassifier());
    String packaging = StringUtils.trim(artifactTransferRequest.getPackaging());
    artifactReference.setType(StringUtils.isEmpty(packaging) ? "jar" : packaging);
    try {
        ManagedRepositoryContent sourceRepository = getManagedRepositoryContent(artifactTransferRequest.getRepositoryId());
        String artifactSourcePath = sourceRepository.toPath(artifactReference);
        if (StringUtils.isEmpty(artifactSourcePath)) {
            log.error("cannot find artifact {}", artifactTransferRequest);
            throw new ArchivaRestServiceException("cannot find artifact " + artifactTransferRequest.toString(), null);
        }
        Path artifactFile = Paths.get(source.getLocation(), artifactSourcePath);
        if (!Files.exists(artifactFile)) {
            log.error("cannot find artifact {}", artifactTransferRequest);
            throw new ArchivaRestServiceException("cannot find artifact " + artifactTransferRequest.toString(), null);
        }
        ManagedRepositoryContent targetRepository = getManagedRepositoryContent(artifactTransferRequest.getTargetRepositoryId());
        String artifactPath = targetRepository.toPath(artifactReference);
        int lastIndex = artifactPath.lastIndexOf('/');
        String path = artifactPath.substring(0, lastIndex);
        Path targetPath = Paths.get(target.getLocation(), path);
        Date lastUpdatedTimestamp = Calendar.getInstance().getTime();
        int newBuildNumber = 1;
        String timestamp = null;
        Path versionMetadataFile = targetPath.resolve(MetadataTools.MAVEN_METADATA);
        /* unused */
        getMetadata(versionMetadataFile);
        if (!Files.exists(targetPath)) {
            Files.createDirectories(targetPath);
        }
        String filename = artifactPath.substring(lastIndex + 1);
        boolean fixChecksums = !(archivaAdministration.getKnownContentConsumers().contains("create-missing-checksums"));
        Path targetFile = targetPath.resolve(filename);
        if (Files.exists(targetFile) && target.isBlockRedeployments()) {
            throw new ArchivaRestServiceException("artifact already exists in target repo: " + artifactTransferRequest.getTargetRepositoryId() + " and redeployment blocked", null);
        } else {
            copyFile(artifactFile, targetPath, filename, fixChecksums);
            queueRepositoryTask(target.getId(), targetFile);
        }
        // copy source pom to target repo
        String pomFilename = filename;
        if (StringUtils.isNotBlank(artifactTransferRequest.getClassifier())) {
            pomFilename = StringUtils.remove(pomFilename, "-" + artifactTransferRequest.getClassifier());
        }
        pomFilename = FilenameUtils.removeExtension(pomFilename) + ".pom";
        Path pomFile = Paths.get(source.getLocation(), artifactSourcePath.substring(0, artifactPath.lastIndexOf('/')), pomFilename);
        if (pomFile != null && Files.size(pomFile) > 0) {
            copyFile(pomFile, targetPath, pomFilename, fixChecksums);
            queueRepositoryTask(target.getId(), targetPath.resolve(pomFilename));
        }
        // explicitly update only if metadata-updater consumer is not enabled!
        if (!archivaAdministration.getKnownContentConsumers().contains("metadata-updater")) {
            updateProjectMetadata(targetPath.toAbsolutePath().toString(), lastUpdatedTimestamp, timestamp, newBuildNumber, fixChecksums, artifactTransferRequest);
        }
        String msg = "Artifact \'" + artifactTransferRequest.getGroupId() + ":" + artifactTransferRequest.getArtifactId() + ":" + artifactTransferRequest.getVersion() + "\' was successfully deployed to repository \'" + artifactTransferRequest.getTargetRepositoryId() + "\'";
        log.debug("copyArtifact {}", msg);
    } catch (RepositoryException e) {
        log.error("RepositoryException: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    } catch (RepositoryAdminException e) {
        log.error("RepositoryAdminException: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    } catch (IOException e) {
        log.error("IOException: {}", e.getMessage(), e);
        throw new ArchivaRestServiceException(e.getMessage(), e);
    }
    return true;
}
Also used : UserNotFoundException(org.apache.archiva.redback.users.UserNotFoundException) Path(java.nio.file.Path) ManagedRepository(org.apache.archiva.admin.model.beans.ManagedRepository) User(org.apache.archiva.redback.users.User) AuthorizationException(org.apache.archiva.redback.authorization.AuthorizationException) SecuritySession(org.apache.archiva.redback.system.SecuritySession) DefaultSecuritySession(org.apache.archiva.redback.system.DefaultSecuritySession) RepositoryException(org.apache.archiva.repository.RepositoryException) MetadataRepositoryException(org.apache.archiva.metadata.repository.MetadataRepositoryException) IOException(java.io.IOException) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) Date(java.util.Date) AuthenticationResult(org.apache.archiva.redback.authentication.AuthenticationResult) UserManagerException(org.apache.archiva.redback.users.UserManagerException) ArchivaRestServiceException(org.apache.archiva.rest.api.services.ArchivaRestServiceException) ManagedRepositoryContent(org.apache.archiva.repository.ManagedRepositoryContent) DefaultSecuritySession(org.apache.archiva.redback.system.DefaultSecuritySession) ArtifactReference(org.apache.archiva.model.ArtifactReference)

Example 2 with UserNotFoundException

use of org.apache.archiva.redback.users.UserNotFoundException in project archiva by apache.

the class ArchivaServletAuthenticator method isAuthorized.

@Override
public boolean isAuthorized(String principal, String repoId, String permission) throws UnauthorizedException {
    try {
        User user = securitySystem.getUserManager().findUser(principal);
        if (user == null) {
            throw new UnauthorizedException("The security system had an internal error - please check your system logs");
        }
        if (user.isLocked()) {
            throw new UnauthorizedException("User account is locked.");
        }
        AuthenticationResult authn = new AuthenticationResult(true, principal, null);
        SecuritySession securitySession = new DefaultSecuritySession(authn, user);
        return securitySystem.isAuthorized(securitySession, permission, repoId);
    } catch (UserNotFoundException e) {
        throw new UnauthorizedException(e.getMessage(), e);
    } catch (AuthorizationException e) {
        throw new UnauthorizedException(e.getMessage(), e);
    } catch (UserManagerException e) {
        throw new UnauthorizedException(e.getMessage(), e);
    }
}
Also used : UserNotFoundException(org.apache.archiva.redback.users.UserNotFoundException) User(org.apache.archiva.redback.users.User) AuthorizationException(org.apache.archiva.redback.authorization.AuthorizationException) UserManagerException(org.apache.archiva.redback.users.UserManagerException) SecuritySession(org.apache.archiva.redback.system.SecuritySession) DefaultSecuritySession(org.apache.archiva.redback.system.DefaultSecuritySession) UnauthorizedException(org.apache.archiva.redback.authorization.UnauthorizedException) DefaultSecuritySession(org.apache.archiva.redback.system.DefaultSecuritySession) AuthenticationResult(org.apache.archiva.redback.authentication.AuthenticationResult)

Example 3 with UserNotFoundException

use of org.apache.archiva.redback.users.UserNotFoundException in project archiva by apache.

the class ArchivaConfigurableUsersManager method deleteUser.

@Override
public void deleteUser(String username) throws UserNotFoundException, UserManagerException {
    Exception lastException = null;
    boolean allFailed = true;
    User user = null;
    for (UserManager userManager : userManagerPerId.values()) {
        try {
            if (!userManager.isReadOnly()) {
                userManager.deleteUser(username);
                allFailed = false;
            }
        } catch (Exception e) {
            lastException = e;
        }
    }
    if (lastException != null && allFailed) {
        throw new UserManagerException(lastException.getMessage(), lastException);
    }
}
Also used : User(org.apache.archiva.redback.users.User) UserManagerException(org.apache.archiva.redback.users.UserManagerException) AbstractUserManager(org.apache.archiva.redback.users.AbstractUserManager) UserManager(org.apache.archiva.redback.users.UserManager) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) UserManagerException(org.apache.archiva.redback.users.UserManagerException) UserNotFoundException(org.apache.archiva.redback.users.UserNotFoundException)

Example 4 with UserNotFoundException

use of org.apache.archiva.redback.users.UserNotFoundException in project archiva by apache.

the class ArchivaLockedAdminEnvironmentCheck method validateEnvironment.

/**
 * This environment check will unlock system administrator accounts that are locked on the restart of the
 * application when the environment checks are processed.
 *
 * @param violations
 */
@Override
public void validateEnvironment(List<String> violations) {
    if (!checked) {
        for (UserManager userManager : userManagers) {
            if (userManager.isReadOnly()) {
                continue;
            }
            List<String> roles = new ArrayList<>();
            roles.add(RedbackRoleConstants.SYSTEM_ADMINISTRATOR_ROLE);
            List<UserAssignment> systemAdminstrators;
            try {
                systemAdminstrators = rbacManager.getUserAssignmentsForRoles(roles);
                for (UserAssignment userAssignment : systemAdminstrators) {
                    try {
                        User admin = userManager.findUser(userAssignment.getPrincipal());
                        if (admin.isLocked()) {
                            log.info("Unlocking system administrator: {}", admin.getUsername());
                            admin.setLocked(false);
                            userManager.updateUser(admin);
                        }
                    } catch (UserNotFoundException ne) {
                        log.warn("Dangling UserAssignment -> {}", userAssignment.getPrincipal());
                    } catch (UserManagerException e) {
                        log.warn("fail to find user {} for admin unlock check: {}", userAssignment.getPrincipal(), e.getMessage());
                    }
                }
            } catch (RbacManagerException e) {
                log.warn("Exception when checking for locked admin user: {}", e.getMessage(), e);
            }
            checked = true;
        }
    }
}
Also used : UserNotFoundException(org.apache.archiva.redback.users.UserNotFoundException) UserAssignment(org.apache.archiva.redback.rbac.UserAssignment) User(org.apache.archiva.redback.users.User) RbacManagerException(org.apache.archiva.redback.rbac.RbacManagerException) UserManagerException(org.apache.archiva.redback.users.UserManagerException) UserManager(org.apache.archiva.redback.users.UserManager) ArrayList(java.util.ArrayList)

Example 5 with UserNotFoundException

use of org.apache.archiva.redback.users.UserNotFoundException in project archiva by apache.

the class ArchivaConfigurableUsersManager method findUser.

@Override
public User findUser(String username, boolean useCache) throws UserNotFoundException, UserManagerException {
    User user = null;
    if (useUsersCache() && useCache) {
        user = usersCache.get(username);
        if (user != null) {
            return user;
        }
    }
    Exception lastException = null;
    for (UserManager userManager : userManagerPerId.values()) {
        try {
            user = userManager.findUser(username);
            if (user != null) {
                if (useUsersCache()) {
                    usersCache.put(username, user);
                }
                return user;
            }
        } catch (UserNotFoundException e) {
            lastException = e;
        } catch (Exception e) {
            lastException = e;
        }
    }
    if (user == null) {
        if (lastException != null) {
            if (lastException instanceof UserNotFoundException) {
                throw (UserNotFoundException) lastException;
            }
            throw new UserManagerException(lastException.getMessage(), lastException);
        }
    }
    return user;
}
Also used : UserNotFoundException(org.apache.archiva.redback.users.UserNotFoundException) User(org.apache.archiva.redback.users.User) UserManagerException(org.apache.archiva.redback.users.UserManagerException) AbstractUserManager(org.apache.archiva.redback.users.AbstractUserManager) UserManager(org.apache.archiva.redback.users.UserManager) RepositoryAdminException(org.apache.archiva.admin.model.RepositoryAdminException) UserManagerException(org.apache.archiva.redback.users.UserManagerException) UserNotFoundException(org.apache.archiva.redback.users.UserNotFoundException)

Aggregations

UserNotFoundException (org.apache.archiva.redback.users.UserNotFoundException)8 User (org.apache.archiva.redback.users.User)7 UserManagerException (org.apache.archiva.redback.users.UserManagerException)6 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)4 AuthenticationResult (org.apache.archiva.redback.authentication.AuthenticationResult)4 UserManager (org.apache.archiva.redback.users.UserManager)4 DefaultSecuritySession (org.apache.archiva.redback.system.DefaultSecuritySession)3 ArrayList (java.util.ArrayList)2 AuthenticationException (org.apache.archiva.redback.authentication.AuthenticationException)2 AuthorizationException (org.apache.archiva.redback.authorization.AuthorizationException)2 UnauthorizedException (org.apache.archiva.redback.authorization.UnauthorizedException)2 AccountLockedException (org.apache.archiva.redback.policy.AccountLockedException)2 MustChangePasswordException (org.apache.archiva.redback.policy.MustChangePasswordException)2 SecuritySession (org.apache.archiva.redback.system.SecuritySession)2 AbstractUserManager (org.apache.archiva.redback.users.AbstractUserManager)2 SyndFeed (com.sun.syndication.feed.synd.SyndFeed)1 FeedException (com.sun.syndication.io.FeedException)1 SyndFeedOutput (com.sun.syndication.io.SyndFeedOutput)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1