001package org.apache.maven.scm.provider.git.jgit.command;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import com.jcraft.jsch.JSch;
023import com.jcraft.jsch.JSchException;
024import com.jcraft.jsch.Session;
025import org.apache.maven.scm.log.ScmLogger;
026import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
027import org.eclipse.jgit.api.TransportConfigCallback;
028import org.eclipse.jgit.transport.JschConfigSessionFactory;
029import org.eclipse.jgit.transport.OpenSshConfig;
030import org.eclipse.jgit.transport.SshSessionFactory;
031import org.eclipse.jgit.transport.SshTransport;
032import org.eclipse.jgit.transport.Transport;
033import org.eclipse.jgit.util.FS;
034import org.eclipse.jgit.util.StringUtils;
035
036/**
037 * Implementation of {@link TransportConfigCallback} which adds
038 * a public/private key identity to ssh URLs if configured.
039 */
040public class JGitTransportConfigCallback implements TransportConfigCallback
041{
042    private SshSessionFactory sshSessionFactory = null;
043
044    public JGitTransportConfigCallback( GitScmProviderRepository repo, ScmLogger logger )
045    {
046        if ( repo.getFetchInfo().getProtocol().equals( "ssh" ) )
047        {
048            if ( !StringUtils.isEmptyOrNull( repo.getPrivateKey() ) && repo.getPassphrase() == null )
049            {
050                logger.debug( "using private key: " + repo.getPrivateKey() );
051                sshSessionFactory = new UnprotectedPrivateKeySessionFactory( repo );
052            }
053            else if ( !StringUtils.isEmptyOrNull( repo.getPrivateKey() ) && repo.getPassphrase() != null )
054            {
055                logger.debug( "using private key with passphrase: " + repo.getPrivateKey() );
056                sshSessionFactory = new ProtectedPrivateKeyFileSessionFactory( repo );
057            }
058            else
059            {
060                sshSessionFactory = new SimpleSessionFactory();
061            }
062        }
063    }
064
065    @Override
066    public void configure( Transport transport )
067    {
068        if ( transport instanceof SshTransport )
069        {
070            SshTransport sshTransport = (SshTransport) transport;
071            sshTransport.setSshSessionFactory( sshSessionFactory );
072        }
073    }
074
075    private static class SimpleSessionFactory extends JschConfigSessionFactory
076    {
077        @Override
078        protected void configure( OpenSshConfig.Host host, Session session )
079        {
080        }
081    }
082
083    private abstract static class PrivateKeySessionFactory extends SimpleSessionFactory
084    {
085        private final GitScmProviderRepository repo;
086
087        GitScmProviderRepository getRepo()
088        {
089            return repo;
090        }
091
092        PrivateKeySessionFactory( GitScmProviderRepository repo )
093        {
094            this.repo = repo;
095        }
096    }
097
098    private static class UnprotectedPrivateKeySessionFactory extends PrivateKeySessionFactory
099    {
100
101        UnprotectedPrivateKeySessionFactory( GitScmProviderRepository repo )
102        {
103            super( repo );
104        }
105
106        @Override
107        protected JSch createDefaultJSch( FS fs ) throws JSchException
108        {
109            JSch defaultJSch = super.createDefaultJSch( fs );
110            defaultJSch.addIdentity( getRepo().getPrivateKey() );
111            return defaultJSch;
112        }
113    }
114
115    private static class ProtectedPrivateKeyFileSessionFactory extends PrivateKeySessionFactory
116    {
117
118        ProtectedPrivateKeyFileSessionFactory( GitScmProviderRepository repo )
119        {
120            super( repo );
121        }
122
123        @Override
124        protected JSch createDefaultJSch( FS fs ) throws JSchException
125        {
126            JSch defaultJSch = super.createDefaultJSch( fs );
127            defaultJSch.addIdentity( getRepo().getPrivateKey(), getRepo().getPassphrase() );
128            return defaultJSch;
129        }
130    }
131}