fix(security): 系统渠道密钥 - 加密落库并在运行时解密
Co-authored-by: dyhhhhhqwer <1613203335@qq.com> Original-PR: https://github.com/ddcat-ai/open-ai-canvas/pull/202
This commit is contained in:
@@ -447,7 +447,25 @@ func (s *Service) PublicSystemChannels() ([]PublicModelChannel, error) {
|
||||
}
|
||||
|
||||
func (s *Service) SystemChannel(id string) (*model.ModelChannel, error) {
|
||||
return s.repo.SystemChannel(id)
|
||||
channel, err := s.repo.SystemChannel(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.decryptSystemChannelSecrets(channel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
func (s *Service) adminSystemChannel(id string) (*model.ModelChannel, error) {
|
||||
channel, err := s.repo.AdminSystemChannel(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.decryptSystemChannelSecrets(channel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
func (s *Service) AdminSystemChannelPage(actor *model.User, query AdminListQuery) (*AdminChannelPage, error) {
|
||||
@@ -488,6 +506,9 @@ func (s *Service) CreateSystemChannel(actor *model.User, req ChannelRequest) (*P
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.encryptSystemChannelSecrets(&channel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.repo.Create(&channel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -510,6 +531,9 @@ func (s *Service) UpdateSystemChannel(actor *model.User, id string, req ChannelR
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.decryptSystemChannelSecrets(channel); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req = mergeChannelRequest(req, *channel)
|
||||
next, err := channelFromRequest(req, *channel)
|
||||
if err != nil {
|
||||
@@ -525,6 +549,9 @@ func (s *Service) UpdateSystemChannel(actor *model.User, id string, req ChannelR
|
||||
if req.SecretKey == "" {
|
||||
next.SecretKey = channel.SecretKey
|
||||
}
|
||||
if err := s.encryptSystemChannelSecrets(&next); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.repo.Save(&next); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -539,6 +566,34 @@ func (s *Service) UpdateSystemChannel(actor *model.User, id string, req ChannelR
|
||||
return &public, nil
|
||||
}
|
||||
|
||||
func (s *Service) encryptSystemChannelSecrets(channel *model.ModelChannel) error {
|
||||
apiKey, err := s.encryptSettingSecret(channel.APIKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
secretKey, err := s.encryptSettingSecret(channel.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
channel.APIKey = apiKey
|
||||
channel.SecretKey = secretKey
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) decryptSystemChannelSecrets(channel *model.ModelChannel) error {
|
||||
apiKey, err := s.decryptSettingSecret(channel.APIKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
secretKey, err := s.decryptSettingSecret(channel.SecretKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
channel.APIKey = apiKey
|
||||
channel.SecretKey = secretKey
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) DeleteSystemChannel(actor *model.User, id string) error {
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return err
|
||||
|
||||
@@ -88,7 +88,7 @@ func (s *Service) FetchAdminChannelModels(ctx context.Context, actor *model.User
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
channel, err := s.repo.AdminSystemChannel(channelID)
|
||||
channel, err := s.adminSystemChannel(channelID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -226,7 +226,7 @@ func (s *Service) TestAdminChannelModel(ctx context.Context, actor *model.User,
|
||||
if err := s.RequireAdmin(actor); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
channel, err := s.repo.AdminSystemChannel(channelID)
|
||||
channel, err := s.adminSystemChannel(channelID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -572,7 +572,7 @@ func (s *Service) resolveProviderConfig(config providerConfig) (providerConfig,
|
||||
}
|
||||
return config, nil
|
||||
}
|
||||
channel, err := s.repo.SystemChannel(channelID)
|
||||
channel, err := s.SystemChannel(channelID)
|
||||
if err != nil {
|
||||
return providerConfig{}, errors.New("系统渠道不存在或已停用")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user