Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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,8 +2,6 @@

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 @@ -12,7 +10,7 @@ public interface OperationsApiService {

ResponseEntity<List<String>> getOperationActions(String spaceGuid, String operationId);

ResponseEntity<Void> executeOperationAction(HttpServletRequest request, String spaceGuid, String operationId, String actionId);
ResponseEntity<Void> executeOperationAction(String spaceGuid, String operationId, String actionId);

ResponseEntity<List<Operation>> getOperations(String spaceGuid, String mtaId, List<String> states, Integer last);

Expand All @@ -22,6 +20,6 @@ public interface OperationsApiService {

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

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
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 All @@ -23,13 +27,6 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Authorization;

@Api
@RestController
@RequestMapping(Resources.OPERATIONS)
Expand All @@ -44,10 +41,10 @@ public class OperationsApi {

}) }, tags = {})
@ApiResponses(value = { @ApiResponse(code = 202, message = "Accepted") })
public ResponseEntity<Void> executeOperationAction(HttpServletRequest request, @PathVariable(PathVariables.SPACE_GUID) String spaceGuid,
public ResponseEntity<Void> executeOperationAction(@PathVariable(PathVariables.SPACE_GUID) String spaceGuid,
@PathVariable(PathVariables.OPERATION_ID) String operationId,
@RequestParam(PathVariables.ACTION_ID) String actionId) {
return delegate.executeOperationAction(request, spaceGuid, operationId, actionId);
return delegate.executeOperationAction(spaceGuid, operationId, actionId);
}

@GetMapping(path = Endpoints.OPERATION, produces = MediaType.APPLICATION_JSON_VALUE)
Expand All @@ -57,9 +54,9 @@ public ResponseEntity<Void> executeOperationAction(HttpServletRequest request, @
}) }, tags = {})
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = Operation.class) })
public ResponseEntity<Operation>
getOperation(@PathVariable(PathVariables.SPACE_GUID) String spaceGuid,
@PathVariable(PathVariables.OPERATION_ID) String operationId,
@ApiParam(value = "Adds the specified property in the response body ") @RequestParam(name = "embed", required = false) String embed) {
getOperation(@PathVariable(PathVariables.SPACE_GUID) String spaceGuid,
@PathVariable(PathVariables.OPERATION_ID) String operationId,
@ApiParam(value = "Adds the specified property in the response body ") @RequestParam(name = "embed", required = false) String embed) {
return delegate.getOperation(spaceGuid, operationId, embed);
}

Expand Down Expand Up @@ -116,9 +113,9 @@ public ResponseEntity<List<String>> getOperationActions(@PathVariable(PathVariab

}) }, tags = {})
@ApiResponses(value = { @ApiResponse(code = 202, message = "Accepted") })
public ResponseEntity<Operation> startOperation(HttpServletRequest request, @PathVariable(PathVariables.SPACE_GUID) String spaceGuid,
public ResponseEntity<Operation> startOperation(@PathVariable(PathVariables.SPACE_GUID) String spaceGuid,
@RequestBody Operation operation) {
return delegate.startOperation(request, spaceGuid, operation);
return delegate.startOperation(spaceGuid, operation);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,63 +25,58 @@ public class CloudControllerClientProvider implements DisposableBean {
/**
* Returns a client for the specified user guid and space id by either getting it from the clients cache or creating a new one.
*
* @param userName the username associated with the client
* @param userGuid the userGuid associated with the client
* @param spaceGuid the space guid associated with the client
* @param correlationId of the process which is used to tag HTTP requests
* @return a CF client for the specified access token, organization, and space
*/
public CloudControllerClient getControllerClient(String userName, String userGuid, String spaceGuid, String correlationId) {
public CloudControllerClient getControllerClient(String userGuid, String spaceGuid, String correlationId) {
try {
return getClientFromCache(userName, userGuid, spaceGuid, correlationId);
return getClientFromCache(userGuid, spaceGuid, correlationId);
} catch (CloudOperationException e) {
throw new SLException(e, Messages.CANT_CREATE_CLIENT_FOR_SPACE_ID, spaceGuid);
}
}

/**
* Returns a client for the specified username and space id by either getting it from the clients cache or creating a new one.
* Returns a client for the specified user and space id by either getting it from the clients cache or creating a new one.
*
* @param userName the username associated with the client
* @param userGuid the userGuid associated with the client
* @param spaceGuid the space guid associated with the client
* @return a CF client for the specified access token, organization, and space
*/
public CloudControllerClient getControllerClientWithNoCorrelation(String userName, String userGuid, String spaceGuid) {
public CloudControllerClient getControllerClientWithNoCorrelation(String userGuid, String spaceGuid) {
try {
return getClientFromCacheWithNoCorrelation(userName, userGuid, spaceGuid);
return getClientFromCacheWithNoCorrelation(userGuid, spaceGuid);
} catch (CloudOperationException e) {
throw new SLException(e, Messages.CANT_CREATE_CLIENT_FOR_SPACE_ID, spaceGuid);
}
}

/**
* Releases the client for the specified username and space id by removing it from the clients cache.
* Releases the client for the specified user and space id by removing it from the clients cache.
*
* @param userGuid the userGuid associated with the client
* @param spaceGuid the space id associated with the client
*/
public void releaseClient(String userGuid, String spaceGuid) {
clients.remove(getKey(spaceGuid, userGuid, null));
clients.remove(getKey(spaceGuid, userGuid));
}

private CloudControllerClient getClientFromCacheWithNoCorrelation(String userName, String userGuid, String spaceId) {
String key = getKey(spaceId, userGuid, userName);
return clients.computeIfAbsent(key, () -> clientFactory.createClient(tokenService.getToken(userName, userGuid), spaceId, null));
private CloudControllerClient getClientFromCacheWithNoCorrelation(String userGuid, String spaceId) {
String key = getKey(spaceId, userGuid);
return clients.computeIfAbsent(key, () -> clientFactory.createClient(tokenService.getToken(userGuid), spaceId, null));
}

private CloudControllerClient getClientFromCache(String userName, String userGuid, String spaceId, String correlationId) {
String key = getKey(spaceId, userGuid, userName);
private CloudControllerClient getClientFromCache(String userGuid, String spaceId, String correlationId) {
String key = getKey(spaceId, userGuid);
return clients.computeIfAbsent(key,
() -> clientFactory.createClient(tokenService.getToken(userName, userGuid), spaceId, correlationId));
() -> clientFactory.createClient(tokenService.getToken(userGuid), spaceId, correlationId));
}

private String getKey(String spaceGuid, String userGuid, String username) {
if (userGuid != null) {
return spaceGuid + "|" + userGuid;
}
// TODO: Remove this branch when userGuid is guaranteed to be non-null(In the next release after introduction of userGuid)
return spaceGuid + "|" + username;
private String getKey(String spaceGuid, String userGuid) {
return spaceGuid + "|" + userGuid;

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public OAuth2AccessTokenWithAdditionalInfo getToken() {
.isBefore(Instant.now()
.plus(120, ChronoUnit.SECONDS))) {
TokenProperties tokenProperties = TokenProperties.fromToken(token);
token = tokenService.getToken(tokenProperties.getUserName(), tokenProperties.getUserId());
token = tokenService.getToken(tokenProperties.getUserId());
LOGGER.info(
MessageFormat.format(Messages.RETRIEVED_TOKEN_FOR_USER_WITH_GUID_0_WITH_EXPIRATION_TIME_1, tokenProperties.getUserId(),
token.getOAuth2AccessToken()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,16 @@ public TokenService(AccessTokenService accessTokenService, TokenParserChain toke
/**
* Chooses a token among all tokens for this user in the access token table.
*
* @param username the username
* @param userGuid the userGuid
* @return the latest token, or throw an exception if token is not found
*/
public OAuth2AccessTokenWithAdditionalInfo getToken(String username, String userGuid) {
if (userGuid != null) {
OAuth2AccessTokenWithAdditionalInfo cachedAccessToken = cachedTokens.get(userGuid);
if (shouldUseCachedToken(cachedAccessToken)) {
return cachedAccessToken;
}
return getLatestAccessTokenByUserGuid(userGuid);
public OAuth2AccessTokenWithAdditionalInfo getToken(String userGuid) {
OAuth2AccessTokenWithAdditionalInfo cachedAccessToken = cachedTokens.get(userGuid);
if (shouldUseCachedToken(cachedAccessToken)) {
return cachedAccessToken;
}
// TODO: If no tokens are found for the userGuid, try to find tokens by username. This is temporary and should be removed in the next release.
return getLatestAccessTokenByUsername(username);
return getLatestAccessTokenByUserGuid(userGuid);

}

private boolean shouldUseCachedToken(OAuth2AccessTokenWithAdditionalInfo cachedAccessToken) {
Expand Down Expand Up @@ -97,21 +93,6 @@ private void addTokenToCache(OAuth2AccessTokenWithAdditionalInfo token, List<Acc
}
}

private OAuth2AccessTokenWithAdditionalInfo getLatestAccessTokenByUsername(String username) {
List<AccessToken> tokensByUsername = getSortedAccessTokensByUsername(username);
if (tokensByUsername.isEmpty()) {
throw new IllegalStateException(MessageFormat.format(Messages.NO_VALID_TOKEN_FOUND, username));
}
return getLatestToken(tokensByUsername);
}

private List<AccessToken> getSortedAccessTokensByUsername(String username) {
return accessTokenService.createQuery()
.username(username)
.orderByExpiresAt(OrderDirection.DESCENDING)
.list();
}

private OAuth2AccessTokenWithAdditionalInfo getLatestToken(List<AccessToken> accessTokens) {
AccessToken latestAccessToken = accessTokens.get(0);
return tokenParserChain.parse(new String(latestAccessToken.getValue(), StandardCharsets.UTF_8));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void setUp() throws Exception {
void testGetTokenWhenThereAreNoTokensForUser() {
AccessTokenQuery accessTokenQuery = Mockito.mock(AccessTokenQuery.class);
mockAccessTokenService(accessTokenQuery);
Exception exception = assertThrows(IllegalStateException.class, () -> tokenService.getToken("deploy-service-user", "123"));
Exception exception = assertThrows(IllegalStateException.class, () -> tokenService.getToken("123"));
assertEquals("No valid access token was found for user guid \"123\"", exception.getMessage());
}

Expand All @@ -62,10 +62,10 @@ void testGetTokenWhenThereIsOnlyOneTokenInDb() {
mockAccessTokenService(accessTokenQuery);
OAuth2AccessTokenWithAdditionalInfo mockedToken = Mockito.mock(OAuth2AccessTokenWithAdditionalInfo.class);
Mockito.when(mockedToken.getAdditionalInfo())
.thenReturn(Map.of(TokenProperties.USER_NAME_KEY, "deploy-service-user", TokenProperties.USER_ID_KEY, "123"));
.thenReturn(Map.of(TokenProperties.USER_ID_KEY, "123"));
Mockito.when(tokenParserChain.parse(any()))
.thenReturn(mockedToken);
OAuth2AccessTokenWithAdditionalInfo token = tokenService.getToken("deploy-service-user", "123");
OAuth2AccessTokenWithAdditionalInfo token = tokenService.getToken("123");
assertEquals(mockedToken, token);
}

Expand All @@ -75,10 +75,10 @@ void testGetTokenWhenThereIsNewerTokenInDb() {
mockAccessTokenService(accessTokenQuery);
OAuth2AccessTokenWithAdditionalInfo mockedToken = Mockito.mock(OAuth2AccessTokenWithAdditionalInfo.class);
Mockito.when(mockedToken.getAdditionalInfo())
.thenReturn(Map.of(TokenProperties.USER_NAME_KEY, "deploy-service-user", TokenProperties.USER_ID_KEY, "123"));
.thenReturn(Map.of(TokenProperties.USER_ID_KEY, "123"));
Mockito.when(tokenParserChain.parse(any()))
.thenReturn(mockedToken);
OAuth2AccessTokenWithAdditionalInfo token = tokenService.getToken("deploy-service-user", "123");
OAuth2AccessTokenWithAdditionalInfo token = tokenService.getToken("123");
assertEquals(mockedToken, token);
}

Expand All @@ -94,11 +94,11 @@ void testGetTokenWhenCachedIsAvailable() {
Mockito.when(mockedToken.getOAuth2AccessToken())
.thenReturn(oAuth2AccessToken);
Mockito.when(mockedToken.getAdditionalInfo())
.thenReturn(Map.of(TokenProperties.USER_NAME_KEY, "deploy-service-user", TokenProperties.USER_ID_KEY, "123"));
.thenReturn(Map.of(TokenProperties.USER_ID_KEY, "123"));
Mockito.when(tokenParserChain.parse(any()))
.thenReturn(mockedToken);
tokenService.getToken("deploy-service-user", "123");
OAuth2AccessTokenWithAdditionalInfo token = tokenService.getToken("deploy-service-user", "123");
tokenService.getToken("123");
OAuth2AccessTokenWithAdditionalInfo token = tokenService.getToken("123");
assertEquals(mockedToken, token);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package org.cloudfoundry.multiapps.controller.process.flowable;

import static java.text.MessageFormat.format;

import java.util.List;

import jakarta.inject.Inject;
import jakarta.inject.Named;

import org.cloudfoundry.multiapps.controller.api.model.Operation;
import org.cloudfoundry.multiapps.controller.core.cf.CloudControllerClientProvider;
import org.cloudfoundry.multiapps.controller.core.util.SafeExecutor;
import org.cloudfoundry.multiapps.controller.core.util.UserInfo;
import org.cloudfoundry.multiapps.controller.persistence.model.HistoricOperationEvent;
import org.cloudfoundry.multiapps.controller.persistence.model.ImmutableHistoricOperationEvent;
import org.cloudfoundry.multiapps.controller.persistence.model.ProgressMessage;
Expand All @@ -25,6 +23,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.text.MessageFormat.format;

@Named
public class AbortProcessAction extends ProcessAction {

Expand All @@ -47,7 +47,7 @@ public AbortProcessAction(FlowableFacade flowableFacade, List<AdditionalProcessA
}

@Override
public void executeActualProcessAction(String user, String superProcessInstanceId) {
public void executeActualProcessAction(UserInfo userInfo, String superProcessInstanceId) {
releaseOperationLock(superProcessInstanceId, Operation.State.ABORTED);
historicEventService.add(ImmutableHistoricOperationEvent.of(superProcessInstanceId, HistoricOperationEvent.EventType.ABORTED));
historicEventService.add(ImmutableHistoricOperationEvent.of(superProcessInstanceId,
Expand Down
Loading
Loading