|
| 1 | +/** |
| 2 | + * Copyright 2017, Google, Inc. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. |
| 5 | + * You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package com.example.dlp; |
| 17 | + |
| 18 | +import com.google.cloud.dlp.v2beta1.DlpServiceClient; |
| 19 | +import com.google.privacy.dlp.v2beta1.ContentItem; |
| 20 | +import com.google.privacy.dlp.v2beta1.InfoType; |
| 21 | +import com.google.privacy.dlp.v2beta1.InspectConfig; |
| 22 | +import com.google.privacy.dlp.v2beta1.Likelihood; |
| 23 | +import com.google.privacy.dlp.v2beta1.RedactContentRequest.ReplaceConfig; |
| 24 | +import com.google.privacy.dlp.v2beta1.RedactContentResponse; |
| 25 | +import com.google.protobuf.ByteString; |
| 26 | +import org.apache.commons.cli.CommandLine; |
| 27 | +import org.apache.commons.cli.CommandLineParser; |
| 28 | +import org.apache.commons.cli.DefaultParser; |
| 29 | +import org.apache.commons.cli.HelpFormatter; |
| 30 | +import org.apache.commons.cli.Option; |
| 31 | +import org.apache.commons.cli.Options; |
| 32 | +import org.apache.commons.cli.ParseException; |
| 33 | + |
| 34 | +import java.util.ArrayList; |
| 35 | +import java.util.Collections; |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +public class Redact { |
| 39 | + |
| 40 | + private static void redactString(String string, String replacement, Likelihood minLikelihood, |
| 41 | + List<InfoType> infoTypes) throws Exception { |
| 42 | + // [START dlp_redact_string] |
| 43 | + // Instantiate the DLP client |
| 44 | + try (DlpServiceClient dlpClient = DlpServiceClient.create()) { |
| 45 | + // The minimum likelihood required before returning a match |
| 46 | + // eg.minLikelihood = LIKELIHOOD_VERY_LIKELY; |
| 47 | + InspectConfig inspectConfig = InspectConfig.newBuilder() |
| 48 | + .addAllInfoTypes(infoTypes) |
| 49 | + .setMinLikelihood(minLikelihood) |
| 50 | + .build(); |
| 51 | + |
| 52 | + ContentItem contentItem = ContentItem.newBuilder() |
| 53 | + .setType("text/plain") |
| 54 | + .setData(ByteString.copyFrom(string.getBytes())) |
| 55 | + .build(); |
| 56 | + |
| 57 | + List<ReplaceConfig> replaceConfigs = new ArrayList<>(); |
| 58 | + |
| 59 | + if (infoTypes.isEmpty()) { |
| 60 | + // replace all detected sensitive elements with replacement string |
| 61 | + replaceConfigs.add( |
| 62 | + ReplaceConfig.newBuilder() |
| 63 | + .setReplaceWith(replacement) |
| 64 | + .build()); |
| 65 | + } else { |
| 66 | + // Replace select info types with chosen replacement string |
| 67 | + for (InfoType infoType : infoTypes) { |
| 68 | + replaceConfigs.add( |
| 69 | + ReplaceConfig.newBuilder() |
| 70 | + .setInfoType(infoType) |
| 71 | + .setReplaceWith(replacement) |
| 72 | + .build()); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + RedactContentResponse contentResponse = dlpClient.redactContent( |
| 77 | + inspectConfig, Collections.singletonList(contentItem), replaceConfigs); |
| 78 | + for (ContentItem responseItem : contentResponse.getItemsList()) { |
| 79 | + // print out string with redacted content |
| 80 | + System.out.println(responseItem.getData().toStringUtf8()); |
| 81 | + } |
| 82 | + } |
| 83 | + // [END dlp_redact_string] |
| 84 | + } |
| 85 | + |
| 86 | + // Command line application to redact strings using the Data Loss Prevention API |
| 87 | + public static void main(String[] args) throws Exception { |
| 88 | + Options commandLineOptions = new Options(); |
| 89 | + |
| 90 | + Option stringOption = Option.builder("s") |
| 91 | + .longOpt("source string") |
| 92 | + .hasArg(true) |
| 93 | + .required(true) |
| 94 | + .build(); |
| 95 | + commandLineOptions.addOption(stringOption); |
| 96 | + |
| 97 | + Option replaceOption = Option.builder("r") |
| 98 | + .longOpt("replace string") |
| 99 | + .hasArg(true) |
| 100 | + .required(true) |
| 101 | + .build(); |
| 102 | + commandLineOptions.addOption(replaceOption); |
| 103 | + |
| 104 | + Option minLikelihoodOption = Option.builder("minLikelihood") |
| 105 | + .hasArg(true) |
| 106 | + .required(false) |
| 107 | + .build(); |
| 108 | + commandLineOptions.addOption(minLikelihoodOption); |
| 109 | + |
| 110 | + Option infoTypesOption = Option.builder("infoTypes") |
| 111 | + .hasArg(true) |
| 112 | + .required(false) |
| 113 | + .build(); |
| 114 | + infoTypesOption.setArgs(Option.UNLIMITED_VALUES); |
| 115 | + commandLineOptions.addOption(infoTypesOption); |
| 116 | + |
| 117 | + CommandLineParser parser = new DefaultParser(); |
| 118 | + HelpFormatter formatter = new HelpFormatter(); |
| 119 | + CommandLine cmd; |
| 120 | + |
| 121 | + try { |
| 122 | + cmd = parser.parse(commandLineOptions, args); |
| 123 | + } catch (ParseException e) { |
| 124 | + System.out.println(e.getMessage()); |
| 125 | + formatter.printHelp(Redact.class.getName(), commandLineOptions); |
| 126 | + System.exit(1); |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + String source = cmd.getOptionValue(stringOption.getOpt()); |
| 131 | + String replacement = cmd.getOptionValue(replaceOption.getOpt()); |
| 132 | + |
| 133 | + List<InfoType> infoTypesList = new ArrayList<>(); |
| 134 | + String[] infoTypes = cmd.getOptionValues(infoTypesOption.getOpt()); |
| 135 | + if (infoTypes != null) { |
| 136 | + for (String infoType : infoTypes) { |
| 137 | + infoTypesList.add(InfoType.newBuilder().setName(infoType).build()); |
| 138 | + } |
| 139 | + } |
| 140 | + redactString(source, replacement, Likelihood.LIKELIHOOD_UNSPECIFIED, infoTypesList); |
| 141 | + } |
| 142 | +} |
0 commit comments