How to check OPA Rego file is correct or not

huangapple go评论93阅读模式
英文:

How to check OPA Rego file is correct or not

问题

我已经创建了一个名为 sample.rego 的文件,并将其编码为 base64。那么在 Golang 库中是否有一种方法可以验证 rego 的 base64 编码值是否正确?

  1. Sample.rego 文件内容如下:

     package policy.authz
    
     default allow = false
     allow {
       input.policy == "abcd"
     }
    
  2. 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.

  1. Sample.rego file:

    package policy.authz
    
    default allow = false
    allow {
      input.policy == "abcd"
    }
    
  2. 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")
    		
    	}

huangapple
  • 本文由 发表于 2021年12月14日 23:21:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/70351314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定