Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import jakarta.servlet.http.HttpServletRequest;
import org.cloudfoundry.multiapps.controller.api.model.Log;
import org.cloudfoundry.multiapps.controller.api.model.Operation;
import org.springframework.http.ResponseEntity;
Expand All @@ -20,6 +21,6 @@ public interface OperationsApiService {

ResponseEntity<String> getOperationLogContent(String spaceGuid, String operationId, String logId);

ResponseEntity<Operation> startOperation(String spaceGuid, Operation operation);
ResponseEntity<Operation> startOperation(String spaceGuid, Operation operation, HttpServletRequest httpServletRequest);

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;
import jakarta.inject.Inject;
import jakarta.servlet.http.HttpServletRequest;
import org.cloudfoundry.multiapps.controller.api.Constants.Endpoints;
import org.cloudfoundry.multiapps.controller.api.Constants.PathVariables;
import org.cloudfoundry.multiapps.controller.api.Constants.QueryVariables;
Expand Down Expand Up @@ -114,8 +115,8 @@ public ResponseEntity<List<String>> getOperationActions(@PathVariable(PathVariab
}) }, tags = {})
@ApiResponses(value = { @ApiResponse(code = 202, message = "Accepted") })
public ResponseEntity<Operation> startOperation(@PathVariable(PathVariables.SPACE_GUID) String spaceGuid,
@RequestBody Operation operation) {
return delegate.startOperation(spaceGuid, operation);
@RequestBody Operation operation, HttpServletRequest httpServletRequest) {
return delegate.startOperation(spaceGuid, operation, httpServletRequest);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ public static OperationMetadata getMetadata() {
.type(ParameterType.BOOLEAN)
.defaultValue(false)
.build())
.addParameter(ImmutableParameterMetadata.builder()
.id(Variables.API_REQUEST_PATH.getName())
.type(ParameterType.STRING)
.build())

// Special blue green deploy parameters:
.addParameter(ImmutableParameterMetadata.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public static OperationMetadata getMetadata() {
.type(ParameterType.BOOLEAN)
.defaultValue(false)
.build())
.addParameter(ImmutableParameterMetadata.builder()
.id(Variables.API_REQUEST_PATH.getName())
.type(ParameterType.STRING)
.build())

// Special CTS+ parameters:
.addParameter(ImmutableParameterMetadata.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ public static OperationMetadata getMetadata() {
.type(ParameterType.BOOLEAN)
.defaultValue(false)
.build())
.addParameter(ImmutableParameterMetadata.builder()
.id(Variables.API_REQUEST_PATH.getName())
.type(ParameterType.STRING)
.build())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ public interface Variables {
Variable<String> DEPLOY_URI = ImmutableSimpleVariable.<String> builder()
.name("deployUri")
.build();
Variable<String> API_REQUEST_PATH = ImmutableSimpleVariable.<String> builder()
.name("apiRequestPath")
.build();
Variable<String> CTS_USERNAME = ImmutableSimpleVariable.<String> builder()
.name("userId")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ protected String[] getParametersIds() {
Variables.STOP_ORDER_IS_DEPENDENCY_AWARE.getName(),
Variables.IS_SECURITY_ENABLED.getName(),
Variables.DISPOSABLE_USER_PROVIDED_SERVICE_NAME.getName(),
Variables.IS_DISPOSABLE_USER_PROVIDED_SERVICE_ENABLED.getName()
Variables.IS_DISPOSABLE_USER_PROVIDED_SERVICE_ENABLED.getName(),
Variables.API_REQUEST_PATH.getName()
// @formatter:on
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ protected String[] getParametersIds() {
Variables.SKIP_APP_DIGEST_CALCULATION.getName(),
Variables.IS_SECURITY_ENABLED.getName(),
Variables.DISPOSABLE_USER_PROVIDED_SERVICE_NAME.getName(),
Variables.IS_DISPOSABLE_USER_PROVIDED_SERVICE_ENABLED.getName()
Variables.IS_DISPOSABLE_USER_PROVIDED_SERVICE_ENABLED.getName(),
Variables.API_REQUEST_PATH.getName()
// @formatter:on
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ protected String[] getParametersIds() {
Variables.SKIP_APP_DIGEST_CALCULATION.getName(),
Variables.IS_SECURITY_ENABLED.getName(),
Variables.DISPOSABLE_USER_PROVIDED_SERVICE_NAME.getName(),
Variables.IS_DISPOSABLE_USER_PROVIDED_SERVICE_ENABLED.getName()
Variables.IS_DISPOSABLE_USER_PROVIDED_SERVICE_ENABLED.getName(),
Variables.API_REQUEST_PATH.getName()
// @formatter:on
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.persistence.NoResultException;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.collections4.ListUtils;
import org.cloudfoundry.multiapps.common.ContentException;
import org.cloudfoundry.multiapps.common.NotFoundException;
Expand Down Expand Up @@ -148,13 +149,13 @@ public ResponseEntity<String> getOperationLogContent(String spaceGuid, String op
}

@Override
public ResponseEntity<Operation> startOperation(String spaceGuid, Operation operation) {
public ResponseEntity<Operation> startOperation(String spaceGuid, Operation operation, HttpServletRequest httpServletRequest) {
operationsApiServiceAuditLog.logStartOperation(SecurityContextUtil.getUsername(), spaceGuid, operation);
UserInfo authenticatedUser = getAuthenticatedUser();
String processDefinitionKey = operationsHelper.getProcessDefinitionKey(operation);
Set<ParameterMetadata> predefinedParameters = operationMetadataMapper.getOperationMetadata(operation.getProcessType())
.getParameters();
operation = addServiceParameters(operation, spaceGuid, authenticatedUser.getName(), authenticatedUser.getId());
operation = addServiceParameters(operation, spaceGuid, authenticatedUser.getName(), authenticatedUser.getId(), httpServletRequest);
operation = addParameterValues(operation, predefinedParameters);
ensureRequiredParametersSet(operation, predefinedParameters);
ProcessInstance processInstance = flowableFacade.startProcess(processDefinitionKey, operation.getParameters());
Expand Down Expand Up @@ -246,7 +247,8 @@ private List<String> getAvailableActions(Operation operation) {
throw new IllegalStateException(MessageFormat.format("State \"{0}\" not recognized!", operation.getState()));
}

private Operation addServiceParameters(Operation operation, String spaceGuid, String user, String userGuid) {
private Operation addServiceParameters(Operation operation, String spaceGuid, String user, String userGuid,
HttpServletRequest httpServletRequest) {
Map<String, Object> parameters = new HashMap<>(operation.getParameters());

CloudSpaceClient client = getSpaceClient();
Expand All @@ -265,6 +267,8 @@ private Operation addServiceParameters(Operation operation, String spaceGuid, St
.toString());
parameters.put(Variables.TIMESTAMP.getName(), DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
.format(ZonedDateTime.now()));
parameters.put(Variables.API_REQUEST_PATH.getName(), httpServletRequest.getRequestURL()
.toString());
String namespace = operation.getNamespace();
if (namespace != null) {
parameters.put(Variables.MTA_NAMESPACE.getName(), namespace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ void testStartOperation() {
Operation operation = createOperation(null, null, parameters);
Mockito.when(operationsHelper.getProcessDefinitionKey(operation))
.thenReturn("deploy");
operationsApiService.startOperation(SPACE_GUID, operation);
HttpServletRequest httpServletRequestMock = Mockito.mock(HttpServletRequest.class);
Mockito.when(httpServletRequestMock.getRequestURL())
.thenReturn(new StringBuffer("test/api/path"));
operationsApiService.startOperation(SPACE_GUID, operation, httpServletRequestMock);
Mockito.verify(flowableFacade)
.startProcess(Mockito.any(), Mockito.anyMap());
}
Expand All @@ -210,12 +213,15 @@ void testStartOperationWithInvalidParametersForTheProcess() {
Mockito.when(operationsHelper.getProcessDefinitionKey(operation))
.thenReturn("deploy");

operationsApiService.startOperation(SPACE_GUID, operation);
HttpServletRequest httpServletRequestMock = Mockito.mock(HttpServletRequest.class);
Mockito.when(httpServletRequestMock.getRequestURL())
.thenReturn(new StringBuffer("test/api/path"));
operationsApiService.startOperation(SPACE_GUID, operation, httpServletRequestMock);

Mockito.verify(flowableFacade)
.startProcess(ArgumentMatchers.eq("deploy"), ArgumentMatchers.argThat(
map -> map.containsKey(Variables.MTA_ID.getName()) && map.containsKey(Variables.EXT_DESCRIPTOR_FILE_ID.getName())
&& !map.containsKey(Variables.CTS_PROCESS_ID.getName()) && !map.containsKey(Variables.DEPLOY_URI.getName())));
&& !map.containsKey(Variables.CTS_PROCESS_ID.getName()) && !map.containsKey(Variables.CTS_PASSWORD.getName())));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why DEPLOY_URI was removed and replaced with CTS_PASSWORD variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussed :)

}

@Test
Expand All @@ -225,8 +231,10 @@ void testStartOperationWithValidParametersForTheProcess() {
Operation operation = createOperation(null, null, parameters);
Mockito.when(operationsHelper.getProcessDefinitionKey(operation))
.thenReturn("deploy");

operationsApiService.startOperation(SPACE_GUID, operation);
HttpServletRequest httpServletRequestMock = Mockito.mock(HttpServletRequest.class);
Mockito.when(httpServletRequestMock.getRequestURL())
.thenReturn(new StringBuffer("test/api/path"));
operationsApiService.startOperation(SPACE_GUID, operation, httpServletRequestMock);

Mockito.verify(flowableFacade)
.startProcess(ArgumentMatchers.eq("deploy"), ArgumentMatchers.argThat(
Expand Down
Loading