gcp-platforms-auto 0.8.3__py3-none-any.whl → 0.8.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -14,7 +14,9 @@ from .db import (
14
14
  Base
15
15
  )
16
16
  from .iam import (
17
- check_user_has_role_in_project,
17
+ check_user_has_role_in_project,
18
+ check_service_account_has_role_in_project,
19
+ check_group_has_role_in_project,
18
20
  get_projects_with_role
19
21
  )
20
22
 
gcp_platforms_auto/iam.py CHANGED
@@ -15,49 +15,179 @@ logger = logging.getLogger("uvicorn")
15
15
  logger.setLevel(logging.INFO)
16
16
 
17
17
 
18
- def _get_identity_string(email: str) -> str:
18
+ def check_user_has_role_in_project(
19
+ project_id: str,
20
+ user_email: str,
21
+ organization_id: str,
22
+ role: str = "roles/owner",
23
+ expand_groups: bool = True
24
+ ) -> bool:
19
25
  """
20
- Helper function to construct the proper identity string.
21
- Automatically detects if the email is a service account or user.
26
+ Check if a user has a specific role in a GCP project.
22
27
 
23
28
  Args:
24
- email: Email address (user or service account)
29
+ project_id: GCP project ID (e.g., 'sky-starfi-mam-res-gcpro-1')
30
+ user_email: Email of the user to check (e.g., 'oshasha10@gcporg.com')
31
+ organization_id: GCP organization ID (e.g., '111111111111')
32
+ role: Role to check (e.g., 'roles/owner', 'roles/editor')
33
+ expand_groups: Whether to expand group memberships (default: True)
25
34
 
26
35
  Returns:
27
- str: Properly formatted identity string
36
+ bool: True if the user has the role in the project, False otherwise
37
+
38
+ Example:
39
+ >>> has_access = check_user_has_role_in_project(
40
+ ... project_id='sky-starfi-mam-res-gcpro-1',
41
+ ... user_email='oshasha10@gcporg.com',
42
+ ... organization_id='111111111111',
43
+ ... role='roles/owner'
44
+ ... )
28
45
  """
29
- if ".gserviceaccount.com" in email.lower():
30
- return f"serviceAccount:{email}"
31
- else:
32
- return f"user:{email}"
46
+ client = asset_v1.AssetServiceClient()
47
+
48
+ # Construct the full resource name
49
+ scope = f"organizations/{organization_id}"
50
+ full_resource_name = f"//cloudresourcemanager.googleapis.com/projects/{project_id}"
51
+ identity = f"user:{user_email}"
52
+
53
+ # Build the request
54
+ request = asset_v1.AnalyzeIamPolicyRequest(
55
+ analysis_query=asset_v1.IamPolicyAnalysisQuery(
56
+ scope=scope,
57
+ resource_selector=asset_v1.IamPolicyAnalysisQuery.ResourceSelector(
58
+ full_resource_name=full_resource_name
59
+ ),
60
+ identity_selector=asset_v1.IamPolicyAnalysisQuery.IdentitySelector(
61
+ identity=identity
62
+ ),
63
+ access_selector=asset_v1.IamPolicyAnalysisQuery.AccessSelector(
64
+ roles=[role]
65
+ ),
66
+ options=asset_v1.IamPolicyAnalysisQuery.Options(
67
+ expand_groups=expand_groups,
68
+ expand_roles=True,
69
+ )
70
+ )
71
+ )
72
+
73
+ try:
74
+ # Execute the analysis
75
+ logger.info(f"Checking if user {user_email} has role {role} in project {project_id}")
76
+ response = client.analyze_iam_policy(request=request)
77
+
78
+ # Check if any results were returned
79
+ if response.main_analysis and response.main_analysis.analysis_results:
80
+ logger.info(f"User {user_email} has role {role} in project {project_id}")
81
+ return True
82
+
83
+ logger.info(f"User {user_email} does not have role {role} in project {project_id}")
84
+ return False
85
+
86
+ except Exception as e:
87
+ logger.exception(f"Error analyzing IAM policy: {e}")
88
+ raise
33
89
 
34
90
 
35
- def check_user_has_role_in_project(
91
+ def check_service_account_has_role_in_project(
36
92
  project_id: str,
37
- user_email: str,
93
+ service_account_email: str,
38
94
  organization_id: str,
39
95
  role: str = "roles/owner",
40
96
  expand_groups: bool = True
41
97
  ) -> bool:
42
98
  """
43
- Check if a user or service account has a specific role in a GCP project.
99
+ Check if a service account has a specific role in a GCP project.
44
100
 
45
101
  Args:
46
- user_email: Email of the user or service account to check
47
- (e.g., 'oshasha10@gcporg.com' or 'my-sa@project.iam.gserviceaccount.com')
102
+ project_id: GCP project ID (e.g., 'sky-starfi-mam-res-gcpro-1')
103
+ service_account_email: Email of the service account to check
104
+ (e.g., 'my-sa@project.iam.gserviceaccount.com')
105
+ organization_id: GCP organization ID (e.g., '111111111111')
48
106
  role: Role to check (e.g., 'roles/owner', 'roles/editor')
107
+ expand_groups: Whether to expand group memberships (default: True)
108
+
109
+ Returns:
110
+ bool: True if the service account has the role in the project, False otherwise
111
+
112
+ Example:
113
+ >>> has_access = check_service_account_has_role_in_project(
114
+ ... project_id='sky-starfi-mam-res-gcpro-1',
115
+ ... service_account_email='my-sa@project.iam.gserviceaccount.com',
116
+ ... organization_id='111111111111',
117
+ ... role='roles/owner'
118
+ ... )
119
+ """
120
+ client = asset_v1.AssetServiceClient()
121
+
122
+ # Construct the full resource name
123
+ scope = f"organizations/{organization_id}"
124
+ full_resource_name = f"//cloudresourcemanager.googleapis.com/projects/{project_id}"
125
+ identity = f"serviceAccount:{service_account_email}"
126
+
127
+ # Build the request
128
+ request = asset_v1.AnalyzeIamPolicyRequest(
129
+ analysis_query=asset_v1.IamPolicyAnalysisQuery(
130
+ scope=scope,
131
+ resource_selector=asset_v1.IamPolicyAnalysisQuery.ResourceSelector(
132
+ full_resource_name=full_resource_name
133
+ ),
134
+ identity_selector=asset_v1.IamPolicyAnalysisQuery.IdentitySelector(
135
+ identity=identity
136
+ ),
137
+ access_selector=asset_v1.IamPolicyAnalysisQuery.AccessSelector(
138
+ roles=[role]
139
+ ),
140
+ options=asset_v1.IamPolicyAnalysisQuery.Options(
141
+ expand_groups=expand_groups,
142
+ expand_roles=True,
143
+ )
144
+ )
145
+ )
146
+
147
+ try:
148
+ # Execute the analysis
149
+ logger.info(f"Checking if service account {service_account_email} has role {role} in project {project_id}")
150
+ response = client.analyze_iam_policy(request=request)
151
+
152
+ # Check if any results were returned
153
+ if response.main_analysis and response.main_analysis.analysis_results:
154
+ logger.info(f"Service account {service_account_email} has role {role} in project {project_id}")
155
+ return True
156
+
157
+ logger.info(f"Service account {service_account_email} does not have role {role} in project {project_id}")
158
+ return False
159
+
160
+ except Exception as e:
161
+ logger.exception(f"Error analyzing IAM policy: {e}")
162
+ raise
163
+
164
+
165
+ def check_group_has_role_in_project(
166
+ project_id: str,
167
+ group_email: str,
168
+ organization_id: str,
169
+ role: str = "roles/owner",
170
+ expand_groups: bool = True
171
+ ) -> bool:
172
+ """
173
+ Check if a group has a specific role in a GCP project.
174
+
175
+ Args:
49
176
  project_id: GCP project ID (e.g., 'sky-starfi-mam-res-gcpro-1')
177
+ group_email: Email of the group to check (e.g., 'dev-team@gcporg.com')
50
178
  organization_id: GCP organization ID (e.g., '111111111111')
179
+ role: Role to check (e.g., 'roles/owner', 'roles/editor')
51
180
  expand_groups: Whether to expand group memberships (default: True)
52
181
 
53
182
  Returns:
54
- bool: True if the user/service account has the role in the project, False otherwise
183
+ bool: True if the group has the role in the project, False otherwise
55
184
 
56
185
  Example:
57
- >>> has_access = check_user_has_role_in_project(
58
- ... user_email='oshasha10@gcporg.com',
59
- ... role='roles/owner',
186
+ >>> has_access = check_group_has_role_in_project(
60
187
  ... project_id='sky-starfi-mam-res-gcpro-1',
188
+ ... group_email='dev-team@gcporg.com',
189
+ ... organization_id='111111111111',
190
+ ... role='roles/owner'
61
191
  ... )
62
192
  """
63
193
  client = asset_v1.AssetServiceClient()
@@ -65,7 +195,7 @@ def check_user_has_role_in_project(
65
195
  # Construct the full resource name
66
196
  scope = f"organizations/{organization_id}"
67
197
  full_resource_name = f"//cloudresourcemanager.googleapis.com/projects/{project_id}"
68
- identity = _get_identity_string(user_email)
198
+ identity = f"group:{group_email}"
69
199
 
70
200
  # Build the request
71
201
  request = asset_v1.AnalyzeIamPolicyRequest(
@@ -83,23 +213,21 @@ def check_user_has_role_in_project(
83
213
  options=asset_v1.IamPolicyAnalysisQuery.Options(
84
214
  expand_groups=expand_groups,
85
215
  expand_roles=True,
86
- # expand_resources=True
87
216
  )
88
217
  )
89
218
  )
90
219
 
91
220
  try:
92
221
  # Execute the analysis
93
- logger.info(f"Checking if {user_email} has role {role} in project {project_id}")
222
+ logger.info(f"Checking if group {group_email} has role {role} in project {project_id}")
94
223
  response = client.analyze_iam_policy(request=request)
95
224
 
96
225
  # Check if any results were returned
97
- # If the user has the role, there will be analysis results
98
226
  if response.main_analysis and response.main_analysis.analysis_results:
99
- logger.info(f"{user_email} has role {role} in project {project_id}")
227
+ logger.info(f"Group {group_email} has role {role} in project {project_id}")
100
228
  return True
101
229
 
102
- logger.info(f"{user_email} does not have role {role} in project {project_id}")
230
+ logger.info(f"Group {group_email} does not have role {role} in project {project_id}")
103
231
  return False
104
232
 
105
233
  except Exception as e:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: gcp_platforms_auto
3
- Version: 0.8.3
3
+ Version: 0.8.4
4
4
  Summary: A brief description of your package
5
5
  Author-email: ofir4858 <ofirshasha10@gmail.com>
6
6
  License: MIT
@@ -0,0 +1,9 @@
1
+ gcp_platforms_auto/__init__.py,sha256=pKouDuMclA8r93k5H9TjGbxNsFwqIYfymjO-owuGGws,526
2
+ gcp_platforms_auto/db.py,sha256=jE5nwmqVHcxT4m6-meUgUz4V4DM8M_sMmeTpvKr2Z2Y,8768
3
+ gcp_platforms_auto/git.py,sha256=NnLDfRzzrzbm9yekepc-qgu8ejYmjNxQ4VDlW46gG2o,5508
4
+ gcp_platforms_auto/iam.py,sha256=wnMZ_jl2YM5dLY8LqtPfEyh_0Osqs1ypsxakJGmwHVw,11614
5
+ gcp_platforms_auto/models.py,sha256=mVg8NKV25kqdTuazqenAp7Ay03N5D8GIh3F_TWP0zyI,853
6
+ gcp_platforms_auto-0.8.4.dist-info/METADATA,sha256=UU0EYfRtyhRgQW_1867j05FBs16hAAan5w_UWKQn15w,600
7
+ gcp_platforms_auto-0.8.4.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
8
+ gcp_platforms_auto-0.8.4.dist-info/top_level.txt,sha256=4q-ofPMmvBaTnIbAzs-Wp_OwheAVxxmJ1fW9vl3-kyE,19
9
+ gcp_platforms_auto-0.8.4.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- gcp_platforms_auto/__init__.py,sha256=MhphRLBFhpZSik83woVgJi3NpyGjBRZ4RyOcXu8dqAk,443
2
- gcp_platforms_auto/db.py,sha256=jE5nwmqVHcxT4m6-meUgUz4V4DM8M_sMmeTpvKr2Z2Y,8768
3
- gcp_platforms_auto/git.py,sha256=NnLDfRzzrzbm9yekepc-qgu8ejYmjNxQ4VDlW46gG2o,5508
4
- gcp_platforms_auto/iam.py,sha256=GLjZ3HBbpbpLLSkOc60QEwdLWTc3GqWS45PrvT_-d5I,6655
5
- gcp_platforms_auto/models.py,sha256=mVg8NKV25kqdTuazqenAp7Ay03N5D8GIh3F_TWP0zyI,853
6
- gcp_platforms_auto-0.8.3.dist-info/METADATA,sha256=EnenLVPNmfEpeXU5yH-zifof3U4OwUScBbYlvT7fv5E,600
7
- gcp_platforms_auto-0.8.3.dist-info/WHEEL,sha256=wUyA8OaulRlbfwMtmQsvNngGrxQHAvkKcvRmdizlJi0,92
8
- gcp_platforms_auto-0.8.3.dist-info/top_level.txt,sha256=4q-ofPMmvBaTnIbAzs-Wp_OwheAVxxmJ1fW9vl3-kyE,19
9
- gcp_platforms_auto-0.8.3.dist-info/RECORD,,