Search in sources :

Example 1 with ConcordApplicationException

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

the class MultipartUtils method toMap.

public static Map<String, Object> toMap(MultipartInput input) {
    Map<String, Object> result = new HashMap<>();
    try {
        for (InputPart p : input.getParts()) {
            String name = MultipartUtils.extractName(p);
            if (name == null || name.startsWith("/") || name.contains("..")) {
                throw new ConcordApplicationException("Invalid attachment name: " + name);
            }
            if (name.endsWith(JSON_FIELD_SUFFIX)) {
                name = name.substring(0, name.length() - JSON_FIELD_SUFFIX.length());
                Object v = objectMapper.readValue(p.getBodyAsString(), Object.class);
                result.put(name, v);
            } else if (p.getMediaType().isCompatible(MediaType.TEXT_PLAIN_TYPE)) {
                String currentValue = p.getBodyAsString().trim();
                result.compute(name, (k, oldValue) -> computeStringMultipartEntry(oldValue, currentValue));
            } else {
                result.put(name, p.getBody(InputStream.class, null));
            }
        }
        return result;
    } catch (IOException e) {
        throw new ConcordApplicationException("Error parsing the request", e);
    }
}
Also used : java.util(java.util) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MultipartInput(org.jboss.resteasy.plugins.providers.multipart.MultipartInput) IOException(java.io.IOException) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) MediaType(javax.ws.rs.core.MediaType) HttpHeaders(javax.ws.rs.core.HttpHeaders) Matcher(java.util.regex.Matcher) Response(javax.ws.rs.core.Response) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) Pattern(java.util.regex.Pattern) InputStream(java.io.InputStream) InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) IOException(java.io.IOException)

Example 2 with ConcordApplicationException

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

the class SecretResource method create.

@POST
@ApiOperation("Creates a new secret")
@Path("/{orgName}/secret")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
@Validate
public SecretOperationResponse create(@ApiParam @PathParam("orgName") @ConcordKey String orgName, @ApiParam MultipartInput input) {
    OrganizationEntry org = orgManager.assertAccess(orgName, true);
    try {
        SecretType type = assertType(input);
        String storeType = assertStoreType(input);
        String name = assertName(input);
        assertUnique(org.getId(), name);
        boolean generatePwd = MultipartUtils.getBoolean(input, Constants.Multipart.GENERATE_PASSWORD, false);
        String storePwd = getOrGenerateStorePassword(input, generatePwd);
        SecretVisibility visibility = getVisibility(input);
        UUID projectId = getProject(input, org.getId());
        switch(type) {
            case KEY_PAIR:
                {
                    return createKeyPair(org.getId(), projectId, name, storePwd, visibility, input, storeType);
                }
            case USERNAME_PASSWORD:
                {
                    return createUsernamePassword(org.getId(), projectId, name, storePwd, visibility, input, storeType);
                }
            case DATA:
                {
                    return createData(org.getId(), projectId, name, storePwd, visibility, input, storeType);
                }
            default:
                throw new ValidationErrorsException("Unsupported secret type: " + type);
        }
    } catch (IOException e) {
        throw new ConcordApplicationException("Error while processing the request: " + e.getMessage(), e);
    }
}
Also used : ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) IOException(java.io.IOException) UUID(java.util.UUID) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException) Validate(org.sonatype.siesta.Validate)

Example 3 with ConcordApplicationException

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

the class SecretResource method updateAccessLevel.

@POST
@ApiOperation("Updates the access level for the specified secret and team")
@Path("/{orgName}/secret/{secretName}/access/bulk")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Validate
public GenericOperationResult updateAccessLevel(@ApiParam @PathParam("orgName") @ConcordKey String orgName, @ApiParam @PathParam("secretName") @ConcordKey String secretName, @ApiParam @Valid Collection<ResourceAccessEntry> entries) {
    OrganizationEntry org = orgManager.assertAccess(orgName, true);
    UUID secretId = secretDao.getId(org.getId(), secretName);
    if (secretId == null) {
        throw new ConcordApplicationException("Secret not found: " + secretName, Status.NOT_FOUND);
    }
    if (entries == null) {
        throw new ConcordApplicationException("List of teams is null.", Status.BAD_REQUEST);
    }
    secretManager.updateAccessLevel(secretId, entries, true);
    return new GenericOperationResult(OperationResult.UPDATED);
}
Also used : GenericOperationResult(com.walmartlabs.concord.server.GenericOperationResult) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) UUID(java.util.UUID) Validate(org.sonatype.siesta.Validate)

Example 4 with ConcordApplicationException

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

the class SecretResource method getData.

@POST
@ApiOperation(value = "Get an existing secret's data", response = File.class)
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = File.class, responseHeaders = @ResponseHeader(name = "X-Concord-SecretType", description = "Secret type", response = String.class)) })
@Path("/{orgName}/secret/{secretName}/data")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
@WithTimer
public Response getData(@ApiParam @PathParam("orgName") @ConcordKey String orgName, @ApiParam @PathParam("secretName") @ConcordKey String secretName, @ApiParam MultipartInput input) {
    OrganizationEntry org = orgManager.assertAccess(orgName, false);
    String password = MultipartUtils.getString(input, Constants.Multipart.STORE_PASSWORD);
    SecretDao.SecretDataEntry entry;
    try {
        entry = secretManager.getRaw(SecretManager.AccessScope.apiRequest(), org.getId(), secretName, password);
        if (entry == null) {
            throw new WebApplicationException("Secret not found: " + secretName, Status.NOT_FOUND);
        }
    } catch (SecurityException e) {
        log.warn("fetchSecret -> error: {}", e.getMessage());
        throw new SecretException("Error while fetching a secret '" + secretName + "': " + e.getMessage());
    } catch (ValidationErrorsException e) {
        log.warn("fetchSecret -> error: {}", e.getMessage());
        return null;
    }
    try {
        return Response.ok((StreamingOutput) output -> output.write(entry.getData()), MediaType.APPLICATION_OCTET_STREAM).header(Constants.Headers.SECRET_TYPE, entry.getType().name()).build();
    } catch (Exception e) {
        log.error("fetchSecret ['{}'] -> error while fetching a secret", secretName, e);
        throw new ConcordApplicationException("Error while fetching a secret '" + secretName + "': " + e.getMessage());
    }
}
Also used : Resource(org.sonatype.siesta.Resource) ConcordKey(com.walmartlabs.concord.common.validation.ConcordKey) UserType(com.walmartlabs.concord.server.user.UserType) LoggerFactory(org.slf4j.LoggerFactory) MultipartInput(org.jboss.resteasy.plugins.providers.multipart.MultipartInput) GenericOperationResult(com.walmartlabs.concord.server.GenericOperationResult) DecryptedKeyPair(com.walmartlabs.concord.server.org.secret.SecretManager.DecryptedKeyPair) Singleton(javax.inject.Singleton) Inject(javax.inject.Inject) Valid(javax.validation.Valid) MultipartUtils(com.walmartlabs.concord.server.MultipartUtils) OperationResult(com.walmartlabs.concord.server.OperationResult) MediaType(javax.ws.rs.core.MediaType) Constants(com.walmartlabs.concord.sdk.Constants) ByteArrayInputStream(java.io.ByteArrayInputStream) TeamDao(com.walmartlabs.concord.server.org.team.TeamDao) DecryptedUsernamePassword(com.walmartlabs.concord.server.org.secret.SecretManager.DecryptedUsernamePassword) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer) io.swagger.annotations(io.swagger.annotations) Named(javax.inject.Named) Status(javax.ws.rs.core.Response.Status) UserManager(com.walmartlabs.concord.server.user.UserManager) Logger(org.slf4j.Logger) Collection(java.util.Collection) Validate(org.sonatype.siesta.Validate) StreamingOutput(javax.ws.rs.core.StreamingOutput) IOException(java.io.IOException) UUID(java.util.UUID) com.walmartlabs.concord.server.org(com.walmartlabs.concord.server.org) File(java.io.File) DecryptedBinaryData(com.walmartlabs.concord.server.org.secret.SecretManager.DecryptedBinaryData) Objects(java.util.Objects) ProjectDao(com.walmartlabs.concord.server.org.project.ProjectDao) List(java.util.List) javax.ws.rs(javax.ws.rs) Response(javax.ws.rs.core.Response) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) InputStream(java.io.InputStream) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException) IOException(java.io.IOException) ValidationErrorsException(org.sonatype.siesta.ValidationErrorsException) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) WithTimer(com.walmartlabs.concord.server.sdk.metrics.WithTimer)

Example 5 with ConcordApplicationException

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

the class SecretResource method updateAccessLevel.

@POST
@ApiOperation("Updates the access level for the specified secret and team")
@Path("/{orgName}/secret/{secretName}/access")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Validate
public GenericOperationResult updateAccessLevel(@ApiParam @PathParam("orgName") @ConcordKey String orgName, @ApiParam @PathParam("secretName") @ConcordKey String secretName, @ApiParam @Valid ResourceAccessEntry entry) {
    OrganizationEntry org = orgManager.assertAccess(orgName, true);
    UUID secretId = secretDao.getId(org.getId(), secretName);
    if (secretId == null) {
        throw new ConcordApplicationException("Secret not found: " + secretName, Status.NOT_FOUND);
    }
    UUID teamId = ResourceAccessUtils.getTeamId(orgDao, teamDao, org.getId(), entry);
    secretManager.updateAccessLevel(secretId, teamId, entry.getLevel());
    return new GenericOperationResult(OperationResult.UPDATED);
}
Also used : GenericOperationResult(com.walmartlabs.concord.server.GenericOperationResult) ConcordApplicationException(com.walmartlabs.concord.server.sdk.ConcordApplicationException) UUID(java.util.UUID) Validate(org.sonatype.siesta.Validate)

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