use of org.finra.herd.model.api.xml.JdbcExecutionResponse in project herd by FINRAOS.
the class ExecuteJdbc method executeSync.
/**
* Executes the task synchronously. overrideActivitiId is provided to customize the name of the variable to set during the execution of the task. This is
* important when the task is running asynchronously because execution.getCurrentActivitiId() will report the ID the task that the workflow is currently in,
* and not the ID of the task that was originally executed.
*
* @param execution The execution.
* @param overrideActivitiId Optionally overrides the task id which to use to generate variable names.
* @param jdbcExecutionRequest The request.
*
* @throws Exception When any exception occurs during the execution of the task.
*/
private void executeSync(DelegateExecution execution, String overrideActivitiId, JdbcExecutionRequest jdbcExecutionRequest) throws Exception {
String executionId = execution.getId();
String activitiId = execution.getCurrentActivityId();
if (overrideActivitiId != null) {
activitiId = overrideActivitiId;
}
// Execute the request. May throw exception here in case of validation or system errors.
// Thrown exceptions should be caught by parent implementation.
JdbcExecutionResponse jdbcExecutionResponse = jdbcService.executeJdbc(jdbcExecutionRequest);
// Set the response
setJsonResponseAsWorkflowVariable(jdbcExecutionResponse, executionId, activitiId);
/*
* Special handling of error condition.
* Any one of the requested SQL statements could've failed without throwing an exception.
* If any of the statements are in ERROR, throw an exception.
* This ensures that the workflow response variable is still available to the users.
*/
for (JdbcStatement jdbcStatement : jdbcExecutionResponse.getStatements()) {
if (JdbcStatementStatus.ERROR.equals(jdbcStatement.getStatus())) {
throw new IllegalArgumentException("There are failed executions. See JSON response for details.");
}
}
}
use of org.finra.herd.model.api.xml.JdbcExecutionResponse in project herd by FINRAOS.
the class JdbcServiceImpl method executeJdbcImpl.
/**
* This implementation uses a {@link DriverManagerDataSource}. Uses existing Spring ORM transaction.
*
* @param jdbcExecutionRequest JDBC execution request
*
* @return {@link JdbcExecutionResponse}
*/
protected JdbcExecutionResponse executeJdbcImpl(JdbcExecutionRequest jdbcExecutionRequest) {
validateJdbcExecutionRequest(jdbcExecutionRequest);
// Optionally, get properties from S3
S3PropertiesLocation s3PropertiesLocation = jdbcExecutionRequest.getS3PropertiesLocation();
Map<String, Object> variables = getVariablesFromS3(s3PropertiesLocation);
// Create data source
DataSource dataSource = createDataSource(jdbcExecutionRequest.getConnection(), variables);
// Execute the requested statements
List<JdbcStatement> requestJdbcStatements = jdbcExecutionRequest.getStatements();
List<JdbcStatement> responseJdbcStatements = executeStatements(requestJdbcStatements, dataSource, variables);
// Create and return the execution result
return new JdbcExecutionResponse(null, responseJdbcStatements);
}
use of org.finra.herd.model.api.xml.JdbcExecutionResponse in project herd by FINRAOS.
the class JdbcServiceTest method testExecuteJdbcSensitiveDataIsMaskedInErrorMessage.
/**
* Some JDBC exception messages echoes back parts of the SQL statement. This is problem for security if some of the variables were replaced, and may
* accidentally expose secret information in the response error message. The application should mask any values given in the properties which exist in the
* exception message.
* <p/>
* This test will use a SQL that will throw an exception, and the exception message is known. Then asserts that the value has been replaced with a mask in
* the response error message.
*/
@Test
public void testExecuteJdbcSensitiveDataIsMaskedInErrorMessage() {
String s3BucketName = "test_bucket";
String s3ObjectKey = "test_key";
String content = "foo=DataIntegrityViolationException";
putS3Object(s3BucketName, s3ObjectKey, content);
JdbcExecutionRequest jdbcExecutionRequest = jdbcServiceTestHelper.createDefaultUpdateJdbcExecutionRequest();
jdbcExecutionRequest.getStatements().get(0).setSql(MockJdbcOperations.CASE_2_SQL);
jdbcExecutionRequest.setS3PropertiesLocation(new S3PropertiesLocation(s3BucketName, s3ObjectKey));
JdbcExecutionResponse jdbcExecutionResponse = jdbcService.executeJdbc(jdbcExecutionRequest);
Assert.assertEquals("jdbc execution response statement [0] error message", "java.sql.SQLException: test **** cause", jdbcExecutionResponse.getStatements().get(0).getErrorMessage());
}
use of org.finra.herd.model.api.xml.JdbcExecutionResponse in project herd by FINRAOS.
the class JdbcServiceTest method testExecuteJdbcStatementError.
/**
* Use case where 3 statements are requested to be executed, but the 2nd is erroneous. The first statement should result in SUCCESS. The second should
* result in ERROR, with appropriate result message. The third should result in SKIPPED with no result.
*/
@Test
public void testExecuteJdbcStatementError() {
// Create test request
JdbcExecutionRequest jdbcExecutionRequest = jdbcServiceTestHelper.createDefaultUpdateJdbcExecutionRequest();
// First statement already included
// Second statement uses case 2 which throws an error
jdbcExecutionRequest.getStatements().add(new JdbcStatement(JdbcStatementType.UPDATE, MockJdbcOperations.CASE_2_SQL, null, null, null, null, null));
jdbcExecutionRequest.getStatements().add(new JdbcStatement(JdbcStatementType.UPDATE, MockJdbcOperations.CASE_1_SQL, null, null, null, null, null));
// Execute
JdbcExecutionResponse jdbcExecutionResponse = jdbcService.executeJdbc(jdbcExecutionRequest);
// Assert results
Assert.assertNull("JDBC connection is null", jdbcExecutionResponse.getConnection());
Assert.assertEquals("JDBC statements size", jdbcExecutionRequest.getStatements().size(), jdbcExecutionResponse.getStatements().size());
{
JdbcStatement actualJdbcStatement = jdbcExecutionResponse.getStatements().get(0);
Assert.assertEquals("JDBC statement [0] status", JdbcStatementStatus.SUCCESS, actualJdbcStatement.getStatus());
}
{
JdbcStatement actualJdbcStatement = jdbcExecutionResponse.getStatements().get(1);
Assert.assertEquals("JDBC statement [1] status", JdbcStatementStatus.ERROR, actualJdbcStatement.getStatus());
Assert.assertNull("JDBC statement [1] result is not null", actualJdbcStatement.getResult());
Assert.assertEquals("JDBC statement [1] error message", "java.sql.SQLException: test DataIntegrityViolationException cause", actualJdbcStatement.getErrorMessage());
}
{
JdbcStatement actualJdbcStatement = jdbcExecutionResponse.getStatements().get(2);
Assert.assertEquals("JDBC statement [2] status", JdbcStatementStatus.SKIPPED, actualJdbcStatement.getStatus());
Assert.assertNull("JDBC statement [2] result is not null", actualJdbcStatement.getResult());
}
}
use of org.finra.herd.model.api.xml.JdbcExecutionResponse in project herd by FINRAOS.
the class JdbcServiceTest method testExecuteJdbcParamValidationStatementsMaximumStatementsSpecified.
/**
* Parameter validation, request statement list has less statements than configured maximum.
*/
@Test
public void testExecuteJdbcParamValidationStatementsMaximumStatementsSpecified() {
/*
* Update environment variable to use a maximum statement
*/
int maxStatements = 1;
try {
HashMap<String, Object> overrideMap = new HashMap<>();
overrideMap.put(ConfigurationValue.JDBC_MAX_STATEMENTS.getKey(), maxStatements);
modifyPropertySourceInEnvironment(overrideMap);
} catch (Exception e) {
throw new RuntimeException("Error modifying environment variable.", e);
}
/*
* Add 1 more statement to the JDBC request.
* The default request should already have 1 statement.
*/
JdbcExecutionRequest jdbcExecutionRequest = jdbcServiceTestHelper.createDefaultUpdateJdbcExecutionRequest();
try {
// Execute
JdbcExecutionResponse jdbcExecutionResponse = jdbcService.executeJdbc(jdbcExecutionRequest);
Assert.assertNotNull("jdbcExecutionResponse", jdbcExecutionResponse);
} finally {
try {
restorePropertySourceInEnvironment();
} catch (Exception e) {
throw new RuntimeException("Error restoring environment variables. Subsequent tests may be affected.", e);
}
}
}
Aggregations