Skip to content

Commit

Permalink
feat: Default file digest algorithm to SHA-256 and allow to override
Browse files Browse the repository at this point in the history
This change allows to configure the digest algorithm used for files
inside the RPM file. Previously this defaulted to MD5. It is now also
possible to override this.

BREAKING CHANGE: By default, this will now create RPMs with a file
digest algorithm of SHA-256. It is possible to override this back to
MD5.

Closes: ctron#70
  • Loading branch information
ctron committed Jan 11, 2023
1 parent e6e6cac commit 6934e28
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/it/test15-default/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ println "Verify: " + result

def m1 = result =~ /MD5 digest\: OK/
def m2 = result =~ /Header SHA1 digest\: OK/
return m1.find() && m2.find()
def m3 = result =~ /Payload SHA256 digest\: OK/
return m1.find() && m2.find() && m3.find()
1 change: 1 addition & 0 deletions src/it/test15-md5-only/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<version>1.0.0</version>
<outputFileName>test15.rpm</outputFileName>
<signatureConfiguration>md5-only</signatureConfiguration>
<fileDigestAlgorithm>MD5</fileDigestAlgorithm>

<signature>
<keyId>${keyId}</keyId>
Expand Down
42 changes: 38 additions & 4 deletions src/main/java/de/dentrassi/rpm/builder/RpmMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,9 @@
import org.eclipse.packager.rpm.RpmLead;
import org.eclipse.packager.rpm.RpmTag;
import org.eclipse.packager.rpm.RpmVersion;
import org.eclipse.packager.rpm.build.BuilderContext;
import org.eclipse.packager.rpm.build.BuilderOptions;
import org.eclipse.packager.rpm.build.RpmBuilder;
import org.eclipse.packager.rpm.build.*;
import org.eclipse.packager.rpm.build.RpmBuilder.PackageInformation;
import org.eclipse.packager.rpm.build.RpmBuilder.Version;
import org.eclipse.packager.rpm.build.RpmFileNameProvider;
import org.eclipse.packager.rpm.deps.RpmDependencyFlags;
import org.eclipse.packager.rpm.signature.RsaHeaderSignatureProcessor;
import org.eclipse.packager.rpm.signature.RsaSignatureProcessor;
Expand Down Expand Up @@ -716,6 +713,24 @@ public void setSignatureConfiguration(final String signatureConfiguration) {
@Parameter(defaultValue = "${project.build.outputTimestamp}")
String outputTimestamp;

/**
* Configure the digest algorithm for files.
*
* <p>
* This configures the algorithm which is used to calculate a digest for each file. This information is stored
* (per file) in the RPM header section.
* </p>
*
* <p>
* <strong>NOTE:</strong> This used to be <code>MD5</code> in releases before <code>1.10.0</code>. Starting
* with <code>1.10.0</code> this defaults to <code>SHA-256</code> and can be overridden using this setting.
* </p>
*
* @since 1.10.0
*/
@Parameter(defaultValue = "SHA-256")
String fileDigestAlgorithm;

private Instant outputTimestampInstant;

@Component(role = SignatureConfiguration.class)
Expand Down Expand Up @@ -775,6 +790,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {
testLeadFlags();

final BuilderOptions options = new BuilderOptions();
options.setFileDigestAlgorithm(evalDigestAlgorithm(this.fileDigestAlgorithm));

// setup basic signature processors

Expand Down Expand Up @@ -848,6 +864,24 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
}

private DigestAlgorithm evalDigestAlgorithm(String algorithm) throws MojoFailureException {
try {
// try enum literal name first
return DigestAlgorithm.valueOf(algorithm);
}
catch (IllegalArgumentException ignored) {}

// try algorithm names next
for (DigestAlgorithm a : DigestAlgorithm.values()) {
if (a.getAlgorithm().equalsIgnoreCase(algorithm)) {
return a;
}
}

// fail
throw new MojoFailureException(String.format("Unknown file digest algorithm: %s", algorithm));
}

private String makeTargetFilename() {
String outputFileName = this.outputFileName;

Expand Down

0 comments on commit 6934e28

Please sign in to comment.