-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented template assignment handling for projects #3522
- Loading branch information
Showing
53 changed files
with
1,831 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
...in/java/com/mercedesbenz/sechub/domain/administration/project/ProjectTemplateService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// SPDX-License-Identifier: MIT | ||
package com.mercedesbenz.sechub.domain.administration.project; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import com.mercedesbenz.sechub.sharedkernel.RoleConstants; | ||
import com.mercedesbenz.sechub.sharedkernel.Step; | ||
import com.mercedesbenz.sechub.sharedkernel.encryption.SecHubProjectTemplates; | ||
import com.mercedesbenz.sechub.sharedkernel.encryption.SecHubProjectToTemplate; | ||
import com.mercedesbenz.sechub.sharedkernel.error.NotAcceptableException; | ||
import com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessage; | ||
import com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageService; | ||
import com.mercedesbenz.sechub.sharedkernel.messaging.DomainMessageSynchronousResult; | ||
import com.mercedesbenz.sechub.sharedkernel.messaging.IsSendingSyncMessage; | ||
import com.mercedesbenz.sechub.sharedkernel.messaging.MessageDataKeys; | ||
import com.mercedesbenz.sechub.sharedkernel.messaging.MessageID; | ||
import com.mercedesbenz.sechub.sharedkernel.usecases.admin.config.UseCaseAdminAssignsTemplateToProject; | ||
import com.mercedesbenz.sechub.sharedkernel.usecases.admin.config.UseCaseAdminUnassignsTemplateFromProject; | ||
import com.mercedesbenz.sechub.sharedkernel.validation.UserInputAssertion; | ||
|
||
import jakarta.annotation.security.RolesAllowed; | ||
|
||
@Service | ||
@RolesAllowed(RoleConstants.ROLE_SUPERADMIN) | ||
public class ProjectTemplateService { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(ProjectTemplateService.class); | ||
|
||
@Autowired | ||
DomainMessageService eventBus; | ||
|
||
@Autowired | ||
ProjectRepository projectRepository; | ||
|
||
@Autowired | ||
ProjectTransactionService projectTansactionService; | ||
|
||
@Autowired | ||
UserInputAssertion assertion; | ||
|
||
/* @formatter:off */ | ||
@UseCaseAdminAssignsTemplateToProject( | ||
@Step( | ||
number = 2, | ||
name = "service assigns template to project", | ||
description = "The service will request the template assignment in domain 'scan' via synchronous event and updates mapping in domain 'administration' afterwards")) | ||
/* @formatter:on */ | ||
public void assignTemplateToProject(String templateId, String projectId) { | ||
assertion.assertIsValidTemplateId(templateId); | ||
assertion.assertIsValidProjectId(projectId); | ||
|
||
Project project = projectRepository.findOrFailProject(projectId); | ||
Set<String> templates = project.getTemplates(); | ||
LOG.debug("Start assigning template '{}' to project '{}'. Formerly assgined templates : {}", templateId, projectId, templates); | ||
|
||
SecHubProjectTemplates result = sendAssignRequestAndFetchResult(templateId, projectId); | ||
List<String> newTemplates = result.getTemplateIds(); | ||
templates.clear(); | ||
templates.addAll(newTemplates); | ||
|
||
projectTansactionService.saveInOwnTransaction(project); | ||
LOG.info("Assigned template '{}' to project '{}'", templateId, projectId); | ||
|
||
LOG.debug("Project '{}' has now following templates: {}", templateId, projectId, templates); | ||
|
||
} | ||
|
||
/* @formatter:off */ | ||
@UseCaseAdminUnassignsTemplateFromProject( | ||
@Step( | ||
number = 2, | ||
name = "service unassigns template from project", | ||
description = "The service will request the template unassignment in domain 'scan' via synchronous event and updates mapping in domain 'administration' afterwards")) | ||
/* @formatter:on */ | ||
public void unassignTemplateFromProject(String templateId, String projectId) { | ||
assertion.assertIsValidTemplateId(templateId); | ||
assertion.assertIsValidProjectId(projectId); | ||
|
||
Project project = projectRepository.findOrFailProject(projectId); | ||
Set<String> templates = project.getTemplates(); | ||
LOG.debug("Start unassigning template '{}' from project '{}'. Formerly assgined templates : {}", templateId, projectId, templates); | ||
|
||
SecHubProjectTemplates result = sendUnassignRequestAndFetchResult(templateId, projectId); | ||
List<String> newTemplates = result.getTemplateIds(); | ||
templates.clear(); | ||
templates.addAll(newTemplates); | ||
|
||
projectTansactionService.saveInOwnTransaction(project); | ||
LOG.info("Unassigned template '{}' from project '{}'", templateId, projectId); | ||
|
||
LOG.debug("Project '{}' has now following templates: {}", templateId, projectId, templates); | ||
|
||
} | ||
|
||
@IsSendingSyncMessage(MessageID.REQUEST_ASSIGN_TEMPLATE_TO_PROJECT) | ||
private SecHubProjectTemplates sendAssignRequestAndFetchResult(String templateId, String projectId) { | ||
|
||
DomainMessage message = new DomainMessage(MessageID.REQUEST_ASSIGN_TEMPLATE_TO_PROJECT); | ||
SecHubProjectToTemplate mapping = new SecHubProjectToTemplate(); | ||
mapping.setProjectId(projectId); | ||
mapping.setTemplateId(templateId); | ||
message.set(MessageDataKeys.PROJECT_TO_TEMPLATE, mapping); | ||
|
||
DomainMessageSynchronousResult result = eventBus.sendSynchron(message); | ||
if (result.hasFailed()) { | ||
throw new NotAcceptableException("Was not able to assign template to project.\nReason:" + result.getErrorMessage()); | ||
} | ||
|
||
MessageID messageID = result.getMessageId(); | ||
if (!(MessageID.RESULT_ASSIGN_TEMPLATE_TO_PROJECT.equals(messageID))) { | ||
throw new IllegalStateException("Result message id not supported: " + messageID); | ||
} | ||
|
||
return result.get(MessageDataKeys.PROJECT_TEMPLATES); | ||
|
||
} | ||
|
||
@IsSendingSyncMessage(MessageID.REQUEST_UNASSIGN_TEMPLATE_FROM_PROJECT) | ||
private SecHubProjectTemplates sendUnassignRequestAndFetchResult(String templateId, String projectId) { | ||
|
||
DomainMessage message = new DomainMessage(MessageID.REQUEST_UNASSIGN_TEMPLATE_FROM_PROJECT); | ||
SecHubProjectToTemplate mapping = new SecHubProjectToTemplate(); | ||
mapping.setProjectId(projectId); | ||
mapping.setTemplateId(templateId); | ||
message.set(MessageDataKeys.PROJECT_TO_TEMPLATE, mapping); | ||
|
||
DomainMessageSynchronousResult result = eventBus.sendSynchron(message); | ||
if (result.hasFailed()) { | ||
throw new NotAcceptableException("Was not able to assign template to project.\nReason:" + result.getErrorMessage()); | ||
} | ||
|
||
MessageID messageID = result.getMessageId(); | ||
if (!(MessageID.RESULT_UNASSIGN_TEMPLATE_FROM_PROJECT.equals(messageID))) { | ||
throw new IllegalStateException("Result message id not supported: " + messageID); | ||
} | ||
|
||
return result.get(MessageDataKeys.PROJECT_TEMPLATES); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...a/com/mercedesbenz/sechub/domain/administration/project/ProjectDetailInformationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.mercedesbenz.sechub.domain.administration.project; | ||
|
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.mockito.Mockito.*; | ||
|
||
import java.net.URI; | ||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.mercedesbenz.sechub.domain.administration.user.User; | ||
import com.mercedesbenz.sechub.sharedkernel.project.ProjectAccessLevel; | ||
|
||
class ProjectDetailInformationTest { | ||
|
||
@Test | ||
void constructor_stores_data_from_project_into_fields() { | ||
/* prepare */ | ||
Project project = mock(Project.class); | ||
|
||
User owner = mock(User.class); | ||
when(owner.getName()).thenReturn("owner1"); | ||
|
||
User user1 = mock(User.class); | ||
when(user1.getName()).thenReturn("user1"); | ||
|
||
User user2 = mock(User.class); | ||
when(user2.getName()).thenReturn("user2"); | ||
|
||
String projectId = "id1"; | ||
ProjectMetaDataEntity metaDataEntity = new ProjectMetaDataEntity(projectId, "key1", "value1"); | ||
ProjectAccessLevel accessLevel = ProjectAccessLevel.FULL; | ||
|
||
when(project.getWhiteList()).thenReturn(Set.of(URI.create("https://example.com"))); | ||
when(project.getAccessLevel()).thenReturn(accessLevel); | ||
when(project.getId()).thenReturn(projectId); | ||
when(project.getOwner()).thenReturn(owner); | ||
when(project.getMetaData()).thenReturn(Set.of(metaDataEntity)); | ||
when(project.getUsers()).thenReturn(Set.of(user1, user2)); | ||
when(project.getTemplates()).thenReturn(Set.of("template1", "template2")); | ||
|
||
/* execute */ | ||
ProjectDetailInformation toTest = new ProjectDetailInformation(project); | ||
|
||
/* test */ | ||
assertThat(toTest.getProjectId()).isEqualTo(projectId); | ||
assertThat(toTest.getAccessLevel()).isEqualTo(accessLevel.getId()); | ||
assertThat(toTest.getOwner()).isEqualTo("owner1"); | ||
assertThat(toTest.getUsers()).contains("user1", "user2"); | ||
assertThat(toTest.getWhiteList()).contains("https://example.com"); | ||
assertThat(toTest.getTemplates()).contains("template1", "template2"); | ||
assertThat(toTest.getMetaData()).containsEntry("key1", "value1"); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.