Example usage for com.amazonaws AmazonServiceException getMessage

List of usage examples for com.amazonaws AmazonServiceException getMessage

Introduction

In this page you can find the example usage for com.amazonaws AmazonServiceException getMessage.

Prototype

@Override
    public String getMessage() 

Source Link

Usage

From source file:org.xmlsh.aws.gradle.ec2.AmazonEC2AuthorizeSecurityGroupEgressTask.java

License:BSD License

@TaskAction
public void authorizeEgress() {
    // to enable conventionMappings feature
    String groupId = getGroupId();
    Object ipPermissions = getIpPermissions();

    if (groupId == null)
        throw new GradleException("groupId is not specified");
    if (ipPermissions == null)
        throw new GradleException("ipPermissions is not specified");

    AmazonEC2PluginExtension ext = getProject().getExtensions().getByType(AmazonEC2PluginExtension.class);
    AmazonEC2 ec2 = ext.getClient();//from www.ja  va2  s .  co  m

    try {
        ec2.authorizeSecurityGroupEgress(new AuthorizeSecurityGroupEgressRequest().withGroupId(groupId)
                .withIpPermissions(parse(ipPermissions)));
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidPermission.Duplicate")) {
            getLogger().warn(e.getMessage());
        } else {
            throw e;
        }
    }
}

From source file:org.xmlsh.aws.gradle.ec2.AmazonEC2AuthorizeSecurityGroupIngressTask.java

License:BSD License

@TaskAction
public void authorizeIngress() {
    // to enable conventionMappings feature
    String groupId = getGroupId();
    Object ipPermissions = getIpPermissions();

    if (groupId == null)
        throw new GradleException("groupId is not specified");
    if (ipPermissions == null)
        throw new GradleException("ipPermissions is not specified");

    AmazonEC2PluginExtension ext = getProject().getExtensions().getByType(AmazonEC2PluginExtension.class);
    AmazonEC2 ec2 = ext.getClient();/* ww  w .j  a  v  a 2 s. c  o m*/

    try {
        ec2.authorizeSecurityGroupIngress(new AuthorizeSecurityGroupIngressRequest().withGroupId(groupId)
                .withIpPermissions(parse(ipPermissions)));
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidPermission.Duplicate")) {
            getLogger().warn(e.getMessage());
        } else {
            throw e;
        }
    }
}

From source file:org.xmlsh.aws.gradle.ec2.AmazonEC2RevokeSecurityGroupEgressTask.java

License:BSD License

@TaskAction
public void revokeEgress() {
    // to enable conventionMappings feature
    String groupId = getGroupId();
    Object ipPermissions = getIpPermissions();

    if (groupId == null)
        throw new GradleException("groupId is not specified");
    if (ipPermissions == null)
        throw new GradleException("ipPermissions is not specified");

    AmazonEC2PluginExtension ext = getProject().getExtensions().getByType(AmazonEC2PluginExtension.class);
    AmazonEC2 ec2 = ext.getClient();// ww  w  . java 2s.c  o m

    try {
        ec2.revokeSecurityGroupEgress(new RevokeSecurityGroupEgressRequest().withGroupId(groupId)
                .withIpPermissions(parse(ipPermissions)));
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidPermission.NotFound")) {
            getLogger().warn(e.getMessage());
        } else {
            throw e;
        }
    }
}

From source file:org.xmlsh.aws.gradle.ec2.AmazonEC2RevokeSecurityGroupIngressTask.java

License:BSD License

@TaskAction
public void revokeIngress() {
    // to enable conventionMappings feature
    String groupId = getGroupId();
    Object ipPermissions = getIpPermissions();

    if (groupId == null)
        throw new GradleException("groupId is not specified");
    if (ipPermissions == null)
        throw new GradleException("ipPermissions is not specified");

    AmazonEC2PluginExtension ext = getProject().getExtensions().getByType(AmazonEC2PluginExtension.class);
    AmazonEC2 ec2 = ext.getClient();/*from ww  w  .  ja  va2s .c  om*/

    try {
        ec2.revokeSecurityGroupIngress(new RevokeSecurityGroupIngressRequest().withGroupId(groupId)
                .withIpPermissions(parse(ipPermissions)));
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidPermission.NotFound")) {
            getLogger().warn(e.getMessage());
        } else {
            throw e;
        }
    }
}

From source file:org.xmlsh.aws.gradle.elasticbeanstalk.AWSElasticBeanstalkCreateApplicationVersionTask.java

License:BSD License

@TaskAction
public void createVersion() {
    // to enable conventionMappings feature
    String appName = getAppName();
    String versionLabel = getVersionLabel();

    AwsBeanstalkPluginExtension ext = getProject().getExtensions().getByType(AwsBeanstalkPluginExtension.class);
    AWSElasticBeanstalk eb = ext.getClient();

    try {//w w w .  j  ava2s  .c o  m
        eb.createApplicationVersion(new CreateApplicationVersionRequest().withApplicationName(appName)
                .withVersionLabel(versionLabel).withSourceBundle(new S3Location(getBucketName(), getKey())));
        getLogger().info("version " + versionLabel + " @ " + appName + " created");
    } catch (AmazonServiceException e) {
        if (e.getMessage().endsWith("already exists.") == false) {
            throw e;
        }
        getLogger().warn("version " + versionLabel + " @ " + appName + " already exists.");
    }
}

From source file:org.zalando.stups.fullstop.plugin.RegistryPlugin.java

License:Apache License

private Map getUserData(final CloudTrailEvent event, final String instanceId) {

    AmazonEC2Client ec2Client = cachingClientProvider.getClient(AmazonEC2Client.class,
            event.getEventData().getUserIdentity().getAccountId(),
            Region.getRegion(Regions.fromName(event.getEventData().getAwsRegion())));

    DescribeInstanceAttributeRequest describeInstanceAttributeRequest = new DescribeInstanceAttributeRequest();
    describeInstanceAttributeRequest.setInstanceId(instanceId);
    describeInstanceAttributeRequest.setAttribute(USER_DATA);

    DescribeInstanceAttributeResult describeInstanceAttributeResult;
    try {//ww w .java  2s .co m
        describeInstanceAttributeResult = ec2Client.describeInstanceAttribute(describeInstanceAttributeRequest);
    } catch (AmazonServiceException e) {
        LOG.error(e.getMessage());
        violationStore
                .save(new ViolationBuilder(format("InstanceId: %s doesn't have any userData.", instanceId))
                        .withEvent(event).build());
        return null;
    }

    String userData = describeInstanceAttributeResult.getInstanceAttribute().getUserData();

    if (userData == null) {
        violationStore
                .save(new ViolationBuilder(format("InstanceId: %s doesn't have any userData.", instanceId))
                        .withEvent(event).build());
        return null;
    }

    byte[] bytesUserData = Base64.decode(userData);
    String decodedUserData = new String(bytesUserData);

    Yaml yaml = new Yaml();

    return (Map) yaml.load(decodedUserData);
}

From source file:org.zalando.stups.fullstop.plugin.SaveSecurityGroupsPlugin.java

License:Apache License

private List<String> listS3Objects(String bucketName, String prefix) {
    final List<String> commonPrefixes = Lists.newArrayList();

    AmazonS3Client s3client = new AmazonS3Client();

    try {//from  w  ww.jav  a  2  s .co  m
        System.out.println("Listing objects");

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withDelimiter("/")
                .withBucketName(bucketName).withPrefix(prefix);

        ObjectListing objectListing;

        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            commonPrefixes.addAll(objectListing.getCommonPrefixes());
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                System.out.println(
                        " - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, " + "which means your request made it "
                + "to Amazon S3, but was rejected with an error response " + "for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, " + "which means the client encountered "
                + "an internal error while trying to communicate" + " with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }

    return commonPrefixes;
}

From source file:org.zalando.stups.fullstop.plugin.snapshot.SnapshotSourcePlugin.java

License:Apache License

@Override
public void processEvent(CloudTrailEvent event) {
    List<String> instanceIds = getInstanceIds(event);
    for (String id : instanceIds) {
        Map userData;//www .  j  a  v  a2 s. c  om
        final String accountId = event.getEventData().getUserIdentity().getAccountId();
        final String region = event.getEventData().getAwsRegion();
        try {
            userData = userDataProvider.getUserData(accountId, region, id);
        } catch (AmazonServiceException e) {
            LOG.error(e.getMessage());
            violationSink.put(violationFor(event).withInstanceId(id)
                    .withPluginFullyQualifiedClassName(SnapshotSourcePlugin.class).withType(MISSING_USER_DATA)
                    .build());
            return;
        }

        if (userData == null) {
            violationSink.put(violationFor(event).withInstanceId(id)
                    .withPluginFullyQualifiedClassName(SnapshotSourcePlugin.class).withType(MISSING_USER_DATA)
                    .build());
            return;
        }

        String source = (String) userData.get(SOURCE);
        if (source == null) {
            violationSink.put(violationFor(event).withInstanceId(id)
                    .withPluginFullyQualifiedClassName(SnapshotSourcePlugin.class)
                    .withType(MISSING_SOURCE_IN_USER_DATA).build());
        } else if (source.matches(SNAPSHOT_REGEX)) {
            violationSink.put(violationFor(event).withInstanceId(id)
                    .withPluginFullyQualifiedClassName(SnapshotSourcePlugin.class)
                    .withType(EC2_WITH_A_SNAPSHOT_IMAGE).build());
        }
    }
}

From source file:org.zalando.stups.fullstop.plugin.SubnetPlugin.java

License:Apache License

@Override
public void processEvent(final CloudTrailEvent event) {
    List<String> subnetIds = newArrayList();
    List<Filter> SubnetIdFilters = newArrayList();
    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
    List<String> instanceIds = getInstanceIds(event);
    AmazonEC2Client amazonEC2Client = cachingClientProvider.getClient(AmazonEC2Client.class,
            event.getEventData().getAccountId(),
            Region.getRegion(Regions.fromName(event.getEventData().getAwsRegion())));

    DescribeInstancesResult describeInstancesResult = null;
    try {//w  w  w. j  av a2  s  .  c  o m
        describeInstancesResult = amazonEC2Client
                .describeInstances(describeInstancesRequest.withInstanceIds(instanceIds));
    } catch (AmazonServiceException e) {
        violationStore.save(new ViolationBuilder(e.getMessage()).withEvent(event).build());
        return;
    }

    List<Reservation> reservations = describeInstancesResult.getReservations();
    for (Reservation reservation : reservations) {
        List<Instance> instances = reservation.getInstances();
        subnetIds.addAll(instances.stream().map(Instance::getSubnetId).collect(Collectors.toList()));

    }

    SubnetIdFilters.add(new Filter().withName("association.subnet-id").withValues(subnetIds)); // filter by subnetId
    DescribeRouteTablesRequest describeRouteTablesRequest = new DescribeRouteTablesRequest()
            .withFilters(SubnetIdFilters);
    DescribeRouteTablesResult describeRouteTablesResult = amazonEC2Client
            .describeRouteTables(describeRouteTablesRequest);
    List<RouteTable> routeTables = describeRouteTablesResult.getRouteTables();
    if (routeTables == null || routeTables.size() == 0) {
        violationStore.save(new ViolationBuilder(
                format("Instances %s have no routing information associated", instanceIds.toString()))
                        .withEvent(event).build());
        return;
    }
    for (RouteTable routeTable : routeTables) {
        List<Route> routes = routeTable.getRoutes();
        routes.stream()
                .filter(route -> route.getState().equals("active") && route.getNetworkInterfaceId() != null
                        && !route.getNetworkInterfaceId().startsWith("eni"))
                .forEach(route -> violationStore.save(

                        new ViolationBuilder(format("ROUTES: instance %s is running in a public subnet %s",
                                route.getInstanceId(), route.getNetworkInterfaceId())).withEvent(event)
                                        .build()));
    }

}

From source file:org.zalando.stups.fullstop.s3.S3Writer.java

License:Apache License

public void putObjectToS3(String bucket, String fileName, String keyName, ObjectMetadata metadata,
        InputStream stream) {/*from w w w  .j  a  v  a 2  s  . c om*/
    AmazonS3 s3client = new AmazonS3Client();
    try {
        logger.info("Uploading a new object to S3 from a file");

        s3client.putObject(
                new PutObjectRequest(bucket, Paths.get(keyName, fileName).toString(), stream, metadata));

    } catch (AmazonServiceException ase) {
        logger.error("Caught an AmazonServiceException, which " + "means your request made it "
                + "to Amazon S3, but was rejected with an error response" + " for some reason.");
        logger.error("Error Message:    " + ase.getMessage());
        logger.error("HTTP Status Code: " + ase.getStatusCode());
        logger.error("AWS Error Code:   " + ase.getErrorCode());
        logger.error("Error Type:       " + ase.getErrorType());
        logger.error("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        logger.error("Caught an AmazonClientException, which " + "means the client encountered "
                + "an internal error while trying to " + "communicate with S3, "
                + "such as not being able to access the network.");
        logger.error("Error Message: " + ace.getMessage());
    }
}