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;
020
021import java.io.IOException;
022import java.io.OutputStream;
023import java.io.Writer;
024import java.nio.file.FileSystem;
025import java.nio.file.Files;
026import java.nio.file.Path;
027import java.nio.file.attribute.PosixFilePermissions;
028import java.security.GeneralSecurityException;
029import java.security.KeyPair;
030import java.security.PublicKey;
031import java.util.ArrayList;
032import java.util.List;
033
034import org.apache.maven.scm.provider.git.sshd.git.pack.GitPackCommandFactory;
035import org.apache.sshd.common.config.keys.KeyUtils;
036import org.apache.sshd.common.config.keys.writer.openssh.OpenSSHKeyEncryptionContext;
037import org.apache.sshd.common.config.keys.writer.openssh.OpenSSHKeyPairResourceWriter;
038import org.apache.sshd.git.GitLocationResolver;
039import org.apache.sshd.server.SshServer;
040import org.apache.sshd.server.auth.pubkey.KeySetPublickeyAuthenticator;
041import org.apache.sshd.server.auth.pubkey.PublickeyAuthenticator;
042import org.apache.sshd.server.session.ServerSession;
043import org.apache.sshd.util.test.CommonTestSupportUtils;
044import org.apache.sshd.util.test.CoreTestSupportUtils;
045import org.bouncycastle.openssl.PKCS8Generator;
046import org.bouncycastle.openssl.jcajce.JcaPEMWriter;
047import org.bouncycastle.openssl.jcajce.JcaPKCS8Generator;
048import org.bouncycastle.util.io.pem.PemObject;
049
050/**
051 * Local Git SSH server for testing purposes.
052 * It acts on top of an existing repository root directory.
053 * It uses <a href="https://mina.apache.org/sshd-project/">Apache MINA SSHD</a> for the SSH server implementation.
054 * <p>
055 * The server generates a key pair during initialization and accepts connections using the private key which can be
056 * extracted via {@link GitSshServer#writePrivateKeyAsPkcs8(Path, String)}.
057 * Alternatively one may use a custom key pair and add the public key using {@link GitSshServer#addPublicKey(PublicKey)}.
058 */
059public class GitSshServer {
060
061    protected final SshServer sshServer;
062    protected final KeyPair keyPair;
063    protected final List<PublicKey> acceptedPublicKeys;
064
065    public GitSshServer() throws GeneralSecurityException {
066        sshServer = CoreTestSupportUtils.setupTestServer(getClass());
067        keyPair = CommonTestSupportUtils.generateKeyPair(KeyUtils.RSA_ALGORITHM, 2048);
068        acceptedPublicKeys = new ArrayList<>();
069        acceptedPublicKeys.add(keyPair.getPublic());
070        PublickeyAuthenticator authenticator = new KeySetPublickeyAuthenticator("onlykey", acceptedPublicKeys);
071        sshServer.setPublickeyAuthenticator(authenticator);
072    }
073
074    /**
075     * Writes a private key which is accepted by this server to the specified file in PKCS8 format.
076     * If a passphrase is provided, the key will be encrypted using OpenSSH's format.
077     * If no passphrase is provided, the key will be written as an unencrypted PKCS8 private key.
078     * For the same server instance the private key is always the same, so it can be reused.
079     *
080     * @param file the file to write the private key to
081     * @param passphrase the passphrase for encryption, or null for unencrypted
082     * @throws IOException if an I/O error occurs
083     * @throws GeneralSecurityException if a security error occurs
084     */
085    public void writePrivateKeyAsPkcs8(Path file, String passphrase) throws IOException, GeneralSecurityException {
086        // encryption only optional
087        if (passphrase != null) {
088            // encryption with format outlined in https://dnaeon.github.io/openssh-private-key-binary-format/
089            OpenSSHKeyPairResourceWriter writer = new OpenSSHKeyPairResourceWriter();
090            OpenSSHKeyEncryptionContext context = new OpenSSHKeyEncryptionContext();
091            context.setCipherType("192");
092            context.setPassword(passphrase);
093            try (OutputStream output = Files.newOutputStream(file)) {
094                writer.writePrivateKey(keyPair, "comment", context, output);
095            }
096        } else {
097            // wrap unencrypted private key as regular PKCS8 private key
098            PKCS8Generator pkcs8Generator = new JcaPKCS8Generator(keyPair.getPrivate(), null);
099            PemObject pemObject = pkcs8Generator.generate();
100
101            try (Writer writer = Files.newBufferedWriter(file);
102                    JcaPEMWriter pw = new JcaPEMWriter(writer)) {
103                pw.writeObject(pemObject);
104            }
105        }
106
107        if (file.getFileSystem().supportedFileAttributeViews().contains("posix")) {
108            // must only be readable/writeable by me
109            Files.setPosixFilePermissions(file, PosixFilePermissions.fromString("rwx------"));
110        }
111    }
112
113    public void addPublicKey(PublicKey publicKey) {
114        if (publicKey == null) {
115            throw new IllegalArgumentException("Public key must not be null");
116        }
117        acceptedPublicKeys.add(publicKey);
118    }
119
120    public int getPort() {
121        if (!sshServer.isStarted()) {
122            throw new IllegalStateException("SSH server is not started");
123        }
124        return sshServer.getPort();
125    }
126
127    public void start(Path repositoryRoot) throws IOException {
128        GitLocationResolver gitLocationResolver = new GitLocationResolver() {
129            @Override
130            public Path resolveRootDirectory(String command, String[] args, ServerSession session, FileSystem fs)
131                    throws IOException {
132                return repositoryRoot;
133            }
134        };
135        // use patched version of GitPackCommandFactory including https://github.com/apache/mina-sshd/pull/794
136        sshServer.setCommandFactory(new GitPackCommandFactory(gitLocationResolver));
137        sshServer.start();
138    }
139
140    public void stop() throws IOException {
141        sshServer.stop();
142    }
143}