Search in sources :

Example 1 with JdbcExecutionResponse

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.");
        }
    }
}
Also used : JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement) JdbcExecutionResponse(org.finra.herd.model.api.xml.JdbcExecutionResponse)

Example 2 with JdbcExecutionResponse

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);
}
Also used : S3PropertiesLocation(org.finra.herd.model.api.xml.S3PropertiesLocation) JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement) JdbcExecutionResponse(org.finra.herd.model.api.xml.JdbcExecutionResponse) DataSource(javax.sql.DataSource) DriverManagerDataSource(org.springframework.jdbc.datasource.DriverManagerDataSource)

Example 3 with JdbcExecutionResponse

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());
}
Also used : S3PropertiesLocation(org.finra.herd.model.api.xml.S3PropertiesLocation) JdbcExecutionRequest(org.finra.herd.model.api.xml.JdbcExecutionRequest) JdbcExecutionResponse(org.finra.herd.model.api.xml.JdbcExecutionResponse) Test(org.junit.Test)

Example 4 with JdbcExecutionResponse

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());
    }
}
Also used : JdbcStatement(org.finra.herd.model.api.xml.JdbcStatement) JdbcExecutionRequest(org.finra.herd.model.api.xml.JdbcExecutionRequest) JdbcExecutionResponse(org.finra.herd.model.api.xml.JdbcExecutionResponse) Test(org.junit.Test)

Example 5 with JdbcExecutionResponse

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);
        }
    }
}
Also used : HashMap(java.util.HashMap) JdbcExecutionRequest(org.finra.herd.model.api.xml.JdbcExecutionRequest) JdbcExecutionResponse(org.finra.herd.model.api.xml.JdbcExecutionResponse) Test(org.junit.Test)

Aggregations

JdbcExecutionResponse (org.finra.herd.model.api.xml.JdbcExecutionResponse)14 JdbcExecutionRequest (org.finra.herd.model.api.xml.JdbcExecutionRequest)12 Test (org.junit.Test)12 JdbcStatement (org.finra.herd.model.api.xml.JdbcStatement)9 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 Parameter (org.finra.herd.model.api.xml.Parameter)3 S3PropertiesLocation (org.finra.herd.model.api.xml.S3PropertiesLocation)3 FieldExtension (org.activiti.bpmn.model.FieldExtension)2 DataSource (javax.sql.DataSource)1 JdbcStatementResultSetRow (org.finra.herd.model.api.xml.JdbcStatementResultSetRow)1 Job (org.finra.herd.model.api.xml.Job)1 JobCreateRequest (org.finra.herd.model.api.xml.JobCreateRequest)1 DriverManagerDataSource (org.springframework.jdbc.datasource.DriverManagerDataSource)1 Transactional (org.springframework.transaction.annotation.Transactional)1