|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Copyright 2020 Google LLC. All Rights Reserved. |
| 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 | +# http://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 | + |
| 18 | +import argparse |
| 19 | + |
| 20 | + |
| 21 | +def analyze_iam_policy_longrunning_gcs(project_id, dump_file_path): |
| 22 | + # [START asset_quickstart_analyze_iam_policy_longrunning_gcs] |
| 23 | + from google.cloud import asset_v1 |
| 24 | + |
| 25 | + # TODO project_id = 'Your Google Cloud Project ID' |
| 26 | + # TODO dump_file_path = 'Your analysis dump file path' |
| 27 | + |
| 28 | + client = asset_v1.AssetServiceClient() |
| 29 | + parent = "projects/{}".format(project_id) |
| 30 | + |
| 31 | + # Build analysis query |
| 32 | + analysis_query = asset_v1.IamPolicyAnalysisQuery() |
| 33 | + analysis_query.scope = parent |
| 34 | + analysis_query.resource_selector.full_resource_name = f"//cloudresourcemanager.googleapis.com/{parent}" |
| 35 | + analysis_query.options.expand_groups = True |
| 36 | + analysis_query.options.output_group_edges = True |
| 37 | + |
| 38 | + output_config = asset_v1.IamPolicyAnalysisOutputConfig() |
| 39 | + output_config.gcs_destination.uri = dump_file_path |
| 40 | + operation = client.analyze_iam_policy_longrunning( |
| 41 | + request={"analysis_query": analysis_query, "output_config": output_config} |
| 42 | + ) |
| 43 | + |
| 44 | + operation.result(300) |
| 45 | + print(operation.done()) |
| 46 | + # [END asset_quickstart_analyze_iam_policy_longrunning_gcs] |
| 47 | + |
| 48 | + |
| 49 | +def analyze_iam_policy_longrunning_bigquery(project_id, dataset, table): |
| 50 | + # [START asset_quickstart_analyze_iam_policy_longrunning_bigquery] |
| 51 | + from google.cloud import asset_v1 |
| 52 | + |
| 53 | + # TODO project_id = 'Your Google Cloud Project ID' |
| 54 | + # TODO dataset = 'Your BigQuery dataset path' |
| 55 | + # TODO table = 'Your BigQuery table name' |
| 56 | + |
| 57 | + client = asset_v1.AssetServiceClient() |
| 58 | + parent = "projects/{}".format(project_id) |
| 59 | + |
| 60 | + # Build analysis query |
| 61 | + analysis_query = asset_v1.IamPolicyAnalysisQuery() |
| 62 | + analysis_query.scope = parent |
| 63 | + analysis_query.resource_selector.full_resource_name = f"//cloudresourcemanager.googleapis.com/{parent}" |
| 64 | + analysis_query.options.expand_groups = True |
| 65 | + analysis_query.options.output_group_edges = True |
| 66 | + |
| 67 | + output_config = asset_v1.IamPolicyAnalysisOutputConfig() |
| 68 | + output_config.bigquery_destination.dataset = dataset |
| 69 | + output_config.bigquery_destination.table_prefix = table |
| 70 | + output_config.bigquery_destination.write_disposition = "WRITE_TRUNCATE" |
| 71 | + operation = client.analyze_iam_policy_longrunning( |
| 72 | + request={"analysis_query": analysis_query, "output_config": output_config} |
| 73 | + ) |
| 74 | + |
| 75 | + operation.result(300) |
| 76 | + print(operation.done()) |
| 77 | + # [END asset_quickstart_analyze_iam_policy_longrunning_bigquery] |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + |
| 82 | + parser = argparse.ArgumentParser( |
| 83 | + description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter |
| 84 | + ) |
| 85 | + parser.add_argument("project_id", help="Your Google Cloud project ID") |
| 86 | + parser.add_argument( |
| 87 | + "dump_file_path", |
| 88 | + help="The GCS file that the analysis results will be dumped to, " |
| 89 | + "e.g.: gs://<bucket-name>/analysis_dump_file", |
| 90 | + ) |
| 91 | + parser.add_argument( |
| 92 | + "dataset", |
| 93 | + help="The BigQuery dataset that analysis results will be exported to, " |
| 94 | + "e.g.: my_dataset", |
| 95 | + ) |
| 96 | + parser.add_argument( |
| 97 | + "table_prefix", |
| 98 | + help="The prefix of the BigQuery table that analysis results will be exported to, " |
| 99 | + "e.g.: my_table", |
| 100 | + ) |
| 101 | + |
| 102 | + args = parser.parse_args() |
| 103 | + |
| 104 | + analyze_iam_policy_longrunning_gcs(args.project_id, args.dump_file_path) |
| 105 | + analyze_iam_policy_longrunning_bigquery(args.project_id, args.dataset, args.table_prefix) |
0 commit comments