Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
filter out cfn tags (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
stlava committed Mar 13, 2020
1 parent b4af98f commit b14378d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
14 changes: 12 additions & 2 deletions core/src/main/java/com/nextdoor/bender/handler/BaseHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.apache.commons.io.FileUtils;
Expand Down Expand Up @@ -159,8 +161,16 @@ public void init(Context ctx) throws HandlerException {
*/
if (config.getHandlerConfig().getIncludeFunctionTags()) {
AWSLambda lambda = this.lambdaClientFactory.newInstance();
ListTagsResult res = lambda.listTags(new ListTagsRequest().withResource(ctx.getInvokedFunctionArn()));
monitor.addTagsMap(res.getTags());
ListTagsResult res =
lambda.listTags(new ListTagsRequest().withResource(ctx.getInvokedFunctionArn()));

monitor.addTagsMap(
/*
* Filter out tags that come from CloudFormation
*/
res.getTags().entrySet().stream()
.filter(map -> !map.getKey().startsWith("aws:cloudformation"))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,42 @@ public void tagsDuplicate() throws HandlerException {
assertTrue(actual.entrySet().containsAll(expected.entrySet()));
}

@Test
public void tagsCloudformation() throws HandlerException {
BaseHandler.CONFIG_FILE = "/config/handler_config_tags_duplicate.json";

TestContext context = new TestContext();
context.setInvokedFunctionArn("arn:aws:lambda:us-east-1:123:function:test:test_tags");

AWSLambdaClientFactory mockFactory = mock(AWSLambdaClientFactory.class);
AWSLambda mockLambda = mock(AWSLambda.class);
doReturn(mockLambda).when(mockFactory).newInstance();

ListTagsResult mockResult = mock(ListTagsResult.class);
HashMap<String, String> functionTags = new HashMap<String, String>() {
{
put("f1", "foo");
put("aws:cloudformation:foo", "foo");
put("aws:cloudformation:bar", "bar");
}
};
doReturn(functionTags).when(mockResult).getTags();
doReturn(mockResult).when(mockLambda).listTags(any());

handler.lambdaClientFactory = mockFactory;
handler.init(context);
Map<String, String> actual = handler.monitor.getTagsMap();

HashMap<String, String> expected = new HashMap<String, String>() {
{
put("f1", "foo");
put("u1", "bar");
}
};

assertTrue(actual.entrySet().containsAll(expected.entrySet()));
}

@Test(expected = HandlerException.class)
public void testBadConfig() throws HandlerException {
BaseHandler.CONFIG_FILE = "/config/handler_config_bad.json";
Expand Down

0 comments on commit b14378d

Please sign in to comment.