From c060caeb3d98c1bd086a43ff6c09b1f49a54cdff Mon Sep 17 00:00:00 2001 From: Ola Saadi Date: Sun, 14 Jan 2024 14:11:53 +0200 Subject: [PATCH] fix --- ..._testing5_all_vpcs_subnetsBased_withPGW.md | 2 -- ...ting5_old_all_vpcs_subnetsBased_withPGW.md | 2 -- pkg/vpcmodel/grouping.go | 5 ++++- pkg/vpcmodel/output.go | 20 +++++++++++-------- pkg/vpcmodel/semanticDiff.go | 10 ++++++---- 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/pkg/ibmvpc/examples/acl_testing5_all_vpcs_subnetsBased_withPGW.md b/pkg/ibmvpc/examples/acl_testing5_all_vpcs_subnetsBased_withPGW.md index 651d5bd9d..26c7c8efa 100644 --- a/pkg/ibmvpc/examples/acl_testing5_all_vpcs_subnetsBased_withPGW.md +++ b/pkg/ibmvpc/examples/acl_testing5_all_vpcs_subnetsBased_withPGW.md @@ -16,5 +16,3 @@ | sub2-2-ky | sub2-1-ky | All Connections | | sub3-1-ky | sub1-1-ky | protocol: ICMP icmp-type: 0 icmp-code: 0 | | sub3-1-ky | sub2-1-ky | protocol: ICMP icmp-type: 0 icmp-code: 0; protocol: TCP dst-ports: 443 | - -connections are stateful unless marked with * diff --git a/pkg/ibmvpc/examples/acl_testing5_old_all_vpcs_subnetsBased_withPGW.md b/pkg/ibmvpc/examples/acl_testing5_old_all_vpcs_subnetsBased_withPGW.md index c2f3eba91..9803523af 100644 --- a/pkg/ibmvpc/examples/acl_testing5_old_all_vpcs_subnetsBased_withPGW.md +++ b/pkg/ibmvpc/examples/acl_testing5_old_all_vpcs_subnetsBased_withPGW.md @@ -12,5 +12,3 @@ | sub2-1-ky | Public Internet 8.8.8.8/32 | protocol: UDP dst-ports: 53 | | sub2-1-ky | sub2-2-ky | All Connections | | sub2-2-ky | sub2-1-ky | All Connections | - -connections are stateful unless marked with * diff --git a/pkg/vpcmodel/grouping.go b/pkg/vpcmodel/grouping.go index f26d146b4..3cb0e859c 100644 --- a/pkg/vpcmodel/grouping.go +++ b/pkg/vpcmodel/grouping.go @@ -498,7 +498,10 @@ func (g *GroupConnLines) String() string { func (g *GroupConnLines) hasStatelessConns() bool { hasStatelessConns := false for _, line := range g.GroupedLines { - hasStatelessConns = hasStatelessConns || line.commonProperties.conn.IsStateful == common.StatefulFalse + if line.commonProperties.conn.IsStateful == common.StatefulFalse { + hasStatelessConns = true + break + } } return hasStatelessConns } diff --git a/pkg/vpcmodel/output.go b/pkg/vpcmodel/output.go index 9bd1bff17..47b6f8c33 100644 --- a/pkg/vpcmodel/output.go +++ b/pkg/vpcmodel/output.go @@ -190,7 +190,7 @@ func (of *serialOutputFormatter) WriteOutput(c1, c2 map[string]*VPCConfig, conns if err2 != nil { return "", err2 } - return of.WriteDiffOutput(vpcAnalysisOutput, outFile) + return of.WriteDiffOutput(vpcAnalysisOutput, uc, outFile) } func WriteToFile(content, fileName string) (string, error) { @@ -201,6 +201,14 @@ func WriteToFile(content, fileName string) (string, error) { return content, nil } +func getAsteriskDetails(uc OutputUseCase, hasStatelessConn bool, outFormat OutFormat) string { + if uc != SingleSubnet && (outFormat == Text || outFormat == MD || outFormat == Debug) && hasStatelessConn { + return asteriskDetails + } + + return "" +} + // AggregateVPCsOutput returns the output string for a list of SingleAnalysisOutput objects // and writes the output to outFile func (of *serialOutputFormatter) AggregateVPCsOutput(outputList []*SingleAnalysisOutput, uc OutputUseCase, outFile string) (string, error) { @@ -218,9 +226,7 @@ func (of *serialOutputFormatter) AggregateVPCsOutput(outputList []*SingleAnalysi vpcsOut := make([]string, len(outputList)) for i, o := range outputList { vpcsOut[i] = o.Output - if uc != SingleSubnet && o.hasStatelessConn { - infoMessage = asteriskDetails - } + infoMessage = getAsteriskDetails(uc, o.hasStatelessConn, of.outFormat) } sort.Strings(vpcsOut) res, err = WriteToFile(strings.Join(vpcsOut, "\n")+infoMessage, outFile) @@ -236,15 +242,13 @@ func (of *serialOutputFormatter) AggregateVPCsOutput(outputList []*SingleAnalysi } // WriteDiffOutput actual writing the output into file, with required format adjustments -func (of *serialOutputFormatter) WriteDiffOutput(output *SingleAnalysisOutput, outFile string) (string, error) { +func (of *serialOutputFormatter) WriteDiffOutput(output *SingleAnalysisOutput, uc OutputUseCase, outFile string) (string, error) { var res string var err error switch of.outFormat { case Text, MD, Debug: // currently, return out as is infoMessage := "" - if output.hasStatelessConn { - infoMessage = asteriskDetails - } + infoMessage = getAsteriskDetails(uc, output.hasStatelessConn, of.outFormat) res, err = WriteToFile(output.Output+infoMessage, outFile) case JSON: all := map[string]interface{}{} diff --git a/pkg/vpcmodel/semanticDiff.go b/pkg/vpcmodel/semanticDiff.go index 735fbbe9f..79811dad2 100644 --- a/pkg/vpcmodel/semanticDiff.go +++ b/pkg/vpcmodel/semanticDiff.go @@ -275,11 +275,13 @@ func (diffCfgs *diffBetweenCfgs) String() string { func (diffCfgs *diffBetweenCfgs) hasStatelessConns() bool { hasStatelessConns := false for _, grouped := range diffCfgs.groupedLines { - hasStatelessConns = hasStatelessConns || - (grouped.commonProperties.connDiff.conn1 != nil && - grouped.commonProperties.connDiff.conn1.IsStateful == common.StatefulFalse) || + if (grouped.commonProperties.connDiff.conn1 != nil && + grouped.commonProperties.connDiff.conn1.IsStateful == common.StatefulFalse) || (grouped.commonProperties.connDiff.conn2 != nil && - grouped.commonProperties.connDiff.conn2.IsStateful == common.StatefulFalse) + grouped.commonProperties.connDiff.conn2.IsStateful == common.StatefulFalse) { + hasStatelessConns = true + break + } } return hasStatelessConns }