diff --git a/cmd/cloud-init-server/instance_data.go b/cmd/cloud-init-server/instance_data.go index b85232d..4b7c2f5 100644 --- a/cmd/cloud-init-server/instance_data.go +++ b/cmd/cloud-init-server/instance_data.go @@ -43,16 +43,29 @@ instance-data: func InstanceDataHandler(smd smdclient.SMDClientInterface, clusterName string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - // id should be the ip address that the request originated from. Check the headers to see if the request has been forwarded and the remote IP is preserved + // ip should be the ip address that the request originated from. Check the headers to see if the request has been forwarded and the remote IP is preserved // Check for the first ip in the X-Forwarded-For header if it exists - var id string + var ip string if r.Header.Get("X-Forwarded-For") != "" { // If it exists, use that - id = strings.Split(r.Header.Get("X-Forwarded-For"), ",")[0] + ip = strings.Split(r.Header.Get("X-Forwarded-For"), ",")[0] } else { - id = r.RemoteAddr + portIndex := strings.LastIndex(r.RemoteAddr, ":") + if portIndex > 0 { + ip = r.RemoteAddr[:portIndex] + } else { + ip = r.RemoteAddr + } } // Get the component information from the SMD client + id, err := smd.IDfromIP(ip) + if err != nil { + fmt.Print(err) + w.WriteHeader(http.StatusUnprocessableEntity) + return + } else { + fmt.Printf("xname %s with ip %s found\n", id, ip) + } smdComponent, err := smd.ComponentInformation(id) if err != nil { // If the component information is not available, return a 404 diff --git a/internal/smdclient/SMDclient.go b/internal/smdclient/SMDclient.go index 75cc303..bb04d8b 100644 --- a/internal/smdclient/SMDclient.go +++ b/internal/smdclient/SMDclient.go @@ -143,8 +143,14 @@ func (s *SMDClient) getSMD(ep string, smd interface{}) error { } } defer resp.Body.Close() - body, _ := io.ReadAll(resp.Body) + body, err := io.ReadAll(resp.Body) + log.Info().Msgf("body: %s", string(body)) + if err != nil { + log.Error().Err(err).Msg("failed to read response body") + return err + } if err := json.Unmarshal(body, smd); err != nil { + log.Error().Err(err).Str("body", string(body)).Msg("failed to unmarshal SMD response") return ErrUnmarshal } return nil @@ -154,7 +160,7 @@ func (s *SMDClient) getSMD(ep string, smd interface{}) error { // with the corresponding node information, including MAC addresses, IP addresses, and descriptions. func (s *SMDClient) PopulateNodes() { var ethIfaceArray []sm.CompEthInterfaceV2 - ep := "/hsm/v2/Inventory/EthernetInterfaces/" + ep := "/Inventory/EthernetInterfaces/" if err := s.getSMD(ep, ðIfaceArray); err != nil { log.Error().Err(err).Msg("Failed to get SMD data") return @@ -263,7 +269,7 @@ func (s *SMDClient) GroupMembership(id string) ([]string, error) { func (s *SMDClient) ComponentInformation(id string) (base.Component, error) { var node base.Component - ep := "/hsm/v2/State/Components/" + id + ep := "/State/Components/" + id err := s.getSMD(ep, &node) if err != nil { return node, err