Skip to content

Commit 8a8f68f

Browse files
authored
chapi: generate unique uuid for 2 hosts with same mac address (#150)
* chapi: generate unique uuid for 2 hosts with same mac address Problem: As seen in issue hpe-storage/csi-driver#270 Two hosts have same mac addresses and although their hostnames differ, we generate the same uuid as the mechanism to generate uuid does not guarantee uniqueness. Update the mechanism to pass it through md5 has to guarantee uniqueness for 32 characters Signed-off-by: Raunak <[email protected]> * fix typos and add expectSame test case Signed-off-by: Raunak <[email protected]>
1 parent 4119420 commit 8a8f68f

File tree

3 files changed

+66
-3
lines changed

3 files changed

+66
-3
lines changed

chapi/chapidriver_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package chapi
44

55
import (
6-
"encoding/hex"
76
"errors"
87
"fmt"
98
"os"
@@ -73,8 +72,9 @@ func generateUniqueNodeId() (string, error) {
7372
// sort mac addresses as best attempt to get same address even if node.gob is deleted
7473
sort.Strings(macs)
7574

76-
// create a unique id using mac addess and host name
77-
idStr := strings.Replace(macs[0], ":", "", -1) + hex.EncodeToString([]byte(hostName))
75+
// create a unique id using mac address and host name using Md5 hash
76+
// https://github.com/hpe-storage/csi-driver/issues/270
77+
idStr := util.GetMD5HashOfTwoStrings(strings.Replace(macs[0], ":", "", -1), hostName)
7878
if len(idStr) < 32 {
7979
// pad with zeroes for minimum length
8080
idStr += strings.Repeat("0", 32-len(idStr))

util/strings.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ package util
44

55
import (
66
"bytes"
7+
"crypto/md5"
8+
"encoding/hex"
79
"fmt"
10+
"io"
811
"regexp"
912
"strings"
1013
)
@@ -46,3 +49,9 @@ func ConvertArrayOfIntToString(lun_ids []int32) string {
4649
}
4750
return buffer.String()
4851
}
52+
53+
func GetMD5HashOfTwoStrings(string1, string2 string) string {
54+
h := md5.New()
55+
io.WriteString(h, string1+string2)
56+
return hex.EncodeToString(h.Sum(nil))
57+
}

util/strings_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// (c) Copyright 2021 Hewlett Packard Enterprise Development LP
2+
3+
package util
4+
5+
import "testing"
6+
7+
func TestGetMacAddressHostNameMD5Hash(t *testing.T) {
8+
testCases := []struct {
9+
name string
10+
macAddress1 string
11+
hostName1 string
12+
macAddress2 string
13+
hostName2 string
14+
expectSame bool
15+
}{
16+
{
17+
name: "same mac address but different hostname",
18+
macAddress1: "0a58a9fe0001",
19+
macAddress2: "0a58a9fe0001",
20+
hostName1: "host-dev",
21+
hostName2: "host-prod",
22+
expectSame: false,
23+
},
24+
{
25+
name: "same hostname but different mac address",
26+
macAddress1: "0a58a9fe0001",
27+
macAddress2: "0a58a9fe0002",
28+
hostName1: "host-dev",
29+
hostName2: "host-dev",
30+
expectSame: false,
31+
},
32+
{
33+
name: "same hostname and same mac address",
34+
macAddress1: "0a58a9fe0001",
35+
macAddress2: "0a58a9fe0001",
36+
hostName1: "host-dev",
37+
hostName2: "host-dev",
38+
expectSame: true,
39+
},
40+
}
41+
42+
for _, tc := range testCases {
43+
t.Run(tc.name, func(t *testing.T) {
44+
idStr1 := GetMD5HashOfTwoStrings(tc.macAddress1, tc.hostName1)
45+
idStr2 := GetMD5HashOfTwoStrings(tc.macAddress2, tc.hostName2)
46+
if idStr1 == idStr2 && !tc.expectSame {
47+
t.Fatalf("Expected %s to be different from %s for test %s", idStr1, idStr2, tc.name)
48+
}
49+
if idStr1 != idStr2 && tc.expectSame {
50+
t.Fatalf("Expected %s to be same as %s for test %s", idStr1, idStr2, tc.name)
51+
}
52+
})
53+
}
54+
}

0 commit comments

Comments
 (0)