Search in sources :

Example 21 with ConcordApplicationException

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

the class TriggerProcessExecutor method assertRoles.

private void assertRoles(String eventName) {
    if (Roles.isAdmin()) {
        return;
    }
    // optional feature: require a specific user role to access the external events endpoint
    Map<String, String> requiredRoles = eventsCfg.getRequiredRoles();
    if (requiredRoles == null || requiredRoles.isEmpty()) {
        return;
    }
    Subject s = SecurityUtils.getSubject();
    requiredRoles.forEach((k, v) -> {
        if (eventName.matches(k) && !s.hasRole(v)) {
            throw new ConcordApplicationException("'" + v + "' role is required", Response.Status.FORBIDDEN);
        }
    });
}
Also used : ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) Subject(org.apache.shiro.subject.Subject)

Example 22 with ConcordApplicationException

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

the class ConsoleService method whoami.

@GET
@Path("/whoami")
@Produces(MediaType.APPLICATION_JSON)
public UserResponse whoami() {
    UserPrincipal p = UserPrincipal.getCurrent();
    if (p == null) {
        throw new ConcordApplicationException("Can't determine current user: pricipal not found", Status.INTERNAL_SERVER_ERROR);
    }
    UserEntry u = p.getUser();
    if (u == null) {
        throw new ConcordApplicationException("Can't determine current user: user entry not found", Status.INTERNAL_SERVER_ERROR);
    }
    String displayName = u.getDisplayName();
    if (displayName == null) {
        LdapPrincipal l = LdapPrincipal.getCurrent();
        if (l != null) {
            displayName = l.getDisplayName();
        }
    }
    if (displayName == null) {
        displayName = p.getUsername();
    }
    UserEntry user = userManager.get(p.getId()).orElseThrow(() -> new ConcordApplicationException("Unknown user: " + p.getId()));
    return new UserResponse(p.getRealm(), user.getName(), user.getDomain(), displayName, user.getOrgs());
}
Also used : LdapPrincipal(com.walmartlabs.concord.server.security.ldap.LdapPrincipal) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) UserEntry(com.walmartlabs.concord.server.user.UserEntry) UserPrincipal(com.walmartlabs.concord.server.security.UserPrincipal)

Example 23 with ConcordApplicationException

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

the class ConsoleService method testRepository.

@POST
@Path("/repository/test")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@WithTimer
public boolean testRepository(RepositoryTestRequest req) {
    OrganizationEntry org = orgManager.assertAccess(null, req.getOrgName(), false);
    ProjectEntry project = projectAccessManager.assertAccess(org.getId(), null, req.getProjectName(), ResourceAccessLevel.READER, false);
    try {
        String secretName = secretDao.getName(req.getSecretId());
        repositoryManager.testConnection(project.getOrgId(), project.getId(), req.getUrl(), req.getBranch(), req.getCommitId(), req.getPath(), secretName);
        return true;
    } catch (InvalidRepositoryPathException e) {
        Map<String, String> m = new HashMap<>();
        m.put("message", "Repository validation error");
        m.put("level", "WARN");
        m.put("details", e.getMessage());
        throw new ConcordApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).entity(m).build());
    } catch (Exception e) {
        String msg;
        Throwable t = e;
        while (true) {
            msg = t.getMessage();
            t = t.getCause();
            if (t == null) {
                break;
            }
        }
        if (msg == null) {
            msg = "Repository test error";
        }
        throw new ConcordApplicationException(Response.status(Status.INTERNAL_SERVER_ERROR).header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN).entity(msg).build());
    }
}
Also used : ProjectEntry(com.walmartlabs.concord.server.org.project.ProjectEntry) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) InvalidRepositoryPathException(com.walmartlabs.concord.server.repository.InvalidRepositoryPathException) OrganizationEntry(com.walmartlabs.concord.server.org.OrganizationEntry) UnauthorizedException(org.apache.shiro.authz.UnauthorizedException) InvalidRepositoryPathException(com.walmartlabs.concord.server.repository.InvalidRepositoryPathException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Example 24 with ConcordApplicationException

use of com.walmartlabs.concord.server.sdk.ConcordApplicationException 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)

Example 25 with ConcordApplicationException

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

the class JsonStoreManager method assertStoragePolicy.

private void assertStoragePolicy(UUID orgId) {
    PolicyEngine policy = policyManager.get(orgId, null, UserPrincipal.assertCurrent().getUser().getId());
    if (policy == null) {
        return;
    }
    CheckResult<JsonStoreRule.StoreRule, Integer> result;
    try {
        result = policy.getJsonStoragePolicy().checkStorage(() -> storeDao.count(orgId));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (!result.getDeny().isEmpty()) {
        throw new ConcordApplicationException("Found JSON store policy violations: " + buildErrorMessage(result.getDeny()));
    }
}
Also used : JsonStoreRule(com.walmartlabs.concord.policyengine.JsonStoreRule) 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

ConcordApplicationException (com.walmartlabs.concord.server.sdk.ConcordApplicationException)70 ApiOperation (io.swagger.annotations.ApiOperation)28 UUID (java.util.UUID)22 WithTimer (com.walmartlabs.concord.server.sdk.metrics.WithTimer)21 PartialProcessKey (com.walmartlabs.concord.server.sdk.PartialProcessKey)18 IOException (java.io.IOException)14 ValidationErrorsException (org.sonatype.siesta.ValidationErrorsException)12 GenericOperationResult (com.walmartlabs.concord.server.GenericOperationResult)11 ProcessKey (com.walmartlabs.concord.server.sdk.ProcessKey)11 Validate (org.sonatype.siesta.Validate)11 Path (java.nio.file.Path)10 UserPrincipal (com.walmartlabs.concord.server.security.UserPrincipal)9 OrganizationEntry (com.walmartlabs.concord.server.org.OrganizationEntry)8 EntryPoint (com.walmartlabs.concord.server.process.PayloadManager.EntryPoint)6 ProcessStatus (com.walmartlabs.concord.server.sdk.ProcessStatus)6 UnauthorizedException (org.apache.shiro.authz.UnauthorizedException)6 UserEntry (com.walmartlabs.concord.server.user.UserEntry)5 InputStream (java.io.InputStream)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Form (com.walmartlabs.concord.forms.Form)3