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 */
040public class ScmProviderAwareSshdSessionFactory extends SshdSessionFactory {
041    private final GitScmProviderRepository repo;
042    private final Logger logger;
043
044    public ScmProviderAwareSshdSessionFactory(GitScmProviderRepository repo, Logger logger) {
045        this.repo = repo;
046        this.logger = logger;
047    }
048
049    @Override
050    protected List<Path> getDefaultIdentities(File sshDir) {
051        if (!StringUtils.isEmptyOrNull(repo.getPrivateKey())) {
052            logger.debug("Using private key at {}", repo.getPrivateKey());
053            return Collections.singletonList(Paths.get(repo.getPrivateKey()));
054        } else {
055            return super.getDefaultIdentities(sshDir);
056        }
057    }
058
059    @Override
060    protected KeyPasswordProvider createKeyPasswordProvider(CredentialsProvider provider) {
061        if (repo.getPassphrase() != null) {
062            return new IdentityPasswordProvider(provider) {
063                @Override
064                public char[] getPassphrase(URIish uri, int attempt) throws IOException {
065                    if (attempt > 0) {
066                        throw new IOException(
067                                "Passphrase was not correct in first attempt, " + "canceling further attempts!");
068                    }
069                    logger.debug("Using stored passphrase");
070                    return repo.getPassphrase().toCharArray();
071                }
072            };
073        } else {
074            return super.createKeyPasswordProvider(provider);
075        }
076    }
077}