/
InspectBigQuerySendToScc.java
151 lines (129 loc) · 6.26 KB
/
InspectBigQuerySendToScc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2023 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dlp.snippets;
// [START dlp_inspect_bigquery_send_to_scc]
import com.google.cloud.dlp.v2.DlpServiceClient;
import com.google.privacy.dlp.v2.Action;
import com.google.privacy.dlp.v2.BigQueryOptions;
import com.google.privacy.dlp.v2.BigQueryTable;
import com.google.privacy.dlp.v2.CreateDlpJobRequest;
import com.google.privacy.dlp.v2.DlpJob;
import com.google.privacy.dlp.v2.InfoType;
import com.google.privacy.dlp.v2.InfoTypeStats;
import com.google.privacy.dlp.v2.InspectConfig;
import com.google.privacy.dlp.v2.InspectDataSourceDetails;
import com.google.privacy.dlp.v2.InspectJobConfig;
import com.google.privacy.dlp.v2.Likelihood;
import com.google.privacy.dlp.v2.LocationName;
import com.google.privacy.dlp.v2.StorageConfig;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class InspectBigQuerySendToScc {
private static final int TIMEOUT_MINUTES = 15;
public static void main(String[] args) throws Exception {
// TODO(developer): Replace these variables before running the sample.
// The Google Cloud project id to use as a parent resource.
String projectId = "your-project-id";
// The BigQuery dataset id to be used and the reference table name to be inspected.
String bigQueryDatasetId = "your-project-bigquery-dataset";
String bigQueryTableId = "your-project-bigquery_table";
inspectBigQuerySendToScc(projectId, bigQueryDatasetId, bigQueryTableId);
}
// Inspects a BigQuery Table to send data to Security Command Center.
public static void inspectBigQuerySendToScc(
String projectId, String bigQueryDatasetId, String bigQueryTableId) throws Exception {
// 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 BigQuery table to be inspected.
BigQueryTable tableReference =
BigQueryTable.newBuilder()
.setProjectId(projectId)
.setDatasetId(bigQueryDatasetId)
.setTableId(bigQueryTableId)
.build();
BigQueryOptions bigQueryOptions =
BigQueryOptions.newBuilder().setTableReference(tableReference).build();
StorageConfig storageConfig =
StorageConfig.newBuilder().setBigQueryOptions(bigQueryOptions).build();
// Specify the type of info the inspection will look for.
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.
Likelihood minLikelihood = Likelihood.UNLIKELY;
// The maximum number of findings to report (0 = server maximum)
InspectConfig.FindingLimits findingLimits =
InspectConfig.FindingLimits.newBuilder().setMaxFindingsPerItem(100).build();
// Specify how the content should be inspected.
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 response = dlpServiceClient.createDlpJob(createDlpJobRequest);
// Get the current time.
long startTime = System.currentTimeMillis();
// Check if the job state is DONE.
while (response.getState() != DlpJob.JobState.DONE) {
// Sleep for 30 second.
Thread.sleep(30000);
// Get the updated job status.
response = dlpServiceClient.getDlpJob(response.getName());
// Check if the timeout duration has exceeded.
long elapsedTime = System.currentTimeMillis() - startTime;
if (TimeUnit.MILLISECONDS.toMinutes(elapsedTime) >= TIMEOUT_MINUTES) {
System.out.printf("Job did not complete within %d minutes.%n", TIMEOUT_MINUTES);
break;
}
}
// Print the results.
System.out.println("Job status: " + response.getState());
System.out.println("Job name: " + response.getName());
InspectDataSourceDetails.Result result = response.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());
}
}
}
}
// [END dlp_inspect_bigquery_send_to_scc]