Explorar o código

add address field to static client api request

abhishek9686 hai 5 meses
pai
achega
42d9f68015
Modificáronse 3 ficheiros con 36 adicións e 4 borrados
  1. 32 4
      controllers/ext_client.go
  2. 2 0
      controllers/regex.go
  3. 2 0
      models/extclient.go

+ 32 - 4
controllers/ext_client.go

@@ -413,7 +413,7 @@ func createExtClient(w http.ResponseWriter, r *http.Request) {
 		logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
 		return
 	}
-	if err := validateCustomExtClient(&customExtClient, true); err != nil {
+	if err := validateCustomExtClient(&customExtClient, true, params["network"]); err != nil {
 		logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
 		return
 	}
@@ -469,6 +469,8 @@ func createExtClient(w http.ResponseWriter, r *http.Request) {
 	extclient.RemoteAccessClientID = customExtClient.RemoteAccessClientID
 	extclient.IngressGatewayID = nodeid
 	extclient.Network = node.Network
+	extclient.Address = customExtClient.Address
+	extclient.Address6 = customExtClient.Address6
 	extclient.Tags = make(map[models.TagID]struct{})
 	// extclient.Tags[models.TagID(fmt.Sprintf("%s.%s", extclient.Network,
 	// 	models.RemoteAccessTagName))] = struct{}{}
@@ -569,6 +571,7 @@ func updateExtClient(w http.ResponseWriter, r *http.Request) {
 		logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
 		return
 	}
+	update.Address = ""
 	clientid := params["clientid"]
 	oldExtClient, err := logic.GetExtClientByName(clientid)
 	if err != nil {
@@ -585,12 +588,12 @@ func updateExtClient(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 	if oldExtClient.ClientID == update.ClientID {
-		if err := validateCustomExtClient(&update, false); err != nil {
+		if err := validateCustomExtClient(&update, false, params["network"]); err != nil {
 			logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
 			return
 		}
 	} else {
-		if err := validateCustomExtClient(&update, true); err != nil {
+		if err := validateCustomExtClient(&update, true, params["network"]); err != nil {
 			logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest"))
 			return
 		}
@@ -767,7 +770,7 @@ func deleteExtClient(w http.ResponseWriter, r *http.Request) {
 }
 
 // validateCustomExtClient	Validates the extclient object
-func validateCustomExtClient(customExtClient *models.CustomExtClient, checkID bool) error {
+func validateCustomExtClient(customExtClient *models.CustomExtClient, checkID bool, netID string) error {
 	v := validator.New()
 	err := v.Struct(customExtClient)
 	if err != nil {
@@ -802,6 +805,31 @@ func validateCustomExtClient(customExtClient *models.CustomExtClient, checkID bo
 		}
 		//extclient.DNS = customExtClient.DNS
 	}
+	if customExtClient.Address != "" {
+		ip := net.ParseIP(customExtClient.Address)
+		if ip == nil || ip.To4() == nil {
+			return errInvalidExtClientIP
+		}
+
+		// check if ip is free
+		if !(logic.IsIPUnique(netID, ip.String(), database.NODES_TABLE_NAME, false) &&
+			logic.IsIPUnique(netID, ip.String(), database.EXT_CLIENT_TABLE_NAME, false)) {
+			return errDuplicateExtClientIP
+		}
+	}
+
+	if customExtClient.Address6 != "" {
+		ip := net.ParseIP(customExtClient.Address6)
+		if ip == nil || ip.To4() != nil {
+			return errInvalidExtClientIP
+		}
+
+		// check if ip is free
+		if !(logic.IsIPUnique(netID, ip.String(), database.NODES_TABLE_NAME, true) &&
+			logic.IsIPUnique(netID, ip.String(), database.EXT_CLIENT_TABLE_NAME, true)) {
+			return errDuplicateExtClientIP
+		}
+	}
 	return nil
 }
 

+ 2 - 0
controllers/regex.go

@@ -10,6 +10,8 @@ var (
 	errInvalidExtClientID      = errors.New("node name must be alphanumderic and/or dashes and less that 15 chars")
 	errInvalidExtClientExtraIP = errors.New("client extra ip must be a valid cidr")
 	errInvalidExtClientDNS     = errors.New("client dns must be a valid ip address")
+	errInvalidExtClientIP      = errors.New("client address must be a valid ip address")
+	errDuplicateExtClientIP    = errors.New("client address must be unique ip address")
 	errDuplicateExtClientName  = errors.New("duplicate client name")
 )
 

+ 2 - 0
models/extclient.go

@@ -35,6 +35,8 @@ type CustomExtClient struct {
 	ClientID                   string              `json:"clientid,omitempty"`
 	PublicKey                  string              `json:"publickey,omitempty"`
 	DNS                        string              `json:"dns,omitempty"`
+	Address                    string              `json:"address"`
+	Address6                   string              `json:"address6"`
 	ExtraAllowedIPs            []string            `json:"extraallowedips,omitempty"`
 	Enabled                    bool                `json:"enabled,omitempty"`
 	DeniedACLs                 map[string]struct{} `json:"deniednodeacls" bson:"acls,omitempty"`