fixes gpg verification

This commit is contained in:
Sebastian Sdorra
2020-07-30 11:58:11 +02:00
parent a1153df50a
commit 274ce561fe
15 changed files with 108 additions and 187 deletions

View File

@@ -52,7 +52,7 @@ class DefaultGPGTest {
@Test
void shouldFindIdInSignature() throws IOException {
String raw = GPGTestHelper.readResource("signature.asc");
String raw = GPGTestHelper.readResourceAsString("signature.asc");
String publicKeyId = gpg.findPublicKeyId(raw.getBytes());
assertThat(publicKeyId).isEqualTo("0x1F17B79A09DAD5B9");
@@ -60,7 +60,7 @@ class DefaultGPGTest {
@Test
void shouldFindPublicKey() throws IOException {
String raw = GPGTestHelper.readResource("subkeys.asc");
String raw = GPGTestHelper.readResourceAsString("subkeys.asc");
RawGpgKey key1 = new RawGpgKey("42", "key_42", "trillian", raw, ImmutableSet.of("trillian", "zaphod"), Instant.now());
when(store.findById("42")).thenReturn(Optional.of(key1));
@@ -76,8 +76,8 @@ class DefaultGPGTest {
@Test
void shouldFindKeysForUsername() throws IOException {
String raw = GPGTestHelper.readResource("single.asc");
String raw2= GPGTestHelper.readResource("subkeys.asc");
String raw = GPGTestHelper.readResourceAsString("single.asc");
String raw2= GPGTestHelper.readResourceAsString("subkeys.asc");
RawGpgKey key1 = new RawGpgKey("1", "1", "trillian", raw, Collections.emptySet(), Instant.now());
RawGpgKey key2 = new RawGpgKey("2", "2", "trillian", raw2, Collections.emptySet(), Instant.now());

View File

@@ -36,7 +36,13 @@ final class GPGTestHelper {
}
@SuppressWarnings("UnstableApiUsage")
static String readResource(String fileName) throws IOException {
static byte[] readResourceAsBytes(String fileName) throws IOException {
URL resource = Resources.getResource("sonia/scm/security/gpg/" + fileName);
return Resources.toByteArray(resource);
}
@SuppressWarnings("UnstableApiUsage")
static String readResourceAsString(String fileName) throws IOException {
URL resource = Resources.getResource("sonia/scm/security/gpg/" + fileName);
return Resources.toString(resource, StandardCharsets.UTF_8);
}

View File

@@ -35,19 +35,14 @@ class GpgKeyTest {
@Test
void shouldVerifyPublicKey() throws IOException {
StringBuilder longContent = new StringBuilder();
for (int i = 1; i < 10000; i++) {
longContent.append(i);
}
String rawPublicKey = GPGTestHelper.readResourceAsString("subkeys.asc");
GpgKey publicKey = new GpgKey("1", "trillian", rawPublicKey, Collections.emptySet());
String raw = GPGTestHelper.readResource("pubKeyEH.asc");
String signature = GPGTestHelper.readResource("signature.asc");
byte[] content = GPGTestHelper.readResourceAsBytes("slarti.txt");
byte[] signature = GPGTestHelper.readResourceAsBytes("slarti.txt.asc");
GpgKey key = new GpgKey("1", "trillian", raw, Collections.emptySet());
boolean verified = key.verify(longContent.toString().getBytes(), signature.getBytes());
//assertThat(verified).isTrue();
boolean verified = publicKey.verify(content, signature);
assertThat(verified).isTrue();
}
}

View File

@@ -38,21 +38,21 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static sonia.scm.security.gpg.GPGTestHelper.readResource;
import static sonia.scm.security.gpg.GPGTestHelper.readResourceAsString;
@ExtendWith(MockitoExtension.class)
class KeysTest {
@Test
void shouldResolveSingleId() throws IOException {
String rawPublicKey = readResource("single.asc");
String rawPublicKey = readResourceAsString("single.asc");
Keys keys = Keys.resolve(rawPublicKey);
assertThat(keys.getMaster()).isEqualTo("0x975922F193B07D6E");
}
@Test
void shouldResolveIdsFromSubkeys() throws IOException {
String rawPublicKey = readResource("subkeys.asc");
String rawPublicKey = readResourceAsString("subkeys.asc");
Keys keys = Keys.resolve(rawPublicKey);
assertThat(keys.getMaster()).isEqualTo("0x13B13D4C8A9350A1");
assertThat(keys.getSubs()).containsOnly("0x247E908C6FD35473", "0xE50E1DD8B90D3A6B", "0xBF49759E43DD0E60");

View File

@@ -36,7 +36,7 @@ class PgpPublicKeyExtractorTest {
@Test
void shouldExtractPublicKeyFromRawKey() throws IOException {
String raw = GPGTestHelper.readResource("pubKeyEH.asc");
String raw = GPGTestHelper.readResourceAsString("pubKeyEH.asc");
Optional<PGPPublicKey> publicKey = PgpPublicKeyExtractor.getFromRawKey(raw);

View File

@@ -104,7 +104,7 @@ class PublicKeyCollectionMapperTest {
}
private RawGpgKey createPublicKey(String displayName) throws IOException {
String raw = GPGTestHelper.readResource("single.asc");
String raw = GPGTestHelper.readResourceAsString("single.asc");
return new RawGpgKey(displayName, displayName, "trillian", raw, Collections.emptySet(), Instant.now());
}

View File

@@ -69,7 +69,7 @@ class PublicKeyMapperTest {
void shouldMapKeyToDto() throws IOException {
when(subject.isPermitted("user:changePublicKeys:trillian")).thenReturn(true);
String raw = GPGTestHelper.readResource("single.asc");
String raw = GPGTestHelper.readResourceAsString("single.asc");
RawGpgKey key = new RawGpgKey("1", "key_42", "trillian", raw, Collections.emptySet(), Instant.now());
RawGpgKeyDto dto = mapper.map(key);
@@ -83,7 +83,7 @@ class PublicKeyMapperTest {
@Test
void shouldNotAppendDeleteLink() throws IOException {
String raw = GPGTestHelper.readResource("single.asc");
String raw = GPGTestHelper.readResourceAsString("single.asc");
RawGpgKey key = new RawGpgKey("1", "key_42", "trillian", raw, Collections.emptySet(), Instant.now());
RawGpgKeyDto dto = mapper.map(key);

View File

@@ -112,7 +112,7 @@ class PublicKeyResourceTest {
@Test
void shouldAddToStore() throws URISyntaxException, IOException {
String raw = GPGTestHelper.readResource("single.asc");
String raw = GPGTestHelper.readResourceAsString("single.asc");
UriInfo uriInfo = mock(UriInfo.class);
UriBuilder builder = mock(UriBuilder.class);

View File

@@ -80,21 +80,21 @@ class PublicKeyStoreTest {
@Test
void shouldThrowAuthorizationExceptionOnAdd() throws IOException {
doThrow(AuthorizationException.class).when(subject).checkPermission("user:changePublicKeys:zaphod");
String rawKey = GPGTestHelper.readResource("single.asc");
String rawKey = GPGTestHelper.readResourceAsString("single.asc");
assertThrows(AuthorizationException.class, () -> keyStore.add("zaphods key", "zaphod", rawKey));
}
@Test
void shouldOnlyStorePublicKeys() throws IOException {
String rawKey = GPGTestHelper.readResource("single.asc").replace("PUBLIC", "PRIVATE");
String rawKey = GPGTestHelper.readResourceAsString("single.asc").replace("PUBLIC", "PRIVATE");
assertThrows(NotPublicKeyException.class, () -> keyStore.add("SCM Package Key", "trillian", rawKey));
}
@Test
void shouldReturnStoredKey() throws IOException {
String rawKey = GPGTestHelper.readResource("single.asc");
String rawKey = GPGTestHelper.readResourceAsString("single.asc");
Instant now = Instant.now();
RawGpgKey key = keyStore.add("SCM Package Key", "trillian", rawKey);
@@ -107,7 +107,7 @@ class PublicKeyStoreTest {
@Test
void shouldFindStoredKeyById() throws IOException {
String rawKey = GPGTestHelper.readResource("single.asc");
String rawKey = GPGTestHelper.readResourceAsString("single.asc");
keyStore.add("SCM Package Key", "trillian", rawKey);
Optional<RawGpgKey> key = keyStore.findById("0x975922F193B07D6E");
assertThat(key).isPresent();
@@ -115,7 +115,7 @@ class PublicKeyStoreTest {
@Test
void shouldDeleteKey() throws IOException {
String rawKey = GPGTestHelper.readResource("single.asc");
String rawKey = GPGTestHelper.readResourceAsString("single.asc");
keyStore.add("SCM Package Key", "trillian", rawKey);
Optional<RawGpgKey> key = keyStore.findById("0x975922F193B07D6E");
@@ -139,10 +139,10 @@ class PublicKeyStoreTest {
@Test
void shouldFindAllKeysForUser() throws IOException {
String singleKey = GPGTestHelper.readResource("single.asc");
String singleKey = GPGTestHelper.readResourceAsString("single.asc");
keyStore.add("SCM Single Key", "trillian", singleKey);
String multiKey = GPGTestHelper.readResource("subkeys.asc");
String multiKey = GPGTestHelper.readResourceAsString("subkeys.asc");
keyStore.add("SCM Multi Key", "trillian", multiKey);
List<RawGpgKey> keys = keyStore.findByUsername("trillian");