forked from Velocidex/velociraptor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflect.go
209 lines (181 loc) · 5.46 KB
/
reflect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
Velociraptor - Dig Deeper
Copyright (C) 2019-2024 Rapid7 Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package api
import (
"regexp"
"strings"
"github.com/Velocidex/yaml/v2"
context "golang.org/x/net/context"
"google.golang.org/protobuf/types/known/emptypb"
api_proto "www.velocidex.com/golang/velociraptor/api/proto"
"www.velocidex.com/golang/velociraptor/artifacts/assets"
artifacts_proto "www.velocidex.com/golang/velociraptor/artifacts/proto"
"www.velocidex.com/golang/velociraptor/services"
vql_subsystem "www.velocidex.com/golang/velociraptor/vql"
"www.velocidex.com/golang/vfilter"
"www.velocidex.com/golang/vfilter/types"
)
var (
doc_regex = regexp.MustCompile("doc=(.+)")
)
// Loads the api description from the embedded asset
func LoadApiDescription() ([]*api_proto.Completion, error) {
assets.InitOnce()
data, err := assets.ReadFile("docs/references/vql.yaml")
if err != nil {
return nil, err
}
result := []*api_proto.Completion{}
err = yaml.Unmarshal(data, &result)
return result, err
}
func IntrospectDescription() []*api_proto.Completion {
result := []*api_proto.Completion{}
scope := vql_subsystem.MakeScope()
defer scope.Close()
type_map := types.NewTypeMap()
info := scope.Describe(type_map)
for _, item := range info.Functions {
var metadata map[string]string
if item.Metadata != nil {
metadata = make(map[string]string)
for _, k := range item.Metadata.Keys() {
v, _ := item.Metadata.GetString(k)
metadata[k] = v
}
}
result = append(result, &api_proto.Completion{
Name: item.Name,
Description: item.Doc,
Type: "Function",
Args: getArgDescriptors(item.ArgType, type_map, scope),
Metadata: metadata,
})
}
for _, item := range info.Plugins {
var metadata map[string]string
if item.Metadata != nil {
metadata = make(map[string]string)
for _, k := range item.Metadata.Keys() {
v, _ := item.Metadata.GetString(k)
metadata[k] = v
}
}
result = append(result, &api_proto.Completion{
Name: item.Name,
Description: item.Doc,
Type: "Plugin",
Args: getArgDescriptors(item.ArgType, type_map, scope),
Metadata: metadata,
})
}
return result
}
func (self *ApiServer) GetKeywordCompletions(
ctx context.Context,
in *emptypb.Empty) (*api_proto.KeywordCompletions, error) {
users := services.GetUserManager()
_, org_config_obj, err := users.GetUserFromContext(ctx)
if err != nil {
return nil, Status(self.verbose, err)
}
result := &api_proto.KeywordCompletions{
Items: []*api_proto.Completion{
{Name: "EXPLAIN", Type: "Keyword"},
{Name: "SELECT", Type: "Keyword"},
{Name: "FROM", Type: "Keyword"},
{Name: "LET", Type: "Keyword"},
{Name: "WHERE", Type: "Keyword"},
{Name: "LIMIT", Type: "Keyword"},
{Name: "GROUP BY", Type: "Keyword"},
{Name: "ORDER BY", Type: "Keyword"},
},
}
descriptions, err := LoadApiDescription()
if err != nil {
descriptions = IntrospectDescription()
}
result.Items = append(result.Items, descriptions...)
manager, err := services.GetRepositoryManager(org_config_obj)
if err != nil {
return nil, Status(self.verbose, err)
}
repository, err := manager.GetGlobalRepository(org_config_obj)
if err != nil {
return nil, Status(self.verbose, err)
}
names, err := repository.List(ctx, org_config_obj)
if err != nil {
return nil, Status(self.verbose, err)
}
for _, name := range names {
artifact, pres := repository.Get(ctx, org_config_obj, name)
if !pres {
continue
}
result.Items = append(result.Items, &api_proto.Completion{
Name: "Artifact." + name,
Type: "Artifact",
Args: getArtifactParamDescriptors(artifact),
})
}
return result, nil
}
func getArgDescriptors(
arg_type string,
type_map *vfilter.TypeMap,
scope vfilter.Scope) []*api_proto.ArgDescriptor {
args := []*api_proto.ArgDescriptor{}
arg_desc, pres := type_map.Get(scope, arg_type)
if pres && arg_desc != nil && arg_desc.Fields != nil {
for _, k := range arg_desc.Fields.Keys() {
v_any, _ := arg_desc.Fields.Get(k)
v, ok := v_any.(*types.TypeReference)
if !ok {
continue
}
target := v.Target
if v.Repeated {
target = " list of " + target
}
required := ""
if strings.Contains(v.Tag, "required") {
required = "(required)"
}
doc := ""
matches := doc_regex.FindStringSubmatch(v.Tag)
if matches != nil {
doc = matches[1]
}
args = append(args, &api_proto.ArgDescriptor{
Name: k,
Description: doc + required,
Type: target,
})
}
}
return args
}
func getArtifactParamDescriptors(artifact *artifacts_proto.Artifact) []*api_proto.ArgDescriptor {
args := []*api_proto.ArgDescriptor{}
for _, parameter := range artifact.Parameters {
args = append(args, &api_proto.ArgDescriptor{
Name: parameter.Name,
Description: parameter.Description,
Type: "Artifact Parameter",
})
}
return args
}