Skip to content

Commit 3a7e706

Browse files
author
Mark Kuhn
authored
Fix: canary not running (#118)
* replace javatuples.pair with map for custom headers
1 parent ed6ceda commit 3a7e706

File tree

4 files changed

+41
-31
lines changed

4 files changed

+41
-31
lines changed

canarytests/agent/build.gradle

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1+
/*
2+
* Copyright 2020 Amazon.com, Inc. or its affiliates.
3+
* Licensed under the Apache License, Version 2.0 (the
4+
* "License"); you may not use this file except in compliance
5+
* with the License. You may obtain a copy of the License at
6+
* http://www.apache.org/licenses/LICENSE-2.0
7+
* Unless required by applicable law or agreed to in writing, software
8+
* distributed under the License is distributed on an "AS IS" BASIS,
9+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
* See the License for the specific language governing permissions and
11+
* limitations under the License.
12+
*
13+
*/
14+
115
plugins {
216
id 'java'
317
}
418

519
repositories {
620
jcenter()
21+
mavenCentral()
722
}
823

924
dependencies {
1025
implementation "software.amazon.awssdk:cloudwatch:2.13.54"
1126
implementation project(path:rootProject.path, configuration:'archives')
12-
implementation "org.apache.logging.log4j:log4j-api:2.13.3"
27+
implementation "org.apache.logging.log4j:log4j-api:2.13.3"
1328
implementation "org.apache.logging.log4j:log4j-core:2.13.3"
1429
implementation "org.apache.logging.log4j:log4j-slf4j-impl:2.13.3"
1530
}

src/main/java/software/amazon/cloudwatchlogs/emf/environment/EC2Environment.java

+7-13
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
2020
import java.net.URI;
2121
import java.util.Collections;
22+
import java.util.Map;
2223
import lombok.Data;
2324
import lombok.extern.slf4j.Slf4j;
24-
import org.javatuples.Pair;
2525
import software.amazon.cloudwatchlogs.emf.Constants;
2626
import software.amazon.cloudwatchlogs.emf.config.Configuration;
2727
import software.amazon.cloudwatchlogs.emf.exception.EMFClientException;
@@ -52,8 +52,8 @@ public class EC2Environment extends AgentBasedEnvironment {
5252
@Override
5353
public boolean probe() {
5454
String token;
55-
Pair<String, String> tokenRequestHeader =
56-
new Pair<>(TOKEN_REQUEST_HEADER_KEY, TOKEN_REQUEST_HEADER_VALUE);
55+
Map<String, String> tokenRequestHeader =
56+
Collections.singletonMap(TOKEN_REQUEST_HEADER_KEY, TOKEN_REQUEST_HEADER_VALUE);
5757

5858
URI tokenEndpoint = null;
5959
try {
@@ -63,16 +63,14 @@ public boolean probe() {
6363
return false;
6464
}
6565
try {
66-
token =
67-
fetcher.fetch(
68-
tokenEndpoint, "PUT", Collections.singletonList(tokenRequestHeader));
66+
token = fetcher.fetch(tokenEndpoint, "PUT", tokenRequestHeader);
6967
} catch (EMFClientException ex) {
7068
log.debug("Failed to get response from: " + tokenEndpoint, ex);
7169
return false;
7270
}
7371

74-
Pair<String, String> metadataRequestTokenHeader =
75-
new Pair<>(METADATA_REQUEST_TOKEN_HEADER_KEY, token);
72+
Map<String, String> metadataRequestTokenHeader =
73+
Collections.singletonMap(METADATA_REQUEST_TOKEN_HEADER_KEY, token);
7674
URI endpoint = null;
7775
try {
7876
endpoint = new URI(INSTANCE_IDENTITY_URL);
@@ -82,11 +80,7 @@ public boolean probe() {
8280
}
8381
try {
8482
metadata =
85-
fetcher.fetch(
86-
endpoint,
87-
"GET",
88-
EC2Metadata.class,
89-
Collections.singletonList(metadataRequestTokenHeader));
83+
fetcher.fetch(endpoint, "GET", EC2Metadata.class, metadataRequestTokenHeader);
9084
return true;
9185
} catch (EMFClientException ex) {
9286
log.debug("Failed to get response from: " + endpoint, ex);

src/main/java/software/amazon/cloudwatchlogs/emf/environment/ResourceFetcher.java

+11-10
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@
2424
import java.net.Proxy;
2525
import java.net.URI;
2626
import java.util.Collections;
27-
import java.util.List;
27+
import java.util.Map;
2828
import lombok.extern.slf4j.Slf4j;
29-
import org.javatuples.Pair;
3029
import software.amazon.cloudwatchlogs.emf.exception.EMFClientException;
3130
import software.amazon.cloudwatchlogs.emf.util.IOUtils;
3231
import software.amazon.cloudwatchlogs.emf.util.Jackson;
@@ -36,21 +35,21 @@ public class ResourceFetcher {
3635

3736
/** Fetch a json object from a given uri and deserialize it to the specified class: clazz. */
3837
<T> T fetch(URI endpoint, Class<T> clazz) {
39-
String response = doReadResource(endpoint, "GET", Collections.emptyList());
38+
String response = doReadResource(endpoint, "GET", Collections.emptyMap());
4039
return Jackson.fromJsonString(response, clazz);
4140
}
4241

4342
/**
4443
* Request a json object from a given uri with the provided headers and deserialize it to the
4544
* specified class: clazz.
4645
*/
47-
<T> T fetch(URI endpoint, String method, Class<T> clazz, List<Pair<String, String>> headers) {
46+
<T> T fetch(URI endpoint, String method, Class<T> clazz, Map<String, String> headers) {
4847
String response = doReadResource(endpoint, method, headers);
4948
return Jackson.fromJsonString(response, clazz);
5049
}
5150

5251
/** Request a string from a given uri with the provided headers */
53-
String fetch(URI endpoint, String method, List<Pair<String, String>> headers) {
52+
String fetch(URI endpoint, String method, Map<String, String> headers) {
5453
return doReadResource(endpoint, method, headers);
5554
}
5655

@@ -59,11 +58,11 @@ String fetch(URI endpoint, String method, List<Pair<String, String>> headers) {
5958
* Jackson ObjectMapper.
6059
*/
6160
<T> T fetch(URI endpoint, ObjectMapper objectMapper, Class<T> clazz) {
62-
String response = doReadResource(endpoint, "GET", Collections.emptyList());
61+
String response = doReadResource(endpoint, "GET", Collections.emptyMap());
6362
return Jackson.fromJsonString(response, objectMapper, clazz);
6463
}
6564

66-
private String doReadResource(URI endpoint, String method, List<Pair<String, String>> headers) {
65+
private String doReadResource(URI endpoint, String method, Map<String, String> headers) {
6766
InputStream inputStream = null;
6867
try {
6968
HttpURLConnection connection = connectToEndpoint(endpoint, method, headers);
@@ -122,15 +121,17 @@ private void handleErrorResponse(InputStream errorStream, String responseMessage
122121
}
123122

124123
private HttpURLConnection connectToEndpoint(
125-
URI endpoint, String method, List<Pair<String, String>> headers) throws IOException {
124+
URI endpoint, String method, Map<String, String> headers) throws IOException {
126125
HttpURLConnection connection =
127126
(HttpURLConnection) endpoint.toURL().openConnection(Proxy.NO_PROXY);
128127
connection.setConnectTimeout(1000);
129128
connection.setReadTimeout(1000);
130129
connection.setRequestMethod(method);
131130
connection.setDoOutput(true);
132-
headers.forEach(
133-
header -> connection.setRequestProperty(header.getValue0(), header.getValue1()));
131+
132+
for (Map.Entry<String, String> header : headers.entrySet()) {
133+
connection.setRequestProperty(header.getKey(), header.getValue());
134+
}
134135

135136
connection.connect();
136137

src/test/java/software/amazon/cloudwatchlogs/emf/environment/ResourceFetcherTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import java.net.URI;
2828
import java.net.URISyntaxException;
2929
import java.util.Collections;
30+
import java.util.Map;
3031
import lombok.Data;
31-
import org.javatuples.Pair;
3232
import org.junit.Before;
3333
import org.junit.ClassRule;
3434
import org.junit.Test;
@@ -98,11 +98,10 @@ public void testReadDataWith200Response() {
9898

9999
@Test
100100
public void testReadDataWithHeaders200Response() {
101-
Pair<String, String> mockHeader = new Pair<>("X-mock-header-key", "headerValue");
101+
Map<String, String> mockHeader =
102+
Collections.singletonMap("X-mock-header-key", "headerValue");
102103
generateStub(200, "{\"name\":\"test\",\"size\":10}");
103-
TestData data =
104-
fetcher.fetch(
105-
endpoint, "GET", TestData.class, Collections.singletonList(mockHeader));
104+
TestData data = fetcher.fetch(endpoint, "GET", TestData.class, mockHeader);
106105

107106
verify(
108107
getRequestedFor(urlEqualTo(endpoint_path))
@@ -114,8 +113,9 @@ public void testReadDataWithHeaders200Response() {
114113
@Test
115114
public void testWithProvidedMethodAndHeadersWith200Response() {
116115
generatePutStub(200, "putResponseData");
117-
Pair<String, String> mockHeader = new Pair<>("X-mock-header-key", "headerValue");
118-
String data = fetcher.fetch(endpoint, "PUT", Collections.singletonList(mockHeader));
116+
Map<String, String> mockHeader =
117+
Collections.singletonMap("X-mock-header-key", "headerValue");
118+
String data = fetcher.fetch(endpoint, "PUT", mockHeader);
119119

120120
verify(
121121
putRequestedFor(urlEqualTo(endpoint_path))

0 commit comments

Comments
 (0)