From b453897e654a07a2a2fd5821c527aa0fc63f8cb5 Mon Sep 17 00:00:00 2001 From: "Matthew R. Kasun" Date: Mon, 14 Nov 2022 14:41:34 -0500 Subject: [PATCH] ensure netclient version is compatible --- controllers/node.go | 7 +++++++ go.mod | 3 +++ go.sum | 4 ++++ logic/version.go | 31 +++++++++++++++++++++++++++++++ logic/version_test.go | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 logic/version.go create mode 100644 logic/version_test.go diff --git a/controllers/node.go b/controllers/node.go index 51a89bac..f67dd3fe 100644 --- a/controllers/node.go +++ b/controllers/node.go @@ -2,6 +2,7 @@ package controller import ( "encoding/json" + "errors" "fmt" "net/http" "strings" @@ -574,6 +575,12 @@ func createNode(w http.ResponseWriter, r *http.Request) { return } + if !logic.IsVersionComptatible(node.Version) { + err := errors.New("incomatible netclient version") + logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) + return + } + node.Network = networkName network, err := logic.GetNetworkByNode(&node) diff --git a/go.mod b/go.mod index 85e07b86..6e370546 100644 --- a/go.mod +++ b/go.mod @@ -46,6 +46,8 @@ require ( golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 ) +require github.com/matryer/is v1.4.0 // indirect + require ( cloud.google.com/go/compute v1.7.0 // indirect fyne.io/systray v1.10.1-0.20220621085403-9a2652634e93 // indirect @@ -73,6 +75,7 @@ require ( github.com/golang/protobuf v1.5.2 // indirect github.com/google/go-cmp v0.5.8 // indirect github.com/gopherjs/gopherjs v1.17.2 // indirect + github.com/hashicorp/go-version v1.6.0 github.com/josharian/native v1.0.0 // indirect github.com/jsummers/gobmp v0.0.0-20151104160322-e2ba15ffa76e // indirect github.com/kr/text v0.2.0 // indirect diff --git a/go.sum b/go.sum index b673cacb..58660545 100644 --- a/go.sum +++ b/go.sum @@ -295,6 +295,8 @@ github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerX github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4= github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= github.com/hashicorp/go-uuid v1.0.1/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro= +github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= +github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= github.com/hashicorp/go.net v0.0.1/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= @@ -335,6 +337,8 @@ github.com/lib/pq v1.10.7/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/lucor/goinfo v0.0.0-20210802170112-c078a2b0f08b/go.mod h1:PRq09yoB+Q2OJReAmwzKivcYyremnibWGbK7WfftHzc= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/magiconair/properties v1.8.5/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60= +github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE= +github.com/matryer/is v1.4.0/go.mod h1:8I/i5uYgLzgsgEloJE1U6xx5HkBQpAZvepWuujKwMRU= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRRpdGg= diff --git a/logic/version.go b/logic/version.go new file mode 100644 index 00000000..24576a93 --- /dev/null +++ b/logic/version.go @@ -0,0 +1,31 @@ +package logic + +import ( + "strings" + "unicode" + + "github.com/hashicorp/go-version" +) + +const MinVersion = "v0.17.0" + +// IsVersionCompatible checks that the version passed is compabtible (>=) with MinVersion +func IsVersionComptatible(ver string) bool { + // during dev, assume developers know what they are doing + if ver == "dev" { + return true + } + trimmed := strings.TrimFunc(ver, func(r rune) bool { + return !unicode.IsNumber(r) + }) + v, err := version.NewVersion(trimmed) + if err != nil { + return false + } + constraint, err := version.NewConstraint(">= " + MinVersion) + if err != nil { + return false + } + return constraint.Check(v) + +} diff --git a/logic/version_test.go b/logic/version_test.go new file mode 100644 index 00000000..c94af967 --- /dev/null +++ b/logic/version_test.go @@ -0,0 +1,35 @@ +package logic + +import ( + "testing" + + "github.com/matryer/is" +) + +func TestVersion(t *testing.T) { + t.Run("valid version", func(t *testing.T) { + is := is.New(t) + valid := IsVersionComptatible("v0.17.1-testing") + is.Equal(valid, true) + }) + t.Run("dev version", func(t *testing.T) { + is := is.New(t) + valid := IsVersionComptatible("dev") + is.Equal(valid, true) + }) + t.Run("invalid version", func(t *testing.T) { + is := is.New(t) + valid := IsVersionComptatible("v0.14.2-refactor") + is.Equal(valid, false) + }) + t.Run("no version", func(t *testing.T) { + is := is.New(t) + valid := IsVersionComptatible("testing") + is.Equal(valid, false) + }) + t.Run("incomplete version", func(t *testing.T) { + is := is.New(t) + valid := IsVersionComptatible("0.18") + is.Equal(valid, true) + }) +}