Skip to content

Commit b1c444a

Browse files
authored
Only add new accounts when pinging SA keys (#644)
1 parent ee3ef92 commit b1c444a

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

fixbackend/cloud_accounts/gcp_service_account_service.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,18 @@ def blocking_call() -> List[Dict[str, Any]]:
9191
return await asyncio.to_thread(blocking_call)
9292

9393
async def update_cloud_accounts(
94-
self, projects: List[Dict[str, Any]], tenant_id: WorkspaceId, key_id: GcpServiceAccountKeyId
94+
self, projects: List[Dict[str, Any]], tenant_id: WorkspaceId, key_id: GcpServiceAccountKeyId, only_new: bool
9595
) -> None:
9696
for project in projects:
9797
await self.cloud_account_service.create_gcp_account(
98-
workspace_id=tenant_id, account_id=project["projectId"], account_name=project.get("name"), key_id=key_id
98+
workspace_id=tenant_id,
99+
account_id=project["projectId"],
100+
account_name=project.get("name"),
101+
key_id=key_id,
102+
only_new_accounts=only_new,
99103
)
100104

101-
async def _import_projects_from_service_account(self, key: GcpServiceAccountKey) -> None:
105+
async def _import_projects_from_service_account(self, key: GcpServiceAccountKey, only_new: bool = False) -> None:
102106
try:
103107
projects = await self.list_projects(key.value)
104108
except MalformedError as e:
@@ -114,7 +118,7 @@ async def _import_projects_from_service_account(self, key: GcpServiceAccountKey)
114118
await self.service_account_key_repo.update_status(key.id, can_access_sa=False, error=str(e))
115119
return None
116120
await self.service_account_key_repo.update_status(key.id, can_access_sa=True)
117-
await self.update_cloud_accounts(projects, key.tenant_id, key.id)
121+
await self.update_cloud_accounts(projects, key.tenant_id, key.id, only_new=only_new)
118122

119123
async def _ping_new_service_account_keys(self) -> None:
120124
created_less_than_30_minutes_ago = await self.service_account_key_repo.list_created_after(
@@ -126,10 +130,14 @@ async def _ping_new_service_account_keys(self) -> None:
126130
tg.create_task(self._import_projects_from_service_account(key))
127131

128132
async def _service_account_healthcheck(self) -> None:
133+
"""
134+
This will look for any new projects created by the users after we imported the SA keys
135+
and import them if we don't know about them yet.
136+
"""
129137
older_than_1_hour = await self.service_account_key_repo.list_created_before(
130138
utc() - timedelta(hours=1), only_valid_keys=True
131139
)
132140

133141
async with asyncio.TaskGroup() as tg:
134142
for key in older_than_1_hour:
135-
tg.create_task(self._import_projects_from_service_account(key))
143+
tg.create_task(self._import_projects_from_service_account(key, only_new=True))

fixbackend/cloud_accounts/service.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ async def create_gcp_account(
826826
account_id: CloudAccountId,
827827
key_id: GcpServiceAccountKeyId,
828828
account_name: Optional[CloudAccountName],
829+
only_new_accounts: bool = False,
829830
) -> CloudAccount:
830831
"""Create a GCP cloud account."""
831832
set_workspace_id(workspace_id)
@@ -838,7 +839,7 @@ async def create_gcp_account(
838839
raise ResourceNotFound("Organization does not exist")
839840

840841
if existing := await self.cloud_account_repository.get_by_account_id(workspace_id, account_id):
841-
if isinstance(existing.state, CloudAccountStates.Configured):
842+
if only_new_accounts or isinstance(existing.state, CloudAccountStates.Configured):
842843
log.info("GCP account already exists")
843844
return existing
844845

tests/fixbackend/cloud_accounts/router_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ async def create_gcp_account(
104104
account_id: CloudAccountId,
105105
key_id: GcpServiceAccountKeyId,
106106
account_name: Optional[CloudAccountName],
107+
only_new_accounts: bool = False,
107108
) -> CloudAccount:
108109
"""Create a GCP cloud account."""
109110
raise NotImplementedError

tests/fixbackend/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ async def list_projects(self, service_account_key: str) -> List[Dict[str, Any]]:
984984
return [{"projectId": "foo", "name": "bar"}]
985985

986986
async def update_cloud_accounts(
987-
self, projects: List[Dict[str, Any]], tenant_id: WorkspaceId, key_id: GcpServiceAccountKeyId
987+
self, projects: List[Dict[str, Any]], tenant_id: WorkspaceId, key_id: GcpServiceAccountKeyId, only_new: bool
988988
) -> None:
989989
return None
990990

0 commit comments

Comments
 (0)