diff --git a/gradle-plugin/README.md b/gradle-plugin/README.md index 9635d1e..9c23bc6 100644 --- a/gradle-plugin/README.md +++ b/gradle-plugin/README.md @@ -7,15 +7,18 @@ Requires Java 21 to build Use `./gradlew build` to build the plugin -## Publishing to MavenLocal -To publish this plugin to your mavenLocal apply the `maven-publish` plugin to this project's `build.gradle` file and run: - ```shell ./gradlew publishToMavenLocal ``` -## Configuration + +## Tasks +The `installAgent` task takes in your configuration as defined by the `contrastConfiguration` block and attaches the java agent to all Test tasks for your project. +If no Agent is provided, the plugin will attempt to download the current Java Agent available on TeamServer, at the endpoint provided in the configuration. + + +## Configuration This plugin is configured via the `contrastConfiguration` block in your projects `gradle.build` script ```shell contrastConfiguration{ @@ -50,8 +53,12 @@ tasks.named("test").configure { TODO auto attach to tests ## Developement -### Testing +### Publishing to MavenLocal +To publish this plugin to your mavenLocal apply the `maven-publish` plugin to this project's `build.gradle` file and run: In order to run the plugin's end-to-end tests, you must configure these variables in your environment + + +### End to End testing ```shell export CONTRAST__API__URL=https://app.contrastsecurity.com/Contrast ##Use your standard endpoint for the org, the plugin will apply `/api` for the restapi functionality export CONTRAST__API__USER_NAME= @@ -59,10 +66,7 @@ export CONTRAST__API__API_KEY= export CONTRAST__API__SERVICE_KEY= export CONTRAST__API__ORGANIZATION_ID= ``` -TODO: have end-to-end tests be turned off unless a certain property is enabled - - - -## Tasks -The `installAgent` task takes in your configuration as defined by the `contrastConfiguration` block and attaches the java agent to all Test tasks for your project. -If no Agent is provided, the plugin will attempt to download the current Java Agent available on TeamServer, at the endpoint provided in the configuration. +To enable end-to-end testing, these variables must be present and you must use the property `e2e` +```shell +./gradkew test -Pe2e +``` diff --git a/gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/InstallAgentTask.java b/gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/InstallAgentTask.java index 4ce1156..9791fa2 100644 --- a/gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/InstallAgentTask.java +++ b/gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/InstallAgentTask.java @@ -2,7 +2,6 @@ import static com.contrastsecurity.gradle.plugin.ContrastGradlePlugin.EXTENSION_NAME; -import com.contrastsecurity.exceptions.UnauthorizedException; import com.contrastsecurity.models.AgentType; import com.contrastsecurity.sdk.ContrastSDK; import com.contrastsecurity.sdk.UserAgentProduct; @@ -15,8 +14,9 @@ import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Date; -import java.util.HashSet; +import java.util.LinkedList; import org.gradle.api.DefaultTask; +import org.gradle.api.GradleException; import org.gradle.api.Project; import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.testing.Test; @@ -93,7 +93,8 @@ public static Collection createContrastArgs( final String serverName, String appVersion) { - final Collection args = new HashSet(); + // List to preserve ordering of arguments + final Collection args = new LinkedList<>(); args.add("-javaagent:" + agentPath.toAbsolutePath()); @@ -105,9 +106,6 @@ public static Collection createContrastArgs( } args.add("-Dcontrast.server=" + serverName); - args.add("-Dcontrast.env=qa"); - // TODO this may be unnecessary, need to figure out why agent fails to retrieve server settings - args.add("-Dcontrast.assess.enable=true"); if (appVersion == null) { appVersion = computeAppVersion(appName); @@ -152,13 +150,13 @@ public static Path retrieveAgent( if (jarPath != null) { final Path agent = Paths.get(jarPath); if (!Files.exists(agent)) { - throw new RuntimeException("Unable to find java agent at " + jarPath); + throw new GradleException("Unable to find java agent at " + jarPath); } System.out.println("Agent provided via configuration retrieved"); return agent; } - System.out.println("No agent path provided or agent does not exist, checking for cached agent"); + System.out.println("No agent path provided, checking for cached agent"); final Path agent = Paths.get(project.getProjectDir().getPath()).resolve(AGENT_NAME); if (Files.exists(agent)) { @@ -169,38 +167,31 @@ public static Path retrieveAgent( System.out.println("Attempting to retrieve agent from TeamServer"); // If no jar is provided, and no jarpath configured, attempt to retrieve the agent from TS final byte[] bytes; + Path downloadedAgent; try { bytes = connection.getAgent(AgentType.JAVA, uuid); - } catch (IOException e) { - throw new RuntimeException("Failed to retrieve Contrast Java Agent: " + e); - } catch (UnauthorizedException e) { - throw new RuntimeException( - "\nWe contacted Contrast successfully but couldn't authorize with the credentials you provided. The error is:", - e); - } - // Save the jar to the 'target' directory - final Path target = Paths.get(project.getProjectDir().getPath()); - try { - Files.createFile(target); - } catch (final FileAlreadyExistsException e) { - System.out.println("Project directory already exists"); - } catch (final IOException e) { - throw new RuntimeException("Unable to create directory " + target, e); - } + // Save the jar to the 'target' directory + final Path target = Paths.get(project.getProjectDir().getPath()); + + try { + Files.createFile(target); + } catch (FileAlreadyExistsException e) { + System.out.println("Project dir already exists"); + } + + downloadedAgent = target.resolve(AGENT_NAME); - final Path downloadedAgent = target.resolve(AGENT_NAME); - try { Files.write(downloadedAgent, bytes, StandardOpenOption.CREATE, StandardOpenOption.WRITE); - } catch (final IOException e) { - throw new RuntimeException("Unable to save the latest java agent.", e); + + } catch (RuntimeException | IOException e) { + throw new GradleException("Failed to download java agent from the Contrast api: " + e); } + System.out.println("Agent retrieved from TeamServer"); return downloadedAgent; } - /** TODO Use ContrastSDK to download agent creds for running the agent */ - private void downloadAgentCredentials(final ContrastSDK connection) {} /** Create ContrastSDK for connecting to TeamServer */ private ContrastSDK connectToContrast() { diff --git a/gradle-plugin/src/test/java/com/contrastsecurity/gradle/plugin/InstallAgentTests.java b/gradle-plugin/src/test/java/com/contrastsecurity/gradle/plugin/InstallAgentTests.java index ce25d51..1e42a67 100644 --- a/gradle-plugin/src/test/java/com/contrastsecurity/gradle/plugin/InstallAgentTests.java +++ b/gradle-plugin/src/test/java/com/contrastsecurity/gradle/plugin/InstallAgentTests.java @@ -22,7 +22,6 @@ void verify_correct_contrast_args() { "-javaagent:/test/path", "-Dcontrast.override.appname=foo", "-Dcontrast.server=bar", - "-Dcontrast.env=qa", "-Dcontrast.override.appversion=0.0.1"); final Collection actualArgs = InstallAgentTask.createContrastArgs("name", Path.of("/test/path"), "foo", "bar", "0.0.1"); diff --git a/gradle-plugin/src/test/java/com/contrastsecurity/gradle/plugin/e2e/EndToEndTests.java b/gradle-plugin/src/test/java/com/contrastsecurity/gradle/plugin/e2e/EndToEndTests.java index 90cb4c0..dee8465 100644 --- a/gradle-plugin/src/test/java/com/contrastsecurity/gradle/plugin/e2e/EndToEndTests.java +++ b/gradle-plugin/src/test/java/com/contrastsecurity/gradle/plugin/e2e/EndToEndTests.java @@ -107,7 +107,6 @@ private static String writeContrastBuildFile() { AGENT_ARGS.add("-javaagent:"); AGENT_ARGS.add("-Dcontrast.override.appname=gradle-end-to-end-test"); AGENT_ARGS.add("-Dcontrast.server=server"); - AGENT_ARGS.add("-Dcontrast.env=qa"); AGENT_ARGS.add("-Dcontrast.override.appversion=0.0.1"); } }