有没有办法在我想要创建的新内置中调用内置(如 io.jwt.decode_verify(string, constraints))?
或者有没有办法调用 OPA 的内部包的方法?
有没有办法在我想要创建的新内置中调用内置(如 io.jwt.decode_verify(string, constraints))?
或者有没有办法调用 OPA 的内部包的方法?
您应该能够使用从函数中导出的内置topdown.GetBuiltin
函数。在下面的示例中,该base64.encode
函数是从my_custom
自定义内置函数调用的:
rego.RegisterBuiltin2(
®o.Function{
Name: "my_custom",
Decl: types.NewFunction(types.Args(types.S), types.A),
},
func(bctx rego.BuiltinContext, a, b *ast.Term) (*ast.Term, error) {
b64encode := topdown.GetBuiltin("base64.encode")
if b64encode == nil {
panic("base64.encode missing")
}
var res *ast.Term
if err := b64encode(bctx, []*ast.Term{a}, func(encoded *ast.Term) error {
res = encoded
return nil
}); err != nil {
return nil, err
}
return res, nil
},
)