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      private File outputDir;
50  
51      private File buildDir;
52  
53      private File baseDir;
54  
55      protected File homeDir;
56  
57      protected String secretKeyring;
58  
59      protected String publicKeyring;
60  
61      protected String lockMode;
62  
63      protected List<String> args;
64  
65      public Log getLog() {
66          return log;
67      }
68  
69      public void setArgs(List<String> args) {
70          this.args = args;
71      }
72  
73      public void setInteractive(boolean b) {
74          isInteractive = b;
75      }
76  
77      public void setLockMode(String lockMode) {
78          this.lockMode = lockMode;
79      }
80  
81      public void setUseAgent(boolean b) {
82          useAgent = b;
83      }
84  
85      public void setDefaultKeyring(boolean enabled) {
86          defaultKeyring = enabled;
87      }
88  
89      public void setKeyName(String s) {
90          keyname = s;
91      }
92  
93      public void setLog(Log log) {
94          this.log = log;
95      }
96  
97      public void setPassPhrase(String s) {
98          passphrase = s;
99      }
100 
101     public void setOutputDirectory(File out) {
102         outputDir = out;
103     }
104 
105     public void setBuildDirectory(File out) {
106         buildDir = out;
107     }
108 
109     public void setBaseDirectory(File out) {
110         baseDir = out;
111     }
112 
113     public void setHomeDirectory(File homeDirectory) {
114         homeDir = homeDirectory;
115     }
116 
117     public void setSecretKeyring(String path) {
118         secretKeyring = path;
119     }
120 
121     public void setPublicKeyring(String path) {
122         publicKeyring = path;
123     }
124 
125     public abstract String signerName();
126 
127     /**
128      * Must be invoked BEFORE signing!
129      *
130      * @since 3.2.0
131      */
132     public void prepare() throws MojoFailureException {}
133 
134     /**
135      * Should return some identification about the used key for logging purposes.
136      * Can be invoked only AFTER {@link #prepare()} was invoked.
137      *
138      * @since 3.2.2
139      */
140     public abstract String getKeyInfo();
141 
142     /**
143      * Create a detached signature file for the provided file.
144      * Can be invoked only AFTER {@link #prepare()} was invoked.
145      *
146      * @param file The file to sign
147      * @return A reference to the generated signature file
148      * @throws MojoExecutionException if signature generation fails
149      */
150     public File generateSignatureForArtifact(File file) throws MojoExecutionException {
151         // ----------------------------------------------------------------------------
152         // Set up the file and directory for the signature file
153         // ----------------------------------------------------------------------------
154 
155         File signature = new File(file + SIGNATURE_EXTENSION);
156 
157         boolean isInBuildDir = false;
158         if (buildDir != null) {
159             File parent = signature.getParentFile();
160             if (buildDir.equals(parent)) {
161                 isInBuildDir = true;
162             }
163         }
164         if (!isInBuildDir && outputDir != null) {
165             String fileDirectory = "";
166             File signatureDirectory = signature;
167 
168             while ((signatureDirectory = signatureDirectory.getParentFile()) != null) {
169                 if (isPossibleRootOfArtifact(signatureDirectory)) {
170                     break;
171                 }
172                 fileDirectory = signatureDirectory.getName() + File.separatorChar + fileDirectory;
173             }
174             signatureDirectory = new File(outputDir, fileDirectory);
175             if (!signatureDirectory.exists()) {
176                 signatureDirectory.mkdirs();
177             }
178             signature = new File(signatureDirectory, file.getName() + SIGNATURE_EXTENSION);
179         }
180 
181         if (signature.exists()) {
182             signature.delete();
183         }
184 
185         // ----------------------------------------------------------------------------
186         // Generate the signature file
187         // ----------------------------------------------------------------------------
188 
189         generateSignatureForFile(file, signature);
190 
191         return signature;
192     }
193 
194     /**
195      * Generate the detached signature file for the provided file.
196      *
197      * @param file The file to sign
198      * @param signature The file in which the generate signature will be put
199      * @throws MojoExecutionException if signature generation fails
200      */
201     protected abstract void generateSignatureForFile(File file, File signature) throws MojoExecutionException;
202 
203     private boolean isPossibleRootOfArtifact(File signatureDirectory) {
204         return signatureDirectory.equals(outputDir)
205                 || signatureDirectory.equals(buildDir)
206                 || signatureDirectory.equals(baseDir);
207     }
208 }