From 77046bb6fd0c7eecadb6657a74bc6f9c600b83fa Mon Sep 17 00:00:00 2001 From: Eduard Heimbuch Date: Tue, 3 May 2022 16:06:38 +0200 Subject: [PATCH] Throw error if repo is created with not supported type (#2019) Throw error if repository is created with not supported type. Currently we only allow git, svn or hg all lowercase. --- gradle/changelog/invalid_repo_type.yaml | 2 + .../RepositoryTypeConstraintValidator.java | 2 +- .../repository/DefaultRepositoryManager.java | 9 ++++ .../InvalidRepositoryTypeException.java | 48 +++++++++++++++++++ .../main/resources/locales/de/plugins.json | 4 ++ .../main/resources/locales/en/plugins.json | 4 ++ .../DefaultRepositoryManagerTest.java | 6 +++ 7 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 gradle/changelog/invalid_repo_type.yaml create mode 100644 scm-webapp/src/main/java/sonia/scm/repository/InvalidRepositoryTypeException.java diff --git a/gradle/changelog/invalid_repo_type.yaml b/gradle/changelog/invalid_repo_type.yaml new file mode 100644 index 0000000000..d179910eac --- /dev/null +++ b/gradle/changelog/invalid_repo_type.yaml @@ -0,0 +1,2 @@ +- type: fixed + description: Validate repository types on creation ([#2019](https://github.com/scm-manager/scm-manager/pull/2019)) diff --git a/scm-core/src/main/java/sonia/scm/repository/RepositoryTypeConstraintValidator.java b/scm-core/src/main/java/sonia/scm/repository/RepositoryTypeConstraintValidator.java index 4b7f1d4ded..735c0f1ae1 100644 --- a/scm-core/src/main/java/sonia/scm/repository/RepositoryTypeConstraintValidator.java +++ b/scm-core/src/main/java/sonia/scm/repository/RepositoryTypeConstraintValidator.java @@ -61,7 +61,7 @@ public class RepositoryTypeConstraintValidator implements ConstraintValidator t.getName().equalsIgnoreCase(type)); + .stream().anyMatch(t -> t.getName().equals(type)); } private String createMessage(ConstraintValidatorContext context) { diff --git a/scm-webapp/src/main/java/sonia/scm/repository/DefaultRepositoryManager.java b/scm-webapp/src/main/java/sonia/scm/repository/DefaultRepositoryManager.java index 0b5c638cdd..2736094ab1 100644 --- a/scm-webapp/src/main/java/sonia/scm/repository/DefaultRepositoryManager.java +++ b/scm-webapp/src/main/java/sonia/scm/repository/DefaultRepositoryManager.java @@ -148,6 +148,9 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager { } public Repository create(Repository repository, Consumer afterCreation, boolean initRepository) { + if (!isSupportedType(repository.getType())) { + throw InvalidRepositoryTypeException.create(repository); + } repository.setId(keyGenerator.createKey()); repository.setNamespace(namespaceStrategyProvider.get().createNamespace(repository)); @@ -181,6 +184,11 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager { ); } + private boolean isSupportedType(String type) { + return getConfiguredTypes() + .stream().anyMatch(t -> t.getName().equals(type)); + } + private void invalidateRepositoryPermissions() { ScmEventBus.getInstance().post(AuthorizationChangedEvent.createForEveryUser()); } @@ -209,6 +217,7 @@ public class DefaultRepositoryManager extends AbstractRepositoryManager { @Override public void init(SCMContextProvider context) { + // Do nothing } @Override diff --git a/scm-webapp/src/main/java/sonia/scm/repository/InvalidRepositoryTypeException.java b/scm-webapp/src/main/java/sonia/scm/repository/InvalidRepositoryTypeException.java new file mode 100644 index 0000000000..853a0d0855 --- /dev/null +++ b/scm-webapp/src/main/java/sonia/scm/repository/InvalidRepositoryTypeException.java @@ -0,0 +1,48 @@ +/* + * 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.repository; + +import sonia.scm.ContextEntry; +import sonia.scm.ExceptionWithContext; + +import java.util.List; + +public class InvalidRepositoryTypeException extends ExceptionWithContext { + + private static final String CODE = "8OT4gBVvp1"; + + public static InvalidRepositoryTypeException create(Repository repository) { + return new InvalidRepositoryTypeException(ContextEntry.ContextBuilder.entity(repository).build()); + } + + private InvalidRepositoryTypeException(List context) { + super(context, "The repository type is not supported"); + } + + @Override + public String getCode() { + return CODE; + } +} diff --git a/scm-webapp/src/main/resources/locales/de/plugins.json b/scm-webapp/src/main/resources/locales/de/plugins.json index c58025e75c..4a2b774123 100644 --- a/scm-webapp/src/main/resources/locales/de/plugins.json +++ b/scm-webapp/src/main/resources/locales/de/plugins.json @@ -398,6 +398,10 @@ "52SqQBdpO1": { "displayName": "Fehlender params Parameter", "description": "Die Antwort der Authentifizierung enthält keinen params Parameter." + }, + "8OT4gBVvp1": { + "displayName": "Repository Typ ungültig", + "description": "Dieser Repository Typ wird nicht unterstützt." } }, "healthCheckFailures": { diff --git a/scm-webapp/src/main/resources/locales/en/plugins.json b/scm-webapp/src/main/resources/locales/en/plugins.json index e91032d2b4..127480c183 100644 --- a/scm-webapp/src/main/resources/locales/en/plugins.json +++ b/scm-webapp/src/main/resources/locales/en/plugins.json @@ -406,6 +406,10 @@ "52SqQBdpO1": { "displayName": "Params missing", "description": "The parameter params is missing." + }, + "8OT4gBVvp1": { + "displayName": "Repository type invalid", + "description": "The repository type is not supported." } }, "healthChecksFailures": { diff --git a/scm-webapp/src/test/java/sonia/scm/repository/DefaultRepositoryManagerTest.java b/scm-webapp/src/test/java/sonia/scm/repository/DefaultRepositoryManagerTest.java index ba684b6733..a34e02b9f7 100644 --- a/scm-webapp/src/test/java/sonia/scm/repository/DefaultRepositoryManagerTest.java +++ b/scm-webapp/src/test/java/sonia/scm/repository/DefaultRepositoryManagerTest.java @@ -136,6 +136,12 @@ public class DefaultRepositoryManagerTest extends ManagerTestBase { assertRepositoriesEquals(dbRepo, heartOfGold); } + @Test + public void shouldThrowInvalidTypeExceptionOnCreate() { + Repository testRepo = RepositoryTestData.create42Puzzle("test_type"); + assertThrows(InvalidRepositoryTypeException.class, () -> manager.create(testRepo)); + } + @SubjectAware( username = "unpriv" )