diff --git a/cmd/commandline/plugin.go b/cmd/commandline/plugin.go index e9cb82d929..fb9a20ac79 100644 --- a/cmd/commandline/plugin.go +++ b/cmd/commandline/plugin.go @@ -196,50 +196,6 @@ If no parameters are provided, an interactive mode will be started.`, }, } - // NOTE: tester is deprecated, maybe, in several months, we will support this again - // pluginTestCommand = &cobra.Command{ - // Use: "test [-i inputs] [-t timeout] package_path invoke_type invoke_action", - // Short: "", - // Long: "Test runs the given plugin package locally, and you can specify the inputs using json format, if not specified, will use default inputs\n" + - // "type: invoke type, available values: \n" + - // "[\n" + - // " tool, model, endpoint\n" + - // "]\n" + - // "action: invoke action, available values: \n" + - // "[\n" + - // " invoke_tool, validate_tool_credentials, \n" + - // " invoke_endpoint\n" + - // " invoke_llm, invoke_text_embedding, invoke_rerank, invoke_tts, invoke_speech2text, invoke_moderation, \n" + - // " validate_provider_credentials, validate_model_credentials, get_tts_model_voices, \n" + - // " get_text_embedding_num_tokens, get_ai_model_schemas, get_llm_num_tokens\n" + - // "]\n", - // Run: func(cmd *cobra.Command, args []string) { - // if len(args) < 3 { - // log.Error("invalid args, please specify package_path, invoke_type, invoke_action") - // return - // } - // // get package path - // package_path_str := args[0] - // // get invoke type - // invoke_type_str := args[1] - // // get invoke action - // invoke_action_str := args[2] - // // get inputs if specified - // inputs := map[string]any{} - // if cmd.Flag("inputs") != nil { - // inputs_str := cmd.Flag("inputs").Value.String() - // err := json.Unmarshal([]byte(inputs_str), &inputs) - // if err != nil { - // log.Error("failed to unmarshal inputs, inputs: %s, error: %v", inputs_str, err) - // return - // } - // } - // // parse flag - // timeout := "" - // if cmd.Flag("timeout") != nil { - // timeout = cmd.Flag("timeout").Value.String() - // } - ) func init() { diff --git a/cmd/commandline/signature/signature_test.go b/cmd/commandline/signature/signature_test.go index 7c8bcafc7b..bd1781be41 100644 --- a/cmd/commandline/signature/signature_test.go +++ b/cmd/commandline/signature/signature_test.go @@ -285,7 +285,7 @@ func TestVerifyPluginWithoutVerificationField(t *testing.T) { t.Fatalf("Failed to create dummy plugin file: %v", err) } - pluginPackageWithoutVerificationField, err := signer.TraditionalSignPlugin(dummyPlugin) + pluginPackageWithoutVerificationField, err := signer.SignPlugin(dummyPlugin, nil) if err != nil { t.Fatalf("Failed to sign plugin: %v", err) } diff --git a/internal/core/local_runtime/tester.go b/internal/core/local_runtime/tester.go deleted file mode 100644 index f5e7662d21..0000000000 --- a/internal/core/local_runtime/tester.go +++ /dev/null @@ -1 +0,0 @@ -package local_runtime diff --git a/internal/core/plugin_manager/tester.go b/internal/core/plugin_manager/tester.go deleted file mode 100644 index 3b2ce4aba6..0000000000 --- a/internal/core/plugin_manager/tester.go +++ /dev/null @@ -1,105 +0,0 @@ -package plugin_manager - -/* -NOTE: tester is deprecated, maybe, in several months, we will support this again -*/ - -// func (p *PluginManager) TestPlugin( -// path string, -// request map[string]any, -// access_type access_types.PluginAccessType, -// access_action access_types.PluginAccessAction, -// timeout string, -// ) (*stream.Stream[any], error) { - -// // launch plugin runtime -// plugin, err := p.getLocalPluginRuntime(path) -// if err != nil { -// return nil, errors.Join(err, errors.New("failed to load plugin")) -// } - -// // get assets -// assets, err := plugin.decoder.Assets() -// if err != nil { -// return nil, errors.Join(err, errors.New("failed to get assets")) -// } - -// local_plugin_runtime := local_runtime.NewLocalPluginRuntime(p.pythonInterpreterPath) -// local_plugin_runtime.PluginRuntime = plugin.runtime -// local_plugin_runtime.PositivePluginRuntime = positive_manager.PositivePluginRuntime{ -// BasicPluginRuntime: basic_runtime.NewBasicPluginRuntime(p.mediaBucket), -// WorkingPath: plugin.runtime.State.WorkingPath, -// Decoder: plugin.decoder, -// } -// if err := local_plugin_runtime.RemapAssets( -// &local_plugin_runtime.Config, -// assets, -// ); err != nil { -// return nil, errors.Join(err, errors.New("failed to remap assets")) -// } - -// identity, err := local_plugin_runtime.Identity() -// if err != nil { -// return nil, errors.Join(err, errors.New("failed to get identity")) -// } - -// // local plugin -// routine.Submit(func() { -// defer func() { -// if r := recover(); r != nil { -// // print stack trace -// buf := make([]byte, 1<<16) -// runtime.Stack(buf, true) -// log.Error("plugin runtime error: %v, stack trace: %s", r, string(buf)) -// } -// }() -// // delete the plugin from the storage when the plugin is stopped -// p.fullDuplexLifecycle(local_plugin_runtime, nil) -// }) - -// // wait for the plugin to start -// var timeout_duration time.Duration -// if timeout == "" { -// timeout_duration = 120 * time.Second -// } else { -// timeout_duration, err = time.ParseDuration(timeout) -// if err != nil { -// return nil, errors.Join(err, errors.New("failed to parse timeout")) -// } -// } -// select { -// case <-local_plugin_runtime.WaitStarted(): -// case <-time.After(timeout_duration): -// return nil, errors.New("failed to start plugin after " + timeout_duration.String()) -// } - -// session := session_manager.NewSession( -// session_manager.NewSessionPayload{ -// TenantID: "test-tenant", -// UserID: "test-user", -// PluginUniqueIdentifier: identity, -// ClusterID: "test-cluster", -// InvokeFrom: access_type, -// Action: access_action, -// Declaration: plugin.runtime.Configuration(), -// BackwardsInvocation: manager.BackwardsInvocation(), -// IgnoreCache: true, -// }, -// ) -// session.BindRuntime(local_plugin_runtime) -// defer session.Close(session_manager.CloseSessionPayload{ -// IgnoreCache: true, -// }) - -// // try send request -// plugin_response, err := plugin_daemon.GenericInvokePlugin[map[string]any, any](session, &request, 1024) -// if err != nil { -// return nil, errors.Join(err, errors.New("failed to invoke plugin")) -// } - -// plugin_response.OnClose(func() { -// local_plugin_runtime.Stop() -// }) - -// return plugin_response, nil -// } diff --git a/internal/core/serverless_runtime/init.go b/internal/core/serverless_runtime/init.go deleted file mode 100644 index 8814cb9c53..0000000000 --- a/internal/core/serverless_runtime/init.go +++ /dev/null @@ -1 +0,0 @@ -package serverless_runtime diff --git a/pkg/plugin_packager/signer/sign.go b/pkg/plugin_packager/signer/sign.go index 8b3c8dcd99..d4fd2d3034 100644 --- a/pkg/plugin_packager/signer/sign.go +++ b/pkg/plugin_packager/signer/sign.go @@ -25,14 +25,3 @@ func SignPlugin(plugin []byte, verification *decoder.Verification) ([]byte, erro return withkey.SignPluginWithPrivateKey(plugin, verification, privateKey) } -// TraditionalSignPlugin, only used for testing -// WARNING: This function is deprecated, use SignPlugin instead -func TraditionalSignPlugin(plugin []byte) ([]byte, error) { - // load private key - privateKey, err := encryption.LoadPrivateKey(private_key.PRIVATE_KEY) - if err != nil { - return nil, err - } - - return withkey.TraditionalSignPlugin(plugin, privateKey) -} diff --git a/pkg/plugin_packager/signer/withkey/sign_with_key.go b/pkg/plugin_packager/signer/withkey/sign_with_key.go index 69fe609dc7..1037a6069c 100644 --- a/pkg/plugin_packager/signer/withkey/sign_with_key.go +++ b/pkg/plugin_packager/signer/withkey/sign_with_key.go @@ -6,7 +6,6 @@ import ( "crypto/rsa" "crypto/sha256" "encoding/base64" - "errors" "io" "path" "strconv" @@ -30,10 +29,6 @@ func SignPluginWithPrivateKey( return nil, err } - if verification == nil { - return nil, errors.New("verification cannot be nil") - } - // create a new zip writer zipBuffer := new(bytes.Buffer) zipWriter := zip.NewWriter(zipBuffer) @@ -74,116 +69,31 @@ func SignPluginWithPrivateKey( return nil, err } - // write the verification into data - // NOTE: .verification.dify.json is a special file that contains the verification information - // and it will be placed at the end of the zip file, checksum is calculated using it also - verificationBytes := parser.MarshalJsonBytes(verification) - - // write verification into the zip file - fileWriter, err := zipWriter.Create(consts.VERIFICATION_FILE) - if err != nil { - return nil, err - } - - if _, err := fileWriter.Write(verificationBytes); err != nil { - return nil, err - } - - // hash the verification - hash := sha256.New() - hash.Write(verificationBytes) - hashed := hash.Sum(nil) - - // write the hash into data - if _, err := data.Write(hashed); err != nil { - return nil, err - } - - // get current time - ct := time.Now().Unix() - - // convert time to bytes - timeString := strconv.FormatInt(ct, 10) - - // write the time into data - data.Write([]byte(timeString)) - - // sign the data - signature, err := encryption.RSASign(privateKey, data.Bytes()) - if err != nil { - return nil, err - } - - // write the signature into the comment field of the zip file - comments := parser.MarshalJson(map[string]any{ - "signature": base64.StdEncoding.EncodeToString(signature), - "time": ct, - }) - - // write signature - err = zipWriter.SetComment(comments) - if err != nil { - return nil, err - } - - // close the zip writer - err = zipWriter.Close() - if err != nil { - return nil, err - } - - return zipBuffer.Bytes(), nil -} - -// Only used for testing -// WARNING: This function is deprecated, use SignPluginWithPrivateKey instead -func TraditionalSignPlugin( - plugin []byte, - privateKey *rsa.PrivateKey, -) ([]byte, error) { - decoder, err := decoder.NewZipPluginDecoder(plugin) - if err != nil { - return nil, err - } + if verification != nil { + // write the verification into data + // NOTE: .verification.dify.json is a special file that contains the verification information + // and it will be placed at the end of the zip file, checksum is calculated using it also + verificationBytes := parser.MarshalJsonBytes(verification) - // create a new zip writer - zipBuffer := new(bytes.Buffer) - zipWriter := zip.NewWriter(zipBuffer) - - defer zipWriter.Close() - // store temporary hash - data := new(bytes.Buffer) - // read one by one - err = decoder.Walk(func(filename, dir string) error { - file, err := decoder.ReadFile(path.Join(dir, filename)) + // write verification into the zip file + fileWriter, err := zipWriter.Create(consts.VERIFICATION_FILE) if err != nil { - return err + return nil, err } - // calculate sha256 hash of the file + if _, err := fileWriter.Write(verificationBytes); err != nil { + return nil, err + } + + // hash the verification hash := sha256.New() - hash.Write(file) + hash.Write(verificationBytes) hashed := hash.Sum(nil) // write the hash into data - data.Write(hashed) - - // create a new file in the zip writer - fileWriter, err := zipWriter.Create(path.Join(dir, filename)) - if err != nil { - return err - } - - _, err = io.Copy(fileWriter, bytes.NewReader(file)) - if err != nil { - return err + if _, err := data.Write(hashed); err != nil { + return nil, err } - - return nil - }) - - if err != nil { - return nil, err } // get current time @@ -221,3 +131,4 @@ func TraditionalSignPlugin( return zipBuffer.Bytes(), nil } +