forked from GoogleCloudPlatform/java-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add bigquery export samples #7
Closed
+638
−0
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e11a2a6
feat: add bigquery export samples
rafaelMurata 280f187
improve readability
rafaelMurata 8fbbd58
change mockito client
rafaelMurata 9dfa50a
Update security-command-center/snippets/src/main/java/vtwo/bigquery/D…
rafaelMurata 1c44b55
Update security-command-center/snippets/src/main/java/vtwo/bigquery/G…
rafaelMurata a77cf41
Update security-command-center/snippets/src/main/java/vtwo/bigquery/L…
rafaelMurata 6ac5a92
Update security-command-center/snippets/src/main/java/vtwo/bigquery/U…
rafaelMurata 433599c
change snippet response and mock static
rafaelMurata 7dc6968
add ByteArrayOutputStream
rafaelMurata f23b9a3
try mock client null
rafaelMurata 7789c3f
try back mockStatic
rafaelMurata File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
security-command-center/snippets/src/main/java/vtwo/bigquery/CreateBigQueryExport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2024 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 vtwo.bigquery; | ||
|
||
// [START securitycenter_create_bigquery_export_v2] | ||
|
||
import com.google.cloud.securitycenter.v2.BigQueryExport; | ||
import com.google.cloud.securitycenter.v2.CreateBigQueryExportRequest; | ||
import com.google.cloud.securitycenter.v2.OrganizationLocationName; | ||
import com.google.cloud.securitycenter.v2.SecurityCenterClient; | ||
import java.io.IOException; | ||
import java.util.UUID; | ||
|
||
public class CreateBigQueryExport { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(Developer): Modify the following variable values. | ||
// organizationId: Google Cloud Organization id. | ||
String organizationId = "{google-cloud-organization-id}"; | ||
|
||
// projectId: Google Cloud Project id. | ||
String projectId = "{your-project}"; | ||
|
||
// Specify the location. | ||
String location = "global"; | ||
|
||
// filter: Expression that defines the filter to apply across create/update events of findings. | ||
String filter = "severity=\"LOW\" OR severity=\"MEDIUM\""; | ||
|
||
// bigQueryDatasetId: The BigQuery dataset to write findings' updates to. | ||
String bigQueryDatasetId = "{bigquery-dataset-id}"; | ||
|
||
// bigQueryExportId: Unique identifier provided by the client. | ||
// For more info, see: | ||
// https://cloud.google.com/security-command-center/docs/how-to-analyze-findings-in-big-query#export_findings_from_to | ||
String bigQueryExportId = "default-" + UUID.randomUUID().toString().split("-")[0]; | ||
|
||
createBigQueryExport(organizationId, location, projectId, filter, bigQueryDatasetId, | ||
bigQueryExportId); | ||
} | ||
|
||
// Create export configuration to export findings from a project to a BigQuery dataset. | ||
// Optionally specify filter to export certain findings only. | ||
public static BigQueryExport createBigQueryExport(String organizationId, String location, | ||
String projectId, String filter, String bigQueryDatasetId, String bigQueryExportId) | ||
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. | ||
try (SecurityCenterClient client = SecurityCenterClient.create()) { | ||
OrganizationLocationName organizationName = OrganizationLocationName.of(organizationId, | ||
location); | ||
// Create the BigQuery export configuration. | ||
BigQueryExport bigQueryExport = | ||
BigQueryExport.newBuilder() | ||
.setDescription( | ||
"Export low and medium findings if the compute resource " | ||
+ "has an IAM anomalous grant") | ||
.setFilter(filter) | ||
.setDataset(String.format("projects/%s/datasets/%s", projectId, bigQueryDatasetId)) | ||
.build(); | ||
|
||
CreateBigQueryExportRequest bigQueryExportRequest = | ||
CreateBigQueryExportRequest.newBuilder() | ||
.setParent(organizationName.toString()) | ||
.setBigQueryExport(bigQueryExport) | ||
.setBigQueryExportId(bigQueryExportId) | ||
.build(); | ||
|
||
// Create the export request. | ||
BigQueryExport response = client.createBigQueryExport(bigQueryExportRequest); | ||
|
||
System.out.printf("BigQuery export request created successfully: %s\n", response.getName()); | ||
return response; | ||
} | ||
} | ||
} | ||
// [END securitycenter_create_bigquery_export_v2] |
65 changes: 65 additions & 0 deletions
65
security-command-center/snippets/src/main/java/vtwo/bigquery/DeleteBigQueryExport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2024 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 vtwo.bigquery; | ||
|
||
// [START securitycenter_delete_bigquery_export_v2] | ||
|
||
import com.google.cloud.securitycenter.v2.BigQueryExportName; | ||
import com.google.cloud.securitycenter.v2.DeleteBigQueryExportRequest; | ||
import com.google.cloud.securitycenter.v2.SecurityCenterClient; | ||
import java.io.IOException; | ||
|
||
public class DeleteBigQueryExport { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(Developer): Modify the following variable values. | ||
// organizationId: Google Cloud Organization id. | ||
String organizationId = "{google-cloud-organization-id}"; | ||
|
||
// Specify the location to list the findings. | ||
String location = "global"; | ||
|
||
// bigQueryExportId: Unique identifier that is used to identify the export. | ||
String bigQueryExportId = "{bigquery-export-id}"; | ||
|
||
deleteBigQueryExport(organizationId, location, bigQueryExportId); | ||
} | ||
|
||
// Delete an existing BigQuery export. | ||
public static void deleteBigQueryExport(String organizationId, String location, | ||
String bigQueryExportId) | ||
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. | ||
try (SecurityCenterClient client = SecurityCenterClient.create()) { | ||
// Optionally BigQueryExportName or String can be used | ||
// String bigQueryExportName = String.format("organizations/%s/locations/%s | ||
// /bigQueryExports/%s",organizationId,location, bigQueryExportId); | ||
BigQueryExportName bigQueryExportName = BigQueryExportName.of(organizationId, location, | ||
bigQueryExportId); | ||
|
||
DeleteBigQueryExportRequest bigQueryExportRequest = | ||
DeleteBigQueryExportRequest.newBuilder() | ||
.setName(bigQueryExportName.toString()) | ||
.build(); | ||
|
||
client.deleteBigQueryExport(bigQueryExportRequest); | ||
System.out.printf("BigQuery export request deleted successfully: %s", bigQueryExportId); | ||
} | ||
} | ||
} | ||
// [END securitycenter_delete_bigquery_export_v2] |
64 changes: 64 additions & 0 deletions
64
security-command-center/snippets/src/main/java/vtwo/bigquery/GetBigQueryExport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2024 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 vtwo.bigquery; | ||
|
||
// [START securitycenter_get_bigquery_export_v2] | ||
|
||
import com.google.cloud.securitycenter.v2.BigQueryExport; | ||
import com.google.cloud.securitycenter.v2.BigQueryExportName; | ||
import com.google.cloud.securitycenter.v2.GetBigQueryExportRequest; | ||
import com.google.cloud.securitycenter.v2.SecurityCenterClient; | ||
import java.io.IOException; | ||
|
||
public class GetBigQueryExport { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(Developer): Modify the following variable values. | ||
// organizationId: Google Cloud Organization id. | ||
String organizationId = "{google-cloud-organization-id}"; | ||
|
||
// Specify the location to list the findings. | ||
String location = "global"; | ||
|
||
// bigQueryExportId: Unique identifier that is used to identify the export. | ||
String bigQueryExportId = "{bigquery-export-id}"; | ||
|
||
getBigQueryExport(organizationId, location, bigQueryExportId); | ||
} | ||
|
||
// Retrieve an existing BigQuery export. | ||
public static BigQueryExport getBigQueryExport(String organizationId, String location, | ||
String bigQueryExportId) 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. | ||
try (SecurityCenterClient client = SecurityCenterClient.create()) { | ||
|
||
BigQueryExportName bigQueryExportName = BigQueryExportName.of(organizationId, location, | ||
bigQueryExportId); | ||
|
||
GetBigQueryExportRequest bigQueryExportRequest = | ||
GetBigQueryExportRequest.newBuilder() | ||
.setName(bigQueryExportName.toString()) | ||
.build(); | ||
|
||
BigQueryExport response = client.getBigQueryExport(bigQueryExportRequest); | ||
System.out.printf("Retrieved the BigQuery export: %s", response.getName()); | ||
return response; | ||
} | ||
} | ||
} | ||
// [END securitycenter_get_bigquery_export_v2] |
64 changes: 64 additions & 0 deletions
64
security-command-center/snippets/src/main/java/vtwo/bigquery/ListBigQueryExports.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright 2024 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 vtwo.bigquery; | ||
|
||
// [START securitycenter_list_bigquery_export_v2] | ||
|
||
import com.google.cloud.securitycenter.v2.BigQueryExport; | ||
import com.google.cloud.securitycenter.v2.ListBigQueryExportsRequest; | ||
import com.google.cloud.securitycenter.v2.OrganizationLocationName; | ||
import com.google.cloud.securitycenter.v2.SecurityCenterClient; | ||
import com.google.cloud.securitycenter.v2.SecurityCenterClient.ListBigQueryExportsPagedResponse; | ||
import java.io.IOException; | ||
|
||
public class ListBigQueryExports { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(Developer): Modify the following variable values. | ||
// organizationId: Google Cloud Organization id. | ||
String organizationId = "{google-cloud-organization-id}"; | ||
|
||
// Specify the location to list the findings. | ||
String location = "global"; | ||
|
||
listBigQueryExports(organizationId, location); | ||
} | ||
|
||
// List BigQuery exports in the given parent. | ||
public static ListBigQueryExportsPagedResponse listBigQueryExports(String organizationId, | ||
String location) 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. | ||
try (SecurityCenterClient client = SecurityCenterClient.create()) { | ||
OrganizationLocationName organizationName = OrganizationLocationName.of(organizationId, | ||
location); | ||
|
||
ListBigQueryExportsRequest request = ListBigQueryExportsRequest.newBuilder() | ||
.setParent(organizationName.toString()) | ||
.build(); | ||
|
||
ListBigQueryExportsPagedResponse response = client.listBigQueryExports(request); | ||
|
||
System.out.println("Listing BigQuery exports:"); | ||
for (BigQueryExport bigQueryExport : response.iterateAll()) { | ||
System.out.println(bigQueryExport.getName()); | ||
} | ||
return response; | ||
} | ||
} | ||
} | ||
// [END securitycenter_list_bigquery_export_v2] |
90 changes: 90 additions & 0 deletions
90
security-command-center/snippets/src/main/java/vtwo/bigquery/UpdateBigQueryExport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2024 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 vtwo.bigquery; | ||
|
||
// [START securitycenter_update_bigquery_export_v2] | ||
|
||
import com.google.cloud.securitycenter.v2.BigQueryExport; | ||
import com.google.cloud.securitycenter.v2.BigQueryExportName; | ||
import com.google.cloud.securitycenter.v2.SecurityCenterClient; | ||
import com.google.cloud.securitycenter.v2.UpdateBigQueryExportRequest; | ||
import com.google.protobuf.FieldMask; | ||
import java.io.IOException; | ||
|
||
public class UpdateBigQueryExport { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO(Developer): Modify the following variable values. | ||
// organizationId: Google Cloud Organization id. | ||
String organizationId = "{google-cloud-organization-id}"; | ||
|
||
// Specify the location to list the findings. | ||
String location = "global"; | ||
|
||
// filter: Expression that defines the filter to apply across create/update events of findings. | ||
String filter = | ||
"severity=\"LOW\" OR severity=\"MEDIUM\" AND " | ||
+ "category=\"Persistence: IAM Anomalous Grant\" AND " | ||
+ "-resource.type:\"compute\""; | ||
|
||
// bigQueryExportId: Unique identifier provided by the client. | ||
// For more info, see: | ||
// https://cloud.google.com/security-command-center/docs/how-to-analyze-findings-in-big-query#export_findings_from_to | ||
String bigQueryExportId = "{bigquery-export-id}"; | ||
|
||
updateBigQueryExport(organizationId, location, filter, bigQueryExportId); | ||
} | ||
|
||
// Updates an existing BigQuery export. | ||
public static BigQueryExport updateBigQueryExport(String organizationId, String location, | ||
String filter, String bigQueryExportId) 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. | ||
try (SecurityCenterClient client = SecurityCenterClient.create()) { | ||
// Optionally BigQueryExportName or String can be used | ||
// String bigQueryExportName = String.format("organizations/%s/locations/%s | ||
// /bigQueryExports/%s",organizationId,location, bigQueryExportId); | ||
BigQueryExportName bigQueryExportName = BigQueryExportName.of(organizationId, location, | ||
bigQueryExportId); | ||
|
||
// Set the new values for export configuration. | ||
BigQueryExport bigQueryExport = | ||
BigQueryExport.newBuilder() | ||
.setName(bigQueryExportName.toString()) | ||
.setDescription("Updated description.") | ||
.setFilter(filter) | ||
.build(); | ||
|
||
UpdateBigQueryExportRequest request = | ||
UpdateBigQueryExportRequest.newBuilder() | ||
.setBigQueryExport(bigQueryExport) | ||
// Set the update mask to specify which properties should be updated. | ||
// If empty, all mutable fields will be updated. | ||
// For more info on constructing field mask path, see the proto or: | ||
// https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask | ||
.setUpdateMask(FieldMask.newBuilder() | ||
.addPaths("filter") | ||
.addPaths("description").build()) | ||
.build(); | ||
|
||
BigQueryExport response = client.updateBigQueryExport(request); | ||
System.out.println("BigQueryExport updated successfully!"); | ||
return response; | ||
} | ||
} | ||
} | ||
// [END securitycenter_update_bigquery_export_v2] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This tag should enclose only the function
public static void createBigQueryExport(
, no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it makes sense if we follow the same example we used in GetIamPolicies:
https://github.com/GoogleCloudPlatform/java-docs-samples/blob/main/security-command-center/snippets/src/main/java/vtwo/iam/GetIamPolicies.java
but I'm worried about the change, as the same PR is already approved in the same syntax.
GoogleCloudPlatform#9291
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, no problem