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 @@ -3,14 +3,15 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.opendevstack.component_provisioner.client.component_catalog.v1.api.CatalogItemUserActionMessageDefinitionsApi;
import org.opendevstack.component_provisioner.client.component_catalog.v1.api.ProjectComponentsApi;
import org.opendevstack.component_provisioner.client.component_catalog.v1.model.*;
import org.opendevstack.component_provisioner.config.ApplicationPropertiesConfiguration;
import org.opendevstack.component_provisioner.server.controllers.exceptions.UserNotAllowedException;
import org.opendevstack.component_provisioner.server.controllers.model.ProjectComponentStatus;
import org.opendevstack.component_provisioner.server.services.exceptions.CatalogClientException;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -170,7 +171,15 @@ public ProjectComponentExtendedInfo getProjectComponentById(String accessToken,
var apiClient = apiClientsBuilder.componentCatalogApiClient(accessToken, componentCatalogServiceProps.getBaseRestUrl().toString());
var componentsApi = apiClientsBuilder.projectComponentsApi(apiClient);

return componentsApi.getProjectComponentById(projectKey, componentId);
try {
return componentsApi.getProjectComponentById(projectKey, componentId);
} catch (HttpStatusCodeException e) {
if (e.getStatusCode().value() == HttpStatus.FORBIDDEN.value()) {
log.warn("Forbidden response from component catalog for project '{}', componentId '{}': {}", projectKey, componentId, e.getMessage());
throw new UserNotAllowedException("Access to component catalog is forbidden for project '" + projectKey + "', componentId '" + componentId + "'");
}
throw e;
}
}

private Map<String, List<String>> obfuscateParameters(Map<String, List<String>> parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -585,4 +585,47 @@ void givenAProjectKeyAndComponentId_whenGetProjectComponentExtendedInfoIsCalled_
verify(projectComponentsApi).getProjectComponentById(projectKey, componentId);
}

@Test
void givenComponentCatalogReturns403_whenGetProjectComponentByIdIsCalled_thenThrowsUserNotAllowedException() throws MalformedURLException {
// given
String accessToken = "bearerToken";
var projectKey = "PRJ";
var componentId = "CID";

URL baseUrl = URI.create("http://component-catalog").toURL();
when(componentCatalogServiceProps.getBaseRestUrl()).thenReturn(baseUrl);
when(apiClientsBuilder.componentCatalogApiClient(accessToken, baseUrl.toString()))
.thenReturn(componentCatalogApiClient);
when(apiClientsBuilder.projectComponentsApi(componentCatalogApiClient))
.thenReturn(projectComponentsApi);
when(projectComponentsApi.getProjectComponentById(projectKey, componentId))
.thenThrow(new HttpClientErrorException(HttpStatus.FORBIDDEN));

// when / then
assertThatThrownBy(() -> componentCatalogService.getProjectComponentById(accessToken, projectKey, componentId))
.isInstanceOf(org.opendevstack.component_provisioner.server.controllers.exceptions.UserNotAllowedException.class);
}

@Test
void givenComponentCatalogReturnsNon403HttpError_whenGetProjectComponentByIdIsCalled_thenRethrowsOriginalException() throws MalformedURLException {
// given
String accessToken = "bearerToken";
var projectKey = "PRJ";
var componentId = "CID";
var originalException = new HttpClientErrorException(HttpStatus.INTERNAL_SERVER_ERROR);

URL baseUrl = URI.create("http://component-catalog").toURL();
when(componentCatalogServiceProps.getBaseRestUrl()).thenReturn(baseUrl);
when(apiClientsBuilder.componentCatalogApiClient(accessToken, baseUrl.toString()))
.thenReturn(componentCatalogApiClient);
when(apiClientsBuilder.projectComponentsApi(componentCatalogApiClient))
.thenReturn(projectComponentsApi);
when(projectComponentsApi.getProjectComponentById(projectKey, componentId))
.thenThrow(originalException);

// when / then
assertThatThrownBy(() -> componentCatalogService.getProjectComponentById(accessToken, projectKey, componentId))
.isSameAs(originalException);
}

}
Loading