forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoracle.go
202 lines (167 loc) · 5.1 KB
/
oracle.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
// Copyright 2020 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package cmd
import (
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/bundle"
"github.com/open-policy-agent/opa/cmd/internal/env"
"github.com/open-policy-agent/opa/internal/oracle"
"github.com/open-policy-agent/opa/internal/presentation"
"github.com/open-policy-agent/opa/loader"
)
type findDefinitionParams struct {
stdinBuffer bool
bundlePaths repeatedStringFlag
v0Compatible bool
v1Compatible bool
}
func (p *findDefinitionParams) regoVersion() ast.RegoVersion {
// v0 takes precedence over v1
if p.v0Compatible {
return ast.RegoV0
}
if p.v1Compatible {
return ast.RegoV1
}
return ast.DefaultRegoVersion
}
func init() {
var findDefinitionParams findDefinitionParams
var oracleCommand = &cobra.Command{
Use: "oracle",
Short: "Answer questions about Rego",
Long: "Answer questions about Rego.",
Hidden: true,
}
var findDefinitionCommand = &cobra.Command{
Use: "find-definition",
Short: "Find the location of a definition",
Long: `Find the location of a definition.
The 'find-definition' command outputs the location of the definition of the symbol
or value referred to by the location passed as a positional argument. The location
should be of the form:
<filename>:<offset>
The offset can be specified as a decimal or hexadecimal number. The output format
specifies the file, row, and column of the definition:
{
"result": {
"file": "/path/to/some/policy.rego",
"row": 18,
"col": 1
}
}
If the 'find-definition' command cannot find a location it will print an error
reason. The exit status will be zero in this case:
{
"error": "no match found"
}
If an unexpected error occurs (e.g., a file read error) the subcommand will print
the error reason to stderr and exit with a non-zero status code.
If the --stdin-buffer flag is supplied the 'find-definition' subcommand will
consume stdin and treat the bytes read as the content of the file referenced
by the input location.`,
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("expected exactly one position <filename>:<offset>")
}
if _, _, err := parseFilenameOffset(args[0]); err != nil {
return err
}
return env.CmdFlags.CheckEnvironmentVariables(cmd)
},
Run: func(_ *cobra.Command, args []string) {
if err := dofindDefinition(findDefinitionParams, os.Stdin, os.Stdout, args); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
},
}
findDefinitionCommand.Flags().BoolVarP(&findDefinitionParams.stdinBuffer, "stdin-buffer", "", false, "read buffer from stdin")
addBundleFlag(findDefinitionCommand.Flags(), &findDefinitionParams.bundlePaths)
oracleCommand.AddCommand(findDefinitionCommand)
addV0CompatibleFlag(oracleCommand.Flags(), &findDefinitionParams.v0Compatible, false)
addV1CompatibleFlag(oracleCommand.Flags(), &findDefinitionParams.v1Compatible, false)
RootCommand.AddCommand(oracleCommand)
}
func dofindDefinition(params findDefinitionParams, stdin io.Reader, stdout io.Writer, args []string) error {
filename, offset, err := parseFilenameOffset(args[0])
if err != nil {
return err
}
var b *bundle.Bundle
if len(params.bundlePaths.v) != 0 {
if len(params.bundlePaths.v) > 1 {
return errors.New("not implemented: multiple bundle paths")
}
b, err = loader.NewFileLoader().
WithSkipBundleVerification(true).
WithFilter(func(_ string, info os.FileInfo, _ int) bool {
// While directories may contain other things of interest for OPA (json, yaml..),
// only .rego will work reliably for the purpose of finding definitions
return strings.HasPrefix(info.Name(), ".rego")
}).
WithRegoVersion(params.regoVersion()).
AsBundle(params.bundlePaths.v[0])
if err != nil {
return err
}
}
modules := map[string]*ast.Module{}
if b != nil {
for _, mf := range b.Modules {
modules[mf.Path] = mf.Parsed
}
}
var bs []byte
if params.stdinBuffer {
stat, err := os.Stdin.Stat()
if err != nil {
return err
}
// Only read from stdin when there is something actually there
if (stat.Mode() & os.ModeCharDevice) == 0 {
bs, err = io.ReadAll(stdin)
if err != nil {
return err
}
}
}
result, err := oracle.New().FindDefinition(oracle.DefinitionQuery{
Buffer: bs,
Filename: filename,
Pos: offset,
Modules: modules,
})
if err != nil {
return presentation.JSON(stdout, map[string]interface{}{
"error": err,
})
}
return presentation.JSON(stdout, result)
}
func parseFilenameOffset(s string) (string, int, error) {
s = strings.TrimPrefix(s, "file://")
parts := strings.Split(s, ":")
if len(parts) != 2 {
return "", 0, errors.New("expected <filename>:<offset> argument")
}
base := 10
str := parts[1]
if strings.HasPrefix(parts[1], "0x") {
base = 16
str = parts[1][2:]
}
offset, err := strconv.ParseInt(str, base, 32)
if err != nil {
return "", 0, err
}
return parts[0], int(offset), nil
}