forked from open-policy-agent/conftest
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request open-policy-agent#583 from drybrough/feat/support-…
…properties Add support for properties files.
- Loading branch information
Showing
7 changed files
with
116 additions
and
14 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
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,19 @@ | ||
package main | ||
|
||
deny_valid_uri[msg] { | ||
value := input[name] | ||
contains(lower(name), "url") | ||
not contains(lower(value), "http") | ||
msg := sprintf("Must have a valid uri defined '%s'", [value]) | ||
} | ||
|
||
secret_exceptions = { | ||
"secret.value.exception" | ||
} | ||
|
||
deny_no_secrets[msg] { | ||
value := input[name] | ||
not secret_exceptions[name] | ||
contains(lower(name), "secret") | ||
msg := sprintf("'%s' may contain a secret value", [name]) | ||
} |
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,3 @@ | ||
SAMPLE_VALUE=something-here | ||
other.value.url=https://example.com/ | ||
secret.value.exception=f9761ebe-d4dc-11eb-8046-1e00e20cdb95 |
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,31 @@ | ||
package properties | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
prop "github.com/magiconair/properties" | ||
) | ||
|
||
// Parser is a properties parser. | ||
type Parser struct{} | ||
|
||
func (pp *Parser) Unmarshal(p []byte, v interface{}) error { | ||
rawProps, err := prop.LoadString(string(p)) | ||
if err != nil { | ||
return fmt.Errorf("Could not parse properties file: %w", err) | ||
} | ||
|
||
result := rawProps.Map() | ||
|
||
j, err := json.Marshal(result) | ||
if err != nil { | ||
return fmt.Errorf("marshal properties to json: %w", err) | ||
} | ||
|
||
if err := json.Unmarshal(j, v); err != nil { | ||
return fmt.Errorf("unmarshal properties json: %w", err) | ||
} | ||
|
||
return 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,38 @@ | ||
package properties | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestPropertiesParser(t *testing.T) { | ||
parser := &Parser{} | ||
sample := `# This is a simle properties file | ||
SAMPLE_KEY=https://example.com/ | ||
! some comment=not-a-prop | ||
my-property=some-value` | ||
|
||
var input interface{} | ||
if err := parser.Unmarshal([]byte(sample), &input); err != nil { | ||
t.Errorf("parser should not have thrown an error: %v", err) | ||
} | ||
|
||
if input == nil { | ||
t.Errorf("there should be information parsed but its nil") | ||
} | ||
|
||
inputMap := input.(map[string]interface{}) | ||
myProp := inputMap["my-property"].(string) | ||
if myProp != "some-value" { | ||
t.Errorf("Failed to parse property: %s", myProp) | ||
} | ||
|
||
spaceProp := inputMap["SAMPLE_KEY"].(string) | ||
if spaceProp != "https://example.com/" { | ||
t.Errorf("Failed to strip whitespace from key: %s", myProp) | ||
} | ||
|
||
inputLen := len(inputMap) | ||
if inputLen != 2 { | ||
t.Errorf("Failed to parse all properties: expected 2 got %d", inputLen) | ||
} | ||
} |