Skip to content

Commit

Permalink
Implemented local sechub configuration parts #2092
Browse files Browse the repository at this point in the history
- projects are created
- profiles are created
- dry run possible
  • Loading branch information
de-jcup committed Apr 21, 2023
1 parent 8211729 commit c6c6463
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 66 deletions.
5 changes: 2 additions & 3 deletions sechub-api-java/build.gradle
Expand Up @@ -16,9 +16,8 @@ dependencies {

testImplementation project(':sechub-testframework')
testImplementation spring_boot_dependency.junit_jupiter

testImplementation project(':sechub-doc') // we use docgen inspection parts

testImplementation spring_boot_dependency.junit_jupiter
testImplementation library.apache_commons_io
}

/*
Expand Down
32 changes: 32 additions & 0 deletions sechub-api-java/fullRegenerateOpenAPIClassFiles.sh
@@ -0,0 +1,32 @@
#!/bin/bash

#
# You can call this script to update all files inside gen folder
# This will NOT regenerate wrapper classes.
# But it will regenerate the open api file (rest doc tests are executed as well) and
# generated the OpenApi gen parts completely.
#
# If there are changes on wrapper side necessary you have to call SystemTestManualLocalServerIntTest
# again with necessary system properties defined!
set -e


echo "-----------------------------------"
echo "Regenerate OpenAPI class files"
echo "-----------------------------------"
echo "Attention:"
echo "**********"
echo "For developers on local machines: If you change RestDoc tests to change OpenAPi"
echo "generation, you have to"
echo ""
echo "- start the corresponding {name}RestDocTest to provide meta data"
echo "- execute this script"
echo ""
echo "The script will do"
echo "- apply spotless"
echo "- generate open api file"
echo "- generate sechub java api"

cd ..
./gradlew spotlessApply generateOpenapi
./gradlew :sechub-api-java:build -Dsechub.build.stage=all
7 changes: 0 additions & 7 deletions sechub-api-java/regenerateOpenAPIClassFiles.sh

This file was deleted.

Expand Up @@ -19,6 +19,7 @@
import com.mercedesbenz.sechub.api.internal.gen.model.OpenApiExecutionProfileFetchConfigurationsInner;
import com.mercedesbenz.sechub.api.internal.gen.model.OpenApiExecutionProfileUpdate;
import com.mercedesbenz.sechub.api.internal.gen.model.OpenApiExecutionProfileUpdateConfigurationsInner;
import com.mercedesbenz.sechub.api.internal.gen.model.OpenApiProjectDetails;
import com.mercedesbenz.sechub.commons.core.FailableRunnable;
import com.mercedesbenz.sechub.commons.core.security.CryptoAccess;
import com.mercedesbenz.sechub.commons.model.JsonMapperFactory;
Expand Down Expand Up @@ -121,11 +122,11 @@ public List<OpenUserSignup> fetchAllOpenSignups() throws SecHubClientException {
return runOrFail(() -> OpenUserSignup.fromDelegates(adminApi.adminListsOpenUserSignups()), "Cannot fetch open signups");
}

public List<String> fetchAllProjectNames() throws SecHubClientException {
public List<String> fetchAllProjectIds() throws SecHubClientException {
return runOrFail(() -> adminApi.adminListsAllProjects(), "Cannot fetch all project names");
}

public List<String> fetchAllUserNames() throws SecHubClientException {
public List<String> fetchAllUserIds() throws SecHubClientException {
return runOrFail(() -> adminApi.adminListsAllUsers(), "Cannot fetch all user names");
}

Expand Down Expand Up @@ -186,6 +187,36 @@ public void addExecutorToProfile(UUID uuidOfExecutorConfigToAdd, String profileI
}, "Cannot add executor config: " + uuidOfExecutorConfigToAdd + " to profile:" + profileId);
}

public boolean isProjectExisting(String projectId) throws SecHubClientException {
return runOrFail(() -> adminApi.adminListsAllProjects().contains(projectId),

"Cannot check if project '" + projectId + "' exists!");
}

public void assignUserToProject(String userId, String projectId) throws SecHubClientException {
runOrFail(() -> adminApi.adminAssignsUserToProject(projectId, userId),

"Was not able to assign user '" + userId + "' to project '" + projectId + "'");

}

public void unassignUserFromProject(String userId, String projectId) throws SecHubClientException {
runOrFail(() -> adminApi.adminUnassignsUserFromProject(projectId, userId),

"Was not able to unassign user '" + userId + "' from project '" + projectId + "'");

}

public boolean isUserAssignedToProject(String userId, String projectId) throws SecHubClientException {
return runOrFail(() -> {
/* not very smart... but works : */
OpenApiProjectDetails details = adminApi.adminShowsProjectDetails(projectId);
List<String> userIds = details.getUsers();
return userIds.contains(userId);
}, "");
}


private OpenApiExecutionProfileUpdate fetchProfileAsUpdateObject(String profileId) throws ApiException {
OpenApiExecutionProfileUpdate update = new OpenApiExecutionProfileUpdate();

Expand All @@ -206,17 +237,5 @@ private OpenApiExecutionProfileUpdate fetchProfileAsUpdateObject(String profileI
return update;
}

public boolean isProjectExisting(String projectName) throws SecHubClientException {
return runOrFail(()->adminApi.adminListsAllProjects().contains(projectName),"Cannot check if project exists:"+projectName);
}

public void assignUserToProject(String userId, String projectId) throws SecHubClientException {
runOrFail(()->adminApi.adminAssignsUserToProject(userId, projectId),"Was not able to assign user "+userId+" to project "+projectId);
}

public boolean isUserAssignedToProject() {
return false;
}



}
Expand Up @@ -13,15 +13,39 @@
*
* Equality is checked by
* <code>SystemTestFallbacksAndDocFallbacksTest.java</code> If it fails, please
* copy content system test DefaultFallback at this location (comments can be customized)
* copy content system test DefaultFallback at this location (class comments are
* ignored means can be custom)
*
* @return
*/
public enum AdoptedSystemTestDefaultFallbacks {

FALLBACK_PROJECT_NAME("Project name", "default-test-project"),

FALLBACK_PROFILE_ID("Profile id", "default-test-profile"),;
FALLBACK_PROFILE_ID("Profile id", "default-test-profile"),

/** Same like default in /sechub-solution/env-sechub */
FALLBACK_SECHUB_LOCAL_URL("(Local) SecHub url", "https://localhost:8443"),

/** Same like default in /sechub-solution/env-sechub */
FALLBACK_SECHUB_ADMIN_USER("(Local) SecHub admin user", "admin"),

/** Same like default in /sechub-solution/env-sechub */
FALLBACK_SECHUB_ADMIN_TOKEN("(Local) SecHub admin token", "myTop$ecret!"),

/** Same like default in /sechub-pds-solutions/shared/environment/env-base */
FALLBACK_PDS_ADMIN_USER("PDS admin user", "admin"),

/** Same like default in /sechub-pds-solutions/shared/environment/env-base */
FALLBACK_PDS_ADMIN_TOKEN("PDS admin token", "pds-apitoken"),

/** Same like default in /sechub-pds-solutions/shared/environment/env-base */
FALLBACK_PDS_TECH_USER("PDS tech user", "techuser"),

/** Same like default in /sechub-pds-solutions/shared/environment/env-base */
FALLBACK_PDS_TECH_TOKEN("PDS tech user token", "pds-apitoken"),

;

private String scope;
private String value;
Expand Down
Expand Up @@ -46,13 +46,17 @@ private String javaClassToPath(String _package, String _class) {
}

private String withoutCommentsOrEmptyLines(String code) {
String regexpNoComments = "\\/\\*(.|\\n)*\\*\\/";
String noComments = code.replaceAll(regexpNoComments, "");

String[] lines = noComments.split("\n");
String[] lines = code.split("\n");
StringBuilder sb = new StringBuilder();
for (String line : lines) {
if (!line.isBlank()) {
String trimmed = line.trim();
boolean isComment = trimmed.startsWith("*");
isComment = isComment || trimmed.startsWith("/*");

if (isComment) {
continue;
}
sb.append(line);
sb.append("\n");
}
Expand Down
Expand Up @@ -105,15 +105,15 @@ private void createProjectAndCheckFoundInList() throws SecHubClientException {

logSuccess("Project " + projectName + " created");

List<String> projects = client.fetchAllProjectNames();
List<String> projects = client.fetchAllProjectIds();
assumeEquals(true, projects.contains(projectName), "Project " + projectName + " was found in list");
}

private void acceptUserAndCheckListedAsUser() throws SecHubClientException {
client.acceptOpenSignup(userName);
waitMilliseconds(300);

List<String> usersList = client.fetchAllUserNames();
List<String> usersList = client.fetchAllUserIds();
logSuccess("List of users has entries: " + usersList.size());

assumeEquals(true, usersList.contains(userName), "Accepted user is found in user list after signup");
Expand Down
Expand Up @@ -10,6 +10,7 @@ public class SystemTestParameters {
private boolean localRun = true;// default always local

private boolean dryRun;
private String pdsServerconfigFileName="pds-config.json";

public SystemTestConfiguration getConfiguration() {
return configuration;
Expand All @@ -31,6 +32,10 @@ public boolean isDryRun() {
return dryRun;
}

public String getPdsServerconfigFileName() {
return pdsServerconfigFileName;
}

public static SystemTestParametersBuilder builder() {
return new SystemTestParametersBuilder();
}
Expand All @@ -51,6 +56,11 @@ public SystemTestParametersBuilder pdsSolutionPath(String pathToPdsSolution) {
parameter.pathToPdsSolution = pathToPdsSolution;
return this;
}

public SystemTestParametersBuilder pdsServerConfigFileName(String pdsServerconfigFileName) {
parameter.pdsServerconfigFileName = pdsServerconfigFileName;
return this;
}

public SystemTestParametersBuilder workspacePath(String pathToWorkspace) {
parameter.pathToWorkspace = pathToWorkspace;
Expand Down
Expand Up @@ -281,6 +281,18 @@ public SolutionSetupBuilder waitForAVailable(boolean waitForAVailable) {
setup.setWaitForAvailable(waitForAVailable);
return this;
}


/**
* If define, the server config file location will not be calculated, but the defined
* part will be used.
* @param pathToPdsServerConfigFile
* @return
*/
public SolutionSetupBuilder pathToServerConfigFile(String pathToPdsServerConfigFile) {
setup.setPathToPdsServerConfigFile(pathToPdsServerConfigFile);
return this;
}

}
}
Expand Down
@@ -1,15 +1,21 @@
package com.mercedesbenz.sechub.systemtest.runtime;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mercedesbenz.sechub.api.ExecutionProfileCreate;
import com.mercedesbenz.sechub.api.Project;
import com.mercedesbenz.sechub.api.SecHubClient;
import com.mercedesbenz.sechub.api.SecHubClientException;
import com.mercedesbenz.sechub.systemtest.config.LocalSecHubDefinition;
import com.mercedesbenz.sechub.systemtest.config.LocalSetupDefinition;
import com.mercedesbenz.sechub.systemtest.config.ProjectDefinition;
import com.mercedesbenz.sechub.systemtest.config.SecHubConfigurationDefinition;
import com.mercedesbenz.sechub.systemtest.config.SecHubExecutorConfigDefinition;

public class SystemTestRuntimeProductConfigurator {

Expand All @@ -22,45 +28,83 @@ public void applyConfigurationWhenLocal(SystemTestRuntimeContext context) throws
if (!context.isLocalSecHubConfigured()) {
return;
}

LocalSetupDefinition localSetup = context.getLocalSetupOrFail();
LocalSecHubDefinition secHub = localSetup.getSecHub();

SecHubConfigurationDefinition config = secHub.getConfigure();

addProjects(context, config);
assignAdminAsUserToProjects(context, config);
addProjects(context);
assignAdminAsUserToProjects(context);
createExecutorsAndProfiles(context);

}

private void createExecutorsAndProfiles(SystemTestRuntimeContext context) throws SecHubClientException {
SecHubConfigurationDefinition config = context.getLocalSecHubConfigurationOrFail();

SecHubClient client = context.getLocalAdminSecHubClient();

List<SecHubExecutorConfigDefinition> executors = config.getExecutors();

for (SecHubExecutorConfigDefinition executorDefinition : executors) {
String profileId = executorDefinition.getProfile();
ExecutionProfileCreate profile = new ExecutionProfileCreate();
profile.setEnabled(true);
List<String> projectIdsForThisProfile = new ArrayList<>();
for (ProjectDefinition projectDefinition : config.getProjects().get()) {
if (projectDefinition.getProfiles().contains(profileId)) {
projectIdsForThisProfile.add(projectDefinition.getName());
}
}
profile.setProjectIds(projectIdsForThisProfile);
profile.setDescription("Generated by system test framework");

if (context.isDryRun()) {
LOG.info("Dry run: create profile '{}' is skipped", profileId);
continue;
}
client.createExecutionProfile(profileId, profile);
}

// the configuration contains no profile elements, but only executors
// to remember the executor config uuids for the profiles we use this map
Map<String, List<UUID>> profileToExecutorConfigUUIDS = new TreeMap<>();
/* FIXME Albert Tregnaghi, 2023-04-21:implement executors and map to profiles */
}
private void assignAdminAsUserToProjects(SystemTestRuntimeContext context, SecHubConfigurationDefinition config) throws SecHubClientException {

private void assignAdminAsUserToProjects(SystemTestRuntimeContext context) throws SecHubClientException {
SecHubConfigurationDefinition config = context.getLocalSecHubConfigurationOrFail();
if (config.getProjects().isEmpty()) {
return;
}
for (ProjectDefinition projectDefinition : config.getProjects().get()) {
String projectName = projectDefinition.getName();

SecHubClient client = context.getLocalAdminSecHubClient();

client.assignUserToProject(client.getUsername(), projectName);
if (! client.isUserAssignedToProject()) {

SecHubClient client = context.getLocalAdminSecHubClient();
String userId = client.getUsername();
for (ProjectDefinition projectDefinition : config.getProjects().get()) {
String projectid = projectDefinition.getName();
if (context.isDryRun()) {
LOG.info("Dry run: assign user '{}' to project '{}' is skipped", userId, projectid);
continue;
}
if (client.isUserAssignedToProject(userId, projectid)) {
LOG.warn("User '{}' is already assigned to project '{}' - skip assignment", userId, projectid);
} else {
client.assignUserToProject(userId, projectid);
}

}
}

private void addProjects(SystemTestRuntimeContext context, SecHubConfigurationDefinition config) throws SecHubClientException {

private void addProjects(SystemTestRuntimeContext context) throws SecHubClientException {
SecHubConfigurationDefinition config = context.getLocalSecHubConfigurationOrFail();
if (config.getProjects().isEmpty()) {
LOG.warn("No project defined - skip project configuration. Should only happen when only PDS is tested without SecHub.");
return;
}

for (ProjectDefinition projectDefinition : config.getProjects().get()) {
String projectName = projectDefinition.getName();

SecHubClient client = context.getLocalAdminSecHubClient();

if (context.isDryRun()) {
LOG.info("Dry run: create project :"+projectName+" is skipped");
LOG.info("Dry run: create project '{}' is skipped", projectName);
continue;
}
if (client.isProjectExisting(projectName)) {
Expand All @@ -72,7 +116,6 @@ private void addProjects(SystemTestRuntimeContext context, SecHubConfigurationDe
project.setDescription("Test project");
project.setName(projectName);
project.setOwner(client.getUsername());// we use the administrator as owner of the project


client.createProject(project);
}
Expand Down

0 comments on commit c6c6463

Please sign in to comment.