From 701990047bd3ef81c7f856c6263fe63f5320fa1e Mon Sep 17 00:00:00 2001 From: Travis Cotton Date: Wed, 22 Jan 2025 18:43:58 -0700 Subject: [PATCH] fixes so the peerName is the right IP when removing the peer --- cmd/cloud-init-server/handlers.go | 14 +++++++++++--- cmd/cloud-init-server/main.go | 2 +- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/cmd/cloud-init-server/handlers.go b/cmd/cloud-init-server/handlers.go index 60fcc0c..4bbdd99 100644 --- a/cmd/cloud-init-server/handlers.go +++ b/cmd/cloud-init-server/handlers.go @@ -130,7 +130,7 @@ func InstanceInfoHandler(sm smdclient.SMDClientInterface, store cistore.Store) h } // Phone home should be a POST request x-www-form-urlencoded like this: pub_key_rsa=rsa_contents&pub_key_ecdsa=ecdsa_contents&pub_key_ed25519=ed25519_contents&instance_id=i-87018aed&hostname=myhost&fqdn=myhost.internal -func PhoneHomeHandler(store cistore.Store, wg *wgtunnel.InterfaceManager) http.HandlerFunc { +func PhoneHomeHandler(wg *wgtunnel.InterfaceManager, sm smdclient.SMDClientInterface) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { w.WriteHeader(http.StatusMethodNotAllowed) @@ -140,7 +140,15 @@ func PhoneHomeHandler(store cistore.Store, wg *wgtunnel.InterfaceManager) http.H log.Info().Msgf("Phone home request from %s", ip) // TODO: validate the request IP against the SMD client and reject if needed - err := r.ParseForm() + id, err := sm.IDfromIP(ip) + if err != nil { + log.Error().Msgf("Error getting ID from IP: %v", err) + } + peerName, err := sm.IPfromID(id) + if err != nil { + log.Error().Msgf("Error getting IP from ID: %v", err) + } + err = r.ParseForm() if err != nil { log.Error().Msgf("Error parsing form data: %v", err) w.WriteHeader(http.StatusBadRequest) @@ -166,7 +174,7 @@ func PhoneHomeHandler(store cistore.Store, wg *wgtunnel.InterfaceManager) http.H if wg != nil { go func() { - wg.RemovePeer(ip) + wg.RemovePeer(peerName) }() w.WriteHeader(http.StatusOK) diff --git a/cmd/cloud-init-server/main.go b/cmd/cloud-init-server/main.go index c734e0d..6e9f0a5 100644 --- a/cmd/cloud-init-server/main.go +++ b/cmd/cloud-init-server/main.go @@ -191,7 +191,7 @@ func initCiClientRouter(router chi.Router, handler *CiHandler, wgInterfaceManage router.With(wireGuardMiddleware).Get("/meta-data", MetaDataHandler(handler.sm, handler.store)) router.With(wireGuardMiddleware).Get("/vendor-data", VendorDataHandler(handler.sm, handler.store)) router.With(wireGuardMiddleware).Get("/{group}.yaml", GroupUserDataHandler(handler.sm, handler.store)) - router.Post("/phone-home/{id}", PhoneHomeHandler(handler.store, wgInterfaceManager)) + router.Post("/phone-home/{id}", PhoneHomeHandler(wgInterfaceManager, handler.sm)) router.Post("/wg-init", wgtunnel.AddClientHandler(wgInterfaceManager, handler.sm)) }