Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit 83942e4

Browse files
committed
Support for https
Added new file DockerCertificate.java to manage all the work related to certificates Drop support for 1.6 and will only support for 1.7 onwards docker.properties - Add new properties for certificates pom.xml - Added dependency on BouncyCastle and Apache HTTPCommon Component sample.properties is updated accordingly with docker.properties samplehttps.properties - Another sample property for HTTPs connection
1 parent 34bcc36 commit 83942e4

File tree

9 files changed

+485
-34
lines changed

9 files changed

+485
-34
lines changed

docker-monitoring-eparest/.classpath

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
<attribute name="maven.pomderived" value="true"/>
77
</attributes>
88
</classpathentry>
9-
10-
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
9+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
1110
<attributes>
1211
<attribute name="maven.pomderived" value="true"/>
1312
</attributes>

docker-monitoring-eparest/docker.properties

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,27 @@ apm.apihost=
1818
# There is no default value.
1919
apm.apiport=
2020

21+
# CA key file name
22+
# If you configure docker using https://docs.docker.com/articles/https/, mention the
23+
# file path location of ca.pem
24+
25+
docker.ca.key=
26+
27+
# Client key file name
28+
# If you configure docker using https://docs.docker.com/articles/https/, mention the
29+
# file path location of key.pem
30+
31+
docker.client.key=
32+
33+
# Certificate file name
34+
# If you configure docker using https://docs.docker.com/articles/https/, mention the
35+
# file path location of cert.pem
36+
37+
docker.client.certificate=
38+
39+
# Password for the keystore
40+
# If you configure docker using https://docs.docker.com/articles/https/, mention the
41+
# keystore password that used while configuring the keys in plain text
42+
43+
docker.keystore.password=
44+

docker-monitoring-eparest/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
<version>${gson.version}</version>
2828
<optional>true</optional>
2929
</dependency>
30+
<dependency>
31+
<groupId>org.bouncycastle</groupId>
32+
<artifactId>bcpkix-jdk15on</artifactId>
33+
<version>1.51</version>
34+
</dependency>
3035
<dependency>
3136
<groupId>com.google.guava</groupId>
3237
<artifactId>guava</artifactId>
@@ -48,6 +53,11 @@
4853
<artifactId>commons-httpclient</artifactId>
4954
<version>3.1</version>
5055
</dependency>
56+
<dependency>
57+
<groupId>org.apache.httpcomponents</groupId>
58+
<artifactId>httpclient</artifactId>
59+
<version>4.3.5</version>
60+
</dependency>
5161
</dependencies>
5262

5363
<build>

docker-monitoring-eparest/sample.properties

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,22 @@ apm.apihost=bhaab01-U148604
1818
# There is no default value.
1919
apm.apiport=8080
2020

21+
# CA key file name
22+
# If you configure docker using https://docs.docker.com/articles/https/, mention the
23+
# file path location of ca.pem
24+
docker.ca.key=
25+
26+
# Client key file name
27+
# If you configure docker using https://docs.docker.com/articles/https/, mention the
28+
# file path location of key.pem
29+
docker.client.key=
30+
31+
# Certificate file name
32+
# If you configure docker using https://docs.docker.com/articles/https/, mention the
33+
# file path location of cert.pem
34+
docker.client.certificate=
35+
36+
# Password for the keystore
37+
# If you configure docker using https://docs.docker.com/articles/https/, mention the
38+
# keystore password that used while configuring the keys in plain text
39+
docker.keystore.password=
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Name or IP address of the docker server host.
2+
# This parameter is required. There is no default value.
3+
docker.hostname=bhaab01-U150071
4+
5+
# Port where docker application is listening. This parameter is required.
6+
# There is no default value.
7+
docker.port=2376
8+
9+
# Data collection interval in seconds. Value should be greater than 5.
10+
# If not, the docker collector will reset this to 60
11+
# This parameter is required. There is no default value.
12+
docker.interval.seconds=15
13+
14+
# Hostname or IP address of APM API server. This parameter is required.
15+
# There is no default value.
16+
apm.apihost=bhaab01-U148604
17+
# Port for APM API server. This parameter is required.
18+
# There is no default value.
19+
apm.apiport=8080
20+
21+
# CA key file name
22+
# If you configure docker using https://docs.docker.com/articles/https/, mention the
23+
# file path location of ca.pem
24+
docker.ca.key=C:\\apm-dev\\source\\docker\\ca.pem
25+
26+
# Client key file name
27+
# If you configure docker using https://docs.docker.com/articles/https/, mention the
28+
# file path location of key.pem
29+
docker.client.key=C:\\apm-dev\\source\\docker\\key.pem
30+
31+
# Certificate file name
32+
# If you configure docker using https://docs.docker.com/articles/https/, mention the
33+
# file path location of cert.pem
34+
docker.client.certificate=C:\\apm-dev\\source\\docker\\cert.pem
35+
36+
# Password for the keystore
37+
# If you configure docker using https://docs.docker.com/articles/https/, mention the
38+
# keystore password that used while configuring the keys in plain text
39+
docker.keystore.password="Notallowed@1234"

docker-monitoring-eparest/src/main/java/com/ca/docker/Constants.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,26 @@ public class Constants
1212
public static final String DOCKER_PORT_PROP = "docker.port";
1313
public static final String APM_HOST_PROP = "apm.apihost";
1414
public static final String APM_PORT_PROP = "apm.apiport";
15+
1516
public static final String DOCKER_POLLING_INTERVAL = "docker.interval.seconds";
16-
public static final String DOCKER_CONTAINER_INFO = "/containers/json?all=1";
17-
public static final String DOCKER_HOST_INFO = "/info";
1817

18+
public static final String DOCKER_CA_KEY = "docker.ca.key";
19+
public static final String DOCKER_CLIENT_KEY = "docker.client.key";
20+
public static final String DOCKER_CLIENT_CERTIFIACTE = "docker.client.certificate";
21+
public static final String DOCKER_KEYSTORE_PASSWORD = "docker.keystore.password";
22+
23+
public static final String DOCKER_CONTAINER_INFO = "/containers/json?all=1";
24+
public static final String DOCKER_HOST_INFO = "/info";
1925
public static final int DOCKER_DEFAULT_POLLING_INTERVAL = 60;
26+
2027
public static final String PIPE = "|";
2128
public static final String COLON = ":";
2229
public static final String UNDER_SCORE = "_";
2330
public static final String SEMI_COLON = ";";
2431

32+
public static final String HTTP = "http://";
33+
public static final String HTTPS = "https://";
34+
35+
2536

2637
}

docker-monitoring-eparest/src/main/java/com/ca/docker/DataPoller.java

Lines changed: 93 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
import java.util.logging.Level;
2121
import java.util.logging.Logger;
2222

23+
import org.apache.http.HttpEntity;
24+
import org.apache.http.HttpResponse;
25+
import org.apache.http.client.HttpClient;
26+
import org.apache.http.client.methods.HttpGet;
27+
import org.apache.http.impl.client.CloseableHttpClient;
28+
import org.apache.http.impl.client.HttpClients;
29+
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
2330
import org.codehaus.jackson.JsonNode;
2431
import org.codehaus.jackson.map.ObjectMapper;
2532
import org.codehaus.jackson.node.BooleanNode;
@@ -284,10 +291,13 @@ private void makeMetric(final String metricPath,
284291
private HostInfo readHostInfoJsonFromUrl(String dockerHostInfo)
285292
{
286293
// TODO Auto-generated method stub
287-
294+
String json;
288295
try
289296
{
290-
String json = readUrl(dockerHostInfo, true);
297+
if (DockerMonitor.sslEnabled)
298+
json = readHttpsUrl(dockerHostInfo, true);
299+
else
300+
json = readUrl(dockerHostInfo, true);
291301

292302
Gson gson = new Gson();
293303
HostInfo hostInfo = gson.fromJson(json, HostInfo.class);
@@ -319,7 +329,10 @@ private ArrayList<Container> readContainerInfoJsonFromUrl(String relativePath)
319329
String json;
320330
try
321331
{
322-
json = readUrl(relativePath, true);
332+
if (DockerMonitor.sslEnabled)
333+
json = readHttpsUrl(relativePath, true);
334+
else
335+
json = readUrl(relativePath, true);
323336
Gson gson = new Gson();
324337
JsonParser parser = new JsonParser();
325338
JsonArray jArray = parser.parse(json).getAsJsonArray();
@@ -343,7 +356,8 @@ private ArrayList<Container> readContainerInfoJsonFromUrl(String relativePath)
343356
lcs.add(cse);
344357
}
345358
// Containers page = gson.fromJson(json, Containers.class);
346-
hostInfo.updateContainerInfo(upContainer, downContainer, getCurrentTime());
359+
hostInfo.updateContainerInfo(upContainer, downContainer,
360+
getCurrentTime());
347361
return lcs;
348362
} catch (Exception e)
349363
{
@@ -353,10 +367,11 @@ private ArrayList<Container> readContainerInfoJsonFromUrl(String relativePath)
353367
return null;
354368

355369
}
356-
/**
357-
*
358-
* @return a String showing current local time
359-
*/
370+
371+
/**
372+
*
373+
* @return a String showing current local time
374+
*/
360375
private String getCurrentTime()
361376
{
362377
// TODO Auto-generated method stub
@@ -418,9 +433,14 @@ private void getContainerResourceStats(String id, String names)
418433
*/
419434
private void readStatInfoJsonFromUrl(String resourcePath, String names)
420435
{
436+
String json = null;
421437
try
422438
{
423-
String json = readUrl(resourcePath, false);
439+
if (DockerMonitor.sslEnabled)
440+
json = readHttpsUrl(resourcePath, false);
441+
else
442+
json = readUrl(resourcePath, false);
443+
// String json = readUrl(resourcePath, false);
424444
ObjectMapper objectMapper = new ObjectMapper();
425445
JsonNode node = objectMapper.readValue(json, JsonNode.class);
426446
ContainerStatInfo csi = new ContainerStatInfo();
@@ -534,11 +554,11 @@ private Double getCPUPercentage(JsonNode node, String containerName)
534554
StringBuilder systemUsageKey = new StringBuilder(containerName);
535555
systemUsageKey.append(Constants.PIPE);
536556
systemUsageKey.append("system_cpu_usage");
537-
538-
Double prevTotalUsage = oldMetricsMap
539-
.getIfPresent(totalUsageKey.toString());
540-
Double prevSystemUsage = oldMetricsMap
541-
.getIfPresent(systemUsageKey);
557+
558+
Double prevTotalUsage = oldMetricsMap.getIfPresent(totalUsageKey
559+
.toString());
560+
Double prevSystemUsage = oldMetricsMap.getIfPresent(systemUsageKey
561+
.toString());
542562
if (prevSystemUsage != null && prevTotalUsage != null
543563
&& totalUsage != null && systemUsage != null)
544564
{
@@ -670,4 +690,63 @@ private String readUrl(String urlString, Boolean readfully)
670690
if (reader != null) reader.close();
671691
}
672692
}
693+
694+
/**
695+
* In case of stats query, readfully is set to false as we just want to read
696+
* that snapshot data and don't want to read the entire content
697+
*
698+
* @param urlString
699+
* @param readfully
700+
* @return
701+
* @throws Exception
702+
*/
703+
private String readHttpsUrl(String urlString, Boolean readfully)
704+
throws Exception
705+
{
706+
PoolingHttpClientConnectionManager c1Manager = new PoolingHttpClientConnectionManager(
707+
DockerMonitor
708+
.getSchemeRegistry(DockerMonitor.certificate));
709+
HttpClient httpClient = HttpClients.custom()
710+
.setConnectionManager(c1Manager).build();
711+
StringBuffer sb = new StringBuffer(this.getDockerMonitor()
712+
.getDockerUrl());
713+
sb.append(urlString);
714+
715+
HttpGet request = new HttpGet(sb.toString());
716+
HttpResponse response = httpClient.execute(request);
717+
718+
BufferedReader reader = null;
719+
HttpEntity entity = response.getEntity();
720+
721+
InputStream in = entity.getContent();
722+
InputStreamReader inr = new InputStreamReader(in);
723+
try
724+
{
725+
reader = new BufferedReader(inr);
726+
String strLine;
727+
728+
if (!readfully)
729+
{
730+
return reader.readLine();
731+
}
732+
StringBuffer buffer = new StringBuffer();
733+
int read;
734+
char[] chars = new char[1024];
735+
while ((read = reader.read(chars)) != -1)
736+
buffer.append(chars, 0, read);
737+
738+
return buffer.toString();
739+
740+
} finally
741+
{
742+
if (reader != null)
743+
744+
{
745+
if (null != entity) entity.consumeContent();
746+
747+
httpClient.getConnectionManager().shutdown();
748+
749+
}
750+
}
751+
}
673752
}

0 commit comments

Comments
 (0)