-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JAVA-8886 fix config and configure task inputs
- Loading branch information
1 parent
0cf6610
commit c433217
Showing
5 changed files
with
148 additions
and
26 deletions.
There are no files selected for viewing
26 changes: 13 additions & 13 deletions
26
gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/ContrastGradlePlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 21 additions & 7 deletions
28
gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/ContrastSDKService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,40 @@ | ||
package com.contrastsecurity.gradle.plugin; | ||
|
||
import com.contrastsecurity.gradle.plugin.extensions.ContrastConfigurationExtension; | ||
import com.contrastsecurity.sdk.ContrastSDK; | ||
import com.contrastsecurity.sdk.UserAgentProduct; | ||
|
||
public class ContrastSDKService { | ||
|
||
private static ContrastSDK INSTANCE = null; | ||
public static ContrastSDK INSTANCE; | ||
|
||
public static void initializeSdk( | ||
final String username, final String serviceKey, final String apiKey, final String apiUrl) { | ||
public static void initializeSdk(final ContrastConfigurationExtension extension) { | ||
if (extension.getUsername() == null | ||
|| extension.getServiceKey() == null | ||
|| extension.getApiKey() == null) { | ||
INSTANCE = NOOP; | ||
return; | ||
} | ||
final UserAgentProduct gradle = UserAgentProduct.of("contrast-gradle-plugin"); | ||
INSTANCE = | ||
new ContrastSDK.Builder(username, serviceKey, apiKey) | ||
.withApiUrl(apiUrl + "/api") | ||
final String url = extension.getApiUrl() == null ? DEFAULT_URL : extension.getApiUrl() + "/api"; | ||
INSTANCE = | ||
new ContrastSDK.Builder( | ||
extension.getUsername(), extension.getServiceKey(), extension.getApiKey()) | ||
.withApiUrl(url) | ||
// TODO JAVA-8883 figure out how to define this proxy | ||
// .withProxy(proxy) //with proxy? | ||
.withUserAgentProduct(gradle) | ||
.build(); | ||
} | ||
|
||
public static ContrastSDK getSdk() { | ||
public static ContrastSDK getInstance(ContrastConfigurationExtension extension) { | ||
if (INSTANCE == null) { | ||
initializeSdk(extension); | ||
} | ||
return INSTANCE; | ||
} | ||
|
||
static final String DEFAULT_URL = "https://app.contrastsecurity.com/Contrast/api"; | ||
public static final ContrastSDK NOOP = | ||
new ContrastSDK.Builder("noop", "noop", "noop").withApiUrl(DEFAULT_URL).build(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 97 additions & 3 deletions
100
gradle-plugin/src/main/java/com/contrastsecurity/gradle/plugin/ResolveAgentTask.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,106 @@ | ||
package com.contrastsecurity.gradle.plugin; | ||
|
||
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 java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.FileAlreadyExistsException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import org.gradle.api.DefaultTask; | ||
import org.gradle.api.GradleException; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.file.RegularFileProperty; | ||
import org.gradle.api.logging.Logger; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.TaskAction; | ||
import org.jetbrains.annotations.VisibleForTesting; | ||
|
||
public class ResolveAgentTask extends DefaultTask { | ||
|
||
@TaskAction | ||
void resolveAgent() { | ||
System.out.println("Resolved Agent"); | ||
final ContrastConfigurationExtension config = | ||
(ContrastConfigurationExtension) getProject().getExtensions().getByName(EXTENSION_NAME); | ||
|
||
@OutputFile | ||
RegularFileProperty agentFile = getProject().getObjects().fileProperty(); | ||
|
||
RegularFileProperty getAgentFile(){ | ||
return agentFile; | ||
} | ||
|
||
private 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()); | ||
} | ||
|
||
|
||
|
||
/** Use ContrastSDK to download agent and return the path where agent jar is stored */ | ||
@VisibleForTesting | ||
public static Path retrieveAgent( | ||
final ContrastSDK connection, | ||
final String jarPath, | ||
final String uuid, | ||
final Project project) { | ||
|
||
final Logger logger = project.getLogger(); | ||
// Initially attempt to get agent from the previously configured location | ||
if (jarPath != null) { | ||
final Path agent = Paths.get(jarPath); | ||
if (!Files.exists(agent)) { | ||
throw new GradleException("Unable to find java agent at " + jarPath); | ||
} | ||
logger.debug("Agent provided via configuration retrieved"); | ||
return agent; | ||
} | ||
|
||
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; | ||
} | ||
|
||
logger.debug("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); | ||
|
||
// Save the jar to the 'target' directory | ||
final Path target = Paths.get(project.getProjectDir().getPath()); | ||
|
||
try { | ||
Files.createFile(target); | ||
} catch (FileAlreadyExistsException e) { | ||
logger.debug("Project dir already exists"); | ||
} | ||
|
||
downloadedAgent = target.resolve(AGENT_NAME); | ||
|
||
Files.write(downloadedAgent, bytes, StandardOpenOption.CREATE, StandardOpenOption.WRITE); | ||
|
||
} catch (RuntimeException | IOException e) { | ||
throw new GradleException("Failed to download java agent from the Contrast api: " + e); | ||
} | ||
|
||
logger.debug("Agent retrieved from TeamServer"); | ||
return downloadedAgent; | ||
} | ||
|
||
private static final String AGENT_NAME = "contrast.jar"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters