com.google.cloud.dlp.v2.DlpServiceClient Java Examples

The following examples show how to use com.google.cloud.dlp.v2.DlpServiceClient. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JobsDelete.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public static void deleteJobs(String projectId, String jobId) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {

    // Construct the complete job name from the projectId and jobId
    DlpJobName jobName = DlpJobName.of(projectId, jobId);

    // Construct the job deletion request to be sent by the client.
    DeleteDlpJobRequest deleteDlpJobRequest =
        DeleteDlpJobRequest.newBuilder().setName(jobName.toString()).build();

    // Send the job deletion request
    dlpServiceClient.deleteDlpJob(deleteDlpJobRequest);
    System.out.println("Job deleted successfully.");
  }
}
 
Example #2
Source File: TemplatesDelete.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public static void deleteInspectTemplate(String projectId, String templateId) throws IOException {
  // Construct the template name to be deleted
  String templateName = String.format("projects/%s/inspectTemplates/%s", projectId, templateId);

  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {

    // Create delete template request to be sent by the client
    DeleteInspectTemplateRequest request =
        DeleteInspectTemplateRequest.newBuilder().setName(templateName).build();

    // Send the request with the client
    dlpServiceClient.deleteInspectTemplate(request);
    System.out.printf("Deleted template: %s\n", templateName);
  }
}
 
Example #3
Source File: TemplatesTests.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
private static InspectTemplate createTemplate() throws IOException {
  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
    List<InfoType> infoTypes =
        Stream.of("PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER")
            .map(it -> InfoType.newBuilder().setName(it).build())
            .collect(Collectors.toList());

    InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).build();

    InspectTemplate inspectTemplate =
        InspectTemplate.newBuilder().setInspectConfig(inspectConfig).build();

    CreateInspectTemplateRequest createInspectTemplateRequest =
        CreateInspectTemplateRequest.newBuilder()
            .setParent(LocationName.of(PROJECT_ID, "global").toString())
            .setInspectTemplate(inspectTemplate)
            .build();

    return dlpServiceClient.createInspectTemplate(createInspectTemplateRequest);
  }
}
 
Example #4
Source File: TextStreamingPipeline.java    From dlp-dataflow-deidentification with Apache License 2.0 6 votes vote down vote up
@ProcessElement
public void processElement(ProcessContext c) throws IOException {

  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {

    ContentItem contentItem = ContentItem.newBuilder().setValue(c.element()).build();

    DeidentifyContentRequest request =
        DeidentifyContentRequest.newBuilder()
            .setParent(ProjectName.of(this.projectId).toString())
            .setDeidentifyTemplateName(this.deIdentifyTemplateName.get())
            .setInspectTemplateName(this.inspectTemplateName.get())
            .setItem(contentItem)
            .build();

    DeidentifyContentResponse response = dlpServiceClient.deidentifyContent(request);

    String encryptedData = response.getItem().getValue();
    LOG.info(
        "Successfully tokenized request size: " + request.toByteString().size() + " bytes");
    c.output(encryptedData);
  }
}
 
Example #5
Source File: JobsGet.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public static void getJobs(String projectId, String jobId) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {

    // Construct the complete job name from the projectId and jobId
    DlpJobName jobName = DlpJobName.of(projectId, jobId);

    // Construct the get job request to be sent by the client.
    GetDlpJobRequest getDlpJobRequest =
        GetDlpJobRequest.newBuilder().setName(jobName.toString()).build();

    // Send the get job request
    dlpServiceClient.getDlpJob(getDlpJobRequest);
    System.out.println("Job got successfully.");
  }
}
 
Example #6
Source File: JobsList.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public static void listJobs(String projectId) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {

    // Construct the request to be sent by the client.
    // For more info on filters and job types,
    // see https://cloud.google.com/dlp/docs/reference/rest/v2/projects.dlpJobs/list
    ListDlpJobsRequest listDlpJobsRequest =
        ListDlpJobsRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setFilter("state=DONE")
            .setType(DlpJobType.valueOf("INSPECT_JOB"))
            .build();

    // Send the request to list jobs and process the response
    DlpServiceClient.ListDlpJobsPagedResponse response =
        dlpServiceClient.listDlpJobs(listDlpJobsRequest);

    System.out.println("DLP jobs found:");
    for (DlpJob dlpJob : response.getPage().getValues()) {
      System.out.println(dlpJob.getName() + " -- " + dlpJob.getState());
    }
  }
}
 
Example #7
Source File: DLPDeidentifyText.java    From beam with Apache License 2.0 6 votes vote down vote up
@Setup
public void setup() throws IOException {
  requestBuilder =
      DeidentifyContentRequest.newBuilder().setParent(ProjectName.of(projectId).toString());
  if (inspectTemplateName != null) {
    requestBuilder.setInspectTemplateName(inspectTemplateName);
  }
  if (inspectConfig != null) {
    requestBuilder.setInspectConfig(inspectConfig);
  }
  if (deidentifyConfig != null) {
    requestBuilder.setDeidentifyConfig(deidentifyConfig);
  }
  if (deidentifyTemplateName != null) {
    requestBuilder.setDeidentifyTemplateName(deidentifyTemplateName);
  }
  dlpServiceClient = DlpServiceClient.create();
}
 
Example #8
Source File: DLPReidentifyText.java    From beam with Apache License 2.0 6 votes vote down vote up
@Setup
public void setup() throws IOException {
  requestBuilder =
      ReidentifyContentRequest.newBuilder().setParent(ProjectName.of(projectId).toString());
  if (inspectTemplateName != null) {
    requestBuilder.setInspectTemplateName(inspectTemplateName);
  }
  if (inspectConfig != null) {
    requestBuilder.setInspectConfig(inspectConfig);
  }
  if (reidentifyConfig != null) {
    requestBuilder.setReidentifyConfig(reidentifyConfig);
  }
  if (reidentifyTemplateName != null) {
    requestBuilder.setReidentifyTemplateName(reidentifyTemplateName);
  }
  dlpServiceClient = DlpServiceClient.create();
}
 
Example #9
Source File: JobsTests.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetJobs() throws Exception {
  // Create a job with a unique UUID to be gotten
  String jobId = UUID.randomUUID().toString();
  DlpJob createdDlpJob = createJob(jobId);

  // Get the job with the specified ID
  JobsGet.getJobs(PROJECT_ID, "i-" + jobId);
  String output = bout.toString();
  assertThat(output).contains("Job got successfully.");

  // Delete the created Dlp Job
  String dlpJobName = createdDlpJob.getName();
  DeleteDlpJobRequest deleteDlpJobRequest =
      DeleteDlpJobRequest.newBuilder().setName(dlpJobName).build();
  try (DlpServiceClient client = DlpServiceClient.create()) {
    client.deleteDlpJob(deleteDlpJobRequest);
  }
}
 
Example #10
Source File: JobsTests.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
private static DlpJob createJob(String jobId) throws IOException {
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    FileSet fileSet = FileSet.newBuilder().setUrl(GCS_PATH).build();
    CloudStorageOptions cloudStorageOptions =
        CloudStorageOptions.newBuilder().setFileSet(fileSet).build();
    StorageConfig storageConfig =
        StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).build();

    InspectJobConfig inspectJobConfig =
        InspectJobConfig.newBuilder()
            .setStorageConfig(storageConfig)
            .setInspectConfig(InspectConfig.newBuilder().build())
            .build();

    CreateDlpJobRequest createDlpJobRequest =
        CreateDlpJobRequest.newBuilder()
            .setParent(LocationName.of(PROJECT_ID, "global").toString())
            .setInspectJob(inspectJobConfig)
            .setJobId(jobId)
            .build();

    return dlp.createDlpJob(createDlpJobRequest);
  }
}
 
Example #11
Source File: TriggersDelete.java    From java-docs-samples with Apache License 2.0 6 votes vote down vote up
public static void deleteTrigger(String projectId, String triggerId) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {

    // Get the full trigger name from the given triggerId and ProjectId
    ProjectJobTriggerName triggerName = ProjectJobTriggerName.of(projectId, triggerId);

    // Construct the trigger deletion request to be sent by the client
    DeleteJobTriggerRequest deleteJobTriggerRequest =
        DeleteJobTriggerRequest.newBuilder().setName(triggerName.toString()).build();

    // Send the trigger deletion request
    dlpServiceClient.deleteJobTrigger(deleteJobTriggerRequest);
    System.out.println("Trigger deleted: " + triggerName.toString());
  }
}
 
Example #12
Source File: DLPClient.java    From android-docs-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new client.
 * <p>
 * Ensure that you have enabled the DLP API for the given project.
 *
 * @param credentials authentication credentials
 * @param projectId   project ID
 */
public DLPClient(@NonNull GoogleCredentials credentials, @NonNull String projectId) {
    mProjectId = projectId;

    // create the client
    try {
        mClient = DlpServiceClient.create(DlpServiceSettings.newBuilder()
                .setCredentialsProvider(() -> credentials)
                .build());
    } catch (IOException e) {
        throw new IllegalStateException("Unable to create client", e);
    }
}
 
Example #13
Source File: RedactImageFileAllInfoTypes.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
static void redactImageFileAllInfoTypes(String projectId, String inputPath, String outputPath)
    throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the content to be redacted.
    ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath));
    ByteContentItem byteItem =
        ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build();

    // Construct the Redact request to be sent by the client.
    // Do not specify the type of info to redact.
    RedactImageRequest request =
        RedactImageRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setByteItem(byteItem)
            .build();

    // Use the client to send the API request.
    RedactImageResponse response = dlp.redactImage(request);

    // Parse the response and process results.
    FileOutputStream redacted = new FileOutputStream(outputPath);
    redacted.write(response.getRedactedImage().toByteArray());
    redacted.close();
    System.out.println("Redacted image written to " + outputPath);
  }
}
 
Example #14
Source File: JobsTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateJobs() throws Exception {
  // Call createJobs to create a Dlp job from project id and gcs path.
  JobsCreate.createJobs(PROJECT_ID, GCS_PATH);
  String output = bout.toString();
  assertThat(output).contains("Job created successfully:");

  // Delete the created Dlp Job
  String dlpJobName = output.split("Job created successfully: ")[1].split("\n")[0];
  DeleteDlpJobRequest deleteDlpJobRequest =
      DeleteDlpJobRequest.newBuilder().setName(dlpJobName).build();
  try (DlpServiceClient client = DlpServiceClient.create()) {
    client.deleteDlpJob(deleteDlpJobRequest);
  }
}
 
Example #15
Source File: TriggersTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateTrigger() throws Exception {
  TriggersCreate.createTrigger(PROJECT_ID, GCS_PATH);
  String output = bout.toString();
  assertThat(output).contains("Created Trigger:");

  // Delete the created trigger
  String triggerId = output.split("Created Trigger: ")[1].split("\n")[0];
  DeleteJobTriggerRequest deleteJobTriggerRequest =
      DeleteJobTriggerRequest.newBuilder().setName(triggerId).build();
  try (DlpServiceClient client = DlpServiceClient.create()) {
    client.deleteJobTrigger(deleteJobTriggerRequest);
  }
}
 
Example #16
Source File: DLPTextToBigQueryStreaming.java    From dlp-dataflow-deidentification with Apache License 2.0 5 votes vote down vote up
@StartBundle
public void startBundle() throws SQLException {

  try {
    this.dlpServiceClient = DlpServiceClient.create();

  } catch (IOException e) {
    LOG.error("Failed to create DLP Service Client", e.getMessage());
    throw new RuntimeException(e);
  }
}
 
Example #17
Source File: DLPServiceFactory.java    From dlp-dataflow-deidentification with Apache License 2.0 5 votes vote down vote up
public static synchronized DlpServiceClient getService()
    throws IOException, GeneralSecurityException {
  if (instance == null) {
    instance = buildService();
  }
  return instance;
}
 
Example #18
Source File: TriggersTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
private static JobTrigger createTrigger() throws IOException {
  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
    CloudStorageOptions cloudStorageOptions =
        CloudStorageOptions.newBuilder()
            .setFileSet(CloudStorageOptions.FileSet.newBuilder().setUrl(GCS_PATH))
            .build();
    StorageConfig storageConfig =
        StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).build();

    InspectJobConfig inspectJobConfig =
        InspectJobConfig.newBuilder()
            .setInspectConfig(InspectConfig.newBuilder().build())
            .setStorageConfig(storageConfig)
            .build();

    Duration duration = Duration.newBuilder().setSeconds(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)
            .setStatus(JobTrigger.Status.HEALTHY)
            .addTriggers(trigger)
            .build();

    CreateJobTriggerRequest createJobTriggerRequest =
        CreateJobTriggerRequest.newBuilder()
            .setParent(LocationName.of(PROJECT_ID, "global").toString())
            .setJobTrigger(jobTrigger)
            .build();

    return dlpServiceClient.createJobTrigger(createJobTriggerRequest);
  }
}
 
Example #19
Source File: DLPTextToBigQueryStreaming.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
@StartBundle
public void startBundle() throws SQLException {

  try {
    this.dlpServiceClient = DlpServiceClient.create();

  } catch (IOException e) {
    LOG.error("Failed to create DLP Service Client", e.getMessage());
    throw new RuntimeException(e);
  }
}
 
Example #20
Source File: DLPTokenizationDoFn.java    From dlp-dataflow-deidentification with Apache License 2.0 5 votes vote down vote up
@StartBundle
public void startBundle() throws SQLException {

  try {
    this.dlpServiceClient = DlpServiceClient.create();

  } catch (IOException e) {

    e.printStackTrace();
  }
}
 
Example #21
Source File: DLPInspectText.java    From beam with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup() throws IOException {
  this.requestBuilder =
      InspectContentRequest.newBuilder().setParent(ProjectName.of(this.projectId).toString());
  if (inspectTemplateName != null) {
    requestBuilder.setInspectTemplateName(this.inspectTemplateName);
  }
  if (inspectConfig != null) {
    requestBuilder.setInspectConfig(inspectConfig);
  }
  dlpServiceClient = DlpServiceClient.create();
}
 
Example #22
Source File: TriggersList.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void listTriggers(String projectId) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
    // Build the request to be sent by the client
    ListJobTriggersRequest listJobTriggersRequest =
        ListJobTriggersRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .build();

    // Use the client to send the API request.
    DlpServiceClient.ListJobTriggersPagedResponse response =
        dlpServiceClient.listJobTriggers(listJobTriggersRequest);

    // Parse the response and process the results
    System.out.println("DLP triggers found:");
    for (JobTrigger trigger : response.getPage().getValues()) {
      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());
    }
    ;
  }
}
 
Example #23
Source File: InfoTypesList.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void listInfoTypes() throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlpClient = DlpServiceClient.create()) {

    // Construct the request to be sent by the client
    ListInfoTypesRequest listInfoTypesRequest =
        ListInfoTypesRequest.newBuilder()
            // Only return infoTypes supported by certain parts of the API.
            // Supported filters are "supported_by=INSPECT" and "supported_by=RISK_ANALYSIS"
            // Defaults to "supported_by=INSPECT"
            .setFilter("supported_by=INSPECT")
            // BCP-47 language code for localized infoType friendly names.
            // Defaults to "en_US"
            .setLanguageCode("en-US")
            .build();

    // Use the client to send the API request.
    ListInfoTypesResponse response = dlpClient.listInfoTypes(listInfoTypesRequest);

    // Parse the response and process the results
    System.out.println("Infotypes found:");
    for (InfoTypeDescription infoTypeDescription : response.getInfoTypesList()) {
      System.out.println("Name : " + infoTypeDescription.getName());
      System.out.println("Display name : " + infoTypeDescription.getDisplayName());
    }
  }
}
 
Example #24
Source File: InspectImageFileAllInfoTypes.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
static void inspectImageFileAllInfoTypes(String projectId, String inputPath)
    throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the content to be inspected.
    ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath));
    ByteContentItem byteItem =
        ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build();

    // Construct the Inspect request to be sent by the client.
    // Do not specify the type of info to inspect.
    InspectContentRequest request =
        InspectContentRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setItem(ContentItem.newBuilder().setByteItem(byteItem).build())
            .build();

    // Use the client to send the API request.
    InspectContentResponse response = dlp.inspectContent(request);

    // Parse the response and process results.
    System.out.println("Findings: " + response.getResult().getFindingsCount());
    for (Finding f : response.getResult().getFindingsList()) {
      System.out.println("\tQuote: " + f.getQuote());
      System.out.println("\tInfo type: " + f.getInfoType().getName());
      System.out.println("\tLikelihood: " + f.getLikelihood());
    }
  }
}
 
Example #25
Source File: TemplatesList.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void listInspectTemplates(String projectId) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {

    // Create the request to be sent by the client
    ListInspectTemplatesRequest request =
        ListInspectTemplatesRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setPageSize(1)
            .build();

    // Send the request
    ListInspectTemplatesPagedResponse response = dlpServiceClient.listInspectTemplates(request);

    // Parse through and process the response
    System.out.println("Templates found:");
    for (InspectTemplate template : response.getPage().getResponse().getInspectTemplatesList()) {
      System.out.printf("Template name: %s\n", template.getName());
      if (template.getDisplayName() != null) {
        System.out.printf("\tDisplay name: %s \n", template.getDisplayName());
        System.out.printf("\tCreate time: %s \n", template.getCreateTime());
        System.out.printf("\tUpdate time: %s \n", template.getUpdateTime());

        // print inspection config
        InspectConfig inspectConfig = template.getInspectConfig();
        for (InfoType infoType : inspectConfig.getInfoTypesList()) {
          System.out.printf("\tInfoType: %s\n", infoType.getName());
        }
        System.out.printf("\tMin likelihood: %s\n", inspectConfig.getMinLikelihood());
        System.out.printf("\tLimits: %s\n", inspectConfig.getLimits().getMaxFindingsPerRequest());
      }
    }
  }
}
 
Example #26
Source File: RedactImageFileAllText.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
static void redactImageFileAllText(String projectId, String inputPath, String outputPath)
    throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlp = DlpServiceClient.create()) {
    // Specify the content to be redacted.
    ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath));
    ByteContentItem byteItem =
        ByteContentItem.newBuilder().setType(BytesType.IMAGE_JPEG).setData(fileBytes).build();

    // Enable redaction of all text.
    ImageRedactionConfig imageRedactionConfig =
        ImageRedactionConfig.newBuilder().setRedactAllText(true).build();

    // Construct the Redact request to be sent by the client.
    // Do not specify the type of info to redact.
    RedactImageRequest request =
        RedactImageRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setByteItem(byteItem)
            .addImageRedactionConfigs(imageRedactionConfig)
            .build();

    // Use the client to send the API request.
    RedactImageResponse response = dlp.redactImage(request);

    // Parse the response and process results.
    FileOutputStream redacted = new FileOutputStream(outputPath);
    redacted.write(response.getRedactedImage().toByteArray());
    redacted.close();
    System.out.println("Redacted image written to " + outputPath);
  }
}
 
Example #27
Source File: TemplatesCreate.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
public static void createInspectTemplate(String projectId) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
    // Specify the type of info the inspection will look for.
    // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
    List<InfoType> infoTypes =
        Stream.of("PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER")
            .map(it -> InfoType.newBuilder().setName(it).build())
            .collect(Collectors.toList());

    // Construct the inspection configuration for the template
    InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).build();

    // Optionally set a display name and a description for the template
    String displayName = "Inspection Config Template";
    String description = "Save configuration for future inspection jobs";

    // Build the template
    InspectTemplate inspectTemplate =
        InspectTemplate.newBuilder()
            .setInspectConfig(inspectConfig)
            .setDisplayName(displayName)
            .setDescription(description)
            .build();

    // Create the request to be sent by the client
    CreateInspectTemplateRequest createInspectTemplateRequest =
        CreateInspectTemplateRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setInspectTemplate(inspectTemplate)
            .build();

    // Send the request to the API and process the response
    InspectTemplate response =
        dlpServiceClient.createInspectTemplate(createInspectTemplateRequest);
    System.out.printf("Template created: %s", response.getName());
  }
}
 
Example #28
Source File: TemplatesTests.java    From java-docs-samples with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateInspectTemplate() throws Exception {
  TemplatesCreate.createInspectTemplate(PROJECT_ID);
  String output = bout.toString();
  assertThat(output).contains("Template created: ");

  // Delete the created template
  String templateId = output.split("Template created: ")[1].split("\n")[0];
  DeleteInspectTemplateRequest deleteInspectTemplateRequest =
      DeleteInspectTemplateRequest.newBuilder().setName(templateId).build();
  try (DlpServiceClient client = DlpServiceClient.create()) {
    client.deleteInspectTemplate(deleteInspectTemplateRequest);
  }
}
 
Example #29
Source File: JobsCreate.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void createJobs(String projectId, String gcsPath) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {

    // Set autoPopulateTimespan to true to scan only new content
    boolean autoPopulateTimespan = true;
    TimespanConfig timespanConfig =
        TimespanConfig.newBuilder()
            .setEnableAutoPopulationOfTimespanConfig(autoPopulateTimespan)
            .build();

    // Specify the GCS file to be inspected.
    CloudStorageOptions cloudStorageOptions =
        CloudStorageOptions.newBuilder()
            .setFileSet(CloudStorageOptions.FileSet.newBuilder().setUrl(gcsPath))
            .build();
    StorageConfig storageConfig =
        StorageConfig.newBuilder()
            .setCloudStorageOptions(cloudStorageOptions)
            .setTimespanConfig(timespanConfig)
            .build();

    // Specify the type of info the inspection will look for.
    // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
    List<InfoType> infoTypes =
        Stream.of("EMAIL_ADDRESS", "PERSON_NAME", "LOCATION", "PHONE_NUMBER")
            .map(it -> InfoType.newBuilder().setName(it).build())
            .collect(Collectors.toList());
    // The minimum likelihood required before returning a match:
    // See: https://cloud.google.com/dlp/docs/likelihood
    Likelihood minLikelihood = Likelihood.UNLIKELY;

    // The maximum number of findings to report (0 = server maximum)
    InspectConfig.FindingLimits findingLimits =
        InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerItem(100).build();

    InspectConfig inspectConfig =
        InspectConfig.newBuilder()
            .addAllInfoTypes(infoTypes)
            .setIncludeQuote(true)
            .setMinLikelihood(minLikelihood)
            .setLimits(findingLimits)
            .build();

    // Specify the action that is triggered when the job completes.
    Action.PublishSummaryToCscc publishSummaryToCscc =
        Action.PublishSummaryToCscc.getDefaultInstance();
    Action action = Action.newBuilder().setPublishSummaryToCscc(publishSummaryToCscc).build();

    // Configure the inspection job we want the service to perform.
    InspectJobConfig inspectJobConfig =
        InspectJobConfig.newBuilder()
            .setInspectConfig(inspectConfig)
            .setStorageConfig(storageConfig)
            .addActions(action)
            .build();

    // Construct the job creation request to be sent by the client.
    CreateDlpJobRequest createDlpJobRequest =
        CreateDlpJobRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setInspectJob(inspectJobConfig)
            .build();

    // Send the job creation request and process the response.
    DlpJob createdDlpJob = dlpServiceClient.createDlpJob(createDlpJobRequest);
    System.out.println("Job created successfully: " + createdDlpJob.getName());
  }
}
 
Example #30
Source File: TriggersCreate.java    From java-docs-samples with Apache License 2.0 4 votes vote down vote up
public static void createTrigger(String projectId, String gcsPath) throws IOException {
  // Initialize client that will be used to send requests. This client only needs to be created
  // once, and can be reused for multiple requests. After completing all of your requests, call
  // the "close" method on the client to safely clean up any remaining background resources.
  try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {

    // Set autoPopulateTimespan to true to scan only new content
    boolean autoPopulateTimespan = true;
    TimespanConfig timespanConfig =
        TimespanConfig.newBuilder()
            .setEnableAutoPopulationOfTimespanConfig(autoPopulateTimespan)
            .build();

    // Specify the GCS file to be inspected.
    CloudStorageOptions cloudStorageOptions =
        CloudStorageOptions.newBuilder()
            .setFileSet(CloudStorageOptions.FileSet.newBuilder().setUrl(gcsPath))
            .build();
    StorageConfig storageConfig =
        StorageConfig.newBuilder()
            .setCloudStorageOptions(cloudStorageOptions)
            .setTimespanConfig(timespanConfig)
            .build();

    // Specify the type of info the inspection will look for.
    // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types
    List<InfoType> infoTypes =
        Stream.of("PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER")
            .map(it -> InfoType.newBuilder().setName(it).build())
            .collect(Collectors.toList());

    InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).build();

    // Configure the inspection job we want the service to perform.
    InspectJobConfig inspectJobConfig =
        InspectJobConfig.newBuilder()
            .setInspectConfig(inspectConfig)
            .setStorageConfig(storageConfig)
            .build();

    // Set scanPeriod to the number of days between scans (minimum: 1 day)
    int scanPeriod = 1;

    // Optionally set a display name of max 100 chars and a description of max 250 chars
    String displayName = "Daily Scan";
    String description = "A daily inspection for personally identifiable information.";

    // 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)
            .setDisplayName(displayName)
            .setDescription(description)
            .setStatus(JobTrigger.Status.HEALTHY)
            .addTriggers(trigger)
            .build();

    // Create scan request to be sent by client
    CreateJobTriggerRequest createJobTriggerRequest =
        CreateJobTriggerRequest.newBuilder()
            .setParent(LocationName.of(projectId, "global").toString())
            .setJobTrigger(jobTrigger)
            .build();

    // Send the scan request and process the response
    JobTrigger createdJobTrigger = dlpServiceClient.createJobTrigger(createJobTriggerRequest);

    System.out.println("Created Trigger: " + createdJobTrigger.getName());
    System.out.println("Display Name: " + createdJobTrigger.getDisplayName());
    System.out.println("Description: " + createdJobTrigger.getDescription());
  }
}