use of org.jooq.exception.DataAccessException in project keywhiz by square.
the class SecretResource method createSecret.
/**
* Creates a secret and assigns to given groups
*
* @excludeParams automationClient
* @param request JSON request to create a secret
*
* @responseMessage 201 Created secret and assigned to given groups
* @responseMessage 409 Secret already exists
*/
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createSecret(@Auth AutomationClient automationClient, @Valid CreateSecretRequestV2 request) {
// allows new version, return version in resulting path
String name = request.name();
String user = automationClient.getName();
SecretBuilder builder = secretController.builder(name, request.content(), automationClient.getName(), request.expiry()).withDescription(request.description()).withMetadata(request.metadata()).withType(request.type());
Secret secret;
try {
secret = builder.create();
} catch (DataAccessException e) {
logger.info(format("Cannot create secret %s", name), e);
throw new ConflictException(format("Cannot create secret %s.", name));
}
Map<String, String> extraInfo = new HashMap<>();
if (request.description() != null) {
extraInfo.put("description", request.description());
}
if (request.metadata() != null) {
extraInfo.put("metadata", request.metadata().toString());
}
extraInfo.put("expiry", Long.toString(request.expiry()));
auditLog.recordEvent(new Event(Instant.now(), EventTag.SECRET_CREATE, user, name, extraInfo));
long secretId = secret.getId();
groupsToGroupIds(request.groups()).forEach((maybeGroupId) -> maybeGroupId.ifPresent((groupId) -> aclDAO.findAndAllowAccess(secretId, groupId, auditLog, user, new HashMap<>())));
UriBuilder uriBuilder = UriBuilder.fromResource(SecretResource.class).path(name);
return Response.created(uriBuilder.build()).build();
}
use of org.jooq.exception.DataAccessException in project keywhiz by square.
the class SecretsResource method createSecret.
/**
* Create Secret
*
* @excludeParams user
* @param request the JSON client request used to formulate the Secret
*
* @description Creates a Secret with the name from a valid secret request.
* Used by Keywhiz CLI and the web ui.
* @responseMessage 200 Successfully created Secret
* @responseMessage 400 Secret with given name already exists
*/
@Timed
@ExceptionMetered
@POST
@Consumes(APPLICATION_JSON)
public Response createSecret(@Auth User user, @Valid CreateSecretRequest request) {
logger.info("User '{}' creating secret '{}'.", user, request.name);
Secret secret;
try {
SecretController.SecretBuilder builder = secretController.builder(request.name, request.content, user.getName(), request.expiry);
if (request.description != null) {
builder.withDescription(request.description);
}
if (request.metadata != null) {
builder.withMetadata(request.metadata);
}
secret = builder.create();
} catch (DataAccessException e) {
logger.info(format("Cannot create secret %s", request.name), e);
throw new ConflictException(format("Cannot create secret %s.", request.name));
}
URI uri = UriBuilder.fromResource(SecretsResource.class).path("{secretId}").build(secret.getId());
Response response = Response.created(uri).entity(secretDetailResponseFromId(secret.getId())).build();
if (response.getStatus() == HttpStatus.SC_CREATED) {
Map<String, String> extraInfo = new HashMap<>();
if (request.description != null) {
extraInfo.put("description", request.description);
}
if (request.metadata != null) {
extraInfo.put("metadata", request.metadata.toString());
}
extraInfo.put("expiry", Long.toString(request.expiry));
auditLog.recordEvent(new Event(Instant.now(), EventTag.SECRET_CREATE, user.getName(), request.name, extraInfo));
}
return response;
}
use of org.jooq.exception.DataAccessException in project keywhiz by square.
the class SecretDAO method createSecret.
@VisibleForTesting
public long createSecret(String name, String encryptedSecret, String hmac, String creator, Map<String, String> metadata, long expiry, String description, @Nullable String type, @Nullable Map<String, String> generationOptions) {
return dslContext.transactionResult(configuration -> {
SecretContentDAO secretContentDAO = secretContentDAOFactory.using(configuration);
SecretSeriesDAO secretSeriesDAO = secretSeriesDAOFactory.using(configuration);
Optional<SecretSeries> secretSeries = secretSeriesDAO.getSecretSeriesByName(name);
long secretId;
if (secretSeries.isPresent()) {
SecretSeries secretSeries1 = secretSeries.get();
if (secretSeries1.currentVersion().isPresent()) {
throw new DataAccessException(format("secret already present: %s", name));
}
secretId = secretSeries1.id();
secretSeriesDAO.updateSecretSeries(secretId, name, creator, description, type, generationOptions);
} else {
secretId = secretSeriesDAO.createSecretSeries(name, creator, description, type, generationOptions);
}
long secretContentId = secretContentDAO.createSecretContent(secretId, encryptedSecret, hmac, creator, metadata, expiry);
secretSeriesDAO.setCurrentVersion(secretId, secretContentId);
return secretId;
});
}
use of org.jooq.exception.DataAccessException in project keywhiz by square.
the class KeywhizTestRunner method createTest.
@Override
protected Object createTest() throws Exception {
// Reset database. Sometimes, the truncate command fails. I don't know why?
DSLContext jooqContext = injector.getInstance(DSLContext.class);
try {
jooqContext.truncate(Users.USERS).execute();
} catch (DataAccessException e) {
}
try {
jooqContext.truncate(SecretsContent.SECRETS_CONTENT).execute();
} catch (DataAccessException e) {
}
try {
jooqContext.truncate(Memberships.MEMBERSHIPS).execute();
} catch (DataAccessException e) {
}
try {
jooqContext.truncate(Accessgrants.ACCESSGRANTS).execute();
} catch (DataAccessException e) {
}
try {
jooqContext.truncate(Clients.CLIENTS).execute();
} catch (DataAccessException e) {
}
try {
jooqContext.truncate(Groups.GROUPS).execute();
} catch (DataAccessException e) {
}
try {
jooqContext.truncate(Secrets.SECRETS).execute();
} catch (DataAccessException e) {
}
Object object = injector.getInstance(getTestClass().getJavaClass());
MockitoAnnotations.initMocks(object);
return object;
}
use of org.jooq.exception.DataAccessException in project torodb by torodb.
the class AbstractWriteInterface method insertDocPartData.
@Override
public void insertDocPartData(DSLContext dsl, String schemaName, DocPartData docPartData) throws UserException {
Iterator<DocPartRow> docPartRowIterator = docPartData.iterator();
if (!docPartRowIterator.hasNext()) {
return;
}
try {
MetaDocPart metaDocPart = docPartData.getMetaDocPart();
Iterator<MetaScalar> metaScalarIterator = docPartData.orderedMetaScalarIterator();
Iterator<MetaField> metaFieldIterator = docPartData.orderedMetaFieldIterator();
standardInsertDocPartData(dsl, schemaName, docPartData, metaDocPart, metaScalarIterator, metaFieldIterator, docPartRowIterator);
} catch (DataAccessException ex) {
throw errorHandler.handleUserException(Context.INSERT, ex);
}
}
Aggregations