Search in sources :

Example 1 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class InventoryProcessor method copy.

private boolean copy(Payload payload, AttachmentKey src, String dstName) {
    ProcessKey processKey = payload.getProcessKey();
    Path workspace = payload.getHeader(Payload.WORKSPACE_DIR);
    Path p = payload.getAttachment(src);
    if (p == null) {
        return false;
    }
    Path dst = workspace.resolve(dstName);
    try {
        Files.copy(p, dst);
    } catch (IOException e) {
        logManager.error(processKey, "Error while copying an inventory file: " + p, e);
        throw new ProcessException(processKey, "Error while copying an inventory file: " + p, e);
    }
    return true;
}
Also used : Path(java.nio.file.Path) ProcessException(com.walmartlabs.concord.server.process.ProcessException) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) IOException(java.io.IOException)

Example 2 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class PrivateKeyProcessor method process.

@Override
@SuppressWarnings("unchecked")
public Payload process(Chain chain, Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    Map<String, Object> cfg = payload.getHeader(Payload.CONFIGURATION);
    Map<String, Object> ansibleCfg = (Map<String, Object>) cfg.get(AnsibleConfigurationConstants.GROUP_KEY);
    if (ansibleCfg == null) {
        return chain.process(payload);
    }
    Collection<Map<String, Object>> keys = (Collection<Map<String, Object>>) ansibleCfg.get(AnsibleConfigurationConstants.PRIVATE_KEYS);
    if (keys == null) {
        return chain.process(payload);
    }
    deprecationWarning(processKey);
    String secret = findMatchingSecret(payload, keys);
    if (secret == null) {
        logManager.error(processKey, "No matching secrets found");
        throw new ProcessException(processKey, "No matching secrets found");
    }
    UUID orgId = getOrgId(payload);
    KeyPair keyPair = secretManager.getKeyPair(SecretManager.AccessScope.internal(), orgId, secret, null);
    if (keyPair == null) {
        logManager.error(processKey, "Secret not found: " + secret);
        throw new ProcessException(processKey, "Secret not found: " + secret);
    }
    if (keyPair.getPrivateKey() == null) {
        logManager.error(processKey, "Private key not found: " + secret);
        throw new ProcessException(processKey, "Private key not found: " + secret);
    }
    Path workspace = payload.getHeader(Payload.WORKSPACE_DIR);
    Path dst = workspace.resolve(PRIVATE_KEY_FILE_NAME);
    try {
        Files.write(dst, keyPair.getPrivateKey(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
    } catch (IOException e) {
        logManager.error(processKey, "Error while copying a private key: " + dst, e);
        throw new ProcessException(processKey, "Error while copying a private key: " + dst, e);
    }
    log.info("process ['{}'] -> done", processKey);
    return chain.process(payload);
}
Also used : Path(java.nio.file.Path) ProcessException(com.walmartlabs.concord.server.process.ProcessException) KeyPair(com.walmartlabs.concord.common.secret.KeyPair) Collection(java.util.Collection) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) IOException(java.io.IOException) UUID(java.util.UUID) Map(java.util.Map)

Example 3 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class PayloadRestoreProcessor method process.

@Override
@WithTimer
public Payload process(Chain chain, Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    Map<String, Object> headers = stateManager.get(processKey, "_initial/payload.json", inputStream -> {
        Map<String, Object> result = deserialize(inputStream);
        return Optional.ofNullable(result);
    }).orElseThrow(() -> new ConcordApplicationException("Initial state not found", Response.Status.INTERNAL_SERVER_ERROR));
    payload = payload.putHeaders(headers);
    Path baseDir = payload.getHeader(Payload.BASE_DIR);
    ProcessStateManager.ItemConsumer cp = ProcessStateManager.copyTo(baseDir);
    Map<String, Path> attachments = new HashMap<>();
    stateManager.exportDirectory(processKey, "_initial/attachments/", (name, unixMode, src) -> {
        cp.accept(name, unixMode, src);
        attachments.put(name, baseDir.resolve(name));
    });
    payload = payload.putAttachments(attachments);
    return chain.process(payload);
}
Also used : ConcordObjectMapper(com.walmartlabs.concord.server.ConcordObjectMapper) Payload(com.walmartlabs.concord.server.process.Payload) ProcessStateManager(com.walmartlabs.concord.server.process.state.ProcessStateManager) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HashMap(java.util.HashMap) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) Inject(javax.inject.Inject) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule) Response(javax.ws.rs.core.Response) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) Map(java.util.Map) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) Optional(java.util.Optional) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) Named(javax.inject.Named) Path(java.nio.file.Path) InputStream(java.io.InputStream) Path(java.nio.file.Path) ProcessStateManager(com.walmartlabs.concord.server.process.state.ProcessStateManager) HashMap(java.util.HashMap) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) HashMap(java.util.HashMap) Map(java.util.Map) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Example 4 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey 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 5 with ProcessKey

use of com.walmartlabs.concord.server.sdk.ProcessKey in project concord by walmartlabs.

the class EnqueueingProcessor method process.

@Override
@WithTimer
public Payload process(Chain chain, Payload payload) {
    boolean enqueued = queueManager.enqueue(payload);
    if (!enqueued) {
        // (e.g. "exclusive" processes can be rejected before reaching the ENQUEUED status)
        return payload;
    }
    ProcessKey processKey = payload.getProcessKey();
    Map<String, Object> requirements = PayloadUtils.getRequirements(payload);
    OffsetDateTime startAt = PayloadUtils.getStartAt(payload);
    if (startAt == null) {
        logManager.info(processKey, "Enqueued. Waiting for an agent (requirements={})...", requirements);
    } else {
        logManager.info(processKey, "Enqueued. Starting at {} (requirements={})...", startAt, requirements);
    }
    return chain.process(payload);
}
Also used : OffsetDateTime(java.time.OffsetDateTime) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Aggregations

ProcessKey (com.walmartlabs.concord.server.sdk.ProcessKey)69 WithTimer (com.walmartlabs.concord.server.sdk.metrics.WithTimer)28 Path (java.nio.file.Path)27 PartialProcessKey (com.walmartlabs.concord.server.sdk.PartialProcessKey)25 ApiOperation (io.swagger.annotations.ApiOperation)22 ProcessException (com.walmartlabs.concord.server.process.ProcessException)20 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)16 IOException (java.io.IOException)16 UUID (java.util.UUID)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)8 PolicyEngine (com.walmartlabs.concord.policyengine.PolicyEngine)8 Inject (javax.inject.Inject)8 Named (javax.inject.Named)8 ProcessKeyCache (com.walmartlabs.concord.server.process.queue.ProcessKeyCache)7 ProcessQueueDao (com.walmartlabs.concord.server.process.queue.ProcessQueueDao)7 ProcessStateManager (com.walmartlabs.concord.server.process.state.ProcessStateManager)7 UserPrincipal (com.walmartlabs.concord.server.security.UserPrincipal)7 ProcessLogManager (com.walmartlabs.concord.server.process.logs.ProcessLogManager)6 HttpUtils (com.walmartlabs.concord.server.HttpUtils)5 ResourceAccessLevel (com.walmartlabs.concord.server.org.ResourceAccessLevel)5