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);
}
}
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);
}
}
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);
}
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());
}
}
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);
}
Aggregations