Search in sources :

Example 1 with TablePolicyTags

use of com.google.cloud.pso.bq_pii_classifier.entities.TablePolicyTags in project bq-pii-classifier by GoogleCloudPlatform.

the class FindingsReaderAutoDlp method getFieldsToPolicyTagsMap.

/**
 * Look for DLP results by a tableSpec. Returns a map of fields to policy tags or null if DLP
 * doesn't have findings
 *
 * @param inspectedTableSpec: "project.dataset.table"
 * @return
 * @throws InterruptedException
 * @throws NonRetryableApplicationException
 * @throws IOException
 */
public TablePolicyTags getFieldsToPolicyTagsMap(String inspectedTableSpec) throws InterruptedException, NonRetryableApplicationException, IOException {
    String formattedQuery = generateQuery(inspectedTableSpec);
    // Create a job ID so that we can safely retry.
    Job queryJob = bqService.submitJob(formattedQuery);
    TableResult result = bqService.waitAndGetJobResults(queryJob);
    // Construct a mapping between field names and DLP infotypes
    Map<String, String> fieldsToPolicyTagMap = new HashMap<>();
    for (FieldValueList row : result.iterateAll()) {
        if (row.get("field_name").isNull()) {
            throw new NonRetryableApplicationException("getFieldsToPolicyTagsMap query returned rows with null field_name");
        }
        String column_name = row.get("field_name").getStringValue();
        if (row.get("info_type").isNull()) {
            throw new NonRetryableApplicationException(String.format("getFieldsToPolicyTagsMap query returned rows with null info_type for column '%s'", column_name));
        }
        String info_type = row.get("info_type").getStringValue();
        if (row.get("policy_tag").isNull()) {
            throw new NonRetryableApplicationException(String.format("getFieldsToPolicyTagsMap query returned rows with null policy_tag for column '%s' of info_type '%s'. Checkout the classification taxonomy configuration and the DLP inspection template. All InfoTypes defined in the inspection template must have corresponding entries in the classification taxonomies.", column_name, info_type));
        }
        String policy_tag = row.get("policy_tag").getStringValue();
        fieldsToPolicyTagMap.put(column_name, policy_tag);
    }
    if (fieldsToPolicyTagMap.isEmpty())
        return null;
    else
        return new TablePolicyTags(TableSpec.fromSqlString(inspectedTableSpec), fieldsToPolicyTagMap);
}
Also used : TableResult(com.google.cloud.bigquery.TableResult) TablePolicyTags(com.google.cloud.pso.bq_pii_classifier.entities.TablePolicyTags) HashMap(java.util.HashMap) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException) FieldValueList(com.google.cloud.bigquery.FieldValueList) Job(com.google.cloud.bigquery.Job)

Example 2 with TablePolicyTags

use of com.google.cloud.pso.bq_pii_classifier.entities.TablePolicyTags in project bq-pii-classifier by GoogleCloudPlatform.

the class FindingsReaderStandardDlp method getFieldsToPolicyTagsMap.

/**
 * Look for DLP results by a tableSpec. Returns a map of fields to policy tags or null if DLP
 * doesn't have findings
 *
 * @param dlpJobName: "projects/<PROJECT>/locations/<GCP REGION>/dlpJobs/<JOB ID>"
 * @return
 * @throws InterruptedException
 * @throws NonRetryableApplicationException
 * @throws IOException
 */
public TablePolicyTags getFieldsToPolicyTagsMap(String dlpJobName) throws InterruptedException, NonRetryableApplicationException, IOException {
    String formattedQuery = generateQuery(dlpJobName);
    // Create a job ID so that we can safely retry.
    Job queryJob = bqService.submitJob(formattedQuery);
    TableResult result = bqService.waitAndGetJobResults(queryJob);
    // Construct a mapping between field names and DLP infotypes
    Map<String, String> fieldsToPolicyTagMap = new HashMap<>();
    String tableSpecStr = "";
    for (FieldValueList row : result.iterateAll()) {
        if (row.get("field_name").isNull()) {
            throw new NonRetryableApplicationException("getFieldsToPolicyTagsMap query returned rows with null field_name");
        }
        String column_name = row.get("field_name").getStringValue();
        if (row.get("info_type").isNull()) {
            throw new NonRetryableApplicationException(String.format("getFieldsToPolicyTagsMap query returned rows with null info_type for column '%s'", column_name));
        }
        String info_type = row.get("info_type").getStringValue();
        if (row.get("policy_tag").isNull()) {
            throw new NonRetryableApplicationException(String.format("getFieldsToPolicyTagsMap query returned rows with null policy_tag for column '%s' of info_type '%s'. Checkout the classification taxonomy configuration and the DLP inspection template. All InfoTypes defined in the inspection template must have corresponding entries in the classification taxonomies.", column_name, info_type));
        }
        String policy_tag = row.get("policy_tag").getStringValue();
        if (row.get("table_spec").isNull()) {
            throw new NonRetryableApplicationException("getFieldsToPolicyTagsMap query returned rows with null table_spec");
        }
        tableSpecStr = row.get("table_spec").getStringValue();
        fieldsToPolicyTagMap.put(column_name, policy_tag);
    }
    if (fieldsToPolicyTagMap.isEmpty())
        return null;
    else
        return new TablePolicyTags(TableSpec.fromSqlString(tableSpecStr), fieldsToPolicyTagMap);
}
Also used : TableResult(com.google.cloud.bigquery.TableResult) TablePolicyTags(com.google.cloud.pso.bq_pii_classifier.entities.TablePolicyTags) HashMap(java.util.HashMap) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException) FieldValueList(com.google.cloud.bigquery.FieldValueList) Job(com.google.cloud.bigquery.Job)

Aggregations

FieldValueList (com.google.cloud.bigquery.FieldValueList)2 Job (com.google.cloud.bigquery.Job)2 TableResult (com.google.cloud.bigquery.TableResult)2 NonRetryableApplicationException (com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException)2 TablePolicyTags (com.google.cloud.pso.bq_pii_classifier.entities.TablePolicyTags)2 HashMap (java.util.HashMap)2