Search in sources :

Example 1 with Policy

use of com.amazonaws.auth.policy.Policy in project glacier-cli by carlossg.

the class Glacier method setupSQS.

// ==============
// Helper methods
// ==============
private QueueConfig setupSQS(String sqsQueueName) {
    QueueConfig config = new QueueConfig();
    CreateQueueRequest request = new CreateQueueRequest().withQueueName(sqsQueueName);
    CreateQueueResult result = sqsClient.createQueue(request);
    config.sqsQueueURL = result.getQueueUrl();
    GetQueueAttributesRequest qRequest = new GetQueueAttributesRequest().withQueueUrl(config.sqsQueueURL).withAttributeNames("QueueArn");
    GetQueueAttributesResult qResult = sqsClient.getQueueAttributes(qRequest);
    config.sqsQueueARN = qResult.getAttributes().get("QueueArn");
    Policy sqsPolicy = new Policy().withStatements(new Statement(Effect.Allow).withPrincipals(Principal.AllUsers).withActions(SQSActions.SendMessage).withResources(new Resource(config.sqsQueueARN)));
    Map<String, String> queueAttributes = new HashMap<String, String>();
    queueAttributes.put("Policy", sqsPolicy.toJson());
    sqsClient.setQueueAttributes(new SetQueueAttributesRequest(config.sqsQueueURL, queueAttributes));
    return config;
}
Also used : Policy(com.amazonaws.auth.policy.Policy) GetQueueAttributesResult(com.amazonaws.services.sqs.model.GetQueueAttributesResult) GetQueueAttributesRequest(com.amazonaws.services.sqs.model.GetQueueAttributesRequest) HashMap(java.util.HashMap) Statement(com.amazonaws.auth.policy.Statement) CreateQueueRequest(com.amazonaws.services.sqs.model.CreateQueueRequest) Resource(com.amazonaws.auth.policy.Resource) CreateQueueResult(com.amazonaws.services.sqs.model.CreateQueueResult) SetQueueAttributesRequest(com.amazonaws.services.sqs.model.SetQueueAttributesRequest)

Example 2 with Policy

use of com.amazonaws.auth.policy.Policy in project aws-doc-sdk-examples by awsdocs.

the class SetBucketPolicy method getBucketPolicyFromFile.

// Loads a JSON-formatted policy from a file, verifying it with the Policy
// class.
private static String getBucketPolicyFromFile(String policy_file) {
    StringBuilder file_text = new StringBuilder();
    try {
        List<String> lines = Files.readAllLines(Paths.get(policy_file), Charset.forName("UTF-8"));
        for (String line : lines) {
            file_text.append(line);
        }
    } catch (IOException e) {
        System.out.format("Problem reading file: \"%s\"", policy_file);
        System.out.println(e.getMessage());
    }
    // Verify the policy by trying to load it into a Policy object.
    Policy bucket_policy = null;
    try {
        bucket_policy = Policy.fromJson(file_text.toString());
    } catch (IllegalArgumentException e) {
        System.out.format("Invalid policy text in file: \"%s\"", policy_file);
        System.out.println(e.getMessage());
    }
    return bucket_policy.toJson();
}
Also used : Policy(com.amazonaws.auth.policy.Policy) IOException(java.io.IOException)

Aggregations

Policy (com.amazonaws.auth.policy.Policy)2 Resource (com.amazonaws.auth.policy.Resource)1 Statement (com.amazonaws.auth.policy.Statement)1 CreateQueueRequest (com.amazonaws.services.sqs.model.CreateQueueRequest)1 CreateQueueResult (com.amazonaws.services.sqs.model.CreateQueueResult)1 GetQueueAttributesRequest (com.amazonaws.services.sqs.model.GetQueueAttributesRequest)1 GetQueueAttributesResult (com.amazonaws.services.sqs.model.GetQueueAttributesResult)1 SetQueueAttributesRequest (com.amazonaws.services.sqs.model.SetQueueAttributesRequest)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1