Search in sources :

Example 21 with DlpServiceClient

use of com.google.cloud.dlp.v2.DlpServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class RiskAnalysis method numericalStatsAnalysis.

// [START dlp_numerical_stats]
/**
 * Calculate numerical statistics for a column in a BigQuery table using the DLP API.
 *
 * @param projectId The Google Cloud Platform project ID to run the API call under.
 * @param datasetId The BigQuery dataset to analyze.
 * @param tableId The BigQuery table to analyze.
 * @param columnName The name of the column to analyze, which must contain only numerical data.
 * @param topicId The name of the Pub/Sub topic to notify once the job completes
 * @param subscriptionId The name of the Pub/Sub subscription to use when listening for job
 *     completion status.
 */
private static void numericalStatsAnalysis(String projectId, String datasetId, String tableId, String columnName, String topicId, String subscriptionId) throws Exception {
    // Instantiates a client
    try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
        BigQueryTable bigQueryTable = BigQueryTable.newBuilder().setTableId(tableId).setDatasetId(datasetId).setProjectId(projectId).build();
        FieldId fieldId = FieldId.newBuilder().setName(columnName).build();
        NumericalStatsConfig numericalStatsConfig = NumericalStatsConfig.newBuilder().setField(fieldId).build();
        PrivacyMetric privacyMetric = PrivacyMetric.newBuilder().setNumericalStatsConfig(numericalStatsConfig).build();
        String topicName = String.format("projects/%s/topics/%s", projectId, topicId);
        PublishToPubSub publishToPubSub = PublishToPubSub.newBuilder().setTopic(topicName).build();
        // Create action to publish job status notifications over Google Cloud Pub/Sub
        Action action = Action.newBuilder().setPubSub(publishToPubSub).build();
        RiskAnalysisJobConfig riskAnalysisJobConfig = RiskAnalysisJobConfig.newBuilder().setSourceTable(bigQueryTable).setPrivacyMetric(privacyMetric).addActions(action).build();
        CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setRiskJob(riskAnalysisJobConfig).build();
        DlpJob dlpJob = dlpServiceClient.createDlpJob(createDlpJobRequest);
        String dlpJobName = dlpJob.getName();
        final SettableApiFuture<Boolean> done = SettableApiFuture.create();
        // Set up a Pub/Sub subscriber to listen on the job completion status
        Subscriber subscriber = Subscriber.newBuilder(ProjectSubscriptionName.newBuilder().setProject(projectId).setSubscription(subscriptionId).build(), (pubsubMessage, ackReplyConsumer) -> {
            if (pubsubMessage.getAttributesCount() > 0 && pubsubMessage.getAttributesMap().get("DlpJobName").equals(dlpJobName)) {
                // notify job completion
                done.set(true);
                ackReplyConsumer.ack();
            }
        }).build();
        subscriber.startAsync();
        // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
        try {
            done.get(1, TimeUnit.MINUTES);
            // Wait for the job to become available
            Thread.sleep(500);
        } catch (TimeoutException e) {
            System.out.println("Unable to verify job completion.");
        }
        // Retrieve completed job status
        DlpJob completedJob = dlpServiceClient.getDlpJob(GetDlpJobRequest.newBuilder().setName(dlpJobName).build());
        System.out.println("Job status: " + completedJob.getState());
        AnalyzeDataSourceRiskDetails riskDetails = completedJob.getRiskDetails();
        AnalyzeDataSourceRiskDetails.NumericalStatsResult result = riskDetails.getNumericalStatsResult();
        System.out.printf("Value range : [%.3f, %.3f]\n", result.getMinValue().getFloatValue(), result.getMaxValue().getFloatValue());
        int percent = 1;
        Double lastValue = null;
        for (Value quantileValue : result.getQuantileValuesList()) {
            Double currentValue = quantileValue.getFloatValue();
            if (lastValue == null || !lastValue.equals(currentValue)) {
                System.out.printf("Value at %s %% quantile : %.3f", percent, currentValue);
            }
            lastValue = currentValue;
        }
    } catch (Exception e) {
        System.out.println("Error in categoricalStatsAnalysis: " + e.getMessage());
    }
}
Also used : Arrays(java.util.Arrays) TimeoutException(java.util.concurrent.TimeoutException) Subscriber(com.google.cloud.pubsub.v1.Subscriber) DefaultParser(org.apache.commons.cli.DefaultParser) KMapEstimationHistogramBucket(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KMapEstimationResult.KMapEstimationHistogramBucket) LDiversityEquivalenceClass(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.LDiversityResult.LDiversityEquivalenceClass) ValueFrequency(com.google.privacy.dlp.v2.ValueFrequency) LDiversityConfig(com.google.privacy.dlp.v2.PrivacyMetric.LDiversityConfig) NumericalStatsConfig(com.google.privacy.dlp.v2.PrivacyMetric.NumericalStatsConfig) Action(com.google.privacy.dlp.v2.Action) KMapEstimationConfig(com.google.privacy.dlp.v2.PrivacyMetric.KMapEstimationConfig) CategoricalStatsHistogramBucket(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.CategoricalStatsResult.CategoricalStatsHistogramBucket) KAnonymityEquivalenceClass(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KAnonymityResult.KAnonymityEquivalenceClass) Value(com.google.privacy.dlp.v2.Value) TaggedField(com.google.privacy.dlp.v2.PrivacyMetric.KMapEstimationConfig.TaggedField) RiskAnalysisJobConfig(com.google.privacy.dlp.v2.RiskAnalysisJobConfig) Collectors(java.util.stream.Collectors) SettableApiFuture(com.google.api.core.SettableApiFuture) List(java.util.List) KAnonymityResult(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KAnonymityResult) ParseException(org.apache.commons.cli.ParseException) BigQueryTable(com.google.privacy.dlp.v2.BigQueryTable) ProjectSubscriptionName(com.google.pubsub.v1.ProjectSubscriptionName) AnalyzeDataSourceRiskDetails(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails) Options(org.apache.commons.cli.Options) HelpFormatter(org.apache.commons.cli.HelpFormatter) CategoricalStatsConfig(com.google.privacy.dlp.v2.PrivacyMetric.CategoricalStatsConfig) ArrayList(java.util.ArrayList) ServiceOptions(com.google.cloud.ServiceOptions) CommandLine(org.apache.commons.cli.CommandLine) FieldId(com.google.privacy.dlp.v2.FieldId) ProjectTopicName(com.google.pubsub.v1.ProjectTopicName) Option(org.apache.commons.cli.Option) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) Iterator(java.util.Iterator) CreateDlpJobRequest(com.google.privacy.dlp.v2.CreateDlpJobRequest) CommandLineParser(org.apache.commons.cli.CommandLineParser) KMapEstimationResult(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KMapEstimationResult) KMapEstimationQuasiIdValues(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KMapEstimationResult.KMapEstimationQuasiIdValues) InfoType(com.google.privacy.dlp.v2.InfoType) LDiversityResult(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.LDiversityResult) KAnonymityConfig(com.google.privacy.dlp.v2.PrivacyMetric.KAnonymityConfig) TimeUnit(java.util.concurrent.TimeUnit) PublishToPubSub(com.google.privacy.dlp.v2.Action.PublishToPubSub) ProjectName(com.google.privacy.dlp.v2.ProjectName) GetDlpJobRequest(com.google.privacy.dlp.v2.GetDlpJobRequest) LDiversityHistogramBucket(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.LDiversityResult.LDiversityHistogramBucket) KAnonymityHistogramBucket(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails.KAnonymityResult.KAnonymityHistogramBucket) PrivacyMetric(com.google.privacy.dlp.v2.PrivacyMetric) OptionGroup(org.apache.commons.cli.OptionGroup) DlpJob(com.google.privacy.dlp.v2.DlpJob) Collections(java.util.Collections) Action(com.google.privacy.dlp.v2.Action) RiskAnalysisJobConfig(com.google.privacy.dlp.v2.RiskAnalysisJobConfig) PrivacyMetric(com.google.privacy.dlp.v2.PrivacyMetric) CreateDlpJobRequest(com.google.privacy.dlp.v2.CreateDlpJobRequest) TimeoutException(java.util.concurrent.TimeoutException) ParseException(org.apache.commons.cli.ParseException) PublishToPubSub(com.google.privacy.dlp.v2.Action.PublishToPubSub) Subscriber(com.google.cloud.pubsub.v1.Subscriber) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) BigQueryTable(com.google.privacy.dlp.v2.BigQueryTable) FieldId(com.google.privacy.dlp.v2.FieldId) Value(com.google.privacy.dlp.v2.Value) DlpJob(com.google.privacy.dlp.v2.DlpJob) AnalyzeDataSourceRiskDetails(com.google.privacy.dlp.v2.AnalyzeDataSourceRiskDetails) NumericalStatsConfig(com.google.privacy.dlp.v2.PrivacyMetric.NumericalStatsConfig) TimeoutException(java.util.concurrent.TimeoutException)

Example 22 with DlpServiceClient

use of com.google.cloud.dlp.v2.DlpServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class Templates method deleteInspectTemplate.

// [END dlp_list_inspect_templates]
// [START dlp_delete_inspect_template]
/**
 * Delete the DLP inspection configuration template with the specified name.
 *
 * @param projectId Google Cloud Project ID
 * @param templateId Template ID to be deleted
 */
private static void deleteInspectTemplate(String projectId, String templateId) {
    // construct the template name to be deleted
    String templateName = String.format("projects/%s/inspectTemplates/%s", projectId, templateId);
    // instantiate the client
    try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
        // create delete template request
        DeleteInspectTemplateRequest request = DeleteInspectTemplateRequest.newBuilder().setName(templateName).build();
        dlpServiceClient.deleteInspectTemplate(request);
        System.out.printf("Deleted template: %s\n", templateName);
    } catch (Exception e) {
        System.err.printf("Error deleting template: %s\n", templateName);
    }
}
Also used : DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) DeleteInspectTemplateRequest(com.google.privacy.dlp.v2.DeleteInspectTemplateRequest) ParseException(org.apache.commons.cli.ParseException)

Example 23 with DlpServiceClient

use of com.google.cloud.dlp.v2.DlpServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class Triggers method createTrigger.

// [START dlp_create_trigger]
/**
 * Schedule a DLP inspection trigger for a GCS location.
 *
 * @param triggerId (Optional) name of the trigger to be created
 * @param displayName (Optional) display name for the trigger to be created
 * @param description (Optional) description for the trigger to be created
 * @param scanPeriod How often to wait between scans, in days (minimum = 1 day)
 * @param infoTypes infoTypes of information to match eg. InfoType.PHONE_NUMBER,
 *     InfoType.EMAIL_ADDRESS
 * @param minLikelihood minimum likelihood required before returning a match
 * @param maxFindings maximum number of findings to report per request (0 = server maximum)
 * @param projectId The project ID to run the API call under
 */
private static void createTrigger(String triggerId, String displayName, String description, String bucketName, String fileName, int scanPeriod, List<InfoType> infoTypes, Likelihood minLikelihood, int maxFindings, String projectId) throws Exception {
    // instantiate a client
    DlpServiceClient dlpServiceClient = DlpServiceClient.create();
    try {
        CloudStorageOptions cloudStorageOptions = CloudStorageOptions.newBuilder().setFileSet(CloudStorageOptions.FileSet.newBuilder().setUrl("gs://" + bucketName + "/" + fileName)).build();
        StorageConfig storageConfig = StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).build();
        InspectConfig.FindingLimits findingLimits = InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
        InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).build();
        InspectJobConfig inspectJobConfig = InspectJobConfig.newBuilder().setInspectConfig(inspectConfig).setStorageConfig(storageConfig).build();
        // Schedule scan of GCS bucket every scanPeriod number of days (minimum = 1 day)
        Duration duration = Duration.newBuilder().setSeconds(scanPeriod * 24 * 3600).build();
        Schedule schedule = Schedule.newBuilder().setRecurrencePeriodDuration(duration).build();
        JobTrigger.Trigger trigger = JobTrigger.Trigger.newBuilder().setSchedule(schedule).build();
        JobTrigger jobTrigger = JobTrigger.newBuilder().setInspectJob(inspectJobConfig).setName(triggerId).setDisplayName(displayName).setDescription(description).setStatus(JobTrigger.Status.HEALTHY).addTriggers(trigger).build();
        // Create scan request
        CreateJobTriggerRequest createJobTriggerRequest = CreateJobTriggerRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setJobTrigger(jobTrigger).build();
        JobTrigger createdJobTrigger = dlpServiceClient.createJobTrigger(createJobTriggerRequest);
        System.out.println("Created Trigger: " + createdJobTrigger.getName());
    } catch (Exception e) {
        System.out.println("Error creating trigger: " + e.getMessage());
    }
}
Also used : StorageConfig(com.google.privacy.dlp.v2.StorageConfig) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) Schedule(com.google.privacy.dlp.v2.Schedule) CloudStorageOptions(com.google.privacy.dlp.v2.CloudStorageOptions) Duration(com.google.protobuf.Duration) InspectConfig(com.google.privacy.dlp.v2.InspectConfig) JobTrigger(com.google.privacy.dlp.v2.JobTrigger) ParseException(org.apache.commons.cli.ParseException) InspectJobConfig(com.google.privacy.dlp.v2.InspectJobConfig) CreateJobTriggerRequest(com.google.privacy.dlp.v2.CreateJobTriggerRequest)

Example 24 with DlpServiceClient

use of com.google.cloud.dlp.v2.DlpServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class Triggers method deleteTrigger.

// [END dlp_list_triggers]
// [START dlp_delete_trigger]
/**
 * Delete a DLP trigger in a project.
 *
 * @param projectId The project ID to run the API call under.
 * @param triggerId Trigger ID
 */
private static void deleteTrigger(String projectId, String triggerId) {
    ProjectJobTriggerName triggerName = ProjectJobTriggerName.of(projectId, triggerId);
    try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
        DeleteJobTriggerRequest deleteJobTriggerRequest = DeleteJobTriggerRequest.newBuilder().setName(triggerName.toString()).build();
        dlpServiceClient.deleteJobTrigger(deleteJobTriggerRequest);
        System.out.println("Trigger deleted: " + triggerName.toString());
    } catch (Exception e) {
        System.out.println("Error deleting trigger :" + e.getMessage());
    }
}
Also used : ProjectJobTriggerName(com.google.privacy.dlp.v2.ProjectJobTriggerName) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) DeleteJobTriggerRequest(com.google.privacy.dlp.v2.DeleteJobTriggerRequest) ParseException(org.apache.commons.cli.ParseException)

Example 25 with DlpServiceClient

use of com.google.cloud.dlp.v2.DlpServiceClient in project java-docs-samples by GoogleCloudPlatform.

the class Triggers method listTriggers.

// [END dlp_create_trigger]
// [START dlp_list_triggers]
/**
 * List all DLP triggers for a given project.
 *
 * @param projectId The project ID to run the API call under.
 */
private static void listTriggers(String projectId) {
    // Instantiates a client
    try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
        ListJobTriggersRequest listJobTriggersRequest = ListJobTriggersRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).build();
        DlpServiceClient.ListJobTriggersPagedResponse response = dlpServiceClient.listJobTriggers(listJobTriggersRequest);
        response.getPage().getValues().forEach(trigger -> {
            System.out.println("Trigger: " + trigger.getName());
            System.out.println("\tCreated: " + trigger.getCreateTime());
            System.out.println("\tUpdated: " + trigger.getUpdateTime());
            if (trigger.getDisplayName() != null) {
                System.out.println("\tDisplay name: " + trigger.getDisplayName());
            }
            if (trigger.getDescription() != null) {
                System.out.println("\tDescription: " + trigger.getDescription());
            }
            System.out.println("\tStatus: " + trigger.getStatus());
            System.out.println("\tError count: " + trigger.getErrorsCount());
        });
    } catch (Exception e) {
        System.out.println("Error listing triggers :" + e.getMessage());
    }
}
Also used : ListJobTriggersRequest(com.google.privacy.dlp.v2.ListJobTriggersRequest) DlpServiceClient(com.google.cloud.dlp.v2.DlpServiceClient) ParseException(org.apache.commons.cli.ParseException)

Aggregations

DlpServiceClient (com.google.cloud.dlp.v2.DlpServiceClient)25 ParseException (org.apache.commons.cli.ParseException)22 InfoType (com.google.privacy.dlp.v2.InfoType)13 InspectConfig (com.google.privacy.dlp.v2.InspectConfig)12 ServiceOptions (com.google.cloud.ServiceOptions)10 ContentItem (com.google.privacy.dlp.v2.ContentItem)10 ProjectName (com.google.privacy.dlp.v2.ProjectName)10 ByteString (com.google.protobuf.ByteString)10 ArrayList (java.util.ArrayList)10 List (java.util.List)10 CommandLine (org.apache.commons.cli.CommandLine)10 CommandLineParser (org.apache.commons.cli.CommandLineParser)10 DefaultParser (org.apache.commons.cli.DefaultParser)10 HelpFormatter (org.apache.commons.cli.HelpFormatter)10 Option (org.apache.commons.cli.Option)10 Options (org.apache.commons.cli.Options)10 DlpJob (com.google.privacy.dlp.v2.DlpJob)9 OptionGroup (org.apache.commons.cli.OptionGroup)9 SettableApiFuture (com.google.api.core.SettableApiFuture)8 Subscriber (com.google.cloud.pubsub.v1.Subscriber)8