Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to specify other log destinations in AP #1147

Merged
merged 3 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions internal/k8s/app_protect_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,27 @@ func ValidateAppProtectLogConf(logConf *unstructured.Unstructured) error {
return nil
}

var logDstEx = regexp.MustCompile(`syslog:server=((?:\d{1,3}\.){3}\d{1,3}|localhost):\d{1,5}`)
var logDstEx = regexp.MustCompile(`(?:syslog:server=((?:\d{1,3}\.){3}\d{1,3}|localhost):\d{1,5})|stderr|(?:\/[\S]+)+`)
var logDstFileEx = regexp.MustCompile(`(?:\/[\S]+)+`)

// ValidateAppProtectLogDestinationAnnotation validates annotation for log destination configuration
func ValidateAppProtectLogDestinationAnnotation(dstAntn string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you possibly add a unit test for this function? It be great to test for each of the possible types of valid values as well as invalid values (a case per eachreturn fmt.Errorf)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added tests, also removed one return fmt.Errorf since i found it is not reachable.

errormsg := "Error parsing App Protect Log config: Destination Annotation must follow format: syslog:server=<ip-address | localhost>:<port>"
errormsg := "Error parsing App Protect Log config: Destination Annotation must follow format: syslog:server=<ip-address | localhost>:<port> or stderr or absolute path to file"
if !logDstEx.MatchString(dstAntn) {
return fmt.Errorf("%s Log Destination did not follow format", errormsg)
}
if dstAntn == "stderr" {
return nil
}

if logDstFileEx.MatchString(dstAntn) {
return nil
}

dstchunks := strings.Split(dstAntn, ":")

port, err := strconv.Atoi(dstchunks[2])
if err != nil {
return fmt.Errorf("Error parsing port: %v", err)
}
// This error can be ingored since the regex check ensures this string will be parsable
port, _ := strconv.Atoi(dstchunks[2])

if port > 65535 || port < 1 {
return fmt.Errorf("Error parsing port: %v not a valid port number", port)
Expand Down
33 changes: 33 additions & 0 deletions internal/k8s/app_protect_resources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package k8s

import (
"strings"
"testing"
)

func TestValidateAppProtectLogDestinationAnnotation(t *testing.T) {
// Positive test cases
var posDstAntns = []string{"stderr", "syslog:server=localhost:9000", "syslog:server=10.1.1.2:9000", "/var/log/ap.log"}

// Negative test cases item, expected error message
var negDstAntns = [][]string{{"stdout", "Log Destination did not follow format"},
{"syslog:server=localhost:99999", "not a valid port number"},
{"syslog:server=999.99.99.99:5678", "is not a valid ip address"}}

for _, tCase := range posDstAntns {
err := ValidateAppProtectLogDestinationAnnotation(tCase)
if err != nil {
t.Errorf("got %v expected nil", err)
}
}
for _, nTCase := range negDstAntns {
err := ValidateAppProtectLogDestinationAnnotation(nTCase[0])
if err == nil {
t.Errorf("got no error expected error containing %s", nTCase[1])
} else {
if !strings.Contains(err.Error(), nTCase[1]) {
t.Errorf("got %v expected to contain: %s", err, nTCase[1])
}
}
}
}