Parcourir la source

fix: clear endpoint credentials cache

Yeuoly il y a 10 mois
Parent
commit
3307c82af3

+ 1 - 1
internal/core/dify_invocation/real/http_request.go

@@ -139,7 +139,7 @@ func (i *RealBackwardsInvocation) InvokeEncrypt(payload *dify_invocation.InvokeE
 	}
 
 	type resp struct {
-		Data map[string]any `json:"data"`
+		Data map[string]any `json:"data,omitempty"`
 	}
 
 	data, err := Request[resp](i, "POST", "invoke/encrypt", http_requests.HttpPayloadJson(payload))

+ 2 - 1
internal/core/dify_invocation/types.go

@@ -147,11 +147,12 @@ type EncryptOpt string
 const (
 	ENCRYPT_OPT_ENCRYPT EncryptOpt = "encrypt"
 	ENCRYPT_OPT_DECRYPT EncryptOpt = "decrypt"
+	ENCRYPT_OPT_CLEAR   EncryptOpt = "clear"
 )
 
 func isEncryptOpt(fl validator.FieldLevel) bool {
 	opt := EncryptOpt(fl.Field().String())
-	return opt == ENCRYPT_OPT_ENCRYPT || opt == ENCRYPT_OPT_DECRYPT
+	return opt == ENCRYPT_OPT_ENCRYPT || opt == ENCRYPT_OPT_DECRYPT || opt == ENCRYPT_OPT_CLEAR
 }
 
 type EncryptNamespace string

+ 37 - 0
internal/service/setup_endpoint.go

@@ -109,6 +109,27 @@ func RemoveEndpoint(endpoint_id string, tenant_id string) *entities.Response {
 		return entities.NewErrorResponse(-500, fmt.Sprintf("failed to remove endpoint: %v", err))
 	}
 
+	manager := plugin_manager.Manager()
+	if manager == nil {
+		return entities.NewErrorResponse(-500, "failed to get plugin manager")
+	}
+
+	// clear credentials cache
+	if _, err := manager.BackwardsInvocation().InvokeEncrypt(&dify_invocation.InvokeEncryptRequest{
+		BaseInvokeDifyRequest: dify_invocation.BaseInvokeDifyRequest{
+			TenantId: tenant_id,
+			UserId:   "",
+			Type:     dify_invocation.INVOKE_TYPE_ENCRYPT,
+		},
+		InvokeEncryptSchema: dify_invocation.InvokeEncryptSchema{
+			Opt:       dify_invocation.ENCRYPT_OPT_CLEAR,
+			Namespace: dify_invocation.ENCRYPT_NAMESPACE_ENDPOINT,
+			Identity:  endpoint.ID,
+		},
+	}); err != nil {
+		return entities.NewErrorResponse(-500, fmt.Sprintf("failed to clear credentials cache: %v", err))
+	}
+
 	return entities.NewSuccessResponse(nil)
 }
 
@@ -209,5 +230,21 @@ func UpdateEndpoint(endpoint_id string, tenant_id string, user_id string, settin
 		return entities.NewErrorResponse(-500, fmt.Sprintf("failed to update endpoint: %v", err))
 	}
 
+	// clear credentials cache
+	if _, err := manager.BackwardsInvocation().InvokeEncrypt(&dify_invocation.InvokeEncryptRequest{
+		BaseInvokeDifyRequest: dify_invocation.BaseInvokeDifyRequest{
+			TenantId: tenant_id,
+			UserId:   user_id,
+			Type:     dify_invocation.INVOKE_TYPE_ENCRYPT,
+		},
+		InvokeEncryptSchema: dify_invocation.InvokeEncryptSchema{
+			Opt:       dify_invocation.ENCRYPT_OPT_CLEAR,
+			Namespace: dify_invocation.ENCRYPT_NAMESPACE_ENDPOINT,
+			Identity:  endpoint.ID,
+		},
+	}); err != nil {
+		return entities.NewErrorResponse(-500, fmt.Sprintf("failed to clear credentials cache: %v", err))
+	}
+
 	return entities.NewSuccessResponse(nil)
 }