forked from open-policy-agent/opa
/
inspect.go
217 lines (179 loc) · 5.03 KB
/
inspect.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
210
211
212
213
214
215
216
217
// Copyright 2021 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 (
"encoding/json"
"fmt"
"io"
"os"
"sort"
"strings"
"github.com/meta-quick/opax/bundle"
ib "github.com/meta-quick/opax/internal/bundle/inspect"
pr "github.com/meta-quick/opax/internal/presentation"
iStrs "github.com/meta-quick/opax/internal/strings"
"github.com/meta-quick/opax/util"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)
const maxTableFieldLen = 50
type inspectCommandParams struct {
outputFormat *util.EnumFlag
}
func newInspectCommandParams() inspectCommandParams {
return inspectCommandParams{
outputFormat: util.NewEnumFlag(evalPrettyOutput, []string{
evalJSONOutput,
evalPrettyOutput,
}),
}
}
func init() {
params := newInspectCommandParams()
var inspectCommand = &cobra.Command{
Use: "inspect <path> [<path> [...]]",
Short: "Inspect OPA bundle(s)",
Long: `Inspect OPA bundle(s).
The 'inspect' command provides a summary of the contents in OPA bundle(s). Bundles are
gzipped tarballs containing policies and data. The 'inspect' command reads bundle(s) and lists
the following:
* packages that are contributed by .rego files
* data locations defined by the data.json and data.yaml files
* manifest data
* signature data
* information about the Wasm module files
Example:
$ ls
bundle.tar.gz
$ opa inspect bundle.tar.gz
You can provide exactly one OPA bundle or path to the 'inspect' command on the command-line. If you provide a path
referring to a directory, the 'inspect' command will load that path as a bundle and summarize its structure and contents.
`,
PreRunE: func(_ *cobra.Command, args []string) error {
return validateInspectParams(¶ms, args)
},
Run: func(_ *cobra.Command, args []string) {
if err := doInspect(params, args[0], os.Stdout); err != nil {
fmt.Fprintln(os.Stderr, "error:", err)
os.Exit(1)
}
},
}
addOutputFormat(inspectCommand.Flags(), params.outputFormat)
RootCommand.AddCommand(inspectCommand)
}
func doInspect(params inspectCommandParams, path string, out io.Writer) error {
info, err := ib.File(path)
if err != nil {
return err
}
switch params.outputFormat.String() {
case evalJSONOutput:
return pr.JSON(out, info)
default:
if info.Manifest.Revision != "" || len(*info.Manifest.Roots) != 0 || len(info.Manifest.Metadata) != 0 {
if err := populateManifest(out, info.Manifest); err != nil {
return err
}
}
if len(info.Namespaces) != 0 {
if err := populateNamespaces(out, info.Namespaces); err != nil {
return err
}
}
return nil
}
}
func validateInspectParams(p *inspectCommandParams, args []string) error {
if len(args) != 1 {
return fmt.Errorf("specify exactly one OPA bundle or path")
}
of := p.outputFormat.String()
if of == evalJSONOutput || of == evalPrettyOutput {
return nil
}
return fmt.Errorf("invalid output format for inspect command")
}
func populateManifest(out io.Writer, m bundle.Manifest) error {
t := generateTableWithKeys(out, "field", "value")
lines := [][]string{}
if m.Revision != "" {
lines = append(lines, []string{"Revision", truncateStr(m.Revision)})
}
if len(*m.Roots) != 0 {
roots := *m.Roots
if len(roots) == 1 {
if roots[0] != "" {
lines = append(lines, []string{"Roots", truncateFileName(roots[0])})
}
} else {
sort.Strings(roots)
for _, root := range roots {
lines = append(lines, []string{"Roots", truncateFileName(root)})
}
}
}
if len(m.Metadata) != 0 {
metadata, err := json.Marshal(m.Metadata)
if err != nil {
return err
}
lines = append(lines, []string{"Metadata", truncateStr(string(metadata))})
}
t.AppendBulk(lines)
if t.NumLines() > 0 {
fmt.Fprintln(out, "MANIFEST:")
t.Render()
}
return nil
}
func populateNamespaces(out io.Writer, n map[string][]string) error {
t := generateTableWithKeys(out, "namespace", "file")
lines := [][]string{}
var keys []string
for k := range n {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
for _, file := range n[k] {
lines = append(lines, []string{k, truncateFileName(file)})
}
}
t.AppendBulk(lines)
if t.NumLines() > 0 {
fmt.Fprintln(out, "NAMESPACES:")
t.Render()
}
return nil
}
func generateTableWithKeys(writer io.Writer, keys ...string) *tablewriter.Table {
table := tablewriter.NewWriter(writer)
aligns := []int{}
var hdrs []string
for _, k := range keys {
hdrs = append(hdrs, strings.Title((k)))
aligns = append(aligns, tablewriter.ALIGN_LEFT)
}
table.SetHeader(hdrs)
table.SetAlignment(tablewriter.ALIGN_CENTER)
table.SetColumnAlignment(aligns)
table.SetAutoMergeCells(true)
table.SetRowLine(false)
table.SetAutoWrapText(false)
return table
}
func truncateStr(s string) string {
if len(s) < maxTableFieldLen {
return s
}
return fmt.Sprintf("%v...", s[:maxTableFieldLen])
}
func truncateFileName(s string) string {
if len(s) < maxTableFieldLen {
return s
}
res, _ := iStrs.TruncateFilePaths(maxTableFieldLen, len(s), s)
return res[s]
}