Skip to content

Commit

Permalink
[MGPG-115] Show more info about key used to sign (#84)
Browse files Browse the repository at this point in the history
Just show some usable feedback which key is used to sign.

---

https://issues.apache.org/jira/browse/MGPG-115
  • Loading branch information
cstamas committed Mar 23, 2024
1 parent 3631830 commit 2be0a00
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 3 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/apache/maven/plugins/gpg/AbstractGpgSigner.java
Expand Up @@ -124,10 +124,24 @@ public void setPublicKeyring(String path) {

public abstract String signerName();

/**
* Must be invoked BEFORE signing!
*
* @since 3.2.0
*/
public void prepare() throws MojoFailureException {}

/**
* Should return some identification about the used key for logging purposes.
* Can be invoked only AFTER {@link #prepare()} was invoked.
*
* @since 3.2.2
*/
public abstract String getKeyInfo();

/**
* Create a detached signature file for the provided file.
* Can be invoked only AFTER {@link #prepare()} was invoked.
*
* @param file The file to sign
* @return A reference to the generated signature file
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/apache/maven/plugins/gpg/BcSigner.java
Expand Up @@ -33,6 +33,7 @@
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -361,6 +362,15 @@ public void prepare() throws MojoFailureException {
}
}

@Override
public String getKeyInfo() {
Iterator<String> userIds = secretKey.getPublicKey().getUserIDs();
if (userIds.hasNext()) {
return userIds.next();
}
return Hex.toHexString(secretKey.getPublicKey().getFingerprint());
}

@Override
protected void generateSignatureForFile(File file, File signature) throws MojoExecutionException {
try (InputStream in = Files.newInputStream(file.toPath());
Expand Down
Expand Up @@ -88,7 +88,7 @@ protected void doExecute() throws MojoExecutionException, MojoFailureException {
signer.setBaseDirectory(project.getBasedir());

getLog().info("Signer '" + signer.signerName() + "' is signing " + items.size() + " file"
+ ((items.size() > 1) ? "s" : ""));
+ ((items.size() > 1) ? "s" : "") + " with key " + signer.getKeyInfo());

for (FilesCollector.Item item : items) {
getLog().debug("Generating signature for " + item.getFile());
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/apache/maven/plugins/gpg/GpgSigner.java
Expand Up @@ -34,7 +34,7 @@
*/
public class GpgSigner extends AbstractGpgSigner {
public static final String NAME = "gpg";
private String executable;
private final String executable;

public GpgSigner(String executable) {
this.executable = executable;
Expand All @@ -45,6 +45,11 @@ public String signerName() {
return NAME;
}

@Override
public String getKeyInfo() {
return keyname != null ? keyname : "default";
}

/**
* {@inheritDoc}
*/
Expand Down
Expand Up @@ -340,7 +340,7 @@ protected void doExecute() throws MojoExecutionException, MojoFailureException {
signer.setBaseDirectory(new File("").getAbsoluteFile());

getLog().info("Signer '" + signer.signerName() + "' is signing " + artifacts.size() + " file"
+ ((artifacts.size() > 1) ? "s" : ""));
+ ((artifacts.size() > 1) ? "s" : "") + " with key " + signer.getKeyInfo());

ArrayList<Artifact> signatures = new ArrayList<>();
for (Artifact a : artifacts) {
Expand Down

0 comments on commit 2be0a00

Please sign in to comment.