Skip to content

Commit

Permalink
fixes so the peerName is the right IP when removing the peer
Browse files Browse the repository at this point in the history
  • Loading branch information
travisbcotton committed Jan 23, 2025
1 parent 590d647 commit 7019900
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions cmd/cloud-init-server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cloud-init-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down

0 comments on commit 7019900

Please # to comment.