Browse Source

ensure netclient version is compatible

Matthew R. Kasun 2 years ago
parent
commit
b453897e65
5 changed files with 80 additions and 0 deletions
  1. 7 0
      controllers/node.go
  2. 3 0
      go.mod
  3. 4 0
      go.sum
  4. 31 0
      logic/version.go
  5. 35 0
      logic/version_test.go

+ 7 - 0
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)

+ 3 - 0
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

+ 4 - 0
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=

+ 31 - 0
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)
+
+}

+ 35 - 0
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)
+	})
+}