From 39fbb45cfe64f7eea1688683f6b8d63192faaf5e Mon Sep 17 00:00:00 2001 From: Aceix Date: Thu, 8 Feb 2024 17:59:43 +0000 Subject: [PATCH] feat(NET-817): add postup/down scripts for clients (#2810) --- controllers/ext_client.go | 27 ++++++++++++++++++++++++--- logic/extpeers.go | 4 ++++ models/extclient.go | 4 ++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/controllers/ext_client.go b/controllers/ext_client.go index 89a3501c..89c6adba 100644 --- a/controllers/ext_client.go +++ b/controllers/ext_client.go @@ -7,7 +7,9 @@ import ( "net" "net/http" "strconv" + "strings" + "github.com/go-playground/validator/v10" "github.com/gorilla/mux" "github.com/gravitl/netmaker/database" "github.com/gravitl/netmaker/logger" @@ -250,11 +252,24 @@ func getExtClientConf(w http.ResponseWriter, r *http.Request) { if host.MTU != 0 { defaultMTU = host.MTU } + + postUp := strings.Builder{} + for _, loc := range strings.Split(client.PostUp, "\n") { + postUp.WriteString(fmt.Sprintf("PostUp = %s\n", loc)) + } + + postDown := strings.Builder{} + for _, loc := range strings.Split(client.PostDown, "\n") { + postDown.WriteString(fmt.Sprintf("PostDown = %s\n", loc)) + } + config := fmt.Sprintf(`[Interface] Address = %s PrivateKey = %s MTU = %d %s +%s +%s [Peer] PublicKey = %s @@ -266,10 +281,13 @@ Endpoint = %s client.PrivateKey, defaultMTU, defaultDNS, + postUp.String(), + postDown.String(), host.PublicKey, newAllowedIPs, gwendpoint, - keepalive) + keepalive, + ) if params["type"] == "qr" { bytes, err := qrcode.Encode(config, qrcode.Medium, 220) @@ -330,7 +348,6 @@ func createExtClient(w http.ResponseWriter, r *http.Request) { return } var customExtClient models.CustomExtClient - if err := json.NewDecoder(r.Body).Decode(&customExtClient); err != nil { logic.ReturnErrorResponse(w, r, logic.FormatError(err, "badrequest")) return @@ -499,7 +516,6 @@ func updateExtClient(w http.ResponseWriter, r *http.Request) { } newclient := logic.UpdateExtClient(&oldExtClient, &update) if err := logic.DeleteExtClient(oldExtClient.Network, oldExtClient.ClientID); err != nil { - slog.Error("failed to delete ext client", "user", r.Header.Get("user"), "id", oldExtClient.ClientID, "network", oldExtClient.Network, "error", err) logic.ReturnErrorResponse(w, r, logic.FormatError(err, "internal")) return @@ -609,6 +625,11 @@ func deleteExtClient(w http.ResponseWriter, r *http.Request) { // validateCustomExtClient Validates the extclient object func validateCustomExtClient(customExtClient *models.CustomExtClient, checkID bool) error { + v := validator.New() + err := v.Struct(customExtClient) + if err != nil { + return err + } //validate clientid if customExtClient.ClientID != "" { if err := isValid(customExtClient.ClientID, checkID); err != nil { diff --git a/logic/extpeers.go b/logic/extpeers.go index fdd967d3..caa031b1 100644 --- a/logic/extpeers.go +++ b/logic/extpeers.go @@ -5,6 +5,7 @@ import ( "fmt" "net" "reflect" + "strings" "sync" "time" @@ -276,6 +277,9 @@ func UpdateExtClient(old *models.ExtClient, update *models.CustomExtClient) mode if update.DeniedACLs != nil && !reflect.DeepEqual(old.DeniedACLs, update.DeniedACLs) { new.DeniedACLs = update.DeniedACLs } + // replace any \r\n with \n in postup and postdown from HTTP request + new.PostUp = strings.Replace(update.PostUp, "\r\n", "\n", -1) + new.PostDown = strings.Replace(update.PostDown, "\r\n", "\n", -1) return new } diff --git a/models/extclient.go b/models/extclient.go index 56977563..9d67207d 100644 --- a/models/extclient.go +++ b/models/extclient.go @@ -18,6 +18,8 @@ type ExtClient struct { OwnerID string `json:"ownerid" bson:"ownerid"` DeniedACLs map[string]struct{} `json:"deniednodeacls" bson:"acls,omitempty"` RemoteAccessClientID string `json:"remote_access_client_id"` // unique ID (MAC address) of RAC machine + PostUp string `json:"postup" bson:"postup"` + PostDown string `json:"postdown" bson:"postdown"` } // CustomExtClient - struct for CustomExtClient params @@ -29,4 +31,6 @@ type CustomExtClient struct { Enabled bool `json:"enabled,omitempty"` DeniedACLs map[string]struct{} `json:"deniednodeacls" bson:"acls,omitempty"` RemoteAccessClientID string `json:"remote_access_client_id"` // unique ID (MAC address) of RAC machine + PostUp string `json:"postup" bson:"postup" validate:"max=1024"` + PostDown string `json:"postdown" bson:"postdown" validate:"max=1024"` }