Skip to content

Commit

Permalink
Use new http client in getAwsInstanceID method (#360)
Browse files Browse the repository at this point in the history
* Use new http client in getAwsInstanceID method

* Fix new method

* Address review comments
  • Loading branch information
sarah-witt authored Aug 29, 2023
1 parent 0c0fea3 commit 1b0ed46
Showing 1 changed file with 24 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ of this software and associated documentation files (the "Software"), to deal
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.function.Function;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
Expand All @@ -66,6 +67,7 @@ of this software and associated documentation files (the "Software"), to deal
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.datadog.jenkins.plugins.datadog.clients.HttpClient;
import org.datadog.jenkins.plugins.datadog.model.CIGlobalTagsAction;
import org.datadog.jenkins.plugins.datadog.model.GitCommitAction;
import org.datadog.jenkins.plugins.datadog.model.GitRepositoryAction;
Expand Down Expand Up @@ -465,36 +467,21 @@ public static Map<String, Set<String>> computeTagListFromVarList(EnvVars envVars

public static String getAwsInstanceID() throws IOException {
String metadataUrl = "http://169.254.169.254/latest/meta-data/instance-id";
HttpURLConnection conn = null;
String instance_id = null;
HttpClient client = null;
// Make request
conn = getHttpURLConnection(new URL(metadataUrl), 300);
conn.setRequestMethod("GET");

// Get response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
StringBuilder result = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();

// Validate
instance_id = result.toString();
try {
if (conn.getResponseCode() == 404) {
logger.fine("Could not retrieve AWS instance ID");
}
conn.disconnect();
} catch (IOException e) {
logger.info("Failed to inspect HTTP response when getting AWS Instance ID");
}

if (instance_id.equals("")) {
client = new HttpClient(60_000);
String instanceId = client.get(metadataUrl, Collections.emptyMap(), Function.identity());
logger.fine("Instance ID detected: " + instanceId);
return instanceId;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
DatadogUtilities.severe(logger, e, "Could not retrieve the AWS instance ID");
return null;
} catch (Exception e) {
DatadogUtilities.severe(logger, e, "Could not retrieve the AWS instance ID");
return null;
}
return instance_id;
}

/**
Expand Down Expand Up @@ -523,24 +510,17 @@ public static String getHostname(EnvVars envVars) {
} catch (NullPointerException e) {
// noop
}

if (isValidHostname(hostname)) {
logger.fine("Using hostname set in 'Manage Plugins'. Hostname: " + hostname);
return hostname;
}

// Check hostname using jenkins env variables
if (envVars != null) {
hostname = envVars.get("HOSTNAME");
if (isValidHostname(hostname)) {
logger.fine("Using hostname found in $HOSTNAME agent environment variable. Hostname: " + hostname);
return hostname;
}
}

final DatadogGlobalConfiguration datadogGlobalConfig = getDatadogGlobalDescriptor();
if (datadogGlobalConfig != null){
if (datadogGlobalConfig.isUseAwsInstanceHostname()) {
try {
logger.fine("Attempting to resolve AWS instance ID for hostname");
hostname = getAwsInstanceID();
} catch (IOException e) {
logger.fine("Error retrieving AWS hostname: " + e);
Expand All @@ -552,6 +532,15 @@ public static String getHostname(EnvVars envVars) {
}
}

// Check hostname using jenkins env variables
if (envVars != null) {
hostname = envVars.get("HOSTNAME");
if (isValidHostname(hostname)) {
logger.fine("Using hostname found in $HOSTNAME agent environment variable. Hostname: " + hostname);
return hostname;
}
}

if (isValidHostname(hostname)) {
logger.fine("Using hostname found in $HOSTNAME controller environment variable. Hostname: " + hostname);
return hostname;
Expand Down

0 comments on commit 1b0ed46

Please sign in to comment.