Home > OS >  How to check OPA Rego file is correct or not
How to check OPA Rego file is correct or not

Time:12-15

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.

CodePudding user response:

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==`))
}

CodePudding user response:

  1. Sample.rego file:
package policy.authz
default allow = false
allow {
  wrong dummy data
  input.policy == "abcd"
}
  1. Base64 encoded value is :

cGFja2FnZSBwb2xpY3kuYXV0aHoKZGVmYXVsdCBhbGxvdyA9IGZhbHNlCmFsbG93IHsKICB3cm9uZyBkdW1teSBkYXRhCiAgaW5wdXQucG9saWN5ID09ICJhYmNkIgp9

Now I have to validate with the help of GoLang Library that file (.rego) we encoded is not valid.

  • Related