Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ee/agent-smith/cmd/signature-new.go
2500 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"encoding/json"
9
"fmt"
10
"log"
11
"os"
12
"strconv"
13
14
"github.com/gitpod-io/gitpod/agent-smith/pkg/classifier"
15
"github.com/spf13/cobra"
16
)
17
18
// signatureNewCmd represents the newSignature command
19
var signatureNewCmd = &cobra.Command{
20
Use: "new <name>",
21
Short: "produces a signature JSON",
22
Args: cobra.ExactArgs(1),
23
Run: func(cmd *cobra.Command, args []string) {
24
ss, err := strconv.ParseInt(cmd.Flags().Lookup("slice-start").Value.String(), 10, 64)
25
if err != nil {
26
log.Fatal(err)
27
}
28
se, err := strconv.ParseInt(cmd.Flags().Lookup("slice-end").Value.String(), 10, 64)
29
if err != nil {
30
log.Fatal(err)
31
}
32
33
fn := cmd.Flags().Lookup("filename").Value.String()
34
var fns []string
35
if fn != "" {
36
fns = []string{fn}
37
}
38
39
kinds := map[string]classifier.ObjectKind{
40
"elf-symbols": classifier.ObjectELFSymbols,
41
"elf-rodata": classifier.ObjectELFRodata,
42
"expensive-any": classifier.ObjectAny,
43
}
44
kindv := cmd.Flags().Lookup("kind").Value.String()
45
kind, ok := kinds[kindv]
46
if !ok {
47
fmt.Fprintf(os.Stderr, "unknown kind: %s\n", kindv)
48
fmt.Fprintf(os.Stderr, "valid choices are (--kind):\n")
49
for k := range kinds {
50
fmt.Fprintf(os.Stderr, "\t%s\n", k)
51
}
52
os.Exit(1)
53
}
54
55
sig := classifier.Signature{
56
Name: args[0],
57
Kind: kind,
58
Pattern: []byte(cmd.Flags().Lookup("pattern").Value.String()),
59
Regexp: cmd.Flags().Lookup("regexp").Value.String() == "true",
60
Slice: classifier.Slice{
61
Start: ss,
62
End: se,
63
},
64
Filename: fns,
65
Domain: classifier.DomainProcess,
66
}
67
err = sig.Validate()
68
if err != nil {
69
log.Fatal(err)
70
}
71
out, err := json.Marshal(sig)
72
if err != nil {
73
log.Fatal(err)
74
}
75
76
fmt.Println(string(out))
77
},
78
}
79
80
func init() {
81
signatureCmd.AddCommand(signatureNewCmd)
82
83
signatureNewCmd.Flags().BoolP("regexp", "r", false, "Make this a regexp signature")
84
signatureNewCmd.Flags().StringP("pattern", "p", "", "The pattern of this signature")
85
signatureNewCmd.Flags().StringP("kind", "k", "", "The kind of this signature (either empty string for any or ELF)")
86
signatureNewCmd.Flags().StringP("filename", "f", "", "The filename this signature can apply to")
87
signatureNewCmd.Flags().Int("slice-start", 0, "Start of the signature slice")
88
signatureNewCmd.Flags().Int("slice-end", 0, "End of the signature slice")
89
}
90
91