Skip to content

Fix: canary not running #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion canarytests/agent/build.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates.
* Licensed 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.
*
*/

plugins {
id 'java'
}

repositories {
jcenter()
mavenCentral()
}

dependencies {
implementation "software.amazon.awssdk:cloudwatch:2.13.54"
implementation project(path:rootProject.path, configuration:'archives')
implementation "org.apache.logging.log4j:log4j-api:2.13.3"
implementation "org.apache.logging.log4j:log4j-api:2.13.3"
implementation "org.apache.logging.log4j:log4j-core:2.13.3"
implementation "org.apache.logging.log4j:log4j-slf4j-impl:2.13.3"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import software.amazon.cloudwatchlogs.emf.config.Configuration;
import software.amazon.cloudwatchlogs.emf.config.EnvironmentConfigurationProvider;
import software.amazon.cloudwatchlogs.emf.environment.Environments;
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;
import software.amazon.cloudwatchlogs.emf.model.DimensionSet;
import software.amazon.cloudwatchlogs.emf.model.Unit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.net.URI;
import java.util.Collections;
import java.util.Map;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.javatuples.Pair;
import software.amazon.cloudwatchlogs.emf.Constants;
import software.amazon.cloudwatchlogs.emf.config.Configuration;
import software.amazon.cloudwatchlogs.emf.exception.EMFClientException;
Expand Down Expand Up @@ -52,8 +52,8 @@ public class EC2Environment extends AgentBasedEnvironment {
@Override
public boolean probe() {
String token;
Pair<String, String> tokenRequestHeader =
new Pair<>(TOKEN_REQUEST_HEADER_KEY, TOKEN_REQUEST_HEADER_VALUE);
Map<String, String> tokenRequestHeader =
Collections.singletonMap(TOKEN_REQUEST_HEADER_KEY, TOKEN_REQUEST_HEADER_VALUE);

URI tokenEndpoint = null;
try {
Expand All @@ -63,16 +63,14 @@ public boolean probe() {
return false;
}
try {
token =
fetcher.fetch(
tokenEndpoint, "PUT", Collections.singletonList(tokenRequestHeader));
token = fetcher.fetch(tokenEndpoint, "PUT", tokenRequestHeader);
} catch (EMFClientException ex) {
log.debug("Failed to get response from: " + tokenEndpoint, ex);
return false;
}

Pair<String, String> metadataRequestTokenHeader =
new Pair<>(METADATA_REQUEST_TOKEN_HEADER_KEY, token);
Map<String, String> metadataRequestTokenHeader =
Collections.singletonMap(METADATA_REQUEST_TOKEN_HEADER_KEY, token);
URI endpoint = null;
try {
endpoint = new URI(INSTANCE_IDENTITY_URL);
Expand All @@ -82,11 +80,7 @@ public boolean probe() {
}
try {
metadata =
fetcher.fetch(
endpoint,
"GET",
EC2Metadata.class,
Collections.singletonList(metadataRequestTokenHeader));
fetcher.fetch(endpoint, "GET", EC2Metadata.class, metadataRequestTokenHeader);
return true;
} catch (EMFClientException ex) {
log.debug("Failed to get response from: " + endpoint, ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
import java.net.Proxy;
import java.net.URI;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.javatuples.Pair;
import software.amazon.cloudwatchlogs.emf.exception.EMFClientException;
import software.amazon.cloudwatchlogs.emf.util.IOUtils;
import software.amazon.cloudwatchlogs.emf.util.Jackson;
Expand All @@ -36,21 +35,21 @@ public class ResourceFetcher {

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

/**
* Request a json object from a given uri with the provided headers and deserialize it to the
* specified class: clazz.
*/
<T> T fetch(URI endpoint, String method, Class<T> clazz, List<Pair<String, String>> headers) {
<T> T fetch(URI endpoint, String method, Class<T> clazz, Map<String, String> headers) {
String response = doReadResource(endpoint, method, headers);
return Jackson.fromJsonString(response, clazz);
}

/** Request a string from a given uri with the provided headers */
String fetch(URI endpoint, String method, List<Pair<String, String>> headers) {
String fetch(URI endpoint, String method, Map<String, String> headers) {
return doReadResource(endpoint, method, headers);
}

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

private String doReadResource(URI endpoint, String method, List<Pair<String, String>> headers) {
private String doReadResource(URI endpoint, String method, Map<String, String> headers) {
InputStream inputStream = null;
try {
HttpURLConnection connection = connectToEndpoint(endpoint, method, headers);
Expand Down Expand Up @@ -122,15 +121,17 @@ private void handleErrorResponse(InputStream errorStream, String responseMessage
}

private HttpURLConnection connectToEndpoint(
URI endpoint, String method, List<Pair<String, String>> headers) throws IOException {
URI endpoint, String method, Map<String, String> headers) throws IOException {
HttpURLConnection connection =
(HttpURLConnection) endpoint.toURL().openConnection(Proxy.NO_PROXY);
connection.setConnectTimeout(1000);
connection.setReadTimeout(1000);
connection.setRequestMethod(method);
connection.setDoOutput(true);
headers.forEach(
header -> connection.setRequestProperty(header.getValue0(), header.getValue1()));

for (Map.Entry<String, String> header : headers.entrySet()) {
connection.setRequestProperty(header.getKey(), header.getValue());
}

connection.connect();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.Map;
import lombok.Data;
import org.javatuples.Pair;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
Expand Down Expand Up @@ -98,11 +98,10 @@ public void testReadDataWith200Response() {

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

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

verify(
putRequestedFor(urlEqualTo(endpoint_path))
Expand Down