From 12a6fddbd8c5be4a921be2b70c96296a5b5bcc9f Mon Sep 17 00:00:00 2001 From: Pyrrvs Date: Mon, 14 Dec 2020 19:15:34 +0100 Subject: [PATCH] handle apple dhcp lease with missing 'name' and 'lease' informations --- builder/vmware/common/driver_parser.go | 14 +++---- builder/vmware/common/driver_parser_test.go | 42 +++++++++++++++++++ .../testdata/apple-dhcpd-example.leases | 7 ++++ 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/builder/vmware/common/driver_parser.go b/builder/vmware/common/driver_parser.go index 7ea077d18..825fa7bd5 100644 --- a/builder/vmware/common/driver_parser.go +++ b/builder/vmware/common/driver_parser.go @@ -2452,7 +2452,7 @@ type appleDhcpLeaseEntry struct { func readAppleDhcpdLeaseEntry(in chan byte) (entry *appleDhcpLeaseEntry, err error) { entry = &appleDhcpLeaseEntry{extra: map[string]string{}} - validFieldCount := 0 + mandatoryFieldCount := 0 // Read up to the lease item and validate that it actually matches _, ch := consumeOpenClosePair('{', '}', in) for insideBraces := true; insideBraces; { @@ -2486,7 +2486,7 @@ func readAppleDhcpdLeaseEntry(in chan byte) (entry *appleDhcpLeaseEntry, err err switch key { case "ip_address": entry.ipAddress = val - validFieldCount++ + mandatoryFieldCount++ case "identifier": fallthrough case "hw_address": @@ -2514,24 +2514,22 @@ func readAppleDhcpdLeaseEntry(in chan byte) (entry *appleDhcpLeaseEntry, err err } else { entry.hwAddress = decodedLease } - validFieldCount++ + mandatoryFieldCount++ case "lease": entry.lease = val - validFieldCount++ case "name": entry.name = val - validFieldCount++ default: // Just stash it for now because we have no idea what it is. entry.extra[key] = val } } // we have most likely parsed the whole file - if validFieldCount == 0 { + if mandatoryFieldCount == 0 { return nil, nil } - // an entry is composed of 5 mandatory fields, we'll check that they all have been set during the parsing - if validFieldCount < 5 { + // an entry is composed of 3 mandatory fields, we'll check that they all have been set during the parsing + if mandatoryFieldCount < 3 { return entry, fmt.Errorf("Error entry `%v` is missing mandatory information", entry) } return entry, nil diff --git a/builder/vmware/common/driver_parser_test.go b/builder/vmware/common/driver_parser_test.go index c93dc90c4..dbd24fa18 100644 --- a/builder/vmware/common/driver_parser_test.go +++ b/builder/vmware/common/driver_parser_test.go @@ -913,6 +913,31 @@ func TestParserReadAppleDhcpdLeaseEntry(t *testing.T) { if result.extra["fake"] != expected_extra_1["fake"] { t.Errorf("expected extra %v, got %v", expected_extra_1["fake"], result.extra["fake"]) } + + test_2 := `{ + ip_address=192.168.111.4 + hw_address=1,0:c:56:3c:e7:23 + identifier=1,0:c:56:3c:e7:23 + }` + expected_2 := map[string]string{ + "ipAddress": "192.168.111.4", + "hwAddress": "000c563ce723", + "id": "000c563ce723", + } + + result, err = readAppleDhcpdLeaseEntry(consumeAppleLeaseString(test_2)) + if err != nil { + t.Errorf("error parsing entry: %v", err) + } + if result.ipAddress != expected_2["ipAddress"] { + t.Errorf("expected ipAddress %v, got %v", expected_2["ipAddress"], result.ipAddress) + } + if hex.EncodeToString(result.hwAddress) != expected_2["hwAddress"] { + t.Errorf("expected hwAddress %v, got %v", expected_2["hwAddress"], hex.EncodeToString(result.hwAddress)) + } + if hex.EncodeToString(result.id) != expected_2["id"] { + t.Errorf("expected id %v, got %v", expected_2["id"], hex.EncodeToString(result.id)) + } } func TestParserReadAppleDhcpdLeases(t *testing.T) { @@ -1023,6 +1048,23 @@ func TestParserReadAppleDhcpdLeases(t *testing.T) { t.Errorf("expected hardware address %s, got %s", test_4["hwAddress"], hex.EncodeToString(res.hwAddress)) } } + + test_5 := map[string]string{ + "ipAddress": "127.0.0.20", + "id": "0dead099aabc", + "hwAddress": "0dead099aabc", + } + test_5_findings := filter_ipAddr(test_5["ipAddress"], results) + if len(test_5_findings) != 1 { + t.Errorf("expected %d matching entries, got %d", 1, len(test_5_findings)) + } else { + res := find_id(test_5["id"], test_5_findings) + if res == nil { + t.Errorf("unable to find item with id %v", test_5["id"]) + } else if hex.EncodeToString(res.hwAddress) != test_5["hwAddress"] { + t.Errorf("expected hardware address %s, got %s", test_5["hwAddress"], hex.EncodeToString(res.hwAddress)) + } + } } func TestParserTokenizeNetworkingConfig(t *testing.T) { diff --git a/builder/vmware/common/testdata/apple-dhcpd-example.leases b/builder/vmware/common/testdata/apple-dhcpd-example.leases index b6ee35d6e..53aacb19c 100644 --- a/builder/vmware/common/testdata/apple-dhcpd-example.leases +++ b/builder/vmware/common/testdata/apple-dhcpd-example.leases @@ -30,4 +30,11 @@ identifier=1,d:ea:d0:99:aa:bb lease=0x5fd72edc name=vagrant-2019 +} + +# this entry does not have all fields +{ + ip_address=127.0.0.20 + hw_address=1,d:ea:d0:99:aa:bc + identifier=1,d:ea:d0:99:aa:bc } \ No newline at end of file