Archive repository (#1477)

This adds a flag "archived" to repositories. Repositories marked with this can no longer be modified in any way. To do this, we switch to a new version of Shiro Static Permissions (sdorra/shiro-static-permissions#4) and specify a permission guard to check for every permission request, whether the repository in question is archived or not. Further we implement checks in stores and other activies so that no writing request may be executed by mistake.

Co-authored-by: Eduard Heimbuch <eduard.heimbuch@cloudogu.com>
This commit is contained in:
René Pfeuffer
2020-12-16 10:58:29 +01:00
committed by GitHub
parent b167d90fea
commit 8e3b0e4145
77 changed files with 2066 additions and 438 deletions

View File

@@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm;
//~--- non-JDK imports --------------------------------------------------------
@@ -32,11 +32,12 @@ import org.apache.shiro.subject.Subject;
import org.apache.shiro.subject.support.SubjectThreadState;
import org.apache.shiro.util.LifecycleUtils;
import org.apache.shiro.util.ThreadState;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import sonia.scm.io.DefaultFileSystem;
import sonia.scm.repository.InitialRepositoryLocationResolver;
import sonia.scm.repository.RepositoryDAO;
@@ -44,17 +45,14 @@ import sonia.scm.repository.RepositoryLocationResolver;
import sonia.scm.util.IOUtil;
import sonia.scm.util.MockUtil;
import static org.junit.Assert.*;
import static org.mockito.Mockito.mock;
//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import java.util.logging.Logger;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
/**
*
* @author Sebastian Sdorra
@@ -73,6 +71,7 @@ public class AbstractTestBase
protected RepositoryDAO repositoryDAO = mock(RepositoryDAO.class);
protected RepositoryLocationResolver repositoryLocationResolver;
@BeforeEach
@Before
public void setUpTest() throws Exception
{
@@ -90,6 +89,7 @@ public class AbstractTestBase
* Method description
*
*/
@AfterAll
@AfterClass
public static void tearDownShiro()
{
@@ -162,6 +162,7 @@ public class AbstractTestBase
*
* @throws Exception
*/
@AfterEach
@After
public void tearDownTest() throws Exception
{

View File

@@ -1,264 +0,0 @@
/*
* 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.store;
//~--- non-JDK imports --------------------------------------------------------
import com.google.common.io.ByteStreams;
import org.junit.Before;
import org.junit.Test;
import sonia.scm.AbstractTestBase;
import sonia.scm.repository.RepositoryTestData;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
//~--- JDK imports ------------------------------------------------------------
/**
*
* @author Sebastian Sdorra
*/
public abstract class BlobStoreTestBase extends AbstractTestBase
{
protected abstract BlobStoreFactory createBlobStoreFactory();
/**
* Method description
*
*/
@Before
public void createBlobStore()
{
store = createBlobStoreFactory()
.withName("test")
.forRepository(RepositoryTestData.createHeartOfGold())
.build();
store.clear();
}
/**
* Method description
*
*/
@Test
public void testClear()
{
store.create("1");
store.create("2");
store.create("3");
assertNotNull(store.get("1"));
assertNotNull(store.get("2"));
assertNotNull(store.get("3"));
store.clear();
assertNull(store.get("1"));
assertNull(store.get("2"));
assertNull(store.get("3"));
}
/**
* Method description
*
*
* @throws IOException
*/
@Test
public void testContent() throws IOException
{
Blob blob = store.create();
write(blob, "Hello");
assertEquals("Hello", read(blob));
blob = store.get(blob.getId());
assertEquals("Hello", read(blob));
write(blob, "Other Text");
assertEquals("Other Text", read(blob));
blob = store.get(blob.getId());
assertEquals("Other Text", read(blob));
}
/**
* Method description
*
*/
@Test(expected = EntryAlreadyExistsStoreException.class)
public void testCreateAlreadyExistingEntry()
{
assertNotNull(store.create("1"));
store.create("1");
}
/**
* Method description
*
*/
@Test
public void testCreateWithId()
{
Blob blob = store.create("1");
assertNotNull(blob);
blob = store.get("1");
assertNotNull(blob);
}
/**
* Method description
*
*/
@Test
public void testCreateWithoutId()
{
Blob blob = store.create();
assertNotNull(blob);
String id = blob.getId();
assertNotNull(id);
blob = store.get(id);
assertNotNull(blob);
}
/**
* Method description
*
*/
@Test
public void testGet()
{
Blob blob = store.get("1");
assertNull(blob);
blob = store.create("1");
assertNotNull(blob);
blob = store.get("1");
assertNotNull(blob);
}
/**
* Method description
*
*/
@Test
public void testGetAll()
{
store.create("1");
store.create("2");
store.create("3");
List<Blob> all = store.getAll();
assertNotNull(all);
assertFalse(all.isEmpty());
assertEquals(3, all.size());
boolean c1 = false;
boolean c2 = false;
boolean c3 = false;
for (Blob b : all)
{
if ("1".equals(b.getId()))
{
c1 = true;
}
else if ("2".equals(b.getId()))
{
c2 = true;
}
else if ("3".equals(b.getId()))
{
c3 = true;
}
}
assertTrue(c1);
assertTrue(c2);
assertTrue(c3);
}
/**
* Method description
*
*
* @param blob
*
* @return
*
* @throws IOException
*/
private String read(Blob blob) throws IOException
{
InputStream input = blob.getInputStream();
byte[] bytes = ByteStreams.toByteArray(input);
input.close();
return new String(bytes);
}
/**
* Method description
*
*
* @param blob
* @param content
*
* @throws IOException
*/
private void write(Blob blob, String content) throws IOException
{
OutputStream output = blob.getOutputStream();
output.write(content.getBytes());
output.close();
blob.commit();
}
//~--- fields ---------------------------------------------------------------
/** Field description */
private BlobStore store;
}