View Javadoc
1   package org.apache.maven.scm.provider.git.jgit.command;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  import java.util.Collections;
27  import java.util.List;
28  
29  import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
30  import org.eclipse.jgit.transport.CredentialsProvider;
31  import org.eclipse.jgit.transport.URIish;
32  import org.eclipse.jgit.transport.sshd.IdentityPasswordProvider;
33  import org.eclipse.jgit.transport.sshd.KeyPasswordProvider;
34  import org.eclipse.jgit.transport.sshd.SshdSessionFactory;
35  import org.eclipse.jgit.util.StringUtils;
36  import org.slf4j.Logger;
37  
38  /**
39   * {@link SshdSessionFactory} considering the settings from {@link GitScmProviderRepository}.
40   *
41   */
42  public class ScmProviderAwareSshdSessionFactory extends SshdSessionFactory
43  {
44      private final GitScmProviderRepository repo;
45      private final Logger logger;
46  
47      public ScmProviderAwareSshdSessionFactory( GitScmProviderRepository repo, Logger logger )
48      {
49          this.repo = repo;
50          this.logger = logger;
51      }
52  
53      @Override
54      protected List<Path> getDefaultIdentities( File sshDir )
55      {
56          if ( !StringUtils.isEmptyOrNull( repo.getPrivateKey() ) )
57          {
58              logger.debug( "Using private key at {}", repo.getPrivateKey() );
59              return Collections.singletonList( Paths.get( repo.getPrivateKey() ) );
60          }
61          else
62          {
63              return super.getDefaultIdentities( sshDir );
64          }
65      }
66  
67      @Override
68      protected KeyPasswordProvider createKeyPasswordProvider( CredentialsProvider provider )
69      {
70          if ( repo.getPassphrase() != null )
71          {
72              return new IdentityPasswordProvider( provider )
73              {
74                  @Override
75                  public char[] getPassphrase( URIish uri, int attempt ) throws IOException
76                  {
77                      if ( attempt > 0 )
78                      {
79                          throw new IOException( "Passphrase was not correct in first attempt, "
80                              + "canceling further attempts!" );
81                      }
82                      logger.debug( "Using stored passphrase" );
83                      return repo.getPassphrase().toCharArray();
84                  }
85              };
86          }
87          else
88          {
89              return super.createKeyPasswordProvider( provider );
90          }
91      }
92  }