Skip to content

Commit

Permalink
handlers: handle MAC-less requests via IP-based lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
LRitzdorf committed Jul 19, 2024
1 parent ff87935 commit 078bf6a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cmd/cloud-init-server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"log"
"net/http"
"strings"

"github.com/OpenCHAMI/cloud-init/internal/memstore"
"github.com/OpenCHAMI/cloud-init/internal/smdclient"
Expand Down Expand Up @@ -121,6 +122,30 @@ func (h CiHandler) GetDataByMAC(dataKind ciDataKind) func(w http.ResponseWriter,
}
}

func (h CiHandler) GetDataByIP(dataKind ciDataKind) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Strip port number from RemoteAddr to obtain raw IP
portIndex := strings.LastIndex(r.RemoteAddr, ":")
var ip string
if portIndex > 0 {
ip = r.RemoteAddr[:portIndex]
} else {
ip = r.RemoteAddr
}
// Retrieve the node's xname based on IP address
name, err := h.sm.IDfromIP(ip)
if err != nil {
log.Print(err)
w.WriteHeader(http.StatusUnprocessableEntity)
return
} else {
log.Printf("xname %s with ip %s found\n", name, ip)
}
// Actually respond with the data
h.getData(name, dataKind, w)
}
}

func (h CiHandler) getData(id string, dataKind ciDataKind, w http.ResponseWriter) {
ci, err := h.store.Get(id, h.sm)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/cloud-init-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func initCiRouter(router chi.Router, handler *CiHandler) {
// Add cloud-init endpoints to router
router.Get("/", handler.ListEntries)
router.Post("/", handler.AddEntry)
router.Get("/user-data", handler.GetDataByIP(UserData))
router.Get("/meta-data", handler.GetDataByIP(MetaData))
router.Get("/vendor-data", handler.GetDataByIP(VendorData))
router.Get("/{id}", handler.GetEntry)
router.Get("/{id}/user-data", handler.GetDataByMAC(UserData))
router.Get("/{id}/meta-data", handler.GetDataByMAC(MetaData))
Expand Down

0 comments on commit 078bf6a

Please sign in to comment.