Skip to content

Commit ab5dd12

Browse files
samples: Add analyze_iam_policy and anlayze_iam_policy_longrunning sa… (#132)
* samples: Add analyze_iam_policy and anlayze_iam_policy_longrunning samples
1 parent 786a70e commit ab5dd12

4 files changed

+262
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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(project_id):
22+
# [START asset_quickstart_analyze_iam_policy]
23+
from google.cloud import asset_v1
24+
25+
# TODO project_id = 'Your Google Cloud Project ID'
26+
27+
client = asset_v1.AssetServiceClient()
28+
parent = "projects/{}".format(project_id)
29+
30+
# Build analysis query
31+
analysis_query = asset_v1.IamPolicyAnalysisQuery()
32+
analysis_query.scope = parent
33+
analysis_query.resource_selector.full_resource_name = f"//cloudresourcemanager.googleapis.com/{parent}"
34+
analysis_query.options.expand_groups = True
35+
analysis_query.options.output_group_edges = True
36+
37+
response = client.analyze_iam_policy(
38+
request={"analysis_query": analysis_query}
39+
)
40+
print(response)
41+
# [END asset_quickstart_analyze_iam_policy]
42+
43+
44+
if __name__ == "__main__":
45+
46+
parser = argparse.ArgumentParser(
47+
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
48+
)
49+
parser.add_argument("project_id", help="Your Google Cloud project ID")
50+
51+
args = parser.parse_args()
52+
53+
analyze_iam_policy(args.project_id)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
import os
18+
19+
import quickstart_analyzeiampolicy
20+
21+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
22+
23+
24+
def test_analyze_iam_policy(capsys):
25+
quickstart_analyzeiampolicy.analyze_iam_policy(PROJECT)
26+
out, _ = capsys.readouterr()
27+
assert "fully_explored: true" in out
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
import os
18+
import uuid
19+
20+
from google.cloud import bigquery
21+
from google.cloud import storage
22+
23+
import pytest
24+
25+
import quickstart_analyzeiampolicylongrunning
26+
27+
PROJECT = os.environ["GOOGLE_CLOUD_PROJECT"]
28+
BUCKET = "analysis-{}".format(int(uuid.uuid4()))
29+
DATASET = "analysis_{}".format(int(uuid.uuid4()))
30+
31+
32+
@pytest.fixture(scope="module")
33+
def storage_client():
34+
yield storage.Client()
35+
36+
37+
@pytest.fixture(scope="module")
38+
def bigquery_client():
39+
yield bigquery.Client()
40+
41+
42+
@pytest.fixture(scope="module")
43+
def analysis_bucket(storage_client):
44+
bucket = storage_client.create_bucket(BUCKET)
45+
46+
yield BUCKET
47+
48+
try:
49+
bucket.delete(force=True)
50+
except Exception as e:
51+
print("Failed to delete bucket{}".format(BUCKET))
52+
raise e
53+
54+
55+
@pytest.fixture(scope="module")
56+
def dataset(bigquery_client):
57+
dataset_id = "{}.{}".format(PROJECT, DATASET)
58+
dataset = bigquery.Dataset(dataset_id)
59+
dataset.location = "US"
60+
dataset = bigquery_client.create_dataset(dataset)
61+
62+
yield DATASET
63+
64+
bigquery_client.delete_dataset(
65+
dataset_id, delete_contents=True, not_found_ok=False)
66+
67+
68+
def test_analyze_iam_policy_longrunning(analysis_bucket, dataset, capsys):
69+
dump_file_path = "gs://{}/analysis-dump.txt".format(analysis_bucket)
70+
quickstart_analyzeiampolicylongrunning.analyze_iam_policy_longrunning_gcs(PROJECT, dump_file_path)
71+
out, _ = capsys.readouterr()
72+
assert "True" in out
73+
74+
dataset_id = "projects/{}/datasets/{}".format(PROJECT, dataset)
75+
quickstart_analyzeiampolicylongrunning.analyze_iam_policy_longrunning_bigquery(PROJECT, dataset_id, "analysis_")
76+
out, _ = capsys.readouterr()
77+
assert "True" in out

0 commit comments

Comments
 (0)