Skip to content

Commit

Permalink
feat(NET-817): add postup/down scripts for clients (#2810)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aceix authored Feb 8, 2024
1 parent cba55e6 commit 39fbb45
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
27 changes: 24 additions & 3 deletions controllers/ext_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions logic/extpeers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net"
"reflect"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 4 additions & 0 deletions models/extclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"`
}

0 comments on commit 39fbb45

Please sign in to comment.