View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.gpg;
20  
21  import java.io.File;
22  import java.util.List;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugin.logging.Log;
27  
28  /**
29   * A base class for all classes that implements signing of files.
30   *
31   * @author Dennis Lundberg
32   * @since 1.5
33   */
34  public abstract class AbstractGpgSigner {
35      public static final String SIGNATURE_EXTENSION = ".asc";
36  
37      protected boolean useAgent;
38  
39      protected boolean isInteractive = true;
40  
41      protected boolean defaultKeyring = true;
42  
43      protected String keyname;
44  
45      private Log log;
46  
47      protected String passphrase;
48  
49      protected boolean terminatePassphrase;
50  
51      private File outputDir;
52  
53      private File buildDir;
54  
55      private File baseDir;
56  
57      protected File homeDir;
58  
59      protected String secretKeyring;
60  
61      protected String publicKeyring;
62  
63      protected String lockMode;
64  
65      protected List<String> args;
66  
67      public Log getLog() {
68          return log;
69      }
70  
71      public void setArgs(List<String> args) {
72          this.args = args;
73      }
74  
75      public void setInteractive(boolean b) {
76          isInteractive = b;
77      }
78  
79      public void setLockMode(String lockMode) {
80          this.lockMode = lockMode;
81      }
82  
83      public void setUseAgent(boolean b) {
84          useAgent = b;
85      }
86  
87      public void setDefaultKeyring(boolean enabled) {
88          defaultKeyring = enabled;
89      }
90  
91      public void setKeyName(String s) {
92          keyname = s;
93      }
94  
95      public void setLog(Log log) {
96          this.log = log;
97      }
98  
99      public void setPassPhrase(String s) {
100         passphrase = s;
101     }
102 
103     public void setTerminatePassphrase(boolean b) {
104         this.terminatePassphrase = b;
105     }
106 
107     public void setOutputDirectory(File out) {
108         outputDir = out;
109     }
110 
111     public void setBuildDirectory(File out) {
112         buildDir = out;
113     }
114 
115     public void setBaseDirectory(File out) {
116         baseDir = out;
117     }
118 
119     public void setHomeDirectory(File homeDirectory) {
120         homeDir = homeDirectory;
121     }
122 
123     public void setSecretKeyring(String path) {
124         secretKeyring = path;
125     }
126 
127     public void setPublicKeyring(String path) {
128         publicKeyring = path;
129     }
130 
131     public abstract String signerName();
132 
133     /**
134      * Must be invoked BEFORE signing!
135      *
136      * @since 3.2.0
137      */
138     public void prepare() throws MojoFailureException {}
139 
140     /**
141      * Should return some identification about the used key for logging purposes.
142      * Can be invoked only AFTER {@link #prepare()} was invoked.
143      *
144      * @since 3.2.2
145      */
146     public abstract String getKeyInfo();
147 
148     /**
149      * Create a detached signature file for the provided file.
150      * Can be invoked only AFTER {@link #prepare()} was invoked.
151      *
152      * @param file The file to sign
153      * @return A reference to the generated signature file
154      * @throws MojoExecutionException if signature generation fails
155      */
156     public File generateSignatureForArtifact(File file) throws MojoExecutionException {
157         // ----------------------------------------------------------------------------
158         // Set up the file and directory for the signature file
159         // ----------------------------------------------------------------------------
160 
161         File signature = new File(file + SIGNATURE_EXTENSION);
162 
163         boolean isInBuildDir = false;
164         if (buildDir != null) {
165             File parent = signature.getParentFile();
166             if (buildDir.equals(parent)) {
167                 isInBuildDir = true;
168             }
169         }
170         if (!isInBuildDir && outputDir != null) {
171             String fileDirectory = "";
172             File signatureDirectory = signature;
173 
174             while ((signatureDirectory = signatureDirectory.getParentFile()) != null) {
175                 if (isPossibleRootOfArtifact(signatureDirectory)) {
176                     break;
177                 }
178                 fileDirectory = signatureDirectory.getName() + File.separatorChar + fileDirectory;
179             }
180             signatureDirectory = new File(outputDir, fileDirectory);
181             if (!signatureDirectory.exists()) {
182                 signatureDirectory.mkdirs();
183             }
184             signature = new File(signatureDirectory, file.getName() + SIGNATURE_EXTENSION);
185         }
186 
187         if (signature.exists()) {
188             signature.delete();
189         }
190 
191         // ----------------------------------------------------------------------------
192         // Generate the signature file
193         // ----------------------------------------------------------------------------
194 
195         generateSignatureForFile(file, signature);
196 
197         return signature;
198     }
199 
200     /**
201      * Generate the detached signature file for the provided file.
202      *
203      * @param file The file to sign
204      * @param signature The file in which the generate signature will be put
205      * @throws MojoExecutionException if signature generation fails
206      */
207     protected abstract void generateSignatureForFile(File file, File signature) throws MojoExecutionException;
208 
209     private boolean isPossibleRootOfArtifact(File signatureDirectory) {
210         return signatureDirectory.equals(outputDir)
211                 || signatureDirectory.equals(buildDir)
212                 || signatureDirectory.equals(baseDir);
213     }
214 }