Coverage Report - org.apache.maven.wagon.providers.ssh.external.ScpExternalWagon
 
Classes in this File Line Coverage Branch Coverage Complexity
ScpExternalWagon
0%
0/167
0%
0/66
2,6
 
 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 java.io.File;
 23  
 import java.io.FileNotFoundException;
 24  
 import java.util.List;
 25  
 import java.util.Locale;
 26  
 
 27  
 import org.apache.maven.wagon.AbstractWagon;
 28  
 import org.apache.maven.wagon.CommandExecutionException;
 29  
 import org.apache.maven.wagon.CommandExecutor;
 30  
 import org.apache.maven.wagon.PathUtils;
 31  
 import org.apache.maven.wagon.PermissionModeUtils;
 32  
 import org.apache.maven.wagon.ResourceDoesNotExistException;
 33  
 import org.apache.maven.wagon.Streams;
 34  
 import org.apache.maven.wagon.TransferFailedException;
 35  
 import org.apache.maven.wagon.WagonConstants;
 36  
 import org.apache.maven.wagon.authentication.AuthenticationException;
 37  
 import org.apache.maven.wagon.authentication.AuthenticationInfo;
 38  
 import org.apache.maven.wagon.authorization.AuthorizationException;
 39  
 import org.apache.maven.wagon.events.TransferEvent;
 40  
 import org.apache.maven.wagon.providers.ssh.ScpHelper;
 41  
 import org.apache.maven.wagon.repository.RepositoryPermissions;
 42  
 import org.apache.maven.wagon.resource.Resource;
 43  
 import org.codehaus.plexus.util.StringUtils;
 44  
 import org.codehaus.plexus.util.cli.CommandLineException;
 45  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 46  
 import org.codehaus.plexus.util.cli.Commandline;
 47  
 
 48  
 /**
 49  
  * SCP deployer using "external" scp program.  To allow for
 50  
  * ssh-agent type behavior, until we can construct a Java SSH Agent and interface for JSch.
 51  
  *
 52  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 53  
  * @version $Id:ScpExternalWagon.java 477260 2006-11-20 17:11:39Z brett $
 54  
  * @todo [BP] add compression flag
 55  
  * 
 56  
  * @plexus.component role="org.apache.maven.wagon.Wagon" 
 57  
  *   role-hint="scpexe"
 58  
  *   instantiation-strategy="per-lookup"
 59  
  */
 60  0
 public class ScpExternalWagon
 61  
     extends AbstractWagon
 62  
     implements CommandExecutor
 63  
 {
 64  
     /**
 65  
      * The external SCP command to use - default is <code>scp</code>.
 66  
      *
 67  
      * @component.configuration default="scp"
 68  
      */
 69  0
     private String scpExecutable = "scp";
 70  
 
 71  
     /**
 72  
      * The external SSH command to use - default is <code>ssh</code>.
 73  
      *
 74  
      * @component.configuration default="ssh"
 75  
      */
 76  0
     private String sshExecutable = "ssh";
 77  
 
 78  
     /**
 79  
      * Arguments to pass to the SCP command.
 80  
      *
 81  
      * @component.configuration
 82  
      */
 83  
     private String scpArgs;
 84  
 
 85  
     /**
 86  
      * Arguments to pass to the SSH command.
 87  
      *
 88  
      * @component.configuration
 89  
      */
 90  
     private String sshArgs;
 91  
 
 92  0
     private ScpHelper sshTool = new ScpHelper( this );
 93  
 
 94  
     private static final int SSH_FATAL_EXIT_CODE = 255;
 95  
 
 96  
     // ----------------------------------------------------------------------
 97  
     //
 98  
     // ----------------------------------------------------------------------
 99  
 
 100  
     protected void openConnectionInternal()
 101  
         throws AuthenticationException
 102  
     {
 103  0
         if ( authenticationInfo == null )
 104  
         {
 105  0
             authenticationInfo = new AuthenticationInfo();
 106  
         }
 107  0
     }
 108  
 
 109  
     public void closeConnection()
 110  
     {
 111  
         // nothing to disconnect
 112  0
     }
 113  
 
 114  
     public boolean getIfNewer( String resourceName, File destination, long timestamp )
 115  
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
 116  
     {
 117  0
         fireSessionDebug( "getIfNewer in SCP wagon is not supported - performing an unconditional get" );
 118  0
         get( resourceName, destination );
 119  0
         return true;
 120  
     }
 121  
 
 122  
     /**
 123  
      * @return The hostname of the remote server prefixed with the username, which comes either from the repository URL
 124  
      *         or from the authenticationInfo.
 125  
      */
 126  
     private String buildRemoteHost()
 127  
     {
 128  0
         String username = this.getRepository().getUsername();
 129  0
         if ( username == null )
 130  
         {
 131  0
             username = authenticationInfo.getUserName();
 132  
         }
 133  
         
 134  0
         if ( username == null )
 135  
         {
 136  0
             return getRepository().getHost();
 137  
         }
 138  
         else
 139  
         {
 140  0
             return username + "@" + getRepository().getHost();
 141  
         }
 142  
     }
 143  
     
 144  
     public void executeCommand( String command )
 145  
         throws CommandExecutionException
 146  
     {
 147  0
         fireTransferDebug( "Executing command: " + command );
 148  
 
 149  0
         executeCommand( command, false );
 150  0
     }
 151  
 
 152  
     public Streams executeCommand( String command, boolean ignoreFailures )
 153  
         throws CommandExecutionException
 154  
     {
 155  0
         boolean putty = isPuTTY();
 156  
 
 157  
         File privateKey;
 158  
         try
 159  
         {
 160  0
             privateKey = ScpHelper.getPrivateKey( authenticationInfo );
 161  
         }
 162  0
         catch ( FileNotFoundException e )
 163  
         {
 164  0
             throw new CommandExecutionException( e.getMessage() );
 165  0
         }
 166  0
         Commandline cl = createBaseCommandLine( putty, sshExecutable, privateKey );
 167  
 
 168  0
         int port =
 169  
             repository.getPort() == WagonConstants.UNKNOWN_PORT ? ScpHelper.DEFAULT_SSH_PORT : repository.getPort();
 170  0
         if ( port != ScpHelper.DEFAULT_SSH_PORT )
 171  
         {
 172  0
             if ( putty )
 173  
             {
 174  0
                 cl.createArgument().setLine( "-P " + port );
 175  
             }
 176  
             else
 177  
             {
 178  0
                 cl.createArgument().setLine( "-p " + port );
 179  
             }
 180  
         }
 181  
 
 182  0
         if ( sshArgs != null )
 183  
         {
 184  0
             cl.createArgument().setLine( sshArgs );
 185  
         }
 186  
         
 187  0
         String remoteHost = this.buildRemoteHost();
 188  
 
 189  0
         cl.createArgument().setValue( remoteHost );
 190  
 
 191  0
         cl.createArgument().setValue( command );
 192  
 
 193  0
         fireSessionDebug( "Executing command: " + cl.toString() );
 194  
 
 195  
         try
 196  
         {
 197  0
             CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();
 198  0
             CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
 199  0
             int exitCode = CommandLineUtils.executeCommandLine( cl, out, err );
 200  0
             Streams streams = new Streams();
 201  0
             streams.setOut( out.getOutput() );
 202  0
             streams.setErr( err.getOutput() );
 203  0
             fireSessionDebug( streams.getOut() );
 204  0
             fireSessionDebug( streams.getErr() );
 205  0
             if ( exitCode != 0 )
 206  
             {
 207  0
                 if ( !ignoreFailures || exitCode == SSH_FATAL_EXIT_CODE )
 208  
                 {
 209  0
                     throw new CommandExecutionException( "Exit code " + exitCode + " - " + err.getOutput() );
 210  
                 }
 211  
             }
 212  0
             return streams;
 213  
         }
 214  0
         catch ( CommandLineException e )
 215  
         {
 216  0
             throw new CommandExecutionException( "Error executing command line", e );
 217  
         }
 218  
     }
 219  
 
 220  
     protected boolean isPuTTY()
 221  
     {
 222  0
         return sshExecutable.toLowerCase( Locale.ENGLISH ).indexOf( "plink" ) >= 0;
 223  
     }
 224  
 
 225  
     private Commandline createBaseCommandLine( boolean putty, String executable, File privateKey )
 226  
     {
 227  0
         Commandline cl = new Commandline();
 228  
 
 229  0
         cl.setExecutable( executable );
 230  
 
 231  0
         if ( privateKey != null )
 232  
         {
 233  0
             cl.createArgument().setValue( "-i" );
 234  0
             cl.createArgument().setFile( privateKey );
 235  
         }
 236  
 
 237  0
         String password = authenticationInfo.getPassword();
 238  0
         if ( putty && password != null )
 239  
         {
 240  0
             cl.createArgument().setValue( "-pw" );
 241  0
             cl.createArgument().setValue( password );
 242  
         }
 243  
 
 244  
         // should check interactive flag, but scpexe never works in interactive mode right now due to i/o streams
 245  0
         if ( putty )
 246  
         {
 247  0
             cl.createArgument().setValue( "-batch" );
 248  
         }
 249  
         else
 250  
         {
 251  0
             cl.createArgument().setValue( "-o" );
 252  0
             cl.createArgument().setValue( "BatchMode yes" );
 253  
         }
 254  0
         return cl;
 255  
     }
 256  
 
 257  
 
 258  
     private void executeScpCommand( Resource resource, File localFile, boolean put )
 259  
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
 260  
     {
 261  0
         boolean putty = isPuTTYSCP();
 262  
 
 263  
         File privateKey;
 264  
         try
 265  
         {
 266  0
             privateKey = ScpHelper.getPrivateKey( authenticationInfo );
 267  
         }
 268  0
         catch ( FileNotFoundException e )
 269  
         {
 270  0
             fireSessionConnectionRefused();
 271  
             
 272  0
             throw new AuthorizationException( e.getMessage() );
 273  0
         }
 274  0
         Commandline cl = createBaseCommandLine( putty, scpExecutable, privateKey );
 275  
 
 276  0
         cl.setWorkingDirectory( localFile.getParentFile().getAbsolutePath() );
 277  
 
 278  0
         int port =
 279  
             repository.getPort() == WagonConstants.UNKNOWN_PORT ? ScpHelper.DEFAULT_SSH_PORT : repository.getPort();
 280  0
         if ( port != ScpHelper.DEFAULT_SSH_PORT )
 281  
         {
 282  0
             cl.createArgument().setLine( "-P " + port );
 283  
         }
 284  
 
 285  0
         if ( scpArgs != null )
 286  
         {
 287  0
             cl.createArgument().setLine( scpArgs );
 288  
         }
 289  
         
 290  0
         String resourceName = normalizeResource( resource );
 291  0
         String remoteFile = getRepository().getBasedir() + "/" + resourceName;
 292  
         
 293  0
         remoteFile = StringUtils.replace( remoteFile, " ", "\\ " );
 294  
         
 295  0
         String qualifiedRemoteFile = this.buildRemoteHost() + ":" + remoteFile;
 296  0
         if ( put )
 297  
         {
 298  0
             cl.createArgument().setValue( localFile.getName() );
 299  0
             cl.createArgument().setValue( qualifiedRemoteFile );
 300  
         }
 301  
         else
 302  
         {
 303  0
             cl.createArgument().setValue( qualifiedRemoteFile );
 304  0
             cl.createArgument().setValue( localFile.getName() );
 305  
         }
 306  
 
 307  0
         fireSessionDebug( "Executing command: " + cl.toString() );
 308  
 
 309  
         try
 310  
         {
 311  0
             CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
 312  0
             int exitCode = CommandLineUtils.executeCommandLine( cl, null, err );
 313  0
             if ( exitCode != 0 )
 314  
             {
 315  0
                 if ( !put && 
 316  
                     err.getOutput().trim().toLowerCase( Locale.ENGLISH ).indexOf( "no such file or directory" ) != -1 )
 317  
                 {
 318  0
                     throw new ResourceDoesNotExistException( err.getOutput() );
 319  
                 }
 320  
                 else
 321  
                 {
 322  0
                     TransferFailedException e = new TransferFailedException( "Exit code: " + exitCode + " - "
 323  
                                                                              + err.getOutput() );
 324  
 
 325  0
                     fireTransferError( resource, e, put ? TransferEvent.REQUEST_PUT : TransferEvent.REQUEST_GET );
 326  
 
 327  0
                     throw e;
 328  
                 }
 329  
             }
 330  
         }
 331  0
         catch ( CommandLineException e )
 332  
         {
 333  0
             fireTransferError( resource, e, put ? TransferEvent.REQUEST_PUT : TransferEvent.REQUEST_GET );
 334  
 
 335  0
             throw new TransferFailedException( "Error executing command line", e );
 336  0
         }
 337  0
     }
 338  
 
 339  
     boolean isPuTTYSCP()
 340  
     {
 341  0
         return scpExecutable.toLowerCase( Locale.ENGLISH ).indexOf( "pscp" ) >= 0;
 342  
     }
 343  
 
 344  
     private String normalizeResource( Resource resource )
 345  
     {
 346  0
         return StringUtils.replace( resource.getName(), "\\", "/" );
 347  
     }
 348  
 
 349  
     public void put( File source, String destination )
 350  
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
 351  
     {
 352  0
         Resource resource = new Resource( destination );
 353  
 
 354  0
         firePutInitiated( resource, source );
 355  
 
 356  0
         if ( !source.exists() )
 357  
         {
 358  0
             throw new ResourceDoesNotExistException( "Specified source file does not exist: " + source );
 359  
         }
 360  
 
 361  0
         String basedir = getRepository().getBasedir();
 362  
 
 363  0
         String resourceName = StringUtils.replace( destination, "\\", "/" );
 364  
 
 365  0
         String dir = PathUtils.dirname( resourceName );
 366  
 
 367  0
         dir = StringUtils.replace( dir, "\\", "/" );
 368  
 
 369  0
         String umaskCmd = null;
 370  0
         if ( getRepository().getPermissions() != null )
 371  
         {
 372  0
             String dirPerms = getRepository().getPermissions().getDirectoryMode();
 373  
 
 374  0
             if ( dirPerms != null )
 375  
             {
 376  0
                 umaskCmd = "umask " + PermissionModeUtils.getUserMaskFor( dirPerms );
 377  
             }
 378  
         }
 379  
 
 380  0
         String mkdirCmd = "mkdir -p " + basedir + "/" + dir + "\n";
 381  
 
 382  0
         if ( umaskCmd != null )
 383  
         {
 384  0
             mkdirCmd = umaskCmd + "; " + mkdirCmd;
 385  
         }
 386  
 
 387  
         try
 388  
         {
 389  0
             executeCommand( mkdirCmd );
 390  
         }
 391  0
         catch ( CommandExecutionException e )
 392  
         {
 393  0
             fireTransferError( resource, e, TransferEvent.REQUEST_PUT );
 394  
             
 395  0
             throw new TransferFailedException( "Error executing command for transfer", e );
 396  0
         }
 397  
         
 398  0
         resource.setContentLength( source.length() );
 399  
         
 400  0
         resource.setLastModified( source.lastModified() );
 401  
 
 402  0
         firePutStarted( resource, source );
 403  
 
 404  0
         executeScpCommand( resource, source, true );
 405  
 
 406  0
         postProcessListeners( resource, source, TransferEvent.REQUEST_PUT );
 407  
 
 408  
         try
 409  
         {
 410  0
             RepositoryPermissions permissions = getRepository().getPermissions();
 411  
 
 412  0
             if ( permissions != null && permissions.getGroup() != null )
 413  
             {
 414  0
                 executeCommand( "chgrp -f " + permissions.getGroup() + " " + basedir + "/" + resourceName + "\n",
 415  
                                 true );
 416  
             }
 417  
 
 418  0
             if ( permissions != null && permissions.getFileMode() != null )
 419  
             {
 420  0
                 executeCommand( "chmod -f " + permissions.getFileMode() + " " + basedir + "/" + resourceName + "\n",
 421  
                                 true );
 422  
             }
 423  
         }
 424  0
         catch ( CommandExecutionException e )
 425  
         {
 426  0
             fireTransferError( resource, e, TransferEvent.REQUEST_PUT );
 427  
 
 428  0
             throw new TransferFailedException( "Error executing command for transfer", e );
 429  0
         }
 430  0
         firePutCompleted( resource, source );
 431  0
     }
 432  
 
 433  
     public void get( String resourceName, File destination )
 434  
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
 435  
     {
 436  0
         String path = StringUtils.replace( resourceName, "\\", "/" );
 437  
 
 438  0
         Resource resource = new Resource( path );
 439  
 
 440  0
         fireGetInitiated( resource, destination );
 441  
         
 442  0
         createParentDirectories( destination );
 443  
 
 444  0
         fireGetStarted( resource, destination );
 445  
 
 446  0
         executeScpCommand( resource, destination, false );
 447  
 
 448  0
         postProcessListeners( resource, destination, TransferEvent.REQUEST_GET );
 449  
 
 450  0
         fireGetCompleted( resource, destination );
 451  0
     }
 452  
 
 453  
     //
 454  
     // these parameters are user specific, so should not be read from the repository itself.
 455  
     // They can be configured by plexus, or directly on the instantiated object.
 456  
     // Alternatively, we may later accept a generic parameters argument to connect, or some other configure(Properties)
 457  
     // method on a Wagon.
 458  
     //
 459  
 
 460  
     public List getFileList( String destinationDirectory )
 461  
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
 462  
     {
 463  0
         return sshTool.getFileList( destinationDirectory, repository );
 464  
     }
 465  
 
 466  
     public void putDirectory( File sourceDirectory, String destinationDirectory )
 467  
         throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException
 468  
     {
 469  0
         sshTool.putDirectory( this, sourceDirectory, destinationDirectory );
 470  0
     }
 471  
 
 472  
     public boolean resourceExists( String resourceName )
 473  
         throws TransferFailedException, AuthorizationException
 474  
     {
 475  0
         return sshTool.resourceExists( resourceName, repository );
 476  
     }
 477  
 
 478  
     public boolean supportsDirectoryCopy()
 479  
     {
 480  0
         return true;
 481  
     }
 482  
 
 483  
     public String getScpExecutable()
 484  
     {
 485  0
         return scpExecutable;
 486  
     }
 487  
 
 488  
     public void setScpExecutable( String scpExecutable )
 489  
     {
 490  0
         this.scpExecutable = scpExecutable;
 491  0
     }
 492  
 
 493  
     public String getSshExecutable()
 494  
     {
 495  0
         return sshExecutable;
 496  
     }
 497  
 
 498  
     public void setSshExecutable( String sshExecutable )
 499  
     {
 500  0
         this.sshExecutable = sshExecutable;
 501  0
     }
 502  
 
 503  
     public String getScpArgs()
 504  
     {
 505  0
         return scpArgs;
 506  
     }
 507  
 
 508  
     public void setScpArgs( String scpArgs )
 509  
     {
 510  0
         this.scpArgs = scpArgs;
 511  0
     }
 512  
 
 513  
     public String getSshArgs()
 514  
     {
 515  0
         return sshArgs;
 516  
     }
 517  
 
 518  
     public void setSshArgs( String sshArgs )
 519  
     {
 520  0
         this.sshArgs = sshArgs;
 521  0
     }
 522  
 }