Merge pull request #1086 from Windfarer/storage-allow-list
feat: add STORAGE_ALLOW_LIST env var
This commit is contained in:
@@ -333,6 +333,10 @@ func (h *InitializationHandler) UpdateKBConfig(c *gin.Context) {
|
||||
if provider == "" {
|
||||
provider = "local"
|
||||
}
|
||||
if !isStorageProviderAllowed(provider) {
|
||||
c.Error(errors.NewBadRequestError("Storage provider is not allowed by STORAGE_ALLOW_LIST"))
|
||||
return
|
||||
}
|
||||
oldProvider := kb.GetStorageProvider()
|
||||
if oldProvider == "" {
|
||||
oldProvider = "local"
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
stderrors "errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/agent/tools"
|
||||
@@ -130,6 +131,11 @@ func (h *KnowledgeBaseHandler) CreateKnowledgeBase(c *gin.Context) {
|
||||
c.Error(err)
|
||||
return
|
||||
}
|
||||
provider := strings.ToLower(strings.TrimSpace(req.GetStorageProvider()))
|
||||
if provider != "" && !isStorageProviderAllowed(provider) {
|
||||
c.Error(apperrors.NewBadRequestError("Storage provider is not allowed by STORAGE_ALLOW_LIST"))
|
||||
return
|
||||
}
|
||||
|
||||
logger.Infof(ctx, "Creating knowledge base, name: %s", secutils.SanitizeForLog(req.Name))
|
||||
// Create knowledge base using the service
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const storageAllowListEnv = "STORAGE_ALLOW_LIST"
|
||||
|
||||
var supportedStorageProviders = []string{"local", "minio", "cos", "tos", "s3", "oss"}
|
||||
|
||||
func getSupportedStorageProviders() []string {
|
||||
providers := make([]string, len(supportedStorageProviders))
|
||||
copy(providers, supportedStorageProviders)
|
||||
return providers
|
||||
}
|
||||
|
||||
func getAllowedStorageProviders() map[string]bool {
|
||||
raw := strings.TrimSpace(os.Getenv(storageAllowListEnv))
|
||||
allowed := make(map[string]bool, len(supportedStorageProviders))
|
||||
|
||||
if raw == "" {
|
||||
for _, provider := range supportedStorageProviders {
|
||||
allowed[provider] = true
|
||||
}
|
||||
return allowed
|
||||
}
|
||||
|
||||
for _, item := range strings.FieldsFunc(raw, func(r rune) bool {
|
||||
switch r {
|
||||
case ',', ';', '|', '\n', '\t', ' ':
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}) {
|
||||
provider := strings.ToLower(strings.TrimSpace(item))
|
||||
if provider == "" {
|
||||
continue
|
||||
}
|
||||
for _, supported := range supportedStorageProviders {
|
||||
if provider == supported {
|
||||
allowed[provider] = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return allowed
|
||||
}
|
||||
|
||||
func isStorageProviderAllowed(provider string) bool {
|
||||
provider = strings.ToLower(strings.TrimSpace(provider))
|
||||
if provider == "" {
|
||||
return true
|
||||
}
|
||||
return getAllowedStorageProviders()[provider]
|
||||
}
|
||||
|
||||
func firstAllowedStorageProvider() string {
|
||||
allowed := getAllowedStorageProviders()
|
||||
for _, provider := range supportedStorageProviders {
|
||||
if allowed[provider] {
|
||||
return provider
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -444,6 +444,7 @@ func (h *SystemHandler) isTOSEnvAvailable() bool {
|
||||
// StorageEngineStatusItem describes one storage engine's availability and description.
|
||||
type StorageEngineStatusItem struct {
|
||||
Name string `json:"name"` // "local", "minio", "cos", "tos"
|
||||
Allowed bool `json:"allowed"`
|
||||
Available bool `json:"available"` // whether the engine can be used
|
||||
Description string `json:"description"` // short description for UI
|
||||
}
|
||||
@@ -451,6 +452,7 @@ type StorageEngineStatusItem struct {
|
||||
// GetStorageEngineStatusResponse is the response for GET /system/storage-engine-status.
|
||||
type GetStorageEngineStatusResponse struct {
|
||||
Engines []StorageEngineStatusItem `json:"engines"`
|
||||
AllowedProviders []string `json:"allowed_providers"`
|
||||
MinioEnvAvailable bool `json:"minio_env_available"`
|
||||
}
|
||||
|
||||
@@ -466,18 +468,27 @@ func (h *SystemHandler) GetStorageEngineStatus(c *gin.Context) {
|
||||
minioEnvAvailable := h.isMinioEnvAvailable()
|
||||
cosConfigured := h.isCOSConfigured(c)
|
||||
tosConfigured := h.isTOSConfigured(c)
|
||||
s3Configured := h.isS3Configured(c)
|
||||
ossConfigured := h.isOSSConfigured(c)
|
||||
allowed := getAllowedStorageProviders()
|
||||
allowedProviders := make([]string, 0, len(supportedStorageProviders))
|
||||
for _, provider := range getSupportedStorageProviders() {
|
||||
if allowed[provider] {
|
||||
allowedProviders = append(allowedProviders, provider)
|
||||
}
|
||||
}
|
||||
engines := []StorageEngineStatusItem{
|
||||
{Name: "local", Available: true, Description: "本地文件系统存储,仅适合单机部署"},
|
||||
{Name: "minio", Available: minioConfigured || minioEnvAvailable, Description: "S3 兼容的自托管对象存储,适合内网和私有云部署"},
|
||||
{Name: "cos", Available: cosConfigured, Description: "腾讯云对象存储服务,适合公有云部署,支持 CDN 加速"},
|
||||
{Name: "tos", Available: tosConfigured, Description: "火山引擎对象存储服务,适合公有云部署"},
|
||||
{Name: "oss", Available: ossConfigured, Description: "阿里云对象存储服务,适合公有云部署,支持 S3 兼容协议"},
|
||||
{Name: "local", Allowed: allowed["local"], Available: true, Description: "本地文件系统存储,仅适合单机部署"},
|
||||
{Name: "minio", Allowed: allowed["minio"], Available: minioConfigured || minioEnvAvailable, Description: "S3 兼容的自托管对象存储,适合内网和私有云部署"},
|
||||
{Name: "cos", Allowed: allowed["cos"], Available: cosConfigured, Description: "腾讯云对象存储服务,适合公有云部署,支持 CDN 加速"},
|
||||
{Name: "tos", Allowed: allowed["tos"], Available: tosConfigured, Description: "火山引擎对象存储服务,适合公有云部署"},
|
||||
{Name: "s3", Allowed: allowed["s3"], Available: s3Configured, Description: "AWS S3 与兼容对象存储服务,适合公有云与混合云部署"},
|
||||
{Name: "oss", Allowed: allowed["oss"], Available: ossConfigured, Description: "阿里云对象存储服务,适合公有云部署,支持 S3 兼容协议"},
|
||||
}
|
||||
c.JSON(200, gin.H{
|
||||
"code": 0,
|
||||
"msg": "success",
|
||||
"data": GetStorageEngineStatusResponse{Engines: engines, MinioEnvAvailable: minioEnvAvailable},
|
||||
"data": GetStorageEngineStatusResponse{Engines: engines, AllowedProviders: allowedProviders, MinioEnvAvailable: minioEnvAvailable},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -607,6 +618,10 @@ func (h *SystemHandler) CheckStorageEngine(c *gin.Context) {
|
||||
c.JSON(400, gin.H{"code": 1, "msg": "请求体格式错误"})
|
||||
return
|
||||
}
|
||||
if !isStorageProviderAllowed(req.Provider) {
|
||||
c.JSON(403, gin.H{"code": 1, "msg": "该存储引擎已被禁用"})
|
||||
return
|
||||
}
|
||||
|
||||
switch req.Provider {
|
||||
case "minio":
|
||||
@@ -624,6 +639,16 @@ func (h *SystemHandler) CheckStorageEngine(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (h *SystemHandler) isS3Configured(c *gin.Context) bool {
|
||||
if v, exists := c.Get(types.TenantInfoContextKey.String()); exists {
|
||||
if tenant, ok := v.(*types.Tenant); ok && tenant != nil && tenant.StorageEngineConfig != nil && tenant.StorageEngineConfig.S3 != nil {
|
||||
s3Conf := tenant.StorageEngineConfig.S3
|
||||
return s3Conf.Endpoint != "" && s3Conf.Region != "" && s3Conf.AccessKey != "" && s3Conf.SecretKey != "" && s3Conf.BucketName != ""
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (h *SystemHandler) checkMinio(c *gin.Context, ctx context.Context, cfg *types.MinIOEngineConfig) {
|
||||
if cfg == nil {
|
||||
c.JSON(200, gin.H{"code": 0, "data": StorageCheckResponse{OK: false, Message: "未提供 MinIO 配置"}})
|
||||
|
||||
@@ -3,6 +3,7 @@ package handler
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -842,6 +843,19 @@ func (h *TenantHandler) updateTenantStorageEngineConfigInternal(c *gin.Context)
|
||||
c.Error(errors.NewValidationError("Invalid request data").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
provider := strings.ToLower(strings.TrimSpace(cfg.DefaultProvider))
|
||||
if provider == "" {
|
||||
provider = firstAllowedStorageProvider()
|
||||
}
|
||||
if provider == "" {
|
||||
c.Error(errors.NewBadRequestError("No storage provider is allowed by STORAGE_ALLOW_LIST"))
|
||||
return
|
||||
}
|
||||
if !isStorageProviderAllowed(provider) {
|
||||
c.Error(errors.NewBadRequestError("Storage provider is not allowed by STORAGE_ALLOW_LIST"))
|
||||
return
|
||||
}
|
||||
cfg.DefaultProvider = provider
|
||||
tenant, _ := types.TenantInfoFromContext(ctx)
|
||||
if tenant == nil {
|
||||
logger.Error(ctx, "Tenant is empty")
|
||||
|
||||
Reference in New Issue
Block a user