-
Notifications
You must be signed in to change notification settings - Fork 13
/
gcp_iam_iterator.py
190 lines (164 loc) · 7.41 KB
/
gcp_iam_iterator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import logging
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from cache_service import CRMProjects, CRMProjectIam, ServiceAccountService, \
ServiceAccountKeyService, BQDataset, BQDatasets, GCSBuckets, GCSBucketACL, \
ServiceManagement
from oauth2client.client import GoogleCredentials
class GcpIamIterator:
def __init__(self, use_cache=True):
credentials = GoogleCredentials.get_application_default()
google_crm_service = build('cloudresourcemanager', 'v1',
credentials=credentials)
google_iam_service = build('iam', 'v1', credentials=credentials)
google_bq_service = build('bigquery', 'v2', credentials=credentials)
google_gcs_service = build('storage', 'v1', credentials=credentials)
google_service_management = build('servicemanagement', 'v1',
credentials=credentials)
self.crm_service = CRMProjects(google_crm_service, use_cache)
self.crm_iam_service = CRMProjectIam(google_crm_service, use_cache)
self.sa_service = ServiceAccountService(google_iam_service, use_cache)
self.sak_service = ServiceAccountKeyService(google_iam_service,
use_cache)
self.datasets_service = BQDatasets(google_bq_service, use_cache)
self.dataset_iam_service = BQDataset(google_bq_service, use_cache)
self.gcs_service = GCSBuckets(google_gcs_service, use_cache)
self.gcs_acl_service = GCSBucketACL(google_gcs_service, use_cache)
self.service_management = ServiceManagement(google_service_management,
use_cache)
def list_projects(self):
response = self.crm_service.get()
while response:
for project in response['projects']:
yield project
if 'nextPageToken' in response:
response = self.crm_service.get(
nextPageToken=response['nextPageToken'])
else:
response = None
def list_project_iam(self, project_id):
try:
iam_policy = self.crm_iam_service.get(project_id=project_id)
except HttpError as e:
if e.resp.status == 403:
logging.warning("403 received for project_id: {0}, content: {1}"
.format(project_id, e.content))
return
else:
raise e
if 'bindings' in iam_policy:
for binding in iam_policy['bindings']:
yield binding
def list_service_accounts(self, project_id):
try:
response = self.sa_service.get(project_id=project_id)
except HttpError as e:
if e.resp.status == 404:
logging.info("404 received for project_id: {0}, content: {1}"
.format(project_id, e.content))
return
elif e.resp.status == 403:
logging.warning("403 received for project_id: {0}, content: {1}"
.format(project_id, e.content))
return
else:
raise e
while response:
for account in response['accounts']:
yield account
if 'nextPageToken' in response:
response = self.sa_service \
.get(project_id=project_id,
nextPageToken=response['nextPageToken'])
else:
response = None
def list_service_account_keys(self, email):
response = self.sak_service.get(email=email)
if 'keys' in response:
for key in response['keys']:
yield key
def list_datasets(self, project_id):
response = None
try:
response = self.datasets_service.get(project_id=project_id)
except HttpError as e:
if e.resp.status == 400:
logging.warning("400 received for project_id: {0}, content: {1}"
.format(project_id, e.content))
return
while response:
if 'datasets' in response:
for dataset in response['datasets']:
yield dataset
if 'nextPageToken' in response:
response = self.datasets_service.get(project_id=project_id,
pageToken=response[
'nextPageToken'])
else:
response = None
def list_dataset_access(self, project_id, dataset_id):
response = self.dataset_iam_service.get(project_id=project_id,
dataset_id=dataset_id)
if 'access' in response:
for access in response['access']:
yield access
def list_buckets(self, project_id):
try:
response = self.gcs_service.get(project_id=project_id)
except HttpError as e:
if e.resp.status == 400:
logging.warning(
"400 received during listing buckets for project_id: {0}, "
"content: {1}".format(project_id, e.content))
return
elif e.resp.status == 403:
logging.warning(
"403 received during listing buckets for project_id: {0}, "
"content: {1}".format(project_id, e.content))
return
else:
raise e
while response:
if 'items' in response:
for bucket in response['items']:
yield bucket
if 'nextPageToken' in response:
response = self.gcs_service.get(project_id=project_id,
pageToken=response[
'nextPageToken'])
else:
response = None
def list_bucket_access(self, bucket_id):
try:
response = self.gcs_acl_service.get(bucket_id=bucket_id)
except HttpError as e:
if e.resp.status == 403:
logging.warning("403 received for bucket_id: {0}, content: {1}"
.format(bucket_id, e.content))
return
else:
raise e
for item in response['items']:
yield item
def list_enabled_services(self, project_id):
try:
response = self.service_management.get(project_id=project_id)
except HttpError as e:
if e.resp.status == 400 or e.resp.status == 403 \
or e.resp.status == 404:
logging.warning(
"{0} received during listing services for project_id: {1}, "
"content: {2}".format(e.resp.status, project_id, e.content))
return
else:
raise e
while response:
if 'services' in response:
for service in response['services']:
yield service
if 'nextPageToken' in response:
response = self.service_management.get(project_id=project_id,
pageToken=response[
'nextPageToken'])
else:
response = None