mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 08:25:44 +01:00
add config form for public keys
This commit is contained in:
@@ -198,6 +198,28 @@ class MeDtoFactoryTest {
|
||||
assertThat(dto.getLinks().getLinkBy("password")).isNotPresent();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAppendPublicKeysLink() {
|
||||
User user = UserTestData.createTrillian();
|
||||
prepareSubject(user);
|
||||
|
||||
when(subject.isPermitted("user:changePublicKeys:trillian")).thenReturn(true);
|
||||
|
||||
MeDto dto = meDtoFactory.create();
|
||||
assertThat(dto.getLinks().getLinkBy("publicKeys").get().getHref()).isEqualTo("https://scm.hitchhiker.com/scm/v2/public_keys/trillian");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotAppendPublicKeysLink() {
|
||||
User user = UserTestData.createTrillian();
|
||||
prepareSubject(user);
|
||||
|
||||
when(subject.isPermitted("user:changePublicKeys:trillian")).thenReturn(false);
|
||||
|
||||
MeDto dto = meDtoFactory.create();
|
||||
assertThat(dto.getLinks().getLinkBy("publicKeys")).isNotPresent();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAppendLinks() {
|
||||
prepareSubject(UserTestData.createTrillian());
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.security.gpg;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.inject.util.Providers;
|
||||
import de.otto.edison.hal.HalRepresentation;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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.api.v2.resources.ScmPathInfoStore;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class PublicKeyCollectionMapperTest {
|
||||
|
||||
|
||||
private PublicKeyCollectionMapper collectionMapper;
|
||||
|
||||
@Mock
|
||||
private PublicKeyMapper mapper;
|
||||
|
||||
@Mock
|
||||
private Subject subject;
|
||||
|
||||
@BeforeEach
|
||||
void setUpObjectUnderTest() {
|
||||
ScmPathInfoStore pathInfoStore = new ScmPathInfoStore();
|
||||
pathInfoStore.set(() -> URI.create("/"));
|
||||
collectionMapper = new PublicKeyCollectionMapper(Providers.of(pathInfoStore), mapper);
|
||||
|
||||
ThreadContext.bind(subject);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void cleanThreadContext() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldMapToCollection() throws IOException {
|
||||
when(mapper.map(any(RawGpgKey.class))).then(ic -> new RawGpgKeyDto());
|
||||
|
||||
RawGpgKey one = createPublicKey("one");
|
||||
RawGpgKey two = createPublicKey("two");
|
||||
|
||||
List<RawGpgKey> keys = Lists.newArrayList(one, two);
|
||||
HalRepresentation collection = collectionMapper.map("trillian", keys);
|
||||
|
||||
List<HalRepresentation> embedded = collection.getEmbedded().getItemsBy("keys");
|
||||
assertThat(embedded).hasSize(2);
|
||||
|
||||
assertThat(collection.getLinks().getLinkBy("self").get().getHref()).isEqualTo("/v2/public_keys/trillian");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAddCreateLinkIfTheUserIsPermitted() {
|
||||
when(subject.isPermitted("user:changePublicKeys:trillian")).thenReturn(true);
|
||||
|
||||
HalRepresentation collection = collectionMapper.map("trillian", Lists.newArrayList());
|
||||
assertThat(collection.getLinks().getLinkBy("create").get().getHref()).isEqualTo("/v2/public_keys/trillian");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotAddCreateLinkWithoutPermission() {
|
||||
HalRepresentation collection = collectionMapper.map("trillian", Lists.newArrayList());
|
||||
assertThat(collection.getLinks().getLinkBy("create")).isNotPresent();
|
||||
}
|
||||
|
||||
private RawGpgKey createPublicKey(String displayName) throws IOException {
|
||||
String raw = GPGTestHelper.readKey("single.asc");
|
||||
return new RawGpgKey(displayName, displayName, "trillian", raw, Instant.now());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.security.gpg;
|
||||
|
||||
import com.google.inject.util.Providers;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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.api.v2.resources.ScmPathInfoStore;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class PublicKeyMapperTest {
|
||||
|
||||
@Mock
|
||||
private Subject subject;
|
||||
|
||||
private final PublicKeyMapper mapper = new PublicKeyMapperImpl();
|
||||
ScmPathInfoStore pathInfoStore = new ScmPathInfoStore();
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
ThreadContext.bind(subject);
|
||||
|
||||
pathInfoStore.set(() -> URI.create("/"));
|
||||
mapper.setScmPathInfoStore(Providers.of(pathInfoStore));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDownSubject() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldMapKeyToDto() throws IOException {
|
||||
when(subject.isPermitted("user:changePublicKeys:trillian")).thenReturn(true);
|
||||
|
||||
String raw = GPGTestHelper.readKey("single.asc");
|
||||
RawGpgKey key = new RawGpgKey("1", "key_42", "trillian", raw, Instant.now());
|
||||
|
||||
RawGpgKeyDto dto = mapper.map(key);
|
||||
|
||||
assertThat(dto.getDisplayName()).isEqualTo(key.getDisplayName());
|
||||
assertThat(dto.getRaw()).isEqualTo(key.getRaw());
|
||||
assertThat(dto.getCreated()).isEqualTo(key.getCreated());
|
||||
assertThat(dto.getLinks().getLinkBy("self").get().getHref()).isEqualTo("/v2/public_keys/1");
|
||||
assertThat(dto.getLinks().getLinkBy("delete").get().getHref()).isEqualTo("/v2/public_keys/delete/1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotAppendDeleteLink() throws IOException {
|
||||
String raw = GPGTestHelper.readKey("single.asc");
|
||||
RawGpgKey key = new RawGpgKey("1", "key_42", "trillian", raw, Instant.now());
|
||||
|
||||
RawGpgKeyDto dto = mapper.map(key);
|
||||
|
||||
assertThat(dto.getLinks().getLinkBy("delete")).isNotPresent();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.security.gpg;
|
||||
|
||||
import de.otto.edison.hal.HalRepresentation;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriBuilder;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class PublicKeyResourceTest {
|
||||
|
||||
@Mock
|
||||
private PublicKeyStore store;
|
||||
|
||||
@Mock
|
||||
private PublicKeyCollectionMapper collectionMapper;
|
||||
|
||||
@Mock
|
||||
private PublicKeyMapper mapper;
|
||||
|
||||
@InjectMocks
|
||||
private PublicKeyResource resource;
|
||||
|
||||
@Mock
|
||||
private Subject subject;
|
||||
|
||||
@BeforeEach
|
||||
void setUpSubject() {
|
||||
ThreadContext.bind(subject);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void clearSubject() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindAll() {
|
||||
List<RawGpgKey> keys = new ArrayList<>();
|
||||
when(store.findByUsername("trillian")).thenReturn(keys);
|
||||
|
||||
HalRepresentation collection = new HalRepresentation();
|
||||
when(collectionMapper.map("trillian", keys)).thenReturn(collection);
|
||||
|
||||
HalRepresentation result = resource.findAll("trillian");
|
||||
assertThat(result).isSameAs(collection);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindById() {
|
||||
RawGpgKey key = new RawGpgKey("42");
|
||||
when(store.findById("42")).thenReturn(Optional.of(key));
|
||||
RawGpgKeyDto dto = new RawGpgKeyDto();
|
||||
when(mapper.map(key)).thenReturn(dto);
|
||||
|
||||
Response response = resource.findById("42");
|
||||
assertThat(response.getStatus()).isEqualTo(200);
|
||||
assertThat(response.getEntity()).isSameAs(dto);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturn404IfIdDoesNotExists() {
|
||||
when(store.findById("42")).thenReturn(Optional.empty());
|
||||
|
||||
Response response = resource.findById("42");
|
||||
assertThat(response.getStatus()).isEqualTo(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAddToStore() throws URISyntaxException, IOException {
|
||||
String raw = GPGTestHelper.readKey("single.asc");
|
||||
|
||||
UriInfo uriInfo = mock(UriInfo.class);
|
||||
UriBuilder builder = mock(UriBuilder.class);
|
||||
when(uriInfo.getAbsolutePathBuilder()).thenReturn(builder);
|
||||
when(builder.path("42")).thenReturn(builder);
|
||||
when(builder.build()).thenReturn(new URI("/v2/public_keys/42"));
|
||||
|
||||
RawGpgKey key = new RawGpgKey("42");
|
||||
RawGpgKeyDto dto = new RawGpgKeyDto();
|
||||
dto.setDisplayName("key_42");
|
||||
dto.setRaw(raw);
|
||||
when(store.add(dto.getDisplayName(), "trillian", dto.getRaw())).thenReturn(key);
|
||||
|
||||
Response response = resource.create(uriInfo, "trillian", dto);
|
||||
|
||||
assertThat(response.getStatus()).isEqualTo(201);
|
||||
assertThat(response.getLocation().toASCIIString()).isEqualTo("/v2/public_keys/42");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDeleteFromStore() {
|
||||
Response response = resource.deleteById("42");
|
||||
assertThat(response.getStatus()).isEqualTo(204);
|
||||
verify(store).delete("42");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,23 +24,65 @@
|
||||
|
||||
package sonia.scm.security.gpg;
|
||||
|
||||
import org.apache.shiro.authz.AuthorizationException;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.util.ThreadContext;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import sonia.scm.store.InMemoryDataStore;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.security.NotPublicKeyException;
|
||||
import sonia.scm.store.DataStoreFactory;
|
||||
import sonia.scm.store.InMemoryDataStoreFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class PublicKeyStoreTest {
|
||||
|
||||
@Mock
|
||||
private Subject subject;
|
||||
|
||||
private PublicKeyStore keyStore;
|
||||
private final DataStoreFactory dataStoreFactory = new InMemoryDataStoreFactory();
|
||||
|
||||
@BeforeEach
|
||||
void setUpKeyStore() {
|
||||
keyStore = new PublicKeyStore(new InMemoryDataStore<>(), () -> "trillian");
|
||||
keyStore = new PublicKeyStore(dataStoreFactory);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void bindSubject() {
|
||||
ThreadContext.bind(subject);
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDownSubject() {
|
||||
ThreadContext.unbindSubject();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldThrowAuthorizationExceptionOnAdd() throws IOException {
|
||||
doThrow(AuthorizationException.class).when(subject).checkPermission("user:modify:zaphod");
|
||||
String rawKey = GPGTestHelper.readKey("single.asc");
|
||||
|
||||
assertThrows(AuthorizationException.class, () -> keyStore.add("zaphods key", "zaphod", rawKey));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldOnlyStorePublicKeys() throws IOException {
|
||||
String rawKey = GPGTestHelper.readKey("single.asc").replace("PUBLIC", "PRIVATE");
|
||||
|
||||
assertThrows(NotPublicKeyException.class, () -> keyStore.add("SCM Package Key", "trillian", rawKey));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -48,7 +90,7 @@ class PublicKeyStoreTest {
|
||||
String rawKey = GPGTestHelper.readKey("single.asc");
|
||||
Instant now = Instant.now();
|
||||
|
||||
RawGpgKey key = keyStore.add("SCM Package Key", rawKey);
|
||||
RawGpgKey key = keyStore.add("SCM Package Key", "trillian", rawKey);
|
||||
assertThat(key.getId()).isEqualTo("0x975922F193B07D6E");
|
||||
assertThat(key.getDisplayName()).isEqualTo("SCM Package Key");
|
||||
assertThat(key.getOwner()).isEqualTo("trillian");
|
||||
@@ -59,9 +101,44 @@ class PublicKeyStoreTest {
|
||||
@Test
|
||||
void shouldFindStoredKeyById() throws IOException {
|
||||
String rawKey = GPGTestHelper.readKey("single.asc");
|
||||
keyStore.add("SCM Package Key", rawKey);
|
||||
keyStore.add("SCM Package Key", "trillian", rawKey);
|
||||
Optional<RawGpgKey> key = keyStore.findById("0x975922F193B07D6E");
|
||||
assertThat(key).isPresent();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDeleteKey() throws IOException {
|
||||
String rawKey = GPGTestHelper.readKey("single.asc");
|
||||
keyStore.add("SCM Package Key", "trillian", rawKey);
|
||||
Optional<RawGpgKey> key = keyStore.findById("0x975922F193B07D6E");
|
||||
|
||||
assertThat(key).isPresent();
|
||||
|
||||
keyStore.delete("0x975922F193B07D6E");
|
||||
key = keyStore.findById("0x975922F193B07D6E");
|
||||
|
||||
assertThat(key).isNotPresent();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldReturnEmptyListIfNoKeysAvailable() {
|
||||
List<RawGpgKey> keys = keyStore.findByUsername("zaphod");
|
||||
|
||||
assertThat(keys).isEmpty();
|
||||
assertThat(keys).isInstanceOf(List.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindAllKeysForUser() throws IOException {
|
||||
String singleKey = GPGTestHelper.readKey("single.asc");
|
||||
keyStore.add("SCM Single Key", "trillian", singleKey);
|
||||
|
||||
String multiKey = GPGTestHelper.readKey("subkeys.asc");
|
||||
keyStore.add("SCM Multi Key", "trillian", multiKey);
|
||||
|
||||
List<RawGpgKey> keys = keyStore.findByUsername("trillian");
|
||||
|
||||
assertThat(keys.size()).isEqualTo(2);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user