From b07aa56ac41b1c6f827af267a65a99f1a9c75910 Mon Sep 17 00:00:00 2001 From: Neil-Jocelyn Schark <neil.schark@h-da.de> Date: Wed, 3 Jul 2024 15:39:35 +0000 Subject: [PATCH] Resolve "Disable checking of IP address in gosdnc" See merge request danet/gosdn!941 --- .gitlab/ci/.test.yml | 8 +++++++ Makefile | 3 +++ cli/cmd/networkElementCreate.go | 2 +- cli/cmd/utils.go | 24 +++++++++++++------- cli/cmd/utils_test.go | 39 +++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 cli/cmd/utils_test.go diff --git a/.gitlab/ci/.test.yml b/.gitlab/ci/.test.yml index 99ea33631..7bc08f6a1 100644 --- a/.gitlab/ci/.test.yml +++ b/.gitlab/ci/.test.yml @@ -39,3 +39,11 @@ integration-test-gosdn: script: - go test -p 1 ./integration-tests/* <<: *test + +unit-test: + image: ${CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX}/golang:$GOLANG_VERSION-bookworm + before_script: + - apt update && apt install -y make + script: + - make unit-test-new + <<: *test diff --git a/Makefile b/Makefile index 0a27ad738..bdd980b1c 100644 --- a/Makefile +++ b/Makefile @@ -106,6 +106,9 @@ integration-tests-debug-up: generate-certs containerize-gosdn containerize-plugi integration-tests-debug-down: docker-compose -f dev_env_data/docker-compose/integration-test_docker-compose.yml down +unit-test-new: install-tools + go test ./cli/... + # Warning: Depending on your go and development configuration might also clean caches, modules and docker containers from your other projects. clean: rm -rf $(BUILD_ARTIFACTS_PATH) diff --git a/cli/cmd/networkElementCreate.go b/cli/cmd/networkElementCreate.go index 121c73238..5aa2397bc 100644 --- a/cli/cmd/networkElementCreate.go +++ b/cli/cmd/networkElementCreate.go @@ -49,7 +49,7 @@ if they diverge from the default credentials (user:'admin' and pw:'arista').`, Run: func(cmd *cobra.Command, args []string) { spinner, _ := pterm.DefaultSpinner.Start("Creating new network element") - err := checkIPPort(address) + err := checkIPorHostnameAndPort(address) if err != nil { spinner.Fail(err) return diff --git a/cli/cmd/utils.go b/cli/cmd/utils.go index ca51618da..cbf9a81df 100644 --- a/cli/cmd/utils.go +++ b/cli/cmd/utils.go @@ -41,19 +41,27 @@ import ( gpb "github.com/openconfig/gnmi/proto/gnmi" gnmiv "github.com/openconfig/gnmi/value" "github.com/openconfig/goyang/pkg/yang" + "golang.org/x/net/idna" "google.golang.org/grpc/metadata" ) -func checkIPPort(string) error { +func checkIPorHostnameAndPort(address string) error { // check if address is in the format <IP>:<port> - ip, _, err := net.SplitHostPort(address) - if err != nil { - return err + ipOrHostname, _, err := net.SplitHostPort(address) + if err != nil || ipOrHostname == "" { + errorMessage := fmt.Sprintf("address '%s' is not in the format <IP/Hostname>:<port>", address) + return errors.New(errorMessage) } - // Check IP - ip2 := net.ParseIP(ip) - if ip2 == nil { - return errors.New("invalid IP") + + // If not IP, check if valid Hostname + _, err = idna.Lookup.ToASCII(ipOrHostname) + if err != nil { + // Check if valid ipIP + ip2 := net.ParseIP(ipOrHostname) + if ip2 == nil { + errorMessage := fmt.Sprintf("address '%s' is neither a valid IP nor a hostname", ipOrHostname) + return errors.New(errorMessage) + } } return nil } diff --git a/cli/cmd/utils_test.go b/cli/cmd/utils_test.go new file mode 100644 index 000000000..72fa690d9 --- /dev/null +++ b/cli/cmd/utils_test.go @@ -0,0 +1,39 @@ +package cmd + +import ( + "testing" +) + +func TestCheckIPorHostnameAndPort(t *testing.T) { + // Test cases for valid IP addresses or hostnames with ports + validCases := []string{ + "127.0.0.1:8080", + "localhost:8080", + "example.com:8080", + "[::1]:8080", + "[2001:db8::1]:1234", + "256.256.256.900:1234", // Looks weird, but is valid as a hostname. + } + + for _, addr := range validCases { + err := checkIPorHostnameAndPort(addr) + if err != nil { + t.Errorf("Expected no error for address %s, but got: %v", addr, err) + } + } +} + +func TestCheckIPorHostnameAndPortInvalid(t *testing.T) { + // Test cases for invalid IP addresses or hostnames with ports + invalidCases := []string{ + ":8080", // Missing IP address or hostname + "example.com", // Missing port + } + + for _, addr := range invalidCases { + err := checkIPorHostnameAndPort(addr) + if err == nil { + t.Errorf("Expected error for address %s, but got no error", addr) + } + } +} -- GitLab