Skip to content

Commit

Permalink
[MGPG-125] Fix "bestPractices" (#95)
Browse files Browse the repository at this point in the history
As currently due defaulted values it would always fail, unless user explicitly re-configures plugin. So, do not set "default value" on mojo level, instead handle it programmatically, when bestPractices is not set. Added ITs for "bestPractices", one for success and one for failure. 

---

https://issues.apache.org/jira/browse/MGPG-125
  • Loading branch information
cstamas authored Apr 13, 2024
1 parent b6f0324 commit 893aedc
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 7 deletions.
18 changes: 18 additions & 0 deletions src/it/sign-release-best-practices-fail/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

invoker.buildResult = failure
100 changes: 100 additions & 0 deletions src/it/sign-release-best-practices-fail/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.its.gpg.sr</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<description>
Tests the installation of a simple release JAR with an attached artifact and its signatures.
</description>

<properties>
<maven.test.skip>true</maven.test.skip>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>@version.maven-compiler-plugin@</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>sign-artifacts</id>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
<configuration>
<bestPractices>true</bestPractices>
<!-- load passphrase from settings.xml -->
<!-- but not the default -->
<passphraseServerId>gpg.passphrase-sign-with-passphase-from-maven-settings</passphraseServerId>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>@version.maven-install-plugin@</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>@version.maven-jar-plugin@</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>@version.maven-resources-plugin@</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>@version.maven-source-plugin@</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>@version.maven-surefire@</version>
</plugin>
</plugins>
</build>

</project>
24 changes: 24 additions & 0 deletions src/it/sign-release-best-practices-fail/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

File buildLog = new File(basedir, "build.log")
String logContent = new File(basedir, "build.log").text

// assert that bestPractice+worstPractice => MojoFailure
assert logContent.contains("MojoFailureException: Do not store passphrase in any file")
18 changes: 18 additions & 0 deletions src/it/sign-release-best-practices/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

invoker.environmentVariables.MAVEN_GPG_PASSPHRASE = TEST
100 changes: 100 additions & 0 deletions src/it/sign-release-best-practices/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.its.gpg.sr</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<description>
Tests the installation of a simple release JAR with an attached artifact and its signatures.
</description>

<properties>
<maven.test.skip>true</maven.test.skip>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>@version.maven-compiler-plugin@</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>@project.version@</version>
<executions>
<execution>
<id>sign-artifacts</id>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
<configuration>
<bestPractices>true</bestPractices>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>@version.maven-install-plugin@</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>@version.maven-jar-plugin@</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>@version.maven-resources-plugin@</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>@version.maven-source-plugin@</version>
<configuration>
<forceCreation>true</forceCreation>
</configuration>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>@version.maven-surefire@</version>
</plugin>
</plugins>
</build>

</project>
36 changes: 36 additions & 0 deletions src/it/sign-release-best-practices/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var artifactDir = new File(localRepositoryPath, "org/apache/maven/its/gpg/sr/test/1.0")

var expectedFiles = [
"test-1.0.pom",
"test-1.0.pom.asc",
"test-1.0.jar",
"test-1.0.jar.asc",
"test-1.0-sources.jar",
"test-1.0-sources.jar.asc"
]

for (String expectedFile : expectedFiles) {
var file = new File(artifactDir, expectedFile)

println "Checking for existence of $file"

assert file.isFile()
}
4 changes: 1 addition & 3 deletions src/it/sign-release/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,5 @@ for (String expectedFile : expectedFiles) {

println "Checking for existence of $file"

if (!file.isFile()) {
throw new Exception("Missing file $file")
}
assert file.isFile()
}
21 changes: 17 additions & 4 deletions src/main/java/org/apache/maven/plugins/gpg/AbstractGpgMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,14 @@ public abstract class AbstractGpgMojo extends AbstractMojo {
* Server id to lookup the passphrase under Maven settings. <em>Do not use this parameter, it leaks
* sensitive data. Passphrase should be provided only via gpg-agent or via env variable.
* If parameter {@link #bestPractices} set to {@code true}, plugin fails when this parameter is configured.</em>
* Is programatically defaulted to {@link #GPG_PASSPHRASE}.
*
* @since 1.6
* @deprecated Do not use this configuration, it may leak sensitive information. Rely on gpg-agent or env
* variables instead.
**/
@Deprecated
@Parameter(property = "gpg.passphraseServerId", defaultValue = GPG_PASSPHRASE)
@Parameter(property = "gpg.passphraseServerId")
private String passphraseServerId;

/**
Expand Down Expand Up @@ -299,14 +300,26 @@ public final void execute() throws MojoExecutionException, MojoFailureException
// We're skipping the signing stuff
return;
}
if (bestPractices && (isNotBlank(passphrase) || isNotBlank(passphraseServerId))) {
if (bestPractices) {
enforceBestPractices();
} else {
if (!isNotBlank(passphraseServerId)) {
// default it programmatically: this is needed to handle different cases re bestPractices
passphraseServerId = GPG_PASSPHRASE;
}
}

doExecute();
}

protected void enforceBestPractices() throws MojoFailureException {
// if any of those are not blank: meaning user did explicitly configure these
if (isNotBlank(passphrase) || isNotBlank(passphraseServerId)) {
// Stop propagating worst practices: passphrase MUST NOT be in any file on disk
throw new MojoFailureException(
"Do not store passphrase in any file (disk or SCM repository), rely on GnuPG agent or provide passphrase in "
+ passphraseEnvName + " environment variable.");
}

doExecute();
}

protected abstract void doExecute() throws MojoExecutionException, MojoFailureException;
Expand Down

0 comments on commit 893aedc

Please sign in to comment.