Find, verify, and analyze leaked credentials
Go credentialsdevsecopsdynamic-analysisprecommitscanningsecretsecret-managementsecretssecuritysecurity-toolstrufflehogverification
Add SliceContainsString common util (#3395)
Bill Rich5280c3877c03
diff --git a/pkg/common/utils.go b/pkg/common/utils.goindex e82de96a8..a8f38ffd7 100644--- a/pkg/common/utils.go+++ b/pkg/common/utils.go@@ -81,3 +81,22 @@ func GetAccountNumFromAWSID(AWSID string) (string, error) {accountNum := (z & mask) >> 7return fmt.Sprintf("%012d", accountNum), nil}++// SliceContainsString searches a slice to determine if it contains a specified string.+// Returns the index of the first match in the slice.+func SliceContainsString(origTargetString string, stringSlice []string, ignoreCase bool) (bool, string, int) {+ targetString := origTargetString+ if ignoreCase {+ targetString = strings.ToLower(origTargetString)+ }+ for i, origStringFromSlice := range stringSlice {+ stringFromSlice := origStringFromSlice+ if ignoreCase {+ stringFromSlice = strings.ToLower(origStringFromSlice)+ }+ if targetString == stringFromSlice {+ return true, targetString, i+ }+ }+ return false, "", 0+}diff --git a/pkg/common/utils_test.go b/pkg/common/utils_test.goindex 02087854b..99bac1cb0 100644--- a/pkg/common/utils_test.go+++ b/pkg/common/utils_test.go@@ -133,3 +133,64 @@ func TestParseResponseForKeywords(t *testing.T) {})}}++func TestSliceContainsString(t *testing.T) {+ testCases := []struct {+ name string+ slice []string+ target string+ expectedBool bool+ expectedString string+ expectedIndex int+ ignoreCase bool+ }{+ {+ name: "matching case, target exists",+ slice: []string{"one", "two", "three"},+ target: "two",+ expectedBool: true,+ expectedString: "two",+ expectedIndex: 1,+ ignoreCase: false,+ },+ {+ name: "non-matching case, target exists, ignore case",+ slice: []string{"one", "two", "three"},+ target: "Two",+ expectedBool: true,+ expectedString: "two",+ expectedIndex: 1,+ ignoreCase: true,+ },+ {+ name: "non-matching case, target in wrong case, case respected",+ slice: []string{"one", "two", "three"},+ target: "Two",+ expectedBool: false,+ expectedString: "",+ expectedIndex: 0,+ ignoreCase: false,+ },+ {+ name: "target not in slice",+ slice: []string{"one", "two", "three"},+ target: "four",+ expectedBool: false,+ expectedString: "",+ expectedIndex: 0,+ ignoreCase: false,+ },+ }+ for _, testCase := range testCases {+ resultBool, resultString, resultIndex := SliceContainsString(testCase.target, testCase.slice, testCase.ignoreCase)+ if resultBool != testCase.expectedBool {+ t.Errorf("%s: bool values do not match. Got: %t, expected: %t", testCase.name, resultBool, testCase.expectedBool)+ }+ if resultString != testCase.expectedString {+ t.Errorf("%s: string values do not match. Got: %s, expected: %s", testCase.name, resultString, testCase.expectedString)+ }+ if resultIndex != testCase.expectedIndex {+ t.Errorf("%s: index values do not match. Got: %d, expected: %d", testCase.name, resultIndex, testCase.expectedIndex)+ }+ }+}