Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
olasaadi99 committed Jan 21, 2024
1 parent a07d3f9 commit f06cdda
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 17 deletions.
9 changes: 7 additions & 2 deletions cmd/analyzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,17 @@ func analysisTypeToUseCase(inArgs *InArgs) vpcmodel.OutputUseCase {
}

func analysisVPCConfigs(c1, c2 map[string]*vpcmodel.VPCConfig, inArgs *InArgs, outFile string) (string, error) {
var explanationArgs *vpcmodel.ExplanationArgs
if *inArgs.AnalysisType == explainMode {
explanationArgs = vpcmodel.NewExplanationArgs(*inArgs.ESrc, *inArgs.EDst, *inArgs.EProtocol,
*inArgs.ESrcMinPort, *inArgs.ESrcMaxPort, *inArgs.EDstMinPort, *inArgs.EDstMaxPort)
}

og, err := vpcmodel.NewOutputGenerator(c1, c2,
*inArgs.Grouping,
analysisTypeToUseCase(inArgs),
false,
vpcmodel.NewExplanationArgs(*inArgs.ESrc, *inArgs.EDst, *inArgs.EProtocol,
*inArgs.ESrcMinPort, *inArgs.ESrcMaxPort, *inArgs.EDstMinPort, *inArgs.EDstMaxPort))
explanationArgs)
if err != nil {
return "", err
}
Expand Down
13 changes: 6 additions & 7 deletions cmd/analyzer/parse_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ func ParseInArgs(cmdlineArgs []string) (*InArgs, error) {
args.ESrc = flagset.String(ESrc, "", "Src name for network_interface or an external ip to be explained")
args.EDst = flagset.String(EDst, "", "Dst name for network_interface or an external ip to be explained")
args.EProtocol = flagset.String(EProtocol, "", "Protocol for connection description")
args.ESrcMinPort = flagset.Int64(ESrcMinPort, common.MinPort, "SrcMinPort for connection description")
args.ESrcMaxPort = flagset.Int64(ESrcMaxPort, common.MaxPort, "SrcMaxPort for connection description")
args.EDstMinPort = flagset.Int64(EDstMinPort, common.MinPort, "DstMinPort for connection description")
args.EDstMaxPort = flagset.Int64(EDstMaxPort, common.MaxPort, "DstMaxPort for connection description")
args.ESrcMinPort = flagset.Int64(ESrcMinPort, common.MinPort, "minimum source port for connection description")
args.ESrcMaxPort = flagset.Int64(ESrcMaxPort, common.MaxPort, "maximum source port for connection description")
args.EDstMinPort = flagset.Int64(EDstMinPort, common.MinPort, "minimum destination port for connection description")
args.EDstMaxPort = flagset.Int64(EDstMaxPort, common.MaxPort, "maximum destination port for connection description")

// calling parseCmdLine prior to flagset.Parse to ensure that excessive and unsupported arguments are handled
// for example, flagset.Parse() ignores input args missing the `-`
Expand Down Expand Up @@ -280,7 +280,7 @@ func validRangeConnectionExplainMode(args *InArgs) error {
func invalidArgsExplainMode(args *InArgs, flagset *flag.FlagSet) error {
if *args.AnalysisType != explainMode {
if wereExplainParamsSpecified(flagset, []string{ESrc, EDst, EProtocol, ESrcMinPort, ESrcMaxPort, EDstMinPort, EDstMaxPort, explainMode}) {
return fmt.Errorf("%s, %s, %s, %s, %s, %s and %s can be specified only when analysis-type is %s",
return fmt.Errorf("Explainability related params %s, %s, %s, %s, %s, %s and %s can be specified only in explain mode: analysis-type equals %s",

Check failure on line 283 in cmd/analyzer/parse_args.go

View workflow job for this annotation

GitHub Actions / golangci-lint

line is 146 characters (lll)
ESrc, EDst, EProtocol, ESrcMinPort, ESrcMaxPort, EDstMinPort, EDstMaxPort, explainMode)
}
return nil
Expand All @@ -292,8 +292,7 @@ func invalidArgsExplainMode(args *InArgs, flagset *flag.FlagSet) error {

if *args.EProtocol == "" {
if wereExplainParamsSpecified(flagset, []string{EProtocol, ESrcMinPort, ESrcMaxPort, EDstMinPort, EDstMaxPort, explainMode}) {
return fmt.Errorf("%s, %s, %s and %s can be specified only when specifying %s",
ESrcMinPort, ESrcMaxPort, EDstMinPort, EDstMaxPort, EProtocol)
return fmt.Errorf("Protocol must be specified when querying a specific connection")

Check failure on line 295 in cmd/analyzer/parse_args.go

View workflow job for this annotation

GitHub Actions / golangci-lint

ST1005: error strings should not be capitalized (stylecheck)
}
return nil
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/ibmvpc/analysis_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,21 @@ var tests = []*vpcGeneralTest{
format: vpcmodel.Text,
},
{
name: "acl_testing3",
// TODO: currently skipping uc3 since it is not supported with partial subnet connectivity
name: "acl_testing3",
useCases: []vpcmodel.OutputUseCase{vpcmodel.Explain},
format: vpcmodel.Text,
ESrc: "vsi1-ky[10.240.10.4]",
EDst: "vsi2-ky[10.240.20.4]",
},
{
name: "sg_testing1_new",
useCases: []vpcmodel.OutputUseCase{vpcmodel.Explain},
format: vpcmodel.Text,
ESrc: "vsi1-ky[10.240.10.4]",
EDst: "vsi2-ky[10.240.20.4]",
ESrcMinPort: 1,
ESrcMaxPort: 5,
},
{
name: "sg_testing1_new",
useCases: []vpcmodel.OutputUseCase{vpcmodel.AllEndpoints, vpcmodel.SingleSubnet, vpcmodel.AllSubnets},
Expand Down
9 changes: 9 additions & 0 deletions pkg/ibmvpc/examples/sg_testing1_new_all_vpcs_explain.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Connectivity explanation between vsi1-ky[10.240.10.4] and vsi2-ky[10.240.20.4]
No connection between vsi1-ky[10.240.10.4] and vsi2-ky[10.240.20.4]; connection blocked by egress
Ingress Rules:
~~~~~~~~~~~~~~
SecurityGroupLayer Rules
------------------------
enabling rules from sg2-ky:
index: 4, direction: inbound, protocol: all, cidr: 10.240.10.4/32

2 changes: 1 addition & 1 deletion pkg/vpcmodel/nodesExplainability.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Explanation struct {
groupedLines []*groupedConnLine
}

func TranslateCDtoConnectionSet(protocol string, srcMinPort, srcMaxPort, dstMinPort, dstMaxPort int64) *common.ConnectionSet {
func translateCDtoConnectionSet(protocol string, srcMinPort, srcMaxPort, dstMinPort, dstMaxPort int64) *common.ConnectionSet {
if protocol == "" {
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/vpcmodel/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func NewOutputGenerator(c1, c2 map[string]*VPCConfig, grouping bool, uc OutputUs
res.cfgsDiff = configsDiff
}
if uc == Explain {
connQuery := TranslateCDtoConnectionSet(explanationArgs.protocol, explanationArgs.srcMinPort,
connQuery := translateCDtoConnectionSet(explanationArgs.protocol, explanationArgs.srcMinPort,
explanationArgs.srcMaxPort, explanationArgs.dstMinPort, explanationArgs.dstMaxPort)
explanation, err := c1[i].ExplainConnectivity(explanationArgs.src, explanationArgs.dst, connQuery)
if err != nil {
Expand Down Expand Up @@ -182,7 +182,7 @@ func (of *serialOutputFormatter) WriteOutput(c1, c2 map[string]*VPCConfig, conns
subnetsConns map[string]*VPCsubnetConnectivity, subnetsDiff *diffBetweenCfgs,
outFile string, grouping bool, uc OutputUseCase, explainStruct *Explanation) (string, error) {
diffAnalysis := uc == EndpointsDiff || uc == SubnetsDiff
if !diffAnalysis {
if !diffAnalysis && uc != Explain {
outputPerVPC := make([]*SingleAnalysisOutput, len(c1))
i := 0
for name := range c1 {
Expand All @@ -197,15 +197,15 @@ func (of *serialOutputFormatter) WriteOutput(c1, c2 map[string]*VPCConfig, conns
}
return of.AggregateVPCsOutput(outputPerVPC, uc, outFile)
}
// its a diff mode, we have only one vpc on each map:
// its diff or explain mode, we have only one vpc on each map:
name, _ := common.AnyMapEntry(c1)
vpcAnalysisOutput, err2 :=
of.createSingleVpcFormatter().WriteOutput(c1[name], c2[name], conns[name], subnetsConns[name],
subnetsDiff, "", grouping, uc, explainStruct)
if err2 != nil {
return "", err2
}
return of.WriteDiffOutput(vpcAnalysisOutput, uc, outFile)
return of.WriteDiffOrExplainOutput(vpcAnalysisOutput, uc, outFile)
}

func WriteToFile(content, fileName string) (string, error) {
Expand Down Expand Up @@ -261,7 +261,7 @@ func (of *serialOutputFormatter) AggregateVPCsOutput(outputList []*SingleAnalysi
}

// WriteDiffOutput actual writing the output into file, with required format adjustments
func (of *serialOutputFormatter) WriteDiffOutput(output *SingleAnalysisOutput, uc OutputUseCase, outFile string) (string, error) {
func (of *serialOutputFormatter) WriteDiffOrExplainOutput(output *SingleAnalysisOutput, uc OutputUseCase, outFile string) (string, error) {
var res string
var err error
switch of.outFormat {
Expand Down

0 comments on commit f06cdda

Please sign in to comment.