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