Search in sources :

Example 1 with AuthenticationInfo

use of org.apache.maven.wagon.authentication.AuthenticationInfo in project archiva by apache.

the class RepositoryModelResolver method connectToRepository.

/**
 * Using wagon, connect to the remote repository.
 *
 * @param wagon the wagon instance to establish the connection on.
 * @return true if the connection was successful. false if not connected.
 */
private boolean connectToRepository(Wagon wagon, RemoteRepository remoteRepository) {
    boolean connected;
    final NetworkProxy proxyConnector = this.networkProxyMap.get(remoteRepository.getId());
    ProxyInfo networkProxy = null;
    if (proxyConnector != null) {
        networkProxy = new ProxyInfo();
        networkProxy.setType(proxyConnector.getProtocol());
        networkProxy.setHost(proxyConnector.getHost());
        networkProxy.setPort(proxyConnector.getPort());
        networkProxy.setUserName(proxyConnector.getUsername());
        networkProxy.setPassword(proxyConnector.getPassword());
        String msg = "Using network proxy " + networkProxy.getHost() + ":" + networkProxy.getPort() + " to connect to remote repository " + remoteRepository.getLocation();
        if (networkProxy.getNonProxyHosts() != null) {
            msg += "; excluding hosts: " + networkProxy.getNonProxyHosts();
        }
        if (StringUtils.isNotBlank(networkProxy.getUserName())) {
            msg += "; as user: " + networkProxy.getUserName();
        }
        log.debug(msg);
    }
    AuthenticationInfo authInfo = null;
    RepositoryCredentials creds = remoteRepository.getLoginCredentials();
    String username = "";
    String password = "";
    if (creds instanceof UsernamePasswordCredentials) {
        UsernamePasswordCredentials c = (UsernamePasswordCredentials) creds;
        username = c.getUserName();
        password = c.getPassword();
    }
    if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
        log.debug("Using username {} to connect to remote repository {}", username, remoteRepository.getLocation());
        authInfo = new AuthenticationInfo();
        authInfo.setUserName(username);
        authInfo.setPassword(password);
    }
    int timeoutInMilliseconds = ((int) remoteRepository.getTimeout().getSeconds()) * 1000;
    // FIXME olamy having 2 config values
    // Set timeout
    wagon.setReadTimeout(timeoutInMilliseconds);
    wagon.setTimeout(timeoutInMilliseconds);
    try {
        org.apache.maven.wagon.repository.Repository wagonRepository = new org.apache.maven.wagon.repository.Repository(remoteRepository.getId(), remoteRepository.getLocation().toString());
        if (networkProxy != null) {
            wagon.connect(wagonRepository, authInfo, networkProxy);
        } else {
            wagon.connect(wagonRepository, authInfo);
        }
        connected = true;
    } catch (ConnectionException | AuthenticationException e) {
        log.error("Could not connect to {}:{} ", remoteRepository.getName(), e.getMessage());
        connected = false;
    }
    return connected;
}
Also used : RepositoryCredentials(org.apache.archiva.repository.RepositoryCredentials) AuthenticationException(org.apache.maven.wagon.authentication.AuthenticationException) NetworkProxy(org.apache.archiva.admin.model.beans.NetworkProxy) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Repository(org.apache.maven.model.Repository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) ManagedRepository(org.apache.archiva.repository.ManagedRepository) ConnectionException(org.apache.maven.wagon.ConnectionException)

Example 2 with AuthenticationInfo

use of org.apache.maven.wagon.authentication.AuthenticationInfo in project archiva by apache.

the class DefaultRepositoryProxyConnectors method connectToRepository.

/**
 * Using wagon, connect to the remote repository.
 *
 * @param connector        the connector configuration to utilize (for obtaining network proxy configuration from)
 * @param wagon            the wagon instance to establish the connection on.
 * @param remoteRepository the remote repository to connect to.
 * @return true if the connection was successful. false if not connected.
 */
private boolean connectToRepository(ProxyConnector connector, Wagon wagon, RemoteRepositoryContent remoteRepository) {
    boolean connected = false;
    final ProxyInfo networkProxy = connector.getProxyId() == null ? null : this.networkProxyMap.get(connector.getProxyId());
    if (log.isDebugEnabled()) {
        if (networkProxy != null) {
            // TODO: move to proxyInfo.toString()
            String msg = "Using network proxy " + networkProxy.getHost() + ":" + networkProxy.getPort() + " to connect to remote repository " + remoteRepository.getURL();
            if (networkProxy.getNonProxyHosts() != null) {
                msg += "; excluding hosts: " + networkProxy.getNonProxyHosts();
            }
            if (StringUtils.isNotBlank(networkProxy.getUserName())) {
                msg += "; as user: " + networkProxy.getUserName();
            }
            log.debug(msg);
        }
    }
    AuthenticationInfo authInfo = null;
    String username = "";
    String password = "";
    RepositoryCredentials repCred = remoteRepository.getRepository().getLoginCredentials();
    if (repCred != null && repCred instanceof PasswordCredentials) {
        PasswordCredentials pwdCred = (PasswordCredentials) repCred;
        username = pwdCred.getUsername();
        password = pwdCred.getPassword() == null ? "" : new String(pwdCred.getPassword());
    }
    if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password)) {
        log.debug("Using username {} to connect to remote repository {}", username, remoteRepository.getURL());
        authInfo = new AuthenticationInfo();
        authInfo.setUserName(username);
        authInfo.setPassword(password);
    }
    // Convert seconds to milliseconds
    long timeoutInMilliseconds = remoteRepository.getRepository().getTimeout().toMillis();
    // Set timeout  read and connect
    // FIXME olamy having 2 config values
    wagon.setReadTimeout((int) timeoutInMilliseconds);
    wagon.setTimeout((int) timeoutInMilliseconds);
    try {
        Repository wagonRepository = new Repository(remoteRepository.getId(), remoteRepository.getURL().toString());
        wagon.connect(wagonRepository, authInfo, networkProxy);
        connected = true;
    } catch (ConnectionException | AuthenticationException e) {
        log.warn("Could not connect to {}: {}", remoteRepository.getRepository().getName(), e.getMessage());
        connected = false;
    }
    return connected;
}
Also used : ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) RepositoryCredentials(org.apache.archiva.repository.RepositoryCredentials) PasswordCredentials(org.apache.archiva.repository.PasswordCredentials) Repository(org.apache.maven.wagon.repository.Repository) ManagedRepository(org.apache.archiva.repository.ManagedRepository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) AuthenticationException(org.apache.maven.wagon.authentication.AuthenticationException) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) ConnectionException(org.apache.maven.wagon.ConnectionException)

Example 3 with AuthenticationInfo

use of org.apache.maven.wagon.authentication.AuthenticationInfo in project archiva by apache.

the class DownloadRemoteIndexTask method run.

@Override
public void run() {
    // so short lock : not sure we need it
    synchronized (this.runningRemoteDownloadIds) {
        if (this.runningRemoteDownloadIds.contains(this.remoteRepository.getId())) {
            // skip it as it's running
            log.info("skip download index remote for repo {} it's already running", this.remoteRepository.getId());
            return;
        }
        this.runningRemoteDownloadIds.add(this.remoteRepository.getId());
    }
    Path tempIndexDirectory = null;
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    try {
        log.info("start download remote index for remote repository {}", this.remoteRepository.getId());
        if (this.remoteRepository.getIndexingContext() == null) {
            throw new IndexNotFoundException("No index context set for repository " + remoteRepository.getId());
        }
        if (this.remoteRepository.getType() != RepositoryType.MAVEN) {
            throw new RepositoryException("Bad repository type");
        }
        if (!this.remoteRepository.supportsFeature(RemoteIndexFeature.class)) {
            throw new RepositoryException("Repository does not support RemotIndexFeature " + remoteRepository.getId());
        }
        RemoteIndexFeature rif = this.remoteRepository.getFeature(RemoteIndexFeature.class).get();
        IndexingContext indexingContext = this.remoteRepository.getIndexingContext().getBaseContext(IndexingContext.class);
        // create a temp directory to download files
        tempIndexDirectory = Paths.get(indexingContext.getIndexDirectoryFile().getParent(), ".tmpIndex");
        Path indexCacheDirectory = Paths.get(indexingContext.getIndexDirectoryFile().getParent(), ".indexCache");
        Files.createDirectories(indexCacheDirectory);
        if (Files.exists(tempIndexDirectory)) {
            org.apache.archiva.common.utils.FileUtils.deleteDirectory(tempIndexDirectory);
        }
        Files.createDirectories(tempIndexDirectory);
        tempIndexDirectory.toFile().deleteOnExit();
        String baseIndexUrl = indexingContext.getIndexUpdateUrl();
        String wagonProtocol = this.remoteRepository.getLocation().getScheme();
        final StreamWagon wagon = (StreamWagon) wagonFactory.getWagon(new WagonFactoryRequest(wagonProtocol, this.remoteRepository.getExtraHeaders()).networkProxy(this.networkProxy));
        // FIXME olamy having 2 config values
        wagon.setReadTimeout((int) rif.getDownloadTimeout().toMillis());
        wagon.setTimeout((int) remoteRepository.getTimeout().toMillis());
        if (wagon instanceof AbstractHttpClientWagon) {
            HttpConfiguration httpConfiguration = new HttpConfiguration();
            HttpMethodConfiguration httpMethodConfiguration = new HttpMethodConfiguration();
            httpMethodConfiguration.setUsePreemptive(true);
            httpMethodConfiguration.setReadTimeout((int) rif.getDownloadTimeout().toMillis());
            httpConfiguration.setGet(httpMethodConfiguration);
            AbstractHttpClientWagon.class.cast(wagon).setHttpConfiguration(httpConfiguration);
        }
        wagon.addTransferListener(new DownloadListener());
        ProxyInfo proxyInfo = null;
        if (this.networkProxy != null) {
            proxyInfo = new ProxyInfo();
            proxyInfo.setType(this.networkProxy.getProtocol());
            proxyInfo.setHost(this.networkProxy.getHost());
            proxyInfo.setPort(this.networkProxy.getPort());
            proxyInfo.setUserName(this.networkProxy.getUsername());
            proxyInfo.setPassword(this.networkProxy.getPassword());
        }
        AuthenticationInfo authenticationInfo = null;
        if (this.remoteRepository.getLoginCredentials() != null && this.remoteRepository.getLoginCredentials() instanceof PasswordCredentials) {
            PasswordCredentials creds = (PasswordCredentials) this.remoteRepository.getLoginCredentials();
            authenticationInfo = new AuthenticationInfo();
            authenticationInfo.setUserName(creds.getUsername());
            authenticationInfo.setPassword(new String(creds.getPassword()));
        }
        log.debug("Connection to {}, authInfo={}", this.remoteRepository.getId(), authenticationInfo);
        wagon.connect(new Repository(this.remoteRepository.getId(), baseIndexUrl), authenticationInfo, proxyInfo);
        Path indexDirectory = indexingContext.getIndexDirectoryFile().toPath();
        if (!Files.exists(indexDirectory)) {
            Files.createDirectories(indexDirectory);
        }
        log.debug("Downloading index file to {}", indexDirectory);
        log.debug("Index cache dir {}", indexCacheDirectory);
        ResourceFetcher resourceFetcher = new WagonResourceFetcher(log, tempIndexDirectory, wagon, remoteRepository);
        IndexUpdateRequest request = new IndexUpdateRequest(indexingContext, resourceFetcher);
        request.setForceFullUpdate(this.fullDownload);
        request.setLocalIndexCacheDir(indexCacheDirectory.toFile());
        IndexUpdateResult result = this.indexUpdater.fetchAndUpdateIndex(request);
        log.debug("Update result success: {}", result.isSuccessful());
        stopWatch.stop();
        log.info("time update index from remote for repository {}: {}ms", this.remoteRepository.getId(), (stopWatch.getTime()));
        // index packing optionnal ??
        // IndexPackingRequest indexPackingRequest =
        // new IndexPackingRequest( indexingContext, indexingContext.getIndexDirectoryFile() );
        // indexPacker.packIndex( indexPackingRequest );
        indexingContext.updateTimestamp(true);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        deleteDirectoryQuiet(tempIndexDirectory);
        this.runningRemoteDownloadIds.remove(this.remoteRepository.getId());
    }
    log.info("end download remote index for remote repository {}", this.remoteRepository.getId());
}
Also used : Path(java.nio.file.Path) AbstractHttpClientWagon(org.apache.maven.wagon.shared.http.AbstractHttpClientWagon) PasswordCredentials(org.apache.archiva.repository.PasswordCredentials) HttpMethodConfiguration(org.apache.maven.wagon.shared.http.HttpMethodConfiguration) ResourceFetcher(org.apache.maven.index.updater.ResourceFetcher) IndexUpdateRequest(org.apache.maven.index.updater.IndexUpdateRequest) RepositoryException(org.apache.archiva.repository.RepositoryException) HttpConfiguration(org.apache.maven.wagon.shared.http.HttpConfiguration) StreamWagon(org.apache.maven.wagon.StreamWagon) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo) TransferFailedException(org.apache.maven.wagon.TransferFailedException) IndexNotFoundException(org.apache.maven.index_shaded.lucene.index.IndexNotFoundException) RepositoryException(org.apache.archiva.repository.RepositoryException) AuthorizationException(org.apache.maven.wagon.authorization.AuthorizationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ResourceDoesNotExistException(org.apache.maven.wagon.ResourceDoesNotExistException) StopWatch(org.apache.commons.lang.time.StopWatch) IndexUpdateResult(org.apache.maven.index.updater.IndexUpdateResult) ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Repository(org.apache.maven.wagon.repository.Repository) RemoteRepository(org.apache.archiva.repository.RemoteRepository) WagonFactoryRequest(org.apache.archiva.proxy.common.WagonFactoryRequest) IndexNotFoundException(org.apache.maven.index_shaded.lucene.index.IndexNotFoundException) RemoteIndexFeature(org.apache.archiva.repository.features.RemoteIndexFeature) IndexingContext(org.apache.maven.index.context.IndexingContext)

Example 4 with AuthenticationInfo

use of org.apache.maven.wagon.authentication.AuthenticationInfo in project maven-archetype by apache.

the class RemoteCatalogArchetypeDataSource method getAuthenticationInfo.

private AuthenticationInfo getAuthenticationInfo(String id) {
    MavenSession session = legacySupport.getSession();
    if (session != null && id != null) {
        MavenExecutionRequest request = session.getRequest();
        if (request != null) {
            List<Server> servers = request.getServers();
            if (servers != null) {
                for (Server server : servers) {
                    if (id.equalsIgnoreCase(server.getId())) {
                        SettingsDecryptionResult result = settingsDecrypter.decrypt(new DefaultSettingsDecryptionRequest(server));
                        server = result.getServer();
                        AuthenticationInfo authInfo = new AuthenticationInfo();
                        authInfo.setUserName(server.getUsername());
                        authInfo.setPassword(server.getPassword());
                        authInfo.setPrivateKey(server.getPrivateKey());
                        authInfo.setPassphrase(server.getPassphrase());
                        return authInfo;
                    }
                }
            }
        }
    }
    // empty one to prevent NPE
    return new AuthenticationInfo();
}
Also used : MavenSession(org.apache.maven.execution.MavenSession) Server(org.apache.maven.settings.Server) MavenExecutionRequest(org.apache.maven.execution.MavenExecutionRequest) DefaultSettingsDecryptionRequest(org.apache.maven.settings.crypto.DefaultSettingsDecryptionRequest) SettingsDecryptionResult(org.apache.maven.settings.crypto.SettingsDecryptionResult) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo)

Example 5 with AuthenticationInfo

use of org.apache.maven.wagon.authentication.AuthenticationInfo in project maven-archetype by apache.

the class RemoteCatalogArchetypeDataSource method downloadCatalog.

private ArchetypeCatalog downloadCatalog(ArtifactRepository repository) throws WagonException, IOException, ArchetypeDataSourceException {
    getLogger().debug("Searching for remote catalog: " + repository.getUrl() + "/" + ARCHETYPE_CATALOG_FILENAME);
    // We use wagon to take advantage of a Proxy that has already been setup in a Maven environment.
    Repository wagonRepository = new Repository(repository.getId(), repository.getUrl());
    AuthenticationInfo authInfo = getAuthenticationInfo(wagonRepository.getId());
    ProxyInfo proxyInfo = getProxy(wagonRepository.getProtocol());
    Wagon wagon = getWagon(wagonRepository);
    File catalog = File.createTempFile("archetype-catalog", ".xml");
    try {
        wagon.connect(wagonRepository, authInfo, proxyInfo);
        wagon.get(ARCHETYPE_CATALOG_FILENAME, catalog);
        return readCatalog(ReaderFactory.newXmlReader(catalog));
    } finally {
        disconnectWagon(wagon);
        catalog.delete();
    }
}
Also used : ProxyInfo(org.apache.maven.wagon.proxy.ProxyInfo) Repository(org.apache.maven.wagon.repository.Repository) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) Wagon(org.apache.maven.wagon.Wagon) File(java.io.File) AuthenticationInfo(org.apache.maven.wagon.authentication.AuthenticationInfo)

Aggregations

AuthenticationInfo (org.apache.maven.wagon.authentication.AuthenticationInfo)15 ProxyInfo (org.apache.maven.wagon.proxy.ProxyInfo)8 ConnectionException (org.apache.maven.wagon.ConnectionException)7 AuthenticationException (org.apache.maven.wagon.authentication.AuthenticationException)7 RemoteRepository (org.apache.archiva.repository.RemoteRepository)6 IOException (java.io.IOException)5 PasswordCredentials (org.apache.archiva.repository.PasswordCredentials)5 File (java.io.File)4 MalformedURLException (java.net.MalformedURLException)4 Path (java.nio.file.Path)4 Repository (org.apache.maven.wagon.repository.Repository)4 URI (java.net.URI)3 RepositoryAdminException (org.apache.archiva.admin.model.RepositoryAdminException)3 NetworkProxy (org.apache.archiva.admin.model.beans.NetworkProxy)3 IndexUpdateFailedException (org.apache.archiva.indexer.IndexUpdateFailedException)3 WagonFactoryException (org.apache.archiva.proxy.common.WagonFactoryException)3 WagonFactoryRequest (org.apache.archiva.proxy.common.WagonFactoryRequest)3 RemoteIndexFeature (org.apache.archiva.repository.features.RemoteIndexFeature)3 IndexUpdateRequest (org.apache.maven.index.updater.IndexUpdateRequest)3 ResourceFetcher (org.apache.maven.index.updater.ResourceFetcher)3