Skip to content

Commit 2a722a2

Browse files
authored
refactor: remove unused code and deprecated features (#422)
* clean up unused code * fix sign_with_key test
1 parent f3e9ffd commit 2a722a2

7 files changed

Lines changed: 18 additions & 269 deletions

File tree

cmd/commandline/plugin.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -196,50 +196,6 @@ If no parameters are provided, an interactive mode will be started.`,
196196
},
197197
}
198198

199-
// NOTE: tester is deprecated, maybe, in several months, we will support this again
200-
// pluginTestCommand = &cobra.Command{
201-
// Use: "test [-i inputs] [-t timeout] package_path invoke_type invoke_action",
202-
// Short: "",
203-
// 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" +
204-
// "type: invoke type, available values: \n" +
205-
// "[\n" +
206-
// " tool, model, endpoint\n" +
207-
// "]\n" +
208-
// "action: invoke action, available values: \n" +
209-
// "[\n" +
210-
// " invoke_tool, validate_tool_credentials, \n" +
211-
// " invoke_endpoint\n" +
212-
// " invoke_llm, invoke_text_embedding, invoke_rerank, invoke_tts, invoke_speech2text, invoke_moderation, \n" +
213-
// " validate_provider_credentials, validate_model_credentials, get_tts_model_voices, \n" +
214-
// " get_text_embedding_num_tokens, get_ai_model_schemas, get_llm_num_tokens\n" +
215-
// "]\n",
216-
// Run: func(cmd *cobra.Command, args []string) {
217-
// if len(args) < 3 {
218-
// log.Error("invalid args, please specify package_path, invoke_type, invoke_action")
219-
// return
220-
// }
221-
// // get package path
222-
// package_path_str := args[0]
223-
// // get invoke type
224-
// invoke_type_str := args[1]
225-
// // get invoke action
226-
// invoke_action_str := args[2]
227-
// // get inputs if specified
228-
// inputs := map[string]any{}
229-
// if cmd.Flag("inputs") != nil {
230-
// inputs_str := cmd.Flag("inputs").Value.String()
231-
// err := json.Unmarshal([]byte(inputs_str), &inputs)
232-
// if err != nil {
233-
// log.Error("failed to unmarshal inputs, inputs: %s, error: %v", inputs_str, err)
234-
// return
235-
// }
236-
// }
237-
// // parse flag
238-
// timeout := ""
239-
// if cmd.Flag("timeout") != nil {
240-
// timeout = cmd.Flag("timeout").Value.String()
241-
// }
242-
243199
)
244200

245201
func init() {

cmd/commandline/signature/signature_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func TestVerifyPluginWithoutVerificationField(t *testing.T) {
285285
t.Fatalf("Failed to create dummy plugin file: %v", err)
286286
}
287287

288-
pluginPackageWithoutVerificationField, err := signer.TraditionalSignPlugin(dummyPlugin)
288+
pluginPackageWithoutVerificationField, err := signer.SignPlugin(dummyPlugin, nil)
289289
if err != nil {
290290
t.Fatalf("Failed to sign plugin: %v", err)
291291
}

internal/core/local_runtime/tester.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

internal/core/plugin_manager/tester.go

Lines changed: 0 additions & 105 deletions
This file was deleted.

internal/core/serverless_runtime/init.go

Lines changed: 0 additions & 1 deletion
This file was deleted.

pkg/plugin_packager/signer/sign.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,3 @@ func SignPlugin(plugin []byte, verification *decoder.Verification) ([]byte, erro
2525
return withkey.SignPluginWithPrivateKey(plugin, verification, privateKey)
2626
}
2727

28-
// TraditionalSignPlugin, only used for testing
29-
// WARNING: This function is deprecated, use SignPlugin instead
30-
func TraditionalSignPlugin(plugin []byte) ([]byte, error) {
31-
// load private key
32-
privateKey, err := encryption.LoadPrivateKey(private_key.PRIVATE_KEY)
33-
if err != nil {
34-
return nil, err
35-
}
36-
37-
return withkey.TraditionalSignPlugin(plugin, privateKey)
38-
}

pkg/plugin_packager/signer/withkey/sign_with_key.go

Lines changed: 17 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"crypto/rsa"
77
"crypto/sha256"
88
"encoding/base64"
9-
"errors"
109
"io"
1110
"path"
1211
"strconv"
@@ -30,10 +29,6 @@ func SignPluginWithPrivateKey(
3029
return nil, err
3130
}
3231

33-
if verification == nil {
34-
return nil, errors.New("verification cannot be nil")
35-
}
36-
3732
// create a new zip writer
3833
zipBuffer := new(bytes.Buffer)
3934
zipWriter := zip.NewWriter(zipBuffer)
@@ -74,116 +69,31 @@ func SignPluginWithPrivateKey(
7469
return nil, err
7570
}
7671

77-
// write the verification into data
78-
// NOTE: .verification.dify.json is a special file that contains the verification information
79-
// and it will be placed at the end of the zip file, checksum is calculated using it also
80-
verificationBytes := parser.MarshalJsonBytes(verification)
81-
82-
// write verification into the zip file
83-
fileWriter, err := zipWriter.Create(consts.VERIFICATION_FILE)
84-
if err != nil {
85-
return nil, err
86-
}
87-
88-
if _, err := fileWriter.Write(verificationBytes); err != nil {
89-
return nil, err
90-
}
91-
92-
// hash the verification
93-
hash := sha256.New()
94-
hash.Write(verificationBytes)
95-
hashed := hash.Sum(nil)
96-
97-
// write the hash into data
98-
if _, err := data.Write(hashed); err != nil {
99-
return nil, err
100-
}
101-
102-
// get current time
103-
ct := time.Now().Unix()
104-
105-
// convert time to bytes
106-
timeString := strconv.FormatInt(ct, 10)
107-
108-
// write the time into data
109-
data.Write([]byte(timeString))
110-
111-
// sign the data
112-
signature, err := encryption.RSASign(privateKey, data.Bytes())
113-
if err != nil {
114-
return nil, err
115-
}
116-
117-
// write the signature into the comment field of the zip file
118-
comments := parser.MarshalJson(map[string]any{
119-
"signature": base64.StdEncoding.EncodeToString(signature),
120-
"time": ct,
121-
})
122-
123-
// write signature
124-
err = zipWriter.SetComment(comments)
125-
if err != nil {
126-
return nil, err
127-
}
128-
129-
// close the zip writer
130-
err = zipWriter.Close()
131-
if err != nil {
132-
return nil, err
133-
}
134-
135-
return zipBuffer.Bytes(), nil
136-
}
137-
138-
// Only used for testing
139-
// WARNING: This function is deprecated, use SignPluginWithPrivateKey instead
140-
func TraditionalSignPlugin(
141-
plugin []byte,
142-
privateKey *rsa.PrivateKey,
143-
) ([]byte, error) {
144-
decoder, err := decoder.NewZipPluginDecoder(plugin)
145-
if err != nil {
146-
return nil, err
147-
}
72+
if verification != nil {
73+
// write the verification into data
74+
// NOTE: .verification.dify.json is a special file that contains the verification information
75+
// and it will be placed at the end of the zip file, checksum is calculated using it also
76+
verificationBytes := parser.MarshalJsonBytes(verification)
14877

149-
// create a new zip writer
150-
zipBuffer := new(bytes.Buffer)
151-
zipWriter := zip.NewWriter(zipBuffer)
152-
153-
defer zipWriter.Close()
154-
// store temporary hash
155-
data := new(bytes.Buffer)
156-
// read one by one
157-
err = decoder.Walk(func(filename, dir string) error {
158-
file, err := decoder.ReadFile(path.Join(dir, filename))
78+
// write verification into the zip file
79+
fileWriter, err := zipWriter.Create(consts.VERIFICATION_FILE)
15980
if err != nil {
160-
return err
81+
return nil, err
16182
}
16283

163-
// calculate sha256 hash of the file
84+
if _, err := fileWriter.Write(verificationBytes); err != nil {
85+
return nil, err
86+
}
87+
88+
// hash the verification
16489
hash := sha256.New()
165-
hash.Write(file)
90+
hash.Write(verificationBytes)
16691
hashed := hash.Sum(nil)
16792

16893
// write the hash into data
169-
data.Write(hashed)
170-
171-
// create a new file in the zip writer
172-
fileWriter, err := zipWriter.Create(path.Join(dir, filename))
173-
if err != nil {
174-
return err
175-
}
176-
177-
_, err = io.Copy(fileWriter, bytes.NewReader(file))
178-
if err != nil {
179-
return err
94+
if _, err := data.Write(hashed); err != nil {
95+
return nil, err
18096
}
181-
182-
return nil
183-
})
184-
185-
if err != nil {
186-
return nil, err
18797
}
18898

18999
// get current time
@@ -221,3 +131,4 @@ func TraditionalSignPlugin(
221131

222132
return zipBuffer.Bytes(), nil
223133
}
134+

0 commit comments

Comments
 (0)