Skip to content

Commit

Permalink
Merge branch 'main' into corneil/distable-trivy-main
Browse files Browse the repository at this point in the history
  • Loading branch information
Corneil du Plessis authored Nov 18, 2024
2 parents 0269f22 + bfa3c51 commit 5ccfdf6
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.springframework.security.oauth2.core.AuthorizationGrantType;
import org.springframework.security.oauth2.core.OAuth2AuthorizationException;
import org.springframework.security.oauth2.core.endpoint.OAuth2AccessTokenResponse;
import org.springframework.security.oauth2.server.resource.BearerTokenAuthenticationToken;
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthenticationToken;
import org.springframework.security.oauth2.server.resource.authentication.OpaqueTokenAuthenticationProvider;
import org.springframework.security.oauth2.server.resource.introspection.OpaqueTokenIntrospector;
import org.springframework.web.client.ResourceAccessException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ void index() throws Exception {
fieldWithPath("_links.tasks/thinexecutions.href").description("Link to the tasks/thinexecutions"),

fieldWithPath("_links.tasks/thinexecutions/name.href").description("Link to the tasks/thinexecutions/name"),
fieldWithPath("_links.tasks/thinexecutions/name.templated").description("Link to the tasks/thinexecutions/name is templated"),
fieldWithPath("_links.tasks/thinexecutions/name.templated").type(JsonFieldType.BOOLEAN).optional().description("Link to the tasks/thinexecutions/name is templated"),

fieldWithPath("_links.tasks/info/executions.href").description("Link to the tasks/info/executions"),
fieldWithPath("_links.tasks/info/executions.templated").type(JsonFieldType.BOOLEAN).optional().description("Link tasks/info is templated"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private Optional<String> id(String containerName) throws IOException, Interrupte
}

private Process followLogs(String container) throws IOException, InterruptedException {
if (version().greaterThanOrEqualTo(VERSION_1_7_0)) {
if (version().isHigherThanOrEquivalentTo(VERSION_1_7_0)) {
return rawExecutable.execute(true, "logs", "--no-color", "--follow", container);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public Version configuredVersion() throws IOException, InterruptedException {
String versionString = command.execute(Command.throwingOnError(), false, "-v");
Matcher matcher = VERSION_PATTERN.matcher(versionString);
Assert.state(matcher.matches(), "Unexpected output of docker -v: " + versionString);
return Version.forIntegers(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)),
return Version.of(Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2)),
Integer.parseInt(matcher.group(3)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.cloud.task.listener.TaskExecutionListenerSupport;
import org.springframework.cloud.task.listener.TaskExecutionListener;
import org.springframework.cloud.task.repository.TaskExecution;

public class ComposedTaskRunnerTaskListener extends TaskExecutionListenerSupport {
public class ComposedTaskRunnerTaskListener implements TaskExecutionListener {
private final static Logger logger = LoggerFactory.getLogger(ComposedTaskRunnerTaskListener.class);

private static Long executionId = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public Optional<String> getDockerTokenServiceUri(String registryHost, boolean di
return Optional.empty();
} catch (HttpClientErrorException httpError) {

if (httpError.getRawStatusCode() != 401) {
if (httpError.getStatusCode().value() != 401) {
return Optional.empty();
}
if (httpError.getResponseHeaders() == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,8 @@ public class TaskTemplate implements TaskOperations {

private static final String DEFINITION_RELATION = "tasks/definitions/definition";

private static final String EXECUTIONS_CURRENT_RELATION_VERSION = "1.7.0";
private static final String VALIDATION_MIN_VERSION = "3.0.0-SNAPSHOT";

private static final String VALIDATION_RELATION_VERSION = "1.7.0";
private static final String VALIDATION_THIN_TASK_VERSION = "2.11.3";
private static final String EXECUTIONS_RELATION = "tasks/executions";

private static final String THIN_EXECUTIONS_RELATION = "tasks/thinexecutions";
Expand Down Expand Up @@ -135,50 +133,37 @@ public class TaskTemplate implements TaskOperations {
EXECUTIONS_INFO_RELATION,
PLATFORM_LIST_RELATION,
RETRIEVE_LOG,
VALIDATION_REL
VALIDATION_REL,
EXECUTIONS_CURRENT_RELATION,
THIN_EXECUTIONS_RELATION,
THIN_EXECUTIONS_BY_NAME_RELATION,
EXECUTION_LAUNCH_RELATION
).forEach(relation -> {
Assert.isTrue(resources.getLink(relation).isPresent(), () -> relation + " relation is required");
});
this.dataFlowServerVersion = dataFlowServerVersion;
this.restTemplate = restTemplate;

String version = VersionUtils.getThreePartVersion(dataFlowServerVersion);
if (VersionUtils.isDataFlowServerVersionGreaterThanOrEqualToRequiredVersion(version, VALIDATION_RELATION_VERSION)) {
Assert.notNull(resources.getLink(VALIDATION_REL), ()-> VALIDATION_REL + " relation is required");
if (StringUtils.hasText(version)) {
Assert.isTrue(
VersionUtils.isDataFlowServerVersionGreaterThanOrEqualToRequiredVersion(version,
VALIDATION_MIN_VERSION),
() -> "Minimum Data Flow version required is " + VALIDATION_MIN_VERSION + " but got " + version);
}
if (VersionUtils.isDataFlowServerVersionGreaterThanOrEqualToRequiredVersion(version, EXECUTIONS_CURRENT_RELATION_VERSION)) {
Assert.isTrue(resources.getLink(EXECUTIONS_CURRENT_RELATION).isPresent(), ()-> EXECUTIONS_CURRENT_RELATION + " relation is required");
this.executionsCurrentLink = resources.getLink(EXECUTIONS_CURRENT_RELATION).get();
} else {
this.executionsCurrentLink = null;
}

this.executionsCurrentLink = resources.getLink(EXECUTIONS_CURRENT_RELATION).get();
this.aboutLink = resources.getLink("about").get();

this.definitionsLink = resources.getLink(DEFINITIONS_RELATION).get();
this.definitionLink = resources.getLink(DEFINITION_RELATION).get();
this.executionsLink = resources.getLink(EXECUTIONS_RELATION).get();
this.executionLink = resources.getLink(EXECUTION_RELATION).get();
if(resources.getLink(THIN_EXECUTIONS_RELATION).isPresent()) {
this.thinExecutionsLink = resources.getLink(THIN_EXECUTIONS_RELATION).get();
} else {
this.thinExecutionsLink = null;
}
if(resources.getLink(THIN_EXECUTIONS_BY_NAME_RELATION).isPresent()) {
this.thinExecutionsByNameLink = resources.getLink(THIN_EXECUTIONS_BY_NAME_RELATION).get();
} else {
this.thinExecutionsByNameLink = null;
}

if(resources.getLink(EXECUTION_LAUNCH_RELATION).isPresent()) {
this.executionLaunchLink = resources.getLink(EXECUTION_LAUNCH_RELATION).get();
} else {
this.executionLaunchLink = null;
}
this.thinExecutionsLink = resources.getLink(THIN_EXECUTIONS_RELATION).get();
this.thinExecutionsByNameLink = resources.getLink(THIN_EXECUTIONS_BY_NAME_RELATION).get();
this.executionLaunchLink = resources.getLink(EXECUTION_LAUNCH_RELATION).get();
this.executionByNameLink = resources.getLink(EXECUTION_RELATION_BY_NAME).get();
if (resources.getLink(EXECUTIONS_INFO_RELATION).isPresent()) {
this.executionsInfoLink = resources.getLink(EXECUTIONS_INFO_RELATION).get();
}
this.executionsInfoLink = resources.getLink(EXECUTIONS_INFO_RELATION).get();
this.validationLink = resources.getLink(VALIDATION_REL).get();
this.platformListLink = resources.getLink(PLATFORM_LIST_RELATION).get();
this.retrieveLogLink = resources.getLink(RETRIEVE_LOG).get();
Expand Down Expand Up @@ -207,39 +192,16 @@ public TaskDefinitionResource create(String name, String definition, String desc
return restTemplate.postForObject(definitionsLink.expand().getHref(), values,
TaskDefinitionResource.class);
}
private boolean isNewServer() {
if(this.actualDataFlowServerCoreVersion == null) {
AboutResource aboutResource = restTemplate.getForObject(aboutLink.expand().getHref(), AboutResource.class);
Assert.notNull(aboutResource, "Expected about");
this.actualDataFlowServerCoreVersion = aboutResource.getVersionInfo().getCore().getVersion();
}
String v2_11_0 = VersionUtils.getThreePartVersion("2.11.0-SNAPSHOT");
String serverVersion = VersionUtils.getThreePartVersion(this.actualDataFlowServerCoreVersion);
return VersionUtils.isDataFlowServerVersionGreaterThanOrEqualToRequiredVersion(serverVersion, v2_11_0);
}

@Override
public LaunchResponseResource launch(String name, Map<String, String> properties, List<String> arguments) {
MultiValueMap<String, Object> values = new LinkedMultiValueMap<>();
String formattedProperties = DeploymentPropertiesUtils.format(properties);
String commandLineArguments = StringUtils.collectionToDelimitedString(arguments, " ");
values.add("properties", formattedProperties);
values.add("arguments", commandLineArguments);
if(isNewServer()) {
Assert.notNull(executionLaunchLink, "This version of SCDF doesn't support tasks/executions/launch");
values.add("name", name);
String url = executionLaunchLink.expand(name).getHref();
values.remove("name");
return restTemplate.postForObject(url, values, LaunchResponseResource.class);
} else {
Long id = restTemplate.postForObject(executionByNameLink.expand(name).getHref(), values, Long.class, name);
if(id != null) {
LaunchResponseResource response = new LaunchResponseResource();
response.setExecutionId(id);
return response;
} else {
throw new RuntimeException("Expected id");
}
}
String url = executionLaunchLink.expand(name).getHref();
return restTemplate.postForObject(url, values, LaunchResponseResource.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.web.client.RestTemplate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;

/**
Expand All @@ -46,22 +47,19 @@ class TaskTemplateTests {
void setup() {
restTemplate = mock(RestTemplate.class);
}

@Test
void oldDataFlow() {
validateExecutionLinkNotPresent("1.6.0");
void invalidVersion() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> validateExecutionLinkPresent("2.11.5"));
}

@Test
void minDataFlow() {
validateExecutionLinkPresent("1.7.0");
validateExecutionLinkPresent("3.0.0");
}

@Test
void futureDataFlow() {
validateExecutionLinkPresent("1.8.0");
validateExecutionLinkPresent("1.9.0");
validateExecutionLinkPresent("2.0.0");
validateExecutionLinkPresent("3.0.0");
}


Expand All @@ -71,12 +69,6 @@ private void validateExecutionLinkPresent(String dataFlowVersion) {
assertThat(testResource.isLinkRequested(CURRENT_TASK_EXECUTION_LINK)).isTrue();
}

private void validateExecutionLinkNotPresent(String version) {
TestResource testResource = new TestResource();
new TaskTemplate(this.restTemplate, testResource, version);
assertThat(testResource.isLinkRequested(CURRENT_TASK_EXECUTION_LINK)).isFalse();
}

public static class TestResource extends RepresentationModel<TestResource> {

private final Map<String, Long> linksRequested = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.io.HttpClientResponseHandler;
import org.junit.jupiter.api.Test;

import org.springframework.boot.SpringBootConfiguration;
Expand Down Expand Up @@ -61,14 +62,12 @@ void resourceBasedAuthorizationHeader() throws Exception {
throw new Passed();
})
.buildHttpClient()) {
assertThatExceptionOfType(Passed.class).isThrownBy(() -> client.execute(new HttpGet(targetHost)));
assertThatExceptionOfType(Passed.class).isThrownBy(() -> client.execute(new HttpGet(targetHost), getNoOpResponseHandler()));
}
}

static final class TestException extends IOException {
TestException() {
super("It broke");
}
private HttpClientResponseHandler<String> getNoOpResponseHandler() {
return response -> "noOp";
}

static final class ByteArrayCheckableResource extends ByteArrayResource implements CheckableResource {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.springframework.hateoas.server.mvc.RepresentationModelAssemblerSupport;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down Expand Up @@ -168,7 +167,7 @@ public void save(
) {
Map<String, String> propertiesToUse = DeploymentPropertiesUtils.parse(properties);
List<String> argumentsToUse = DeploymentPropertiesUtils.parseArgumentList(arguments, " ");
this.schedulerService.schedule(StringUtils.trimWhitespace(scheduleName), taskDefinitionName,
this.schedulerService.schedule(scheduleName.strip(), taskDefinitionName,
propertiesToUse, argumentsToUse, platform);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.dataflow.core.DataFlowPropertyKeys;
import org.springframework.util.Assert;

/**
* Properties used to define the behavior of the composed task runner.
Expand All @@ -44,7 +45,7 @@ public class ComposedTaskRunnerConfigurationProperties {
* If true SCDF will set the dataflow-server-access-token for the composed
* task runner to the user's token when launching composed tasks.
*/
private Boolean useUserAccessToken;
private Boolean useUserAccessToken = false;

public String getUri() {
return uri;
Expand All @@ -67,6 +68,7 @@ public Boolean isUseUserAccessToken() {
}

public void setUseUserAccessToken(Boolean useUserAccessToken) {
Assert.notNull(useUserAccessToken, "'useUserAccessToken' cannot be null");
this.useUserAccessToken = useUserAccessToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,21 +93,21 @@ void serverDoesNotStartWhenUrlIsH2ButInvalidForm() {
"spring.datasource.url=jdbc:h2:tcp://localhost:-1/mem:dataflow;DATABASE_TO_UPPER=FALSE",
"spring.dataflow.embedded.database.enabled=true")
.run(context -> assertThat(context)
.getFailure().getRootCause().isInstanceOf(IllegalArgumentException.class)
.getFailure().rootCause().isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching("DataSource URL .* does not match regex pattern: .*"));

runner.withPropertyValues(
"spring.datasource.url=jdbc:h2:tcp://localhost:port/mem:dataflow;DATABASE_TO_UPPER=FALSE",
"spring.dataflow.embedded.database.enabled=true")
.run(context -> assertThat(context)
.getFailure().getRootCause().isInstanceOf(IllegalArgumentException.class)
.getFailure().rootCause().isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching("DataSource URL .* does not match regex pattern: .*"));

runner.withPropertyValues(
"spring.datasource.url=jdbc:h2:tcp://localhost:99999/mem:dataflow;DATABASE_TO_UPPER=FALSE",
"spring.dataflow.embedded.database.enabled=true")
.run(context -> assertThat(context)
.getFailure().getRootCause().isInstanceOf(IllegalArgumentException.class)
.getFailure().rootCause().isInstanceOf(IllegalArgumentException.class)
.hasMessage("Port value out of range: 99999"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ void getAllThinExecutions() throws Exception {
}
@Test
void getThinExecutionsByName() throws Exception {
mockMvc.perform(get("/tasks/thinexecutions").queryParam("name", TASK_NAME_ORIG).accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.taskExecutionThinResourceList[*].executionId", containsInAnyOrder(2, 1)))
.andExpect(jsonPath("$._embedded.taskExecutionThinResourceList[*].parentExecutionId", containsInAnyOrder(null, 1)))
.andExpect(jsonPath("$._embedded.taskExecutionThinResourceList[*].taskExecutionStatus", containsInAnyOrder("RUNNING", "RUNNING")))
.andExpect(jsonPath("$._embedded.taskExecutionThinResourceList", hasSize(2)));
mockMvc.perform(get("/tasks/thinexecutions").queryParam("name", "nope").accept(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.page.totalElements", is(0)));
mockMvc.perform(get("/tasks/thinexecutions").queryParam("name", "none").accept(MediaType.APPLICATION_JSON))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

public class ComposedTaskRunnerConfigurationPropertiesTests {

Expand All @@ -36,14 +38,23 @@ void useUserAccessTokenFromCTRPropFalse() {
ComposedTaskRunnerConfigurationProperties composedTaskRunnerConfigurationProperties =
new ComposedTaskRunnerConfigurationProperties();
composedTaskRunnerConfigurationProperties.setUseUserAccessToken(false);
assertThat(composedTaskRunnerConfigurationProperties.isUseUserAccessToken()).as("Use user access token should be false").isFalse();
assertThat(composedTaskRunnerConfigurationProperties.isUseUserAccessToken()).isFalse();
}

@Test
void useUserAccessTokenFromCTRPropNotSet() {
ComposedTaskRunnerConfigurationProperties composedTaskRunnerConfigurationProperties =
new ComposedTaskRunnerConfigurationProperties();
assertThat(composedTaskRunnerConfigurationProperties.isUseUserAccessToken()).as("Use user access token should be false").isNull();
assertThat(composedTaskRunnerConfigurationProperties.isUseUserAccessToken()).isFalse();
}

@Test
void setUserAccessTokenFromCTRToNull() {
ComposedTaskRunnerConfigurationProperties composedTaskRunnerConfigurationProperties =
new ComposedTaskRunnerConfigurationProperties();
assertThatIllegalArgumentException().isThrownBy(() -> composedTaskRunnerConfigurationProperties.setUseUserAccessToken(null)).
withMessageContaining("'useUserAccessToken' cannot be null")
;
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ public interface DeriveType {
}

public static int loadCSV(String tableName, DataSource dataSource, Resource cvsResource, DeriveType deriveType) throws IOException {
CSVParser parser = CSVFormat.RFC4180.withFirstRecordAsHeader()
CSVParser parser = CSVFormat.RFC4180.builder()
.setHeader()
.setSkipHeaderRecord(true)
.build()
.parse(new InputStreamReader(cvsResource.getInputStream()));
List<String> headerNames = parser.getHeaderNames();
final List<CSVRecord> records = parser.getRecords();
Expand Down

0 comments on commit 5ccfdf6

Please sign in to comment.