【问题标题】:How to use a built-in in another built-in for Open policy agent如何在另一个内置的 Open 策略代理中使用内置
【发布时间】:2021-01-30 01:19:36
【问题描述】:

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

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

【问题讨论】:

    标签: go open-policy-agent rego


    【解决方案1】:

    您应该能够使用从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
            },
        )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-03
      • 2021-08-04
      • 1970-01-01
      • 1970-01-01
      • 2014-02-24
      • 1970-01-01
      • 2019-05-25
      • 1970-01-01
      相关资源
      最近更新 更多