Skip to content

Commit

Permalink
JAVA-8886 refactor ContrastSDK initiliazation
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianPhillips2020 committed Dec 19, 2024
1 parent 5ae7fc3 commit 0cf6610
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package com.contrastsecurity.gradle.plugin;

import com.contrastsecurity.gradle.plugin.extensions.ContrastConfigurationExtension;
import com.contrastsecurity.sdk.ContrastSDK;
import com.contrastsecurity.sdk.UserAgentProduct;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.provider.Provider;

/**
* Gradle plugin for contrast utilities. The goals for this plugin are defined here <a
* href=https://contrast.atlassian.net/browse/JAVA-8252>JAVA-8252</a>
*/
public class ContrastGradlePlugin implements Plugin<Project> {

ContrastConfigurationExtension extension;

public void apply(final Project target) {

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

//initialize instance of the ContrastSDK
ContrastSDKService.initializeSdk(extension.getUsername(), extension.getServiceKey(), extension.getApiKey(), extension.getApiUrl());

target.getTasks().register("installAgent", InstallAgentTask.class);
target.getTasks().register("resolveAgent", ResolveAgentTask.class);

}

public static final String EXTENSION_NAME = "contrastConfiguration";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.contrastsecurity.gradle.plugin;

import com.contrastsecurity.sdk.ContrastSDK;
import com.contrastsecurity.sdk.UserAgentProduct;

public class ContrastSDKService {

private static ContrastSDK INSTANCE = null;

public static void initializeSdk(
final String username, final String serviceKey, final String apiKey, final String apiUrl) {
final UserAgentProduct gradle = UserAgentProduct.of("contrast-gradle-plugin");
INSTANCE =
new ContrastSDK.Builder(username, serviceKey, apiKey)
.withApiUrl(apiUrl + "/api")
// TODO JAVA-8883 figure out how to define this proxy
// .withProxy(proxy) //with proxy?
.withUserAgentProduct(gradle)
.build();
}

public static ContrastSDK getSdk() {
return INSTANCE;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import static com.contrastsecurity.gradle.plugin.ContrastGradlePlugin.EXTENSION_NAME;

import com.contrastsecurity.gradle.plugin.extensions.ContrastConfigurationExtension;
import com.contrastsecurity.models.AgentType;
import com.contrastsecurity.sdk.ContrastSDK;
import com.contrastsecurity.sdk.UserAgentProduct;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
Expand Down Expand Up @@ -37,7 +37,7 @@ void installAgent() {

logger.debug("Running installAgentTask");
// create sdk object for connecting to Contrast
final ContrastSDK sdk = connectToContrast();
final ContrastSDK sdk = ContrastSDKService.getSdk();

logger.debug("Connected to Contrast at: " + sdk.getRestApiURL());

Expand Down Expand Up @@ -197,18 +197,6 @@ public static Path retrieveAgent(
return downloadedAgent;
}

/** Create ContrastSDK for connecting to TeamServer */
private ContrastSDK connectToContrast() {
// TODO get plugin version for this as well
final UserAgentProduct gradle = UserAgentProduct.of("contrast-gradle-plugin");
return new ContrastSDK.Builder(config.getUsername(), config.getServiceKey(), config.getApiKey())
.withApiUrl(config.getApiUrl() + "/api")
// TODO JAVA-8883 figure out how to define this proxy
// .withProxy(proxy) //with proxy?
.withUserAgentProduct(gradle)
.build();
}

private static final String AGENT_NAME = "contrast.jar";
final Logger logger = getProject().getLogger();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.contrastsecurity.gradle.plugin;

import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;

public class ResolveAgentTask extends DefaultTask {

@TaskAction
void resolveAgent() {
System.out.println("Resolved Agent");
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.contrastsecurity.gradle.plugin;
package com.contrastsecurity.gradle.plugin.extensions;

/** Extension for configuring TeamServer API Credentials for downloading agent */
public class ContrastConfigurationExtension {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ void pluginRegistersATask() {

// Verify the result
assertNotNull(project.getTasks().findByName("installAgent"));
assertNotNull(project.getTasks().findByName("resolveAgent"));
}
}
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 0cf6610

Please sign in to comment.