001package org.apache.maven.wagon.providers.ssh.external;
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 org.apache.commons.io.FileUtils;
023import org.apache.maven.wagon.Streams;
024import org.apache.maven.wagon.Wagon;
025import org.apache.maven.wagon.authentication.AuthenticationInfo;
026import org.apache.maven.wagon.providers.ssh.AbstractEmbeddedScpWagonWithKeyTest;
027import org.codehaus.plexus.util.cli.CommandLineUtils;
028import org.codehaus.plexus.util.cli.Commandline;
029
030import java.io.File;
031
032/**
033 * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
034 *
035 */
036public class EmbeddedScpExternalWagonWithKeyTest
037    extends AbstractEmbeddedScpWagonWithKeyTest
038{
039
040
041    @Override
042    protected Wagon getWagon()
043        throws Exception
044    {
045        ScpExternalWagon scpWagon = (ScpExternalWagon) super.getWagon();
046        scpWagon.setInteractive( false );
047        File dummyKnowHostsFile = new File( "target/dummy_knowhost" );
048        if ( dummyKnowHostsFile.exists() )
049        {
050            dummyKnowHostsFile.delete();
051        }
052        scpWagon.setScpArgs(
053            "-o StrictHostKeyChecking=no -o UserKnownHostsFile=" + dummyKnowHostsFile.getCanonicalPath() );
054        scpWagon.setSshArgs(
055            "-o StrictHostKeyChecking=no -o UserKnownHostsFile=" + dummyKnowHostsFile.getCanonicalPath() );
056        dummyKnowHostsFile.deleteOnExit();
057        return scpWagon;
058    }
059
060
061    protected String getProtocol()
062    {
063        return "scpexe";
064    }
065
066
067    protected AuthenticationInfo getAuthInfo()
068    {
069        try
070        {
071            AuthenticationInfo authInfo = super.getAuthInfo();
072            // user : guest/guest123 -  passphrase : toto01
073            authInfo.setUserName( "guest" );
074            File sshKeysTarget = new File( "target/ssh-keys" );
075            FileUtils.copyDirectory( new File( "src/test/ssh-keys" ), sshKeysTarget );
076            // ssh keys need to 700 permissions
077            // to prevent WARNING: UNPROTECTED PRIVATE KEY FILE!
078            Commandline commandline = new Commandline( "chmod" );
079            commandline.createArg().setValue( "-R" );
080            commandline.createArg().setValue( "700" );
081            commandline.createArg().setValue( sshKeysTarget.getCanonicalPath() );
082            CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
083            CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
084            int exitCode = CommandLineUtils.executeCommandLine( commandline, out, err );
085            Streams streams = new Streams();
086            streams.setOut( out.getOutput() );
087            streams.setErr( err.getOutput() );
088            if ( exitCode != 0 )
089            {
090                throw new RuntimeException(
091                    "fail to chmod exit code " + exitCode + ", error" + streams.getErr() + ", out "
092                        + streams.getOut() );
093            }
094
095            authInfo.setPrivateKey( new File( sshKeysTarget, "id_rsa" ).getCanonicalPath() );
096
097            return authInfo;
098        }
099        catch ( Exception e )
100        {
101            throw new RuntimeException( e.getMessage(), e );
102
103        }
104    }
105
106    public void testFailedGetToStream()
107        throws Exception
108    {
109        // ignore this test as it need a stream wagon
110    }
111
112
113}