From 84350210ac6df97ddb28ca3f3084c0098a345ab9 Mon Sep 17 00:00:00 2001 From: Ola Saadi Date: Sun, 14 Jan 2024 12:42:13 +0200 Subject: [PATCH] fix --- cmd/analyzer/main.go | 2 +- cmd/analyzer/parse_args.go | 37 +++++++++++++++++++++++-------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/cmd/analyzer/main.go b/cmd/analyzer/main.go index 0546dddc0..e116dac33 100644 --- a/cmd/analyzer/main.go +++ b/cmd/analyzer/main.go @@ -139,7 +139,7 @@ func _main(cmdlineArgs []string) error { } fmt.Println(vpcAnalysisOutput) - if *inArgs.QProtocol != "" { + if *inArgs.AnalysisType == queryMode { _ = translateCDtoConnectionSet(inArgs) } diff --git a/cmd/analyzer/parse_args.go b/cmd/analyzer/parse_args.go index fd2e9816c..228a9c7fb 100644 --- a/cmd/analyzer/parse_args.go +++ b/cmd/analyzer/parse_args.go @@ -205,7 +205,7 @@ func ParseInArgs(cmdlineArgs []string) (*InArgs, error) { if err != nil { return nil, err } - err = invalidArgsConnectionDescription(&args, flagset) + err = invalidArgsQueryMode(&args, flagset) if err != nil { return nil, err } @@ -223,22 +223,16 @@ func isFlagPassed(name string, flagset *flag.FlagSet) bool { return found } -func invalidArgsConnectionDescription(args *InArgs, flagset *flag.FlagSet) error { - if *args.AnalysisType != queryMode { - if isFlagPassed(QProtocol, flagset) || isFlagPassed(QSrcMinPort, flagset) || isFlagPassed(QSrcMaxPort, flagset) || - isFlagPassed(QDstMinPort, flagset) || isFlagPassed(QDstMaxPort, flagset) { - return fmt.Errorf("%s, %s, %s, %s and %s can be specified only when analysis-type is query", - QProtocol, QSrcMinPort, QSrcMaxPort, QDstMinPort, QDstMaxPort) - } - return nil +func isQueryModeParamsPassed(flagset *flag.FlagSet) bool { + if isFlagPassed(QProtocol, flagset) || isFlagPassed(QSrcMinPort, flagset) || isFlagPassed(QSrcMaxPort, flagset) || + isFlagPassed(QDstMinPort, flagset) || isFlagPassed(QDstMaxPort, flagset) { + return true } - protocol := strings.ToUpper(*args.QProtocol) - if protocol != "TCP" && protocol != "UDP" && protocol != "ICMP" { - return fmt.Errorf("wrong connection description protocol '%s'; must be one of: 'TCP, UDP, ICMP'", protocol) - } - args.QProtocol = &protocol + return false +} +func validRangeConnectionQueryMode(args *InArgs) error { if *args.QSrcMinPort > *args.QSrcMaxPort { return fmt.Errorf("srcMaxPort %d should be higher than srcMinPort %d", *args.QSrcMaxPort, *args.QSrcMinPort) } @@ -260,6 +254,21 @@ func invalidArgsConnectionDescription(args *InArgs, flagset *flag.FlagSet) error return nil } +func invalidArgsQueryMode(args *InArgs, flagset *flag.FlagSet) error { + if *args.AnalysisType != queryMode && isQueryModeParamsPassed(flagset) { + return fmt.Errorf("%s, %s, %s, %s and %s can be specified only when analysis-type is query", + QProtocol, QSrcMinPort, QSrcMaxPort, QDstMinPort, QDstMaxPort) + } + + protocol := strings.ToUpper(*args.QProtocol) + if protocol != "TCP" && protocol != "UDP" && protocol != "ICMP" { + return fmt.Errorf("wrong connection description protocol '%s'; must be one of: 'TCP, UDP, ICMP'", protocol) + } + args.QProtocol = &protocol + + return validRangeConnectionQueryMode(args) +} + func errorInErgs(args *InArgs, flagset *flag.FlagSet) error { if !*args.Version && (args.InputConfigFile == nil || *args.InputConfigFile == "") { flagset.PrintDefaults()