Skip to content

Commit

Permalink
Replace status page API calls with SecHub Java API calls #2513 and im…
Browse files Browse the repository at this point in the history
…plement #469

- return JSON instead of text for the server version #469
- adjust the REST Doc tests für #469
- replace the status page API calls with SecHub Java API calls #2513
- improve return value of server status API call #2513

closes: #2513
closes: #469
  • Loading branch information
Jeeppler committed Aug 28, 2023
1 parent 2f57762 commit 712eca5
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 120 deletions.
11 changes: 3 additions & 8 deletions gradle/projects.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,10 @@ projectType = [

],

/* SecHub frontend projects */
springBootWebUiProjects:[
project(':sechub-webui'),
],

bootableSpringApplicationProjects:[
project(':sechub-server'),
project(':sechub-pds'),
project(':sechub-wrapper-checkmarx'),
project(':sechub-webui'),
],

/* documentation projects */
Expand Down Expand Up @@ -128,7 +122,7 @@ projectType = [
}

if (secHubBuildStage.providesGeneratedOpenApiFile()){
/* add the java projects which need a open api file / compiled java api */
/* add the java projects which need an open api file / compiled java api */
projectType.javaProjects.add(project(':sechub-api-java'))

projectType.javaProjects.add(project(':sechub-systemtest'))
Expand All @@ -141,6 +135,8 @@ if (secHubBuildStage.providesGeneratedOpenApiFile()){

/* make it possible to use integration test parts for systemtest (unit tests) as well */
projectType.integrationTestProjects.add(project(':sechub-systemtest'))

//projectType.bootableSpringApplicationProjects.add(project(':sechub-webui'))
}


Expand All @@ -149,7 +145,6 @@ projectType.springBootProjects.addAll(projectType.springBootSecHubServerProjects
projectType.springBootProjects.addAll(projectType.springBootPDSProjects)
projectType.springBootProjects.addAll(projectType.springBootAdapterProjects)
projectType.springBootProjects.addAll(projectType.springDocProjects)
projectType.springBootProjects.addAll(projectType.springBootWebUiProjects)

projectType.javaProjects.addAll(projectType.springBootProjects)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.UUID;
import java.util.concurrent.Callable;

Expand All @@ -19,6 +22,8 @@
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.json.JsonMapper;
import com.mercedesbenz.sechub.api.SecHubStatus.Jobs;
import com.mercedesbenz.sechub.api.SecHubStatus.Scheduler;
import com.mercedesbenz.sechub.api.internal.ApiClientBuilder;
import com.mercedesbenz.sechub.api.internal.OpenApiSecHubClientConversionHelper;
import com.mercedesbenz.sechub.api.internal.WorkaroundAdminApi;
Expand Down Expand Up @@ -275,16 +280,26 @@ public List<ExecutorConfigurationInfo> fetchAllExecutorConfigurationInfo() throw
/* + ................Status........................... + */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
public SecHubStatus fetchSecHubStatus() throws SecHubClientException {
SecHubStatus status = new SecHubStatus();
SecHubStatus status = null;

Map<String, String> statusInformation = new TreeMap<>();

runOrFail(() -> {
List<OpenApiStatusInformationInner> statusInformationList = adminApi.adminListsStatusInformation();

for (OpenApiStatusInformationInner info : statusInformationList) {
String key = info.getKey();
if (key != null) {
status.statusInformation.put(key, info.getValue());
statusInformation.put(key, info.getValue());
}
}

}, "Was not able to fetch SecHub status!");

if (!statusInformation.isEmpty()) {
status = convertStatusMapToStatus(statusInformation);
}

return status;
}

Expand Down Expand Up @@ -468,6 +483,19 @@ public void approveJob(String projectId, UUID jobUUID) throws SecHubClientExcept
runOrFail(() -> projectApi.userApprovesJob(projectId, jobUUID.toString()), "Job approve");
}

/* +++++++++++++++++++++++++++++++++++++++++++++++++ */
/* + ................Version...................... + */
/* +++++++++++++++++++++++++++++++++++++++++++++++++ */

public Optional<String> getServerVersion() throws SecHubClientException {
runOrFail(() -> {
Optional<String> serverVersion = Optional.of(adminApi.adminChecksServerVersion().getServerVersion());
return serverVersion;
}, "Get server version");

return Optional.empty();
}

/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
/* + ................Helpers......................... + */
/* ++++++++++++++++++++++++++++++++++++++++++++++++++++ */
Expand Down Expand Up @@ -509,4 +537,20 @@ private interface SecHubClientListenerCaller {
public void inform(SecHubClientListener listener);

}

private SecHubStatus convertStatusMapToStatus(Map<String, String> statusMap) {
var schedulerEnabled = Boolean.valueOf(statusMap.get("status.scheduler.enabled")).booleanValue();
var jobsAll = Long.valueOf(statusMap.get("status.scheduler.jobs.all"));
var jobsCancelRequested = Long.valueOf(statusMap.get("status.scheduler.jobs.cancel_requested"));
var jobsCanceled = Long.valueOf(statusMap.get("status.scheduler.jobs.canceled"));
var jobsEnded = Long.valueOf(statusMap.get("status.scheduler.jobs.ended"));
var jobsInitializing = Long.valueOf(statusMap.get("status.scheduler.jobs.initializing"));
var jobsReadyToStart = Long.valueOf(statusMap.get("status.scheduler.jobs.ready_to_start"));
var jobsStarted = Long.valueOf(statusMap.get("status.scheduler.jobs.started"));

SecHubStatus.Scheduler scheduler = new Scheduler(schedulerEnabled);
SecHubStatus.Jobs jobs = new Jobs(jobsAll, jobsCancelRequested, jobsCanceled, jobsEnded, jobsInitializing, jobsReadyToStart, jobsStarted);

return new SecHubStatus(scheduler, jobs);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
// SPDX-License-Identifier: MIT
package com.mercedesbenz.sechub.api;

import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
public record SecHubStatus(Scheduler scheduler, Jobs jobs) {

public class SecHubStatus {

Map<String, String> statusInformation = new TreeMap<>();

public Map<String, String> getStatusInformationMap() {
return Collections.unmodifiableMap(statusInformation);
public record Scheduler(boolean isEnabled) {
}

@Override
public String toString() {
return "SecHubStatus [" + (statusInformation != null ? "statusInformation=" + statusInformation : "") + "]";
public record Jobs(long all, long cancelRequested, long canceled, long ended, long initializating, long readyToStart, long started) {
}

}
114 changes: 60 additions & 54 deletions sechub-api-java/src/main/resources/reduced-openapi3.json
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@
"200": {
"description": "200",
"content": {
"text/plain;charset=UTF-8": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ServerVersion"
}
Expand Down Expand Up @@ -2771,6 +2771,16 @@
"type": "string"
}
},
"ServerVersion": {
"title": "ServerVersion",
"type": "object",
"properties": {
"serverVersion": {
"type": "string",
"description": "The sechub server version."
}
}
},
"UserSignup": {
"title": "UserSignup",
"type": "object",
Expand Down Expand Up @@ -3185,6 +3195,55 @@
}
}
},
"ExecutionProfileFetch": {
"title": "ExecutionProfileFetch",
"type": "object",
"properties": {
"configurations": {
"type": "array",
"items": {
"type": "object",
"properties": {
"productIdentifier": {
"type": "string",
"description": "executed product"
},
"name": {
"type": "string",
"description": "name of configuration"
},
"executorVersion": {
"type": "number",
"description": "executor version"
},
"uuid": {
"type": "string",
"description": "uuid of configuration"
},
"enabled": {
"type": "boolean",
"description": "enabled state of this config"
}
}
}
},
"description": {
"type": "string",
"description": "A short description for the profile"
},
"projectIds": {
"type": "array",
"description": "Projects can be linked by their ids here",
"items": {
"type": "string"
}
},
"enabled": {
"type": "boolean",
"description": "Enabled state of profile, default is false"
}
}
},
"FalsePositives": {
"title": "FalsePositives",
"type": "object",
Expand Down Expand Up @@ -3298,55 +3357,6 @@
}
}
},
"ExecutionProfileFetch": {
"title": "ExecutionProfileFetch",
"type": "object",
"properties": {
"configurations": {
"type": "array",
"items": {
"type": "object",
"properties": {
"productIdentifier": {
"type": "string",
"description": "executed product"
},
"name": {
"type": "string",
"description": "name of configuration"
},
"executorVersion": {
"type": "number",
"description": "executor version"
},
"uuid": {
"type": "string",
"description": "uuid of configuration"
},
"enabled": {
"type": "boolean",
"description": "enabled state of this config"
}
}
}
},
"description": {
"type": "string",
"description": "A short description for the profile"
},
"projectIds": {
"type": "array",
"description": "Projects can be linked by their ids here",
"items": {
"type": "string"
}
},
"enabled": {
"type": "boolean",
"description": "Enabled state of profile, default is false"
}
}
},
"FullScanDataZIP": {
"title": "FullScanDataZIP",
"type": "object"
Expand Down Expand Up @@ -3383,10 +3393,6 @@
}
}
},
"ServerVersion": {
"title": "ServerVersion",
"type": "object"
},
"JobId": {
"title": "JobId",
"type": "object",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// SPDX-License-Identifier: MIT
package com.mercedesbenz.sechub.restdoc;

import static com.mercedesbenz.sechub.restdoc.RestDocumentation.*;
import static com.mercedesbenz.sechub.test.SecHubTestURLBuilder.*;
import static org.mockito.Mockito.*;
import static org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static com.mercedesbenz.sechub.restdoc.RestDocumentation.defineRestService;
import static com.mercedesbenz.sechub.test.SecHubTestURLBuilder.https;
import static org.mockito.Mockito.when;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.lang.annotation.Annotation;

Expand All @@ -28,6 +30,7 @@
import com.mercedesbenz.sechub.docgen.util.RestDocFactory;
import com.mercedesbenz.sechub.server.core.InfoService;
import com.mercedesbenz.sechub.server.core.ServerInfoAdministrationRestController;
import com.mercedesbenz.sechub.server.core.ServerVersion;
import com.mercedesbenz.sechub.sharedkernel.Profiles;
import com.mercedesbenz.sechub.sharedkernel.RoleConstants;
import com.mercedesbenz.sechub.sharedkernel.configuration.AbstractAllowSecHubAPISecurityConfiguration;
Expand All @@ -48,7 +51,8 @@ public class ServerInfoAdministrationRestControllerRestDocTest implements TestIs

private static final int PORT_USED = TestPortProvider.DEFAULT_INSTANCE.getRestDocTestPort();

private static final String SERVER_VERSION = "0.12.3";
private static final String SECHUB_SERVER_VERSION = "0.12.3";
private static final String SERVER_VERSION = "serverVersion";

@Autowired
private MockMvc mockMvc;
Expand All @@ -63,26 +67,29 @@ public void restdoc_admin_get_server_version() throws Exception {
String apiEndpoint = https(PORT_USED).buildGetServerVersionUrl();
Class<? extends Annotation> useCase = UseCaseAdminChecksServerVersion.class;

when(serverInfoService.getVersionAsString()).thenReturn(SERVER_VERSION);
when(serverInfoService.getVersionAsString()).thenReturn(new ServerVersion(SECHUB_SERVER_VERSION));

String expectedContent = "{\"" + SERVER_VERSION + "\":\"" + SECHUB_SERVER_VERSION + "\"}";

/* execute + test @formatter:off */
this.mockMvc.perform(
get(apiEndpoint).
contentType(MediaType.TEXT_PLAIN_VALUE).
contentType(MediaType.APPLICATION_JSON).
header(AuthenticationHelper.HEADER_NAME, AuthenticationHelper.getHeaderValue())
).
andExpect(status().isOk()).
andExpect(content().string(SERVER_VERSION)).
andExpect(content().string(expectedContent)).
andDo(defineRestService().
with().
useCaseData(useCase).
tag(RestDocFactory.extractTag(apiEndpoint)).
responseSchema(OpenApiSchema.SERVER_VERSION.getSchema()).
and().
document(
requestHeaders(
responseFields(
fieldWithPath(SERVER_VERSION).description("The sechub server version.")
)

)
)
);
/* @formatter:on */
Expand Down
Loading

0 comments on commit 712eca5

Please sign in to comment.