Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set can_access_account flag to false when updating creds #642

Merged
merged 5 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fixbackend/cloud_accounts/azure_subscription_repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ async def upsert(
existing.client_id = client_id
existing.client_secret = client_secret
existing.created_at = utc() # update to trigger list_created_after
existing.updated_at = utc()
existing.can_access_azure_account = False
model = existing.to_model()
await session.commit()
return model
Expand All @@ -93,6 +95,7 @@ async def upsert(
azure_tenant_id=azure_tenant_id,
client_id=client_id,
client_secret=client_secret,
can_access_azure_account=False,
)
session.add(entity)
await session.commit()
Expand Down
53 changes: 45 additions & 8 deletions fixbackend/cloud_accounts/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -838,8 +838,9 @@ async def create_gcp_account(
raise ResourceNotFound("Organization does not exist")

if existing := await self.cloud_account_repository.get_by_account_id(workspace_id, account_id):
log.info("GCP account already exists")
return existing
if isinstance(existing.state, CloudAccountStates.Configured):
log.info("GCP account already exists")
return existing

should_be_enabled = await self._should_be_enabled(workspace)

Expand Down Expand Up @@ -870,8 +871,25 @@ async def create_gcp_account(
last_degraded_scan_started_at=None,
)

result = await self.cloud_account_repository.create(account)
log.info(f"GCP cloud Account {account_id} created")
if existing:

def set_state(acc: CloudAccount) -> CloudAccount:
return evolve(
acc,
state=CloudAccountStates.Configured(
access=GcpCloudAccess(key_id), enabled=should_be_enabled, scan=should_be_enabled
),
account_name=account_name,
state_updated_at=utc(),
created_at=created_at,
updated_at=created_at,
)

result = await self.cloud_account_repository.update(existing.id, set_state)
log.info(f"GCP cloud Account {account_id} updated from deleted to configured")
else:
result = await self.cloud_account_repository.create(account)
log.info(f"GCP cloud Account {account_id} created")

await self.domain_events.publish(
CloudAccountConfigured(
Expand Down Expand Up @@ -903,8 +921,9 @@ async def create_azure_account(
raise ResourceNotFound("Organization does not exist")

if existing := await self.cloud_account_repository.get_by_account_id(workspace_id, account_id):
log.info("Azure account already exists")
return existing
if isinstance(existing.state, CloudAccountStates.Configured):
log.info("Azure account already exists")
return existing

should_be_enabled = await self._should_be_enabled(workspace)

Expand Down Expand Up @@ -935,8 +954,26 @@ async def create_azure_account(
last_degraded_scan_started_at=None,
)

result = await self.cloud_account_repository.create(account)
log.info(f"Azure cloud Account {account_id} created")
if existing:

def set_state(acc: CloudAccount) -> CloudAccount:
return evolve(
acc,
state=CloudAccountStates.Configured(
access=AzureCloudAccess(subscription_credentials_id),
enabled=should_be_enabled,
scan=should_be_enabled,
),
state_updated_at=utc(),
created_at=created_at,
updated_at=created_at,
)

result = await self.cloud_account_repository.update(existing.id, set_state)
log.info(f"Azure cloud Account {account_id} updated from deleted to configured")
else:
result = await self.cloud_account_repository.create(account)
log.info(f"Azure cloud Account {account_id} created")

await self.domain_events.publish(
CloudAccountConfigured(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ async def test_store_azure_subscription(
client_secret = "foo_bar"
azure_credentials = await azure_repo.upsert(workspace.id, azure_tenant_id, client_id, client_secret)

assert azure_credentials.can_access_azure_account is None
assert azure_credentials.can_access_azure_account is False
assert azure_credentials.tenant_id == workspace.id
assert azure_credentials.azure_tenant_id == azure_tenant_id
assert azure_credentials.client_id == client_id
Expand Down