Skip to content

Commit

Permalink
Merge branch 'main' into 313_explain_add_routingResources
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiriMoran authored Jan 17, 2024
2 parents 3797fb0 + 97c810a commit da1956f
Show file tree
Hide file tree
Showing 13 changed files with 5,907 additions and 477 deletions.
21 changes: 21 additions & 0 deletions cmd/analyzer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"os"

"github.com/np-guard/vpc-network-config-analyzer/pkg/common"
"github.com/np-guard/vpc-network-config-analyzer/pkg/ibmvpc"
"github.com/np-guard/vpc-network-config-analyzer/pkg/version"
"github.com/np-guard/vpc-network-config-analyzer/pkg/vpcmodel"
Expand Down Expand Up @@ -48,6 +49,8 @@ func analysisTypeToUseCase(inArgs *InArgs) vpcmodel.OutputUseCase {
return vpcmodel.SubnetsDiff
case allEndpointsDiff:
return vpcmodel.EndpointsDiff
case explainMode:
return vpcmodel.Explain
}
return vpcmodel.AllEndpoints
}
Expand Down Expand Up @@ -83,6 +86,19 @@ func vpcConfigsFromFile(fileName string, inArgs *InArgs) (map[string]*vpcmodel.V
return vpcConfigs, nil
}

func translateCDtoConnectionSet(inArgs *InArgs) *common.ConnectionSet {
connection := common.NewConnectionSet(false)
if common.ProtocolStr(*inArgs.QProtocol) == common.ProtocolICMP {
connection.AddICMPConnection(common.MinICMPtype, common.MaxICMPtype,
common.MinICMPcode, common.MaxICMPcode)
} else {
connection.AddTCPorUDPConn(common.ProtocolStr(*inArgs.QProtocol), *inArgs.QSrcMinPort, *inArgs.QSrcMaxPort,
*inArgs.QDstMinPort, *inArgs.QDstMaxPort)
}

return connection
}

// The actual main function
// Takes command-line flags and returns an error rather than exiting, so it can be more easily used in testing
func _main(cmdlineArgs []string) error {
Expand Down Expand Up @@ -123,6 +139,11 @@ func _main(cmdlineArgs []string) error {
return err2
}
fmt.Println(vpcAnalysisOutput)

if *inArgs.AnalysisType == explainMode {
_ = translateCDtoConnectionSet(inArgs)
}

return nil
}

Expand Down
23 changes: 19 additions & 4 deletions cmd/analyzer/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
package main

import (
"errors"
"os"
"path/filepath"
"strings"
"testing"
)

// //////////////////////////////////
// this file need to be rewritten, no need to code review it
// ///////////////////////////////////////
func Test_main(t *testing.T) {
// TODO: this file need to be rewritten
func TestMain(t *testing.T) {
tests := []struct {
name string
args string
Expand All @@ -28,4 +29,18 @@ func Test_main(t *testing.T) {
}
})
}
removeGeneratedFiles()
}

func removeGeneratedFiles() {
files1, err1 := filepath.Glob("*.txt")
files2, err2 := filepath.Glob("*.drawio")
if err1 != nil || err2 != nil {
panic(errors.Join(err1, err2))
}
for _, f := range append(files1, files2...) {
if err := os.Remove(f); err != nil {
panic(err)
}
}
}
103 changes: 103 additions & 0 deletions cmd/analyzer/parse_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"slices"
"strings"

"github.com/np-guard/vpc-network-config-analyzer/pkg/common"
)

// InArgs contains the input arguments for the analyzer
Expand All @@ -18,6 +20,11 @@ type InArgs struct {
VPC *string
Debug *bool
Version *bool
QProtocol *string
QSrcMinPort *int64
QSrcMaxPort *int64
QDstMinPort *int64
QDstMaxPort *int64
}

// flagHasValue indicates for each input arg if it is expected to have a value in the cli or not
Expand All @@ -31,6 +38,11 @@ var flagHasValue = map[string]bool{
VPC: true,
Debug: false,
Version: false,
QProtocol: true,
QSrcMinPort: true,
QSrcMaxPort: true,
QDstMinPort: true,
QDstMaxPort: true,
}

const (
Expand All @@ -44,6 +56,11 @@ const (
VPC = "vpc"
Debug = "debug"
Version = "version"
QProtocol = "q-protocol"
QSrcMinPort = "q-src-min-port"
QSrcMaxPort = "q-src-max-port"
QDstMinPort = "q-dst-min-port"
QDstMaxPort = "q-dst-max-port"

// output formats supported
JSONFormat = "json"
Expand All @@ -59,6 +76,7 @@ const (
singleSubnet = "single_subnet" // single subnet connectivity analysis
allEndpointsDiff = "diff_all_endpoints" // semantic diff of allEndpoints analysis between two configurations
allSubnetsDiff = "diff_all_subnets" // semantic diff of allSubnets analysis between two configurations
explainMode = "explain" // explain specified connectivity, given src,dst and connection

// separator
separator = ", "
Expand All @@ -80,6 +98,7 @@ var supportedAnalysisTypesMap = map[string][]string{
singleSubnet: {TEXTFormat},
allEndpointsDiff: {TEXTFormat, MDFormat},
allSubnetsDiff: {TEXTFormat, MDFormat},
explainMode: {TEXTFormat},
}

// supportedOutputFormatsList is an ordered list of supported output formats (usage details presented in this order)
Expand All @@ -99,6 +118,7 @@ var supportedAnalysisTypesList = []string{
singleSubnet,
allEndpointsDiff,
allSubnetsDiff,
explainMode,
}

func getSupportedAnalysisTypesMapString() string {
Expand Down Expand Up @@ -160,6 +180,11 @@ func ParseInArgs(cmdlineArgs []string) (*InArgs, error) {
args.VPC = flagset.String(VPC, "", "CRN of the VPC to analyze")
args.Debug = flagset.Bool(Debug, false, "Run in debug mode")
args.Version = flagset.Bool(Version, false, "Prints the release version number")
args.QProtocol = flagset.String(QProtocol, "", "Protocol for connection description")
args.QSrcMinPort = flagset.Int64(QSrcMinPort, common.MinPort, "SrcMinPort for connection description")
args.QSrcMaxPort = flagset.Int64(QSrcMaxPort, common.MaxPort, "SrcMaxPort for connection description")
args.QDstMinPort = flagset.Int64(QDstMinPort, common.MinPort, "DstMinPort for connection description")
args.QDstMaxPort = flagset.Int64(QDstMaxPort, common.MaxPort, "DstMaxPort 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 All @@ -180,9 +205,87 @@ func ParseInArgs(cmdlineArgs []string) (*InArgs, error) {
if err != nil {
return nil, err
}
err = invalidArgsExplainMode(&args, flagset)
if err != nil {
return nil, err
}

return &args, nil
}

func wasFlagSpecified(name string, flagset *flag.FlagSet) bool {
found := false
flagset.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}

func wereExplainParamsSpecified(flagset *flag.FlagSet) bool {
if wasFlagSpecified(QProtocol, flagset) || wasFlagSpecified(QSrcMinPort, flagset) || wasFlagSpecified(QSrcMaxPort, flagset) ||
wasFlagSpecified(QDstMinPort, flagset) || wasFlagSpecified(QDstMaxPort, flagset) {
return true
}

return false
}

func PortInRange(port int64) bool {
if port > common.MaxPort || port < common.MinPort {
return false
}

return true
}

func minMaxValidity(minPort, maxPort int64, minPortName, maxPortName string) error {
if minPort > maxPort {
return fmt.Errorf("%s %d must not be larger than %s %d", minPortName, minPort, maxPortName, maxPort)
}

return nil
}

func validRangeConnectionExplainMode(args *InArgs) error {
err := minMaxValidity(*args.QSrcMinPort, *args.QSrcMaxPort, QSrcMinPort, QSrcMaxPort)
if err != nil {
return err
}
err = minMaxValidity(*args.QDstMinPort, *args.QDstMaxPort, QDstMinPort, QDstMaxPort)
if err != nil {
return err
}

if !PortInRange(*args.QSrcMinPort) || !PortInRange(*args.QSrcMaxPort) ||
!PortInRange(*args.QDstMinPort) || !PortInRange(*args.QDstMaxPort) {
return fmt.Errorf("%s, %s, %s and %s must be in ranges [%d, %d]",
QSrcMinPort, QSrcMaxPort, QDstMinPort, QDstMaxPort, common.MinPort, common.MaxPort)
}

return nil
}

func invalidArgsExplainMode(args *InArgs, flagset *flag.FlagSet) error {
if *args.AnalysisType != explainMode && wereExplainParamsSpecified(flagset) {
return fmt.Errorf("%s, %s, %s, %s and %s can be specified only when analysis-type is %s",
QProtocol, QSrcMinPort, QSrcMaxPort, QDstMinPort, QDstMaxPort, explainMode)
}

if *args.AnalysisType != explainMode {
return nil
}

protocol := strings.ToUpper(*args.QProtocol)
if protocol != string(common.ProtocolTCP) && protocol != string(common.ProtocolUDP) && protocol != string(common.ProtocolICMP) {
return fmt.Errorf("wrong connection description protocol '%s'; must be one of: 'TCP, UDP, ICMP'", protocol)
}
args.QProtocol = &protocol

return validRangeConnectionExplainMode(args)
}

func errorInErgs(args *InArgs, flagset *flag.FlagSet) error {
if !*args.Version && (args.InputConfigFile == nil || *args.InputConfigFile == "") {
flagset.PrintDefaults()
Expand Down
17 changes: 11 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@ go 1.21

require (
github.com/IBM/vpc-go-sdk v0.47.0
github.com/np-guard/cloud-resource-collector v0.4.0
github.com/np-guard/vpc-network-config-synthesis v0.1.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/IBM/go-sdk-core/v5 v5.14.1 // indirect
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20230118060037-101bda076037 // indirect
github.com/IBM/go-sdk-core/v5 v5.15.0 // indirect
github.com/IBM/networking-go-sdk v0.44.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-openapi/errors v0.20.3 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/go-openapi/errors v0.20.4 // indirect
github.com/go-openapi/strfmt v0.21.7 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.13.0 // indirect
github.com/go-playground/validator/v10 v10.15.5 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.2 // indirect
github.com/leodido/go-urn v1.2.3 // indirect
github.com/hashicorp/go-retryablehttp v0.7.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.11.4 // indirect
go.mongodb.org/mongo-driver v1.12.1 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit da1956f

Please sign in to comment.