Skip to content

Commit 5ea0269

Browse files
committed
chore: migrate code from googleapis/java-securitycenter
1 parent 6161d0f commit 5ea0269

16 files changed

+1288
-2
lines changed

security-command-center/snippets/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
<dependency>
3333
<groupId>com.google.cloud</groupId>
3434
<artifactId>libraries-bom</artifactId>
35-
<version>26.1.4</version>
35+
<version>26.1.3</version>
3636
<type>pom</type>
3737
<scope>import</scope>
3838
</dependency>
@@ -43,7 +43,7 @@
4343
<dependency>
4444
<groupId>com.google.cloud</groupId>
4545
<artifactId>google-cloud-securitycenter</artifactId>
46-
<version>2.13.0</version>
46+
<version>2.11.1</version>
4747
</dependency>
4848

4949
<dependency>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package bigqueryexport;
18+
19+
// [START securitycenter_create_bigquery_export]
20+
21+
import com.google.cloud.securitycenter.v1.BigQueryExport;
22+
import com.google.cloud.securitycenter.v1.CreateBigQueryExportRequest;
23+
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
24+
import java.io.IOException;
25+
import java.util.UUID;
26+
27+
public class CreateBigQueryExport {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(Developer): Modify the following variable values.
31+
32+
// parent: Use any one of the following resource paths:
33+
// - organizations/{organization_id}
34+
// - folders/{folder_id}
35+
// - projects/{project_id}
36+
String parent = String.format("projects/%s", "your-google-cloud-project-id");
37+
38+
// filter: Expression that defines the filter to apply across create/update events of findings.
39+
String filter =
40+
"severity=\"LOW\" OR severity=\"MEDIUM\" AND "
41+
+ "category=\"Persistence: IAM Anomalous Grant\" AND "
42+
+ "-resource.type:\"compute\"";
43+
44+
// bigQueryDatasetId: The BigQuery dataset to write findings' updates to.
45+
String bigQueryDatasetId = "your-bigquery-dataset-id";
46+
47+
// bigQueryExportId: Unique identifier provided by the client.
48+
// For more info, see:
49+
// https://cloud.google.com/security-command-center/docs/how-to-analyze-findings-in-big-query#export_findings_from_to
50+
String bigQueryExportId = "default-" + UUID.randomUUID().toString().split("-")[0];
51+
52+
createBigQueryExport(parent, filter, bigQueryDatasetId, bigQueryExportId);
53+
}
54+
55+
// Create export configuration to export findings from a project to a BigQuery dataset.
56+
// Optionally specify filter to export certain findings only.
57+
public static void createBigQueryExport(
58+
String parent, String filter, String bigQueryDatasetId, String bigQueryExportId)
59+
throws IOException {
60+
// Initialize client that will be used to send requests. This client only needs to be created
61+
// once, and can be reused for multiple requests. After completing all of your requests, call
62+
// the "close" method on the client to safely clean up any remaining background resources.
63+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
64+
65+
// Create the BigQuery export configuration.
66+
BigQueryExport bigQueryExport =
67+
BigQueryExport.newBuilder()
68+
.setDescription(
69+
"Export low and medium findings if the compute resource "
70+
+ "has an IAM anomalous grant")
71+
.setFilter(filter)
72+
.setDataset(String.format("%s/datasets/%s", parent, bigQueryDatasetId))
73+
.build();
74+
75+
CreateBigQueryExportRequest bigQueryExportRequest =
76+
CreateBigQueryExportRequest.newBuilder()
77+
.setParent(parent)
78+
.setBigQueryExport(bigQueryExport)
79+
.setBigQueryExportId(bigQueryExportId)
80+
.build();
81+
82+
// Create the export request.
83+
BigQueryExport response = client.createBigQueryExport(bigQueryExportRequest);
84+
85+
System.out.printf("BigQuery export request created successfully: %s\n", response.getName());
86+
}
87+
}
88+
}
89+
// [END securitycenter_create_bigquery_export]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package bigqueryexport;
18+
19+
// [START securitycenter_delete_bigquery_export]
20+
21+
import com.google.cloud.securitycenter.v1.DeleteBigQueryExportRequest;
22+
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
23+
import java.io.IOException;
24+
25+
public class DeleteBigQueryExport {
26+
27+
public static void main(String[] args) throws IOException {
28+
// TODO(Developer): Modify the following variable values.
29+
30+
// parent: Use any one of the following resource paths:
31+
// - organizations/{organization_id}
32+
// - folders/{folder_id}
33+
// - projects/{project_id}
34+
String parent = String.format("projects/%s", "your-google-cloud-project-id");
35+
36+
// bigQueryExportId: Unique identifier that is used to identify the export.
37+
String bigQueryExportId = "export-id";
38+
39+
deleteBigQueryExport(parent, bigQueryExportId);
40+
}
41+
42+
// Delete an existing BigQuery export.
43+
public static void deleteBigQueryExport(String parent, String bigQueryExportId)
44+
throws IOException {
45+
// Initialize client that will be used to send requests. This client only needs to be created
46+
// once, and can be reused for multiple requests. After completing all of your requests, call
47+
// the "close" method on the client to safely clean up any remaining background resources.
48+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
49+
50+
DeleteBigQueryExportRequest bigQueryExportRequest =
51+
DeleteBigQueryExportRequest.newBuilder()
52+
.setName(String.format("%s/bigQueryExports/%s", parent, bigQueryExportId))
53+
.build();
54+
55+
client.deleteBigQueryExport(bigQueryExportRequest);
56+
System.out.printf("BigQuery export request deleted successfully: %s", bigQueryExportId);
57+
}
58+
}
59+
}
60+
// [END securitycenter_delete_bigquery_export]
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package bigqueryexport;
18+
19+
// [START securitycenter_get_bigquery_export]
20+
21+
import com.google.cloud.securitycenter.v1.BigQueryExport;
22+
import com.google.cloud.securitycenter.v1.GetBigQueryExportRequest;
23+
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
24+
import java.io.IOException;
25+
26+
public class GetBigQueryExport {
27+
28+
public static void main(String[] args) throws IOException {
29+
// TODO(Developer): Modify the following variable values.
30+
31+
// parent: Use any one of the following resource paths:
32+
// - organizations/{organization_id}
33+
// - folders/{folder_id}
34+
// - projects/{project_id}
35+
String parent = String.format("projects/%s", "your-google-cloud-project-id");
36+
37+
// bigQueryExportId: Unique identifier that is used to identify the export.
38+
String bigQueryExportId = "export-id";
39+
40+
getBigQueryExport(parent, bigQueryExportId);
41+
}
42+
43+
// Retrieve an existing BigQuery export.
44+
public static void getBigQueryExport(String parent, String bigQueryExportId) throws IOException {
45+
// Initialize client that will be used to send requests. This client only needs to be created
46+
// once, and can be reused for multiple requests. After completing all of your requests, call
47+
// the "close" method on the client to safely clean up any remaining background resources.
48+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
49+
50+
GetBigQueryExportRequest bigQueryExportRequest =
51+
GetBigQueryExportRequest.newBuilder()
52+
.setName(String.format("%s/bigQueryExports/%s", parent, bigQueryExportId))
53+
.build();
54+
55+
BigQueryExport response = client.getBigQueryExport(bigQueryExportRequest);
56+
System.out.printf("Retrieved the BigQuery export: %s", response.getName());
57+
}
58+
}
59+
}
60+
// [END securitycenter_get_bigquery_export]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package bigqueryexport;
18+
19+
// [START securitycenter_list_bigquery_export]
20+
21+
import com.google.cloud.securitycenter.v1.BigQueryExport;
22+
import com.google.cloud.securitycenter.v1.ListBigQueryExportsRequest;
23+
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
24+
import com.google.cloud.securitycenter.v1.SecurityCenterClient.ListBigQueryExportsPagedResponse;
25+
import java.io.IOException;
26+
27+
public class ListBigQueryExports {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(Developer): Modify the following variable values.
31+
32+
// parent: The parent, which owns the collection of BigQuery exports.
33+
// Use any one of the following resource paths:
34+
// - organizations/{organization_id}
35+
// - folders/{folder_id}
36+
// - projects/{project_id}
37+
String parent = String.format("projects/%s", "your-google-cloud-project-id");
38+
39+
listBigQueryExports(parent);
40+
}
41+
42+
// List BigQuery exports in the given parent.
43+
public static void listBigQueryExports(String parent) throws IOException {
44+
// Initialize client that will be used to send requests. This client only needs to be created
45+
// once, and can be reused for multiple requests. After completing all of your requests, call
46+
// the "close" method on the client to safely clean up any remaining background resources.
47+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
48+
49+
ListBigQueryExportsRequest request =
50+
ListBigQueryExportsRequest.newBuilder().setParent(parent).build();
51+
52+
ListBigQueryExportsPagedResponse response = client.listBigQueryExports(request);
53+
54+
System.out.println("Listing BigQuery exports:");
55+
for (BigQueryExport bigQueryExport : response.iterateAll()) {
56+
System.out.println(bigQueryExport.getName());
57+
}
58+
}
59+
}
60+
}
61+
// [END securitycenter_list_bigquery_export]
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package bigqueryexport;
18+
19+
// [START securitycenter_update_bigquery_export]
20+
21+
import com.google.cloud.securitycenter.v1.BigQueryExport;
22+
import com.google.cloud.securitycenter.v1.SecurityCenterClient;
23+
import com.google.cloud.securitycenter.v1.UpdateBigQueryExportRequest;
24+
import com.google.protobuf.FieldMask;
25+
import java.io.IOException;
26+
27+
public class UpdateBigQueryExport {
28+
29+
public static void main(String[] args) throws IOException {
30+
// TODO(Developer): Modify the following variable values.
31+
32+
// parent: Use any one of the following resource paths:
33+
// - organizations/{organization_id}
34+
// - folders/{folder_id}
35+
// - projects/{project_id}
36+
String parent = String.format("projects/%s", "your-google-cloud-project-id");
37+
38+
// filter: Expression that defines the filter to apply across create/update events of findings.
39+
String filter =
40+
"severity=\"LOW\" OR severity=\"MEDIUM\" AND "
41+
+ "category=\"Persistence: IAM Anomalous Grant\" AND "
42+
+ "-resource.type:\"compute\"";
43+
44+
// bigQueryExportId: Unique identifier provided by the client.
45+
// For more info, see:
46+
// https://cloud.google.com/security-command-center/docs/how-to-analyze-findings-in-big-query#export_findings_from_to
47+
String bigQueryExportId = "big-query-export-id";
48+
49+
updateBigQueryExport(parent, filter, bigQueryExportId);
50+
}
51+
52+
// Updates an existing BigQuery export.
53+
public static void updateBigQueryExport(String parent, String filter, String bigQueryExportId)
54+
throws IOException {
55+
// Initialize client that will be used to send requests. This client only needs to be created
56+
// once, and can be reused for multiple requests. After completing all of your requests, call
57+
// the "close" method on the client to safely clean up any remaining background resources.
58+
try (SecurityCenterClient client = SecurityCenterClient.create()) {
59+
60+
// Set the new values for export configuration.
61+
BigQueryExport bigQueryExport =
62+
BigQueryExport.newBuilder()
63+
.setName(String.format("%s/bigQueryExports/%s", parent, bigQueryExportId))
64+
.setFilter(filter)
65+
.build();
66+
67+
UpdateBigQueryExportRequest request =
68+
UpdateBigQueryExportRequest.newBuilder()
69+
.setBigQueryExport(bigQueryExport)
70+
// Set the update mask to specify which properties should be updated.
71+
// If empty, all mutable fields will be updated.
72+
// For more info on constructing field mask path, see the proto or:
73+
// https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask
74+
.setUpdateMask(FieldMask.newBuilder().addPaths("filter").build())
75+
.build();
76+
77+
BigQueryExport response = client.updateBigQueryExport(request);
78+
if (!response.getFilter().equalsIgnoreCase(filter)) {
79+
System.out.println("Failed to update BigQueryExport!");
80+
return;
81+
}
82+
System.out.println("BigQueryExport updated successfully!");
83+
}
84+
}
85+
}
86+
// [END securitycenter_update_bigquery_export]

0 commit comments

Comments
 (0)