Skip to content

Commit

Permalink
Auto configuration most parts now done #2092
Browse files Browse the repository at this point in the history
- health check parts added
- auto prepare some parts added
- executors, profiles and projects are now configured
  completely (and it is possible to restart the setup,
  old data is automatically removed except old but now
  unused executor configurations)
- credentials are still missing for PDS solution
  • Loading branch information
de-jcup committed Apr 25, 2023
1 parent c6c6463 commit fcad7e9
Show file tree
Hide file tree
Showing 28 changed files with 789 additions and 276 deletions.
Expand Up @@ -86,4 +86,12 @@ com.mercedesbenz.sechub.api.internal.gen.model.OpenApiExecutorConfigurationSetup
return delegate;
}

public void addParameter(String key, String value) {
ExecutorConfigurationSetupJobParameter parameter = new ExecutorConfigurationSetupJobParameter();
parameter.setKey(key);
parameter.setValue(value);

getJobParameters().add(parameter);
}

}
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: MIT
package com.mercedesbenz.sechub.api;

import static java.util.Objects.*;

import java.net.URI;
import java.util.List;
import java.util.UUID;
Expand All @@ -10,15 +12,17 @@

import com.fasterxml.jackson.databind.json.JsonMapper;
import com.mercedesbenz.sechub.api.internal.ApiClientBuilder;
import com.mercedesbenz.sechub.api.internal.OpenApiSecHubClientConversionHelper;
import com.mercedesbenz.sechub.api.internal.WorkaroundAdminApi;
import com.mercedesbenz.sechub.api.internal.gen.AdminApi;
import com.mercedesbenz.sechub.api.internal.gen.AnonymousApi;
import com.mercedesbenz.sechub.api.internal.gen.invoker.ApiClient;
import com.mercedesbenz.sechub.api.internal.gen.invoker.ApiException;
import com.mercedesbenz.sechub.api.internal.gen.model.OpenApiExecutionProfileFetch;
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.OpenApiExecutorConfiguration;
import com.mercedesbenz.sechub.api.internal.gen.model.OpenApiExecutorConfigurationSetup;
import com.mercedesbenz.sechub.api.internal.gen.model.OpenApiProjectDetails;
import com.mercedesbenz.sechub.commons.core.FailableRunnable;
import com.mercedesbenz.sechub.commons.core.security.CryptoAccess;
Expand Down Expand Up @@ -46,6 +50,8 @@ public class SecHubClient {

private WorkaroundAdminApi workaroundAdminApi;

private OpenApiSecHubClientConversionHelper conversionHelper;

public SecHubClient(URI serverUri, String username, String apiToken) {
this(serverUri, username, apiToken, false);
}
Expand All @@ -62,6 +68,8 @@ public SecHubClient(URI serverUri, String username, String apiToken, boolean tru
anonymousApi = new AnonymousApi(getApiClient());
adminApi = new AdminApi(getApiClient());
workaroundAdminApi = new WorkaroundAdminApi(getApiClient());

conversionHelper = new OpenApiSecHubClientConversionHelper(adminApi);
}

private ApiClient getApiClient() {
Expand All @@ -88,18 +96,37 @@ public boolean isTrustAll() {
/* + ................Create.......................... + */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
public void createSignup(UserSignup signUp) throws SecHubClientException {
requireNonNull(signUp, "signUp may not be null!");

runOrFail(() -> anonymousApi.userSignup(signUp.getDelegate()), "User signup failed");
}

public void createProject(Project project) throws SecHubClientException {
requireNonNull(project, "project may not be null!");

runOrFail(() -> adminApi.adminCreatesProject(project.getDelegate()), "Cannot create project:" + project.getName());
}

public UUID createExecutorConfiguration(ExecutorConfiguration config) throws SecHubClientException {
return runOrFail(() -> workaroundAdminApi.adminCreatesExecutorConfiguration(config.getDelegate()), "Cannot accept open signups");
requireNonNull(config, "config may not be null!");

return runOrFail(() -> {
OpenApiExecutorConfiguration delegate = config.getDelegate();
OpenApiExecutorConfigurationSetup setup = delegate.getSetup();
/*
* necessary because two different lists - delegate has its own, we overwrite
* here
*/
setup.setJobParameters(ExecutorConfigurationSetupJobParameter.toDelegates(config.getSetup().getJobParameters()));
UUID result = workaroundAdminApi.adminCreatesExecutorConfiguration(delegate);
return result;
}, "Cannot create executor configuration");
}

public void createExecutionProfile(String profileName, ExecutionProfileCreate profile) throws SecHubClientException {
requireNonNull(profileName, "profileName may not be null!");
requireNonNull(profile, "profile may not be null!");

runOrFail(() -> adminApi.adminCreatesExecutionProfile(profileName, profile.getDelegate()), "Was not able to create profile:" + profileName);
}

Expand All @@ -115,6 +142,38 @@ public boolean checkIsServerAlive() throws SecHubClientException {
}
}

public boolean isProjectExisting(String projectId) throws SecHubClientException {
requireNonNull(projectId, "projectId may not be null!");
return runOrFail(() -> adminApi.adminListsAllProjects().contains(projectId),

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

public boolean isUserAssignedToProject(String userId, String projectId) throws SecHubClientException {
requireNonNull(userId, "userId may not be null!");
requireNonNull(projectId, "projectId may not be null!");

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

public boolean isExecutionProfileExisting(String profileId) throws SecHubClientException {
try {
OpenApiExecutionProfileFetch result = adminApi.adminFetchesExecutionProfile(profileId);
return result != null;
} catch (ApiException e) {
if (e.getCode() == 404) {
/* not found */
return false;
}
throw new SecHubClientException("Was not able check if profile " + profileId + " does exist.", e);
}
}

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* + ................Fetch........................... + */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
Expand All @@ -131,52 +190,40 @@ public List<String> fetchAllUserIds() throws SecHubClientException {
}

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* + ................Accept.......................... + */
/* + ................Assign/Unassign................. + */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
public void acceptOpenSignup(String signupUsername) throws SecHubClientException {
requireNonNull(signupUsername, "signupUsername may not be null!");

runOrFail(() -> adminApi.adminAcceptsSignup(signupUsername), "Cannot accept open signups");
}

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* + ................Helpers......................... + */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
public void assignUserToProject(String userId, String projectId) throws SecHubClientException {
requireNonNull(userId, "userId may not be null!");
requireNonNull(projectId, "projectId may not be null!");

private void runOrFail(FailableRunnable<ApiException> failable, String failureMessage) throws SecHubClientException {
try {
failable.runOrFail();
} catch (ApiException e) {
throw createClientException(failureMessage, e);
}
}
runOrFail(() -> adminApi.adminAssignsUserToProject(projectId, userId),

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

private SecHubClientException createClientException(String message, Exception cause) throws SecHubClientException {
return new SecHubClientException(message + " - " + cause.getMessage(), cause);
}

private <T> T runOrFail(Callable<T> callable, String failureMessage) throws SecHubClientException {
try {
return callable.call();
} catch (ApiException e) {
throw createClientException(failureMessage, e);
} catch (Exception e) {
if (e instanceof RuntimeException) {
RuntimeException re = (RuntimeException) e;
throw re;
}
throw new IllegalStateException("Unhandled exception - should not happen", e);
}
public void unassignUserFromProject(String userId, String projectId) throws SecHubClientException {
requireNonNull(userId, "userId may not be null!");
requireNonNull(projectId, "projectId may not be null!");

runOrFail(() -> adminApi.adminUnassignsUserFromProject(projectId, userId),

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

}

public void addExecutorToProfile(UUID uuidOfExecutorConfigToAdd, String profileId) throws SecHubClientException {
if (uuidOfExecutorConfigToAdd == null) {
throw new IllegalArgumentException("uuid may not be null!");
}
if (profileId == null) {
throw new IllegalArgumentException("profileId may not be null!");
}
public void addExecutorConfigurationToProfile(UUID uuidOfExecutorConfigToAdd, String profileId) throws SecHubClientException {
requireNonNull(uuidOfExecutorConfigToAdd, "uuidOfExecutorConfigToAdd may not be null!");
requireNonNull(profileId, "profileId may not be null!");

runOrFail(() -> {
OpenApiExecutionProfileUpdate update = fetchProfileAsUpdateObject(profileId);
OpenApiExecutionProfileUpdate update = conversionHelper.fetchProfileAndConvertToUpdateObject(profileId);

OpenApiExecutionProfileUpdateConfigurationsInner newItem = new OpenApiExecutionProfileUpdateConfigurationsInner();
newItem.setUuid(uuidOfExecutorConfigToAdd.toString());
Expand All @@ -187,55 +234,56 @@ 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),
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* + ................Delete.......................... + */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */

"Was not able to assign user '" + userId + "' to project '" + projectId + "'");
public void deleteProject(String projectId) throws SecHubClientException {
requireNonNull(projectId, "projectId may not be null!");

runOrFail(() -> adminApi.adminDeleteProject(projectId), "Was not able to delete 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 void deleteExecutionProfile(String profileId) throws SecHubClientException {
requireNonNull(profileId, "profileId may not be null!");

runOrFail(() -> adminApi.adminDeletesExecutionProfile(profileId), "Was not able to delete execution profile: " + profileId);
}

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);
}, "");
public void deleteExecutorConfiguration(UUID executorUUID) throws SecHubClientException {
requireNonNull(executorUUID, "executor uuid may not be null!");

runOrFail(() -> adminApi.adminDeletesExecutorConfiguration(executorUUID.toString()), "Was not able to delete executor configuration: " + executorUUID);
}


private OpenApiExecutionProfileUpdate fetchProfileAsUpdateObject(String profileId) throws ApiException {
OpenApiExecutionProfileUpdate update = new OpenApiExecutionProfileUpdate();
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* + ................Helpers......................... + */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */

OpenApiExecutionProfileFetch fetched = adminApi.adminFetchesExecutionProfile(profileId);
update.setDescription(fetched.getDescription());
update.setEnabled(fetched.getEnabled());
List<OpenApiExecutionProfileFetchConfigurationsInner> fetchedConfigurations = fetched.getConfigurations();
private void runOrFail(FailableRunnable<ApiException> failable, String failureMessage) throws SecHubClientException {
try {
failable.runOrFail();
} catch (ApiException e) {
throw createClientException(failureMessage, e);
}
}

for (OpenApiExecutionProfileFetchConfigurationsInner fetchedConfiguration : fetchedConfigurations) {
/* we only need the uuid on server side - everything else is ignored */
String uuid = fetchedConfiguration.getUuid();
private SecHubClientException createClientException(String message, Exception cause) throws SecHubClientException {
return new SecHubClientException(message + " - " + cause.getMessage(), cause);
}

/* add to update again */
OpenApiExecutionProfileUpdateConfigurationsInner existingItem = new OpenApiExecutionProfileUpdateConfigurationsInner();
existingItem.setUuid(uuid);
update.addConfigurationsItem(existingItem);
private <T> T runOrFail(Callable<T> callable, String failureMessage) throws SecHubClientException {
try {
return callable.call();
} catch (ApiException e) {
throw createClientException(failureMessage, e);
} catch (Exception e) {
if (e instanceof RuntimeException) {
RuntimeException re = (RuntimeException) e;
throw re;
}
throw new IllegalStateException("Unhandled exception - should not happen", e);
}
return update;
}


}
@@ -0,0 +1,40 @@
package com.mercedesbenz.sechub.api.internal;

import java.util.List;

import com.mercedesbenz.sechub.api.internal.gen.AdminApi;
import com.mercedesbenz.sechub.api.internal.gen.invoker.ApiException;
import com.mercedesbenz.sechub.api.internal.gen.model.OpenApiExecutionProfileFetch;
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;

public class OpenApiSecHubClientConversionHelper {

private AdminApi adminApi;

public OpenApiSecHubClientConversionHelper(AdminApi adminApi) {
this.adminApi = adminApi;
}

public OpenApiExecutionProfileUpdate fetchProfileAndConvertToUpdateObject(String profileId) throws ApiException {
OpenApiExecutionProfileUpdate update = new OpenApiExecutionProfileUpdate();

OpenApiExecutionProfileFetch fetched = adminApi.adminFetchesExecutionProfile(profileId);
update.setDescription(fetched.getDescription());
update.setEnabled(fetched.getEnabled());
List<OpenApiExecutionProfileFetchConfigurationsInner> fetchedConfigurations = fetched.getConfigurations();

for (OpenApiExecutionProfileFetchConfigurationsInner fetchedConfiguration : fetchedConfigurations) {
/* we only need the uuid on server side - everything else is ignored */
String uuid = fetchedConfiguration.getUuid();

/* add to update again */
OpenApiExecutionProfileUpdateConfigurationsInner existingItem = new OpenApiExecutionProfileUpdateConfigurationsInner();
existingItem.setUuid(uuid);
update.addConfigurationsItem(existingItem);
}
return update;
}

}

0 comments on commit fcad7e9

Please sign in to comment.