diff --git a/README.md b/README.md index 3886487d5..1d8facc40 100644 --- a/README.md +++ b/README.md @@ -226,6 +226,16 @@ From a job specific configuration page: | Custom tags | Set from a `File` in the job workspace (not compatible with pipeline jobs) or as text `Properties` directly from the configuration page. If set, this overrides the `Global Job Tags` configuration. | | Send source control management events | Submits the `Source Control Management Events Type` of events and metrics (enabled by default). | +### Test Visibility Configuration + +The plugin can automatically configure Datadog Test Visibility for a job or a pipeline. + +Before enabling Test Visibility, be sure to properly configure the plugin to submit data to Datadog. + +To enable Test Visibility, go to the `Configure` page of the job or pipeline whose tests need to be traced, tick `Enable Datadog Test Visibility` checkbox in the `General` section and save your changes. + +Please bear in mind that Test Visibility is a separate Datadog product that is billed separately. + ## Data collected This plugin is collecting the following [events](#events), [metrics](#metrics), and [service checks](#service-checks): diff --git a/pom.xml b/pom.xml index 37ae20295..77a5e6e65 100644 --- a/pom.xml +++ b/pom.xml @@ -167,6 +167,11 @@ jetty-client 9.4.51.v20230217 + + org.bouncycastle + bcpg-jdk18on + 1.72 + diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration.java b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration.java index 344c95c2f..6c950b19b 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration.java @@ -214,7 +214,7 @@ private void loadEnvVariables(){ } String traceServiceNameVar = System.getenv(TARGET_TRACE_SERVICE_NAME_PROPERTY); - if(StringUtils.isNotBlank(targetApiKeyEnvVar)) { + if(StringUtils.isNotBlank(traceServiceNameVar)) { this.traceServiceName = traceServiceNameVar; } @@ -455,7 +455,6 @@ public ListBoxModel doFillTargetCredentialsApiKeyItems( .includeCurrentValue(targetCredentialsApiKey); } - /** * Tests the targetCredentialsApiKey field from the configuration screen, to check its' validity. * @@ -477,7 +476,7 @@ public FormValidation doCheckTargetCredentialsApiKey( } else { if (!item.hasPermission(Item.EXTENDED_READ) && !item.hasPermission(CredentialsProvider.USE_ITEM)) { - return FormValidation.ok(); + return FormValidation.ok(); } } if (StringUtils.isBlank(targetCredentialsApiKey)) { @@ -488,7 +487,7 @@ public FormValidation doCheckTargetCredentialsApiKey( } if (CredentialsProvider.listCredentials(StringCredentials.class, item, - ACL.SYSTEM, + ACL.SYSTEM, Collections.emptyList(), CredentialsMatchers.withId(targetCredentialsApiKey)).isEmpty()) { return FormValidation.error("Cannot find currently selected credentials"); @@ -766,6 +765,7 @@ public boolean configure(final StaplerRequest req, final JSONObject formData) th final Secret apiKeySecret = findSecret(formData.getString("targetApiKey"), formData.getString("targetCredentialsApiKey")); this.setUsedApiKey(apiKeySecret); + //When form is saved.... DatadogClient client = ClientFactory.getClient(DatadogClient.ClientType.valueOf(this.getReportWith()), this.getTargetApiURL(), this.getTargetLogIntakeURL(), this.getTargetWebhookIntakeURL(), this.getUsedApiKey(), this.getTargetHost(), diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/clients/HttpClient.java b/src/main/java/org/datadog/jenkins/plugins/datadog/clients/HttpClient.java index ad24a70a2..e72babe77 100644 --- a/src/main/java/org/datadog/jenkins/plugins/datadog/clients/HttpClient.java +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/clients/HttpClient.java @@ -1,5 +1,7 @@ package org.datadog.jenkins.plugins.datadog.clients; +import java.io.IOException; +import java.io.InputStream; import java.util.Collections; import java.util.List; import java.util.Map; @@ -10,6 +12,7 @@ import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; +import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; import java.util.logging.Logger; @@ -26,6 +29,7 @@ import org.eclipse.jetty.client.api.Result; import org.eclipse.jetty.client.util.BufferingResponseListener; import org.eclipse.jetty.client.util.BytesContentProvider; +import org.eclipse.jetty.client.util.InputStreamResponseListener; import org.eclipse.jetty.http.HttpField; import org.eclipse.jetty.http.HttpHeader; import org.eclipse.jetty.http.HttpMethod; @@ -152,7 +156,7 @@ public boolean matches(Origin origin) { return false; } } - return true; + return super.matches(origin); } }); } @@ -182,6 +186,24 @@ public T get(String url, Map headers, Function re responseParser); } + public void getBinary(String url, Map headers, Consumer responseParser) throws ExecutionException, InterruptedException, TimeoutException, IOException { + ensureClientIsUpToDate(); + + Request request = requestSupplier(url, HttpMethod.GET, headers, null, null).get(); + InputStreamResponseListener responseListener = new InputStreamResponseListener(); + request.send(responseListener); + + Response response = responseListener.get(timeoutMillis, TimeUnit.MILLISECONDS); + int responseStatus = response.getStatus(); + if (responseStatus >= 200 && responseStatus < 300) { + try (InputStream responseStream = responseListener.getInputStream()) { + responseParser.accept(responseStream); + } + } else { + throw new ResponseProcessingException("Received erroneous response " + response); + } + } + public T post(String url, Map headers, String contentType, byte[] body, Function responseParser) throws ExecutionException, InterruptedException, TimeoutException { return executeSynchronously( requestSupplier(url, HttpMethod.POST, headers, contentType, body), diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerConfigurator.java b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerConfigurator.java new file mode 100644 index 000000000..48be9c254 --- /dev/null +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerConfigurator.java @@ -0,0 +1,174 @@ +package org.datadog.jenkins.plugins.datadog.tracer; + +import hudson.FilePath; +import hudson.model.AbstractBuild; +import hudson.model.AbstractProject; +import hudson.model.InvisibleAction; +import hudson.model.Job; +import hudson.model.Node; +import hudson.model.Run; +import hudson.model.TopLevelItem; +import hudson.util.Secret; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.Collection; +import java.util.Collections; +import java.util.EnumMap; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.datadog.jenkins.plugins.datadog.DatadogClient; +import org.datadog.jenkins.plugins.datadog.DatadogGlobalConfiguration; +import org.datadog.jenkins.plugins.datadog.DatadogUtilities; + +public class DatadogTracerConfigurator { + + private static final Logger LOGGER = Logger.getLogger(DatadogTracerConfigurator.class.getName()); + + private final Map configurators; + + public static final DatadogTracerConfigurator INSTANCE = new DatadogTracerConfigurator(); + + public DatadogTracerConfigurator() { + configurators = new EnumMap<>(TracerLanguage.class); + configurators.put(TracerLanguage.JAVA, new JavaConfigurator()); + configurators.put(TracerLanguage.JAVASCRIPT, new JavascriptConfigurator()); + configurators.put(TracerLanguage.PYTHON, new PythonConfigurator()); + } + + public Map configure(Run run, Node node, Map envs) { + Job job = run.getParent(); + DatadogTracerJobProperty tracerConfig = job.getProperty(DatadogTracerJobProperty.class); + if (tracerConfig == null || !tracerConfig.isOn()) { + return Collections.emptyMap(); + } + + Collection languages = tracerConfig.getLanguages(); + for (ConfigureTracerAction action : run.getActions(ConfigureTracerAction.class)) { + if (action.node == node && languages.equals(action.languages)) { + return action.variables; + } + } + + DatadogGlobalConfiguration datadogConfig = DatadogUtilities.getDatadogGlobalDescriptor(); + if (datadogConfig == null) { + LOGGER.log(Level.WARNING, "Cannot set up tracer: Datadog config not found"); + return Collections.emptyMap(); + } + + TopLevelItem topLevelItem = getTopLevelItem(run); + FilePath workspacePath = node.getWorkspaceFor(topLevelItem); + if (workspacePath == null) { + throw new IllegalStateException("Cannot find workspace path for " + topLevelItem + " on " + node); + } + + Map variables = new HashMap<>(getCommonEnvVariables(datadogConfig, tracerConfig)); + for (TracerLanguage language : languages) { + TracerConfigurator tracerConfigurator = configurators.get(language); + if (tracerConfigurator == null) { + LOGGER.log(Level.WARNING, "Cannot find tracer configurator for " + language); + continue; + } + + try { + Map languageVariables = tracerConfigurator.configure(tracerConfig, node, workspacePath, envs); + variables.putAll(languageVariables); + } catch (Exception e) { + LOGGER.log(Level.WARNING, "Error while configuring " + language + " Datadog Tracer for run " + run + " and node " + node, e); + return Collections.emptyMap(); + } + } + run.addAction(new ConfigureTracerAction(node, languages, variables)); + return variables; + } + + private static TopLevelItem getTopLevelItem(Run run) { + if (run instanceof AbstractBuild) { + AbstractBuild build = (AbstractBuild) run; + AbstractProject project = build.getProject(); + if (project instanceof TopLevelItem) { + return (TopLevelItem) project; + } else { + throw new IllegalArgumentException("Unexpected type of project: " + project); + } + } else { + Job parent = run.getParent(); + if (parent instanceof TopLevelItem) { + return (TopLevelItem) parent; + } else { + throw new IllegalArgumentException("Unexpected type of run parent: " + parent); + } + } + } + + private static Map getCommonEnvVariables(DatadogGlobalConfiguration datadogConfig, + DatadogTracerJobProperty tracerConfig) { + Map variables = new HashMap<>(); + variables.put("DD_CIVISIBILITY_ENABLED", "true"); + variables.put("DD_ENV", "ci"); + variables.put("DD_SERVICE", tracerConfig.getServiceName()); + + DatadogClient.ClientType clientType = DatadogClient.ClientType.valueOf(datadogConfig.getReportWith()); + switch (clientType) { + case HTTP: + variables.put("DD_CIVISIBILITY_AGENTLESS_ENABLED", "true"); + variables.put("DD_SITE", getSite(datadogConfig.getTargetApiURL())); + variables.put("DD_API_KEY", Secret.toString(datadogConfig.getUsedApiKey())); + break; + case DSD: + variables.put("DD_AGENT_HOST", datadogConfig.getTargetHost()); + variables.put("DD_TRACE_AGENT_PORT", getAgentPort(datadogConfig.getTargetTraceCollectionPort())); + break; + default: + throw new IllegalArgumentException("Unexpected client type: " + clientType); + } + + Map additionalVariables = tracerConfig.getAdditionalVariables(); + if (additionalVariables != null) { + variables.putAll(additionalVariables); + } + + return variables; + } + + private static String getSite(String apiUrl) { + // what users configure for Pipelines looks like "https://api.datadoghq.com/api/" + // while what the tracer needs "datadoghq.com" + try { + URI uri = new URL(apiUrl).toURI(); + String host = uri.getHost(); + if (host == null) { + throw new IllegalArgumentException("Cannot find host in Datadog API URL: " + uri); + } + + String[] parts = host.split("\\."); + return (parts.length >= 2 ? parts[parts.length - 2] + "." : "") + parts[parts.length - 1]; + + } catch (MalformedURLException | URISyntaxException e) { + throw new IllegalArgumentException("Cannot parse Datadog API URL", e); + } + } + + private static String getAgentPort(Integer traceCollectionPort) { + if (traceCollectionPort == null) { + throw new IllegalArgumentException("Traces collection port is not set"); + } else { + return traceCollectionPort.toString(); + } + } + + private static final class ConfigureTracerAction extends InvisibleAction { + private final Node node; + private final Collection languages; + private final Map variables; + + private ConfigureTracerAction(Node node, Collection languages, Map variables) { + this.node = node; + this.languages = languages; + this.variables = variables; + } + } +} diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerEnvironmentContributor.java b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerEnvironmentContributor.java new file mode 100644 index 000000000..ef8d1332f --- /dev/null +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerEnvironmentContributor.java @@ -0,0 +1,33 @@ +package org.datadog.jenkins.plugins.datadog.tracer; + +import edu.umd.cs.findbugs.annotations.NonNull; +import hudson.EnvVars; +import hudson.Extension; +import hudson.model.EnvironmentContributor; +import hudson.model.Executor; +import hudson.model.Node; +import hudson.model.Run; +import hudson.model.TaskListener; +import java.util.Map; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; + +@Extension +public class DatadogTracerEnvironmentContributor extends EnvironmentContributor { + + @Override + public void buildEnvironmentFor(@NonNull Run run, @NonNull EnvVars envs, @NonNull TaskListener listener) { + if (run instanceof WorkflowRun) { + // Pipelines are handled by org.datadog.jenkins.plugins.datadog.tracer.DatadogTracerStepEnvironmentContributor + return; + } + + Executor executor = run.getExecutor(); + if (executor == null) { + return; + } + + Node node = executor.getOwner().getNode(); + Map additionalEnvVars = DatadogTracerConfigurator.INSTANCE.configure(run, node, envs); + envs.putAll(additionalEnvVars); + } +} diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty.java b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty.java new file mode 100644 index 000000000..1903a377c --- /dev/null +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty.java @@ -0,0 +1,121 @@ +package org.datadog.jenkins.plugins.datadog.tracer; + +import edu.umd.cs.findbugs.annotations.CheckForNull; +import edu.umd.cs.findbugs.annotations.NonNull; +import hudson.Extension; +import hudson.model.Job; +import hudson.model.JobProperty; +import hudson.model.JobPropertyDescriptor; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collection; +import java.util.EnumSet; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; +import net.sf.json.JSONObject; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.StaplerRequest; + +public class DatadogTracerJobProperty> extends JobProperty { + + private static final String DISPLAY_NAME = "Enable Datadog Test Visibility"; + + private final boolean on; + private final String serviceName; + private final Collection languages; + private final Map additionalVariables; + + public DatadogTracerJobProperty(boolean on, String serviceName, Collection languages, Map additionalVariables) { + this.on = on; + this.serviceName = serviceName; + this.languages = languages; + this.additionalVariables = additionalVariables; + } + + public boolean isOn() { + return on; + } + + public String getServiceName() { + return serviceName; + } + + public Collection getLanguages() { + return languages; + } + + public Map getAdditionalVariables() { + return additionalVariables; + } + + public List getAdditionalVariablesAsList() { + List list = new ArrayList<>(); + for (Map.Entry e : additionalVariables.entrySet()) { + list.add(new DatadogTracerEnvironmentProperty(e.getKey(), e.getValue())); + } + return list; + } + + @Extension + public static final class DatadogTracerJobPropertyDescriptor extends JobPropertyDescriptor { + @NonNull + @Override + public String getDisplayName() { + return DISPLAY_NAME; + } + + @Override + public boolean isApplicable(Class jobType) { + return true; + } + + @Override + public DatadogTracerJobProperty newInstance(StaplerRequest req, JSONObject formData) { + if (!formData.optBoolean("on")) { + return null; + } + + String serviceName = formData.getString("serviceName"); + + Set languages = EnumSet.noneOf(TracerLanguage.class); + JSONObject languagesJson = (JSONObject) formData.get("languages"); + for (Map.Entry e : (Set>) languagesJson.entrySet()) { + TracerLanguage language = TracerLanguage.valueOf(e.getKey()); + if (e.getValue()) { + languages.add(language); + } + } + + Map additionalVariables = new HashMap<>(); + List additionalVariablesList = req.bindJSONToList(DatadogTracerEnvironmentProperty.class, formData.get("additionalVariable")); + for (DatadogTracerEnvironmentProperty additionalVariable : additionalVariablesList) { + additionalVariables.put(additionalVariable.getName(), additionalVariable.getValue()); + } + + return new DatadogTracerJobProperty<>(true, serviceName, languages, additionalVariables); + } + } + + public static class DatadogTracerEnvironmentProperty implements Serializable { + private final String name; + private final String value; + + @DataBoundConstructor + public DatadogTracerEnvironmentProperty(String name, String value) { + this.name = name; + this.value = value; + } + + @CheckForNull + public String getName() { + return name; + } + + @NonNull + public String getValue() { + return value; + } + } +} diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerStepEnvironmentContributor.java b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerStepEnvironmentContributor.java new file mode 100644 index 000000000..42334311e --- /dev/null +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerStepEnvironmentContributor.java @@ -0,0 +1,27 @@ +package org.datadog.jenkins.plugins.datadog.tracer; + +import edu.umd.cs.findbugs.annotations.NonNull; +import hudson.EnvVars; +import hudson.Extension; +import hudson.model.Node; +import hudson.model.Run; +import hudson.model.TaskListener; +import java.io.IOException; +import java.util.Map; +import org.jenkinsci.plugins.workflow.steps.StepContext; +import org.jenkinsci.plugins.workflow.steps.StepEnvironmentContributor; + +@Extension +public class DatadogTracerStepEnvironmentContributor extends StepEnvironmentContributor { + + @Override + public void buildEnvironmentFor(StepContext stepContext, @NonNull EnvVars envs, @NonNull TaskListener listener) throws IOException, InterruptedException { + Run run = stepContext.get(Run.class); + Node node = stepContext.get(Node.class); + if (run != null && node != null) { + Map additionalEnvVars = DatadogTracerConfigurator.INSTANCE.configure(run, node, envs); + envs.putAll(additionalEnvVars); + } + } + +} diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/JavaConfigurator.java b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/JavaConfigurator.java new file mode 100644 index 000000000..99f221373 --- /dev/null +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/JavaConfigurator.java @@ -0,0 +1,204 @@ +package org.datadog.jenkins.plugins.datadog.tracer; + +import hudson.FilePath; +import hudson.model.Node; +import hudson.util.Secret; +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.charset.StandardCharsets; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; +import jenkins.model.Jenkins; +import org.datadog.jenkins.plugins.datadog.clients.HttpClient; +import org.datadog.jenkins.plugins.datadog.tracer.signature.SignatureVerifier; + +final class JavaConfigurator implements TracerConfigurator { + + private static final String TRACER_DISTRIBUTION_URL_ENV_VAR = "DATADOG_JENKINS_PLUGIN_TRACER_DISTRIBUTION_URL"; + private static final String DEFAULT_TRACER_DISTRIBUTION_URL = "https://dtdg.co/latest-java-tracer"; + private static final String TRACER_FILE_NAME = "dd-java-agent.jar"; + private static final String TRACER_IGNORE_JENKINS_PROXY_ENV_VAR = "DATADOG_JENKINS_PLUGIN_TRACER_IGNORE_JENKINS_PROXY"; + private static final String TRACER_JAR_CACHE_TTL_ENV_VAR = "DATADOG_JENKINS_PLUGIN_TRACER_JAR_CACHE_TTL_MINUTES"; + private static final int DEFAULT_TRACER_JAR_CACHE_TTL_MINUTES = 60 * 12; + private static final int TRACER_DOWNLOAD_TIMEOUT_MILLIS = 60_000; + private static final String DATADOG_DISTRIBUTION_HOST = "dtdg.co"; + private static final String MAVEN_CENTRAL_HOST = "repo1.maven.org"; + private static final String DATADOG_AGENT_MAVEN_DISTRIBUTION_PATH = "/maven2/com/datadoghq/dd-java-agent/"; + + private final HttpClient httpClient = new HttpClient(TRACER_DOWNLOAD_TIMEOUT_MILLIS); + + @Override + public Map configure(DatadogTracerJobProperty tracerConfig, Node node, FilePath workspacePath, Map envs) throws Exception { + FilePath tracerFile = downloadTracer(tracerConfig, workspacePath); + return getEnvVariables(tracerConfig, node, tracerFile, envs); + } + + private FilePath downloadTracer(DatadogTracerJobProperty tracerConfig, FilePath workspacePath) throws Exception { + FilePath datadogFolder = workspacePath.child(".datadog"); + datadogFolder.mkdirs(); + + FilePath datadogTracerFile = datadogFolder.child(TRACER_FILE_NAME); + long minutesSinceModification = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - datadogTracerFile.lastModified()); + if (minutesSinceModification < getTracerJarCacheTtlMinutes(tracerConfig)) { + // downloaded tracer is fresh enough + return datadogTracerFile.absolutize(); + } + + String tracerDistributionUrl = getTracerDistributionUrl(tracerConfig); + httpClient.getBinary(tracerDistributionUrl, Collections.emptyMap(), is -> { + try { + datadogTracerFile.copyFrom(is); + } catch (Exception e) { + throw new RuntimeException(e); + } + }); + + if (!DEFAULT_TRACER_DISTRIBUTION_URL.equals(tracerDistributionUrl)) { + // verify signature if downloading from Maven Central + String signatureFileUrl = tracerDistributionUrl + ".asc"; + byte[] signaturePublicKey = getTracerSignaturePublicKey(tracerConfig); + + httpClient.getBinary(signatureFileUrl, Collections.emptyMap(), signatureStream -> { + try (InputStream tracerStream = datadogTracerFile.read(); + InputStream publicKeyStream = new ByteArrayInputStream(signaturePublicKey)) { + boolean signatureValid = SignatureVerifier.verifySignature(tracerStream, signatureStream, publicKeyStream); + if (!signatureValid) { + throw new IllegalStateException("Tracer downloaded from " + tracerDistributionUrl + " is not signed with a valid signature"); + } + } catch (Exception e) { + throw new RuntimeException("Error while verifying tracer signature", e); + } + }); + } + + return datadogTracerFile.absolutize(); + } + + private int getTracerJarCacheTtlMinutes(DatadogTracerJobProperty tracerConfig) { + return getSetting(tracerConfig, TRACER_JAR_CACHE_TTL_ENV_VAR, DEFAULT_TRACER_JAR_CACHE_TTL_MINUTES, Integer::parseInt); + } + + private String getTracerDistributionUrl(DatadogTracerJobProperty tracerConfig) { + return getSetting(tracerConfig, TRACER_DISTRIBUTION_URL_ENV_VAR, DEFAULT_TRACER_DISTRIBUTION_URL, this::validateUserSuppliedTracerUrl); + } + + private byte[] getTracerSignaturePublicKey(DatadogTracerJobProperty tracerConfig) { + return getSetting(tracerConfig, TRACER_DISTRIBUTION_URL_ENV_VAR, SignatureVerifier.DATADOG_PUBLIC_KEY.getBytes(StandardCharsets.UTF_8), String::getBytes); + } + + private T getSetting(DatadogTracerJobProperty tracerConfig, String envVariableName, T defaultValue, Function parser) { + String envVariable = getEnvVariable(tracerConfig, envVariableName); + if (envVariable != null) { + return parser.apply(envVariable); + } + return defaultValue; + } + + private String getEnvVariable(DatadogTracerJobProperty tracerConfig, String name) { + Map additionalVariables = tracerConfig.getAdditionalVariables(); + if (additionalVariables != null) { + String envVariable = additionalVariables.get(name); + if (envVariable != null) { + return envVariable; + } + } + return System.getenv(TRACER_JAR_CACHE_TTL_ENV_VAR); + } + + private String validateUserSuppliedTracerUrl(String distributionUrl) { + URL url; + try { + url = new URL(distributionUrl); + } catch (MalformedURLException e) { + throw new IllegalArgumentException("Error while parsing tracer distribution URL: " + distributionUrl, e); + } + + String host = url.getHost(); + if (DATADOG_DISTRIBUTION_HOST.equals(host)) { + return distributionUrl; + } + + if (!MAVEN_CENTRAL_HOST.equals(host)) { + throw new IllegalArgumentException("Illegal tracer distribution host: " + host + " (" + distributionUrl + ")"); + } + + String path = url.getPath(); + if (!path.startsWith(DATADOG_AGENT_MAVEN_DISTRIBUTION_PATH)) { + throw new IllegalArgumentException("Illegal tracer distribution path: " + path + " (" + distributionUrl + ")"); + } + return distributionUrl; + } + + private static Map getEnvVariables(DatadogTracerJobProperty tracerConfig, + Node node, + FilePath tracerFile, + Map envs) { + Map variables = new HashMap<>(); + + String tracerAgent = "-javaagent:" + tracerFile.getRemote(); + variables.put("MAVEN_OPTS", PropertyUtils.prepend(envs, "MAVEN_OPTS", tracerAgent)); + variables.put("GRADLE_OPTS", PropertyUtils.prepend(envs, "GRADLE_OPTS", "-Dorg.gradle.jvmargs=" + tracerAgent)); + + String proxyConfiguration = getProxyConfiguration(tracerConfig, node); + if (proxyConfiguration != null) { + variables.put("JAVA_TOOL_OPTIONS", PropertyUtils.prepend(envs, "JAVA_TOOL_OPTIONS", proxyConfiguration)); + } + + Map additionalVariables = tracerConfig.getAdditionalVariables(); + if (additionalVariables != null) { + variables.putAll(additionalVariables); + } + + return variables; + } + + private static String getProxyConfiguration(DatadogTracerJobProperty tracerConfig, Node node) { + if (!(node instanceof Jenkins)) { + // only apply Jenkins proxy settings if tracer will be run on master node + return null; + } + + Jenkins jenkins = Jenkins.getInstanceOrNull(); + if (jenkins == null) { + return null; + } + hudson.ProxyConfiguration jenkinsProxyConfiguration = jenkins.getProxy(); + if (jenkinsProxyConfiguration == null) { + return null; + } + + Map additionalVariables = tracerConfig.getAdditionalVariables(); + if (Boolean.parseBoolean(additionalVariables.get(TRACER_IGNORE_JENKINS_PROXY_ENV_VAR))) { + return null; + } + + String proxyHost = jenkinsProxyConfiguration.getName(); + int proxyPort = jenkinsProxyConfiguration.getPort(); + String noProxyHost = jenkinsProxyConfiguration.getNoProxyHost(); + String userName = jenkinsProxyConfiguration.getUserName(); + Secret password = jenkinsProxyConfiguration.getSecretPassword(); + + StringBuilder proxyOptions = new StringBuilder(); + if (proxyHost != null) { + proxyOptions.append("-Dhttp.proxyHost=").append(proxyHost); + } + if (proxyPort > 0) { + proxyOptions.append("-Dhttp.proxyPort=").append(proxyPort); + } + if (noProxyHost != null) { + proxyOptions.append("-Dhttp.nonProxyHosts=").append(noProxyHost); + } + if (userName != null) { + proxyOptions.append("-Dhttp.proxyUser=").append(userName); + } + if (password != null) { + proxyOptions.append("-Dhttp.proxyPassword=").append(Secret.toString(password)); + } + return proxyOptions.length() > 0 ? proxyOptions.toString() : null; + } +} diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/JavascriptConfigurator.java b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/JavascriptConfigurator.java new file mode 100644 index 000000000..96cae3b43 --- /dev/null +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/JavascriptConfigurator.java @@ -0,0 +1,35 @@ +package org.datadog.jenkins.plugins.datadog.tracer; + +import hudson.FilePath; +import hudson.model.Node; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +final class JavascriptConfigurator implements TracerConfigurator { + + private static final Logger LOGGER = Logger.getLogger(JavascriptConfigurator.class.getName()); + + private static final int GET_NPM_VERSION_TIMEOUT_MILLIS = 30_000; + private static final int INSTALL_TRACER_TIMEOUT_MILLIS = 300_000; + + @Override + public Map configure(DatadogTracerJobProperty tracerConfig, Node node, FilePath workspacePath, Map envs) throws Exception { + String nodeVersion = workspacePath.act(new ShellCommandCallable(GET_NPM_VERSION_TIMEOUT_MILLIS, "npm", "-v")); + LOGGER.log(Level.FINE, "Got npm version " + nodeVersion + " from " + workspacePath + " on " + node); + + String installTracerOutput = workspacePath.act(new ShellCommandCallable(INSTALL_TRACER_TIMEOUT_MILLIS, "npm", "install", "dd-trace")); + LOGGER.log(Level.FINE, "Tracer installed in " + workspacePath + " on " + node + "; output: " + installTracerOutput); + + Path absoluteWorkspacePath = Paths.get(workspacePath.absolutize().getRemote()); + Path tracerPath = absoluteWorkspacePath.resolve("node_modules/dd-trace"); + + Map variables = new HashMap<>(); + variables.put("DD_TRACE_PATH", tracerPath.toString()); + variables.put("NODE_OPTIONS", PropertyUtils.prepend(envs, "NODE_OPTIONS", "-r ./example.js -r $DD_TRACE_PATH/ci/init")); + return variables; + } +} diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/PropertyUtils.java b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/PropertyUtils.java new file mode 100644 index 000000000..3ff559503 --- /dev/null +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/PropertyUtils.java @@ -0,0 +1,10 @@ +package org.datadog.jenkins.plugins.datadog.tracer; + +import java.util.Map; + +public class PropertyUtils { + public static String prepend(Map envs, String propertyName, String propertyValue) { + String existingPropertyValue = envs.get(propertyName); + return propertyValue + (existingPropertyValue != null ? " " + existingPropertyValue : ""); + } +} diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/PythonConfigurator.java b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/PythonConfigurator.java new file mode 100644 index 000000000..d2bb0f6ad --- /dev/null +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/PythonConfigurator.java @@ -0,0 +1,44 @@ +package org.datadog.jenkins.plugins.datadog.tracer; + +import hudson.FilePath; +import hudson.model.Node; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; + +final class PythonConfigurator implements TracerConfigurator { + + private static final Logger LOGGER = Logger.getLogger(PythonConfigurator.class.getName()); + + private static final int GET_PIP_VERSION_TIMEOUT_MILLIS = 30_000; + private static final int INSTALL_TRACER_TIMEOUT_MILLIS = 300_000; + private static final int SHOW_TRACER_PACKAGE_DETAILS_TIMEOUT_MILLIS = 300_000; + + @Override + public Map configure(DatadogTracerJobProperty tracerConfig, Node node, FilePath workspacePath, Map envs) throws Exception { + String pipVersion = workspacePath.act(new ShellCommandCallable(GET_PIP_VERSION_TIMEOUT_MILLIS, "pip", "-V")); + LOGGER.log(Level.FINE, "Got pip version " + pipVersion + " from " + workspacePath + " on " + node); + + String installTracerOutput = workspacePath.act(new ShellCommandCallable(INSTALL_TRACER_TIMEOUT_MILLIS, "pip", "install", "-U", "ddtrace")); + LOGGER.log(Level.FINE, "Tracer installed in " + workspacePath + " on " + node + "; output: " + installTracerOutput); + + String tracerLocation = getTracerLocation(workspacePath); + + Map variables = new HashMap<>(); + variables.put("PYTEST_ADDOPTS", PropertyUtils.prepend(envs, "PYTEST_ADDOPTS", "--ddtrace")); + variables.put("PYTHONPATH", PropertyUtils.prepend(envs, "PYTHONPATH", tracerLocation + ":")); + return variables; + } + + private static String getTracerLocation(FilePath workspacePath) throws IOException, InterruptedException { + String getTracerLocationOutput = workspacePath.act(new ShellCommandCallable(SHOW_TRACER_PACKAGE_DETAILS_TIMEOUT_MILLIS, "pip", "show", "ddtrace")); + for (String line : getTracerLocationOutput.split("\n")) { + if (line.contains("Location")) { + return line.substring(line.indexOf(':') + 2); + } + } + throw new IllegalStateException("Could not determine tracer location in " + workspacePath + "; command output is: " + getTracerLocationOutput); + } +} diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/ShellCommandCallable.java b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/ShellCommandCallable.java new file mode 100644 index 000000000..09288a225 --- /dev/null +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/ShellCommandCallable.java @@ -0,0 +1,33 @@ +package org.datadog.jenkins.plugins.datadog.tracer; + +import hudson.remoting.VirtualChannel; +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import jenkins.MasterToSlaveFileCallable; +import org.datadog.jenkins.plugins.datadog.util.ShellCommandExecutor; + +public class ShellCommandCallable extends MasterToSlaveFileCallable { + private final long timeoutMillis; + private final String[] command; + + public ShellCommandCallable(long timeoutMillis, String... command) { + this.timeoutMillis = timeoutMillis; + this.command = Arrays.copyOf(command, command.length); + } + + @Override + public String invoke(File workspace, VirtualChannel channel) throws IOException { + try { + ShellCommandExecutor shellCommandExecutor = new ShellCommandExecutor(workspace, timeoutMillis); + return shellCommandExecutor.executeCommand(new ShellCommandExecutor.ToStringOutputParser(), command); + + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IOException("Interrupted while waiting for shell command: " + Arrays.toString(command)); + + } catch (Exception e) { + throw new IOException("Error running shell command: " + Arrays.toString(command), e); + } + } +} diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/TracerConfigurator.java b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/TracerConfigurator.java new file mode 100644 index 000000000..a583e7f9a --- /dev/null +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/TracerConfigurator.java @@ -0,0 +1,12 @@ +package org.datadog.jenkins.plugins.datadog.tracer; + +import hudson.FilePath; +import hudson.model.Node; +import java.util.Map; + +interface TracerConfigurator { + Map configure(DatadogTracerJobProperty tracerConfig, + Node node, + FilePath workspacePath, + Map envs) throws Exception; +} diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/TracerLanguage.java b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/TracerLanguage.java new file mode 100644 index 000000000..1e91277e0 --- /dev/null +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/TracerLanguage.java @@ -0,0 +1,18 @@ +package org.datadog.jenkins.plugins.datadog.tracer; + +public enum TracerLanguage { + JAVA("Java"), + JAVASCRIPT("JS"), + PYTHON("Python"); + + private final String label; + + TracerLanguage(String label) { + this.label = label; + } + + @Override + public String toString() { + return label; + } +} diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/signature/SignatureVerifier.java b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/signature/SignatureVerifier.java new file mode 100644 index 000000000..0f436674b --- /dev/null +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/tracer/signature/SignatureVerifier.java @@ -0,0 +1,101 @@ +package org.datadog.jenkins.plugins.datadog.tracer.signature; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import org.bouncycastle.openpgp.PGPCompressedData; +import org.bouncycastle.openpgp.PGPLiteralData; +import org.bouncycastle.openpgp.PGPObjectFactory; +import org.bouncycastle.openpgp.PGPPublicKeyRing; +import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; +import org.bouncycastle.openpgp.PGPSignature; +import org.bouncycastle.openpgp.PGPSignatureList; +import org.bouncycastle.openpgp.PGPUtil; +import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator; +import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider; + +public class SignatureVerifier { + + public static final String DATADOG_PUBLIC_KEY = ( + "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" + + "Version: Hockeypuck 2.1.0-222-g25248d4\n" + + "Comment: Hostname:\n" + + "\n" + + "xsFNBGBbo2cBEAC16RsAMkhlN1YYzmihO//AwHRQ+n33bnmXsGkXgp2vKPcM7bty\n" + + "oiZxdz3FAM6fBlBKG0RNn7DBEmkiu2EdxFCp7zLzLa2NC3KW89D0GnmkvX9Uebgf\n" + + "Iv8a4QcMHsWl3sAINnbiy2uNnJqmDzA5ofsAkJyL9fEMVdagLASC/CsxckxPDPgg\n" + + "RBp8Et625lMdIA4Owf5ZPibYVMR5xOdoucsH3KFGJNVyvX+xwx8hQDyLMg7hbI0T\n" + + "9nu840oZR+CQqndu6sFxqxGNzbwRYoJO/zZKttwnfDQnruJjkGL8dptHxuBJ7c+8\n" + + "TAabUzvbhNltuJDnt3+qKbV9hg1+tNgbA6oo1E5xONzg4mm/IG9TzejU1uFGNRC2\n" + + "2PSzwG8ps9saB61+Px3/1VmYFisdpaGssQJHbSFylQcgWJx4gVo0EBZER/i+JLn6\n" + + "g8AiNf2RQoFKkliHMHgR0UXek+5vxiPYCF5fqMvpDqPjKq0TgbVZyfPz50LrNEy/\n" + + "HA/a3hdJbYyMThNQ1LTKQVW7JJnIO/87P3/XV0wSpwt4JBJHXNFk+w1YY/DbAHyS\n" + + "OGDxYUvJ8AOz1gvt8MBf/pDoV/kbY8b+PsX41eCe0uQ58tmbXhAXBZss2P2AcuZq\n" + + "BwNho0CnfXWp04g5I3FlaWb+/ZMQ+/AsXgbTWCRldC+P2Cou5zPVrGrgMQARAQAB\n" + + "zWdEYXRhZG9nIGRkLXRyYWNlLWphdmEgUGFja2FnaW5nIChEYXRhZG9nIGRkLXRy\n" + + "YWNlLWphdmEgUGFja2FnaW5nKSA8cGFja2FnZStkZC10cmFjZS1qYXZhQGRhdGFk\n" + + "b2docS5jb20+wsGTBBMBCAA9FiEE2O7camr/v79OPdjRyiBgiJT0O00FAmBbo2cC\n" + + "GwMFCQICKQAECwkIBwUVCgkICwUWAwIBAAIeAQIXgAAKCRDKIGCIlPQ7TV2RD/4w\n" + + "wypKpk4GFYoxnfpsNT7g3U1ZSy3qGwabo4FxXN0mH0i92AI2bREWKjkvQQcQUmfR\n" + + "+vG05nyF0MJ3Vpre/Qzt7TOcy2sBIOpFzo4pMJpRHp5W9Pqm+PCUpzs+X5LBz04i\n" + + "6DRhNWT0kBJI9U5mQCdZETEHQZ8iUC/UAfNkXRrUMNF6OcjkPhWUPZB2OtH07bZl\n" + + "GZkX9iwxUcM7nsAbj/qnqcVAxSnr3ylYSBo8ctgEIY/YsQsMzq6JasxtBCaMMz10\n" + + "TflGD0bNV0zd8xjY4OfF42C9W+o0lDlTPq3HkxOUC/uUmaX/gSniJOyAp9wtD3Al\n" + + "P9oi3RWXEAdbIQ/D38smTYZD/0VopeoX0M7QrH6ifsCCRiMj/rbw3M/KDheWDk6w\n" + + "5CKpYDqJVmsVvWm+h7H51nQTM5CZLXHkasWmhOkj48SU3p+NSAnvQywaVW9drU79\n" + + "wxjUgCXSwl8IHmTDSEbrBRbLSXWLiGGeqw+EmLZckJE6BQNDFYflkVLuObhNJtRi\n" + + "pZtYi8gqyelflomEN6YBJ8dOk6SCcnU6SEzua/2YpYsHakGld0/uyux2MfXwCiJ9\n" + + "Wf37KG6e1eegDQWy8BPg6iRoIaEyyeEWQ8CpG8ZG2zfMxUOe4pqsRfUT5k2LE5kx\n" + + "IPdMz7WGf1S8iuZcBwF6PhAqJJv41pJ0mO4ewnZbnQ==\n" + + "=7erW\n" + + "-----END PGP PUBLIC KEY BLOCK-----" + ); + + public static boolean verifySignature(InputStream artifactStream, InputStream signatureStream, InputStream publicKeyStream) throws Exception { + PGPPublicKeyRing publicKeys = loadPublicKeyRing(publicKeyStream); + PGPSignature signature = loadSignature(signatureStream); + signature.init(new BcPGPContentVerifierBuilderProvider(), publicKeys.getPublicKey()); + readSignedContentInto(signature, artifactStream); + return signature.verify(); + } + + private static PGPPublicKeyRing loadPublicKeyRing(InputStream keyStream) throws Exception { + InputStream keyIn = PGPUtil.getDecoderStream(keyStream); + PGPPublicKeyRingCollection pgpRing = new PGPPublicKeyRingCollection(keyIn, new BcKeyFingerprintCalculator()); + return pgpRing.getKeyRings().next(); + } + + private static PGPSignature loadSignature(InputStream signatureStream) throws Exception { + InputStream sigInputStream = PGPUtil.getDecoderStream(signatureStream); + PGPObjectFactory pgpObjectFactory = new PGPObjectFactory(sigInputStream, new BcKeyFingerprintCalculator()); + + Object object; + while ((object = pgpObjectFactory.nextObject()) != null) { + if (object instanceof PGPSignatureList) { + return ((PGPSignatureList) object).get(0); + } + + if (object instanceof PGPCompressedData) { + pgpObjectFactory = new PGPObjectFactory(((PGPCompressedData) object).getDataStream(), + new BcKeyFingerprintCalculator()); + } + + if (object instanceof PGPLiteralData) { + InputStream dataStream = ((PGPLiteralData) object).getDataStream(); + byte[] buf = new byte[8192]; + while (dataStream.read(buf) > 0) { + } + } + } + throw new IllegalArgumentException("PGP signature not found"); + } + + private static void readSignedContentInto(PGPSignature signature, InputStream inputStream) throws IOException { + byte[] buf = new byte[8192]; + int t; + while ((t = inputStream.read(buf)) >= 0) { + signature.update(buf, 0, t); + } + } + +} diff --git a/src/main/java/org/datadog/jenkins/plugins/datadog/util/ShellCommandExecutor.java b/src/main/java/org/datadog/jenkins/plugins/datadog/util/ShellCommandExecutor.java new file mode 100644 index 000000000..5238ec242 --- /dev/null +++ b/src/main/java/org/datadog/jenkins/plugins/datadog/util/ShellCommandExecutor.java @@ -0,0 +1,211 @@ +package org.datadog.jenkins.plugins.datadog.util; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class ShellCommandExecutor { + + private static final int NORMAL_TERMINATION_TIMEOUT_MILLIS = 3000; + + private final File executionFolder; + private final long timeoutMillis; + + public ShellCommandExecutor(File executionFolder, long timeoutMillis) { + this.executionFolder = executionFolder; + this.timeoutMillis = timeoutMillis; + } + + /** + * Executes given shell command and returns parsed output + * + * @param outputParser Parses that is used to process command output + * @param command Command to be executed + * @return Parsed command output + * @param Type of parsed command output + * @throws IOException If an error was encountered while writing command input or reading output + * @throws TimeoutException If timeout was reached while waiting for command to finish + * @throws InterruptedException If current thread was interrupted while waiting for command to + * finish + */ + public T executeCommand(OutputParser outputParser, String... command) + throws IOException, InterruptedException, TimeoutException { + return executeCommand(outputParser, null, false, command); + } + + /** + * Executes given shell command, supplies to it provided input, and returns parsed output + * + * @param outputParser Parses that is used to process command output + * @param input Bytes that are written to command's input stream + * @param command Command to be executed + * @return Parsed command output + * @param Type of parsed command output + * @throws IOException If an error was encountered while writing command input or reading output + * @throws TimeoutException If timeout was reached while waiting for command to finish + * @throws InterruptedException If current thread was interrupted while waiting for command to + * finish + */ + public T executeCommand(OutputParser outputParser, byte[] input, String... command) + throws IOException, InterruptedException, TimeoutException { + return executeCommand(outputParser, input, false, command); + } + + /** + * Executes given shell command and returns parsed error stream + * + * @param errorParser Parses that is used to process command's error stream + * @param command Command to be executed + * @return Parsed command output + * @param Type of parsed command output + * @throws IOException If an error was encountered while writing command input or reading output + * @throws TimeoutException If timeout was reached while waiting for command to finish + * @throws InterruptedException If current thread was interrupted while waiting for command to + * finish + */ + public T executeCommandReadingError(OutputParser errorParser, String... command) + throws IOException, InterruptedException, TimeoutException { + return executeCommand(errorParser, null, true, command); + } + + private T executeCommand( + OutputParser outputParser, byte[] input, boolean readFromError, String... command) + throws IOException, TimeoutException, InterruptedException { + ProcessBuilder processBuilder = new ProcessBuilder(command); + processBuilder.directory(executionFolder); + + Process p = processBuilder.start(); + + StreamConsumer inputStreamConsumer = new StreamConsumer(p.getInputStream()); + Thread inputStreamThread = new Thread(inputStreamConsumer, "input-stream-consumer-" + command[0]); + inputStreamThread.setDaemon(true); + inputStreamThread.start(); + + StreamConsumer errorStreamConsumer = new StreamConsumer(p.getErrorStream()); + Thread errorStreamThread = new Thread(errorStreamConsumer, "error-stream-consumer-" + command[0]); + errorStreamThread.setDaemon(true); + errorStreamThread.start(); + + if (input != null) { + p.getOutputStream().write(input); + p.getOutputStream().close(); + } + + try { + if (p.waitFor(timeoutMillis, TimeUnit.MILLISECONDS)) { + int exitValue = p.exitValue(); + if (exitValue != 0) { + throw new IOException( + "Command '" + + Arrays.toString(command) + + "' failed with exit code " + + exitValue + + ": " + + readLines(errorStreamConsumer.read(), Charset.defaultCharset())); + } + + if (outputParser != OutputParser.IGNORE) { + if (readFromError) { + errorStreamThread.join(NORMAL_TERMINATION_TIMEOUT_MILLIS); + return outputParser.parse(errorStreamConsumer.read()); + } else { + inputStreamThread.join(NORMAL_TERMINATION_TIMEOUT_MILLIS); + return outputParser.parse(inputStreamConsumer.read()); + } + } else { + return null; + } + + } else { + terminate(p); + throw new TimeoutException( + "Timeout while waiting for '" + + Arrays.toString(command) + + "'; " + + readLines(errorStreamConsumer.read(), Charset.defaultCharset())); + } + } catch (InterruptedException e) { + terminate(p); + throw e; + } + } + + private void terminate(Process p) throws InterruptedException { + p.destroy(); + try { + if (!p.waitFor(NORMAL_TERMINATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS)) { + p.destroyForcibly(); + } + } catch (InterruptedException e) { + p.destroyForcibly(); + throw e; + } + } + + private static List readLines(InputStream input, Charset charset) throws IOException { + final InputStreamReader reader = new InputStreamReader(input, charset); + final BufferedReader bufReader = new BufferedReader(reader); + final List list = new ArrayList<>(); + String line; + while ((line = bufReader.readLine()) != null) { + list.add(line); + } + return list; + } + + private static final class StreamConsumer implements Runnable { + private final byte[] buffer = new byte[2048]; + private final ByteArrayOutputStream output = new ByteArrayOutputStream(); + private final InputStream input; + + private StreamConsumer(InputStream input) { + this.input = input; + } + + @Override + public void run() { + try { + int read; + while ((read = input.read(buffer)) != -1) { + output.write(buffer, 0, read); + } + } catch (Exception e) { + // ignore + } + } + + InputStream read() { + return new ByteArrayInputStream(output.toByteArray()); + } + } + + public interface OutputParser { + OutputParser IGNORE = is -> null; + + T parse(InputStream inputStream) throws IOException; + } + + public static final class ToStringOutputParser implements OutputParser { + @Override + public String parse(InputStream inputStream) throws IOException { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + byte[] buffer = new byte[4096]; + int count; + while ((count = inputStream.read(buffer)) != -1) { + output.write(buffer, 0, count); + } + return new String(output.toByteArray(), Charset.defaultCharset()); + } + } + +} diff --git a/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/config.jelly b/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/config.jelly index e972a9fb9..7f0b723d6 100644 --- a/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/config.jelly +++ b/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/config.jelly @@ -59,6 +59,7 @@ + diff --git a/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/help-apiKeyEntry.html b/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/help-apiKeyEntry.html deleted file mode 100644 index a8af7252e..000000000 --- a/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/help-apiKeyEntry.html +++ /dev/null @@ -1,4 +0,0 @@ -
- Enter your Datadog API key, which can be found - here. -
diff --git a/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/help-credentialsApiKeyEntry.html b/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/help-credentialsApiKeyEntry.html deleted file mode 100644 index 55b35ffe9..000000000 --- a/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/help-credentialsApiKeyEntry.html +++ /dev/null @@ -1,6 +0,0 @@ -
- Enter your Datadog API key, which can be found - here. - This field is optional, use it only if you would like to store your API key with - Jenkins Credentials. -
diff --git a/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/help-targetApiKeyEntry.html b/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/help-targetApiKeyEntry.html new file mode 100644 index 000000000..85559287d --- /dev/null +++ b/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/help-targetApiKeyEntry.html @@ -0,0 +1,4 @@ +
+ Enter your Datadog API key, which can be found + here. +
diff --git a/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/help-targetCredentialsApiKeyEntry.html b/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/help-targetCredentialsApiKeyEntry.html new file mode 100644 index 000000000..417a9dec3 --- /dev/null +++ b/src/main/resources/org/datadog/jenkins/plugins/datadog/DatadogGlobalConfiguration/help-targetCredentialsApiKeyEntry.html @@ -0,0 +1,6 @@ +
+ Enter your Datadog API key, which can be found + here. + This field is optional, use it only if you would like to store your API key with + Jenkins Credentials. +
diff --git a/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/config.jelly b/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/config.jelly new file mode 100644 index 000000000..78c59de7a --- /dev/null +++ b/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/config.jelly @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + +
+ + + + + + + + + + +
+ +
+
+ +
+
+
+ +
+ +
+ diff --git a/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/help-additionalVariables.html b/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/help-additionalVariables.html new file mode 100644 index 000000000..70ac7ceb3 --- /dev/null +++ b/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/help-additionalVariables.html @@ -0,0 +1,9 @@ +
+

+ Additional environment variables to fine-tune the tracer behavior. + See Datadog docs for more details. +

+

+ These are optional, use only if you want to change defaults. +

+
diff --git a/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/help-languages.html b/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/help-languages.html new file mode 100644 index 000000000..9d47dbc3f --- /dev/null +++ b/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/help-languages.html @@ -0,0 +1,37 @@ +
+

Automatic tracer injection comes with some language-specific caveats.

+ + Java +

+ Injection works for any JVM-based language (Java, Kotlin, Groovy, etc). + Any Maven or Gradle build that the job executes will be traced. +

+

+ MAVEN_OPTS and GRADLE_OPTS environment variables are used for injection. + If these variables are overridden inside the job (with their existing value being discarded rather than preserved), + the injection will not happen. +

+ + JS +

+ The plugin assumes that npm is available on the node that executes the job. +

+

+ NODE_OPTIONS environment variable is used for injection. + If this variable is overridden inside the job (with its existing value being discarded rather than preserved), + the injection will not happen. +

+ + Python +

+ The plugin assumes that pip is available on the node that executes the job. +

+

+ The plugin assumes that the job uses the same pip/python versions as the default ones that are available on the execution node. +

+

+ PYTEST_ADDOPTS and PYTHONPATH environment variables are used for injection. + If these variables are overridden inside the job (with their existing value being discarded rather than preserved), + the injection will not happen. +

+
diff --git a/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/help-on.html b/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/help-on.html new file mode 100644 index 000000000..ee4ea96bc --- /dev/null +++ b/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/help-on.html @@ -0,0 +1,5 @@ +
+ Enable Datadog Test Visibility for this job. + This requires configuring Datadog Plugin connectivity. + Please bear in mind that Test Visibility is a separate Datadog product that is billed separately. +
diff --git a/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/help-serviceName.html b/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/help-serviceName.html new file mode 100644 index 000000000..1ecb4660a --- /dev/null +++ b/src/main/resources/org/datadog/jenkins/plugins/datadog/tracer/DatadogTracerJobProperty/help-serviceName.html @@ -0,0 +1,4 @@ +
+ The name of the service or library being tested. + This value is used for populating test.service tag in Datadog. +
diff --git a/src/test/java/org/datadog/jenkins/plugins/datadog/tracer/TracerInjectionIT.java b/src/test/java/org/datadog/jenkins/plugins/datadog/tracer/TracerInjectionIT.java new file mode 100644 index 000000000..baae85bb6 --- /dev/null +++ b/src/test/java/org/datadog/jenkins/plugins/datadog/tracer/TracerInjectionIT.java @@ -0,0 +1,195 @@ +package org.datadog.jenkins.plugins.datadog.tracer; + +import hudson.FilePath; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import hudson.model.Job; +import hudson.model.TopLevelItem; +import hudson.slaves.DumbSlave; +import hudson.tasks.Shell; +import java.io.File; +import java.io.IOException; +import java.net.URL; +import java.util.Collections; +import org.datadog.jenkins.plugins.datadog.DatadogGlobalConfiguration; +import org.datadog.jenkins.plugins.datadog.DatadogUtilities; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.rules.TestRule; +import org.jvnet.hudson.test.FlagRule; +import org.jvnet.hudson.test.JenkinsRule; + +public class TracerInjectionIT { + + // "mvn" script does not quote MAVEN_OPTS properly, this is outside of our control + @ClassRule + public static TestRule noSpaceInTmpDirs = FlagRule.systemProperty("jenkins.test.noSpaceInTmpDirs", "true"); + + @ClassRule + public static JenkinsRule jenkinsRule = new JenkinsRule(); + private static DumbSlave agentNode; + + @BeforeClass + public static void suiteSetUp() throws Exception { + DatadogGlobalConfiguration datadogConfig = DatadogUtilities.getDatadogGlobalDescriptor(); + datadogConfig.setReportWith("DSD"); + + agentNode = jenkinsRule.createOnlineSlave(); + } + + @AfterClass + public static void suiteTearDown() throws Exception { + jenkinsRule.disconnectSlave(agentNode); + } + + @Test + public void testTracerInjectionInFreestyleProject() throws Exception { + FreeStyleProject project = givenFreestyleProject(); + try { + givenProjectIsAMavenBuild(project); + givenTracerInjectionEnabled(project); + FreeStyleBuild build = whenRunningBuild(project); + thenTracerIsInjected(build); + } finally { + project.delete(); + } + } + + @Test + public void testTracerInjectionInFreestyleProjectExecutedOnAgentNode() throws Exception { + FreeStyleProject project = givenFreestyleProject(); + try { + givenProjectIsBuiltOnAgentNode(project); + givenProjectIsAMavenBuild(project); + givenTracerInjectionEnabled(project); + FreeStyleBuild build = whenRunningBuild(project); + thenTracerIsInjected(build); + } finally { + project.delete(); + } + } + + @Test + public void testTracerInjectionInPipeline() throws Exception { + WorkflowJob pipeline = givenPipelineProjectBuiltOnMaster(); + try { + givenPipelineIsAMavenBuild(pipeline, false); + givenTracerInjectionEnabled(pipeline); + WorkflowRun build = whenRunningBuild(pipeline); + thenTracerIsInjected(build); + } finally { + pipeline.delete(); + } + } + + @Test + public void testTracerInjectionInPipelineExecutedOnAgentNode() throws Exception { + WorkflowJob pipeline = givenPipelineProjectBuiltOnAgentNode(); + try { + givenPipelineIsAMavenBuild(pipeline, true); + givenTracerInjectionEnabled(pipeline); + WorkflowRun build = whenRunningBuild(pipeline); + thenTracerIsInjected(build); + } finally { + pipeline.delete(); + } + } + + private FreeStyleProject givenFreestyleProject() throws IOException { + return jenkinsRule.createFreeStyleProject("freestyleProject"); + } + + private WorkflowJob givenPipelineProjectBuiltOnMaster() throws Exception { + return givenPipelineProject(false); + } + + private WorkflowJob givenPipelineProjectBuiltOnAgentNode() throws Exception { + return givenPipelineProject(true); + } + + private WorkflowJob givenPipelineProject(boolean builtOnAgentNode) throws Exception { + WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "pipelineProject"); + String definition = "pipeline {\n" + + " agent {\n" + + " label '" + (builtOnAgentNode ? agentNode.getSelfLabel().getName() : "built-in") + "'\n" + + " }\n" + + " stages {\n" + + " stage('test'){\n" + + " steps {\n" + + " " + getMavenCommand() + "\n" + + " }\n" + + " }\n" + + " }\n" + + "}"; + job.setDefinition(new CpsFlowDefinition(definition, true)); + return job; + } + + private String getMavenCommand() { + return isRunningOnWindows() + ? "bat \"./mvnw.cmd clean\"" + : "sh \"./mvnw clean\""; + } + + private boolean isRunningOnWindows() { + return System.getProperty("os.name").toLowerCase().contains("win"); + } + + private void givenProjectIsBuiltOnAgentNode(FreeStyleProject project) throws Exception { + project.setAssignedNode(agentNode); + } + + private void givenProjectIsAMavenBuild(FreeStyleProject project) throws Exception { + copyMavenFilesToWorkspace(agentNode.getSelfLabel().equals(project.getAssignedLabel()), project); + project.getBuildersList().add(new Shell("./mvnw clean")); + } + + private void givenPipelineIsAMavenBuild(WorkflowJob pipeline, boolean builtOnAgent) throws Exception { + copyMavenFilesToWorkspace(builtOnAgent, pipeline); + } + + private void copyMavenFilesToWorkspace(boolean builtOnAgent, TopLevelItem item) throws Exception { + FilePath projectWorkspace; + if (builtOnAgent) { + projectWorkspace = agentNode.getWorkspaceFor(item); + } else { + projectWorkspace = jenkinsRule.jenkins.getWorkspaceFor(item); + } + + URL mavenProjectUrl = TracerInjectionIT.class.getResource("/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project"); + File mavenProject = new File(mavenProjectUrl.getFile()); + FilePath mavenProjectPath = new FilePath(mavenProject); + mavenProjectPath.copyRecursiveTo(projectWorkspace); + } + + private void givenTracerInjectionEnabled(Job job) throws IOException { + DatadogTracerJobProperty traceInjectionConfig = new DatadogTracerJobProperty<>( + true, + "integration-test-service-name", + Collections.singletonList(TracerLanguage.JAVA), + Collections.singletonMap("DD_CIVISIBILITY_ENABLED", "false") + ); + job.addProperty(traceInjectionConfig); + } + + private FreeStyleBuild whenRunningBuild(FreeStyleProject project) throws Exception { + return jenkinsRule.buildAndAssertSuccess(project); + } + + private WorkflowRun whenRunningBuild(WorkflowJob pipeline) throws Exception { + return jenkinsRule.buildAndAssertSuccess(pipeline); + } + + private void thenTracerIsInjected(FreeStyleBuild build) throws IOException { + jenkinsRule.assertLogContains("DATADOG TRACER CONFIGURATION", build); + } + + private void thenTracerIsInjected(WorkflowRun build) throws IOException { + jenkinsRule.assertLogContains("DATADOG TRACER CONFIGURATION", build); + } +} diff --git a/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/.mvn/wrapper/maven-wrapper.jar b/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/.mvn/wrapper/maven-wrapper.jar new file mode 100644 index 000000000..cb28b0e37 Binary files /dev/null and b/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/.mvn/wrapper/maven-wrapper.jar differ diff --git a/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/.mvn/wrapper/maven-wrapper.properties b/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/.mvn/wrapper/maven-wrapper.properties new file mode 100644 index 000000000..70f4f50fc --- /dev/null +++ b/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/.mvn/wrapper/maven-wrapper.properties @@ -0,0 +1,18 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.8/apache-maven-3.8.8-bin.zip +wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar diff --git a/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/mvnw b/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/mvnw new file mode 100755 index 000000000..8d937f4c1 --- /dev/null +++ b/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/mvnw @@ -0,0 +1,308 @@ +#!/bin/sh +# ---------------------------------------------------------------------------- +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# ---------------------------------------------------------------------------- + +# ---------------------------------------------------------------------------- +# Apache Maven Wrapper startup batch script, version 3.2.0 +# +# Required ENV vars: +# ------------------ +# JAVA_HOME - location of a JDK home dir +# +# Optional ENV vars +# ----------------- +# MAVEN_OPTS - parameters passed to the Java VM when running Maven +# e.g. to debug Maven itself, use +# set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +# MAVEN_SKIP_RC - flag to disable loading of mavenrc files +# ---------------------------------------------------------------------------- + +if [ -z "$MAVEN_SKIP_RC" ] ; then + + if [ -f /usr/local/etc/mavenrc ] ; then + . /usr/local/etc/mavenrc + fi + + if [ -f /etc/mavenrc ] ; then + . /etc/mavenrc + fi + + if [ -f "$HOME/.mavenrc" ] ; then + . "$HOME/.mavenrc" + fi + +fi + +# OS specific support. $var _must_ be set to either true or false. +cygwin=false; +darwin=false; +mingw=false +case "$(uname)" in + CYGWIN*) cygwin=true ;; + MINGW*) mingw=true;; + Darwin*) darwin=true + # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home + # See https://developer.apple.com/library/mac/qa/qa1170/_index.html + if [ -z "$JAVA_HOME" ]; then + if [ -x "/usr/libexec/java_home" ]; then + JAVA_HOME="$(/usr/libexec/java_home)"; export JAVA_HOME + else + JAVA_HOME="/Library/Java/Home"; export JAVA_HOME + fi + fi + ;; +esac + +if [ -z "$JAVA_HOME" ] ; then + if [ -r /etc/gentoo-release ] ; then + JAVA_HOME=$(java-config --jre-home) + fi +fi + +# For Cygwin, ensure paths are in UNIX format before anything is touched +if $cygwin ; then + [ -n "$JAVA_HOME" ] && + JAVA_HOME=$(cygpath --unix "$JAVA_HOME") + [ -n "$CLASSPATH" ] && + CLASSPATH=$(cygpath --path --unix "$CLASSPATH") +fi + +# For Mingw, ensure paths are in UNIX format before anything is touched +if $mingw ; then + [ -n "$JAVA_HOME" ] && [ -d "$JAVA_HOME" ] && + JAVA_HOME="$(cd "$JAVA_HOME" || (echo "cannot cd into $JAVA_HOME."; exit 1); pwd)" +fi + +if [ -z "$JAVA_HOME" ]; then + javaExecutable="$(which javac)" + if [ -n "$javaExecutable" ] && ! [ "$(expr "\"$javaExecutable\"" : '\([^ ]*\)')" = "no" ]; then + # readlink(1) is not available as standard on Solaris 10. + readLink=$(which readlink) + if [ ! "$(expr "$readLink" : '\([^ ]*\)')" = "no" ]; then + if $darwin ; then + javaHome="$(dirname "\"$javaExecutable\"")" + javaExecutable="$(cd "\"$javaHome\"" && pwd -P)/javac" + else + javaExecutable="$(readlink -f "\"$javaExecutable\"")" + fi + javaHome="$(dirname "\"$javaExecutable\"")" + javaHome=$(expr "$javaHome" : '\(.*\)/bin') + JAVA_HOME="$javaHome" + export JAVA_HOME + fi + fi +fi + +if [ -z "$JAVACMD" ] ; then + if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + else + JAVACMD="$(\unset -f command 2>/dev/null; \command -v java)" + fi +fi + +if [ ! -x "$JAVACMD" ] ; then + echo "Error: JAVA_HOME is not defined correctly." >&2 + echo " We cannot execute $JAVACMD" >&2 + exit 1 +fi + +if [ -z "$JAVA_HOME" ] ; then + echo "Warning: JAVA_HOME environment variable is not set." +fi + +# traverses directory structure from process work directory to filesystem root +# first directory with .mvn subdirectory is considered project base directory +find_maven_basedir() { + if [ -z "$1" ] + then + echo "Path not specified to find_maven_basedir" + return 1 + fi + + basedir="$1" + wdir="$1" + while [ "$wdir" != '/' ] ; do + if [ -d "$wdir"/.mvn ] ; then + basedir=$wdir + break + fi + # workaround for JBEAP-8937 (on Solaris 10/Sparc) + if [ -d "${wdir}" ]; then + wdir=$(cd "$wdir/.." || exit 1; pwd) + fi + # end of workaround + done + printf '%s' "$(cd "$basedir" || exit 1; pwd)" +} + +# concatenates all lines of a file +concat_lines() { + if [ -f "$1" ]; then + # Remove \r in case we run on Windows within Git Bash + # and check out the repository with auto CRLF management + # enabled. Otherwise, we may read lines that are delimited with + # \r\n and produce $'-Xarg\r' rather than -Xarg due to word + # splitting rules. + tr -s '\r\n' ' ' < "$1" + fi +} + +log() { + if [ "$MVNW_VERBOSE" = true ]; then + printf '%s\n' "$1" + fi +} + +BASE_DIR=$(find_maven_basedir "$(dirname "$0")") +if [ -z "$BASE_DIR" ]; then + exit 1; +fi + +MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}; export MAVEN_PROJECTBASEDIR +log "$MAVEN_PROJECTBASEDIR" + +########################################################################################## +# Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +# This allows using the maven wrapper in projects that prohibit checking in binary data. +########################################################################################## +wrapperJarPath="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" +if [ -r "$wrapperJarPath" ]; then + log "Found $wrapperJarPath" +else + log "Couldn't find $wrapperJarPath, downloading it ..." + + if [ -n "$MVNW_REPOURL" ]; then + wrapperUrl="$MVNW_REPOURL/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" + else + wrapperUrl="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" + fi + while IFS="=" read -r key value; do + # Remove '\r' from value to allow usage on windows as IFS does not consider '\r' as a separator ( considers space, tab, new line ('\n'), and custom '=' ) + safeValue=$(echo "$value" | tr -d '\r') + case "$key" in (wrapperUrl) wrapperUrl="$safeValue"; break ;; + esac + done < "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties" + log "Downloading from: $wrapperUrl" + + if $cygwin; then + wrapperJarPath=$(cygpath --path --windows "$wrapperJarPath") + fi + + if command -v wget > /dev/null; then + log "Found wget ... using wget" + [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--quiet" + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + wget $QUIET "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + else + wget $QUIET --http-user="$MVNW_USERNAME" --http-password="$MVNW_PASSWORD" "$wrapperUrl" -O "$wrapperJarPath" || rm -f "$wrapperJarPath" + fi + elif command -v curl > /dev/null; then + log "Found curl ... using curl" + [ "$MVNW_VERBOSE" = true ] && QUIET="" || QUIET="--silent" + if [ -z "$MVNW_USERNAME" ] || [ -z "$MVNW_PASSWORD" ]; then + curl $QUIET -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath" + else + curl $QUIET --user "$MVNW_USERNAME:$MVNW_PASSWORD" -o "$wrapperJarPath" "$wrapperUrl" -f -L || rm -f "$wrapperJarPath" + fi + else + log "Falling back to using Java to download" + javaSource="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.java" + javaClass="$MAVEN_PROJECTBASEDIR/.mvn/wrapper/MavenWrapperDownloader.class" + # For Cygwin, switch paths to Windows format before running javac + if $cygwin; then + javaSource=$(cygpath --path --windows "$javaSource") + javaClass=$(cygpath --path --windows "$javaClass") + fi + if [ -e "$javaSource" ]; then + if [ ! -e "$javaClass" ]; then + log " - Compiling MavenWrapperDownloader.java ..." + ("$JAVA_HOME/bin/javac" "$javaSource") + fi + if [ -e "$javaClass" ]; then + log " - Running MavenWrapperDownloader.java ..." + ("$JAVA_HOME/bin/java" -cp .mvn/wrapper MavenWrapperDownloader "$wrapperUrl" "$wrapperJarPath") || rm -f "$wrapperJarPath" + fi + fi + fi +fi +########################################################################################## +# End of extension +########################################################################################## + +# If specified, validate the SHA-256 sum of the Maven wrapper jar file +wrapperSha256Sum="" +while IFS="=" read -r key value; do + case "$key" in (wrapperSha256Sum) wrapperSha256Sum=$value; break ;; + esac +done < "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.properties" +if [ -n "$wrapperSha256Sum" ]; then + wrapperSha256Result=false + if command -v sha256sum > /dev/null; then + if echo "$wrapperSha256Sum $wrapperJarPath" | sha256sum -c > /dev/null 2>&1; then + wrapperSha256Result=true + fi + elif command -v shasum > /dev/null; then + if echo "$wrapperSha256Sum $wrapperJarPath" | shasum -a 256 -c > /dev/null 2>&1; then + wrapperSha256Result=true + fi + else + echo "Checksum validation was requested but neither 'sha256sum' or 'shasum' are available." + echo "Please install either command, or disable validation by removing 'wrapperSha256Sum' from your maven-wrapper.properties." + exit 1 + fi + if [ $wrapperSha256Result = false ]; then + echo "Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised." >&2 + echo "Investigate or delete $wrapperJarPath to attempt a clean download." >&2 + echo "If you updated your Maven version, you need to update the specified wrapperSha256Sum property." >&2 + exit 1 + fi +fi + +MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS" + +# For Cygwin, switch paths to Windows format before running java +if $cygwin; then + [ -n "$JAVA_HOME" ] && + JAVA_HOME=$(cygpath --path --windows "$JAVA_HOME") + [ -n "$CLASSPATH" ] && + CLASSPATH=$(cygpath --path --windows "$CLASSPATH") + [ -n "$MAVEN_PROJECTBASEDIR" ] && + MAVEN_PROJECTBASEDIR=$(cygpath --path --windows "$MAVEN_PROJECTBASEDIR") +fi + +# Provide a "standardized" way to retrieve the CLI args that will +# work with both Windows and non-Windows executions. +MAVEN_CMD_LINE_ARGS="$MAVEN_CONFIG $*" +export MAVEN_CMD_LINE_ARGS + +WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +# shellcheck disable=SC2086 # safe args +exec "$JAVACMD" \ + $MAVEN_OPTS \ + $MAVEN_DEBUG_OPTS \ + -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \ + "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \ + ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@" diff --git a/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/mvnw.cmd b/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/mvnw.cmd new file mode 100644 index 000000000..f80fbad3e --- /dev/null +++ b/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/mvnw.cmd @@ -0,0 +1,205 @@ +@REM ---------------------------------------------------------------------------- +@REM Licensed to the Apache Software Foundation (ASF) under one +@REM or more contributor license agreements. See the NOTICE file +@REM distributed with this work for additional information +@REM regarding copyright ownership. The ASF licenses this file +@REM to you under the Apache License, Version 2.0 (the +@REM "License"); you may not use this file except in compliance +@REM with the License. You may obtain a copy of the License at +@REM +@REM http://www.apache.org/licenses/LICENSE-2.0 +@REM +@REM Unless required by applicable law or agreed to in writing, +@REM software distributed under the License is distributed on an +@REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +@REM KIND, either express or implied. See the License for the +@REM specific language governing permissions and limitations +@REM under the License. +@REM ---------------------------------------------------------------------------- + +@REM ---------------------------------------------------------------------------- +@REM Apache Maven Wrapper startup batch script, version 3.2.0 +@REM +@REM Required ENV vars: +@REM JAVA_HOME - location of a JDK home dir +@REM +@REM Optional ENV vars +@REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands +@REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a keystroke before ending +@REM MAVEN_OPTS - parameters passed to the Java VM when running Maven +@REM e.g. to debug Maven itself, use +@REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 +@REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files +@REM ---------------------------------------------------------------------------- + +@REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on' +@echo off +@REM set title of command window +title %0 +@REM enable echoing by setting MAVEN_BATCH_ECHO to 'on' +@if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO% + +@REM set %HOME% to equivalent of $HOME +if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%") + +@REM Execute a user defined script before this one +if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre +@REM check for pre script, once with legacy .bat ending and once with .cmd ending +if exist "%USERPROFILE%\mavenrc_pre.bat" call "%USERPROFILE%\mavenrc_pre.bat" %* +if exist "%USERPROFILE%\mavenrc_pre.cmd" call "%USERPROFILE%\mavenrc_pre.cmd" %* +:skipRcPre + +@setlocal + +set ERROR_CODE=0 + +@REM To isolate internal variables from possible post scripts, we use another setlocal +@setlocal + +@REM ==== START VALIDATION ==== +if not "%JAVA_HOME%" == "" goto OkJHome + +echo. +echo Error: JAVA_HOME not found in your environment. >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +:OkJHome +if exist "%JAVA_HOME%\bin\java.exe" goto init + +echo. +echo Error: JAVA_HOME is set to an invalid directory. >&2 +echo JAVA_HOME = "%JAVA_HOME%" >&2 +echo Please set the JAVA_HOME variable in your environment to match the >&2 +echo location of your Java installation. >&2 +echo. +goto error + +@REM ==== END VALIDATION ==== + +:init + +@REM Find the project base dir, i.e. the directory that contains the folder ".mvn". +@REM Fallback to current working directory if not found. + +set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR% +IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir + +set EXEC_DIR=%CD% +set WDIR=%EXEC_DIR% +:findBaseDir +IF EXIST "%WDIR%"\.mvn goto baseDirFound +cd .. +IF "%WDIR%"=="%CD%" goto baseDirNotFound +set WDIR=%CD% +goto findBaseDir + +:baseDirFound +set MAVEN_PROJECTBASEDIR=%WDIR% +cd "%EXEC_DIR%" +goto endDetectBaseDir + +:baseDirNotFound +set MAVEN_PROJECTBASEDIR=%EXEC_DIR% +cd "%EXEC_DIR%" + +:endDetectBaseDir + +IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig + +@setlocal EnableExtensions EnableDelayedExpansion +for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a +@endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS% + +:endReadAdditionalConfig + +SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe" +set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar" +set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain + +set WRAPPER_URL="https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" + +FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperUrl" SET WRAPPER_URL=%%B +) + +@REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central +@REM This allows using the maven wrapper in projects that prohibit checking in binary data. +if exist %WRAPPER_JAR% ( + if "%MVNW_VERBOSE%" == "true" ( + echo Found %WRAPPER_JAR% + ) +) else ( + if not "%MVNW_REPOURL%" == "" ( + SET WRAPPER_URL="%MVNW_REPOURL%/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar" + ) + if "%MVNW_VERBOSE%" == "true" ( + echo Couldn't find %WRAPPER_JAR%, downloading it ... + echo Downloading from: %WRAPPER_URL% + ) + + powershell -Command "&{"^ + "$webclient = new-object System.Net.WebClient;"^ + "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^ + "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^ + "}"^ + "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%WRAPPER_URL%', '%WRAPPER_JAR%')"^ + "}" + if "%MVNW_VERBOSE%" == "true" ( + echo Finished downloading %WRAPPER_JAR% + ) +) +@REM End of extension + +@REM If specified, validate the SHA-256 sum of the Maven wrapper jar file +SET WRAPPER_SHA_256_SUM="" +FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO ( + IF "%%A"=="wrapperSha256Sum" SET WRAPPER_SHA_256_SUM=%%B +) +IF NOT %WRAPPER_SHA_256_SUM%=="" ( + powershell -Command "&{"^ + "$hash = (Get-FileHash \"%WRAPPER_JAR%\" -Algorithm SHA256).Hash.ToLower();"^ + "If('%WRAPPER_SHA_256_SUM%' -ne $hash){"^ + " Write-Output 'Error: Failed to validate Maven wrapper SHA-256, your Maven wrapper might be compromised.';"^ + " Write-Output 'Investigate or delete %WRAPPER_JAR% to attempt a clean download.';"^ + " Write-Output 'If you updated your Maven version, you need to update the specified wrapperSha256Sum property.';"^ + " exit 1;"^ + "}"^ + "}" + if ERRORLEVEL 1 goto error +) + +@REM Provide a "standardized" way to retrieve the CLI args that will +@REM work with both Windows and non-Windows executions. +set MAVEN_CMD_LINE_ARGS=%* + +%MAVEN_JAVA_EXE% ^ + %JVM_CONFIG_MAVEN_PROPS% ^ + %MAVEN_OPTS% ^ + %MAVEN_DEBUG_OPTS% ^ + -classpath %WRAPPER_JAR% ^ + "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" ^ + %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %* +if ERRORLEVEL 1 goto error +goto end + +:error +set ERROR_CODE=1 + +:end +@endlocal & set ERROR_CODE=%ERROR_CODE% + +if not "%MAVEN_SKIP_RC%"=="" goto skipRcPost +@REM check for post script, once with legacy .bat ending and once with .cmd ending +if exist "%USERPROFILE%\mavenrc_post.bat" call "%USERPROFILE%\mavenrc_post.bat" +if exist "%USERPROFILE%\mavenrc_post.cmd" call "%USERPROFILE%\mavenrc_post.cmd" +:skipRcPost + +@REM pause the script if MAVEN_BATCH_PAUSE is set to 'on' +if "%MAVEN_BATCH_PAUSE%"=="on" pause + +if "%MAVEN_TERMINATE_CMD%"=="on" exit %ERROR_CODE% + +cmd /C exit /B %ERROR_CODE% diff --git a/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/pom.xml b/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/pom.xml new file mode 100644 index 000000000..ac88cb596 --- /dev/null +++ b/src/test/resources/org/datadog/jenkins/plugins/datadog/tracer/test-maven-project/pom.xml @@ -0,0 +1,16 @@ + + + 4.0.0 + + org.example + simplest-maven-project + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + \ No newline at end of file