Skip to content

Commit

Permalink
validate api key generation (microsoft#56)
Browse files Browse the repository at this point in the history
* validate SIMULATOR_API_KEY is not set with empty value
* default simulator_api_key to empty string
  • Loading branch information
lucashuet93 authored Sep 18, 2024
1 parent 600fe67 commit df1ae48
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/aoai-api-simulator/src/aoai_api_simulator/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# from aoai_api_simulator.pipeline import RequestContext
from fastapi import Request, Response
from pydantic import Field
from pydantic import Field, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict
from requests import Response as requests_Response
from starlette.routing import Match, Route
Expand Down Expand Up @@ -125,12 +125,22 @@ class LatencyConfig(BaseSettings):

class PatchableConfig(BaseSettings):
simulator_mode: str = Field(default="generate", alias="SIMULATOR_MODE", pattern="^(generate|record|replay)$")
simulator_api_key: str = Field(default=nanoid.generate(size=30), alias="SIMULATOR_API_KEY")
simulator_api_key: str = Field(default="", alias="SIMULATOR_API_KEY")
recording: RecordingConfig = Field(default=RecordingConfig())
openai_deployments: dict[str, "OpenAIDeployment"] | None = Field(default=None)
latency: Annotated[LatencyConfig, Field(default=LatencyConfig())]
allow_undefined_openai_deployments: bool = Field(default=True, alias="ALLOW_UNDEFINED_OPENAI_DEPLOYMENTS")

# Disable all the no-self-argument violations in this function
# pylint: disable=no-self-argument
@field_validator("simulator_api_key")
def simulator_api_key_should_not_be_empty_string(cls, v):
if v == "":
return nanoid.generate(size=30)
return v

# pylint: enable=no-self-argument


class Config(PatchableConfig):
"""
Expand Down

0 comments on commit df1ae48

Please sign in to comment.