Skip to content

Commit 5ac207a

Browse files
authored
Set can_access_account flag to false when updating creds (#642)
1 parent 36e3109 commit 5ac207a

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

fixbackend/cloud_accounts/azure_subscription_repo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ async def upsert(
8383
existing.client_id = client_id
8484
existing.client_secret = client_secret
8585
existing.created_at = utc() # update to trigger list_created_after
86+
existing.updated_at = utc()
87+
existing.can_access_azure_account = False
8688
model = existing.to_model()
8789
await session.commit()
8890
return model
@@ -93,6 +95,7 @@ async def upsert(
9395
azure_tenant_id=azure_tenant_id,
9496
client_id=client_id,
9597
client_secret=client_secret,
98+
can_access_azure_account=False,
9699
)
97100
session.add(entity)
98101
await session.commit()

fixbackend/cloud_accounts/service.py

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -838,8 +838,9 @@ async def create_gcp_account(
838838
raise ResourceNotFound("Organization does not exist")
839839

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

844845
should_be_enabled = await self._should_be_enabled(workspace)
845846

@@ -870,8 +871,25 @@ async def create_gcp_account(
870871
last_degraded_scan_started_at=None,
871872
)
872873

873-
result = await self.cloud_account_repository.create(account)
874-
log.info(f"GCP cloud Account {account_id} created")
874+
if existing:
875+
876+
def set_state(acc: CloudAccount) -> CloudAccount:
877+
return evolve(
878+
acc,
879+
state=CloudAccountStates.Configured(
880+
access=GcpCloudAccess(key_id), enabled=should_be_enabled, scan=should_be_enabled
881+
),
882+
account_name=account_name,
883+
state_updated_at=utc(),
884+
created_at=created_at,
885+
updated_at=created_at,
886+
)
887+
888+
result = await self.cloud_account_repository.update(existing.id, set_state)
889+
log.info(f"GCP cloud Account {account_id} updated from deleted to configured")
890+
else:
891+
result = await self.cloud_account_repository.create(account)
892+
log.info(f"GCP cloud Account {account_id} created")
875893

876894
await self.domain_events.publish(
877895
CloudAccountConfigured(
@@ -903,8 +921,9 @@ async def create_azure_account(
903921
raise ResourceNotFound("Organization does not exist")
904922

905923
if existing := await self.cloud_account_repository.get_by_account_id(workspace_id, account_id):
906-
log.info("Azure account already exists")
907-
return existing
924+
if isinstance(existing.state, CloudAccountStates.Configured):
925+
log.info("Azure account already exists")
926+
return existing
908927

909928
should_be_enabled = await self._should_be_enabled(workspace)
910929

@@ -935,8 +954,26 @@ async def create_azure_account(
935954
last_degraded_scan_started_at=None,
936955
)
937956

938-
result = await self.cloud_account_repository.create(account)
939-
log.info(f"Azure cloud Account {account_id} created")
957+
if existing:
958+
959+
def set_state(acc: CloudAccount) -> CloudAccount:
960+
return evolve(
961+
acc,
962+
state=CloudAccountStates.Configured(
963+
access=AzureCloudAccess(subscription_credentials_id),
964+
enabled=should_be_enabled,
965+
scan=should_be_enabled,
966+
),
967+
state_updated_at=utc(),
968+
created_at=created_at,
969+
updated_at=created_at,
970+
)
971+
972+
result = await self.cloud_account_repository.update(existing.id, set_state)
973+
log.info(f"Azure cloud Account {account_id} updated from deleted to configured")
974+
else:
975+
result = await self.cloud_account_repository.create(account)
976+
log.info(f"Azure cloud Account {account_id} created")
940977

941978
await self.domain_events.publish(
942979
CloudAccountConfigured(

tests/fixbackend/cloud_accounts/azure_subscription_repo_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async def test_store_azure_subscription(
3434
client_secret = "foo_bar"
3535
azure_credentials = await azure_repo.upsert(workspace.id, azure_tenant_id, client_id, client_secret)
3636

37-
assert azure_credentials.can_access_azure_account is None
37+
assert azure_credentials.can_access_azure_account is False
3838
assert azure_credentials.tenant_id == workspace.id
3939
assert azure_credentials.azure_tenant_id == azure_tenant_id
4040
assert azure_credentials.client_id == client_id

0 commit comments

Comments
 (0)