com.google.privacy.dlp.v2.InfoType Java Examples
The following examples show how to use
com.google.privacy.dlp.v2.InfoType.
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: TemplatesTests.java From java-docs-samples with Apache License 2.0 | 6 votes |
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 #2
Source File: TemplatesList.java From java-docs-samples with Apache License 2.0 | 5 votes |
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 #3
Source File: TemplatesCreate.java From java-docs-samples with Apache License 2.0 | 5 votes |
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 #4
Source File: InspectStringCustomHotword.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectStringCustomHotword(String projectId, String textToInspect, String customHotword) 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 type and content to be inspected. ByteContentItem byteItem = ByteContentItem.newBuilder() .setType(BytesType.TEXT_UTF8) .setData(ByteString.copyFromUtf8(textToInspect)) .build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); // Increase likelihood of matches that have customHotword nearby HotwordRule hotwordRule = HotwordRule.newBuilder() .setHotwordRegex(Regex.newBuilder().setPattern(customHotword)) .setProximity(Proximity.newBuilder().setWindowBefore(50)) .setLikelihoodAdjustment( LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.VERY_LIKELY)) .build(); // Construct a ruleset that applies the hotword rule to the PERSON_NAME infotype. InspectionRuleSet ruleSet = InspectionRuleSet.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME").build()) .addRules(InspectionRule.newBuilder().setHotwordRule(hotwordRule)) .build(); // Construct the configuration for the Inspect request. InspectConfig config = InspectConfig.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME").build()) .setIncludeQuote(true) .addRuleSet(ruleSet) .setMinLikelihood(Likelihood.VERY_LIKELY) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .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 #5
Source File: InspectStringCustomOmitOverlap.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectStringCustomOmitOverlap(String projectId, String textToInspect) 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 type and content to be inspected. ByteContentItem byteItem = ByteContentItem.newBuilder() .setType(BytesType.TEXT_UTF8) .setData(ByteString.copyFromUtf8(textToInspect)) .build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); // Construct the custom infotype. CustomInfoType customInfoType = CustomInfoType.newBuilder() .setInfoType(InfoType.newBuilder().setName("VIP_DETECTOR")) .setRegex(Regex.newBuilder().setPattern("Larry Page|Sergey Brin")) .setExclusionType(ExclusionType.EXCLUSION_TYPE_EXCLUDE) .build(); // Exclude matches that also match the custom infotype. ExclusionRule exclusionRule = ExclusionRule.newBuilder() .setExcludeInfoTypes( ExcludeInfoTypes.newBuilder().addInfoTypes(customInfoType.getInfoType())) .setMatchingType(MatchingType.MATCHING_TYPE_FULL_MATCH) .build(); // Construct a ruleset that applies the exclusion rule to the PERSON_NAME infotype. InspectionRuleSet ruleSet = InspectionRuleSet.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME")) .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule)) .build(); // Construct the configuration for the Inspect request, including the ruleset. InspectConfig config = InspectConfig.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME")) .addCustomInfoTypes(customInfoType) .setIncludeQuote(true) .addRuleSet(ruleSet) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .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 #6
Source File: RedactImageFileColoredInfoTypes.java From java-docs-samples with Apache License 2.0 | 4 votes |
static void redactImageFileColoredInfoTypes(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(); // Define types of info to redact associate each one with a different color. // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types ImageRedactionConfig ssnRedactionConfig = ImageRedactionConfig.newBuilder() .setInfoType(InfoType.newBuilder().setName("US_SOCIAL_SECURITY_NUMBER").build()) .setRedactionColor(Color.newBuilder().setRed(.3f).setGreen(.1f).setBlue(.6f).build()) .build(); ImageRedactionConfig emailRedactionConfig = ImageRedactionConfig.newBuilder() .setInfoType(InfoType.newBuilder().setName("EMAIL_ADDRESS").build()) .setRedactionColor(Color.newBuilder().setRed(.5f).setGreen(.5f).setBlue(1).build()) .build(); ImageRedactionConfig phoneRedactionConfig = ImageRedactionConfig.newBuilder() .setInfoType(InfoType.newBuilder().setName("PHONE_NUMBER").build()) .setRedactionColor(Color.newBuilder().setRed(1).setGreen(0).setBlue(.6f).build()) .build(); // Create collection of all redact configurations. List<ImageRedactionConfig> imageRedactionConfigs = Arrays.asList(ssnRedactionConfig, emailRedactionConfig, phoneRedactionConfig); // List types of info to search for. InspectConfig config = InspectConfig.newBuilder() .addAllInfoTypes(imageRedactionConfigs.stream() .map(ImageRedactionConfig::getInfoType) .collect(Collectors.toList())) .build(); // Construct the Redact request to be sent by the client. RedactImageRequest request = RedactImageRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setByteItem(byteItem) .addAllImageRedactionConfigs(imageRedactionConfigs) .setInspectConfig(config) .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 #7
Source File: DeIdentifyWithExceptionList.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void deIdentifyWithExceptionList(String projectId, String textToDeIdentify) 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 what content you want the service to DeIdentify. ContentItem contentItem = ContentItem.newBuilder().setValue(textToDeIdentify).build(); // Construct the custom word list to be detected. Dictionary wordList = Dictionary.newBuilder() .setWordList( WordList.newBuilder() .addWords("[email protected]") .addWords("[email protected]") .build()) .build(); // Construct the custom dictionary detector associated with the word list. InfoType developerEmail = InfoType.newBuilder().setName("DEVELOPER_EMAIL").build(); CustomInfoType customInfoType = CustomInfoType.newBuilder().setInfoType(developerEmail).setDictionary(wordList).build(); // Specify the word list custom info type and build-in info type the inspection will look for. InfoType emailAddress = InfoType.newBuilder().setName("EMAIL_ADDRESS").build(); InspectConfig inspectConfig = InspectConfig.newBuilder() .addInfoTypes(emailAddress) .addCustomInfoTypes(customInfoType) .build(); // Define type of deidentification as replacement. PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() .setReplaceWithInfoTypeConfig(ReplaceWithInfoTypeConfig.getDefaultInstance()) .build(); // Associate de-identification type with info type. InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder() .addInfoTypes(emailAddress) .setPrimitiveTransformation(primitiveTransformation) .build(); // Construct the configuration for the de-id request and list all desired transformations. DeidentifyConfig deidentifyConfig = DeidentifyConfig.newBuilder() .setInfoTypeTransformations( InfoTypeTransformations.newBuilder().addTransformations(transformation)) .build(); // Combine configurations into a request for the service. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(contentItem) .setInspectConfig(inspectConfig) .setDeidentifyConfig(deidentifyConfig) .build(); // Send the request and receive response from the service DeidentifyContentResponse response = dlp.deidentifyContent(request); // Print the results System.out.println( "Text after replace with infotype config: " + response.getItem().getValue()); } }
Example #8
Source File: InspectString.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectString(String projectId, String textToInspect) 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 type and content to be inspected. ByteContentItem byteItem = ByteContentItem.newBuilder() .setType(BytesType.TEXT_UTF8) .setData(ByteString.copyFromUtf8(textToInspect)) .build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); // Specify the type of info the inspection will look for. List<InfoType> infoTypes = new ArrayList<>(); // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types for (String typeName : new String[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } // Construct the configuration for the Inspect request. InspectConfig config = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .setIncludeQuote(true) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .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 #9
Source File: DeIdentifyTableInfoTypes.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static Table deIdentifyTableInfoTypes(String projectId, Table tableToDeIdentify) 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 what content you want the service to de-identify. ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); // Specify how the content should be de-identified. // Select type of info to be replaced. InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build(); // Specify that findings should be replaced with corresponding info type name. ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig = ReplaceWithInfoTypeConfig.getDefaultInstance(); PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig).build(); // Associate info type with the replacement strategy InfoTypeTransformation infoTypeTransformation = InfoTypeTransformation.newBuilder() .addInfoTypes(infoType) .setPrimitiveTransformation(primitiveTransformation) .build(); InfoTypeTransformations infoTypeTransformations = InfoTypeTransformations.newBuilder() .addTransformations(infoTypeTransformation) .build(); // Specify fields to be de-identified. List<FieldId> fieldIds = Stream.of("PATIENT", "FACTOID") .map(id -> FieldId.newBuilder().setName(id).build()) .collect(Collectors.toList()); // Associate the de-identification and conditions with the specified field. FieldTransformation fieldTransformation = FieldTransformation.newBuilder() .setInfoTypeTransformations(infoTypeTransformations) .addAllFields(fieldIds) .build(); RecordTransformations transformations = RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); DeidentifyConfig deidentifyConfig = DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); // Combine configurations into a request for the service. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(contentItem) .setDeidentifyConfig(deidentifyConfig) .build(); // Send the request and receive response from the service. DeidentifyContentResponse response = dlp.deidentifyContent(request); // Print the results. System.out.println( "Table after de-identification: " + response.getItem().getTable()); return response.getItem().getTable(); } }
Example #10
Source File: TriggersCreate.java From java-docs-samples with Apache License 2.0 | 4 votes |
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()); } }
Example #11
Source File: DeIdentifyTableConditionInfoTypes.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static Table deIdentifyTableConditionInfoTypes(String projectId, Table tableToDeIdentify) 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 what content you want the service to de-identify. ContentItem contentItem = ContentItem.newBuilder().setTable(tableToDeIdentify).build(); // Specify how the content should be de-identified. // Select type of info to be replaced. InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build(); // Specify that findings should be replaced with corresponding info type name. ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig = ReplaceWithInfoTypeConfig.getDefaultInstance(); PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig).build(); // Associate info type with the replacement strategy InfoTypeTransformation infoTypeTransformation = InfoTypeTransformation.newBuilder() .addInfoTypes(infoType) .setPrimitiveTransformation(primitiveTransformation) .build(); InfoTypeTransformations infoTypeTransformations = InfoTypeTransformations.newBuilder() .addTransformations(infoTypeTransformation) .build(); // Specify fields to be de-identified. List<FieldId> fieldIds = Stream.of("PATIENT", "FACTOID") .map(id -> FieldId.newBuilder().setName(id).build()) .collect(Collectors.toList()); // Specify when the above fields should be de-identified. Condition condition = Condition.newBuilder() .setField(FieldId.newBuilder().setName("AGE").build()) .setOperator(RelationalOperator.GREATER_THAN) .setValue(Value.newBuilder().setIntegerValue(89).build()) .build(); // Apply the condition to records RecordCondition recordCondition = RecordCondition.newBuilder() .setExpressions(Expressions.newBuilder() .setConditions(Conditions.newBuilder() .addConditions(condition) .build()) .build()) .build(); // Associate the de-identification and conditions with the specified fields. FieldTransformation fieldTransformation = FieldTransformation.newBuilder() .setInfoTypeTransformations(infoTypeTransformations) .addAllFields(fieldIds) .setCondition(recordCondition) .build(); RecordTransformations transformations = RecordTransformations.newBuilder().addFieldTransformations(fieldTransformation).build(); DeidentifyConfig deidentifyConfig = DeidentifyConfig.newBuilder().setRecordTransformations(transformations).build(); // Combine configurations into a request for the service. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(contentItem) .setDeidentifyConfig(deidentifyConfig) .build(); // Send the request and receive response from the service. DeidentifyContentResponse response = dlp.deidentifyContent(request); // Print the results. System.out.println( "Table after de-identification: " + response.getItem().getTable()); return response.getItem().getTable(); } }
Example #12
Source File: InspectStringWithExclusionDict.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectStringWithExclusionDict(String projectId, String textToInspect, List<String> excludedMatchList) 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 type and content to be inspected. ByteContentItem byteItem = ByteContentItem.newBuilder() .setType(BytesType.TEXT_UTF8) .setData(ByteString.copyFromUtf8(textToInspect)) .build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).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 = new ArrayList<>(); for (String typeName : new String[]{"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } // Exclude matches from the specified excludedMatchList. ExclusionRule exclusionRule = ExclusionRule.newBuilder() .setMatchingType(MatchingType.MATCHING_TYPE_FULL_MATCH) .setDictionary(Dictionary.newBuilder() .setWordList(WordList.newBuilder().addAllWords(excludedMatchList))) .build(); // Construct a ruleset that applies the exclusion rule to the EMAIL_ADDRESSES infotype. InspectionRuleSet ruleSet = InspectionRuleSet.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS")) .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule)) .build(); // Construct the configuration for the Inspect request, including the ruleset. InspectConfig config = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .setIncludeQuote(true) .addRuleSet(ruleSet) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .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 #13
Source File: JobsCreate.java From java-docs-samples with Apache License 2.0 | 4 votes |
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 #14
Source File: InspectTable.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectTable(String projectId, Table tableToInspect) { // 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 table to be inspected. ContentItem item = ContentItem.newBuilder().setTable(tableToInspect).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 InfoType infoType = InfoType.newBuilder().setName("PHONE_NUMBER").build(); // Construct the configuration for the Inspect request. InspectConfig config = InspectConfig.newBuilder() .addInfoTypes(infoType) .setIncludeQuote(true) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .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()); } } catch (Exception e) { System.out.println("Error during inspectString: \n" + e.toString()); } }
Example #15
Source File: InspectImageFile.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectImageFile(String projectId, String filePath) 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 type and content to be inspected. ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath)); ByteContentItem byteItem = ByteContentItem.newBuilder().setType(BytesType.IMAGE).setData(fileBytes).build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); // Specify the type of info the inspection will look for. List<InfoType> infoTypes = new ArrayList<>(); // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types for (String typeName : new String[]{"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } // Construct the configuration for the Inspect request. InspectConfig config = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .setIncludeQuote(true) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .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 #16
Source File: RedactImageFileListedInfoTypes.java From java-docs-samples with Apache License 2.0 | 4 votes |
static void redactImageFileListedInfoTypes(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(); // Specify the types of info necessary to redact. List<InfoType> infoTypes = new ArrayList<>(); // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types for (String typeName : new String[] {"US_SOCIAL_SECURITY_NUMBER", "EMAIL_ADDRESS", "PHONE_NUMBER"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } InspectConfig inspectConfig = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .build(); // Prepare redaction configs. List<ImageRedactionConfig> imageRedactionConfigs = infoTypes.stream() .map(infoType -> ImageRedactionConfig.newBuilder().setInfoType(infoType).build()) .collect(Collectors.toList()); // Construct the Redact request to be sent by the client. RedactImageRequest request = RedactImageRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setByteItem(byteItem) .addAllImageRedactionConfigs(imageRedactionConfigs) .setInspectConfig(inspectConfig) .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 #17
Source File: InspectTextFile.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectTextFile(String projectId, String filePath) 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 type and content to be inspected. ByteString fileBytes = ByteString.readFrom(new FileInputStream(filePath)); ByteContentItem byteItem = ByteContentItem.newBuilder().setType(BytesType.TEXT_UTF8).setData(fileBytes).build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); // Specify the type of info the inspection will look for. List<InfoType> infoTypes = new ArrayList<>(); // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types for (String typeName : new String[]{"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } // Construct the configuration for the Inspect request. InspectConfig config = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .setIncludeQuote(true) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .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 #18
Source File: InspectPhoneNumber.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectString(String projectId, String textToInspect) 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 type and content to be inspected. ContentItem item = ContentItem.newBuilder() .setValue(textToInspect) .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 InfoType infoType = InfoType.newBuilder().setName("PHONE_NUMBER").build(); // Construct the configuration for the Inspect request. InspectConfig config = InspectConfig.newBuilder() .setIncludeQuote(true) .setMinLikelihood(Likelihood.POSSIBLE) .addInfoTypes(infoType) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .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 #19
Source File: DeIdentifyWithInfoType.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void deIdentifyWithInfoType(String projectId, String textToRedact) 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. ContentItem item = ContentItem.newBuilder() .setValue(textToRedact).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 InfoType infoType = InfoType.newBuilder().setName("EMAIL_ADDRESS").build(); InspectConfig inspectConfig = InspectConfig.newBuilder().addInfoTypes(infoType).build(); // Specify replacement string to be used for the finding. ReplaceWithInfoTypeConfig replaceWithInfoTypeConfig = ReplaceWithInfoTypeConfig.newBuilder().build(); // Define type of deidentification as replacement with info type. PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() .setReplaceWithInfoTypeConfig(replaceWithInfoTypeConfig) .build(); // Associate deidentification type with info type. InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder() .addInfoTypes(infoType) .setPrimitiveTransformation(primitiveTransformation) .build(); // Construct the configuration for the Redact request and list all desired transformations. DeidentifyConfig redactConfig = DeidentifyConfig.newBuilder() .setInfoTypeTransformations(InfoTypeTransformations.newBuilder() .addTransformations(transformation)) .build(); // Construct the Redact request to be sent by the client. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setDeidentifyConfig(redactConfig) .setInspectConfig(inspectConfig) .build(); // Use the client to send the API request. DeidentifyContentResponse response = dlp.deidentifyContent(request); // Parse the response and process results System.out.println("Text after redaction: " + response.getItem().getValue()); } }
Example #20
Source File: InspectStringMultipleRules.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectStringMultipleRules(String projectId, String textToInspect) 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 type and content to be inspected. ByteContentItem byteItem = ByteContentItem.newBuilder() .setType(BytesType.TEXT_UTF8) .setData(ByteString.copyFromUtf8(textToInspect)) .build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); // Construct hotword rules HotwordRule patientRule = HotwordRule.newBuilder() .setHotwordRegex(Regex.newBuilder().setPattern("patient")) .setProximity(Proximity.newBuilder().setWindowBefore(10)) .setLikelihoodAdjustment( LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.VERY_LIKELY)) .build(); HotwordRule doctorRule = HotwordRule.newBuilder() .setHotwordRegex(Regex.newBuilder().setPattern("doctor")) .setProximity(Proximity.newBuilder().setWindowBefore(10)) .setLikelihoodAdjustment( LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.UNLIKELY)) .build(); // Construct exclusion rules ExclusionRule quasimodoRule = ExclusionRule.newBuilder() .setDictionary( Dictionary.newBuilder().setWordList(WordList.newBuilder().addWords("Quasimodo"))) .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH) .build(); ExclusionRule redactedRule = ExclusionRule.newBuilder() .setRegex(Regex.newBuilder().setPattern("REDACTED")) .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH) .build(); // Construct a ruleset that applies the rules to the PERSON_NAME infotype. InspectionRuleSet ruleSet = InspectionRuleSet.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME")) .addRules(InspectionRule.newBuilder().setHotwordRule(patientRule)) .addRules(InspectionRule.newBuilder().setHotwordRule(doctorRule)) .addRules(InspectionRule.newBuilder().setExclusionRule(quasimodoRule)) .addRules(InspectionRule.newBuilder().setExclusionRule(redactedRule)) .build(); // Construct the configuration for the Inspect request, including the ruleset. InspectConfig config = InspectConfig.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME")) .setIncludeQuote(true) .addRuleSet(ruleSet) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .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 #21
Source File: DeIdentifyWithSimpleWordList.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void deidentifyWithSimpleWordList(String projectId, String textToDeIdentify) 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 what content you want the service to DeIdentify. ContentItem contentItem = ContentItem.newBuilder().setValue(textToDeIdentify).build(); // Construct the word list to be detected Dictionary wordList = Dictionary.newBuilder() .setWordList( WordList.newBuilder() .addWords("RM-GREEN") .addWords("RM-YELLOW") .addWords("RM-ORANGE") .build()) .build(); // Specify the word list custom info type the inspection will look for. InfoType infoType = InfoType.newBuilder().setName("CUSTOM_ROOM_ID").build(); CustomInfoType customInfoType = CustomInfoType.newBuilder().setInfoType(infoType).setDictionary(wordList).build(); InspectConfig inspectConfig = InspectConfig.newBuilder().addCustomInfoTypes(customInfoType).build(); // Define type of deidentification as replacement. PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() .setReplaceWithInfoTypeConfig(ReplaceWithInfoTypeConfig.getDefaultInstance()) .build(); // Associate deidentification type with info type. InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder() .addInfoTypes(infoType) .setPrimitiveTransformation(primitiveTransformation) .build(); // Construct the configuration for the Redact request and list all desired transformations. DeidentifyConfig deidentifyConfig = DeidentifyConfig.newBuilder() .setInfoTypeTransformations( InfoTypeTransformations.newBuilder().addTransformations(transformation)) .build(); // Combine configurations into a request for the service. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(contentItem) .setInspectConfig(inspectConfig) .setDeidentifyConfig(deidentifyConfig) .build(); // Send the request and receive response from the service DeidentifyContentResponse response = dlp.deidentifyContent(request); // Print the results System.out.println( "Text after replace with infotype config: " + response.getItem().getValue()); } }
Example #22
Source File: DeIdentifyWithReplacement.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void deIdentifyWithReplacement(String projectId, String textToRedact) { // 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. ContentItem item = ContentItem.newBuilder() .setValue(textToRedact).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 InfoType infoType = InfoType.newBuilder().setName("EMAIL_ADDRESS").build(); InspectConfig inspectConfig = InspectConfig.newBuilder().addInfoTypes(infoType).build(); // Specify replacement string to be used for the finding. ReplaceValueConfig replaceValueConfig = ReplaceValueConfig.newBuilder() .setNewValue(Value.newBuilder().setStringValue("[email-address]").build()) .build(); // Define type of deidentification as replacement. PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() .setReplaceConfig(replaceValueConfig) .build(); // Associate deidentification type with info type. InfoTypeTransformation transformation = InfoTypeTransformation.newBuilder() .addInfoTypes(infoType) .setPrimitiveTransformation(primitiveTransformation) .build(); // Construct the configuration for the Redact request and list all desired transformations. DeidentifyConfig redactConfig = DeidentifyConfig.newBuilder() .setInfoTypeTransformations(InfoTypeTransformations.newBuilder() .addTransformations(transformation)) .build(); // Construct the Redact request to be sent by the client. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setDeidentifyConfig(redactConfig) .setInspectConfig(inspectConfig) .build(); // Use the client to send the API request. DeidentifyContentResponse response = dlp.deidentifyContent(request); // Parse the response and process results System.out.println("Text after redaction: " + response.getItem().getValue()); } catch (Exception e) { System.out.println("Error during inspectString: \n" + e.toString()); } }
Example #23
Source File: InspectBigQueryTableWithSampling.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectBigQueryTableWithSampling( String projectId, String topicId, String subscriptionId) throws ExecutionException, InterruptedException, 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 BigQuery table to be inspected. BigQueryTable tableReference = BigQueryTable.newBuilder() .setProjectId("bigquery-public-data") .setDatasetId("usa_names") .setTableId("usa_1910_current") .build(); BigQueryOptions bigQueryOptions = BigQueryOptions.newBuilder() .setTableReference(tableReference) .setRowsLimit(1000) .setSampleMethod(SampleMethod.RANDOM_START) .addIdentifyingFields(FieldId.newBuilder().setName("name")) .build(); StorageConfig storageConfig = StorageConfig.newBuilder().setBigQueryOptions(bigQueryOptions).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 InfoType infoType = InfoType.newBuilder().setName("PERSON_NAME").build(); // Specify how the content should be inspected. InspectConfig inspectConfig = InspectConfig.newBuilder() .addInfoTypes(infoType) .setIncludeQuote(true) .build(); // Specify the action that is triggered when the job completes. String pubSubTopic = String.format("projects/%s/topics/%s", projectId, topicId); Action.PublishToPubSub publishToPubSub = Action.PublishToPubSub.newBuilder().setTopic(pubSubTopic).build(); Action action = Action.newBuilder().setPubSub(publishToPubSub).build(); // Configure the long running job we want the service to perform. InspectJobConfig inspectJobConfig = InspectJobConfig.newBuilder() .setStorageConfig(storageConfig) .setInspectConfig(inspectConfig) .addActions(action) .build(); // Create the request for the job configured above. CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setInspectJob(inspectJobConfig) .build(); // Use the client to send the request. final DlpJob dlpJob = dlp.createDlpJob(createDlpJobRequest); System.out.println("Job created: " + dlpJob.getName()); // Set up a Pub/Sub subscriber to listen on the job completion status final SettableApiFuture<Boolean> done = SettableApiFuture.create(); ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId); MessageReceiver messageHandler = (PubsubMessage pubsubMessage, AckReplyConsumer ackReplyConsumer) -> { handleMessage(dlpJob, done, pubsubMessage, ackReplyConsumer); }; Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageHandler).build(); subscriber.startAsync(); // Wait for job completion semi-synchronously // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions try { done.get(15, TimeUnit.MINUTES); } catch (TimeoutException e) { System.out.println("Job was not completed after 15 minutes."); return; } finally { subscriber.stopAsync(); subscriber.awaitTerminated(); } // Get the latest state of the job from the service GetDlpJobRequest request = GetDlpJobRequest.newBuilder().setName(dlpJob.getName()).build(); DlpJob completedJob = dlp.getDlpJob(request); // Parse the response and process results. System.out.println("Job status: " + completedJob.getState()); InspectDataSourceDetails.Result result = completedJob.getInspectDetails().getResult(); System.out.println("Findings: "); for (InfoTypeStats infoTypeStat : result.getInfoTypeStatsList()) { System.out.print("\tInfo type: " + infoTypeStat.getInfoType().getName()); System.out.println("\tCount: " + infoTypeStat.getCount()); } } }
Example #24
Source File: DLPClient.java From android-docs-samples with Apache License 2.0 | 4 votes |
/** * Example showing how to inspect an image to identify names, phone number, and * credit card numbers. * * @param inputImage image to analyze (PNG) * @return a redacted image */ @NonNull public ByteString redactPhoto(@NonNull ByteString inputImage) { // set sources of information to identify List<InfoType> infoTypes = ImmutableList.of( InfoType.newBuilder().setName("PERSON_NAME").build(), InfoType.newBuilder().setName("CREDIT_CARD_NUMBER").build(), InfoType.newBuilder().setName("PHONE_NUMBER").build()); // set sources of information that should be redacted (to match infoTypes) List<RedactImageRequest.ImageRedactionConfig> imageRedactionConfigs = infoTypes.stream().map(infoType -> RedactImageRequest.ImageRedactionConfig.newBuilder() .setInfoType(infoType) .build()) .collect(Collectors.toList()); // configuration for the request InspectConfig inspectConfig = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .setMinLikelihood(Likelihood.POSSIBLE) .build(); // content to be redacted ByteContentItem byteContentItem = ByteContentItem.newBuilder() .setType(ByteContentItem.BytesType.IMAGE_PNG) .setData(inputImage) .build(); // create a request RedactImageRequest redactImageRequest = RedactImageRequest.newBuilder() .setParent(ProjectName.of(mProjectId).toString()) .addAllImageRedactionConfigs(imageRedactionConfigs) .setByteItem(byteContentItem) .setInspectConfig(inspectConfig) .build(); // call the API and return the redacted image RedactImageResponse redactImageResponse = mClient.redactImage(redactImageRequest); return redactImageResponse.getRedactedImage(); }
Example #25
Source File: InspectStringWithoutOverlap.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectStringWithoutOverlap(String projectId, String textToInspect) 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 type and content to be inspected. ByteContentItem byteItem = ByteContentItem.newBuilder() .setType(BytesType.TEXT_UTF8) .setData(ByteString.copyFromUtf8(textToInspect)) .build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).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 = new ArrayList<>(); for (String typeName : new String[]{"DOMAIN_NAME", "EMAIL_ADDRESS"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } // Define a custom info type to exclude email addresses CustomInfoType customInfoType = CustomInfoType.newBuilder() .setInfoType(InfoType.newBuilder().setName("EMAIL_ADDRESS")) .setExclusionType(ExclusionType.EXCLUSION_TYPE_EXCLUDE) .build(); // Exclude EMAIL_ADDRESS matches ExclusionRule exclusionRule = ExclusionRule.newBuilder() .setExcludeInfoTypes(ExcludeInfoTypes.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS"))) .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH) .build(); // Construct a ruleset that applies the exclusion rule to the DOMAIN_NAME infotype. // If a DOMAIN_NAME match is part of an EMAIL_ADDRESS match, the DOMAIN_NAME match will // be excluded. InspectionRuleSet ruleSet = InspectionRuleSet.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("DOMAIN_NAME")) .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule)) .build(); // Construct the configuration for the Inspect request, including the ruleset. InspectConfig config = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .addCustomInfoTypes(customInfoType) .setIncludeQuote(true) .addRuleSet(ruleSet) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .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 #26
Source File: InspectWithHotwordRules.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectWithHotwordRules( String projectId, String textToInspect, String customRegexPattern, String hotwordRegexPattern) 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 type and content to be inspected. ByteContentItem byteItem = ByteContentItem.newBuilder() .setType(BytesType.TEXT_UTF8) .setData(ByteString.copyFromUtf8(textToInspect)) .build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).build(); // Specify the regex pattern the inspection will look for. Regex regex = Regex.newBuilder().setPattern(customRegexPattern).build(); // Construct the custom regex detector. InfoType infoType = InfoType.newBuilder().setName("C_MRN").build(); CustomInfoType customInfoType = CustomInfoType.newBuilder().setInfoType(infoType).setRegex(regex).build(); // Specify hotword likelihood adjustment. LikelihoodAdjustment likelihoodAdjustment = LikelihoodAdjustment.newBuilder().setFixedLikelihood(Likelihood.VERY_LIKELY).build(); // Specify a window around a finding to apply a detection rule. Proximity proximity = Proximity.newBuilder().setWindowBefore(10).build(); // Construct hotword rule. HotwordRule hotwordRule = HotwordRule.newBuilder() .setHotwordRegex(Regex.newBuilder().setPattern(hotwordRegexPattern).build()) .setLikelihoodAdjustment(likelihoodAdjustment) .setProximity(proximity) .build(); // Construct rule set for the inspect config. InspectionRuleSet inspectionRuleSet = InspectionRuleSet.newBuilder() .addInfoTypes(infoType) .addRules(InspectionRule.newBuilder().setHotwordRule(hotwordRule)) .build(); // Construct the configuration for the Inspect request. InspectConfig config = InspectConfig.newBuilder() .addCustomInfoTypes(customInfoType) .setIncludeQuote(true) .setMinLikelihood(Likelihood.POSSIBLE) .addRuleSet(inspectionRuleSet) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .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 #27
Source File: RedactImageFile.java From java-docs-samples with Apache License 2.0 | 4 votes |
static void redactImageFile(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 inspected. ByteString fileBytes = ByteString.readFrom(new FileInputStream(inputPath)); ByteContentItem byteItem = ByteContentItem.newBuilder().setType(BytesType.IMAGE).setData(fileBytes).build(); // Specify the type of info and likelihood necessary to redact. List<InfoType> infoTypes = new ArrayList<>(); // See https://cloud.google.com/dlp/docs/infotypes-reference for complete list of info types for (String typeName : new String[] {"PHONE_NUMBER", "EMAIL_ADDRESS", "CREDIT_CARD_NUMBER"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } InspectConfig config = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .setMinLikelihood(Likelihood.LIKELY) .build(); // Construct the Redact request to be sent by the client. RedactImageRequest request = RedactImageRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setByteItem(byteItem) .setInspectConfig(config) .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 #28
Source File: InspectStringOmitOverlap.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectStringOmitOverlap(String projectId, String textToInspect) 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 type and content to be inspected. ByteContentItem byteItem = ByteContentItem.newBuilder() .setType(BytesType.TEXT_UTF8) .setData(ByteString.copyFromUtf8(textToInspect)) .build(); ContentItem item = ContentItem.newBuilder().setByteItem(byteItem).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 = new ArrayList<>(); for (String typeName : new String[]{"PERSON_NAME", "EMAIL_ADDRESS"}) { infoTypes.add(InfoType.newBuilder().setName(typeName).build()); } // Exclude EMAIL_ADDRESS matches ExclusionRule exclusionRule = ExclusionRule.newBuilder() .setExcludeInfoTypes(ExcludeInfoTypes.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("EMAIL_ADDRESS"))) .setMatchingType(MatchingType.MATCHING_TYPE_PARTIAL_MATCH) .build(); // Construct a ruleset that applies the exclusion rule to the PERSON_NAME infotype. // If a PERSON_NAME match overlaps with an EMAIL_ADDRESS match, the PERSON_NAME match will // be excluded. InspectionRuleSet ruleSet = InspectionRuleSet.newBuilder() .addInfoTypes(InfoType.newBuilder().setName("PERSON_NAME")) .addRules(InspectionRule.newBuilder().setExclusionRule(exclusionRule)) .build(); // Construct the configuration for the Inspect request, including the ruleset. InspectConfig config = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .setIncludeQuote(true) .addRuleSet(ruleSet) .build(); // Construct the Inspect request to be sent by the client. InspectContentRequest request = InspectContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(item) .setInspectConfig(config) .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 #29
Source File: InspectGcsFile.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void inspectGcsFile( String projectId, String gcsUri, String topicId, String subscriptionId) throws ExecutionException, InterruptedException, 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 GCS file to be inspected. CloudStorageOptions cloudStorageOptions = CloudStorageOptions.newBuilder() .setFileSet(FileSet.newBuilder().setUrl(gcsUri)) .build(); StorageConfig storageConfig = StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).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()); // Specify how the content should be inspected. InspectConfig inspectConfig = InspectConfig.newBuilder() .addAllInfoTypes(infoTypes) .setIncludeQuote(true) .build(); // Specify the action that is triggered when the job completes. String pubSubTopic = String.format("projects/%s/topics/%s", projectId, topicId); Action.PublishToPubSub publishToPubSub = Action.PublishToPubSub.newBuilder().setTopic(pubSubTopic).build(); Action action = Action.newBuilder().setPubSub(publishToPubSub).build(); // Configure the long running job we want the service to perform. InspectJobConfig inspectJobConfig = InspectJobConfig.newBuilder() .setStorageConfig(storageConfig) .setInspectConfig(inspectConfig) .addActions(action) .build(); // Create the request for the job configured above. CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setInspectJob(inspectJobConfig) .build(); // Use the client to send the request. final DlpJob dlpJob = dlp.createDlpJob(createDlpJobRequest); System.out.println("Job created: " + dlpJob.getName()); // Set up a Pub/Sub subscriber to listen on the job completion status final SettableApiFuture<Boolean> done = SettableApiFuture.create(); ProjectSubscriptionName subscriptionName = ProjectSubscriptionName.of(projectId, subscriptionId); MessageReceiver messageHandler = (PubsubMessage pubsubMessage, AckReplyConsumer ackReplyConsumer) -> { handleMessage(dlpJob, done, pubsubMessage, ackReplyConsumer); }; Subscriber subscriber = Subscriber.newBuilder(subscriptionName, messageHandler).build(); subscriber.startAsync(); // Wait for job completion semi-synchronously // For long jobs, consider using a truly asynchronous execution model such as Cloud Functions try { done.get(15, TimeUnit.MINUTES); } catch (TimeoutException e) { System.out.println("Job was not completed after 15 minutes."); return; } finally { subscriber.stopAsync(); subscriber.awaitTerminated(); } // Get the latest state of the job from the service GetDlpJobRequest request = GetDlpJobRequest.newBuilder().setName(dlpJob.getName()).build(); DlpJob completedJob = dlp.getDlpJob(request); // Parse the response and process results. System.out.println("Job status: " + completedJob.getState()); InspectDataSourceDetails.Result result = completedJob.getInspectDetails().getResult(); System.out.println("Findings: "); for (InfoTypeStats infoTypeStat : result.getInfoTypeStatsList()) { System.out.print("\tInfo type: " + infoTypeStat.getInfoType().getName()); System.out.println("\tCount: " + infoTypeStat.getCount()); } } }
Example #30
Source File: DeIdentifyWithMasking.java From java-docs-samples with Apache License 2.0 | 4 votes |
public static void deIdentifyWithMasking(String projectId, String textToDeIdentify) 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 what content you want the service to DeIdentify ContentItem contentItem = ContentItem.newBuilder().setValue(textToDeIdentify).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 InfoType infoType = InfoType.newBuilder().setName("US_SOCIAL_SECURITY_NUMBER").build(); InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(Arrays.asList(infoType)).build(); // Specify how the info from the inspection should be masked. CharacterMaskConfig characterMaskConfig = CharacterMaskConfig.newBuilder() .setMaskingCharacter("X") // Character to replace the found info with .setNumberToMask(5) // How many characters should be masked .build(); PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder() .setReplaceWithInfoTypeConfig(ReplaceWithInfoTypeConfig.getDefaultInstance()) .build(); InfoTypeTransformation infoTypeTransformation = InfoTypeTransformation.newBuilder() .setPrimitiveTransformation(primitiveTransformation) .build(); InfoTypeTransformations transformations = InfoTypeTransformations.newBuilder().addTransformations(infoTypeTransformation).build(); DeidentifyConfig deidentifyConfig = DeidentifyConfig.newBuilder().setInfoTypeTransformations(transformations).build(); // Combine configurations into a request for the service. DeidentifyContentRequest request = DeidentifyContentRequest.newBuilder() .setParent(LocationName.of(projectId, "global").toString()) .setItem(contentItem) .setInspectConfig(inspectConfig) .setDeidentifyConfig(deidentifyConfig) .build(); // Send the request and receive response from the service DeidentifyContentResponse response = dlp.deidentifyContent(request); // Print the results System.out.println("Text after masking: " + response.getItem().getValue()); } }