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