From 98bc0724235486c68a44243d456b2e3f41773dee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotrek=20=C5=BBygie=C5=82o?= Date: Wed, 18 Dec 2024 21:08:43 +0100 Subject: [PATCH] Flag to fail on keymap duplicate key item --- .../invoker.properties | 14 +++++ .../keysmap1.list | 16 +++++ .../keysmap2.list | 16 +++++ .../pom-test.xml | 61 +++++++++++++++++++ .../postbuild.groovy | 16 +++++ .../keysmap1.list | 16 +++++ .../keysmap2.list | 16 +++++ .../pom-test.xml | 60 ++++++++++++++++++ .../postbuild.groovy | 17 ++++++ .../keysmap1.list | 15 +++++ .../keysmap2.list | 15 +++++ .../pom-test.xml | 61 +++++++++++++++++++ .../postbuild.groovy | 16 +++++ .../org/simplify4u/plugins/CheckMojo.java | 14 +++++ .../DuplicateKeyMapKeyItemException.java | 22 +++++++ .../simplify4u/plugins/keysmap/KeyItems.java | 8 +++ .../simplify4u/plugins/keysmap/KeysMap.java | 7 +++ 17 files changed, 390 insertions(+) create mode 100644 src/it/multipleKeysMap-duplicates-fail/invoker.properties create mode 100644 src/it/multipleKeysMap-duplicates-fail/keysmap1.list create mode 100644 src/it/multipleKeysMap-duplicates-fail/keysmap2.list create mode 100644 src/it/multipleKeysMap-duplicates-fail/pom-test.xml create mode 100644 src/it/multipleKeysMap-duplicates-fail/postbuild.groovy create mode 100644 src/it/multipleKeysMap-duplicates-log/keysmap1.list create mode 100644 src/it/multipleKeysMap-duplicates-log/keysmap2.list create mode 100644 src/it/multipleKeysMap-duplicates-log/pom-test.xml create mode 100644 src/it/multipleKeysMap-duplicates-log/postbuild.groovy create mode 100644 src/it/multipleKeysMap-no-duplicates/keysmap1.list create mode 100644 src/it/multipleKeysMap-no-duplicates/keysmap2.list create mode 100644 src/it/multipleKeysMap-no-duplicates/pom-test.xml create mode 100644 src/it/multipleKeysMap-no-duplicates/postbuild.groovy create mode 100644 src/main/java/org/simplify4u/plugins/DuplicateKeyMapKeyItemException.java diff --git a/src/it/multipleKeysMap-duplicates-fail/invoker.properties b/src/it/multipleKeysMap-duplicates-fail/invoker.properties new file mode 100644 index 00000000..a8a45aa0 --- /dev/null +++ b/src/it/multipleKeysMap-duplicates-fail/invoker.properties @@ -0,0 +1,14 @@ +# +# Licensed 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 diff --git a/src/it/multipleKeysMap-duplicates-fail/keysmap1.list b/src/it/multipleKeysMap-duplicates-fail/keysmap1.list new file mode 100644 index 00000000..277b043f --- /dev/null +++ b/src/it/multipleKeysMap-duplicates-fail/keysmap1.list @@ -0,0 +1,16 @@ +# +# Licensed 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. +# + +commons-beanutils:commons-beanutils:1.7.0 = noSig +commons-logging:commons-logging:1.0.3 = noSig diff --git a/src/it/multipleKeysMap-duplicates-fail/keysmap2.list b/src/it/multipleKeysMap-duplicates-fail/keysmap2.list new file mode 100644 index 00000000..277b043f --- /dev/null +++ b/src/it/multipleKeysMap-duplicates-fail/keysmap2.list @@ -0,0 +1,16 @@ +# +# Licensed 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. +# + +commons-beanutils:commons-beanutils:1.7.0 = noSig +commons-logging:commons-logging:1.0.3 = noSig diff --git a/src/it/multipleKeysMap-duplicates-fail/pom-test.xml b/src/it/multipleKeysMap-duplicates-fail/pom-test.xml new file mode 100644 index 00000000..a1af80f3 --- /dev/null +++ b/src/it/multipleKeysMap-duplicates-fail/pom-test.xml @@ -0,0 +1,61 @@ + + + + 4.0.0 + + + test + it-test-parent + 0.0.1-SNAPSHOT + + + + test + pom + + + + commons-beanutils + commons-beanutils + 1.7.0 + + + + + + + org.simplify4u.plugins + pgpverify-maven-plugin + @project.version@ + + + ${project.basedir}/keysmap1.list + ${project.basedir}/keysmap2.list + + true + + + + + check + + + + + + + diff --git a/src/it/multipleKeysMap-duplicates-fail/postbuild.groovy b/src/it/multipleKeysMap-duplicates-fail/postbuild.groovy new file mode 100644 index 00000000..ab32f2be --- /dev/null +++ b/src/it/multipleKeysMap-duplicates-fail/postbuild.groovy @@ -0,0 +1,16 @@ +/* + * Licensed 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. + */ +buildLog = new File(basedir, 'build.log').text + +assert buildLog.matches('(?s).*Failing for the duplicate key item was found.*') diff --git a/src/it/multipleKeysMap-duplicates-log/keysmap1.list b/src/it/multipleKeysMap-duplicates-log/keysmap1.list new file mode 100644 index 00000000..277b043f --- /dev/null +++ b/src/it/multipleKeysMap-duplicates-log/keysmap1.list @@ -0,0 +1,16 @@ +# +# Licensed 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. +# + +commons-beanutils:commons-beanutils:1.7.0 = noSig +commons-logging:commons-logging:1.0.3 = noSig diff --git a/src/it/multipleKeysMap-duplicates-log/keysmap2.list b/src/it/multipleKeysMap-duplicates-log/keysmap2.list new file mode 100644 index 00000000..277b043f --- /dev/null +++ b/src/it/multipleKeysMap-duplicates-log/keysmap2.list @@ -0,0 +1,16 @@ +# +# Licensed 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. +# + +commons-beanutils:commons-beanutils:1.7.0 = noSig +commons-logging:commons-logging:1.0.3 = noSig diff --git a/src/it/multipleKeysMap-duplicates-log/pom-test.xml b/src/it/multipleKeysMap-duplicates-log/pom-test.xml new file mode 100644 index 00000000..e20edab4 --- /dev/null +++ b/src/it/multipleKeysMap-duplicates-log/pom-test.xml @@ -0,0 +1,60 @@ + + + + 4.0.0 + + + test + it-test-parent + 0.0.1-SNAPSHOT + + + + test + pom + + + + commons-beanutils + commons-beanutils + 1.7.0 + + + + + + + org.simplify4u.plugins + pgpverify-maven-plugin + @project.version@ + + + ${project.basedir}/keysmap1.list + ${project.basedir}/keysmap2.list + + + + + + check + + + + + + + diff --git a/src/it/multipleKeysMap-duplicates-log/postbuild.groovy b/src/it/multipleKeysMap-duplicates-log/postbuild.groovy new file mode 100644 index 00000000..9e9d2d0d --- /dev/null +++ b/src/it/multipleKeysMap-duplicates-log/postbuild.groovy @@ -0,0 +1,17 @@ +/* + * Licensed 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. + */ +buildLog = new File(basedir, 'build.log').text + +assert buildLog.matches('(?m)(?s).*^\\[WARNING] Duplicate key item: noSig in: keysMap: (?-s).*\\/keysmap2.list lineNumber: 15$(?s).*') +assert buildLog.matches('(?m)(?s).*^\\[WARNING] Duplicate key item: noSig in: keysMap: (?-s).*\\/keysmap2.list lineNumber: 16$(?s).*') diff --git a/src/it/multipleKeysMap-no-duplicates/keysmap1.list b/src/it/multipleKeysMap-no-duplicates/keysmap1.list new file mode 100644 index 00000000..38fed7d8 --- /dev/null +++ b/src/it/multipleKeysMap-no-duplicates/keysmap1.list @@ -0,0 +1,15 @@ +# +# Licensed 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. +# + +commons-beanutils:commons-beanutils:1.7.0 = noSig diff --git a/src/it/multipleKeysMap-no-duplicates/keysmap2.list b/src/it/multipleKeysMap-no-duplicates/keysmap2.list new file mode 100644 index 00000000..dc3f6c0d --- /dev/null +++ b/src/it/multipleKeysMap-no-duplicates/keysmap2.list @@ -0,0 +1,15 @@ +# +# Licensed 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. +# + +commons-logging:commons-logging:1.0.3 = noSig diff --git a/src/it/multipleKeysMap-no-duplicates/pom-test.xml b/src/it/multipleKeysMap-no-duplicates/pom-test.xml new file mode 100644 index 00000000..a1af80f3 --- /dev/null +++ b/src/it/multipleKeysMap-no-duplicates/pom-test.xml @@ -0,0 +1,61 @@ + + + + 4.0.0 + + + test + it-test-parent + 0.0.1-SNAPSHOT + + + + test + pom + + + + commons-beanutils + commons-beanutils + 1.7.0 + + + + + + + org.simplify4u.plugins + pgpverify-maven-plugin + @project.version@ + + + ${project.basedir}/keysmap1.list + ${project.basedir}/keysmap2.list + + true + + + + + check + + + + + + + diff --git a/src/it/multipleKeysMap-no-duplicates/postbuild.groovy b/src/it/multipleKeysMap-no-duplicates/postbuild.groovy new file mode 100644 index 00000000..fbdda00e --- /dev/null +++ b/src/it/multipleKeysMap-no-duplicates/postbuild.groovy @@ -0,0 +1,16 @@ +/* + * Licensed 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. + */ +buildLog = new File(basedir, 'build.log').text + +assert !buildLog.matches('(?s).*\\[WARNING] Duplicate key item: .*') diff --git a/src/main/java/org/simplify4u/plugins/CheckMojo.java b/src/main/java/org/simplify4u/plugins/CheckMojo.java index 1e3d6851..c7c1a5e3 100644 --- a/src/main/java/org/simplify4u/plugins/CheckMojo.java +++ b/src/main/java/org/simplify4u/plugins/CheckMojo.java @@ -175,6 +175,16 @@ public static class VerificationResult { @Parameter(property = "pgpverify.reportWrite", defaultValue = "false") private boolean reportWrite; + /** + *

+ * Indicate if build should fail if duplicate item is found in key maps. + *

+ * + * @since 1.19.0 + */ + @Parameter(property = "pgpverify.failDuplicateKeyItem", defaultValue = "false") + private boolean failDuplicateKeyItem; + @Override protected String getMojoName() { return MOJO_NAME; @@ -335,6 +345,10 @@ protected void processVerificationResult(Collection verifica if (verificationResult.stream().anyMatch(result -> result.error)) { throw new PGPMojoException("Signature errors"); } + + if (failDuplicateKeyItem && keysMap.isWithDuplicateKeyItems()) { + throw new DuplicateKeyMapKeyItemException(); + } } diff --git a/src/main/java/org/simplify4u/plugins/DuplicateKeyMapKeyItemException.java b/src/main/java/org/simplify4u/plugins/DuplicateKeyMapKeyItemException.java new file mode 100644 index 00000000..763ee418 --- /dev/null +++ b/src/main/java/org/simplify4u/plugins/DuplicateKeyMapKeyItemException.java @@ -0,0 +1,22 @@ +/* + * Copyright 2024 Simplify4U Contributors + * + * Licensed 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. + */ +package org.simplify4u.plugins; + +final class DuplicateKeyMapKeyItemException extends PGPMojoException { + DuplicateKeyMapKeyItemException() { + super("Failing for the duplicate key item was found"); + } +} diff --git a/src/main/java/org/simplify4u/plugins/keysmap/KeyItems.java b/src/main/java/org/simplify4u/plugins/keysmap/KeyItems.java index ecb0800f..282ab26c 100644 --- a/src/main/java/org/simplify4u/plugins/keysmap/KeyItems.java +++ b/src/main/java/org/simplify4u/plugins/keysmap/KeyItems.java @@ -18,6 +18,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; import lombok.ToString; import lombok.extern.slf4j.Slf4j; @@ -34,6 +35,8 @@ class KeyItems { private final List keys = new ArrayList<>(); + private final AtomicBoolean duplicateKeyItemFound = new AtomicBoolean(false); + /** * Add new keys to current list. * @@ -99,6 +102,7 @@ private void addKey(KeyItem keyItem, KeysMapContext keysMapContext) { keys.add(keyItem); } else { LOGGER.warn("Duplicate key item: {} in: {}", keyItem, keysMapContext); + duplicateKeyItemFound.set(true); } } @@ -150,4 +154,8 @@ public void excludes(List values) { public boolean isEmpty() { return keys.isEmpty(); } + + public boolean isWithDuplicateKeyItems() { + return duplicateKeyItemFound.get(); + } } diff --git a/src/main/java/org/simplify4u/plugins/keysmap/KeysMap.java b/src/main/java/org/simplify4u/plugins/keysmap/KeysMap.java index 29faaa96..e7b809f3 100644 --- a/src/main/java/org/simplify4u/plugins/keysmap/KeysMap.java +++ b/src/main/java/org/simplify4u/plugins/keysmap/KeysMap.java @@ -136,6 +136,13 @@ public boolean isWithKey(Artifact artifact) { .anyMatch(entry -> !entry.getValue().isNoSignature()); } + public boolean isWithDuplicateKeyItems() { + return items + .values() + .stream() + .anyMatch(KeyItems::isWithDuplicateKeyItems); + } + /** * Test if given key is trust for artifact. *