Skip to content

Commit

Permalink
DXE-3621 [TFP][#370] Implement support for distinct values of priorit…
Browse files Browse the repository at this point in the history
…y, weight and port for SRV records with multiple targets
  • Loading branch information
mgwoj committed Apr 5, 2024
1 parent 983ccdd commit 779623c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@



* DNS
* Modified `ParseRData` method to remove priority, weight and port from targets **only** when those values are same for all `SRV` targets.
Otherwise, targets are returned untouched and `priority`, `weight` and `port` in the map are not populated.



Expand Down Expand Up @@ -78,7 +81,6 @@






## 8.0.0 (March 19, 2024)
Expand Down
40 changes: 32 additions & 8 deletions pkg/dns/record_lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,15 +387,39 @@ func resolveRRSIGType(rData []string, fieldMap map[string]interface{}) {
}

func resolveSRVType(rData, newRData []string, fieldMap map[string]interface{}) {
// pull out some fields
parts := strings.Split(rData[0], " ")
fieldMap["priority"], _ = strconv.Atoi(parts[0])
fieldMap["weight"], _ = strconv.Atoi(parts[1])
fieldMap["port"], _ = strconv.Atoi(parts[2])
// populate target
// if all targets have the same priority, weight and port, process it in the old way
priorityMap := make(map[int]struct{})
weightMap := make(map[int]struct{})
portMap := make(map[int]struct{})
for _, rContent := range rData {
parts = strings.Split(rContent, " ")
newRData = append(newRData, parts[3])
parts := strings.Split(rContent, " ")
priority, _ := strconv.Atoi(parts[0])
weight, _ := strconv.Atoi(parts[1])
port, _ := strconv.Atoi(parts[2])
priorityMap[priority] = struct{}{}
weightMap[weight] = struct{}{}
portMap[port] = struct{}{}
}
// all values are the same, so process in the old way
if len(priorityMap) == 1 && len(weightMap) == 1 && len(portMap) == 1 {
// pull out some fields
parts := strings.Split(rData[0], " ")
fieldMap["priority"], _ = strconv.Atoi(parts[0])
fieldMap["weight"], _ = strconv.Atoi(parts[1])
fieldMap["port"], _ = strconv.Atoi(parts[2])
// populate target
for _, rContent := range rData {
parts = strings.Split(rContent, " ")
newRData = append(newRData, parts[3])
}
} else {
delete(fieldMap, "priority")
delete(fieldMap, "weight")
delete(fieldMap, "port")
// populate target
for _, rContent := range rData {
newRData = append(newRData, rContent)
}
}
fieldMap["target"] = newRData
}
Expand Down
23 changes: 22 additions & 1 deletion pkg/dns/record_lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,20 @@ func TestDNS_ParseRData(t *testing.T) {
client := Client(session.Must(session.New()))

tests := map[string]struct {
rType string
rdata []string
expect map[string]interface{}
}{
"AFSDB": {
rType: "AFSDB",
rdata: []string{"1 bar.com"},
expect: map[string]interface{}{
"subtype": 1,
"target": []string{"bar.com"},
},
},
"SVCB": {
rType: "SVCB",
rdata: []string{"0 svc4.example.com."},
expect: map[string]interface{}{
"target": []string{},
Expand All @@ -337,6 +340,7 @@ func TestDNS_ParseRData(t *testing.T) {
},
},
"HTTPS": {
rType: "HTTPS",
rdata: []string{"3 https.example.com. alpn=bar port=8080"},
expect: map[string]interface{}{
"target": []string{},
Expand All @@ -345,11 +349,28 @@ func TestDNS_ParseRData(t *testing.T) {
"svc_params": "alpn=bar port=8080",
},
},
"SRV with default values": {
rType: "SRV",
rdata: []string{"10 60 5060 big.example.com.", "10 60 5060 small.example.com."},
expect: map[string]interface{}{
"port": 5060,
"priority": 10,
"weight": 60,
"target": []string{"big.example.com.", "small.example.com."},
},
},
"SRV without default values": {
rType: "SRV",
rdata: []string{"10 60 5060 big.example.com.", "20 50 5060 small.example.com."},
expect: map[string]interface{}{
"target": []string{"10 60 5060 big.example.com.", "20 50 5060 small.example.com."},
},
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
out := client.ParseRData(context.Background(), name, test.rdata)
out := client.ParseRData(context.Background(), test.rType, test.rdata)

assert.Equal(t, test.expect, out)
})
Expand Down

0 comments on commit 779623c

Please sign in to comment.