-
Notifications
You must be signed in to change notification settings - Fork 545
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add custom map value function (#847)
- support passing a function into the mapvalue of Contains function
- Loading branch information
1 parent
ac78d78
commit 715c2d4
Showing
5 changed files
with
80 additions
and
3 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,13 +1,22 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
|
||
{ | ||
"name": "Basic tfsec", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${workspaceFolder}/cmd/tfsec/main.go", | ||
"args": ["${workspaceFolder}/example"] | ||
}, | ||
{ | ||
"name": "custom_functions test", | ||
"type": "go", | ||
"request": "launch", | ||
"mode": "auto", | ||
"program": "${workspaceFolder}/cmd/tfsec/main.go", | ||
"args": ["${workspaceFolder}/example/custom_functions"] | ||
} | ||
] | ||
} |
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,22 @@ | ||
--- | ||
checks: | ||
- code: TAG_custom | ||
description: Custom check to ensure the Environment tag is applied | ||
errorMessage: The required Environment tag was missing or invalid | ||
matchSpec: | ||
action: contains | ||
name: tags | ||
value: | ||
Environment: | ||
action: isAny | ||
value: | ||
- production | ||
- test | ||
- dev | ||
- staging | ||
|
||
requiredTypes: | ||
- resource | ||
severity: ERROR | ||
requiredLabels: | ||
- aws_lb |
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,6 @@ | ||
resource "aws_lb" "test_lb" { | ||
|
||
tags = { | ||
Environment = "uat" | ||
} | ||
} |
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,42 @@ | ||
package block | ||
|
||
const ( | ||
functionNameKey = "action" | ||
valueNameKey = "value" | ||
) | ||
|
||
var functions = map[string]func(interface{}, interface{}) bool{ | ||
"isAny": isAny, | ||
"isNone": isNone, | ||
} | ||
|
||
func evaluate(criteriaValue interface{}, testValue interface{}) bool { | ||
switch t := criteriaValue.(type) { | ||
case map[interface{}]interface{}: | ||
if t[functionNameKey] != nil { | ||
functionName := t[functionNameKey].(string) | ||
if functions[functionName] != nil { | ||
return functions[functionName](t[valueNameKey], testValue) | ||
} | ||
} | ||
default: | ||
return t == testValue | ||
} | ||
return false | ||
} | ||
|
||
func isAny(criteriaValues interface{}, testValue interface{}) bool { | ||
switch t := criteriaValues.(type) { | ||
case []interface{}: | ||
for _, v := range t { | ||
if v == testValue { | ||
return true | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func isNone(criteriaValues interface{}, testValue interface{}) bool { | ||
return !isAny(criteriaValues, testValue) | ||
} |