Example usage for com.amazonaws.auth DefaultAWSCredentialsProviderChain DefaultAWSCredentialsProviderChain

List of usage examples for com.amazonaws.auth DefaultAWSCredentialsProviderChain DefaultAWSCredentialsProviderChain

Introduction

In this page you can find the example usage for com.amazonaws.auth DefaultAWSCredentialsProviderChain DefaultAWSCredentialsProviderChain.

Prototype

public DefaultAWSCredentialsProviderChain() 

Source Link

Usage

From source file:com.pinterest.secor.uploader.S3UploadManager.java

License:Apache License

public S3UploadManager(SecorConfig config) {
    super(config);

    final String accessKey = mConfig.getAwsAccessKey();
    final String secretKey = mConfig.getAwsSecretKey();
    final String endpoint = mConfig.getAwsEndpoint();
    final String region = mConfig.getAwsRegion();
    final String awsRole = mConfig.getAwsRole();

    s3Path = mConfig.getS3Path();

    AmazonS3 client;//from  w  w w  . j  a  v  a2  s. c  o  m
    AWSCredentialsProvider provider;

    ClientConfiguration clientConfiguration = new ClientConfiguration();
    boolean isHttpProxyEnabled = mConfig.getAwsProxyEnabled();

    //proxy settings
    if (isHttpProxyEnabled) {
        LOG.info("Http Proxy Enabled for S3UploadManager");
        String httpProxyHost = mConfig.getAwsProxyHttpHost();
        int httpProxyPort = mConfig.getAwsProxyHttpPort();
        clientConfiguration.setProxyHost(httpProxyHost);
        clientConfiguration.setProxyPort(httpProxyPort);
    }

    if (accessKey.isEmpty() || secretKey.isEmpty()) {
        provider = new DefaultAWSCredentialsProviderChain();
    } else {
        provider = new AWSCredentialsProvider() {
            public AWSCredentials getCredentials() {
                return new BasicAWSCredentials(accessKey, secretKey);
            }

            public void refresh() {
            }
        };
    }

    if (!awsRole.isEmpty()) {
        provider = new STSAssumeRoleSessionCredentialsProvider(provider, awsRole, "secor");
    }

    client = new AmazonS3Client(provider, clientConfiguration);

    if (!endpoint.isEmpty()) {
        client.setEndpoint(endpoint);
    } else if (!region.isEmpty()) {
        client.setRegion(Region.getRegion(Regions.fromName(region)));
    }

    mManager = new TransferManager(client);
}

From source file:com.qubole.presto.kinesis.KinesisClientManager.java

License:Apache License

@Inject
KinesisClientManager(KinesisConnectorConfig kinesisConnectorConfig) {
    log.info("Creating new client for Consumer");
    if (nonEmpty(kinesisConnectorConfig.getAccessKey()) && nonEmpty(kinesisConnectorConfig.getSecretKey())) {
        this.kinesisAwsCredentials = new KinesisAwsCredentials(kinesisConnectorConfig.getAccessKey(),
                kinesisConnectorConfig.getSecretKey());
        this.client = new AmazonKinesisClient(this.kinesisAwsCredentials);
        this.amazonS3Client = new AmazonS3Client(this.kinesisAwsCredentials);
        this.dynamoDBClient = new AmazonDynamoDBClient(this.kinesisAwsCredentials);
    } else {/*from w w  w.java 2s  .co  m*/
        this.kinesisAwsCredentials = null;
        DefaultAWSCredentialsProviderChain defaultChain = new DefaultAWSCredentialsProviderChain();
        this.client = new AmazonKinesisClient(defaultChain);
        this.amazonS3Client = new AmazonS3Client(defaultChain);
        this.dynamoDBClient = new AmazonDynamoDBClient(defaultChain);
    }

    this.client.setEndpoint("kinesis." + kinesisConnectorConfig.getAwsRegion() + ".amazonaws.com");
    this.dynamoDBClient.setEndpoint("dynamodb." + kinesisConnectorConfig.getAwsRegion() + ".amazonaws.com");
}

From source file:com.remediatetheflag.global.utils.AWSHelper.java

License:Apache License

public void terminateTask(String taskArn) {
    String regionFromArn = taskArn.split(":")[3];
    AmazonECS client = AmazonECSClientBuilder.standard().withRegion(regionFromArn)
            .withCredentials(new DefaultAWSCredentialsProviderChain()).build();
    try {// w w w . j ava 2  s . c  o m
        StopTaskRequest stopTaskRequest = new StopTaskRequest();
        stopTaskRequest.withCluster(RTFConfig.getExercisesCluster()).withTask(taskArn);
        client.stopTask(stopTaskRequest);
    } catch (Exception e) {
        logger.error("Error terminating task " + e.getMessage());
    }
}

From source file:com.remediatetheflag.global.utils.AWSHelper.java

License:Apache License

public void terminateTask(RTFECSContainerTask ecsInstance) {
    AmazonECS client = AmazonECSClientBuilder.standard().withRegion(ecsInstance.getRegion())
            .withCredentials(new DefaultAWSCredentialsProviderChain()).build();
    try {/*from   ww  w .  j a va  2 s  .c o  m*/
        StopTaskRequest stopTaskRequest = new StopTaskRequest();
        stopTaskRequest.withCluster(RTFConfig.getExercisesCluster()).withTask(ecsInstance.getTaskArn());
        client.stopTask(stopTaskRequest);
    } catch (Exception e) {
        logger.error("Error terminating task " + e.getMessage());
    }
}

From source file:com.remediatetheflag.global.utils.AWSHelper.java

License:Apache License

public Integer getClusterContainerInstances(Region region) {
    AmazonECS client = AmazonECSClientBuilder.standard().withRegion(region.getName())
            .withCredentials(new DefaultAWSCredentialsProviderChain()).build();
    DescribeClustersRequest request = new DescribeClustersRequest();
    request.withClusters(RTFConfig.getExercisesCluster());
    logger.debug("Requesting number of cluster running instances for region " + region.getName() + " cluster "
            + RTFConfig.getExercisesCluster());
    try {/*from  w  ww  .  j a  v  a 2  s .  co m*/
        DescribeClustersResult response = client.describeClusters(request);
        return response.getClusters().get(0).getRegisteredContainerInstancesCount();
    } catch (Exception e) {
        logger.error("Error getClusterContainerInstances for region " + region.getName() + " due to:\n"
                + e.getMessage());
        return 0;
    }
}

From source file:com.remediatetheflag.global.utils.AWSHelper.java

License:Apache License

public Double getClusterMemoryReservation(Region region) {
    AmazonCloudWatch client = AmazonCloudWatchClientBuilder.standard().withRegion(region.getName())
            .withCredentials(new DefaultAWSCredentialsProviderChain()).build();

    Dimension dimension = new Dimension();
    dimension.setName("ClusterName");
    dimension.setValue(RTFConfig.getExercisesCluster());
    Date date = new Date();
    Calendar cal = Calendar.getInstance();
    cal.setTime(date);//from w  w  w. ja  va  2  s. c  om
    cal.add(Calendar.MINUTE, -5);
    GetMetricStatisticsRequest request = new GetMetricStatisticsRequest().withMetricName("MemoryReservation")
            .withDimensions(dimension).withPeriod(60).withStartTime(cal.getTime()).withEndTime(date)
            .withStatistics("Average").withNamespace("AWS/ECS");
    try {
        logger.debug("Requesting memory reservation for region " + region.getName() + " cluster "
                + RTFConfig.getExercisesCluster());

        GetMetricStatisticsResult response = client.getMetricStatistics(request);
        if (response.getDatapoints().isEmpty())
            return 0.0;
        return response.getDatapoints().get(0).getAverage();
    } catch (Exception e) {
        logger.error("Error getClusterContainerInstances for memory reservation in region " + region.getName()
                + " due to:\n" + e.getMessage());
        return 0.0;
    }
}

From source file:com.remediatetheflag.global.utils.AWSHelper.java

License:Apache License

public String createECSTaskDefinition(RTFECSTaskDefinition taskDef, User user) {

    createExerciseLogGroup(taskDef.getRegion(), Constants.EXERCISE_LOG_GROUP);

    AmazonECS client = AmazonECSClientBuilder.standard().withRegion(taskDef.getRegion())
            .withCredentials(new DefaultAWSCredentialsProviderChain()).build();

    RegisterTaskDefinitionRequest request = new RegisterTaskDefinitionRequest();

    ArrayList<PortMapping> portMappings = new ArrayList<PortMapping>();
    portMappings.add(new PortMapping().withContainerPort(8080).withHostPort(0).withProtocol("tcp"));
    portMappings.add(new PortMapping().withContainerPort(3389).withHostPort(0).withProtocol("tcp"));

    LinuxParameters linuxParameters = new LinuxParameters();
    KernelCapabilities capabilities = new KernelCapabilities();
    capabilities.setAdd(Arrays.asList("NET_ADMIN"));
    linuxParameters.setCapabilities(capabilities);

    LogConfiguration logConfiguration = new LogConfiguration();
    logConfiguration.setLogDriver("awslogs");
    Map<String, String> options = new HashMap<String, String>();
    options.put("awslogs-group", Constants.EXERCISE_LOG_GROUP);
    options.put("awslogs-region", taskDef.getRegion().getName().toLowerCase());
    logConfiguration.setOptions(options);

    final ContainerDefinition def = new ContainerDefinition().withName(taskDef.getContainerName())
            .withImage(taskDef.getRepositoryImageUrl()).withMemoryReservation(taskDef.getSoftMemoryLimit())
            .withMemory(taskDef.getHardMemoryLimit()).withPortMappings(portMappings)
            .withLinuxParameters(linuxParameters).withLogConfiguration(logConfiguration).withEssential(true);

    request.setContainerDefinitions(Arrays.asList(def));
    request.setFamily(taskDef.getTaskDefinitionName());
    request.setNetworkMode("bridge");

    try {/*from  w ww  . j  a va 2s .c o  m*/
        RegisterTaskDefinitionResult response = client.registerTaskDefinition(request);
        logger.debug("# ECS Task Definition " + taskDef.getTaskDefinitionName() + " created for user "
                + user.getIdUser() + " in region " + taskDef.getRegion());
        return response.getTaskDefinition().getTaskDefinitionArn();
    } catch (Exception e) {
        logger.debug(
                "# ECS Task Definition " + taskDef.getTaskDefinitionName() + " COULD NOT BE created for user "
                        + user.getIdUser() + " in region " + taskDef.getRegion() + "\n" + e.getMessage());
        return null;
    }
}

From source file:com.remediatetheflag.global.utils.AWSHelper.java

License:Apache License

public Boolean removeTaskDefinitionInRegion(String taskDefinitionArn, Regions region) {
    AmazonECS client = AmazonECSClientBuilder.standard().withRegion(region)
            .withCredentials(new DefaultAWSCredentialsProviderChain()).build();
    try {/*w  w  w  .ja v  a2s . c om*/
        DeregisterTaskDefinitionRequest request = new DeregisterTaskDefinitionRequest()
                .withTaskDefinition(taskDefinitionArn);
        DeregisterTaskDefinitionResult result = client.deregisterTaskDefinition(request);
        return result.getSdkHttpMetadata().getHttpStatusCode() == 200;
    } catch (Exception e) {
        logger.warn(
                "# ECS TaskDefinition " + taskDefinitionArn + " could not be deregistered " + e.getMessage());
        return false;
    }
}

From source file:com.remediatetheflag.global.utils.AWSHelper.java

License:Apache License

protected RTFECSContainerTask createInstance(String clusterName, String instanceName, String password,
        RTFECSTaskDefinition taskDef, Integer duration, User user) {

    AmazonECS client = AmazonECSClientBuilder.standard().withRegion(taskDef.getRegion())
            .withCredentials(new DefaultAWSCredentialsProviderChain()).build();
    TaskOverride overrides = new TaskOverride();
    List<ContainerOverride> containerOverrides = new LinkedList<ContainerOverride>();
    ContainerOverride co = new ContainerOverride();
    List<KeyValuePair> environment = new LinkedList<KeyValuePair>();
    KeyValuePair kv = new KeyValuePair();
    kv.setName(Constants.ENV_USR_PWD);/*from w w w.  j a  va  2  s .  c  om*/
    kv.setValue(password);
    environment.add(kv);
    co.setEnvironment(environment);
    co.setName(taskDef.getContainerName());
    containerOverrides.add(co);
    overrides.setContainerOverrides(containerOverrides);
    RunTaskRequest request = new RunTaskRequest().withCluster(clusterName)
            .withTaskDefinition(taskDef.getTaskDefinitionArn()).withOverrides(overrides);
    logger.debug("# ECS Requesting Task " + instanceName + " for user " + user.getIdUser()
            + " with task definition: " + taskDef.getTaskDefinitionArn() + " on cluster: " + clusterName
            + " on region " + taskDef.getRegion().getName());
    try {
        RunTaskResult response = client.runTask(request);
        String failureReason = "";
        if (response.getTasks().isEmpty()) {
            for (Failure failure : response.getFailures()) {
                failureReason += "\n" + failure.getReason();
            }
            logger.error("Task creation failed due to: \n" + failureReason);
            return null;
        }
        Task task = response.getTasks().get(0);
        RTFECSContainerTask rtfInstance = new RTFECSContainerTask();
        rtfInstance.setCluster(task.getClusterArn());
        rtfInstance.setTaskArn(task.getTaskArn());
        rtfInstance.setIdContainerInstance(task.getContainerInstanceArn());
        rtfInstance.setName(instanceName);
        rtfInstance.setRegion(taskDef.getRegion());
        rtfInstance.setUser(user);
        rtfInstance.setCreateTime(task.getCreatedAt());
        rtfInstance.setStatus(Constants.STATUS_PENDING);
        logger.debug("# ECS Task " + instanceName + " created for user " + user.getIdUser()
                + " with task definition: " + taskDef.getTaskDefinitionName() + " start: "
                + rtfInstance.getCreateTime());
        return rtfInstance;
    } catch (Exception e) {
        logger.warn("# ECS Task " + instanceName + " could not be created for user " + user.getIdUser() + " "
                + e.getMessage());
        return null;
    }
}

From source file:com.remediatetheflag.global.utils.AWSHelper.java

License:Apache License

public List<String> getRunningECSTasks(List<Region> activeRegions) {
    LinkedList<String> list = new LinkedList<String>();
    for (Region region : activeRegions) {
        logger.debug("Enumerating running tasks on cluster " + RTFConfig.getExercisesCluster() + " for region "
                + region.getName());/*from  ww w  .  j a v a 2s.c  om*/
        AmazonECS client = AmazonECSClientBuilder.standard().withRegion(Regions.fromName(region.getName()))
                .withCredentials(new DefaultAWSCredentialsProviderChain()).build();
        ListTasksRequest request = new ListTasksRequest().withCluster(RTFConfig.getExercisesCluster());
        try {
            ListTasksResult response = client.listTasks(request);
            list.addAll(response.getTaskArns());
        } catch (Exception e) {
            logger.error("Error getRunningECSTasks for region " + region + " due to:\n" + e.getMessage());
        }
    }
    return list;
}