Search in sources :

Example 56 with WithTimer

use of com.walmartlabs.concord.server.sdk.metrics.WithTimer in project concord by walmartlabs.

the class ProcessAnsibleResource method listEvents.

/**
 * Lists Ansible events of a specific process.
 */
@GET
@ApiOperation("List Ansible events of a specific process")
@Path("/{processInstanceId}/ansible/events")
@Produces(MediaType.APPLICATION_JSON)
@WithTimer
public List<ProcessEventEntry> listEvents(@PathParam("processInstanceId") UUID processInstanceId, @QueryParam("host") String host, @QueryParam("hostGroup") String hostGroup, @QueryParam("status") String status, @QueryParam("playbookId") UUID playbookId) {
    ProcessKey key = processKeyCache.get(processInstanceId);
    if (key == null) {
        return Collections.emptyList();
    }
    Map<String, String> eventFilter = new HashMap<>();
    if (host != null) {
        eventFilter.put("host", host);
    }
    if (hostGroup != null) {
        eventFilter.put("hostGroup", hostGroup);
    }
    if (status != null) {
        eventFilter.put("status", status);
    }
    if (playbookId != null) {
        eventFilter.put("playbookId", playbookId.toString());
    }
    return eventDao.list(key, eventFilter);
}
Also used : ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) ApiOperation(io.swagger.annotations.ApiOperation)

Example 57 with WithTimer

use of com.walmartlabs.concord.server.sdk.metrics.WithTimer in project concord by walmartlabs.

the class PayloadStoreProcessor method process.

@Override
@WithTimer
public Payload process(Chain chain, Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    // remove things that shouldn't be in the serialized payload
    Map<String, Object> headers = payload.getHeaders().entrySet().stream().filter(e -> !(e.getValue() instanceof Path)).filter(e -> !EXCLUDED_HEADERS.contains(e.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    String serializedHeaders = serialize(headers);
    stateManager.tx(tx -> {
        stateManager.insert(tx, processKey, "_initial/payload.json", serializedHeaders.getBytes());
        stateManager.importPath(tx, processKey, "_initial/attachments/", payload.getHeader(Payload.BASE_DIR), (path, basicFileAttributes) -> payload.getAttachments().containsValue(path));
    });
    return chain.process(payload);
}
Also used : Path(java.nio.file.Path) Payload(com.walmartlabs.concord.server.process.Payload) ProcessStateManager(com.walmartlabs.concord.server.process.state.ProcessStateManager) Arrays(java.util.Arrays) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) Collectors(java.util.stream.Collectors) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) Inject(javax.inject.Inject) HashSet(java.util.HashSet) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) Map(java.util.Map) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) Named(javax.inject.Named) Path(java.nio.file.Path) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) Map(java.util.Map) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Example 58 with WithTimer

use of com.walmartlabs.concord.server.sdk.metrics.WithTimer in project concord by walmartlabs.

the class RepositoryProcessor method process.

@Override
@WithTimer
public Payload process(Chain chain, final Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    UUID projectId = payload.getHeader(Payload.PROJECT_ID);
    RepositoryEntry repo = getRepositoryEntry(payload);
    if (projectId == null || repo == null) {
        return chain.process(payload);
    }
    logManager.info(processKey, "Copying the repository's data: {} @ {}:{}, path: {}", repo.getUrl(), repo.getBranch() != null ? repo.getBranch() : "*", repo.getCommitId() != null ? repo.getCommitId() : "head", repo.getPath() != null ? repo.getPath() : "/");
    Path dst = payload.getHeader(Payload.WORKSPACE_DIR);
    Payload newPayload = repositoryManager.withLock(repo.getUrl(), () -> {
        try {
            Repository repository = payload.getHeader(Payload.REPOSITORY);
            if (repository == null) {
                repository = repositoryManager.fetch(projectId, repo, true);
            }
            Snapshot snapshot = repository.export(dst);
            CommitInfo ci = null;
            if (repository.fetchResult() != null) {
                FetchResult r = Objects.requireNonNull(repository.fetchResult());
                ci = new CommitInfo(r.head(), r.branchOrTag(), r.author(), r.message());
            }
            RepositoryInfo i = new RepositoryInfo(repo.getId(), repo.getName(), repo.getUrl(), repo.getPath(), repo.getBranch(), repo.getCommitId(), ci);
            return payload.putHeader(REPOSITORY_INFO_KEY, i).putHeader(Payload.REPOSITORY, repository).putHeader(Payload.REPOSITORY_SNAPSHOT, Collections.singletonList(snapshot));
        } catch (Exception e) {
            log.error("process -> repository error", e);
            logManager.error(processKey, "Error while processing a repository: " + repo.getUrl(), e);
            throw new ProcessException(processKey, "Error while processing a repository: " + repo.getUrl(), e);
        }
    });
    return chain.process(newPayload);
}
Also used : Path(java.nio.file.Path) Snapshot(com.walmartlabs.concord.repository.Snapshot) Repository(com.walmartlabs.concord.repository.Repository) ProcessException(com.walmartlabs.concord.server.process.ProcessException) FetchResult(com.walmartlabs.concord.repository.FetchResult) Payload(com.walmartlabs.concord.server.process.Payload) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) RepositoryEntry(com.walmartlabs.concord.server.org.project.RepositoryEntry) UUID(java.util.UUID) ProcessException(com.walmartlabs.concord.server.process.ProcessException) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Example 59 with WithTimer

use of com.walmartlabs.concord.server.sdk.metrics.WithTimer in project concord by walmartlabs.

the class StateImportingProcessor method process.

@Override
@WithTimer
public Payload process(Chain chain, Payload payload) {
    ProcessKey processKey = payload.getProcessKey();
    Path workspace = payload.getHeader(Payload.WORKSPACE_DIR);
    List<Snapshot> snapshots = payload.getHeader(Payload.REPOSITORY_SNAPSHOT);
    stateManager.replacePath(processKey, workspace, (p, attrs) -> filter(p, attrs, snapshots, workspace));
    return chain.process(payload);
}
Also used : Path(java.nio.file.Path) Snapshot(com.walmartlabs.concord.repository.Snapshot) ProcessKey(com.walmartlabs.concord.server.sdk.ProcessKey) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Example 60 with WithTimer

use of com.walmartlabs.concord.server.sdk.metrics.WithTimer in project concord by walmartlabs.

the class SecretManager method assertAccess.

@WithTimer
public SecretEntry assertAccess(UUID orgId, UUID secretId, String secretName, ResourceAccessLevel level, boolean orgMembersOnly) {
    if (secretId == null && (orgId == null || secretName == null)) {
        throw new ValidationErrorsException("Secret ID or an organization ID and a secret name is required");
    }
    SecretEntry e = null;
    if (secretId != null) {
        e = secretDao.get(secretId);
        if (e == null) {
            throw new WebApplicationException("Secret not found: " + secretId, Status.NOT_FOUND);
        }
    }
    if (e == null) {
        e = secretDao.getByName(orgId, secretName);
        if (e == null) {
            throw new WebApplicationException("Secret not found: " + secretName, Status.NOT_FOUND);
        }
    }
    if (Roles.isAdmin()) {
        // an admin can access any secret
        return e;
    }
    if (level == ResourceAccessLevel.READER && (Roles.isGlobalReader() || Roles.isGlobalWriter())) {
        return e;
    } else if (level == ResourceAccessLevel.WRITER && Roles.isGlobalWriter()) {
        return e;
    }
    UserPrincipal p = UserPrincipal.assertCurrent();
    EntityOwner owner = e.getOwner();
    if (owner != null && p.getId().equals(owner.id())) {
        // the owner can do anything with his secrets
        return e;
    }
    if (orgMembersOnly && e.getVisibility() == SecretVisibility.PUBLIC && level == ResourceAccessLevel.READER && userDao.isInOrganization(p.getId(), e.getOrgId())) {
        // organization members can access any public secret in the same organization
        return e;
    }
    OrganizationEntry org = orgManager.assertAccess(e.getOrgId(), false);
    if (ResourceAccessUtils.isSame(p, org.getOwner())) {
        // the org owner can do anything with the org's secrets
        return e;
    }
    if (orgMembersOnly || e.getVisibility() != SecretVisibility.PUBLIC) {
        // the organization's members or the secret is not public
        if (!secretDao.hasAccessLevel(e.getId(), p.getId(), ResourceAccessLevel.atLeast(level))) {
            throw new UnauthorizedException("The current user doesn't have " + "the necessary access level (" + level + ") to the secret: " + e.getName());
        }
    }
    return e;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException) UserPrincipal(com.walmartlabs.concord.server.security.UserPrincipal) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Aggregations

WithTimer (com.walmartlabs.concord.server.sdk.metrics.WithTimer)64 ApiOperation (io.swagger.annotations.ApiOperation)32 ProcessKey (com.walmartlabs.concord.server.sdk.ProcessKey)26 PartialProcessKey (com.walmartlabs.concord.server.sdk.PartialProcessKey)24 ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)22 UserPrincipal (com.walmartlabs.concord.server.security.UserPrincipal)16 UnauthorizedException (org.apache.shiro.authz.UnauthorizedException)10 UUID (java.util.UUID)9 ProcessEntry (com.walmartlabs.concord.server.process.ProcessEntry)7 EntryPoint (com.walmartlabs.concord.server.process.PayloadManager.EntryPoint)6 Inject (javax.inject.Inject)5 Named (javax.inject.Named)5 Payload (com.walmartlabs.concord.server.process.Payload)4 Path (java.nio.file.Path)4 ValidationErrorsException (org.sonatype.siesta.ValidationErrorsException)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 HttpUtils (com.walmartlabs.concord.server.HttpUtils)3 ProcessFilter (com.walmartlabs.concord.server.process.queue.ProcessFilter)3 UserEntry (com.walmartlabs.concord.server.user.UserEntry)3 IOException (java.io.IOException)3