英文:
How to check OPA Rego file is correct or not
问题
我已经创建了一个名为 sample.rego
的文件,并将其编码为 base64。那么在 Golang
库中是否有一种方法可以验证 rego 的 base64 编码值是否正确?
-
Sample.rego 文件内容如下:
package policy.authz default allow = false allow { input.policy == "abcd" }
-
Base64 编码值为:
cGFja2FnZSBwb2xpY3kuYXV0aHoKCiMgbG9naWMgdGhhdCBpbXBsZW1lbnRzIHBvbGljeS4KZGVmYXVsdCBhbGxvdyA9IGZhbHNlCmFsbG93IHsKICBpbnB1dC5wb2xpY3kgPT0gImFiY2QiCn0=
现在我需要借助 GoLang 库来验证编码后的文件是否有效。
现在,如果我将 Sample.rego 文件更改为:
package policy.authz
default allow = false
allow {
wrong dummy data
input.policy == "abcd"
}
- Base64 编码值为:
> cGFja2FnZSBwb2xpY3kuYXV0aHoKZGVmYXVsdCBhbGxvdyA9IGZhbHNlCmFsbG93IHsKICB3cm9uZyBkdW1teSBkYXRhCiAgaW5wdXQucG9saWN5ID09ICJhYmNkIgp9
现在我需要借助 GoLang 库来验证编码后的文件是否无效。
英文:
I have created one sample.rego
file and I encoded in base64.
so is there any way in Golang
Library that validate rego base64 encoded value is correct or not.
-
Sample.rego file:
package policy.authz default allow = false allow { input.policy == "abcd" }
-
Base64 encoded value is :
cGFja2FnZSBwb2xpY3kuYXV0aHoKCiMgbG9naWMgdGhhdCBpbXBsZW1lbnRzIHBvbGljeS4KZGVmYXVsdCBhbGxvdyA9IGZhbHNlCmFsbG93IHsKICBpbnB1dC5wb2xpY3kgPT0gImFiY2QiCn0=
Now I have to validate with the help of GoLang Library that file we encoded is valid.
Now If I changed Sample.rego file to :
package policy.authz
default allow = false
allow {
wrong dummy data
input.policy == "abcd"
}
- Base64 encoded value is :
> cGFja2FnZSBwb2xpY3kuYXV0aHoKZGVmYXVsdCBhbGxvdyA9IGZhbHNlCmFsbG93IHsKICB3cm9uZyBkdW1teSBkYXRhCiAgaW5wdXQucG9saWN5ID09ICJhYmNkIgp9
Now I have to validate with the help of GoLang Library that file we encoded is not valid.
答案1
得分: 1
在Golang库中,有一种方法可以验证rego base64编码值是否正确。你需要对其进行解码,如果解码失败,则表示它不是base64编码或者编码不正确。
以下是一个示例代码:
package main
import (
"encoding/base64"
"fmt"
)
func IsBase64(s string) bool {
_, err := base64.StdEncoding.DecodeString(s)
return err == nil
}
func main() {
fmt.Println(IsBase64(`Invalid string`))
fmt.Println(IsBase64(`VmFsaWQgc3RyaW5nCg==`))
}
这段代码中的IsBase64
函数可以用来验证给定的字符串s
是否是正确的base64编码。在main
函数中,我们对两个字符串进行了验证,第一个字符串是无效的base64编码,第二个字符串是有效的base64编码。运行代码后,会输出两个验证结果。
英文:
> is there any way in Golang Library that validate rego base64 encoded value is correct or not.
You have to decode it, if it fails it's not base64 or not encoded correctly.
Here you have an example.
package main
import (
"encoding/base64"
"fmt"
)
func IsBase64(s string) bool {
_, err := base64.StdEncoding.DecodeString(s)
return err == nil
}
func main() {
fmt.Println(IsBase64(`Invalid string`))
fmt.Println(IsBase64(`VmFsaWQgc3RyaW5nCg==`))
}
答案2
得分: 0
import "github.com/open-policy-agent/opa/ast"
modules := map[string]*ast.Module{}
compiler := ast.NewCompiler()
modules["policyModule"], err = ast.ParseModule("policyModule", string(policy))
if err != nil {
log.Error(err, "unable to parse the module")
}
compiler.Compile(modules)
if compiler.Failed() {
log.Error(compiler.Errors, "unable to compile the module")
}
这是一个Go语言的代码片段,它使用了github.com/open-policy-agent/opa/ast
包。代码的功能是解析和编译一个名为"policyModule"的策略模块。如果解析或编译失败,将会输出相应的错误信息。
英文:
import "github.com/open-policy-agent/opa/ast"
modules := map[string]*ast.Module{}
compiler := ast.NewCompiler()
modules["policyModule"], err = ast.ParseModule("policyModule", string(policy))
if err!=nil{
log.Error(err, "unable to parse the module")
}
compiler.Compile(modules)
if compiler.Failed() {
log.Error(compiler.Errors, "unable to compile the module")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论