Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IA-5006: filter chartNames based on allowed values #16

Merged
merged 6 commits into from
Jul 3, 2024
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
@@ -0,0 +1,9 @@
package bio.terra.appmanager.config;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "appmanager.charts")
public record ChartServiceConfiguration(@NotNull @NotEmpty List<String> allowedNames) {}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bio.terra.appmanager.service;

import bio.terra.appmanager.config.ChartServiceConfiguration;
import bio.terra.appmanager.controller.ChartNotFoundException;
import bio.terra.appmanager.dao.ChartDao;
import bio.terra.appmanager.model.Chart;
Expand All @@ -13,34 +14,43 @@
@Service
public class ChartService {

private final List<String> allowedChartNames;
private final ChartDao chartDao;

public ChartService(ChartDao chartDao) {
public ChartService(ChartServiceConfiguration chartServiceConfiguration, ChartDao chartDao) {
this.allowedChartNames = chartServiceConfiguration.allowedNames();
this.chartDao = chartDao;
}

/**
* Create chart entries with associated chart and application versions.
*
* @param charts non-null list of {@link Chart}s to create
* @throws IllegalArgumentException if Chart.name is not an allowed value
*/
@WriteTransaction
public void createCharts(@NotNull List<Chart> charts) {
charts.forEach(chartDao::upsert);
charts.forEach(
chart -> {
checkChartName(chart);
chartDao.upsert(chart);
});
}

/**
* Update chart entries with associated chart and application versions. It is assumed that the
* caller of this validates whether the versions exist, and this method `upserts` for all records
*
* @param versions non-null list of {@link Chart}s to update
* @throws IllegalArgumentException if Chart.name is not an allowed value
*/
@WriteTransaction
public void updateVersions(@NotNull List<Chart> versions) {

List<Chart> existingVersions;
ArrayList<String> nonexistentVersions = new ArrayList<>();
for (Chart chart : versions) {
checkChartName(chart);
existingVersions = getCharts(List.of(chart.name()), true);
if (existingVersions.isEmpty()) {
nonexistentVersions.add(chart.name());
Expand All @@ -61,6 +71,7 @@ public void updateVersions(@NotNull List<Chart> versions) {
*
* @param name non-null chart name to delete
*/
@WriteTransaction
public void deleteVersion(@NotNull String name) {
chartDao.delete(List.of(name));
}
Expand All @@ -76,4 +87,10 @@ public void deleteVersion(@NotNull String name) {
public List<Chart> getCharts(@NotNull List<String> names, @NotNull Boolean includeAll) {
return chartDao.get(names, includeAll);
}

private void checkChartName(Chart chart) {
if (!allowedChartNames.contains(chart.name())) {
throw new IllegalArgumentException("unrecogrnized chartName provided");
}
}
}
10 changes: 10 additions & 0 deletions service/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ appmanager:
service_accounts_for_read: ${env.admin.service_accounts_for_read}
service_accounts_for_write: ${env.admin.service_accounts_for_write}

charts:
allowedNames:
- leonardo/hail-batch-terra-azure
- leonardo/terra-app-setup
- cromwell-helm/cromwell-on-azure
- terra-helm/cromwell-runner-app
- terra-helm/listener
- terra-helm/wds
- terra-helm/workflows-app

ingress:
# Default value that's overridden by Helm.
domainName: localhost:8080
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.calls;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;

import bio.terra.appmanager.BaseSpringBootTest;
import bio.terra.appmanager.config.ChartServiceConfiguration;
import bio.terra.appmanager.controller.ChartNotFoundException;
import bio.terra.appmanager.dao.ChartDao;
import bio.terra.appmanager.model.Chart;
Expand All @@ -15,19 +22,38 @@
import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;

class ChartServiceTest extends BaseSpringBootTest {

@MockBean ChartDao chartDao;

@Autowired ChartService chartService;
@Autowired
@Qualifier("mockService")
ChartService chartService;

@Test
void createChart_withEmptyList() {
void testCreateChart_withEmptyList() {
chartService.createCharts(List.of());
verifyNoInteractions(chartDao);
}

@Test
void testCreateChart_unknownChartName() {
String chartName1 = "unknown-chart";
String chartVersion1 = ChartTestUtils.makeChartVersion(0);
Chart version1 = new Chart(chartName1, chartVersion1);
List<Chart> chartList = List.of(version1);

Exception exception =
assertThrows(IllegalArgumentException.class, () -> chartService.createCharts(chartList));

assertTrue(exception.getMessage().contains("unrecogrnized chartName provided"));
}

@Test
void testCreateChart_singleElement() {
String chartName1 = "chart-name-here";
Expand Down Expand Up @@ -103,6 +129,19 @@ void testUpdateVersions() {
verify(chartDao, times(1)).upsert(chart);
}

@Test
void testUpdateChart_unknownChartName() {
String chartName1 = "unknown-chart";
String chartVersion1 = ChartTestUtils.makeChartVersion(0);
Chart version1 = new Chart(chartName1, chartVersion1);
List<Chart> chartList = List.of(version1);

Exception exception =
assertThrows(IllegalArgumentException.class, () -> chartService.updateVersions(chartList));

assertTrue(exception.getMessage().contains("unrecogrnized chartName provided"));
}

@Test
void testUpdateVersions_chartNotFound() {
String chartName = "chart-name";
Expand Down Expand Up @@ -135,4 +174,15 @@ void testUpdate_multipleChartNotFound() {
+ notPresentCharts,
ex.getMessage());
}

@TestConfiguration
public static class MockChartServiceConfiguration {
@Bean(name = "mockService")
public ChartService getChartService(ChartDao chartDao) {
return new ChartService(
new ChartServiceConfiguration(
List.of("chart-name-here", "chart-name", "chart-name2", "chart-name3")),
chartDao);
}
}
}
Loading