-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
managed_secrets
app and integrate Azure Key Vault
- Introduce `ManagedSecret` model and admin interface for managing secrets - Update `FunctionsPage` and API key UI with `manage_secrets_view` for creating, editing, and deleting secrets - Configure `AZURE_KEY_VAULT_ENDPOINT` and add dependencies (`azure-identity`, `azure-keyvault-secrets`, etc.) to securely handle secrets - Enhance security by enabling encrypted secret storage and streamlined secret management in the UI This change improves the application's security by providing a robust mechanism for managing sensitive environment variables using Azure Key Vault.
- Loading branch information
Showing
15 changed files
with
511 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.contrib import admin | ||
|
||
from managed_secrets.models import ManagedSecret | ||
|
||
|
||
@admin.register(ManagedSecret) | ||
class ManagedSecretAdmin(admin.ModelAdmin): | ||
list_display = [ | ||
"__str__", | ||
"workspace", | ||
"created_at", | ||
"updated_at", | ||
"usage_count", | ||
"last_used_at", | ||
] | ||
autocomplete_fields = ["workspace", "created_by"] | ||
readonly_fields = ["usage_count", "last_used_at", "external_id"] | ||
search_fields = ["name", "external_id"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class ManagedSecretsConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "managed_secrets" | ||
|
||
def ready(self): | ||
from . import signals | ||
|
||
assert signals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Generated by Django 5.1.3 on 2024-12-23 15:47 | ||
|
||
import django.db.models.deletion | ||
import uuid | ||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
initial = True | ||
|
||
dependencies = [ | ||
('app_users', '0023_alter_appusertransaction_workspace'), | ||
('workspaces', '0006_workspace_description'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='ManagedSecret', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('external_id', models.UUIDField(default=uuid.uuid4, editable=False)), | ||
('name', models.CharField(max_length=255)), | ||
('created_at', models.DateTimeField(auto_now_add=True)), | ||
('updated_at', models.DateTimeField(auto_now=True)), | ||
('usage_count', models.PositiveIntegerField(db_index=True, default=0)), | ||
('last_used_at', models.DateTimeField(blank=True, default=None, null=True)), | ||
('created_by', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='app_users.appuser')), | ||
('workspace', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='managed_secrets', to='workspaces.workspace')), | ||
], | ||
options={ | ||
'unique_together': {('workspace', 'name')}, | ||
}, | ||
), | ||
] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import uuid | ||
|
||
from django.db import models | ||
|
||
from daras_ai_v2 import settings | ||
|
||
'az role assignment create --role "Key Vault Secrets Officer" --assignee "<upn>" --scope "/subscriptions/<subscription-id>/resourceGroups/<resource-group-name>/providers/Microsoft.KeyVault/vaults/<your-unique-keyvault-name>"' | ||
|
||
|
||
class ManagedSecretQuerySet(models.QuerySet): | ||
def create(self, *, value: str, **kwargs): | ||
secret = super().create(**kwargs) | ||
secret.store_value(value) | ||
return secret | ||
|
||
|
||
class ManagedSecret(models.Model): | ||
external_id = models.UUIDField(default=uuid.uuid4, editable=False) | ||
|
||
name = models.CharField(max_length=255) | ||
|
||
workspace = models.ForeignKey( | ||
"workspaces.Workspace", on_delete=models.CASCADE, related_name="managed_secrets" | ||
) | ||
created_by = models.ForeignKey( | ||
"app_users.AppUser", on_delete=models.SET_NULL, null=True | ||
) | ||
|
||
created_at = models.DateTimeField(auto_now_add=True) | ||
updated_at = models.DateTimeField(auto_now=True) | ||
|
||
usage_count = models.PositiveIntegerField(default=0, db_index=True) | ||
last_used_at = models.DateTimeField(null=True, blank=True, default=None) | ||
|
||
objects: ManagedSecretQuerySet = ManagedSecretQuerySet.as_manager() | ||
|
||
value: str | None = None | ||
|
||
class Meta: | ||
unique_together = ("workspace", "name") | ||
|
||
def __str__(self): | ||
return f"{self.name} ({self._external_name()})" | ||
|
||
def store_value(self, value: str): | ||
client = _get_az_secret_client() | ||
client.set_secret(self._external_name(), value) | ||
self.value = value | ||
|
||
def load_value(self): | ||
client = _get_az_secret_client() | ||
self.value = client.get_secret(self._external_name()).value | ||
|
||
def delete_value(self): | ||
import azure.core.exceptions | ||
|
||
client = _get_az_secret_client() | ||
try: | ||
client.begin_delete_secret(self._external_name()) | ||
except azure.core.exceptions.ResourceNotFoundError: | ||
pass | ||
|
||
def _external_name(self): | ||
return f"ms-gooey-{self.external_id}" | ||
|
||
|
||
def _get_az_secret_client(): | ||
from azure.keyvault.secrets import SecretClient | ||
from azure.identity import DefaultAzureCredential | ||
|
||
credential = DefaultAzureCredential() | ||
return SecretClient( | ||
vault_url=settings.AZURE_KEY_VAULT_ENDPOINT, credential=credential | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from django.db.models.signals import post_save, post_delete | ||
from django.dispatch import receiver | ||
|
||
from managed_secrets.models import ManagedSecret | ||
|
||
|
||
@receiver(post_save, sender=ManagedSecret) | ||
def on_secret_saved(instance: ManagedSecret, **kwargs): | ||
if instance.value is None: | ||
return | ||
instance.store_value(instance.value) | ||
|
||
|
||
@receiver(post_delete, sender=ManagedSecret) | ||
def on_secret_deleted(instance: ManagedSecret, **kwargs): | ||
instance.delete_value() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.test import TestCase | ||
|
||
# Create your tests here. |
Oops, something went wrong.