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