Example usage for com.amazonaws.auth.policy Statement setActions

List of usage examples for com.amazonaws.auth.policy Statement setActions

Introduction

In this page you can find the example usage for com.amazonaws.auth.policy Statement setActions.

Prototype

public void setActions(Collection<Action> actions) 

Source Link

Document

Sets the list of actions to which this policy statement applies.

Usage

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  a v a2 s. co m*/
        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 w  w . j a va2s. c  o  m
    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.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);//  www  . j a v  a  2 s  . c  o 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   w  w w  . ja  v a2s.c o  m
 */
@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//  w w w.  j a  v  a2s . 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;
}

From source file:org.finra.herd.service.helper.AwsPolicyBuilder.java

License:Apache License

/**
 * Adds a permission to allow the specified actions to the given KMS key id.
 *
 * @param kmsKeyId Full ARN to the kms key
 * @param actions List of actions/*from  w  w w .  j  a v a2  s  .c  o  m*/
 *
 * @return This builder
 */
@SuppressWarnings("PMD.CloseResource")
public AwsPolicyBuilder withKms(String kmsKeyId, KmsActions... actions) {
    Statement statement = new Statement(Effect.Allow);
    statement.setActions(Arrays.asList(actions));
    statement.setResources(Arrays.asList(new Resource(kmsKeyId)));
    policy.getStatements().add(statement);
    return this;
}

From source file:org.finra.herd.service.helper.AwsPolicyBuilder.java

License:Apache License

/**
 * Adds a permission to allow the specified actions to the given bucket and s3 object key. The permission will allow the given actions only to the specified
 * object key. If object key is null, the permission is applied to the bucket itself.
 *
 * @param bucketName S3 bucket name//from w w  w.ja  v a2s .  co m
 * @param objectKey S3 object key
 * @param actions List of actions to allow
 *
 * @return This builder
 */
@SuppressWarnings("PMD.CloseResource")
public AwsPolicyBuilder withS3(String bucketName, String objectKey, S3Actions... actions) {
    Statement statement = new Statement(Effect.Allow);
    statement.setActions(Arrays.asList(actions));
    String resource = "arn:aws:s3:::" + bucketName;
    if (objectKey != null) {
        resource += "/" + objectKey;
    }
    statement.setResources(Arrays.asList(new Resource(resource)));
    policy.getStatements().add(statement);
    return this;
}