use of com.walmartlabs.concord.server.process.ProcessException in project concord by walmartlabs.
the class EffectiveProcessDefinitionProcessor method process.
@Override
public Payload process(Chain chain, Payload payload) {
ProcessDefinition pd = payload.getHeader(Payload.PROJECT_DEFINITION);
if (pd == null) {
return chain.process(payload);
}
Options opts = Options.builder().instanceId(payload.getProcessKey().getInstanceId()).parentInstanceId(payload.getHeader(Payload.PARENT_INSTANCE_ID)).configuration(sanitizeConfiguration(payload.getHeader(Payload.CONFIGURATION, Collections.emptyMap()))).activeProfiles(payload.getHeader(Payload.ACTIVE_PROFILES, Collections.emptyList())).build();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
pd.serialize(opts, out);
byte[] bytes = out.toByteArray();
if (bytes.length == 0) {
return chain.process(payload);
}
stateManager.tx(tx -> {
stateManager.deleteFile(tx, payload.getProcessKey(), EFFECTIVE_YAML_PATH);
stateManager.insert(tx, payload.getProcessKey(), EFFECTIVE_YAML_PATH, bytes);
});
} catch (Exception e) {
log.warn("process ['{}'] -> error: {}", payload.getProcessKey(), e.getMessage());
throw new ProcessException(payload.getProcessKey(), "Error while processing effective concord.yml: " + e.getMessage(), e);
}
return chain.process(payload);
}
use of com.walmartlabs.concord.server.process.ProcessException in project concord by walmartlabs.
the class TemplateFilesProcessor method process.
@Override
public Payload process(Chain chain, Payload payload) {
ProcessKey processKey = payload.getProcessKey();
Map<String, Object> cfg = payload.getHeader(Payload.CONFIGURATION);
Object s = cfg.get(Constants.Request.TEMPLATE_KEY);
if (!(s instanceof String)) {
return chain.process(payload);
}
try {
URI uri = getUri(processKey, (String) s);
Path template = dependencyManager.resolveSingle(uri).getPath();
extract(payload, template);
return chain.process(payload);
} catch (URISyntaxException | IOException e) {
logManager.error(processKey, "Template error: " + s, e);
throw new ProcessException(processKey, "Error while processing a template: " + s, e);
}
}
use of com.walmartlabs.concord.server.process.ProcessException in project concord by walmartlabs.
the class TemplateScriptProcessor method process.
@Override
public Payload process(Chain chain, Payload payload) {
Path workspace = payload.getHeader(Payload.WORKSPACE_DIR);
// process _main.js
Path scriptPath = workspace.resolve(REQUEST_DATA_TEMPLATE_FILE_NAME);
if (!Files.exists(scriptPath)) {
return chain.process(payload);
}
ProcessKey processKey = payload.getProcessKey();
Map<String, Object> in = payload.getHeader(Payload.CONFIGURATION);
Map<String, Object> out = processScript(processKey, in, scriptPath);
Map<String, Object> merged = ConfigurationUtils.deepMerge(in, out);
CycleChecker.CheckResult result = CycleChecker.check(INPUT_REQUEST_DATA_KEY, merged);
if (result.isHasCycle()) {
throw new ProcessException(processKey, "Found cycle in " + REQUEST_DATA_TEMPLATE_FILE_NAME + ": " + result.getNode1() + " <-> " + result.getNode2());
}
payload = payload.putHeader(Payload.CONFIGURATION, merged);
return chain.process(payload);
}
use of com.walmartlabs.concord.server.process.ProcessException in project concord by walmartlabs.
the class TemplateScriptProcessor method processScript.
@SuppressWarnings("unchecked")
private Map<String, Object> processScript(ProcessKey processKey, Map<String, Object> meta, Path templateMeta) {
Object result;
try (Reader r = new FileReader(templateMeta.toFile())) {
Bindings b = scriptEngine.createBindings();
b.put("polyglot.js.allowAllAccess", true);
b.put(INPUT_REQUEST_DATA_KEY, meta != null ? meta : Collections.emptyMap());
result = scriptEngine.eval(r, b);
if (!(result instanceof Map)) {
throw new ProcessException(processKey, "Invalid template result. Expected a Java Map instance, got " + result);
}
} catch (IOException | ScriptException e) {
logManager.error(processKey, "Template script execution error", e);
throw new ProcessException(processKey, "Template script execution error", e);
}
return (Map<String, Object>) result;
}
use of com.walmartlabs.concord.server.process.ProcessException in project concord by walmartlabs.
the class ContainerPolicyApplier method apply.
@Override
@SuppressWarnings("unchecked")
public void apply(Payload payload, PolicyEngine policy) {
ProcessKey processKey = payload.getProcessKey();
Path workDir = payload.getHeader(Payload.WORKSPACE_DIR);
Path p = workDir.resolve(Constants.Files.CONFIGURATION_FILE_NAME);
if (!Files.exists(p)) {
return;
}
Map<String, Object> containerOptions;
try (InputStream in = Files.newInputStream(p)) {
Map<String, Object> m = objectMapper.readValue(in, Map.class);
containerOptions = (Map<String, Object>) m.get(Constants.Request.CONTAINER);
} catch (IOException e) {
logManager.error(processKey, "Error while reading container configuration: {}", e);
throw new ProcessException(processKey, "Error while reading container configuration", e);
}
CheckResult<ContainerRule, Object> result = policy.getContainerPolicy().check(containerOptions);
result.getWarn().forEach(i -> {
policyWarn.inc();
logManager.warn(processKey, appendMsg("Potential container policy violation (policy: {})", i.getMsg()), i.getRule());
});
result.getDeny().forEach(i -> {
policyDeny.inc();
logManager.error(processKey, appendMsg("Container policy violation", i.getMsg()), i.getRule());
});
if (!result.getDeny().isEmpty()) {
throw new ProcessException(processKey, "Found container policy violations");
}
}
Aggregations