-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates config and library with the proper functions from utils. Remo…
…ves utils completely.
- Loading branch information
Showing
9 changed files
with
467 additions
and
32 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
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,102 @@ | ||
package models | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
library "github.com/kyallanum/athena/v1.0.0/models/library" | ||
) | ||
|
||
// func resolveLine determines whether the current log line matches a | ||
// matches a given log line | ||
func resolveLine(line string, regex string) *map[string]string { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
panic(fmt.Errorf("the provided regular expression cannot be compiled: \n\t%s", err.(string))) | ||
} | ||
}() | ||
|
||
currentRegexp := regexp.MustCompile(regex) | ||
match := currentRegexp.FindStringSubmatch(line) | ||
result := make(map[string]string) | ||
|
||
if len(match) > 0 { | ||
for index, name := range currentRegexp.SubexpNames() { | ||
if index != 0 && name != "" { | ||
result[name] = match[index] | ||
} | ||
} | ||
return &result | ||
} | ||
return nil | ||
} | ||
|
||
func translateSearchTermReference(regex string, currentSearchTermData *library.SearchTermData) (string, error) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
panic(fmt.Errorf("the search term could not be translated. this is most likely an internal error: \n\t%s", err.(string))) | ||
} | ||
}() | ||
|
||
// Matches the pattern: {{name_to_replace}} | ||
nameExtractRegex := `(\{\{(?P<name_to_replace>[\w]+?)\}\})` | ||
re := regexp.MustCompile(nameExtractRegex) | ||
matches := re.FindAllStringSubmatch(regex, -1) | ||
numMatches := len(matches) | ||
|
||
keysToLookup := []string{} | ||
|
||
// The first three groups are useless for this case. So get the last one. | ||
for _, match := range matches { | ||
keysToLookup = append(keysToLookup, match[2]) | ||
} | ||
|
||
// Validate all strings to replace and then replace them one by one. | ||
for i := 0; i < numMatches; i++ { | ||
stringToReplace, err := currentSearchTermData.Value(strings.TrimSpace(strings.ToLower(keysToLookup[i]))) | ||
if err != nil { | ||
return "", fmt.Errorf("an error occurred when translating a search term reference. \n\tthe following key was not registered in a previous search term: %s", keysToLookup[i]) | ||
} | ||
stringToReplace = escapeSpecialCharacters(stringToReplace) | ||
foundString := re.FindString(regex) | ||
if foundString != "" { | ||
regex = strings.Replace(regex, foundString, stringToReplace, 1) | ||
} | ||
} | ||
|
||
return regex, nil | ||
} | ||
|
||
func escapeSpecialCharacters(regex string) string { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
panic(fmt.Errorf("a string could not be escaped. this is most likely an internal error: \n\t%s", err.(string))) | ||
} | ||
}() | ||
charactersToEscape := `([\*\+\?\\\.\^\[\]\$\&\|]{1})` | ||
re := regexp.MustCompile(charactersToEscape) | ||
|
||
regex = re.ReplaceAllString(regex, `\${1}`) | ||
return regex | ||
} | ||
|
||
func translateConfigurationNamedGroups(regex *string) error { | ||
if *regex == "" { | ||
return fmt.Errorf("empty search terms are not allowed") | ||
} | ||
|
||
defer func() { | ||
if err := recover(); err != nil { | ||
panic(fmt.Errorf("unable to translate regex: \"%s\" for Go standards, this is most likely an internal error: \n\t%s", *regex, err.(string))) | ||
} | ||
}() | ||
|
||
// Matches the pattern (?<group_name>) | ||
regexAddGolangGroupName := `(\(\?)(\<[\w\W]+?\>)` | ||
compiledRegex := regexp.MustCompile(regexAddGolangGroupName) | ||
|
||
*regex = compiledRegex.ReplaceAllString(*regex, "${1}P${2}") | ||
|
||
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,102 @@ | ||
package models | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
library "github.com/kyallanum/athena/v1.0.0/models/library" | ||
) | ||
|
||
func TestResolveLine(t *testing.T) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
t.Errorf("An error was returned improperly when calling resolveLine: \n\t%s", (err.(error)).Error()) | ||
} | ||
}() | ||
line := "test line 1" | ||
regex := "stuff" | ||
|
||
result := resolveLine(line, regex) | ||
if result != nil { | ||
t.Errorf("An incorrect object was returned when calling resolveLine") | ||
} | ||
|
||
regex = `(?P<test_name>line \d+)` | ||
result = resolveLine(line, regex) | ||
|
||
if reflect.TypeOf(result).String() != "*map[string]string" { | ||
t.Errorf("resolveLine did not return the proper data type: \n\t%s", reflect.TypeOf(result).String()) | ||
} | ||
} | ||
|
||
func TestResolveLineBadRegex(t *testing.T) { | ||
defer func() { | ||
if err := recover(); err == nil { | ||
t.Errorf("An error was not returned properly when it should have") | ||
} else if (err.(error)).Error() != "the provided regular expression cannot be compiled: \n\tregexp: Compile(`(stuff`): error parsing regexp: missing closing ): `(stuff`" { | ||
t.Errorf("The improper error was returned when calling with a bad regex: \n\t%s", (err.(error)).Error()) | ||
} | ||
}() | ||
|
||
line := "test line 1" | ||
regex := "(stuff" | ||
|
||
_ = resolveLine(line, regex) | ||
} | ||
|
||
func TestTranslateSearchTermReference(t *testing.T) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
t.Errorf("An error was returned when it shouldn't have: \n\t%s", (err.(error).Error())) | ||
} | ||
}() | ||
|
||
regex := `Testing {{test}}` | ||
st_data := library.SearchTermData.New(library.SearchTermData{}) | ||
st_data.AddValue("test", "Test1") | ||
|
||
newRegex, err := translateSearchTermReference(regex, st_data) | ||
if err != nil { | ||
t.Errorf("An error was returned when it shouldn't have: \n\t%s", err.Error()) | ||
} | ||
|
||
if newRegex != "Testing Test1" { | ||
t.Errorf("TranslateSearchTermReference returned the wrong value: \n\t%s", newRegex) | ||
} | ||
} | ||
|
||
func TestTranslateSearchTermReferenceBadReference(t *testing.T) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
t.Errorf("An error was returned when it shouldn't have: \n\t%s", (err.(error).Error())) | ||
} | ||
}() | ||
|
||
regex := `Testing {{test}}` | ||
st_data := library.SearchTermData.New(library.SearchTermData{}) | ||
st_data.AddValue("bad_test", "testing") | ||
|
||
_, err := translateSearchTermReference(regex, st_data) | ||
if err == nil { | ||
t.Errorf("An error was not returned when it should have.") | ||
} | ||
|
||
if err.Error() != "an error occurred when translating a search term reference. \n\tthe following key was not registered in a previous search term: test" { | ||
t.Errorf("An improper error was returned when attempting to translate search term reference: \n\t%s", err.Error()) | ||
} | ||
} | ||
|
||
func TestValidateString(t *testing.T) { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
t.Errorf("An error occurred when it shouldn't have: \n\t%s", (err.(error).Error())) | ||
} | ||
}() | ||
|
||
stringToValidate := `*+?\\.^[]$&|` | ||
validatedString := escapeSpecialCharacters(stringToValidate) | ||
|
||
if validatedString != "\\*\\+\\?\\\\\\\\\\.\\^\\[\\]\\$\\&\\|" { | ||
t.Errorf("String was not validated properly: %s", validatedString) | ||
} | ||
} |
Oops, something went wrong.