|
| 1 | +package builtins |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/open-policy-agent/conftest/parser" |
| 9 | + "github.com/open-policy-agent/opa/ast" |
| 10 | + "github.com/open-policy-agent/opa/ast/location" |
| 11 | + "github.com/open-policy-agent/opa/rego" |
| 12 | + "github.com/open-policy-agent/opa/types" |
| 13 | +) |
| 14 | + |
| 15 | +func init() { |
| 16 | + registerParseConfig() |
| 17 | + registerParseConfigFile() |
| 18 | +} |
| 19 | + |
| 20 | +func registerParseConfig() { |
| 21 | + decl := rego.Function{ |
| 22 | + Name: "parse_config", |
| 23 | + Decl: types.NewFunction( |
| 24 | + types.Args(types.S, types.S), // parser name, configuration |
| 25 | + types.NewObject(nil, types.NewDynamicProperty(types.S, types.NewAny())), // map[string]interface{} aka JSON |
| 26 | + ), |
| 27 | + } |
| 28 | + rego.RegisterBuiltin2(&decl, parseConfig) |
| 29 | +} |
| 30 | + |
| 31 | +func registerParseConfigFile() { |
| 32 | + decl := rego.Function{ |
| 33 | + Name: "parse_config_file", |
| 34 | + Decl: types.NewFunction( |
| 35 | + types.Args(types.S), // path to configuration file |
| 36 | + types.NewObject(nil, types.NewDynamicProperty(types.S, types.NewAny())), // map[string]interface{} aka JSON |
| 37 | + ), |
| 38 | + } |
| 39 | + rego.RegisterBuiltin1(&decl, parseConfigFile) |
| 40 | +} |
| 41 | + |
| 42 | +// parseConfig takes a parser name and configuration as strings and returns the |
| 43 | +// parsed configuration as a Rego object. This can be used to parse all of the |
| 44 | +// configuration formats conftest supports in-line in Rego policies. |
| 45 | +func parseConfig(bctx rego.BuiltinContext, op1, op2 *ast.Term) (*ast.Term, error) { |
| 46 | + args, err := decodeArgs([]*ast.Term{op1, op2}) |
| 47 | + if err != nil { |
| 48 | + return nil, fmt.Errorf("decode args: %w", err) |
| 49 | + } |
| 50 | + parserName, ok := args[0].(string) |
| 51 | + if !ok { |
| 52 | + return nil, fmt.Errorf("parser name %v [%T] is not expected type string", args[0], args[0]) |
| 53 | + } |
| 54 | + config, ok := args[1].(string) |
| 55 | + if !ok { |
| 56 | + return nil, fmt.Errorf("config %v [%T] is not expected type string", args[1], args[1]) |
| 57 | + } |
| 58 | + parser, err := parser.New(parserName) |
| 59 | + if err != nil { |
| 60 | + return nil, fmt.Errorf("create config parser: %w", err) |
| 61 | + } |
| 62 | + |
| 63 | + return toAST(bctx, parser, []byte(config)) |
| 64 | +} |
| 65 | + |
| 66 | +// parseConfigFile takes a config file path, parses the config file, and |
| 67 | +// returns the parsed configuration as a Rego object. |
| 68 | +func parseConfigFile(bctx rego.BuiltinContext, op1 *ast.Term) (*ast.Term, error) { |
| 69 | + args, err := decodeArgs([]*ast.Term{op1}) |
| 70 | + if err != nil { |
| 71 | + return nil, fmt.Errorf("decode args: %w", err) |
| 72 | + } |
| 73 | + file, ok := args[0].(string) |
| 74 | + if !ok { |
| 75 | + return nil, fmt.Errorf("file %v [%T] is not expected type string", args[0], args[0]) |
| 76 | + } |
| 77 | + filePath := filepath.Join(filepath.Dir(bctx.Location.File), file) |
| 78 | + parser, err := parser.NewFromPath(filePath) |
| 79 | + if err != nil { |
| 80 | + return nil, fmt.Errorf("create config parser: %w", err) |
| 81 | + } |
| 82 | + contents, err := ioutil.ReadFile(filePath) |
| 83 | + if err != nil { |
| 84 | + return nil, fmt.Errorf("read config file %s: %w", filePath, err) |
| 85 | + } |
| 86 | + |
| 87 | + return toAST(bctx, parser, contents) |
| 88 | +} |
| 89 | + |
| 90 | +func decodeArgs(args []*ast.Term) ([]interface{}, error) { |
| 91 | + decoded := make([]interface{}, len(args)) |
| 92 | + for i, arg := range args { |
| 93 | + iface, err := ast.ValueToInterface(arg.Value, nil) |
| 94 | + if err != nil { |
| 95 | + return nil, fmt.Errorf("ast.ValueToInterface: %w", err) |
| 96 | + } |
| 97 | + decoded[i] = iface |
| 98 | + } |
| 99 | + |
| 100 | + return decoded, nil |
| 101 | +} |
| 102 | + |
| 103 | +func toAST(bctx rego.BuiltinContext, parser parser.Parser, contents []byte) (*ast.Term, error) { |
| 104 | + var cfg map[string]interface{} |
| 105 | + if err := parser.Unmarshal(contents, &cfg); err != nil { |
| 106 | + return nil, fmt.Errorf("unmarshal config: %w", err) |
| 107 | + } |
| 108 | + val, err := ast.InterfaceToValue(cfg) |
| 109 | + if err != nil { |
| 110 | + return nil, fmt.Errorf("convert config to ast.Value: %w", err) |
| 111 | + } |
| 112 | + var loc *location.Location |
| 113 | + if bctx.Location != nil { |
| 114 | + loc = bctx.Location |
| 115 | + } else { |
| 116 | + loc = &ast.Location{ |
| 117 | + File: "-", // stdin |
| 118 | + Text: contents, |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return &ast.Term{Value: val, Location: loc}, nil |
| 123 | +} |
0 commit comments