File tree Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Expand file tree Collapse file tree 2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change 99 "net"
1010 "net/http"
1111 "net/url"
12+ "strings"
1213
1314 "github.com/avast/retry-go/v4"
1415 "github.com/pkg/errors"
@@ -218,14 +219,36 @@ func (h *HardwareAddr) UnmarshalJSON(data []byte) error {
218219 if err := json .Unmarshal (data , & s ); err != nil {
219220 return errors .Wrap (err , "failed to unmarshal JSON data" )
220221 }
221- mac , err := net .ParseMAC (s )
222+
223+ mac , err := parseMacAddress (s )
222224 if err != nil {
223225 return errors .Wrap (err , "failed to parse MAC address" )
224226 }
225227 * h = HardwareAddr (mac )
226228 return nil
227229}
228230
231+ // parseMacAddress is a wrapper around net.ParseMAC to handle Windows MAC address. Windows MAC addresse is a pure hex
232+ // dump without delimiter, so we need to add delimiters. This happens when CNS gets MAC address from IMDS.
233+ func parseMacAddress (s string ) (net.HardwareAddr , error ) {
234+ if ! strings .ContainsAny (s , ":-." ) && len (s )% 2 == 0 {
235+ var sb strings.Builder
236+ for i := 0 ; i < len (s ); i += 2 {
237+ if i > 0 {
238+ sb .WriteByte (':' )
239+ }
240+ sb .WriteString (s [i : i + 2 ])
241+ }
242+ s = sb .String ()
243+ }
244+
245+ mac , err := net .ParseMAC (s )
246+ if err != nil {
247+ return nil , errors .Wrap (err , "failed to parse MAC address" )
248+ }
249+ return mac , nil
250+ }
251+
229252func (h * HardwareAddr ) String () string {
230253 return net .HardwareAddr (* h ).String ()
231254}
Original file line number Diff line number Diff line change @@ -111,7 +111,7 @@ func TestGetNetworkInterfaces(t *testing.T) {
111111 },
112112 {
113113 "interfaceCompartmentID": "",
114- "macAddress": "00:00:5e:00:53:02 "
114+ "macAddress": "00005e005302 "
115115 }
116116 ]
117117 }` )
You can’t perform that action at this time.
0 commit comments