Skip to content

Commit

Permalink
Merge branch 'main' into gitlab-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
gitu authored Jul 21, 2023
2 parents 30a009b + 768dcd9 commit fd0ba9d
Show file tree
Hide file tree
Showing 24 changed files with 1,138 additions and 432 deletions.
14 changes: 14 additions & 0 deletions ast/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ var DefaultBuiltins = [...]*Builtin{
CryptoX509ParseCertificateRequest,
CryptoX509ParseRSAPrivateKey,
CryptoX509ParseKeyPair,
CryptoParsePrivateKeys,
CryptoHmacMd5,
CryptoHmacSha1,
CryptoHmacSha256,
Expand Down Expand Up @@ -2312,6 +2313,19 @@ var CryptoX509ParseRSAPrivateKey = &Builtin{
),
}

var CryptoParsePrivateKeys = &Builtin{
Name: "crypto.parse_private_keys",
Description: `Returns zero or more private keys from the given encoded string containing DER certificate data.
If the input is empty, the function will return null. The input string should be a list of one or more concatenated PEM blocks. The whole input of concatenated PEM blocks can optionally be Base64 encoded.`,
Decl: types.NewFunction(
types.Args(
types.Named("keys", types.S).Description("PEM encoded data containing one or more private keys as concatenated blocks. Optionally Base64 encoded."),
),
types.Named("output", types.NewArray(nil, types.NewObject(nil, types.NewDynamicProperty(types.S, types.A)))).Description("parsed private keys represented as objects"),
),
}

var CryptoMd5 = &Builtin{
Name: "crypto.md5",
Description: "Returns a string representing the input string hashed with the MD5 function",
Expand Down
35 changes: 35 additions & 0 deletions ast/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1844,6 +1844,41 @@ bar.baz contains "quz" if true`,
assertCompilerErrorStrings(t, c, expected)
}

func TestCompilerCheckRuleConflictsDefaultFunction(t *testing.T) {
tests := []struct {
note string
modules []*Module
err string
}{
{
note: "conflicting rules",
modules: modules(
`package pkg
default f(_) = 100
f(x, y) = x {
x == y
}`),
err: "rego_type_error: conflicting rules data.pkg.f found",
},
}
for _, tc := range tests {
t.Run(tc.note, func(t *testing.T) {
mods := make(map[string]*Module, len(tc.modules))
for i, m := range tc.modules {
mods[fmt.Sprint(i)] = m
}
c := NewCompiler()
c.Modules = mods
compileStages(c, c.checkRuleConflicts)
if tc.err != "" {
assertCompilerErrorStrings(t, c, []string{tc.err})
} else {
assertCompilerErrorStrings(t, c, []string{})
}
})
}
}

func TestCompilerCheckRuleConflictsDotsInRuleHeads(t *testing.T) {

tests := []struct {
Expand Down
38 changes: 38 additions & 0 deletions ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,12 @@ func (p *Parser) parseRules() []*Rule {
return nil
}

if len(rule.Head.Args) > 0 {
if !p.validateDefaultRuleArgs(&rule) {
return nil
}
}

rule.Body = NewBody(NewExpr(BooleanTerm(true).SetLocation(rule.Location)).SetLocation(rule.Location))
return []*Rule{&rule}
}
Expand Down Expand Up @@ -2176,6 +2182,38 @@ func (p *Parser) validateDefaultRuleValue(rule *Rule) bool {
return valid
}

func (p *Parser) validateDefaultRuleArgs(rule *Rule) bool {

valid := true
vars := NewVarSet()

vis := NewGenericVisitor(func(x interface{}) bool {
switch x := x.(type) {
case Var:
if vars.Contains(x) {
p.error(rule.Loc(), fmt.Sprintf("illegal default rule (arguments cannot be repeated %v)", x))
valid = false
return true
}
vars.Add(x)

case *Term:
switch v := x.Value.(type) {
case Var: // do nothing
default:
p.error(rule.Loc(), fmt.Sprintf("illegal default rule (arguments cannot contain %v)", TypeName(v)))
valid = false
return true
}
}

return false
})

vis.Walk(rule.Head.Args)
return valid
}

// We explicitly use yaml unmarshalling, to accommodate for the '_' in 'related_resources',
// which isn't handled properly by json for some reason.
type rawAnnotation struct {
Expand Down
8 changes: 8 additions & 0 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1611,6 +1611,14 @@ func TestRule(t *testing.T) {
assertParseErrorContains(t, "default invalid rule head builtin call", `default a = upper("foo")`, "illegal default rule (value cannot contain call)")
assertParseErrorContains(t, "default invalid rule head call", `default a = b`, "illegal default rule (value cannot contain var)")

assertParseErrorContains(t, "default invalid function head ref", `default f(x) = b.c.d`, "illegal default rule (value cannot contain ref)")
assertParseErrorContains(t, "default invalid function head call", `default f(x) = g(x)`, "illegal default rule (value cannot contain call)")
assertParseErrorContains(t, "default invalid function head builtin call", `default f(x) = upper("foo")`, "illegal default rule (value cannot contain call)")
assertParseErrorContains(t, "default invalid function head call", `default f(x) = b`, "illegal default rule (value cannot contain var)")
assertParseErrorContains(t, "default invalid function composite argument", `default f([x]) = 1`, "illegal default rule (arguments cannot contain array)")
assertParseErrorContains(t, "default invalid function number argument", `default f(1) = 1`, "illegal default rule (arguments cannot contain number)")
assertParseErrorContains(t, "default invalid function repeated vars", `default f(x, x) = 1`, "illegal default rule (arguments cannot be repeated x)")

assertParseError(t, "extra braces", `{ a := 1 }`)
assertParseError(t, "invalid rule name hyphen", `a-b = x { x := 1 }`)

Expand Down
21 changes: 21 additions & 0 deletions builtin_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"crypto.hmac.sha256",
"crypto.hmac.sha512",
"crypto.md5",
"crypto.parse_private_keys",
"crypto.sha1",
"crypto.sha256",
"crypto.x509.parse_and_verify_certificates",
Expand Down Expand Up @@ -3712,6 +3713,26 @@
},
"wasm": false
},
"crypto.parse_private_keys": {
"args": [
{
"description": "PEM encoded data containing one or more private keys as concatenated blocks. Optionally Base64 encoded.",
"name": "keys",
"type": "string"
}
],
"available": [
"edge"
],
"description": "Returns zero or more private keys from the given encoded string containing DER certificate data.\n\nIf the input is empty, the function will return null. The input string should be a list of one or more concatenated PEM blocks. The whole input of concatenated PEM blocks can optionally be Base64 encoded.",
"introduced": "edge",
"result": {
"description": "parsed private keys represented as objects",
"name": "output",
"type": "array[object[string: any]]"
},
"wasm": false
},
"crypto.sha1": {
"args": [
{
Expand Down
25 changes: 25 additions & 0 deletions capabilities.json
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,31 @@
"type": "function"
}
},
{
"name": "crypto.parse_private_keys",
"decl": {
"args": [
{
"type": "string"
}
],
"result": {
"dynamic": {
"dynamic": {
"key": {
"type": "string"
},
"value": {
"type": "any"
}
},
"type": "object"
},
"type": "array"
},
"type": "function"
}
},
{
"name": "crypto.sha1",
"decl": {
Expand Down
Loading

0 comments on commit fd0ba9d

Please sign in to comment.