-
Notifications
You must be signed in to change notification settings - Fork 32
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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.*; | ||||||
import java.util.function.Consumer; | ||||||
import java.util.function.Supplier; | ||||||
import java.util.stream.Collectors; | ||||||
|
@@ -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; | ||||||
|
@@ -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; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer a non-abbreviated name, e.g. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||||||
|
||||||
|
@@ -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 () ); | ||||||
|
@@ -785,6 +796,47 @@ public void execute () throws MojoExecutionException, MojoFailureException | |||||
provider.apply ( builder ); | ||||||
} | ||||||
|
||||||
// load any default signing properties from settings.xml | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||||||
|
@@ -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>(); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Set<String> activeProfiles = new HashSet<String>(activeProfilesList); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
// 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; | ||||||
} | ||||||
|
||||||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.