use of com.google.privacy.dlp.v2.InfoType in project java-docs-samples by GoogleCloudPlatform.
the class Triggers method main.
// [END dlp_delete_trigger]
/**
* Command line application to crate, list and delete triggers.
*/
public static void main(String[] args) throws Exception {
OptionGroup optionsGroup = new OptionGroup();
optionsGroup.setRequired(true);
Option createTriggerOption = new Option("c", "create", false, "Create trigger to scan a GCS bucket");
optionsGroup.addOption(createTriggerOption);
Option listTriggersOption = new Option("l", "list", false, "List triggers");
optionsGroup.addOption(listTriggersOption);
Option deleteTriggerOption = new Option("d", "delete", false, "Delete trigger");
optionsGroup.addOption(deleteTriggerOption);
Options commandLineOptions = new Options();
commandLineOptions.addOptionGroup(optionsGroup);
Option bucketNameOption = Option.builder("bucketName").hasArg(true).required(false).build();
commandLineOptions.addOption(bucketNameOption);
Option gcsFileNameOption = Option.builder("fileName").hasArg(true).required(false).build();
commandLineOptions.addOption(gcsFileNameOption);
Option minLikelihoodOption = Option.builder("minLikelihood").hasArg(true).required(false).build();
commandLineOptions.addOption(minLikelihoodOption);
Option maxFindingsOption = Option.builder("maxFindings").hasArg(true).required(false).build();
commandLineOptions.addOption(maxFindingsOption);
Option infoTypesOption = Option.builder("infoTypes").hasArg(true).required(false).build();
infoTypesOption.setArgs(Option.UNLIMITED_VALUES);
commandLineOptions.addOption(infoTypesOption);
Option projectIdOption = Option.builder("projectId").hasArg(true).required(false).build();
commandLineOptions.addOption(projectIdOption);
Option triggerIdOption = Option.builder("triggerId").hasArg(true).required(false).build();
commandLineOptions.addOption(triggerIdOption);
Option displayNameOption = Option.builder("displayName").hasArg(true).required(false).build();
commandLineOptions.addOption(displayNameOption);
Option descriptionOption = Option.builder("description").hasArg(true).required(false).build();
commandLineOptions.addOption(descriptionOption);
Option scanPeriodOption = Option.builder("scanPeriod").hasArg(true).required(false).build();
commandLineOptions.addOption(scanPeriodOption);
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
try {
cmd = parser.parse(commandLineOptions, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp(DeIdentification.class.getName(), commandLineOptions);
System.exit(1);
return;
}
String projectId = cmd.getOptionValue(projectIdOption.getOpt(), ServiceOptions.getDefaultProjectId());
if (cmd.hasOption("c")) {
Likelihood minLikelihood = Likelihood.valueOf(cmd.getOptionValue(minLikelihoodOption.getOpt(), Likelihood.LIKELIHOOD_UNSPECIFIED.name()));
int maxFindings = Integer.parseInt(cmd.getOptionValue(maxFindingsOption.getOpt(), "0"));
String triggerId = cmd.getOptionValue(triggerIdOption.getOpt());
String displayName = cmd.getOptionValue(displayNameOption.getOpt(), "");
String description = cmd.getOptionValue(descriptionOption.getOpt(), "");
String bucketName = cmd.getOptionValue(bucketNameOption.getOpt());
String fileName = cmd.getOptionValue(gcsFileNameOption.getOpt());
int scanPeriod = Integer.valueOf(cmd.getOptionValue(scanPeriodOption.getOpt()));
List<InfoType> infoTypesList = new ArrayList<>();
if (cmd.hasOption(infoTypesOption.getOpt())) {
infoTypesList = new ArrayList<>();
String[] infoTypes = cmd.getOptionValues(infoTypesOption.getOpt());
for (String infoType : infoTypes) {
infoTypesList.add(InfoType.newBuilder().setName(infoType).build());
}
}
createTrigger(triggerId, displayName, description, bucketName, fileName, scanPeriod, infoTypesList, minLikelihood, maxFindings, projectId);
} else if (cmd.hasOption("l")) {
// list triggers
listTriggers(projectId);
} else if (cmd.hasOption("d")) {
String triggerId = cmd.getOptionValue(triggerIdOption.getOpt());
deleteTrigger(projectId, triggerId);
}
}
use of com.google.privacy.dlp.v2.InfoType in project java-docs-samples by GoogleCloudPlatform.
the class DeIdentification method reIdentifyWithFpe.
// [END dlp_deidentify_fpe]
// [START dlp_reidentify_fpe]
/**
* Reidentify a string by encrypting sensitive information while preserving format.
*
* @param string The string to reidentify.
* @param alphabet The set of characters used when encrypting the input. For more information, see
* cloud.google.com/dlp/docs/reference/rest/v2/content/deidentify
* @param keyName The name of the Cloud KMS key to use when decrypting the wrapped key.
* @param wrappedKey The encrypted (or "wrapped") AES-256 encryption key.
* @param projectId ID of Google Cloud project to run the API under.
* @param surrogateType The name of the surrogate custom info type to used during the encryption
* process.
*/
private static void reIdentifyWithFpe(String string, FfxCommonNativeAlphabet alphabet, String keyName, String wrappedKey, String projectId, String surrogateType) {
// instantiate a client
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
ContentItem contentItem = ContentItem.newBuilder().setValue(string).build();
InfoType surrogateTypeObject = InfoType.newBuilder().setName(surrogateType).build();
// Create the format-preserving encryption (FPE) configuration
KmsWrappedCryptoKey kmsWrappedCryptoKey = KmsWrappedCryptoKey.newBuilder().setWrappedKey(ByteString.copyFrom(BaseEncoding.base64().decode(wrappedKey))).setCryptoKeyName(keyName).build();
CryptoKey cryptoKey = CryptoKey.newBuilder().setKmsWrapped(kmsWrappedCryptoKey).build();
CryptoReplaceFfxFpeConfig cryptoReplaceFfxFpeConfig = CryptoReplaceFfxFpeConfig.newBuilder().setCryptoKey(cryptoKey).setCommonAlphabet(alphabet).setSurrogateInfoType(surrogateTypeObject).build();
// Create the deidentification transformation configuration
PrimitiveTransformation primitiveTransformation = PrimitiveTransformation.newBuilder().setCryptoReplaceFfxFpeConfig(cryptoReplaceFfxFpeConfig).build();
InfoTypeTransformation infoTypeTransformationObject = InfoTypeTransformation.newBuilder().setPrimitiveTransformation(primitiveTransformation).addInfoTypes(surrogateTypeObject).build();
InfoTypeTransformations infoTypeTransformationArray = InfoTypeTransformations.newBuilder().addTransformations(infoTypeTransformationObject).build();
// Create the inspection config
CustomInfoType customInfoType = CustomInfoType.newBuilder().setInfoType(surrogateTypeObject).setSurrogateType(SurrogateType.newBuilder().build()).build();
InspectConfig inspectConfig = InspectConfig.newBuilder().addCustomInfoTypes(customInfoType).build();
// Create the reidentification request object
DeidentifyConfig reidentifyConfig = DeidentifyConfig.newBuilder().setInfoTypeTransformations(infoTypeTransformationArray).build();
ReidentifyContentRequest request = ReidentifyContentRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setReidentifyConfig(reidentifyConfig).setInspectConfig(inspectConfig).setItem(contentItem).build();
// Execute the deidentification request
ReidentifyContentResponse response = dlpServiceClient.reidentifyContent(request);
// Print the reidentified input value
// e.g. "My SSN is 7261298621" --> "My SSN is 123456789"
String result = response.getItem().getValue();
System.out.println(result);
} catch (Exception e) {
System.out.println("Error in reidentifyWithFpe: " + e.getMessage());
}
}
use of com.google.privacy.dlp.v2.InfoType in project java-docs-samples by GoogleCloudPlatform.
the class Inspect method inspectGcsFile.
// [END dlp_inspect_file]
// [START dlp_inspect_gcs]
/**
* Inspect GCS file for Info types and wait on job completion using Google Cloud Pub/Sub
* notification
*
* @param bucketName The name of the bucket where the file resides.
* @param fileName The path to the file within the bucket to inspect (can include wildcards, eg.
* my-image.*)
* @param minLikelihood The minimum likelihood required before returning a match
* @param infoTypes The infoTypes of information to match
* @param maxFindings The maximum number of findings to report (0 = server maximum)
* @param topicId Google Cloud Pub/Sub topic Id to notify of job status
* @param subscriptionId Google Cloud Subscription to above topic to listen for job status updates
* @param projectId Google Cloud project ID
*/
private static void inspectGcsFile(String bucketName, String fileName, Likelihood minLikelihood, List<InfoType> infoTypes, int maxFindings, String topicId, String subscriptionId, String projectId) throws Exception {
// Instantiates a client
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
CloudStorageOptions cloudStorageOptions = CloudStorageOptions.newBuilder().setFileSet(CloudStorageOptions.FileSet.newBuilder().setUrl("gs://" + bucketName + "/" + fileName)).build();
StorageConfig storageConfig = StorageConfig.newBuilder().setCloudStorageOptions(cloudStorageOptions).build();
FindingLimits findingLimits = FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).build();
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();
InspectJobConfig inspectJobConfig = InspectJobConfig.newBuilder().setStorageConfig(storageConfig).setInspectConfig(inspectConfig).addActions(action).build();
// Semi-synchronously submit an inspect job, and wait on results
CreateDlpJobRequest createDlpJobRequest = CreateDlpJobRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setInspectJob(inspectJobConfig).build();
DlpJob dlpJob = dlpServiceClient.createDlpJob(createDlpJobRequest);
System.out.println("Job created with ID:" + dlpJob.getName());
final SettableApiFuture<Boolean> done = SettableApiFuture.create();
// Set up a Pub/Sub subscriber to listen on the job completion status
Subscriber subscriber = Subscriber.newBuilder(ProjectSubscriptionName.of(projectId, subscriptionId), (pubsubMessage, ackReplyConsumer) -> {
if (pubsubMessage.getAttributesCount() > 0 && pubsubMessage.getAttributesMap().get("DlpJobName").equals(dlpJob.getName())) {
// notify job completion
done.set(true);
ackReplyConsumer.ack();
}
}).build();
subscriber.startAsync();
// For long jobs, consider using a truly asynchronous execution model such as Cloud Functions
try {
done.get(1, TimeUnit.MINUTES);
// Wait for the job to become available
Thread.sleep(500);
} catch (Exception e) {
System.out.println("Unable to verify job completion.");
}
DlpJob completedJob = dlpServiceClient.getDlpJob(GetDlpJobRequest.newBuilder().setName(dlpJob.getName()).build());
System.out.println("Job status: " + completedJob.getState());
InspectDataSourceDetails inspectDataSourceDetails = completedJob.getInspectDetails();
InspectDataSourceDetails.Result result = inspectDataSourceDetails.getResult();
if (result.getInfoTypeStatsCount() > 0) {
System.out.println("Findings: ");
for (InfoTypeStats infoTypeStat : result.getInfoTypeStatsList()) {
System.out.print("\tInfo type: " + infoTypeStat.getInfoType().getName());
System.out.println("\tCount: " + infoTypeStat.getCount());
}
} else {
System.out.println("No findings.");
}
}
}
use of com.google.privacy.dlp.v2.InfoType in project java-docs-samples by GoogleCloudPlatform.
the class Inspect method inspectFile.
// [END dlp_inspect_string]
// [START dlp_inspect_file]
/**
* Inspect a local file
*
* @param filePath The path to a local file to inspect. Can be a text, JPG, or PNG file.
* @param minLikelihood The minimum likelihood required before returning a match
* @param maxFindings The maximum number of findings to report (0 = server maximum)
* @param infoTypes The infoTypes of information to match
* @param includeQuote Whether to include the matching string
* @param projectId Google Cloud project ID
*/
private static void inspectFile(String filePath, Likelihood minLikelihood, int maxFindings, List<InfoType> infoTypes, boolean includeQuote, String projectId) {
// Instantiates a client
try (DlpServiceClient dlpServiceClient = DlpServiceClient.create()) {
// detect file mime type, default to application/octet-stream
String mimeType = URLConnection.guessContentTypeFromName(filePath);
if (mimeType == null) {
mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(filePath);
}
ByteContentItem.BytesType bytesType;
switch(mimeType) {
case "image/jpeg":
bytesType = ByteContentItem.BytesType.IMAGE_JPEG;
break;
case "image/bmp":
bytesType = ByteContentItem.BytesType.IMAGE_BMP;
break;
case "image/png":
bytesType = ByteContentItem.BytesType.IMAGE_PNG;
break;
case "image/svg":
bytesType = ByteContentItem.BytesType.IMAGE_SVG;
break;
default:
bytesType = ByteContentItem.BytesType.BYTES_TYPE_UNSPECIFIED;
break;
}
byte[] data = Files.readAllBytes(Paths.get(filePath));
ByteContentItem byteContentItem = ByteContentItem.newBuilder().setType(bytesType).setData(ByteString.copyFrom(data)).build();
ContentItem contentItem = ContentItem.newBuilder().setByteItem(byteContentItem).build();
FindingLimits findingLimits = FindingLimits.newBuilder().setMaxFindingsPerRequest(maxFindings).build();
InspectConfig inspectConfig = InspectConfig.newBuilder().addAllInfoTypes(infoTypes).setMinLikelihood(minLikelihood).setLimits(findingLimits).setIncludeQuote(includeQuote).build();
InspectContentRequest request = InspectContentRequest.newBuilder().setParent(ProjectName.of(projectId).toString()).setInspectConfig(inspectConfig).setItem(contentItem).build();
InspectContentResponse response = dlpServiceClient.inspectContent(request);
InspectResult result = response.getResult();
if (result.getFindingsCount() > 0) {
System.out.println("Findings: ");
for (Finding finding : result.getFindingsList()) {
if (includeQuote) {
System.out.print("\tQuote: " + finding.getQuote());
}
System.out.print("\tInfo type: " + finding.getInfoType().getName());
System.out.println("\tLikelihood: " + finding.getLikelihood());
}
} else {
System.out.println("No findings.");
}
} catch (Exception e) {
System.out.println("Error in inspectFile: " + e.getMessage());
}
}
use of com.google.privacy.dlp.v2.InfoType in project java-docs-samples by GoogleCloudPlatform.
the class RiskAnalysis method main.
// [END dlp_k_map]
/**
* Command line application to perform risk analysis using the Data Loss Prevention API. Supported
* data format: BigQuery tables
*/
public static void main(String[] args) throws Exception {
OptionGroup optionsGroup = new OptionGroup();
optionsGroup.setRequired(true);
Option numericalAnalysisOption = new Option("n", "numerical");
optionsGroup.addOption(numericalAnalysisOption);
Option categoricalAnalysisOption = new Option("c", "categorical");
optionsGroup.addOption(categoricalAnalysisOption);
Option kanonymityOption = new Option("a", "kAnonymity");
optionsGroup.addOption(kanonymityOption);
Option kmapOption = new Option("m", "kAnonymity");
optionsGroup.addOption(kmapOption);
Option ldiversityOption = new Option("l", "lDiversity");
optionsGroup.addOption(ldiversityOption);
Options commandLineOptions = new Options();
commandLineOptions.addOptionGroup(optionsGroup);
Option datasetIdOption = Option.builder("datasetId").hasArg(true).required(false).build();
commandLineOptions.addOption(datasetIdOption);
Option tableIdOption = Option.builder("tableId").hasArg(true).required(false).build();
commandLineOptions.addOption(tableIdOption);
Option projectIdOption = Option.builder("projectId").hasArg(true).required(false).build();
commandLineOptions.addOption(projectIdOption);
Option topicIdOption = Option.builder("topicId").hasArg(true).required(false).build();
commandLineOptions.addOption(topicIdOption);
Option subscriptionIdOption = Option.builder("subscriptionId").hasArg(true).required(false).build();
commandLineOptions.addOption(subscriptionIdOption);
Option columnNameOption = Option.builder("columnName").hasArg(true).required(false).build();
commandLineOptions.addOption(columnNameOption);
Option sensitiveAttributeOption = Option.builder("sensitiveAttribute").hasArg(true).required(false).build();
commandLineOptions.addOption(sensitiveAttributeOption);
Option regionCodeOption = Option.builder("regionCode").hasArg(true).required(false).build();
commandLineOptions.addOption(regionCodeOption);
Option quasiIdColumnNamesOption = Option.builder("quasiIdColumnNames").hasArg(true).required(false).build();
quasiIdColumnNamesOption.setArgs(Option.UNLIMITED_VALUES);
commandLineOptions.addOption(quasiIdColumnNamesOption);
Option infoTypesOption = Option.builder("infoTypes").hasArg(true).required(false).build();
infoTypesOption.setArgs(Option.UNLIMITED_VALUES);
commandLineOptions.addOption(infoTypesOption);
CommandLineParser parser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();
CommandLine cmd;
try {
cmd = parser.parse(commandLineOptions, args);
} catch (ParseException e) {
System.out.println(e.getMessage());
formatter.printHelp(RiskAnalysis.class.getName(), commandLineOptions);
System.exit(1);
return;
}
String datasetId = cmd.getOptionValue(datasetIdOption.getOpt());
String tableId = cmd.getOptionValue(tableIdOption.getOpt());
// use default project id when project id is not specified
String projectId = cmd.getOptionValue(projectIdOption.getOpt(), ServiceOptions.getDefaultProjectId());
String regionCode = cmd.getOptionValue(regionCodeOption.getOpt(), "US");
String topicId = cmd.getOptionValue(topicIdOption.getOpt());
String subscriptionId = cmd.getOptionValue(subscriptionIdOption.getOpt());
List<InfoType> infoTypesList = Collections.emptyList();
if (cmd.hasOption(infoTypesOption.getOpt())) {
infoTypesList = new ArrayList<>();
String[] infoTypes = cmd.getOptionValues(infoTypesOption.getOpt());
for (String infoType : infoTypes) {
infoTypesList.add(InfoType.newBuilder().setName(infoType).build());
}
}
if (cmd.hasOption("n")) {
// numerical stats analysis
String columnName = cmd.getOptionValue(columnNameOption.getOpt());
numericalStatsAnalysis(projectId, datasetId, tableId, columnName, topicId, subscriptionId);
} else if (cmd.hasOption("c")) {
// categorical stats analysis
String columnName = cmd.getOptionValue(columnNameOption.getOpt());
categoricalStatsAnalysis(projectId, datasetId, tableId, columnName, topicId, subscriptionId);
} else if (cmd.hasOption("a")) {
// k-anonymity analysis
List<String> quasiIdColumnNames = Arrays.asList(cmd.getOptionValues(quasiIdColumnNamesOption.getOpt()));
calculateKAnonymity(projectId, datasetId, tableId, quasiIdColumnNames, topicId, subscriptionId);
} else if (cmd.hasOption("m")) {
// k-map analysis
List<String> quasiIdColumnNames = Arrays.asList(cmd.getOptionValues(quasiIdColumnNamesOption.getOpt()));
calculateKMap(projectId, datasetId, tableId, quasiIdColumnNames, infoTypesList, regionCode, topicId, subscriptionId);
} else if (cmd.hasOption("l")) {
// l-diversity analysis
String sensitiveAttribute = cmd.getOptionValue(sensitiveAttributeOption.getOpt());
List<String> quasiIdColumnNames = Arrays.asList(cmd.getOptionValues(quasiIdColumnNamesOption.getOpt()));
calculateLDiversity(projectId, datasetId, tableId, sensitiveAttribute, quasiIdColumnNames, topicId, subscriptionId);
}
}
Aggregations