Fix regex check in creation of config

This commit is contained in:
Chandrapal Badshah
2025-05-27 13:38:02 +05:30
parent 57acb43251
commit f4febf9e08
3 changed files with 48 additions and 2 deletions
+3 -2
View File
@@ -1377,7 +1377,8 @@ class LighthouseConfig(RowLevelSecurityProtectedModel):
)
business_context = models.TextField(
blank=True,
null=True,
null=False,
default="",
help_text="Additional business context for this AI model configuration",
)
is_active = models.BooleanField(default=True)
@@ -1444,7 +1445,7 @@ class LighthouseConfig(RowLevelSecurityProtectedModel):
openai_key_pattern = r"^sk-[\w-]+T3BlbkFJ[\w-]+$"
if not re.match(openai_key_pattern, value):
raise ModelValidationError(
detail="Invalid OpenAI API key format. Must start with 'sk-'.",
detail="Invalid OpenAI API key format.",
code="invalid_api_key",
pointer="/data/attributes/api_key",
)
+13
View File
@@ -1,4 +1,5 @@
import json
import re
from datetime import datetime, timedelta, timezone
from django.conf import settings
@@ -2203,6 +2204,18 @@ class LighthouseConfigCreateSerializer(RLSSerializer, BaseWriteSerializer):
def create(self, validated_data):
api_key = validated_data.pop("api_key")
# Validate API key format before creating the instance
if not api_key:
raise serializers.ValidationError({"api_key": "API key is required"})
# Validate OpenAI API key format
openai_key_pattern = r"^sk-[\w-]+T3BlbkFJ[\w-]+$"
if not re.match(openai_key_pattern, api_key):
raise serializers.ValidationError(
{"api_key": "Invalid OpenAI API key format."}
)
instance = super().create(validated_data)
instance.api_key_decoded = api_key
instance.save()
+32
View File
@@ -2907,6 +2907,38 @@ class IntegrationViewSet(BaseRLSViewSet):
return context
@extend_schema_view(
list=extend_schema(
tags=["Lighthouse"],
summary="List all Lighthouse configurations",
description="Retrieve a list of all Lighthouse configurations.",
),
create=extend_schema(
tags=["Lighthouse"],
summary="Create a new Lighthouse configuration",
description="Create a new Lighthouse configuration with the specified details.",
),
partial_update=extend_schema(
tags=["Lighthouse"],
summary="Partially update a Lighthouse configuration",
description="Update certain fields of an existing Lighthouse configuration.",
),
retrieve=extend_schema(
tags=["Lighthouse"],
summary="Retrieve a Lighthouse configuration",
description="Fetch detailed information about a specific Lighthouse configuration by its ID. Add query param `fields[lighthouse-config]=api_key` to get API key.",
),
destroy=extend_schema(
tags=["Lighthouse"],
summary="Delete a Lighthouse configuration",
description="Remove a Lighthouse configuration by its ID.",
),
check_connection=extend_schema(
tags=["Lighthouse"],
summary="Check the connection to the OpenAI API",
description="Verify the connection to the OpenAI API for a specific Lighthouse configuration.",
),
)
class LighthouseConfigViewSet(BaseRLSViewSet):
"""
API endpoint for managing Lighthouse configuration.