use of com.walmartlabs.concord.policyengine.PolicyEngineRules in project concord by walmartlabs.
the class RunnerJob method from.
@SuppressWarnings("unchecked")
public static RunnerJob from(RunnerJobExecutorConfiguration runnerExecutorCfg, JobRequest jobRequest, ProcessLogFactory processLogFactory) throws ExecutionException, IOException {
Map<String, Object> cfg = Collections.emptyMap();
Path payloadDir = jobRequest.getPayloadDir();
Path p = payloadDir.resolve(Constants.Files.CONFIGURATION_FILE_NAME);
if (Files.exists(p)) {
try (InputStream in = Files.newInputStream(p)) {
cfg = new ObjectMapper().readValue(in, Map.class);
} catch (IOException e) {
throw new ExecutionException("Error while reading process configuration", e);
}
}
RunnerConfiguration runnerCfg = createRunnerConfiguration(runnerExecutorCfg, cfg);
RunnerLog log;
try {
log = new RunnerLog(processLogFactory.createRedirectedLog(jobRequest.getInstanceId(), runnerExecutorCfg.segmentedLogs()), processLogFactory.createRemoteLog(jobRequest.getInstanceId()));
} catch (IOException e) {
throw new ExecutionException("Error while creating the runner's log: " + e.getMessage(), e);
}
Path policyFile = payloadDir.resolve(Constants.Files.CONCORD_SYSTEM_DIR_NAME).resolve(Constants.Files.POLICY_FILE_NAME);
PolicyEngine policyEngine = null;
if (Files.exists(policyFile)) {
PolicyEngineRules rules = createObjectMapper().readValue(policyFile.toFile(), PolicyEngineRules.class);
if (rules != null) {
policyEngine = new PolicyEngine(rules);
}
}
return new RunnerJob(jobRequest.getInstanceId(), payloadDir, cfg, runnerCfg, log, policyEngine);
}
use of com.walmartlabs.concord.policyengine.PolicyEngineRules in project concord by walmartlabs.
the class PolicyCacheTest method allowNullValues.
@Test
public void allowNullValues() {
PolicyCache.Dao dao = mock(PolicyCache.Dao.class);
PolicyCache pc = new PolicyCache(TestObjectMapper.INSTANCE, new PolicyCacheConfiguration(), dao);
// ---
Map<String, Object> ruleParams = new HashMap<>();
ruleParams.put("nullValue", null);
ruleParams.put("nullValue2", null);
Map<String, Object> conditions = Collections.singletonMap("entity", ImmutableMap.of("params", ruleParams));
Map<String, Object> denyEntityRule = new HashMap<>();
denyEntityRule.put("msg", "test message");
denyEntityRule.put("action", "create");
denyEntityRule.put("entity", "trigger");
denyEntityRule.put("conditions", conditions);
Map<String, Object> rules = new HashMap<>();
rules.put("entity", Collections.singletonMap("deny", Collections.singletonList(denyEntityRule)));
List<PolicyCache.PolicyRules> policies = Collections.singletonList(PolicyCache.PolicyRules.builder().id(UUID.randomUUID()).name("test").rules(rules).build());
// ---
Map<UUID, PolicyCache.Policy> merged = pc.mergePolicies(policies);
assertEquals(1, merged.size());
PolicyEngineRules actualRules = merged.values().iterator().next().rules();
assertEquals(1, actualRules.getEntityRules().getDeny().size());
EntityRule actualRule = actualRules.getEntityRules().getDeny().get(0);
assertEquals(conditions, actualRule.getConditions());
}
use of com.walmartlabs.concord.policyengine.PolicyEngineRules in project concord by walmartlabs.
the class ConfigurationProcessorTest method testAllCfg.
@Test
public void testAllCfg() throws Exception {
Path workDir = Files.createTempDirectory("testAllCfg_workDir");
UUID instanceId = UUID.randomUUID();
UUID orgId = UUID.randomUUID();
UUID prjId = UUID.randomUUID();
Map<String, Object> req = new HashMap<>();
req.put("a", "a-req");
req.put("req", "req-value");
Map<String, Object> orgCfg = new HashMap<>();
orgCfg.put("a", "a-org");
orgCfg.put("org", "org-value");
Map<String, Object> prjCfg = new HashMap<>();
prjCfg.put("a", "a-prj");
prjCfg.put("project", "prj-value");
ProjectEntry projectEntry = new ProjectEntry(prjId, null, null, null, null, null, prjCfg, null, null, null, null, null, null);
Map<String, Object> processCfgPolicy = new HashMap<>();
processCfgPolicy.put("a", "a-process-cfg-policy");
processCfgPolicy.put("process-cfg-policy", "process-cfg-policy-value");
Map<String, Object> defaultProcessCfgPolicy = new HashMap<>();
defaultProcessCfgPolicy.put("a", "default");
defaultProcessCfgPolicy.put("process-cfg-policy", "default-2");
PolicyEngineRules policy = new PolicyEngineRules(null, null, null, null, null, null, null, null, processCfgPolicy, null, defaultProcessCfgPolicy, null, null, null, null);
// ---
when(orgDao.getConfiguration(eq(orgId))).thenReturn(orgCfg);
when(projectDao.get(eq(prjId))).thenReturn(projectEntry);
Payload payload = new Payload(new ProcessKey(instanceId, OffsetDateTime.now()));
payload = payload.putHeader(Payload.CONFIGURATION, req).putHeader(Payload.ORGANIZATION_ID, orgId).putHeader(Payload.PROJECT_ID, prjId).putHeader(Payload.WORKSPACE_DIR, workDir).putHeader(Payload.POLICY, new PolicyEngine("test", policy));
// ---
Map<String, Object> expected = new HashMap<>();
expected.put("activeProfiles", Collections.singletonList("default"));
// orgCfg < prjCfg < req < org-policy < prj-policy
expected.put("a", "a-process-cfg-policy");
expected.put("org", "org-value");
expected.put("project", "prj-value");
expected.put("req", "req-value");
expected.put("process-cfg-policy", "process-cfg-policy-value");
Map<String, Object> result = process(payload);
// don't care about arguments and other stuff here
result.remove(Constants.Request.ARGUMENTS_KEY);
result.remove(Constants.Request.PROCESS_INFO_KEY);
result.remove(Constants.Request.PROJECT_INFO_KEY);
assertEquals(expected, result);
}
Aggregations