Commit 86f24c91 authored by Peter Harrison's avatar Peter Harrison

AWS Action

parent b88ddbdf
package nz.net.orcon.kanban.automation.actions;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import com.amazonaws.AmazonWebServiceClient;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.cloudfront.AmazonCloudFrontClient;
import com.amazonaws.services.cloudfront.model.CookiePreference;
import com.amazonaws.services.cloudfront.model.CreateDistributionRequest;
import com.amazonaws.services.cloudfront.model.CreateDistributionResult;
import com.amazonaws.services.cloudfront.model.CustomOriginConfig;
import com.amazonaws.services.cloudfront.model.DefaultCacheBehavior;
import com.amazonaws.services.cloudfront.model.DistributionConfig;
import com.amazonaws.services.cloudfront.model.ForwardedValues;
import com.amazonaws.services.cloudfront.model.Headers;
import com.amazonaws.services.cloudfront.model.Origin;
import com.amazonaws.services.cloudfront.model.OriginProtocolPolicy;
import com.amazonaws.services.cloudfront.model.Origins;
import com.amazonaws.services.cloudfront.model.TrustedSigners;
import com.amazonaws.services.cloudfront.model.ViewerCertificate;
import com.amazonaws.services.cloudfront.model.ViewerProtocolPolicy;
import com.amazonaws.services.elasticloadbalancing.AmazonElasticLoadBalancingClient;
import com.amazonaws.services.elasticloadbalancing.model.DescribeLoadBalancersResult;
import com.amazonaws.services.elasticloadbalancing.model.LoadBalancerDescription;
import com.amazonaws.services.elasticloadbalancing.model.SetLoadBalancerListenerSSLCertificateRequest;
import com.amazonaws.services.identitymanagement.AmazonIdentityManagementClient;
import com.amazonaws.services.identitymanagement.model.GetAccountSummaryResult;
import com.amazonaws.services.identitymanagement.model.ListServerCertificatesResult;
import com.amazonaws.services.identitymanagement.model.ServerCertificateMetadata;
import com.amazonaws.services.identitymanagement.model.UploadServerCertificateRequest;
import com.amazonaws.services.identitymanagement.model.UploadServerCertificateResult;
public class AwsAutomationAction {
@Autowired
private AmazonElasticLoadBalancingClient loadBalanceClient;
@Autowired
private AmazonIdentityManagementClient identityClient;
@Autowired
private AmazonCloudFrontClient cloudFrontClient;
public List<ServerCertificateMetadata> getCertificates(){
ListServerCertificatesResult listSigningCertificates = identityClient.listServerCertificates();
return listSigningCertificates.getServerCertificateMetadataList();
}
public String loadSSLCertificate(
String certificateName,
String certificate,
String privateKey,
String path){
String certificateId = this.getCertificateId(certificateName);
if(certificateId==null){
UploadServerCertificateRequest certificateRequest =
new UploadServerCertificateRequest( certificateName, certificate, privateKey);
certificateRequest.setPath(path);
UploadServerCertificateResult uploadServerCertificate =
getIdentityClient().uploadServerCertificate(certificateRequest);
certificateId = uploadServerCertificate.getServerCertificateMetadata().getServerCertificateId();
}
return certificateId;
}
public void setLoadBalancerCertificate( String loadBalancerName, String certificateId, Integer port ){
System.out.println( "LB CertID: " + certificateId);
this.loadBalanceClient.setRegion(Region.getRegion(Regions.AP_SOUTHEAST_2));
SetLoadBalancerListenerSSLCertificateRequest lbRequest =
new SetLoadBalancerListenerSSLCertificateRequest(
loadBalancerName,
port,
certificateId);
getLoadBalanceClient().setLoadBalancerListenerSSLCertificate(lbRequest);
}
public boolean isSSLCertificateLoaded( String certificateName ){
List<ServerCertificateMetadata> certificates = this.getCertificates();
for( ServerCertificateMetadata certificate : certificates){
if(certificate.getServerCertificateName().equals(certificateName)) {
return true;
}
}
return false;
}
public String getCertificateId( String certificateName){
List<ServerCertificateMetadata> certificates = this.getCertificates();
for( ServerCertificateMetadata certificate : certificates){
if(certificate.getServerCertificateName().equals(certificateName)) {
return certificate.getServerCertificateId();
}
}
return null;
}
public Collection<String> getLoadBalancerList(){
DescribeLoadBalancersResult loadBalancers = this.loadBalanceClient.describeLoadBalancers();
System.out.println(loadBalancers.toString());
Collection returnList = new ArrayList<String>();
List<LoadBalancerDescription> loadBalancerDescriptions = loadBalancers.getLoadBalancerDescriptions();
for( LoadBalancerDescription desc : loadBalancerDescriptions){
returnList.add(desc.getDNSName());
}
return returnList;
}
public String createCloudFrontDistribution(
String domainName,
String certificateId,
String originId,
String callerReference) {
CreateDistributionRequest createDistributionRequest = new CreateDistributionRequest();
DistributionConfig distributionConfig = new DistributionConfig();
DefaultCacheBehavior defaultCacheBehavior = new DefaultCacheBehavior();
ViewerCertificate viewerCertificate = new ViewerCertificate();
ForwardedValues forwardedValues = new ForwardedValues();
Headers headers = new Headers();
TrustedSigners trustedSigners = new TrustedSigners();
CookiePreference cookies = new CookiePreference();
Origin origin = new Origin();
Origins origins = new Origins();
Collection<Origin> items = new ArrayList<Origin>();
CustomOriginConfig customOriginConfig = new CustomOriginConfig();
// Origins
customOriginConfig.setHTTPPort(80);
customOriginConfig.setHTTPSPort(443);
customOriginConfig.setOriginProtocolPolicy(OriginProtocolPolicy.MatchViewer);
origin.setDomainName(domainName);
origin.setId(originId);
origin.setOriginPath("");
origin.setCustomOriginConfig(customOriginConfig);
items.add(origin);
origins.setItems(items);
origins.setQuantity(1);
// Forwarded Values & Headers & Cookies
cookies.setForward("all");
Collection<String> headerItems = new ArrayList<String>();
headers.setItems(headerItems);
headers.setQuantity(0);
forwardedValues.setHeaders(headers);
forwardedValues.setQueryString(false);
forwardedValues.setCookies(cookies);
// Trusted Signers
trustedSigners.setEnabled(false);
trustedSigners.setQuantity(0);
// Viewer Certificate
viewerCertificate.setIAMCertificateId(certificateId);
// Default Cache BEhaviour
defaultCacheBehavior.setMinTTL(30l);
defaultCacheBehavior.setViewerProtocolPolicy(ViewerProtocolPolicy.AllowAll);
defaultCacheBehavior.setForwardedValues(forwardedValues);
defaultCacheBehavior.setTargetOriginId("");
defaultCacheBehavior.setTrustedSigners(trustedSigners);
// Distribution Config
distributionConfig.setOrigins(origins);
distributionConfig.setViewerCertificate(viewerCertificate);
distributionConfig.setCallerReference(callerReference);
distributionConfig.setDefaultCacheBehavior(defaultCacheBehavior);
distributionConfig.setEnabled(true);
distributionConfig.setComment("no comment");
distributionConfig.setDefaultRootObject("");
createDistributionRequest.setDistributionConfig(distributionConfig);
CreateDistributionResult createDistribution
= this.cloudFrontClient.createDistribution(createDistributionRequest);
return createDistribution.getLocation();
}
public Map<String, Integer> getAccountSummary(){
GetAccountSummaryResult accountSummary = getIdentityClient().getAccountSummary();
Map<String, Integer> summaryMap = accountSummary.getSummaryMap();
return summaryMap;
}
public AmazonElasticLoadBalancingClient getLoadBalanceClient() {
return loadBalanceClient;
}
public void setLoadBalanceClient(AmazonElasticLoadBalancingClient loadBalanceClient) {
this.loadBalanceClient = loadBalanceClient;
}
public AmazonIdentityManagementClient getIdentityClient() {
return identityClient;
}
public void setIdentityClient(AmazonIdentityManagementClient identityClient) {
this.identityClient = identityClient;
}
public AmazonCloudFrontClient getCloudFrontClient() {
return cloudFrontClient;
}
public void setCloudFrontClient(AmazonCloudFrontClient cloudFrontClient) {
this.cloudFrontClient = cloudFrontClient;
}
}
package nz.net.orcon.kanban.automation.actions;
import static org.junit.Assert.*;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.amazonaws.services.identitymanagement.model.ListSigningCertificatesResult;
import com.amazonaws.services.identitymanagement.model.ServerCertificateMetadata;
import com.amazonaws.services.identitymanagement.model.SigningCertificate;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/test-aws.xml" })
public class AwsAutomationActionTest {
@Autowired
AwsAutomationAction automationAction;
@Test
@Ignore
public void testLoadSSLCertificate() throws IOException {
// String certificate = getFile("/home/peter/key/test2.crt");
// String privateKey = getFile("/home/peter/key/test2.key");
String certificate = getFile("/home/peter/devcentre/devcentre.org.crt");
String privateKey = getFile("/home/peter/devcentre/devcentre.org.key");
String path = "/cloudfront/production/";
// System.out.println(certificate);
// System.out.println(privateKey);
// automationAction.loadSSLCertificate("testlb", "testcert2", 443, certificate, privateKey, null);
String certificateId = automationAction.loadSSLCertificate("prodcert", certificate, privateKey, path);
System.out.println("Certificate ID: " + certificateId );
//automationAction.setLoadBalancerCertificate("testlb", certificateId, 443);
}
@Test
public void testGetSSLCertificates() throws IOException {
List<ServerCertificateMetadata> certs = automationAction.getCertificates();
System.out.println("Certificates: " + certs.size() );
for( ServerCertificateMetadata cert : certs) {
System.out.println(cert.getServerCertificateId() + " -> " + cert.getServerCertificateName());
}
}
@Test
public void testCreateCloudFrontDistribution(){
Collection<String> loadBalancerList = automationAction.getLoadBalancerList();
System.out.println("LB Size: " + loadBalancerList.size() );
for( String item : loadBalancerList){
System.out.println("LB: " + item );
}
String domainName = "testlb-1715940860.ap-southeast-2.elb.amazonaws.com";
String certificateId = automationAction.getCertificateId("prodcert");
String originId = "testlblink";
String ref = "TestDistribution";
String location =
automationAction.createCloudFrontDistribution(domainName, certificateId, originId, ref);
System.out.println("Location: " + location );
}
public void testGetAccountSummary() throws IOException {
Map<String, Integer> accountSummary = automationAction.getAccountSummary();
for( Entry<String,Integer> entry : accountSummary.entrySet()){
System.out.println(entry.getKey() +" = " + entry.getValue().toString());
}
}
public String getFile(String fileName) throws IOException{
FileInputStream in = new FileInputStream(fileName);
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int result = bis.read();
while(result != -1) {
byte b = (byte)result;
buf.write(b);
result = bis.read();
}
return buf.toString();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment