-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from hawkv6/config-validation
Config validation
- Loading branch information
Showing
12 changed files
with
223 additions
and
23 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,8 @@ | ||
package config | ||
|
||
type validationError struct { | ||
Namespace string `json:"namespace"` | ||
Field string `json:"field"` | ||
ActualValue string `json:"actual_value"` | ||
Message string `json:"message"` | ||
} |
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,47 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"reflect" | ||
|
||
"github.com/go-playground/validator" | ||
) | ||
|
||
var validate *validator.Validate | ||
|
||
func Validate() { | ||
validate = validator.New() | ||
|
||
validate.RegisterTagNameFunc(func(fld reflect.StructField) string { | ||
return fld.Tag.Get("mapstructure") | ||
}) | ||
|
||
validate.RegisterStructValidation(ServiceConfigValidation, ServiceConfig{}) | ||
validate.RegisterStructValidation(ApplicationValidation, Application{}) | ||
validate.RegisterStructValidation(IntentValidation, Intent{}) | ||
|
||
err := validate.Struct(Params) | ||
if err != nil { | ||
if _, ok := err.(*validator.InvalidValidationError); ok { | ||
log.Info(err) | ||
} | ||
|
||
for _, err := range err.(validator.ValidationErrors) { | ||
e := validationError{ | ||
Namespace: err.Namespace(), | ||
Field: err.Field(), | ||
ActualValue: fmt.Sprintf("%v", err.Value()), | ||
Message: err.Tag(), | ||
} | ||
|
||
indent, err := json.MarshalIndent(e, "", " ") | ||
if err != nil { | ||
log.Fatalf("failed to validate config: %v", err) | ||
} | ||
fmt.Println(string(indent)) | ||
log.Fatalf("failed to validate config with errors above") | ||
} | ||
} | ||
|
||
} |
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,54 @@ | ||
package config | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/go-playground/validator" | ||
"github.com/hawkv6/hawkwing/pkg/types" | ||
) | ||
|
||
func ApplicationValidation(sl validator.StructLevel) { | ||
application := sl.Current().Interface().(Application) | ||
|
||
if application.Port > 65535 || application.Port < 0 { | ||
sl.ReportError(application.Port, "port", "", "port must be between 0 and 65535", "") | ||
} | ||
|
||
if application.Sid != nil { | ||
for _, sid := range application.Sid { | ||
ip := net.ParseIP(sid) | ||
if ip == nil || ip.To4() != nil { | ||
sl.ReportError(sid, "sid", "", "sid must be a valid ipv6 address", "") | ||
} | ||
} | ||
} | ||
|
||
if application.Sid == nil && application.Intents == nil { | ||
sl.ReportError(application.Sid, "sid", "", "sid or intents must be specified", "") | ||
} | ||
|
||
for k, intent := range application.Intents { | ||
intentType, err := types.ParseIntentType(intent.Intent) | ||
if err != nil { | ||
sl.ReportError(intent.Intent, "intent", "", "invalid intent", "") | ||
} | ||
|
||
// sfc intent must be the first intent in the list | ||
if intentType == types.IntentTypeSfc { | ||
if k != 0 { | ||
sl.ReportError(intent.Intent, "intent", "", "sfc intent must be the first intent in the list", "") | ||
} | ||
|
||
if application.Sid == nil { | ||
sl.ReportError(intent.Intent, "sid", "", "sid is required as backup path when using an sfc intent", "") | ||
} | ||
} | ||
|
||
// flex-algo intent must be the first intent in the list if there is no sfc intent | ||
if intentType == types.IntentTypeFlexAlgo { | ||
if k >= 1 && application.Intents[0].Intent != types.IntentTypeSfc.String() { | ||
sl.ReportError(intent.Intent, "intent", "", "flex-algo intent must be the first intent in the list", "") | ||
} | ||
} | ||
} | ||
} |
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,37 @@ | ||
package config | ||
|
||
import ( | ||
"github.com/go-playground/validator" | ||
"github.com/hawkv6/hawkwing/pkg/types" | ||
) | ||
|
||
func IntentValidation(sl validator.StructLevel) { | ||
intent := sl.Current().Interface().(Intent) | ||
|
||
intentType, err := types.ParseIntentType(intent.Intent) | ||
if err != nil { | ||
sl.ReportError(intent.Intent, "intent", "", "invalid intent", "") | ||
} | ||
|
||
if intentType == types.IntentTypeFlexAlgo || intentType == types.IntentTypeSfc { | ||
if intent.MinValue > 0 { | ||
sl.ReportError(intent.MinValue, "min_value", "", "min_value and max_value are not allowed for flex-algo and sfc intents", "") | ||
} | ||
if intent.MaxValue > 0 { | ||
sl.ReportError(intent.MaxValue, "max_value", "", "min_value and max_value are not allowed for flex-algo and sfc intents", "") | ||
} | ||
} | ||
|
||
if intentType == types.IntentTypeFlexAlgo { | ||
if intent.FlexAlgoNr == 0 { | ||
sl.ReportError(intent.FlexAlgoNr, "flex_algo_number", "", "flex_algo_number is required when using an flex_algo intent", "") | ||
} | ||
} | ||
|
||
if intentType == types.IntentTypeSfc { | ||
if intent.Functions == nil { | ||
sl.ReportError(intent.Functions, "functions", "", "functions is required when using an sfc intent", "") | ||
} | ||
} | ||
|
||
} |
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,29 @@ | ||
package config | ||
|
||
import ( | ||
"net" | ||
|
||
"github.com/go-playground/validator" | ||
) | ||
|
||
func ServiceConfigValidation(sl validator.StructLevel) { | ||
serviceCfg := sl.Current().Interface().(ServiceConfig) | ||
|
||
if len(serviceCfg.DomainName) == 0 && len(serviceCfg.Ipv6Addresses) == 0 { | ||
sl.ReportError(serviceCfg.DomainName, "domain_name", "", "domain_name or ipv6_addresses is required", "") | ||
sl.ReportError(serviceCfg.Ipv6Addresses, "ipv6_addresses", "", "domain_name or ipv6_addresses is required", "") | ||
} | ||
|
||
if serviceCfg.Ipv6Addresses != nil { | ||
for _, ipv6 := range serviceCfg.Ipv6Addresses { | ||
ip := net.ParseIP(ipv6) | ||
if ip == nil || ip.To4() != nil { | ||
sl.ReportError(ipv6, "ipv6_addresses", "", "ipv6_addresses must be valid ipv6 addresses", "") | ||
} | ||
} | ||
} | ||
|
||
if len(serviceCfg.Applications) == 0 { | ||
sl.ReportError(serviceCfg.Applications, "applications", "", "at least one application must be specified", "") | ||
} | ||
} |
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
Oops, something went wrong.