|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Copyright 2022 Google LLC |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +"""Snippets on exporting findings from Security Command Center to BigQuery.""" |
| 18 | + |
| 19 | + |
| 20 | +# [START securitycenter_create_bigquery_export] |
| 21 | + |
| 22 | + |
| 23 | +def create_bigquery_export( |
| 24 | + parent: str, export_filter: str, bigquery_dataset_id: str, bigquery_export_id: str |
| 25 | +): |
| 26 | + |
| 27 | + from google.cloud import securitycenter |
| 28 | + |
| 29 | + """ |
| 30 | + Create export configuration to export findings from a project to a BigQuery dataset. |
| 31 | + Optionally specify filter to export certain findings only. |
| 32 | +
|
| 33 | + Args: |
| 34 | + parent: Use any one of the following resource paths: |
| 35 | + - organizations/{organization_id} |
| 36 | + - folders/{folder_id} |
| 37 | + - projects/{project_id} |
| 38 | + export_filter: Expression that defines the filter to apply across create/update events of findings. |
| 39 | + bigquery_dataset_id: The BigQuery dataset to write findings' updates to. |
| 40 | + bigquery_export_id: Unique identifier provided by the client. |
| 41 | + - example id: f"default-{str(uuid.uuid4()).split('-')[0]}" |
| 42 | + For more info, see: |
| 43 | + https://cloud.google.com/security-command-center/docs/how-to-analyze-findings-in-big-query#export_findings_from_to |
| 44 | + """ |
| 45 | + client = securitycenter.SecurityCenterClient() |
| 46 | + |
| 47 | + # Create the BigQuery export configuration. |
| 48 | + bigquery_export = securitycenter.BigQueryExport() |
| 49 | + bigquery_export.description = "Export low and medium findings if the compute resource has an IAM anomalous grant" |
| 50 | + bigquery_export.filter = export_filter |
| 51 | + bigquery_export.dataset = f"{parent}/datasets/{bigquery_dataset_id}" |
| 52 | + |
| 53 | + request = securitycenter.CreateBigQueryExportRequest() |
| 54 | + request.parent = parent |
| 55 | + request.big_query_export = bigquery_export |
| 56 | + request.big_query_export_id = bigquery_export_id |
| 57 | + |
| 58 | + # Create the export request. |
| 59 | + response = client.create_big_query_export(request) |
| 60 | + |
| 61 | + print(f"BigQuery export request created successfully: {response.name}\n") |
| 62 | + |
| 63 | + |
| 64 | +# [END securitycenter_create_bigquery_export] |
| 65 | + |
| 66 | + |
| 67 | +# [START securitycenter_get_bigquery_export] |
| 68 | +def get_bigquery_export(parent: str, bigquery_export_id: str): |
| 69 | + from google.cloud import securitycenter |
| 70 | + |
| 71 | + """ |
| 72 | + Retrieve an existing BigQuery export. |
| 73 | + Args: |
| 74 | + parent: Use any one of the following resource paths: |
| 75 | + - organizations/{organization_id} |
| 76 | + - folders/{folder_id} |
| 77 | + - projects/{project_id} |
| 78 | + bigquery_export_id: Unique identifier that is used to identify the export. |
| 79 | + """ |
| 80 | + |
| 81 | + client = securitycenter.SecurityCenterClient() |
| 82 | + |
| 83 | + request = securitycenter.GetBigQueryExportRequest() |
| 84 | + request.name = f"{parent}/bigQueryExports/{bigquery_export_id}" |
| 85 | + |
| 86 | + response = client.get_big_query_export(request) |
| 87 | + print(f"Retrieved the BigQuery export: {response.name}") |
| 88 | + |
| 89 | + |
| 90 | +# [END securitycenter_get_bigquery_export] |
| 91 | + |
| 92 | + |
| 93 | +# [START securitycenter_list_bigquery_export] |
| 94 | +def list_bigquery_exports(parent: str): |
| 95 | + from google.cloud import securitycenter |
| 96 | + |
| 97 | + """ |
| 98 | + List BigQuery exports in the given parent. |
| 99 | + Args: |
| 100 | + parent: The parent which owns the collection of BigQuery exports. |
| 101 | + Use any one of the following resource paths: |
| 102 | + - organizations/{organization_id} |
| 103 | + - folders/{folder_id} |
| 104 | + - projects/{project_id} |
| 105 | + """ |
| 106 | + |
| 107 | + client = securitycenter.SecurityCenterClient() |
| 108 | + |
| 109 | + request = securitycenter.ListBigQueryExportsRequest() |
| 110 | + request.parent = parent |
| 111 | + |
| 112 | + response = client.list_big_query_exports(request) |
| 113 | + |
| 114 | + print("Listing BigQuery exports:") |
| 115 | + for bigquery_export in response: |
| 116 | + print(bigquery_export.name) |
| 117 | + |
| 118 | + |
| 119 | +# [END securitycenter_list_bigquery_export] |
| 120 | + |
| 121 | + |
| 122 | +# [START securitycenter_update_bigquery_export] |
| 123 | +def update_bigquery_export(parent: str, export_filter: str, bigquery_export_id: str): |
| 124 | + """ |
| 125 | + Updates an existing BigQuery export. |
| 126 | + Args: |
| 127 | + parent: Use any one of the following resource paths: |
| 128 | + - organizations/{organization_id} |
| 129 | + - folders/{folder_id} |
| 130 | + - projects/{project_id} |
| 131 | + export_filter: Expression that defines the filter to apply across create/update events of findings. |
| 132 | + bigquery_export_id: Unique identifier provided by the client. |
| 133 | + For more info, see: |
| 134 | + https://cloud.google.com/security-command-center/docs/how-to-analyze-findings-in-big-query#export_findings_from_to |
| 135 | + """ |
| 136 | + from google.cloud import securitycenter |
| 137 | + from google.protobuf import field_mask_pb2 |
| 138 | + |
| 139 | + client = securitycenter.SecurityCenterClient() |
| 140 | + |
| 141 | + # Set the new values for export configuration. |
| 142 | + bigquery_export = securitycenter.BigQueryExport() |
| 143 | + bigquery_export.name = f"{parent}/bigQueryExports/{bigquery_export_id}" |
| 144 | + bigquery_export.filter = export_filter |
| 145 | + |
| 146 | + # Field mask to only update the export filter. |
| 147 | + # Set the update mask to specify which properties should be updated. |
| 148 | + # If empty, all mutable fields will be updated. |
| 149 | + # For more info on constructing field mask path, see the proto or: |
| 150 | + # https://googleapis.dev/python/protobuf/latest/google/protobuf/field_mask_pb2.html |
| 151 | + field_mask = field_mask_pb2.FieldMask(paths=["filter"]) |
| 152 | + |
| 153 | + request = securitycenter.UpdateBigQueryExportRequest() |
| 154 | + request.big_query_export = bigquery_export |
| 155 | + request.update_mask = field_mask |
| 156 | + |
| 157 | + response = client.update_big_query_export(request) |
| 158 | + |
| 159 | + if response.filter != export_filter: |
| 160 | + print("Failed to update BigQueryExport!") |
| 161 | + return |
| 162 | + print("BigQueryExport updated successfully!") |
| 163 | + |
| 164 | + |
| 165 | +# [END securitycenter_update_bigquery_export] |
| 166 | + |
| 167 | + |
| 168 | +# [START securitycenter_delete_bigquery_export] |
| 169 | +def delete_bigquery_export(parent: str, bigquery_export_id: str): |
| 170 | + """ |
| 171 | + Delete an existing BigQuery export. |
| 172 | + Args: |
| 173 | + parent: Use any one of the following resource paths: |
| 174 | + - organizations/{organization_id} |
| 175 | + - folders/{folder_id} |
| 176 | + - projects/{project_id} |
| 177 | + bigquery_export_id: Unique identifier that is used to identify the export. |
| 178 | + """ |
| 179 | + from google.cloud import securitycenter |
| 180 | + |
| 181 | + client = securitycenter.SecurityCenterClient() |
| 182 | + |
| 183 | + request = securitycenter.DeleteBigQueryExportRequest() |
| 184 | + request.name = f"{parent}/bigQueryExports/{bigquery_export_id}" |
| 185 | + |
| 186 | + client.delete_big_query_export(request) |
| 187 | + print(f"BigQuery export request deleted successfully: {bigquery_export_id}") |
| 188 | + |
| 189 | + |
| 190 | +# [END securitycenter_delete_bigquery_export] |
0 commit comments