Search in sources :

Example 1 with PolicyEngine

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);
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) PolicyEngine(com.walmartlabs.concord.policyengine.PolicyEngine) IOException(java.io.IOException) PolicyEngineRules(com.walmartlabs.concord.policyengine.PolicyEngineRules) ExecutionException(com.walmartlabs.concord.agent.ExecutionException) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with 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;
}
Also used : PolicyEngine(com.walmartlabs.concord.policyengine.PolicyEngine) Dependency(com.walmartlabs.concord.policyengine.DependencyVersionsPolicy.Dependency)

Example 3 with PolicyEngine

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

Example 4 with PolicyEngine

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);
}
Also used : Path(java.nio.file.Path) ProcessHeartbeat(com.walmartlabs.concord.runtime.common.ProcessHeartbeat) PolicyEngine(com.walmartlabs.concord.policyengine.PolicyEngine) ApiClient(com.walmartlabs.concord.ApiClient)

Example 5 with PolicyEngine

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()));
    }
}
Also used : ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) PolicyEngine(com.walmartlabs.concord.policyengine.PolicyEngine) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException)

Aggregations

PolicyEngine (com.walmartlabs.concord.policyengine.PolicyEngine)18 ProcessKey (com.walmartlabs.concord.server.sdk.ProcessKey)5 IOException (java.io.IOException)4 Path (java.nio.file.Path)4 ProcessException (com.walmartlabs.concord.server.process.ProcessException)3 UUID (java.util.UUID)3 ValidationErrorsException (org.sonatype.siesta.ValidationErrorsException)3 ExecutionException (com.walmartlabs.concord.agent.ExecutionException)2 PolicyEngineRules (com.walmartlabs.concord.policyengine.PolicyEngineRules)2 PolicyException (com.walmartlabs.concord.server.policy.PolicyException)2 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ApiClient (com.walmartlabs.concord.ApiClient)1 ProcessLog (com.walmartlabs.concord.agent.logging.ProcessLog)1 DependencyEntity (com.walmartlabs.concord.dependencymanager.DependencyEntity)1 AttachmentsRule (com.walmartlabs.concord.policyengine.AttachmentsRule)1 ConcurrentProcessRule (com.walmartlabs.concord.policyengine.ConcurrentProcessRule)1 DependencyRule (com.walmartlabs.concord.policyengine.DependencyRule)1 Dependency (com.walmartlabs.concord.policyengine.DependencyVersionsPolicy.Dependency)1 EntityRule (com.walmartlabs.concord.policyengine.EntityRule)1