Skip to content

Commit

Permalink
JAVA-8886 🚧 not working input/output for tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianPhillips2020 committed Dec 20, 2024
1 parent c433217 commit f5bb67b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.contrastsecurity.gradle.plugin;

import com.contrastsecurity.gradle.plugin.extensions.ContrastConfigurationExtension;
import java.io.File;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskProvider;
Expand All @@ -13,18 +14,28 @@ public class ContrastGradlePlugin implements Plugin<Project> {

public void apply(final Project target) {

target.getExtensions().create(EXTENSION_NAME, ContrastConfigurationExtension.class);
ContrastConfigurationExtension extension =
target.getExtensions().create(EXTENSION_NAME, ContrastConfigurationExtension.class);

TaskProvider<ResolveAgentTask> resolveAgentTask = target.getTasks().register("resolveAgent", ResolveAgentTask.class);
TaskProvider<ResolveAgentTask> resolveAgentTask =
target
.getTasks()
.register(
"resolveAgent",
ResolveAgentTask.class,
task -> {
task.setAgentFile(new File(target.getProjectDir() + "/agent.jar"));
});
target
.getTasks()
.register("installAgent", InstallAgentTask.class)
.configure(task -> {
//Ensure resolve agent task runs before attempting to install the agent
task.dependsOn(ResolveAgentTask.class);
//set input for the agent file as the output from the resolve agent task
task.getInputFile().set(resolveAgentTask.flatMap(ResolveAgentTask::getAgentFile));
});
.configure(
task -> {
// Ensure resolve agent task runs before attempting to install the agent
task.dependsOn(resolveAgentTask);
// set input for the agent file as the output from the resolve agent task
task.getInputFile().set(resolveAgentTask.flatMap(ResolveAgentTask::getAgentFile));
});
}

public static final String EXTENSION_NAME = "contrastConfiguration";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import com.contrastsecurity.gradle.plugin.extensions.ContrastConfigurationExtension;
import com.contrastsecurity.models.AgentType;
import com.contrastsecurity.sdk.ContrastSDK;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
Expand All @@ -22,12 +20,9 @@
import org.gradle.api.Project;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.logging.Logger;
import org.gradle.api.provider.Property;
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFile;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.testing.Test;
import org.gradle.internal.impldep.org.tomlj.internal.TomlParser;
import org.jetbrains.annotations.VisibleForTesting;

/**
Expand All @@ -39,14 +34,13 @@ public class InstallAgentTask extends DefaultTask {
final ContrastConfigurationExtension config =
(ContrastConfigurationExtension) getProject().getExtensions().getByName(EXTENSION_NAME);

@InputFile
final RegularFileProperty agent;
@InputFile final RegularFileProperty agent = getProject().getObjects().fileProperty();

public RegularFileProperty getInputFile(){
return agent;
public RegularFileProperty getInputFile() {
return agent;
}

@TaskAction
@TaskAction
void installAgent() {

logger.debug("Running installAgentTask");
Expand All @@ -56,10 +50,16 @@ void installAgent() {
logger.debug("Connected to Contrast at: " + sdk.getRestApiURL());

// get agent, either from configured jar path or from TS
final Path agent = retrieveAgent(sdk, config.getJarPath(), config.getOrgUuid(), getProject());
// final Path agent = retrieveAgent(sdk, config.getJarPath(), config.getOrgUuid(),
// getProject());

Path agentPath = getInputFile().get().getAsFile().toPath();
if (!Files.exists(agentPath)) {
throw new GradleException("Java Agent not found for installAgent task");
}
logger.debug("preparing to attach agent");
attachAgentToTasks(agent.toAbsolutePath());
// attachAgentToTasks(agent.toAbsolutePath());
attachAgentToTasks(agentPath);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.contrastsecurity.gradle.plugin.extensions.ContrastConfigurationExtension;
import com.contrastsecurity.models.AgentType;
import com.contrastsecurity.sdk.ContrastSDK;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
Expand All @@ -27,29 +26,25 @@ public class ResolveAgentTask extends DefaultTask {
final ContrastConfigurationExtension config =
(ContrastConfigurationExtension) getProject().getExtensions().getByName(EXTENSION_NAME);

@OutputFile
RegularFileProperty agentFile = getProject().getObjects().fileProperty();
@OutputFile RegularFileProperty agentFile = getProject().getObjects().fileProperty();

RegularFileProperty getAgentFile(){
return agentFile;
public RegularFileProperty getAgentFile() {
return agentFile;
}

private void setAgentFile(final File file){
this.agentFile.set(file);
public void setAgentFile(final File file) {
this.agentFile.set(file);
}

@TaskAction
void resolveAgent() {
final ContrastSDK sdk = ContrastSDKService.getInstance(config);

retrieveAgent(sdk, config.getJarPath(), config.getOrgUuid(), getProject());
setAgentFile(retrieveAgent(sdk, config.getJarPath(), config.getOrgUuid(), getProject()));
}



/** Use ContrastSDK to download agent and return the path where agent jar is stored */
@VisibleForTesting
public static Path retrieveAgent(
public static File retrieveAgent(
final ContrastSDK connection,
final String jarPath,
final String uuid,
Expand All @@ -63,15 +58,15 @@ public static Path retrieveAgent(
throw new GradleException("Unable to find java agent at " + jarPath);
}
logger.debug("Agent provided via configuration retrieved");
return agent;
return new File(agent.toString());
}

logger.debug("No agent path provided, checking for cached agent");

final Path agent = Paths.get(project.getProjectDir().getPath()).resolve(AGENT_NAME);
if (Files.exists(agent)) {
System.out.println("Agent jar found at " + project.getProjectDir().getPath());
return agent;
return new File(agent.toString());
}

logger.debug("Attempting to retrieve agent from TeamServer");
Expand Down Expand Up @@ -99,8 +94,9 @@ public static Path retrieveAgent(
}

logger.debug("Agent retrieved from TeamServer");
return downloadedAgent;
return new File(downloadedAgent.toString());
}

private static final String AGENT_NAME = "contrast.jar";
private final Logger logger = getProject().getLogger();
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void verify_attaches_agent_to_tests() throws IOException {
testRunner.forwardOutput();
testRunner.withPluginClasspath();
// outputs debug logs to stdout for testing
testRunner.withArguments("installAgent", "--debug");
testRunner.withArguments("installAgent");
testRunner.withDebug(true);
testRunner.withProjectDir(projectDir);
final BuildResult result = testRunner.build();
Expand Down

0 comments on commit f5bb67b

Please sign in to comment.