use of com.walmartlabs.concord.policyengine.PolicyEngine 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.PolicyEngine in project concord by walmartlabs.
the class JobDependencies method getDependencyVersions.
private static Map<String, String> getDependencyVersions(RunnerJob job) throws ExecutionException {
Map<String, String> result = getDependencyVersionsFromFile(job);
PolicyEngine pe = job.getPolicyEngine();
if (pe != null) {
// make mutable
result = new HashMap<>(result);
result.putAll(pe.getDefaultDependencyVersionsPolicy().get().stream().collect(Collectors.toMap(Dependency::getArtifact, Dependency::getVersion)));
}
return result;
}
use of com.walmartlabs.concord.policyengine.PolicyEngine in project concord by walmartlabs.
the class PolicyExportProcessor method process.
@Override
public Payload process(Chain chain, Payload payload) {
ProcessKey processKey = payload.getProcessKey();
UUID orgId = payload.getHeader(Payload.ORGANIZATION_ID);
UUID projectId = payload.getHeader(Payload.PROJECT_ID);
UUID userId = payload.getHeader(Payload.INITIATOR_ID);
PolicyEngine policy = policyManager.get(orgId, projectId, userId);
if (policy == null) {
return chain.process(payload);
}
logManager.info(processKey, "Storing policy '{}' data", policy.policyNames());
Path ws = payload.getHeader(Payload.WORKSPACE_DIR);
try {
Path dst = Files.createDirectories(ws.resolve(Constants.Files.CONCORD_SYSTEM_DIR_NAME));
objectMapper.writeValue(dst.resolve(Constants.Files.POLICY_FILE_NAME).toFile(), policy.getRules());
} catch (IOException e) {
logManager.error(processKey, "Error while storing process policy: {}", e);
throw new ProcessException(processKey, "Storing process policy error", e);
}
payload = payload.putHeader(Payload.POLICY, policy);
return chain.process(payload);
}
use of com.walmartlabs.concord.policyengine.PolicyEngine in project concord by walmartlabs.
the class Main method run.
public void run(RunnerConfiguration runnerCfg, Path baseDir) throws Exception {
log.debug("run -> working directory: {}", baseDir.toAbsolutePath());
long t1 = System.currentTimeMillis();
Path idPath = baseDir.resolve(Constants.Files.INSTANCE_ID_FILE_NAME);
UUID instanceId = readInstanceId(idPath);
long t2 = System.currentTimeMillis();
if (runnerCfg.debug()) {
log.info("Spent {}ms waiting for the payload", (t2 - t1));
}
Map<String, Object> policy = readPolicyRules(baseDir);
if (policy.isEmpty()) {
PolicyEngineHolder.INSTANCE.setEngine(null);
} else {
PolicyEngineHolder.INSTANCE.setEngine(new PolicyEngine(objectMapper.convertValue(policy, PolicyEngineRules.class)));
}
// read the process configuration
Map<String, Object> processCfg = readRequest(baseDir);
processCfg = variablesConverter.convert(baseDir, processCfg);
String sessionToken = getSessionToken(processCfg);
ApiClient apiClient = apiClientFactory.create(ApiClientConfiguration.builder().sessionToken(sessionToken).txId(instanceId).build());
ProcessHeartbeat heartbeat = new ProcessHeartbeat(apiClient, instanceId, runnerCfg.api().maxNoHeartbeatInterval());
heartbeat.start();
ProcessApiClient processApiClient = new ProcessApiClient(runnerCfg, apiClient);
processApiClient.updateStatus(instanceId, runnerCfg.agentId(), ProcessEntry.StatusEnum.RUNNING);
CheckpointManager checkpointManager = new CheckpointManager(instanceId, processApiClient);
long t3 = System.currentTimeMillis();
if (runnerCfg.debug()) {
log.info("Ready to start in {}ms", (t3 - t2));
}
executeProcess(instanceId.toString(), checkpointManager, baseDir, processCfg);
}
use of com.walmartlabs.concord.policyengine.PolicyEngine in project concord by walmartlabs.
the class JsonStoreDataManager method assertStorageDataPolicy.
private void assertStorageDataPolicy(UUID orgId, UUID storeId, long currentItemSize, String jsonData) {
PolicyEngine policy = policyManager.get(orgId, null, UserPrincipal.assertCurrent().getUser().getId());
if (policy == null) {
return;
}
CheckResult<JsonStoreRule.StoreDataRule, Long> result;
try {
result = policy.getJsonStoragePolicy().checkStorageData(() -> storeDataDao.getSize(storeId) - currentItemSize + jsonData.length());
} catch (Exception e) {
throw new RuntimeException(e);
}
if (!result.getDeny().isEmpty()) {
throw new ConcordApplicationException("Found JSON store policy violations: " + buildErrorMessage(result.getDeny()));
}
}
Aggregations