feat: reuse decoding_rsa_key & decoding_cipher_rsa & optimize construct (#1937)

This commit is contained in:
takatost
2024-01-05 12:13:45 +08:00
committed by GitHub
parent af7be9bdd7
commit 296bf443a8
3 changed files with 60 additions and 16 deletions

View File

@ -520,7 +520,13 @@ class ProviderConfiguration(BaseModel):
provider_models.extend(
[
ModelWithProviderEntity(
**m.dict(),
model=m.model,
label=m.label,
model_type=m.model_type,
features=m.features,
fetch_from=m.fetch_from,
model_properties=m.model_properties,
deprecated=m.deprecated,
provider=SimpleModelProviderEntity(self.provider),
status=ModelStatus.ACTIVE
)
@ -569,7 +575,13 @@ class ProviderConfiguration(BaseModel):
for m in models:
provider_models.append(
ModelWithProviderEntity(
**m.dict(),
model=m.model,
label=m.label,
model_type=m.model_type,
features=m.features,
fetch_from=m.fetch_from,
model_properties=m.model_properties,
deprecated=m.deprecated,
provider=SimpleModelProviderEntity(self.provider),
status=ModelStatus.ACTIVE if credentials else ModelStatus.NO_CONFIGURE
)
@ -597,7 +609,13 @@ class ProviderConfiguration(BaseModel):
provider_models.append(
ModelWithProviderEntity(
**custom_model_schema.dict(),
model=custom_model_schema.model,
label=custom_model_schema.label,
model_type=custom_model_schema.model_type,
features=custom_model_schema.features,
fetch_from=custom_model_schema.fetch_from,
model_properties=custom_model_schema.model_properties,
deprecated=custom_model_schema.deprecated,
provider=SimpleModelProviderEntity(self.provider),
status=ModelStatus.ACTIVE
)

View File

@ -24,6 +24,9 @@ class ProviderManager:
"""
ProviderManager is a class that manages the model providers includes Hosting and Customize Model Providers.
"""
def __init__(self) -> None:
self.decoding_rsa_key = None
self.decoding_cipher_rsa = None
def get_configurations(self, tenant_id: str) -> ProviderConfigurations:
"""
@ -472,15 +475,16 @@ class ProviderManager:
provider_credentials = {}
# Get decoding rsa key and cipher for decrypting credentials
decoding_rsa_key, decoding_cipher_rsa = encrypter.get_decrypt_decoding(tenant_id)
if self.decoding_rsa_key is None or self.decoding_cipher_rsa is None:
self.decoding_rsa_key, self.decoding_cipher_rsa = encrypter.get_decrypt_decoding(tenant_id)
for variable in provider_credential_secret_variables:
if variable in provider_credentials:
try:
provider_credentials[variable] = encrypter.decrypt_token_with_decoding(
provider_credentials.get(variable),
decoding_rsa_key,
decoding_cipher_rsa
self.decoding_rsa_key,
self.decoding_cipher_rsa
)
except ValueError:
pass
@ -524,15 +528,16 @@ class ProviderManager:
continue
# Get decoding rsa key and cipher for decrypting credentials
decoding_rsa_key, decoding_cipher_rsa = encrypter.get_decrypt_decoding(tenant_id)
if self.decoding_rsa_key is None or self.decoding_cipher_rsa is None:
self.decoding_rsa_key, self.decoding_cipher_rsa = encrypter.get_decrypt_decoding(tenant_id)
for variable in model_credential_secret_variables:
if variable in provider_model_credentials:
try:
provider_model_credentials[variable] = encrypter.decrypt_token_with_decoding(
provider_model_credentials.get(variable),
decoding_rsa_key,
decoding_cipher_rsa
self.decoding_rsa_key,
self.decoding_cipher_rsa
)
except ValueError:
pass
@ -641,15 +646,16 @@ class ProviderManager:
)
# Get decoding rsa key and cipher for decrypting credentials
decoding_rsa_key, decoding_cipher_rsa = encrypter.get_decrypt_decoding(tenant_id)
if self.decoding_rsa_key is None or self.decoding_cipher_rsa is None:
self.decoding_rsa_key, self.decoding_cipher_rsa = encrypter.get_decrypt_decoding(tenant_id)
for variable in provider_credential_secret_variables:
if variable in provider_credentials:
try:
provider_credentials[variable] = encrypter.decrypt_token_with_decoding(
provider_credentials.get(variable),
decoding_rsa_key,
decoding_cipher_rsa
self.decoding_rsa_key,
self.decoding_cipher_rsa
)
except ValueError:
pass