Search in sources :

Example 1 with AttachmentsRule

use of com.walmartlabs.concord.policyengine.AttachmentsRule in project concord by walmartlabs.

the class ProcessResource method buildErrorMessage.

private String buildErrorMessage(List<CheckResult.Item<AttachmentsRule, Long>> errors) {
    String defaultMessage = "Attachments too big: current {0} bytes, limit {1} bytes";
    StringBuilder sb = new StringBuilder();
    for (CheckResult.Item<AttachmentsRule, Long> e : errors) {
        AttachmentsRule r = e.getRule();
        String msg = r.getMsg() != null ? r.getMsg() : defaultMessage;
        long actualSize = e.getEntity();
        long limit = r.getMaxSizeInBytes();
        sb.append(MessageFormat.format(msg, actualSize, limit)).append(';');
    }
    return sb.toString();
}
Also used : CheckResult(com.walmartlabs.concord.policyengine.CheckResult) AttachmentsRule(com.walmartlabs.concord.policyengine.AttachmentsRule)

Example 2 with AttachmentsRule

use of com.walmartlabs.concord.policyengine.AttachmentsRule in project concord by walmartlabs.

the class ProcessResource method assertAttachmentsPolicy.

private void assertAttachmentsPolicy(Path tmpDir, ProcessEntry entry) throws IOException {
    PolicyEngine policy = policyManager.get(entry.orgId(), entry.projectId(), UserPrincipal.assertCurrent().getUser().getId());
    if (policy == null) {
        return;
    }
    CheckResult<AttachmentsRule, Long> checkResult = policy.getAttachmentsPolicy().check(tmpDir);
    if (!checkResult.getDeny().isEmpty()) {
        String errorMessage = buildErrorMessage(checkResult.getDeny());
        processLogManager.error(new ProcessKey(entry.instanceId(), entry.createdAt()), errorMessage);
        throw new PolicyException("Found forbidden policy: " + errorMessage);
    }
}
Also used : PolicyException(com.walmartlabs.concord.server.policy.PolicyException) PolicyEngine(com.walmartlabs.concord.policyengine.PolicyEngine) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) AttachmentsRule(com.walmartlabs.concord.policyengine.AttachmentsRule)

Aggregations

AttachmentsRule (com.walmartlabs.concord.policyengine.AttachmentsRule)2 CheckResult (com.walmartlabs.concord.policyengine.CheckResult)1 PolicyEngine (com.walmartlabs.concord.policyengine.PolicyEngine)1 PolicyException (com.walmartlabs.concord.server.policy.PolicyException)1 PartialProcessKey (com.walmartlabs.concord.server.sdk.PartialProcessKey)1 ProcessKey (com.walmartlabs.concord.server.sdk.ProcessKey)1