From 2106192633043d59dac09b9110492aed1c4ae833 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 17 Feb 2022 15:39:09 +0100 Subject: [PATCH] Add endpoint list validation regex and function --- profile/endpoints/endpoints.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/profile/endpoints/endpoints.go b/profile/endpoints/endpoints.go index 1f158017..7b16fab5 100644 --- a/profile/endpoints/endpoints.go +++ b/profile/endpoints/endpoints.go @@ -2,6 +2,7 @@ package endpoints import ( "context" + "errors" "fmt" "strings" @@ -58,6 +59,27 @@ entriesLoop: return endpoints, nil } +// ListEntryValidationRegex is a regex to bullshit check endpoint list entries. +var ListEntryValidationRegex = strings.Join([]string{ + `^(\+|\-) `, // Rule verdict. + `[A-z0-9\.:\-*/]+`, // Entity matching. + `( `, // Start of optional matching. + `[A-z0-9*]+`, // Protocol matching. + `(/[A-z0-9]+(\-[A-z0-9]+)?)?`, // Port and port range matching. + `)?$`, // End of optional matching. +}, "") + +// ValidateEndpointListConfigOption validates the given value. +func ValidateEndpointListConfigOption(value interface{}) error { + list, ok := value.([]string) + if !ok { + return errors.New("invalid type") + } + + _, err := ParseEndpoints(list) + return err +} + // IsSet returns whether the Endpoints object is "set". func (e Endpoints) IsSet() bool { return len(e) > 0