List of usage examples for com.amazonaws.auth.policy Policy Policy
public Policy(String id)
From source file:awslabs.lab31.SolutionCode.java
License:Open Source License
@Override public void grantNotificationPermission(AmazonSQSClient sqsClient, String queueArn, String queueUrl, String topicArn) {//from w w w. j a v a2 s . c o m Statement statement = new Statement(Effect.Allow).withActions(SQSActions.SendMessage) .withPrincipals(new Principal("*")).withConditions(ConditionFactory.newSourceArnCondition(topicArn)) .withResources(new Resource(queueArn)); Policy policy = new Policy("SubscriptionPermission").withStatements(statement); HashMap<String, String> attributes = new HashMap<String, String>(); attributes.put("Policy", policy.toJson()); // Create the request to set the queue attributes for policy SetQueueAttributesRequest setQueueAttributesRequest = new SetQueueAttributesRequest().withQueueUrl(queueUrl) .withAttributes(attributes); // Set the queue policy sqsClient.setQueueAttributes(setQueueAttributesRequest); }
From source file:com.netflix.conductor.contribs.queue.sqs.SQSObservableQueue.java
License:Apache License
private String getPolicy(List<String> accountIds) { Policy policy = new Policy("ReloadedWorkerAccessPolicy"); Statement stmt = new Statement(Effect.Allow); Action action = SQSActions.SendMessage; stmt.getActions().add(action);// w w w .j ava 2s.c om stmt.setResources(new LinkedList<>()); for (String accountId : accountIds) { Principal principal = new Principal(accountId); stmt.getPrincipals().add(principal); } stmt.getResources().add(new Resource(getQueueARN())); policy.getStatements().add(stmt); return policy.toJson(); }
From source file:n3phele.storage.s3.CloudStorageImpl.java
License:Open Source License
public boolean setPermissions(Repository repo, String filename, boolean isPublic) { String bucket = repo.getRoot(); Credential credential = repo.getCredential().decrypt(); AmazonS3Client s3 = new AmazonS3Client( new BasicAWSCredentials(credential.getAccount(), credential.getSecret())); String key = new S3ObjectResource(bucket, filename).getId(); boolean inserted = false; s3.setEndpoint(repo.getTarget().toString()); try {//from w ww .j av a2 s . c om List<Statement> statements = new ArrayList<Statement>(); Policy policy = null; BucketPolicy bp = s3.getBucketPolicy(repo.getRoot()); if (bp != null && bp.getPolicyText() != null) { log.info("Policy text " + bp.getPolicyText()); policy = PolicyHelper.parse(bp.getPolicyText()); log.info("Policy object is " + (policy == null ? null : policy.toJson())); if (policy != null) { if (policy.getStatements() != null) { for (Statement statement : policy.getStatements()) { if (statement.getId().equals("n3phele")) { List<com.amazonaws.auth.policy.Resource> resources = statement.getResources(); List<com.amazonaws.auth.policy.Resource> update = new ArrayList<com.amazonaws.auth.policy.Resource>(); if (resources != null) { for (com.amazonaws.auth.policy.Resource resource : resources) { String resourceName = resource.getId(); if (resourceName.endsWith("*")) { resourceName = resourceName.substring(0, resourceName.length() - 1); } if (!(resourceName + "/").startsWith(key + "/")) { update.add(resource); } else { log.info("Removing " + resource.getId()); } } } if (isPublic && !inserted) update.add(new S3ObjectResource(repo.getRoot(), filename + "*")); if (update.size() > 0) { statement.setResources(update); statements.add(statement); } inserted = true; } else { statements.add(statement); } } } if (!inserted && isPublic) { Statement statement = new Statement(Effect.Allow); statement.setId("n3phele"); statement.setPrincipals(Arrays.asList(new Principal("*"))); statement.setActions(Arrays.asList((Action) S3Actions.GetObject)); statement.setResources(Arrays .asList((com.amazonaws.auth.policy.Resource) new S3ObjectResource(repo.getRoot(), filename + "*"))); statements.add(statement); } } } if (policy == null && isPublic) { policy = new Policy("n3phele-" + repo.getRoot()); Statement statement = new Statement(Effect.Allow); statement.setId("n3phele"); statement.setPrincipals(Arrays.asList(new Principal("*"))); statement.setActions(Arrays.asList((Action) S3Actions.GetObject)); statement.setResources(Arrays.asList( (com.amazonaws.auth.policy.Resource) new S3ObjectResource(repo.getRoot(), filename + "*"))); statements.add(statement); } if (policy != null) { if (statements.size() != 0) { policy.setStatements(statements); s3.setBucketPolicy(repo.getRoot(), policy.toJson()); log.info("Set policy " + policy.toJson()); } else { s3.deleteBucketPolicy(repo.getRoot()); } } return true; } catch (AmazonServiceException e) { log.log(Level.WARNING, "Service Error processing " + repo, e); } catch (AmazonClientException e) { log.log(Level.SEVERE, "Client Error processing " + repo, e); } catch (IllegalArgumentException e) { log.log(Level.SEVERE, "parse error ", e); log.log(Level.SEVERE, "cause", e.getCause()); } return false; }
From source file:n3phele.storage.s3.PolicyHelper.java
License:Open Source License
public static Policy parse(String s) { Policy result = null;//from w ww . j av a2 s. com try { JSONObject jo = new JSONObject(s); String id = jo.getString("Id"); result = new Policy(id); JSONArray statementArray = jo.getJSONArray("Statement"); List<Statement> statements = new ArrayList<Statement>(); if (statementArray != null) { for (int i = 0; i < statementArray.length(); i++) { JSONObject js = statementArray.getJSONObject(i); Statement statement = new Statement(Effect.valueOf((js.getString("Effect")))); String sid = js.getString("Sid"); statement.setId(sid); if (js.has("Action")) statement.setActions(parseActions(js.get("Action"))); if (js.has("Resource")) statement.setResources(parseResources(js.get("Resource"))); if (js.has("Principal")) statement.setPrincipals(parsePrincipal(js.get("Principal"))); if (js.has("Condition")) statement.setConditions(parseCondition(js.get("Condition"))); statements.add(statement); } result.setStatements(statements); } } catch (JSONException e) { log.log(Level.SEVERE, "error parsing policy", e); } return result; }
From source file:org.dasein.prototype.iamc.AWS.java
License:Apache License
public boolean grantAccessToUser(String username, Service service) { String entityName;/*from w w w . j ava 2s . com*/ Action action; switch (service) { case ElasticBeanstalk: entityName = "iamc-eb"; action = ElasticBeanstalkActions.AllElasticBeanstalkActions; break; case EC2: entityName = "iamc-ec2"; action = EC2Actions.AllEC2Actions; break; default: return false; } try { iamClient.getGroup(new GetGroupRequest(entityName)); } catch (NoSuchEntityException e) { iamClient.createGroup(new CreateGroupRequest(entityName)); } Policy policy = new Policy(entityName).withStatements( new Statement(Statement.Effect.Allow).withActions(action).withResources(new Resource("*"))); iamClient.putGroupPolicy(new PutGroupPolicyRequest(entityName, entityName, policy.toJson())); iamClient.addUserToGroup(new AddUserToGroupRequest(entityName, username)); return true; }