Skip to content

Commit

Permalink
Changes to make the ELB handlers more org-agnostic + Sonar violation …
Browse files Browse the repository at this point in the history
…fixes
  • Loading branch information
Daniel Fritz authored and Daniel Fritz committed Jul 20, 2018
1 parent b013e70 commit 10a6e38
Show file tree
Hide file tree
Showing 28 changed files with 482 additions and 495 deletions.
61 changes: 36 additions & 25 deletions src/main/java/com/libertymutualgroup/herman/aws/cft/CftPush.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.amazonaws.services.lambda.AWSLambdaClientBuilder;
import com.amazonaws.services.lambda.model.InvocationType;
import com.amazonaws.services.lambda.model.InvokeRequest;
import com.amazonaws.util.IOUtils;
import com.atlassian.bamboo.deployments.execution.DeploymentTaskContext;
import com.atlassian.bamboo.task.TaskException;
import com.atlassian.bamboo.variable.CustomVariableContext;
Expand All @@ -48,22 +47,22 @@
import com.libertymutualgroup.herman.aws.ecs.TaskContextPropertyHandler;
import com.libertymutualgroup.herman.logging.HermanLogger;
import com.libertymutualgroup.herman.task.cft.CFTPushTaskProperties;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.libertymutualgroup.herman.util.FileUtil;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CftPush {

Expand All @@ -75,6 +74,7 @@ public class CftPush {
private static final String MAVEN_VERS = "maven.version";
private static final int RANDOM_PASSWORD_LENGTH = 20;
private static final int POLLING_INTERVAL_MS = 10000;
private static final List<String> CFT_FILE_NAMES = Arrays.asList("cft.template", "cft.yml", "cft.json");
private Properties props = new Properties();
private Properties output = new Properties();
private HermanLogger buildLogger;
Expand All @@ -86,8 +86,8 @@ public class CftPush {
private CFTPushTaskProperties taskProperties;

public CftPush(HermanLogger buildLogger, DeploymentTaskContext taskContext, AWSCredentials sessionCredentials,
ClientConfiguration config, Regions region, CustomVariableContext customVariableContext,
CFTPushTaskProperties taskProperties) {
ClientConfiguration config, Regions region, CustomVariableContext customVariableContext,
CFTPushTaskProperties taskProperties) {

this.buildLogger = buildLogger;
this.taskContext = taskContext;
Expand All @@ -112,7 +112,8 @@ public void push() throws TaskException {
String projecName = taskContext.getDeploymentContext().getDeploymentProjectName();

if (!this.taskProperties.getCftPushVariableBrokerLambda().isEmpty()) {
buildLogger.addLogEntry("Getting CFT variables from Lambda: " + this.taskProperties.getCftPushVariableBrokerLambda());
buildLogger.addLogEntry(
"Getting CFT variables from Lambda: " + this.taskProperties.getCftPushVariableBrokerLambda());
introspectEnvironment();
}
injectBambooContext();
Expand Down Expand Up @@ -199,16 +200,7 @@ private void injectBambooContext() {
}

private void createStack(String name) {
String root = taskContext.getRootDirectory().getAbsolutePath();
File file = new File(root + File.separator + "cft.template");

String template;
try {
template = IOUtils.toString(new FileInputStream(file));
} catch (IOException e1) {
throw new AwsExecException(e1);
}

String template = getTemplate();
List<Parameter> parameters = convertPropsToCftParams(template);

String deployEnvironment = taskContext.getDeploymentContext().getEnvironmentName();
Expand Down Expand Up @@ -265,9 +257,28 @@ private void createStack(String name) {

}

private String getTemplate() {
String root = taskContext.getRootDirectory().getAbsolutePath();
FileUtil fileUtil = new FileUtil(root, buildLogger);

String template = null;
for (String fileName: CFT_FILE_NAMES) {
boolean fileExists = fileUtil.fileExists(fileName);
if (fileExists) {
template = fileUtil.findFile(fileName, false);
buildLogger.addLogEntry("Template used: " + fileName);
}
}
if (template == null) {
throw new AwsExecException("CloudFormation template not found. Valid file names: "
+ String.join(", ", CFT_FILE_NAMES));
}
return template;
}

private List<Parameter> convertPropsToCftParams(String template) {
List<Parameter> parameters = new ArrayList<>();
for (Object key : props.keySet()) {
for (Object key: props.keySet()) {
if (template.contains((String) key)) {
parameters.add(new Parameter().withParameterKey((String) key)
.withParameterValue(props.getProperty((String) key)));
Expand All @@ -293,7 +304,7 @@ private void introspectEnvironment() throws TaskException {
throw new TaskException(e.getMessage(), e);
}

for (Map.Entry<String, String> entry : variables.entrySet()) {
for (Map.Entry<String, String> entry: variables.entrySet()) {
buildLogger.addLogEntry("Injecting " + entry.getKey() + " = " + entry.getValue());
props.put(entry.getKey(), entry.getValue());
}
Expand All @@ -309,14 +320,14 @@ private void outputStack(String stackName) {

DescribeStackResourcesResult res = cftClient.describeStackResources(req);

for (StackResource r : res.getStackResources()) {
for (StackResource r: res.getStackResources()) {
buildLogger.addLogEntry(r.getPhysicalResourceId());
buildLogger.addLogEntry(r.getResourceType());
output.put("aws.stack." + r.getLogicalResourceId(), r.getPhysicalResourceId());
}

List<String> resources = new ArrayList<>();
for (StackResource r : res.getStackResources()) {
for (StackResource r: res.getStackResources()) {
if ("AWS::ECS::TaskDefinition".equals(r.getResourceType())) {
String id = r.getPhysicalResourceId();
String task = id.split("/")[1];
Expand Down Expand Up @@ -362,7 +373,7 @@ private void sleep() {
}

private Boolean reportStatusAndCheckCompletionOf(List<Stack> stacks) {
for (Stack stack : stacks) {
for (Stack stack: stacks) {
reportStatusOf(stack);
if (stack.getStackStatus().contains("IN_PROGRESS")) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@

public class BambooCredentialsHandler extends CredentialsHandler {

private BambooCredentialsHandler() {
throw new IllegalAccessError("Utility class");
}

public static AWSCredentials getCredentials(CommonTaskContext context) {
if (lookupVar("custom.aws.accessKeyId", context) != null) {
return new BasicSessionCredentials(lookupVar("custom.aws.accessKeyId", context),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;

public class CredentialsHandler {

CredentialsHandler() {
throw new IllegalAccessError("Utility class");
}

public static AWSCredentials getCredentials() {
return new DefaultAWSCredentialsProviderChain().getCredentials();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ public class CliPropertyHandler implements PropertyHandler {

private Set<String> propertyKeysUsed = new HashSet<>();

private HermanLogger logger;
private HermanLogger hermanLogger;
private String environmentName;
private String rootDirectory;
private Map<String, String> customVariables;

public CliPropertyHandler(HermanLogger logger, String environmentName, String rootDirectory, Map<String, String> customVariables) {
this.logger = logger;
public CliPropertyHandler(HermanLogger hermanLogger, String environmentName, String rootDirectory, Map<String, String> customVariables) {
this.hermanLogger = hermanLogger;
this.environmentName = environmentName;
this.rootDirectory = rootDirectory;
this.customVariables = customVariables;
Expand Down Expand Up @@ -132,7 +132,7 @@ public String lookupVariable(String key) {
}

private void importPropFiles() {
FileUtil util = new FileUtil(this.rootDirectory, this.logger);
FileUtil util = new FileUtil(this.rootDirectory, this.hermanLogger);
String envProps = util.findFile(this.environmentName + ".properties", true);

if (props != null && envProps != null) {
Expand All @@ -141,7 +141,7 @@ private void importPropFiles() {
props.load(propStream);
} catch (IOException e) {
LOGGER.debug("Error loading properties file: " + this.environmentName, e);
this.logger.addLogEntry("Error loading " + this.environmentName + ".properties: " + e.getMessage());
this.hermanLogger.addLogEntry("Error loading " + this.environmentName + ".properties: " + e.getMessage());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void push() {
boolean useAlb = decider.shouldUseAlb(definition.getAppName(), definition);

DnsRegistrar dnsRegistrar = new DnsRegistrar(lambdaClient, logger, taskProperties.getDnsBrokerLambda());
CertHandler certHandler = new CertHandler(iamClient, logger, taskProperties.getSslCertificates());
CertHandler certHandler = new CertHandler(logger, taskProperties.getSslCertificates());
if (useAlb) {
EcsLoadBalancerV2Handler loadBalancerV2Handler = new EcsLoadBalancerV2Handler(elbV2Client,
certHandler, dnsRegistrar, logger, taskProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ public void brokerNewRelicApplicationDeployment(
InvokeResult invokeResult = this.lambdaClient.invoke(dnsBrokerInvokeRequest);


if (isSuccessful(invokeResult.getStatusCode()) && StringUtils
.isEmpty(invokeResult.getFunctionError())) {
if (isSuccessful(invokeResult.getStatusCode()) && StringUtils.isEmpty(invokeResult.getFunctionError())) {
String nrBrokerResponseJson = new String(invokeResult.getPayload().array(), Charset.forName("UTF-8"));

NewRelicBrokerResponse response;
Expand All @@ -103,7 +102,8 @@ public void brokerNewRelicApplicationDeployment(
addNewRelicLinkToLogs(response.getApplicationId());
} else {
buildLogger.addLogEntry("... Error thrown by the NR Broker given payload: " + payload);
throw new RuntimeException("Error invoking the New Relic Broker: " + invokeResult);
String nrBrokerResponseJson = new String(invokeResult.getPayload().array(), Charset.forName("UTF-8"));
throw new RuntimeException("Error invoking the New Relic Broker: " + nrBrokerResponseJson);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,11 @@ public BucketMeta brokerFromConfigurationFile() {
.withClientConfiguration(BambooCredentialsHandler.getConfiguration()).withRegion(context.getRegion()).build();

TagSet tags = new TagSet();
tags.setTag(taskProperties.getSbuTagKey(), configuration.getSbu());
tags.setTag(taskProperties.getOrgTagKey(), configuration.getOrg());
tags.setTag(taskProperties.getAppTagKey(), configuration.getAppName());
if (taskProperties != null) {
tags.setTag(taskProperties.getSbuTagKey(), configuration.getSbu());
tags.setTag(taskProperties.getOrgTagKey(), configuration.getOrg());
tags.setTag(taskProperties.getAppTagKey(), configuration.getAppName());
}
String policy = null;

if (configuration.getPolicyName() != null) {
Expand All @@ -96,10 +98,12 @@ public BucketMeta brokerFromConfigurationFile() {
public void brokerBucketFromEcsPush(AmazonS3 client, S3Bucket bucket, String bucketPolicy,
EcsClusterMetadata clusterMetadata, EcsPushDefinition definition) {
TagSet tags = new TagSet();
tags.setTag(taskProperties.getSbuTagKey(), clusterMetadata.getNewrelicSbuTag());
tags.setTag(taskProperties.getOrgTagKey(), clusterMetadata.getNewrelicOrgTag());
tags.setTag(taskProperties.getAppTagKey(), definition.getAppName());
tags.setTag(taskProperties.getClusterTagKey(), clusterMetadata.getClusterId());
if (taskProperties != null) {
tags.setTag(taskProperties.getSbuTagKey(), clusterMetadata.getNewrelicSbuTag());
tags.setTag(taskProperties.getOrgTagKey(), clusterMetadata.getNewrelicOrgTag());
tags.setTag(taskProperties.getAppTagKey(), definition.getAppName());
tags.setTag(taskProperties.getClusterTagKey(), clusterMetadata.getClusterId());
}
S3InjectConfiguration configuration = new S3InjectConfiguration();
configuration.setAppName(bucket.getName());
configuration.setSbu(clusterMetadata.getNewrelicSbuTag());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,12 @@
import com.amazonaws.services.cloudformation.model.StackResource;
import com.amazonaws.services.cloudformation.model.Tag;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.model.DescribeSecurityGroupsRequest;
import com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult;
import com.amazonaws.services.ec2.model.DescribeSubnetsResult;
import com.amazonaws.services.ec2.model.DescribeVpcsResult;
import com.amazonaws.services.ec2.model.Filter;
import com.amazonaws.services.ec2.model.SecurityGroup;
import com.amazonaws.services.ec2.model.Subnet;
import com.amazonaws.services.ec2.model.Vpc;
import com.libertymutualgroup.herman.aws.AwsExecException;
import com.libertymutualgroup.herman.logging.HermanLogger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
Expand All @@ -60,7 +54,7 @@ public EcsClusterMetadata introspect(String name) {

DescribeStackResourcesResult clusterStackResult = cftClient.describeStackResources(req);

for (StackResource r : clusterStackResult.getStackResources()) {
for (StackResource r: clusterStackResult.getStackResources()) {
updateClusterMetadataWithStackResourceValue(ecsClusterMetadata, r);
}

Expand All @@ -76,7 +70,7 @@ public EcsClusterMetadata introspect(String name) {
DescribeVpcsResult res = ec2Client.describeVpcs();

Vpc vpc = null;
for (Vpc v : res.getVpcs()) {
for (Vpc v: res.getVpcs()) {
if (isProperVpc(v)) {
vpc = v;
}
Expand All @@ -90,9 +84,9 @@ public EcsClusterMetadata introspect(String name) {

DescribeSubnetsResult sub = ec2Client.describeSubnets();

for (Subnet net : sub.getSubnets()) {
for (Subnet net: sub.getSubnets()) {
if (subnetMatches(vpc, net)) {
for (com.amazonaws.services.ec2.model.Tag t : net.getTags()) {
for (com.amazonaws.services.ec2.model.Tag t: net.getTags()) {
if ("Name".equals(t.getKey())) {
if (t.getValue().contains("private-elb")) {
elbSubnets.add(net.getSubnetId());
Expand All @@ -103,7 +97,6 @@ public EcsClusterMetadata introspect(String name) {
}
}
}
ecsClusterMetadata.setAkamaiSecurityGroup(getAkamaiSecurityGroups(vpc));

logger.addLogEntry("Introspection complete:");
logger.addLogEntry(ecsClusterMetadata.toString());
Expand Down Expand Up @@ -150,10 +143,12 @@ private void updateClusterMetadataWithStackResourceValue(EcsClusterMetadata ecsC
}

private boolean isProperVpc(Vpc vpc) {
String[] defaultIds = {"sandbox-vpc", "dev-vpc", "nonprod-vpc", "prod-vpc"};
String[] defaultIds = {"sandbox", "dev", "nonprod", "prod"};

for (com.amazonaws.services.ec2.model.Tag t : vpc.getTags()) {
if ("Name".equals(t.getKey()) && Arrays.asList(defaultIds).contains(t.getValue())) {
for (com.amazonaws.services.ec2.model.Tag t: vpc.getTags()) {
if ("Name".equals(t.getKey())
&& Arrays.asList(defaultIds).stream()
.filter(defaultId -> t.getValue().contains(defaultId)).findAny().isPresent()) {
return true;
}
}
Expand All @@ -163,42 +158,4 @@ private boolean isProperVpc(Vpc vpc) {
private boolean subnetMatches(Vpc vpc, Subnet subnet) {
return vpc != null && subnet != null && subnet.getVpcId() != null && subnet.getVpcId().equals(vpc.getVpcId());
}


private List<String> getAkamaiSecurityGroups(Vpc vpc) {
String region = null;
for (com.amazonaws.services.ec2.model.Tag t : vpc.getTags()) {
if ("Name".equals(t.getKey())) {
String name = t.getValue();
region = name.replaceAll("-vpc", "");
}
}

List<String> groups = new ArrayList<>();
String prefix = "aws-shared-external-elb-" + region;
SecurityGroup groupOne = getSecurityGroup(prefix + "-1");
SecurityGroup groupTwo = getSecurityGroup(prefix + "-2");
groups.add(groupOne.getGroupId());
groups.add(groupTwo.getGroupId());

return groups;
}


private SecurityGroup getSecurityGroup(String sgName) {
Filter filter = new Filter().withName("tag:Name").withValues(sgName);
DescribeSecurityGroupsRequest secReq = new DescribeSecurityGroupsRequest().withFilters(filter);

DescribeSecurityGroupsResult sgResult = ec2Client.describeSecurityGroups(secReq);

SecurityGroup secGroup;
if (sgResult.getSecurityGroups().size() == 1) {
secGroup = sgResult.getSecurityGroups().get(0);
} else {
logger.addLogEntry("Used: " + sgName);

throw new AwsExecException("Error looking up SG :" + sgResult.getSecurityGroups().size());
}
return secGroup;
}
}
Loading

0 comments on commit 10a6e38

Please sign in to comment.