-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
187 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.idea | ||
bundle.tar.gz | ||
opa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package lib.sh | ||
|
||
test_parse_commands_with_ampersands { | ||
cmds := sh.parse_commands("apt update && apt install curl") | ||
count(cmds) == 2 | ||
cmds[0] == ["apt", "update"] | ||
cmds[1] == ["apt", "install", "curl"] | ||
} | ||
|
||
test_parse_commands_empty_input { | ||
cmds := sh.parse_commands("") | ||
count(cmds) == 0 | ||
} | ||
|
||
test_parse_commands_with_semicolon { | ||
cmds := sh.parse_commands("apt update;apt install curl") | ||
count(cmds) == 2 | ||
cmds[0] == ["apt", "update"] | ||
cmds[1] == ["apt", "install", "curl"] | ||
} | ||
|
||
test_parse_commands_mixed { | ||
cmds := sh.parse_commands("apt update; apt install curl && apt install git") | ||
count(cmds) == 3 | ||
cmds[0] == ["apt", "update"] | ||
cmds[1] == ["apt", "install", "curl"] | ||
cmds[2] == ["apt", "install", "git"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package rego | ||
|
||
import ( | ||
"sync" | ||
|
||
opa "github.com/open-policy-agent/opa/rego" | ||
) | ||
|
||
var registerOnce sync.Once | ||
|
||
func RegisterBuiltins() { | ||
registerOnce.Do(func() { | ||
opa.RegisterBuiltin1(shParseCommandsDecl, shParseCommandsImpl) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package rego | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/open-policy-agent/opa/ast" | ||
"github.com/open-policy-agent/opa/rego" | ||
"github.com/open-policy-agent/opa/topdown/builtins" | ||
"github.com/open-policy-agent/opa/types" | ||
"mvdan.cc/sh/v3/syntax" | ||
) | ||
|
||
var shParseCommandsDecl = ®o.Function{ | ||
Name: "sh.parse_commands", | ||
Decl: types.NewFunction(types.Args(types.S), types.NewArray(nil, types.NewArray(nil, types.S))), | ||
Description: "Parse command sequence", | ||
Memoize: true, | ||
} | ||
|
||
var shParseCommandsImpl = func(c rego.BuiltinContext, a *ast.Term) (*ast.Term, error) { | ||
astr, err := builtins.StringOperand(a.Value, 0) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid parameter type: %w", err) | ||
} | ||
|
||
commands, err := parseCommands(string(astr)) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("parse command sequence error: %w", err) | ||
} | ||
|
||
var commandsTerm []*ast.Term | ||
for _, cmd := range commands { | ||
var cmdTerm []*ast.Term | ||
for _, cmd_part := range cmd { | ||
cmdTerm = append(cmdTerm, ast.StringTerm(cmd_part)) | ||
} | ||
commandsTerm = append(commandsTerm, ast.ArrayTerm(cmdTerm...)) | ||
} | ||
|
||
return ast.ArrayTerm(commandsTerm...), nil | ||
} | ||
|
||
func parseCommands(cmdsSeq string) ([][]string, error) { | ||
f, err := syntax.NewParser().Parse(strings.NewReader(cmdsSeq), "") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
printer := syntax.NewPrinter() | ||
|
||
var commands [][]string | ||
syntax.Walk(f, func(node syntax.Node) bool { | ||
switch x := node.(type) { | ||
case *syntax.CallExpr: | ||
args := x.Args | ||
var cmd []string | ||
for _, word := range args { | ||
var buffer bytes.Buffer | ||
printer.Print(&buffer, word) | ||
cmd = append(cmd, buffer.String()) | ||
} | ||
if cmd != nil { | ||
commands = append(commands, cmd) | ||
} | ||
} | ||
return true | ||
}) | ||
|
||
return commands, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package rego | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParseCommands(t *testing.T) { | ||
tests := []struct { | ||
cmdsSeq string | ||
expected [][]string | ||
}{ | ||
{ | ||
cmdsSeq: "apt update; apt install -y nginx", | ||
expected: [][]string{{"apt", "update"}, {"apt", "install", "-y", "nginx"}}, | ||
}, | ||
{ | ||
cmdsSeq: "apt update && apt install -y nginx", | ||
expected: [][]string{{"apt", "update"}, {"apt", "install", "-y", "nginx"}}, | ||
}, | ||
{ | ||
cmdsSeq: "apt update || apt install -y nginx", | ||
expected: [][]string{{"apt", "update"}, {"apt", "install", "-y", "nginx"}}, | ||
}, | ||
{ | ||
cmdsSeq: `echo "test;test" ;apt update && apt install -y nginx`, | ||
expected: [][]string{{"echo", "\"test;test\""}, {"apt", "update"}, {"apt", "install", "-y", "nginx"}}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.cmdsSeq, func(t *testing.T) { | ||
got, err := parseCommands(test.cmdsSeq) | ||
require.NoError(t, err) | ||
assert.Equal(t, test.expected, got) | ||
}) | ||
} | ||
} |