Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature - allow signing to be specified in settings.xml #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 110 additions & 8 deletions src/main/java/de/dentrassi/rpm/builder/RpmMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.*;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to have expanded imports.

Copy link
Author

@adamretter adamretter Jun 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay sure. Apologies, my IDE (IntelliJ) insists on doing these automated "improvements" for me.

import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand All @@ -50,6 +45,8 @@
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectHelper;
import org.apache.maven.settings.Profile;
import org.apache.maven.settings.Settings;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.codehaus.plexus.util.DirectoryScanner;
import org.eclipse.packager.rpm.Architecture;
Expand Down Expand Up @@ -691,6 +688,21 @@ public void setSignatureConfiguration ( final String signatureConfiguration )
this.signatureConfiguration = signatureConfiguration;
}

/**
* The prefix to use when reading signing variables
* from Maven's settings.xml.
*
* The signing variables are: keyId, keyringFile, passphrase, hashAlgorithm.
*/
@Parameter(defaultValue = "rpm.")
private String signCfgPrefix;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a non-abbreviated name, e.g. signConfigurationPrefix. And wouldn't it make sense to allow the user to set this via properties as well?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can certainly do that. I just took the existing syntax from jdeb, but I can update it...


/**
* The settings.
*/
@Parameter(defaultValue = "${settings}")
private Settings settings;

@Component ( role = SignatureConfiguration.class )
protected Map<String, SignatureConfiguration> signatureConfigurationProviders;

Expand Down Expand Up @@ -772,8 +784,7 @@ public void execute () throws MojoExecutionException, MojoFailureException
fillPayload ( builder );
fillPrefixes ( builder );

// setup basic signature processors

// setup signature providers
if ( this.signatureConfiguration != null )
{
this.logger.info ( "Initialize with custom signature configuration: %s (%s)", this.signatureConfiguration, this.signatureConfiguration.getClass () );
Expand All @@ -785,6 +796,47 @@ public void execute () throws MojoExecutionException, MojoFailureException
provider.apply ( builder );
}

// load any default signing properties from settings.xml
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe wrap this section up in another method?

final Map<String, String> properties =
readPropertiesFromActiveProfiles(signCfgPrefix, "keyId", "keyringFile", "passphrase", "hashAlgorithm");
final String keyId = properties.get("keyId");
if (keyId != null) {
if (signature == null) {
signature = new Signature();
}
if (signature.getKeyId() == null) {
signature.setKeyId(keyId);
}
}
final String keyringFile = properties.get("keyringFile");
if (keyringFile != null) {
if (signature == null) {
signature = new Signature();
}
if (signature.getKeyringFile() == null) {
signature.setKeyringFile(new File(keyringFile));
}
}
final String passphrase = properties.get("passphrase");
if (passphrase != null) {
if (signature == null) {
signature = new Signature();
}
if (signature.getPassphrase() == null) {
signature.setPassphrase(passphrase);
}
}
final String hashAlgorithm = properties.get("hashAlgorithm");
if (hashAlgorithm != null) {
if (signature == null) {
signature = new Signature();
}
if (signature.getHashAlgorithm() == null) {
signature.setHashAlgorithm(hashAlgorithm);
}
}

// setup basic signature processors
if ( !this.skipSigning && this.signature != null )
{
final SignatureProcessor signer = makeRsaSigner ( this.signature );
Expand Down Expand Up @@ -1396,4 +1448,54 @@ private static void ifSet ( final Consumer<String> setter, final String value, f
}
}

/**
* Read properties from the active profiles.
*
* Goes through all active profiles (in the order the
* profiles are defined in settings.xml) and extracts
* the desired properties (if present). The prefix is
* used when looking up properties in the profile but
* not in the returned map.
*
* @param prefix The prefix to use or null if no prefix should be used
* @param properties The properties to read
*
* @return A map containing the values for the properties that were found
*/
public Map<String, String> readPropertiesFromActiveProfiles(final String prefix,
final String... properties) {
if (settings == null) {
this.logger.debug("No maven setting injected");
return Collections.emptyMap();
}

final List<String> activeProfilesList = settings.getActiveProfiles();
if (activeProfilesList.isEmpty()) {
this.logger.debug("No active profiles found");
return Collections.emptyMap();
}

final Map<String, String> map = new HashMap<String, String>();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC we are on Java 8. So you can drop the type arguments on the constructor:

Suggested change
final Map<String, String> map = new HashMap<String, String>();
final Map<String, String> map = new HashMap<>();

final Set<String> activeProfiles = new HashSet<String>(activeProfilesList);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final Set<String> activeProfiles = new HashSet<String>(activeProfilesList);
final Set<String> activeProfiles = new HashSet<>(activeProfilesList);


// Iterate over all active profiles in order
for (final Profile profile : settings.getProfiles()) {
// Check if the profile is active
final String profileId = profile.getId();
if (activeProfiles.contains(profileId)) {
this.logger.debug("Trying active profile " + profileId);
for (final String property : properties) {
final String propKey = prefix != null ? prefix + property : property;
final String value = profile.getProperties().getProperty(propKey);
if (value != null) {
this.logger.debug("Found property " + property + " in profile " + profileId);
map.put(property, value);
}
}
}
}

return map;
}

}