Răsfoiți Sursa

fix(go): stabilize get user remote access gw;

VishalDalwadi 1 zi în urmă
părinte
comite
103a8f076c
2 a modificat fișierele cu 34 adăugiri și 12 ștergeri
  1. 25 11
      controllers/ext_client.go
  2. 9 1
      pro/controllers/users.go

+ 25 - 11
controllers/ext_client.go

@@ -703,20 +703,34 @@ func createExtClient(w http.ResponseWriter, r *http.Request) {
 			logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal"))
 			return
 		}
-		for _, extclient := range extclients {
-			// if device id is sent, then make sure extclient with the same device id
-			// does not exist.
-			if customExtClient.DeviceID != "" && extclient.DeviceID == customExtClient.DeviceID &&
-				extclient.OwnerID == caller.UserName && nodeid == extclient.IngressGatewayID {
-				err = errors.New("remote client config already exists on the gateway")
-				slog.Error("failed to create extclient", "user", userName, "error", err)
-				logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
-				return
+
+		// if device id is sent, we don't want to create another extclient for the same user
+		// and gw, with the same device id.
+		if customExtClient.DeviceID != "" {
+			// let's first confirm that none of the user's extclients for this gw have device id.
+			for _, extclient := range extclients {
+				if extclient.DeviceID == customExtClient.DeviceID &&
+					extclient.OwnerID == caller.UserName && nodeid == extclient.IngressGatewayID {
+					err = errors.New("remote client config already exists on the gateway")
+					slog.Error("failed to create extclient", "user", userName, "error", err)
+					logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
+					return
+				}
 			}
+		}
 
+		for _, extclient := range extclients {
 			if extclient.RemoteAccessClientID != "" &&
-				extclient.RemoteAccessClientID == customExtClient.RemoteAccessClientID && extclient.OwnerID == caller.UserName && nodeid == extclient.IngressGatewayID {
-				// extclient on the gw already exists for the remote access client
+				extclient.RemoteAccessClientID == customExtClient.RemoteAccessClientID &&
+				extclient.OwnerID == caller.UserName && nodeid == extclient.IngressGatewayID {
+				if customExtClient.DeviceID != "" && extclient.DeviceID == "" {
+					// This extclient doesn’t include a device ID (and neither do the others).
+					// We patch it by assigning the device ID from the incoming request.
+					// When clients see that the config already exists, they will fetch
+					// the one with their device ID. And we will return this one.
+					extclient.DeviceID = customExtClient.DeviceID
+					_ = logic.SaveExtClient(&extclient)
+				}
 				err = errors.New("remote client config already exists on the gateway")
 				slog.Error("failed to create extclient", "user", userName, "error", err)
 				logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))

+ 9 - 1
pro/controllers/users.go

@@ -14,6 +14,7 @@ import (
 	"github.com/gravitl/netmaker/pro/idp/azure"
 	"github.com/gravitl/netmaker/pro/idp/google"
 	"github.com/gravitl/netmaker/pro/idp/okta"
+	"golang.org/x/exp/slices"
 
 	"github.com/google/uuid"
 	"github.com/gorilla/mux"
@@ -1500,7 +1501,14 @@ func getUserRemoteAccessGwsV1(w http.ResponseWriter, r *http.Request) {
 			userExtClients[extClient.IngressGatewayID] = []models.ExtClient{}
 		}
 
-		userExtClients[extClient.IngressGatewayID] = append(userExtClients[extClient.IngressGatewayID], extClient)
+		index, ok := slices.BinarySearchFunc(userExtClients[extClient.IngressGatewayID], extClient, func(a models.ExtClient, b models.ExtClient) int {
+			return strings.Compare(a.ClientID, b.ClientID)
+		})
+		if ok {
+			continue
+		}
+
+		userExtClients[extClient.IngressGatewayID] = slices.Insert(userExtClients[extClient.IngressGatewayID], index, extClient)
 	}
 
 	for ingressGatewayID, extClients := range userExtClients {