List of usage examples for com.amazonaws.auth.policy Policy setStatements
public void setStatements(Collection<Statement> statements)
From source file:com.clicktravel.infrastructure.messaging.aws.sqs.DefaultSqsQueueResourceFactory.java
License:Apache License
private Policy acceptMessagesFromTopicsPolicy(final SqsQueueResource sqsQueueResource, final SnsTopicResource... snsTopics) { final Collection<Statement> statements = new ArrayList<>(); for (final SnsTopicResource snsTopicResource : snsTopics) { statements.add(acceptMessagesFromTopicStatement(sqsQueueResource, snsTopicResource)); }// w w w . ja v a 2 s . c om final Policy policy = new Policy(); policy.setStatements(statements); return policy; }
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 {// w w w. j a va2 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; try {// ww w .jav a2 s . c om 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.applicationMigrator.userManagement.UserManagementWorker.java
License:Apache License
public void grantPermissions(CreateUserRequest user, AmazonIdentityManagementClient client) { Resource resource = new Resource(BUCKET_NAME + "/" + user.getUserName() + "/*"); Statement statement = new Statement(Effect.Allow); Action deleteObjectAction = S3Actions.DeleteObject; Action getObjectaAction = S3Actions.GetObject; Action putObjectAction = S3Actions.PutObject; Collection<Action> actions = new ArrayList<Action>(); actions.add(deleteObjectAction);/*from w ww. ja v a 2 s .co m*/ actions.add(getObjectaAction); actions.add(putObjectAction); statement.setActions(actions); Collection<Resource> resources = new ArrayList<Resource>(); resources.add(resource); statement.setResources(resources); Policy userPolicy = new Policy(); Collection<Statement> statements = new ArrayList<Statement>(); statements.add(statement); userPolicy.setStatements(statements); PutUserPolicyRequest putUserPolicyRequest = new PutUserPolicyRequest(); putUserPolicyRequest.setPolicyDocument(userPolicy.toJson()); putUserPolicyRequest.setPolicyName(new Date().getTime() + "Policy"); putUserPolicyRequest.setUserName(user.getUserName()); client.putUserPolicy(putUserPolicyRequest); }
From source file:org.finra.dm.service.impl.UploadDownloadServiceImpl.java
License:Apache License
/** * Creates a restricted policy JSON string which only allows PutObject to the given bucket name and object key, and allows GenerateDataKey and Decrypt for * the given key ID. The Decrypt is required for multipart upload with KMS encryption. * * @param s3BucketName - The S3 bucket name to restrict uploads to * @param s3Key - The S3 object key to restrict the uploads to * @param awsKmsKeyId - The KMS key ID to allow access * * @return the policy JSON string/*from ww w . j av a2 s. c om*/ */ @SuppressWarnings("PMD.CloseResource") // These are not SQL statements so they don't need to be closed. private Policy createUploaderPolicy(String s3BucketName, String s3Key, String awsKmsKeyId) { Policy policy = new Policy(); List<Statement> statements = new ArrayList<>(); { Statement statement = new Statement(Effect.Allow); statement.setActions(Arrays.<Action>asList(S3Actions.PutObject)); statement.setResources(Arrays.asList(new Resource("arn:aws:s3:::" + s3BucketName + "/" + s3Key))); statements.add(statement); } { Statement statement = new Statement(Effect.Allow); statement.setActions(Arrays.<Action>asList(new KmsGenerateDataKeyAction(), new KmsDecryptAction())); statement.setResources(Arrays.asList(new Resource(awsKmsKeyId))); statements.add(statement); } policy.setStatements(statements); return policy; }
From source file:org.finra.dm.service.impl.UploadDownloadServiceImpl.java
License:Apache License
/** * Creates a restricted policy JSON string which only allows GetObject to the given bucket name and object key, and allows Decrypt for the given key ID. * * @param s3BucketName - The S3 bucket name to restrict uploads to * @param s3Key - The S3 object key to restrict the uploads to * @param awsKmsKeyId - The KMS key ID to allow access * * @return the policy JSON string/*from w ww .ja v a 2s. c o m*/ */ @SuppressWarnings("PMD.CloseResource") // These are not SQL statements so they don't need to be closed. private Policy createDownloaderPolicy(String s3BucketName, String s3Key, String awsKmsKeyId) { Policy policy = new Policy(); List<Statement> statements = new ArrayList<>(); { Statement statement = new Statement(Effect.Allow); statement.setActions(Arrays.<Action>asList(S3Actions.GetObject)); statement.setResources(Arrays.asList(new Resource("arn:aws:s3:::" + s3BucketName + "/" + s3Key))); statements.add(statement); } { Statement statement = new Statement(Effect.Allow); statement.setActions(Arrays.<Action>asList(new KmsDecryptAction())); statement.setResources(Arrays.asList(new Resource(awsKmsKeyId))); statements.add(statement); } policy.setStatements(statements); return policy; }