mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 08:25:44 +01:00
show signature key on changeset
This commit is contained in:
@@ -35,6 +35,7 @@ import sonia.scm.security.PublicKey;
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -50,15 +51,16 @@ class DefaultGPGTest {
|
||||
|
||||
@Test
|
||||
void shouldFindIdInSignature() throws IOException {
|
||||
String raw = GPGTestHelper.readKey("single.asc");
|
||||
String raw = GPGTestHelper.readKey("signature.asc");
|
||||
String publicKeyId = gpg.findPublicKeyId(raw.getBytes());
|
||||
|
||||
assertThat(publicKeyId).isEqualTo("0x975922F193B07D6E");
|
||||
assertThat(publicKeyId).isEqualTo("0x1F17B79A09DAD5B9");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindPublicKey() {
|
||||
RawGpgKey key1 = new RawGpgKey("42", "key_42", "trillian", "raw", Instant.now());
|
||||
void shouldFindPublicKey() throws IOException {
|
||||
String raw = GPGTestHelper.readKey("subkeys.asc");
|
||||
RawGpgKey key1 = new RawGpgKey("42", "key_42", "trillian", raw, Instant.now());
|
||||
|
||||
when(store.findById("42")).thenReturn(Optional.of(key1));
|
||||
|
||||
@@ -68,12 +70,17 @@ class DefaultGPGTest {
|
||||
assertThat(publicKey.get().getOwner()).isPresent();
|
||||
assertThat(publicKey.get().getOwner().get()).contains("trillian");
|
||||
assertThat(publicKey.get().getId()).isEqualTo("42");
|
||||
assertThat(publicKey.get().getContacts()).contains("Sebastian Sdorra <s.sdorra@gmail.com>",
|
||||
"Sebastian Sdorra <sebastian.sdorra@cloudogu.com>");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindKeysForUsername() {
|
||||
RawGpgKey key1 = new RawGpgKey("1", "1", "trillian", "raw", Instant.now());
|
||||
RawGpgKey key2 = new RawGpgKey("2", "2", "trillian", "raw", Instant.now());
|
||||
void shouldFindKeysForUsername() throws IOException {
|
||||
String raw = GPGTestHelper.readKey("single.asc");
|
||||
String raw2= GPGTestHelper.readKey("subkeys.asc");
|
||||
|
||||
RawGpgKey key1 = new RawGpgKey("1", "1", "trillian", raw, Instant.now());
|
||||
RawGpgKey key2 = new RawGpgKey("2", "2", "trillian", raw2, Instant.now());
|
||||
when(store.findByUsername("trillian")).thenReturn(ImmutableList.of(key1, key2));
|
||||
|
||||
Iterable<PublicKey> keys = gpg.findPublicKeysByUsername("trillian");
|
||||
|
||||
@@ -28,8 +28,6 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class GpgKeyTest {
|
||||
|
||||
@Test
|
||||
@@ -41,11 +39,11 @@ class GpgKeyTest {
|
||||
|
||||
byte[] raw = GPGTestHelper.readKey("subkeys.asc").getBytes();
|
||||
|
||||
GpgKey key = new GpgKey("1", "trillian");
|
||||
GpgKey key = new GpgKey("1", "trillian", raw);
|
||||
|
||||
boolean verified = key.verify(longContent.toString().getBytes(), raw);
|
||||
|
||||
// assertThat(verified).isTrue();
|
||||
// assertThat(verified).isTrue();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,26 +24,58 @@
|
||||
|
||||
package sonia.scm.security.gpg;
|
||||
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
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 java.io.IOException;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
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.readKey;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class KeysTest {
|
||||
|
||||
@Test
|
||||
void shouldResolveId() throws IOException, PGPException {
|
||||
void shouldResolveSingleId() throws IOException {
|
||||
String rawPublicKey = readKey("single.asc");
|
||||
assertThat(Keys.resolveIdFromKey(rawPublicKey)).isEqualTo("0x975922F193B07D6E");
|
||||
Keys keys = Keys.resolve(rawPublicKey);
|
||||
assertThat(keys.getMaster()).isEqualTo("0x975922F193B07D6E");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldResolveIdFromMasterKey() throws IOException, PGPException {
|
||||
void shouldResolveIdsFromSubkeys() throws IOException {
|
||||
String rawPublicKey = readKey("subkeys.asc");
|
||||
assertThat(Keys.resolveIdFromKey(rawPublicKey)).isEqualTo("0x13B13D4C8A9350A1");
|
||||
Keys keys = Keys.resolve(rawPublicKey);
|
||||
assertThat(keys.getMaster()).isEqualTo("0x13B13D4C8A9350A1");
|
||||
assertThat(keys.getSubs()).containsOnly("0x247E908C6FD35473", "0xE50E1DD8B90D3A6B", "0xBF49759E43DD0E60");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowIllegalArgumentExceptionForMultipleMasterKeys() {
|
||||
PGPPublicKey one = mockMasterKey(42L);
|
||||
PGPPublicKey two = mockMasterKey(21L);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> Keys.resolve("", raw -> ImmutableList.of(one, two)));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowIllegalArgumentExceptionWithoutMasterKey() {
|
||||
assertThrows(IllegalArgumentException.class, () -> Keys.resolve("", raw -> Collections.emptyList()));
|
||||
}
|
||||
|
||||
private PGPPublicKey mockMasterKey(long id) {
|
||||
PGPPublicKey key = mock(PGPPublicKey.class);
|
||||
when(key.isMasterKey()).thenReturn(true);
|
||||
lenient().when(key.getKeyID()).thenReturn(id);
|
||||
return key;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,7 +33,9 @@ import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.event.ScmEventBus;
|
||||
import sonia.scm.security.NotPublicKeyException;
|
||||
import sonia.scm.security.PublicKeyDeletedEvent;
|
||||
import sonia.scm.store.DataStoreFactory;
|
||||
import sonia.scm.store.InMemoryDataStoreFactory;
|
||||
|
||||
@@ -44,7 +46,9 @@ import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class PublicKeyStoreTest {
|
||||
@@ -52,12 +56,15 @@ class PublicKeyStoreTest {
|
||||
@Mock
|
||||
private Subject subject;
|
||||
|
||||
@Mock
|
||||
private ScmEventBus eventBus;
|
||||
|
||||
private PublicKeyStore keyStore;
|
||||
private final DataStoreFactory dataStoreFactory = new InMemoryDataStoreFactory();
|
||||
|
||||
@BeforeEach
|
||||
void setUpKeyStore() {
|
||||
keyStore = new PublicKeyStore(dataStoreFactory);
|
||||
keyStore = new PublicKeyStore(dataStoreFactory, eventBus);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
@@ -72,7 +79,7 @@ class PublicKeyStoreTest {
|
||||
|
||||
@Test
|
||||
void shouldThrowAuthorizationExceptionOnAdd() throws IOException {
|
||||
doThrow(AuthorizationException.class).when(subject).checkPermission("user:modify:zaphod");
|
||||
doThrow(AuthorizationException.class).when(subject).checkPermission("user:changePublicKeys:zaphod");
|
||||
String rawKey = GPGTestHelper.readKey("single.asc");
|
||||
|
||||
assertThrows(AuthorizationException.class, () -> keyStore.add("zaphods key", "zaphod", rawKey));
|
||||
@@ -118,6 +125,8 @@ class PublicKeyStoreTest {
|
||||
key = keyStore.findById("0x975922F193B07D6E");
|
||||
|
||||
assertThat(key).isNotPresent();
|
||||
|
||||
verify(eventBus).post(any(PublicKeyDeletedEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user