-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
242 lines (207 loc) · 8.5 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package main
import (
"strconv"
"strings"
. "github.com/dirtman/sitepkg"
)
// Very crude routine to add a slice of key=value pairs to a JSON "string".
// "value" is assumed to be a JSON string, unless it looks like a boolean
// or a number. "body" is assumed to be an existing JSON string to which
// each key/value pair is appended, which each pair preceeded with a ','.
func appendFieldsJSON(body string, fields []string) string {
for _, field := range fields {
f := strings.SplitN(field, "=", 2)
setting := f[0]
value := f[1]
body += `,"` + setting + `":`
_, err := strconv.Atoi(value)
if err == nil || value == "true" || value == "false" {
body += value
} else {
value = strings.ReplaceAll(value, `"`, `\"`)
body += `"` + value + `"`
}
}
return body
}
// checkConflict is called by some of the Update commands to check for already
// existing records that conflict with the updates being requested for a record.
// The supported record types for which to check are Host, A, AAAA, CNAME, and Alias.
// The f slice defines the query for all the record types being checked.
// The boolean flags determine which of the supported record types to check.
// tt specifies an Alias target_type and is only applicable to Alias checks.
// Return when the first conflict or error is encountered.
// Note that we do not bother unmarshalling the raw records, and an "empty" body
// should be 2 bytes: "[]".
func checkConflict(f []string, host, a, aaaa, cname, alias bool, tt string) (string, error) {
var conflict string
ShowDebug("checkConflict: fields: %#v", f)
if a {
ShowDebug(" checkConflict: checking A record")
if records, err := getRecords("record:a", "", "", "", "", f, nil); err != nil {
return conflict, Error("A record query failed: %v", err)
} else if !(records == nil || len(records) <= 2) {
return "Address record with same name or value already exists", nil
}
}
if host {
ShowDebug(" checkConflict: checking Host record")
if records, err := getRecords("record:host", "", "", "", "", f, nil); err != nil {
return conflict, Error("host query failed: %v", err)
} else {
ShowDebug(" checkConflict: Host record: %#v", records)
ShowDebug(" checkConflict: Host record: %s", records)
ShowDebug(" checkConflict: len(record): %d", len(records))
if !(records == nil || len(records) <= 2) {
return "Host with same name or value already exists", nil
}
ShowDebug(" checkConflict: Host: no return")
}
}
if cname {
ShowDebug(" checkConflict: checking CNAME record")
if records, err := getRecords("record:cname", "", "", "", "", f, nil); err != nil {
return conflict, Error("cname query failed: %v", err)
} else if !(records == nil || len(records) <= 2) {
return "CNAME record with same name or value already exists", nil
}
}
if alias {
ShowDebug(" checkConflict: checking Alias record")
if tt != "" {
f = append(f, "target_type="+tt)
}
if records, err := getRecords("record:alias", "", "", "", "", f, nil); err != nil {
return conflict, Error("alias query failed: %v", err)
} else if !(records == nil || len(records) <= 2) {
return "Alias record with same name or value already exists", nil
}
}
return conflict, nil
}
// Return the keys of a map (https://gosamples.dev/generics-map-keys/).
func keys[K comparable, V any](m map[K]V) []K {
keys := make([]K, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return keys
}
/*****************************************************************************\
sanitizeRecordData is used to sanitize the record data provided by the user,
generally for TXT record requests, in 2 ways:
1) special URL chars in the data string, such as "&", need to be escaped.
2) strings > 255 chars need to be split into multiple sub-strings.
The max size of a string in a TXT record is 255 chars, but a record can have
multiple strings (the client joins the strings back into one). In the
Infloblox GUI, this could be done by splitting a long string into sub-strings
with doule quotes: "bigString1" "bigString2". Here I do the same.
I noticed via trial and error that if I split a string with `""` like this:
"thisistoolong" -> "this""isto""olon""g",
an add works, and the resultant record is valid. But I cannot get the txt
record with the data ("thisistoolong"). I then noticed that Infoblox shows
the data as "this" "isto" "olon" "g". So I tried spitting with `" "`, but
then the data gets quietly truncated at the first space (so Infoblox has
"this". I then somehow stumbled upon using `"+"`, and this seems to work
best. Go figure. A `+` seems to get converted to a space.
11/26/2024: I realized I could not get > 255 TXT records: ibapi txt get
did not find them. I realized that whether via via the UI or ibapi, IB
seems to un-quote the second (perhaps last?) string. For instance, if I
enter this in the UI text box: "t1" "t2":
1) if both t1/t2 are small, IB de-quotes both: t1 t2
2) if at least t1 is large, IB de-quotes t2 only: "t1" t2
dig gives same result for either case: "t1" "t2"
\*****************************************************************************/
func sanitizeRecordData(dataString string) string {
if dataString == "" {
return dataString
}
var splitString, splitter string
var currentLen, currentStart int
// If the TXT data contains spaces, it must be quoted; else Infoblox will
// convert it into individual quoted words, and the spaces will be lost.
// However, I found by chance that for GETs, if the data does not have spaces,
// it must NOT be quoted!! These GETs work:
// "/record:txt?name=rb3.rice.edu&text=TextRecord&_return_fields%2b=disable"
// "/record:txt?name=rb3.rice.edu&text=\"Text%20record\"&_return_fields%2b=disable"
// These do not:
// "/record:txt?name=rb3.rice.edu&text=\"TextRecord\"&_return_fields%2b=disable"
// "/record:txt?name=rb3.rice.edu&text=Text%20record&_return_fields%2b=disable"
// It may depend on if the data is in the URL or the body of the request.
if strings.Contains(dataString, " ") {
// Do not add a double quote if it starts with one:
if dataString[0] != 34 { // 34 == `"`
dataString = `"` + dataString + `"`
ShowDebug("Added double quotes to TXT data: \"%v\"", dataString)
}
}
if len(dataString) < maxDataStringSize {
return escapeURLText(dataString)
}
for i := range dataString {
if currentLen == maxDataStringSize {
splitString += splitter + escapeURLText(dataString[currentStart:i])
splitter = `"+"`
currentLen = 0
currentStart = i
ShowDebug("sanitizeRecordData: splitString: %s", splitString)
}
currentLen++
}
// We may have some remaining string (< maxDataStringSize) to append.
if len(dataString[currentStart:]) > 0 {
// Corner case: only the trailing double quote is left. Let's not
// append two additional double quotes in this case.
if dataString[currentStart:] == `"` {
splitString += `"`
} else {
splitString += splitter + escapeURLText(dataString[currentStart:])
}
}
ShowDebug("sanitizeRecordData: splitString: %s", splitString)
if Debug && splitString != dataString {
ShowDebug("sanitizeRecordData: %s", dataString)
ShowDebug(" %s", splitString)
}
return splitString
}
// joinDataStrings joins multiple strings into one long string. This is the reverse
// of sanitizeRecordData, except joinDataStrings does not need to "un-escape" anything.
func joinDataStrings(dataString string) string {
var joinedString string
var subStrings []string
quote := "%22"
splitter := `"+"`
ShowDebug("dataString[0:3]: %s", dataString[0:3])
ShowDebug("dataString[len(dataString)-3:]: %s", dataString[len(dataString)-3:])
if dataString[0:3] == quote && dataString[len(dataString)-3:] == quote {
Print("**********************************")
subStrings = strings.Split(dataString[3:len(dataString)-3], splitter)
} else {
subStrings = strings.Split(dataString, splitter)
}
for _, subString := range subStrings {
joinedString += subString
}
if Debug && joinedString != dataString {
ShowDebug("joinDataStrings: %s", dataString)
ShowDebug(" Joined: %s", joinedString)
}
return joinedString
}
// escapeURLText escapes chars in a TXT record that cause issues in a URL. These
// seem to get converted back by the WAPI.
func escapeURLText(urlText string) string {
replacer := strings.NewReplacer(
`+`, "%2B",
`=`, "%3D",
`;`, "%3B",
` `, "%20",
)
escaped := replacer.Replace(urlText)
if Debug && urlText != escaped {
ShowDebug("escapeURLText: %s", urlText)
ShowDebug(" Escaped: %s", escaped)
}
return escaped
}