Search in sources :

Example 6 with ProcessException

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);
}
Also used : Options(com.walmartlabs.concord.process.loader.model.Options) ProcessException(com.walmartlabs.concord.server.process.ProcessException) ProcessDefinition(com.walmartlabs.concord.process.loader.model.ProcessDefinition) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProcessException(com.walmartlabs.concord.server.process.ProcessException)

Example 7 with ProcessException

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);
    }
}
Also used : Path(java.nio.file.Path) ProcessException(com.walmartlabs.concord.server.process.ProcessException) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) PartialProcessKey(com.walmartlabs.concord.server.sdk.PartialProcessKey) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI)

Example 8 with ProcessException

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);
}
Also used : Path(java.nio.file.Path) ProcessException(com.walmartlabs.concord.server.process.ProcessException) CycleChecker(com.walmartlabs.concord.common.CycleChecker) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey)

Example 9 with ProcessException

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;
}
Also used : ScriptException(javax.script.ScriptException) ProcessException(com.walmartlabs.concord.server.process.ProcessException) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) IOException(java.io.IOException) Bindings(javax.script.Bindings) Map(java.util.Map)

Example 10 with ProcessException

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");
    }
}
Also used : Path(java.nio.file.Path) ProcessException(com.walmartlabs.concord.server.process.ProcessException) InputStream(java.io.InputStream) ContainerRule(com.walmartlabs.concord.policyengine.ContainerRule) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) IOException(java.io.IOException)

Aggregations

ProcessException (com.walmartlabs.concord.server.process.ProcessException)31 ProcessKey (com.walmartlabs.concord.server.sdk.ProcessKey)20 Path (java.nio.file.Path)19 IOException (java.io.IOException)18 UUID (java.util.UUID)9 InputStream (java.io.InputStream)4 Map (java.util.Map)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 PolicyEngine (com.walmartlabs.concord.policyengine.PolicyEngine)3 ProjectEntry (com.walmartlabs.concord.server.org.project.ProjectEntry)3 ProcessDefinition (com.walmartlabs.concord.process.loader.model.ProcessDefinition)2 Snapshot (com.walmartlabs.concord.repository.Snapshot)2 Payload (com.walmartlabs.concord.server.process.Payload)2 PartialProcessKey (com.walmartlabs.concord.server.sdk.PartialProcessKey)2 WithTimer (com.walmartlabs.concord.server.sdk.metrics.WithTimer)2 UserInfo (com.walmartlabs.concord.server.user.UserInfoProvider.UserInfo)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 ConfigurationUtils (com.walmartlabs.concord.common.ConfigurationUtils)1 CycleChecker (com.walmartlabs.concord.common.CycleChecker)1