Example usage for com.amazonaws.auth BasicAWSCredentials BasicAWSCredentials

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

Introduction

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

Prototype

public BasicAWSCredentials(String accessKey, String secretKey) 

Source Link

Document

Constructs a new BasicAWSCredentials object, with the specified AWS access key and AWS secret key.

Usage

From source file:com.keybox.manage.action.ApplicationKeysAction.java

License:Apache License

/**
  * returns keypairs as a json string/*from w w w . j a  va2s  .  c  o  m*/
  */
@Action(value = "/manage/getKeyPairJSON")
public String getKeyPairJSON() {

    AWSCred awsCred = AWSCredDB.getAWSCred(ec2Key.getAwsCredentials().getId());

    //set  AWS credentials for service
    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(),
            awsCred.getSecretKey());
    AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig());

    service.setEndpoint(ec2Key.getEc2Region());

    DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();

    DescribeKeyPairsResult describeKeyPairsResult = service.describeKeyPairs(describeKeyPairsRequest);

    List<KeyPairInfo> keyPairInfoList = describeKeyPairsResult.getKeyPairs();
    String json = new Gson().toJson(keyPairInfoList);
    try {
        servletResponse.getOutputStream().write(json.getBytes());
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:com.keybox.manage.db.ProfileSystemsDB.java

License:Apache License

/**
 * Update Profile and AWS Systems in DB/*from   www. j a v  a2s  .com*/
 * <br><br>
 * Call first updateAWSSystem() to Update EC2 systems<br>
 * Clean SystemProfileEntries from EC2 systems<br>
 * Rebuild SystemProfileEntries for EC2 systems
 */
public static void updateProfileAWSSysteme() {

    SystemDB.updateAWSSystems();
    deleteAWSSystemProfileEntries();

    try {
        List<Profile> profileList = ProfileDB.getAllProfiles();
        for (Profile profile : profileList) {
            if (profile.getTag().equals("")) {
                continue;
            }

            List<String> ec2RegionList = PrivateKeyDB.getEC2Regions();

            //remove Formating Char
            String tags = profile.getTag().replaceAll("\n", "").replaceAll("\r", "").replaceAll("\t", "");

            String[] tagArr1 = tags.split(",");

            Map<String, String> tagMap = new HashMap<>();
            List<String> tagList = new ArrayList<>();

            if (tagArr1.length > 0) {
                for (String tag1 : tagArr1) {
                    String[] tagArr2 = tag1.split("=");
                    if (tagArr2.length > 1) {
                        tagMap.put(tag1.split("=")[0], tag1.split("=")[1]);
                    } else {
                        tagList.add(tag1);
                    }
                }
            }

            //get AWS credentials from DB
            for (AWSCred awsCred : AWSCredDB.getAWSCredList()) {

                if (awsCred != null && awsCred.isValid()) {
                    //set  AWS credentials for service
                    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(),
                            awsCred.getSecretKey());

                    for (String ec2Region : ec2RegionList) {
                        //create service

                        AmazonEC2 service = new AmazonEC2Client(awsCredentials,
                                AWSClientConfig.getClientConfig());
                        service.setEndpoint(ec2Region);

                        //only return systems that have keys set
                        List<String> keyValueList = new ArrayList<String>();
                        for (ApplicationKey ec2Key : PrivateKeyDB.getEC2KeyByRegion(ec2Region)) {
                            if (ec2Key.isEnabled()) {
                                keyValueList.add(ec2Key.getKeyname());
                            }
                        }

                        DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();

                        Filter keyNmFilter = new Filter("key-name", keyValueList);
                        describeInstancesRequest.withFilters(keyNmFilter);

                        if (tagList.size() > 0) {
                            Filter tagFilter = new Filter("tag-key", tagList);
                            describeInstancesRequest.withFilters(tagFilter);
                        }

                        //set name value pair for tag filter
                        for (String tag : tagMap.keySet()) {
                            Filter tagValueFilter = new Filter("tag:" + tag, Arrays.asList(tagMap.get(tag)));
                            describeInstancesRequest.withFilters(tagValueFilter);
                        }

                        DescribeInstancesResult describeInstancesResult = service
                                .describeInstances(describeInstancesRequest);

                        for (Reservation res : describeInstancesResult.getReservations()) {
                            for (Instance instance : res.getInstances()) {
                                HostSystem ec2System = SystemDB.getSystemByInstance(instance.getInstanceId());
                                addSystemToProfile(profile.getId(), ec2System.getId());
                            }
                        }
                    }
                }
            }
        }
    } catch (AmazonServiceException ex) {
        ex.printStackTrace();
    }
}

From source file:com.keybox.manage.db.SystemDB.java

License:Apache License

/**
 * Update AWS Systems/*from  ww w. ja v  a  2  s .co  m*/
 * <br><br>
 * Update all EC2 System based on setting AWS Credentials and the EC2 Keys
 * and added new System
 */
public static void updateAWSSystems() {

    List<String> ec2RegionList = PrivateKeyDB.getEC2Regions();

    try {
        //get AWS credentials from DB
        for (AWSCred awsCred : AWSCredDB.getAWSCredList()) {

            if (awsCred != null && awsCred.isValid()) {
                //set  AWS credentials for service
                BasicAWSCredentials awsCredentials = new BasicAWSCredentials(awsCred.getAccessKey(),
                        awsCred.getSecretKey());

                for (String ec2Region : ec2RegionList) {
                    //create service

                    AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig());
                    service.setEndpoint(ec2Region);

                    //only return systems that have keys set
                    List<String> keyValueList = new ArrayList<String>();
                    for (ApplicationKey ec2Key : PrivateKeyDB.getEC2KeyByRegion(ec2Region)) {
                        if (ec2Key.isEnabled()) {
                            keyValueList.add(ec2Key.getKeyname());
                        }
                    }

                    DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();

                    Filter keyNmFilter = new Filter("key-name", keyValueList);

                    describeInstancesRequest.withFilters(keyNmFilter);

                    DescribeInstancesResult describeInstancesResult = service
                            .describeInstances(describeInstancesRequest);

                    for (Reservation res : describeInstancesResult.getReservations()) {
                        for (Instance instance : res.getInstances()) {
                            HostSystem hostSystem = transformerEC2InstanzToHostSystem(instance, ec2Region);

                            setEC2System(hostSystem);
                        }
                    }
                }
            }
        }
    } catch (AmazonServiceException ex) {
        ex.printStackTrace();
    }
}

From source file:com.keybox.manage.model.AWSCred.java

License:Apache License

/**
 * Test if AWS Credentials are valid/*w  w w.  j  a va2 s.  co m*/
 * @return AWS Credentials valid
 */
public boolean isValid() {
    boolean valid = true;

    try {
        //check if credential are valid
        BasicAWSCredentials awsCredentials = new BasicAWSCredentials(getAccessKey(), getSecretKey());
        AmazonEC2 service = new AmazonEC2Client(awsCredentials, AWSClientConfig.getClientConfig());
        service.describeKeyPairs();
    } catch (Exception ex) {
        valid = false;
    }
    return valid;
}

From source file:com.kikini.logging.simpledb.SimpleDBAppender.java

License:Apache License

/**
 * Obtain a SimpleDB instance from the Factory and get the logging domain
 * //from  www  . j  a v a  2s. c  o m
 * @see ch.qos.logback.core.AppenderBase#start()
 */
@Override
public void start() {
    boolean requiredPropsSet = true;

    if (null == accessId) {
        addStatus(new ErrorStatus("Access ID not set", this));
        requiredPropsSet = false;
    }
    if (null == secretKey) {
        addStatus(new ErrorStatus("Secret key not set", this));
        requiredPropsSet = false;
    }
    if (null == domainName) {
        addStatus(new ErrorStatus("Domain name not set", this));
        requiredPropsSet = false;
    }
    if (null == endPoint) {
        addStatus(new ErrorStatus("EndPoint not set", this));
        requiredPropsSet = false;
    }
    if (!requiredPropsSet)
        return;

    if (sdb == null) {
        try {
            if (instanceRole != null && !instanceRole.equals("")) {
                //Use instance profile credentials
                sdb = new AmazonSimpleDBClient();
            } else {
                final AWSCredentials credentials = new BasicAWSCredentials(accessId, secretKey);
                sdb = new AmazonSimpleDBClient(credentials);
            }
            sdb.setEndpoint(this.endPoint);

            // See if the domain exists
            boolean found = false;
            ListDomainsResult result = sdb.listDomains();
            for (String dom : result.getDomainNames()) {
                if (dom.equals(domainName)) {
                    found = true;
                    break;
                }
            }
            // Didn't find it, so create it
            if (!found) {
                sdb.createDomain(new CreateDomainRequest(domainName));
            }
        } catch (AmazonClientException e) {
            addStatus(new ErrorStatus("Could not get access SimpleDB", this, e));
            return;
        }
    }

    if (queue == null) {
        this.queue = new DelayQueue<SimpleDBRow>();
    }

    if (writer == null) {
        this.writer = new SimpleDBWriter(sdb, domainName);
    }

    if (timeZone != null) {
        writer.setTimeZone(DateTimeZone.forID(timeZone));
    }

    if (consumer == null) {
        consumer = new SimpleDBConsumer(queue, writer);
    }

    Thread consumerThread = new Thread(consumer);
    Runnable shutdown = new SimpleDBShutdownHook(queue, writer, consumerThread);
    Runtime.getRuntime().addShutdownHook(new Thread(shutdown));
    consumerThread.setDaemon(true);
    consumerThread.start();
    super.start();
}

From source file:com.kixeye.chassis.support.metrics.aws.DefaultCloudWatchFactory.java

License:Apache License

@Override
public AmazonCloudWatchClient getCloudWatchClient() {
    AmazonCloudWatchClient client;/*from  w  ww  .j  a  va 2  s. c  o m*/
    if (StringUtils.isBlank(accessKeyId) || StringUtils.isBlank(secretKey)) {
        LOGGER.debug(
                "Constructing AmazonCloudWatchClient using DefaultAWSCredentialsProviderChain for region {}.",
                region);
        client = new AmazonCloudWatchClient(new DefaultAWSCredentialsProviderChain());
    } else {
        LOGGER.debug("Constructing AmazonCloudWatchClient from given credentials for region {}.", region);
        client = new AmazonCloudWatchClient(new BasicAWSCredentials(accessKeyId, secretKey));
    }
    client.setRegion(region);
    return client;
}

From source file:com.kodemore.aws.s3.KmS3Uploader.java

License:Open Source License

private AmazonS3Client createClient() {
    AWSCredentials creds = new BasicAWSCredentials(_accessKeyId, _secretKey);
    return new AmazonS3Client(creds);
}

From source file:com.kpbird.aws.Main.java

private void init() {
    credentials = new BasicAWSCredentials(accessKey, secretKey);
    // end point for singapore 
    endPoint = "https://rds.ap-southeast-1.amazonaws.com";
    // regions for singapore
    region = Region.getRegion(Regions.AP_SOUTHEAST_1);
    // EC2Client object
    ec2client = new AmazonEC2Client(credentials);
    ec2client.setEndpoint(endPoint);/*w w w.  j a v a 2 s.  c o m*/
    ec2client.setRegion(region);
    // RDSClient object
    rdsclient = new AmazonRDSClient(credentials);
    rdsclient.setRegion(region);
    rdsclient.setEndpoint(endPoint);

}

From source file:com.kurtraschke.nyctrtproxy.services.CloudwatchProxyDataListener.java

License:Apache License

@PostConstruct
public void init() {
    if (_secretKey == null || _accessKey == null || _namespace == null) {
        _log.info("No AWS credentials supplied, disabling cloudwatch");
        _disabled = true;/*  www  .j  av  a  2  s .  c  o  m*/
        return;
    }
    BasicAWSCredentials cred = new BasicAWSCredentials(_accessKey, _secretKey);
    _client = AmazonCloudWatchAsyncClientBuilder.standard()
            .withCredentials(new AWSStaticCredentialsProvider(cred)).build();
    _handler = new AsyncHandler<PutMetricDataRequest, PutMetricDataResult>() {
        @Override
        public void onError(Exception e) {
            _log.error("Error sending to cloudwatch: " + e);
        }

        @Override
        public void onSuccess(PutMetricDataRequest request, PutMetricDataResult putMetricDataResult) {
            // do nothing
        }
    };
}

From source file:com.liferay.amazontools.BaseAMIBuilder.java

License:Open Source License

protected AmazonEC2Client getAmazonEC2Client(String accessKey, String secretKey, String endpoint) {

    AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);

    AmazonEC2Client amazonEC2Client = new AmazonEC2Client(awsCredentials);

    amazonEC2Client.setEndpoint(endpoint);

    return amazonEC2Client;
}