mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
internal/bundle/inspect: add required capabilities to output
This commit adds the required capabilities ot the output of the inspect operation. This allows users who are determined enough to lookup the capabilities for the bundle. This is just the MVP so only the JSON format will show the capabilities. Signed-off-by: Torin Sandall <torin@styra.com>
This commit is contained in:
committed by
Torin Sandall
parent
340611de83
commit
3e6f747743
@@ -3226,6 +3226,21 @@ func category(cs ...string) []string {
|
||||
return cs
|
||||
}
|
||||
|
||||
// Minimal returns a shallow copy of b with the descriptions and categories and
|
||||
// named arguments stripped out.
|
||||
func (b *Builtin) Minimal() *Builtin {
|
||||
cpy := *b
|
||||
fargs := b.Decl.FuncArgs()
|
||||
if fargs.Variadic != nil {
|
||||
cpy.Decl = types.NewVariadicFunction(fargs.Args, fargs.Variadic, b.Decl.Result())
|
||||
} else {
|
||||
cpy.Decl = types.NewFunction(fargs.Args, b.Decl.Result())
|
||||
}
|
||||
cpy.Categories = nil
|
||||
cpy.Description = ""
|
||||
return &cpy
|
||||
}
|
||||
|
||||
// IsDeprecated returns true if the Builtin function is deprecated and will be removed in a future release.
|
||||
func (b *Builtin) IsDeprecated() bool {
|
||||
return b.deprecated
|
||||
|
||||
+4
-4
@@ -26,14 +26,14 @@ const FeatureRefHeadStringPrefixes = "rule_head_ref_string_prefixes"
|
||||
// Capabilities defines a structure containing data that describes the capabilities
|
||||
// or features supported by a particular version of OPA.
|
||||
type Capabilities struct {
|
||||
Builtins []*Builtin `json:"builtins"`
|
||||
FutureKeywords []string `json:"future_keywords"`
|
||||
WasmABIVersions []WasmABIVersion `json:"wasm_abi_versions"`
|
||||
Builtins []*Builtin `json:"builtins,omitempty"`
|
||||
FutureKeywords []string `json:"future_keywords,omitempty"`
|
||||
WasmABIVersions []WasmABIVersion `json:"wasm_abi_versions,omitempty"`
|
||||
|
||||
// Features is a bit of a mixed bag for checking that an older version of OPA
|
||||
// is able to do what needs to be done.
|
||||
// TODO(sr): find better words ^^
|
||||
Features []string `json:"features"`
|
||||
Features []string `json:"features,omitempty"`
|
||||
|
||||
// allow_net is an array of hostnames or IP addresses, that an OPA instance is
|
||||
// allowed to connect to.
|
||||
|
||||
@@ -972,6 +972,10 @@ func (c *Compiler) buildRequiredCapabilities() {
|
||||
}
|
||||
|
||||
c.Required.Features = stringMapToSortedSlice(features)
|
||||
|
||||
for i, bi := range c.Required.Builtins {
|
||||
c.Required.Builtins[i] = bi.Minimal()
|
||||
}
|
||||
}
|
||||
|
||||
func stringMapToSortedSlice(xs map[string]struct{}) []string {
|
||||
|
||||
@@ -27,6 +27,7 @@ type Info struct {
|
||||
WasmModules []map[string]interface{} `json:"wasm_modules,omitempty"`
|
||||
Namespaces map[string][]string `json:"namespaces,omitempty"`
|
||||
Annotations []*ast.AnnotationsRef `json:"annotations,omitempty"`
|
||||
Required *ast.Capabilities `json:"capabilities,omitempty"`
|
||||
}
|
||||
|
||||
func File(path string, includeAnnotations bool) (*Info, error) {
|
||||
@@ -103,6 +104,19 @@ func File(path string, includeAnnotations bool) (*Info, error) {
|
||||
}
|
||||
bi.WasmModules = wasmModules
|
||||
|
||||
moduleMap := make(map[string]*ast.Module, len(b.Modules))
|
||||
for _, f := range b.Modules {
|
||||
moduleMap[f.URL] = f.Parsed
|
||||
}
|
||||
|
||||
c := ast.NewCompiler()
|
||||
c.Compile(moduleMap)
|
||||
if c.Failed() {
|
||||
return bi, c.Errors
|
||||
}
|
||||
|
||||
bi.Required = c.Required
|
||||
|
||||
return bi, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -28,7 +28,7 @@ func TestGenerateBundleInfoWithFileDir(t *testing.T) {
|
||||
"/data.json": `{"a": {"b": {"c": [123]}}}`,
|
||||
"/foo/policy.rego": "package foo\np = 1",
|
||||
"/baz/authz.rego": "package foo\nx = 1",
|
||||
"base.rego": "package bar\nx = 1",
|
||||
"base.rego": "package bar\nx = 1 { input > 7 }",
|
||||
"/.manifest": `{"roots": ["foo", "bar", "fuz", "baz", "a"], "revision": "rev"}`,
|
||||
}
|
||||
|
||||
@@ -59,6 +59,17 @@ func TestGenerateBundleInfoWithFileDir(t *testing.T) {
|
||||
if !reflect.DeepEqual(info.Namespaces, expectedNamespaces) {
|
||||
t.Fatalf("expected namespaces %v, but got %v", expectedNamespaces, info.Namespaces)
|
||||
}
|
||||
|
||||
var builtinNames []string
|
||||
for _, bi := range info.Required.Builtins {
|
||||
builtinNames = append(builtinNames, bi.Name)
|
||||
}
|
||||
|
||||
expBuiltinNames := []string{"eq", "gt"}
|
||||
|
||||
if !reflect.DeepEqual(expBuiltinNames, builtinNames) {
|
||||
t.Fatalf("expected builtin names to be %v but got %v", expBuiltinNames, builtinNames)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,6 @@ import (
|
||||
"os"
|
||||
|
||||
"github.com/open-policy-agent/opa/ast"
|
||||
"github.com/open-policy-agent/opa/types"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@@ -25,14 +24,7 @@ func main() {
|
||||
|
||||
for i, bi := range f.Builtins {
|
||||
// NOTE(sr): This ensures that there are no type names and descriptions in capabilities.json
|
||||
fargs := bi.Decl.FuncArgs()
|
||||
if fargs.Variadic != nil {
|
||||
f.Builtins[i].Decl = types.NewVariadicFunction(fargs.Args, fargs.Variadic, bi.Decl.Result())
|
||||
} else {
|
||||
f.Builtins[i].Decl = types.NewFunction(fargs.Args, bi.Decl.Result())
|
||||
}
|
||||
f.Builtins[i].Categories = nil
|
||||
f.Builtins[i].Description = ""
|
||||
f.Builtins[i] = bi.Minimal()
|
||||
}
|
||||
|
||||
if err := enc.Encode(f); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user