Skip to content

Commit

Permalink
add check flag, using similar logic from opa -check
Browse files Browse the repository at this point in the history
  • Loading branch information
animale66 authored and tsandall committed Mar 12, 2018
1 parent 04efcc6 commit 3623937
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (

"github.com/docker/go-plugins-helpers/authorization"
"github.com/open-policy-agent/opa/rego"
"github.com/open-policy-agent/opa/ast"
"github.com/open-policy-agent/opa/loader"
)

// DockerAuthZPlugin implements the authorization.Plugin interface. Every
Expand Down Expand Up @@ -123,6 +125,34 @@ func makeInput(r authorization.Request) (interface{}, error) {
return input, nil
}

func regoSyntax(p string) int {

stuffs := []string{p}

result, err := loader.AllRegos(stuffs)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return 1
}

modules := map[string]*ast.Module{}

for _,m := range result.Modules {
modules[m.Name] = m.Parsed
}

compiler := ast.NewCompiler().SetErrorLimit(0)

if compiler.Compile(modules); compiler.Failed() {
for _, err := range compiler.Errors {
fmt.Fprintln(os.Stderr, err)
}
return 1
}

return 0
}

// Version is set by the build.
var Version = ""

Expand All @@ -132,6 +162,7 @@ func main() {
allowPath := flag.String("allowPath", "data.docker.authz.allow", "sets the path of the allow decision in OPA")
policyFile := flag.String("policy-file", "policy.rego", "sets the path of the policy file to load")
version := flag.Bool("version", false, "print the version of the plugin")
check := flag.Bool("check", false , "checks the syntax of the policy-file")

flag.Parse()

Expand All @@ -145,6 +176,10 @@ func main() {
allowPath: *allowPath,
}

if *check {
os.Exit(regoSyntax(*policyFile))
}

h := authorization.NewHandler(p)
log.Println("Starting server.")
h.ServeUnix(*pluginName, 0)
Expand Down

0 comments on commit 3623937

Please sign in to comment.