$ ./opa -h An open source project to policy-enable your service.
Usage: E:\goproject\gopath\bin\opa.exe [command]
Available Commands: bench Benchmark a Rego query build Build an OPA bundle check Check Rego source files completion Generate the autocompletion script for the specified shell deps Analyze Rego query dependencies eval Evaluate a Rego query exec Execute against input files fmt Format Rego source files help Help about any command inspect Inspect OPA bundle(s) parse Parse Rego source file run Start OPA in interactive or server mode sign Generate an OPA bundle signature test Execute Rego test cases version Print the version of OPA
Flags: -h, --help help for E:\goproject\gopath\bin\opa.exe
Use "E:\goproject\gopath\bin\opa.exe [command] --help" for more information about a command.
# allow will be true when user has role and role has permission allow { # opa eval -f pretty -d quick-start -i quick-start/input.json "data.example_rbac.allow" --explain=notes # trace(role_name) some role_name user_has_role[role_name] role_has_permission[role_name] }
# check user role binding exist user_has_role[role_name] { role_binding = data.bindings[_] role_binding.role = role_name role_binding.user == input.subject.user }
# check role permission exist role_has_permission[role_name] { role = data.roles[_] role.name = role_name role.operation == input.action.operation role.resource == input.action.resource }
$ opa run quick-start OPA 0.37.2 (commit , built at ) Run 'help' to see a list of commands and check for updates. #获取上下文 > data { "action": { "operation": "read", "resource": "widgets" }, ... }
你会得到 json 格式的 data 下的所有节点内容
主要有两类,所有quick-start目录下的
配置文件(.json|.yaml|.yml)
对应的父节点是data
Rego文件(包括 test 文件)
对应的父节点是data.<package name>
Tips: 这样配置文件都挂在data这个根节点下了。
如果想加载配置文件时增加父节点(如data.example.file)该怎么办?
可以文件的路径映射前缀 opa run example.file:quick-start注意只会改变配置文件的父节点
这一点很有用,以后讲bundle也会提到
当然也可以指定输入文件input。这个比较特殊,命令行保留了包前缀repl.input给input
也就是说,可以用repl.input:<path to input.json>的方式传递输入,而避免挂载到data根节点下