0

有没有办法在我想要创建的新内置中调用内置(如 io.jwt.decode_verify(string, constraints))?

或者有没有办法调用 OPA 的内部包的方法?

4

1 回答 1

0

您应该能够使用从函数中导出的内置topdown.GetBuiltin函数。在下面的示例中,该base64.encode函数是从my_custom自定义内置函数调用的:

    rego.RegisterBuiltin2(
        &rego.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
        },
    )
于 2021-01-30T15:36:32.697 回答