View Javadoc
1   package org.apache.maven.wagon.providers.ssh.external;
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 org.apache.commons.io.FileUtils;
23  import org.apache.maven.wagon.Streams;
24  import org.apache.maven.wagon.Wagon;
25  import org.apache.maven.wagon.authentication.AuthenticationInfo;
26  import org.apache.maven.wagon.providers.ssh.AbstractEmbeddedScpWagonWithKeyTest;
27  import org.codehaus.plexus.util.cli.CommandLineUtils;
28  import org.codehaus.plexus.util.cli.Commandline;
29  
30  import java.io.File;
31  
32  /**
33   * @author <a href="michal.maczka@dimatics.com">Michal Maczka</a>
34   *
35   */
36  public class EmbeddedScpExternalWagonWithKeyTest
37      extends AbstractEmbeddedScpWagonWithKeyTest
38  {
39  
40  
41      @Override
42      protected Wagon getWagon()
43          throws Exception
44      {
45          ScpExternalWagon scpWagon = (ScpExternalWagon) super.getWagon();
46          scpWagon.setInteractive( false );
47          File dummyKnowHostsFile = new File( "target/dummy_knowhost" );
48          if ( dummyKnowHostsFile.exists() )
49          {
50              dummyKnowHostsFile.delete();
51          }
52          scpWagon.setScpArgs(
53              "-o StrictHostKeyChecking=no -o UserKnownHostsFile=" + dummyKnowHostsFile.getCanonicalPath() );
54          scpWagon.setSshArgs(
55              "-o StrictHostKeyChecking=no -o UserKnownHostsFile=" + dummyKnowHostsFile.getCanonicalPath() );
56          dummyKnowHostsFile.deleteOnExit();
57          return scpWagon;
58      }
59  
60  
61      protected String getProtocol()
62      {
63          return "scpexe";
64      }
65  
66  
67      protected AuthenticationInfo getAuthInfo()
68      {
69          try
70          {
71              AuthenticationInfo authInfo = super.getAuthInfo();
72              // user : guest/guest123 -  passphrase : toto01
73              authInfo.setUserName( "guest" );
74              File sshKeysTarget = new File( "target/ssh-keys" );
75              FileUtils.copyDirectory( new File( "src/test/ssh-keys" ), sshKeysTarget );
76              // ssh keys need to 700 permissions
77              // to prevent WARNING: UNPROTECTED PRIVATE KEY FILE!
78              Commandline commandline = new Commandline( "chmod" );
79              commandline.createArg().setValue( "-R" );
80              commandline.createArg().setValue( "700" );
81              commandline.createArg().setValue( sshKeysTarget.getCanonicalPath() );
82              CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
83              CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
84              int exitCode = CommandLineUtils.executeCommandLine( commandline, out, err );
85              Streams streams = new Streams();
86              streams.setOut( out.getOutput() );
87              streams.setErr( err.getOutput() );
88              if ( exitCode != 0 )
89              {
90                  throw new RuntimeException(
91                      "fail to chmod exit code " + exitCode + ", error" + streams.getErr() + ", out "
92                          + streams.getOut() );
93              }
94  
95              authInfo.setPrivateKey( new File( sshKeysTarget, "id_rsa" ).getCanonicalPath() );
96  
97              return authInfo;
98          }
99          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 }