Skip to content

Instantly share code, notes, and snippets.

@svishwanath-tw
Created October 17, 2019 14:20
Show Gist options
  • Save svishwanath-tw/44f53c6dac8f6f23ae9a951e0e57488e to your computer and use it in GitHub Desktop.
Save svishwanath-tw/44f53c6dac8f6f23ae9a951e0e57488e to your computer and use it in GitHub Desktop.
diff --git a/detector/filecontent_detector.go b/detector/filecontent_detector.go
index 15e4d17..f1397b4 100644
--- a/detector/filecontent_detector.go
+++ b/detector/filecontent_detector.go
@@ -31,49 +31,10 @@ func (fc *FileContentDetector) AggressiveMode() *FileContentDetector {
return fc
}
-type contentType int
-
-const (
- base64Content contentType = iota
- hexContent
- creditCardContent
-)
-
-func (ct contentType) getInfo() string {
- switch ct {
- case base64Content:
- return "Failing file as it contains a base64 encoded text."
- case hexContent:
- return "Failing file as it contains a hex encoded text."
- case creditCardContent:
- return "Failing file as it contains a potential credit card number."
- }
- return ""
-}
-
-func (ct contentType) getMessageFormat() string {
- switch ct {
- case base64Content:
- return "Expected file to not to contain base64 encoded texts such as: %s"
- case hexContent:
- return "Expected file to not to contain hex encoded texts such as: %s"
- case creditCardContent:
- return "Expected file to not to contain credit card numbers such as: %s"
- }
-
- return ""
-}
-
-func (ct contentType) getCheckFn() fn {
- switch ct {
- case base64Content:
- return checkBase64
- case hexContent:
- return checkHex
- case creditCardContent:
- return checkCreditCardNumber
- }
- return nil
+type contentType struct {
+ infoMessage string
+ expectationMessageFormat string
+ checkFuntion fn
}
type content struct {
@@ -83,11 +44,24 @@ type content struct {
results []string
}
+// Test runs a set of content detectors on a slice of additions using a TalismanRCIgnore configuration
func (fc *FileContentDetector) Test(additions []gitrepo.Addition, ignoreConfig TalismanRCIgnore, result *DetectionResults) {
contentTypes := []contentType{
- base64Content,
- hexContent,
- creditCardContent,
+ contentType{
+ "Failing file as it contains a base64 encoded text.",
+ "Expected file to not to contain base64 encoded texts such as: %s",
+ checkBase64,
+ },
+ contentType{
+ "Failing file as it contains a hex encoded text.",
+ "Expected file to not to contain hex encoded texts such as: %s",
+ checkHex,
+ },
+ contentType{
+ "Failing file as it contains a potential credit card number.",
+ "Expected file to not to contain credit card numbers such as: %s",
+ checkCreditCardNumber,
+ },
}
cc := NewChecksumCompare(additions, ignoreConfig)
re := regexp.MustCompile(`(?i)checksum[ \t]*:[ \t]*[0-9a-fA-F]+`)
@@ -115,7 +89,7 @@ func (fc *FileContentDetector) Test(additions []gitrepo.Addition, ignoreConfig T
name: addition.Name,
path: addition.Path,
contentType: ct,
- results: fc.detectFile(addition.Data, ct.getCheckFn()),
+ results: fc.detectFile(addition.Data, ct.checkFuntion),
}
}
}(addition)
@@ -150,11 +124,11 @@ func processContent(c content, result *DetectionResults) {
if res != "" {
log.WithFields(log.Fields{
"filePath": c.path,
- }).Info(c.contentType.getInfo())
+ }).Info(c.contentType.infoMessage)
if string(c.name) == DefaultRCFileName {
- result.Warn(c.path, "filecontent", fmt.Sprintf(c.contentType.getMessageFormat(), res), []string{})
+ result.Warn(c.path, "filecontent", fmt.Sprintf(c.contentType.expectationMessageFormat, res), []string{})
} else {
- result.Fail(c.path, "filecontent", fmt.Sprintf(c.contentType.getMessageFormat(), res), []string{})
+ result.Fail(c.path, "filecontent", fmt.Sprintf(c.contentType.expectationMessageFormat, res), []string{})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment