View Javadoc

1   package org.apache.maven.plugin.gpg;
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.ByteArrayInputStream;
23  import java.io.File;
24  import java.io.InputStream;
25  import java.util.Iterator;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.codehaus.plexus.util.Os;
29  import org.codehaus.plexus.util.StringUtils;
30  import org.codehaus.plexus.util.cli.CommandLineException;
31  import org.codehaus.plexus.util.cli.CommandLineUtils;
32  import org.codehaus.plexus.util.cli.Commandline;
33  import org.codehaus.plexus.util.cli.DefaultConsumer;
34  
35  /**
36   * A signer implementation that uses the GnuPG command line executable.
37   */
38  public class GpgSigner
39      extends AbstractGpgSigner
40  {
41      private String executable;
42  
43      public GpgSigner( String executable )
44      {
45          this.executable = executable;
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      @Override
52      protected void generateSignatureForFile( File file, File signature )
53          throws MojoExecutionException
54      {
55          // ----------------------------------------------------------------------------
56          // Set up the command line
57          // ----------------------------------------------------------------------------
58  
59          Commandline cmd = new Commandline();
60  
61          if ( StringUtils.isNotEmpty( executable ) )
62          {
63              cmd.setExecutable( executable );
64          }
65          else
66          {
67              cmd.setExecutable( "gpg" + ( Os.isFamily( Os.FAMILY_WINDOWS ) ? ".exe" : "" ) );
68          }
69  
70          if ( args != null )
71          {
72              for ( String arg : args )
73              {
74                  cmd.createArg().setValue( arg );
75              }
76          }
77  
78          if ( homeDir != null )
79          {
80              cmd.createArg().setValue( "--homedir" );
81              cmd.createArg().setFile( homeDir );
82          }
83  
84          if ( useAgent )
85          {
86              cmd.createArg().setValue( "--use-agent" );
87          }
88          else
89          {
90              cmd.createArg().setValue( "--no-use-agent" );
91          }
92  
93          InputStream in = null;
94          if ( null != passphrase )
95          {
96              // make --passphrase-fd effective in gpg2
97              cmd.createArg().setValue( "--batch" );
98  
99              cmd.createArg().setValue( "--passphrase-fd" );
100 
101             cmd.createArg().setValue( "0" );
102 
103             // Prepare the input stream which will be used to pass the passphrase to the executable
104             in = new ByteArrayInputStream( passphrase.getBytes() );
105         }
106 
107         if ( null != keyname )
108         {
109             cmd.createArg().setValue( "--local-user" );
110 
111             cmd.createArg().setValue( keyname );
112         }
113 
114         cmd.createArg().setValue( "--armor" );
115 
116         cmd.createArg().setValue( "--detach-sign" );
117 
118         if ( !isInteractive )
119         {
120             cmd.createArg().setValue( "--no-tty" );
121         }
122 
123         if ( !defaultKeyring )
124         {
125             cmd.createArg().setValue( "--no-default-keyring" );
126         }
127 
128         if ( StringUtils.isNotEmpty( secretKeyring ) )
129         {
130             cmd.createArg().setValue( "--secret-keyring" );
131             cmd.createArg().setValue( secretKeyring );
132         }
133 
134         if ( StringUtils.isNotEmpty( publicKeyring ) )
135         {
136             cmd.createArg().setValue( "--keyring" );
137             cmd.createArg().setValue( publicKeyring );
138         }
139 
140         if ( "once".equalsIgnoreCase( lockMode ) )
141         {
142             cmd.createArg().setValue( "--lock-once" );
143         }
144         else if ( "multiple".equalsIgnoreCase( lockMode ) )
145         {
146             cmd.createArg().setValue( "--lock-multiple" );
147         }
148         else if ( "never".equalsIgnoreCase( lockMode ) )
149         {
150             cmd.createArg().setValue( "--lock-never" );
151         }
152 
153         cmd.createArg().setValue( "--output" );
154         cmd.createArg().setFile( signature );
155 
156         cmd.createArg().setFile( file );
157 
158         // ----------------------------------------------------------------------------
159         // Execute the command line
160         // ----------------------------------------------------------------------------
161 
162         try
163         {
164             int exitCode = CommandLineUtils.executeCommandLine( cmd, in, new DefaultConsumer(), new DefaultConsumer() );
165 
166             if ( exitCode != 0 )
167             {
168                 throw new MojoExecutionException( "Exit code: " + exitCode );
169             }
170         }
171         catch ( CommandLineException e )
172         {
173             throw new MojoExecutionException( "Unable to execute gpg command", e );
174         }
175     }
176 
177 }