mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-03-06 04:10:52 +01:00
Enhance error messages on gpg key import (#1879)
This commit is contained in:
2
gradle/changelog/optimize_gpg_error_message.yaml
Normal file
2
gradle/changelog/optimize_gpg_error_message.yaml
Normal file
@@ -0,0 +1,2 @@
|
||||
- type: fixed
|
||||
description: Better error descriptions for gpg key import ([#1879](https://github.com/scm-manager/scm-manager/pull/1879))
|
||||
@@ -33,6 +33,7 @@ import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
||||
import org.bouncycastle.openpgp.PGPUtil;
|
||||
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
|
||||
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
|
||||
import sonia.scm.ScmConstraintViolationException;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
@@ -46,6 +47,8 @@ import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static sonia.scm.ScmConstraintViolationException.Builder.doThrow;
|
||||
|
||||
@Value
|
||||
final class Keys {
|
||||
|
||||
@@ -71,18 +74,18 @@ final class Keys {
|
||||
|
||||
for (PGPPublicKey key : parsedKeys) {
|
||||
if (key.isMasterKey()) {
|
||||
if (master != null) {
|
||||
throw new IllegalArgumentException("Found more than one master key");
|
||||
}
|
||||
doThrow()
|
||||
.violation("Found more than one master key")
|
||||
.when(master != null);
|
||||
master = createId(key);
|
||||
} else {
|
||||
subs.add(createId(key));
|
||||
}
|
||||
}
|
||||
|
||||
if (master == null) {
|
||||
throw new IllegalArgumentException("No master key found");
|
||||
}
|
||||
doThrow()
|
||||
.violation("No master key found")
|
||||
.when(master == null);
|
||||
|
||||
return new Keys(master, Collections.unmodifiableSet(subs));
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ import org.bouncycastle.openpgp.PGPPublicKey;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.ScmConstraintViolationException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
@@ -63,12 +64,12 @@ class KeysTest {
|
||||
PGPPublicKey one = mockMasterKey(42L);
|
||||
PGPPublicKey two = mockMasterKey(21L);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> Keys.resolve("", raw -> ImmutableList.of(one, two)));
|
||||
assertThrows(ScmConstraintViolationException.class, () -> Keys.resolve("", raw -> ImmutableList.of(one, two)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowIllegalArgumentExceptionWithoutMasterKey() {
|
||||
assertThrows(IllegalArgumentException.class, () -> Keys.resolve("", raw -> Collections.emptyList()));
|
||||
assertThrows(ScmConstraintViolationException.class, () -> Keys.resolve("", raw -> Collections.emptyList()));
|
||||
}
|
||||
|
||||
private PGPPublicKey mockMasterKey(long id) {
|
||||
|
||||
Reference in New Issue
Block a user