001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.git.jgit.command;
020
021import java.io.File;
022import java.io.IOException;
023import java.nio.file.Path;
024import java.nio.file.Paths;
025import java.util.Collections;
026import java.util.List;
027
028import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
029import org.eclipse.jgit.transport.CredentialsProvider;
030import org.eclipse.jgit.transport.URIish;
031import org.eclipse.jgit.transport.sshd.IdentityPasswordProvider;
032import org.eclipse.jgit.transport.sshd.KeyPasswordProvider;
033import org.eclipse.jgit.transport.sshd.SshdSessionFactory;
034import org.eclipse.jgit.util.StringUtils;
035import org.slf4j.Logger;
036
037/**
038 * {@link SshdSessionFactory} considering the settings from {@link GitScmProviderRepository}.
039 *
040 */
041public class ScmProviderAwareSshdSessionFactory extends SshdSessionFactory {
042    private final GitScmProviderRepository repo;
043    private final Logger logger;
044
045    public ScmProviderAwareSshdSessionFactory(GitScmProviderRepository repo, Logger logger) {
046        this.repo = repo;
047        this.logger = logger;
048    }
049
050    @Override
051    protected List<Path> getDefaultIdentities(File sshDir) {
052        if (!StringUtils.isEmptyOrNull(repo.getPrivateKey())) {
053            logger.debug("Using private key at {}", repo.getPrivateKey());
054            return Collections.singletonList(Paths.get(repo.getPrivateKey()));
055        } else {
056            return super.getDefaultIdentities(sshDir);
057        }
058    }
059
060    @Override
061    protected KeyPasswordProvider createKeyPasswordProvider(CredentialsProvider provider) {
062        if (repo.getPassphrase() != null) {
063            return new IdentityPasswordProvider(provider) {
064                @Override
065                public char[] getPassphrase(URIish uri, int attempt) throws IOException {
066                    if (attempt > 0) {
067                        throw new IOException(
068                                "Passphrase was not correct in first attempt, " + "canceling further attempts!");
069                    }
070                    logger.debug("Using stored passphrase");
071                    return repo.getPassphrase().toCharArray();
072                }
073            };
074        } else {
075            return super.createKeyPasswordProvider(provider);
076        }
077    }
078}