Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion cns/imds/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net"
"net/http"
"net/url"
"strings"

"github.com/avast/retry-go/v4"
"github.com/pkg/errors"
Expand Down Expand Up @@ -218,14 +219,36 @@ func (h *HardwareAddr) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &s); err != nil {
return errors.Wrap(err, "failed to unmarshal JSON data")
}
mac, err := net.ParseMAC(s)

mac, err := parseMacAddress(s)
if err != nil {
return errors.Wrap(err, "failed to parse MAC address")
}
*h = HardwareAddr(mac)
return nil
}

// parseMacAddress is a wrapper around net.ParseMAC to handle Windows MAC address. Windows MAC addresse is a pure hex
// dump without delimiter, so we need to add delimiters. This happens when CNS gets MAC address from IMDS.
func parseMacAddress(s string) (net.HardwareAddr, error) {
if !strings.ContainsAny(s, ":-.") && len(s)%2 == 0 {
var sb strings.Builder
for i := 0; i < len(s); i += 2 {
if i > 0 {
sb.WriteByte(':')
}
sb.WriteString(s[i : i+2])
}
s = sb.String()
}

mac, err := net.ParseMAC(s)
if err != nil {
return nil, errors.Wrap(err, "failed to parse MAC address")
}
return mac, nil
}

func (h *HardwareAddr) String() string {
return net.HardwareAddr(*h).String()
}
Expand Down
2 changes: 1 addition & 1 deletion cns/imds/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func TestGetNetworkInterfaces(t *testing.T) {
},
{
"interfaceCompartmentID": "",
"macAddress": "00:00:5e:00:53:02"
"macAddress": "00005e005302"
}
]
}`)
Expand Down
Loading