Skip to content

Commit

Permalink
JAVA-3738 update readme and change exception handling for retrieving …
Browse files Browse the repository at this point in the history
…agent
  • Loading branch information
BrianPhillips2020 committed Dec 16, 2024
1 parent cea9c10 commit a217eab
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 44 deletions.
28 changes: 16 additions & 12 deletions gradle-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -50,19 +53,20 @@ 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=<your-user-name>
export CONTRAST__API__API_KEY=<your-api-key>
export CONTRAST__API__SERVICE_KEY=<your-service-key>
export CONTRAST__API__ORGANIZATION_ID=<your-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
```
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -93,7 +93,8 @@ public static Collection<String> createContrastArgs(
final String serverName,
String appVersion) {

final Collection<String> args = new HashSet<String>();
// List to preserve ordering of arguments
final Collection<String> args = new LinkedList<>();

args.add("-javaagent:" + agentPath.toAbsolutePath());

Expand All @@ -105,9 +106,6 @@ public static Collection<String> 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);
Expand Down Expand Up @@ -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)) {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> actualArgs =
InstallAgentTask.createContrastArgs("name", Path.of("/test/path"), "foo", "bar", "0.0.1");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

0 comments on commit a217eab

Please sign in to comment.