Skip to content

Commit 7d35185

Browse files
authored
Replace sort with awk
This will make sure that we do not add any duplicate entries, while keeping the ordering of the domain names the same as when they are encountered.
1 parent 33e422a commit 7d35185

File tree

2 files changed

+36
-31
lines changed

2 files changed

+36
-31
lines changed

src/scripts/util.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,12 @@ parse_config_file() {
229229
debug "Appending to already existing key '${cert_name}'"
230230
fi
231231
# Make sure we only add unique entries every time.
232-
certs["${cert_name}"]="$(echo ${certs["${cert_name}"]} "${server_names[@]}" | xargs -n1 echo | sort -u | tr '\n' ' ')"
232+
# This invocation of awk works like 'sort -u', but preserves order. This
233+
# set the first 'server_name' entry as the first '-d' domain artgument
234+
# for the certbot command. This domain will be your Common Name on the
235+
# certificate.
236+
# stackoverflow on this awk usage: https://stackoverflow.com/a/45808487
237+
certs["${cert_name}"]="$(echo ${certs["${cert_name}"]} "${server_names[@]}" | xargs -n1 echo | awk '!a[$0]++' | tr '\n' ' ')"
233238
done
234239
}
235240

tests/util.bats

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,9 @@ load "${SCRIPTS_DIR}/util.sh"
9090

9191
local server_names=(${certificates[my-cert]})
9292
[ ${#server_names[@]} -eq 3 ]
93-
[ "${server_names[0]}" == "another.example.org" ]
94-
[ "${server_names[1]}" == "example.org" ]
95-
[ "${server_names[2]}" == "www.example.org" ]
93+
[ "${server_names[0]}" == "example.org" ]
94+
[ "${server_names[1]}" == "www.example.org" ]
95+
[ "${server_names[2]}" == "another.example.org" ]
9696
}
9797

9898
@test "parse_config_file works for single server block, multiple certificate names, single server name" {
@@ -129,9 +129,9 @@ load "${SCRIPTS_DIR}/util.sh"
129129

130130
local server_names=(${certificates[my-cert]})
131131
[ ${#server_names[@]} -eq 3 ]
132-
[ "${server_names[0]}" == "another.example.org" ]
133-
[ "${server_names[1]}" == "example.org" ]
134-
[ "${server_names[2]}" == "www.example.org" ]
132+
[ "${server_names[0]}" == "example.org" ]
133+
[ "${server_names[1]}" == "www.example.org" ]
134+
[ "${server_names[2]}" == "another.example.org" ]
135135
}
136136

137137
@test "parse_config_file works for multiple server blocks, multiple certificate names, multiple server names" {
@@ -147,17 +147,17 @@ load "${SCRIPTS_DIR}/util.sh"
147147

148148
local server_names_cert1=(${certificates[my-cert1]})
149149
[ ${#server_names_cert1[@]} -eq 4 ]
150-
[ "${server_names_cert1[0]}" == "anew.example.org" ]
151-
[ "${server_names_cert1[1]}" == "another.example.org" ]
152-
[ "${server_names_cert1[2]}" == "example.org" ]
153-
[ "${server_names_cert1[3]}" == "www.example.org" ]
150+
[ "${server_names_cert1[0]}" == "example.org" ]
151+
[ "${server_names_cert1[1]}" == "www.example.org" ]
152+
[ "${server_names_cert1[2]}" == "another.example.org" ]
153+
[ "${server_names_cert1[3]}" == "anew.example.org" ]
154154

155155
local server_names_cert2=(${certificates[my-cert2]})
156156
[ ${#server_names_cert2[@]} -eq 4 ]
157-
[ "${server_names_cert1[0]}" == "anew.example.org" ]
158-
[ "${server_names_cert1[1]}" == "another.example.org" ]
159-
[ "${server_names_cert1[2]}" == "example.org" ]
160-
[ "${server_names_cert1[3]}" == "www.example.org" ]
157+
[ "${server_names_cert2[0]}" == "example.org" ]
158+
[ "${server_names_cert2[1]}" == "www.example.org" ]
159+
[ "${server_names_cert2[2]}" == "another.example.org" ]
160+
[ "${server_names_cert2[3]}" == "anew.example.org" ]
161161

162162
}
163163

@@ -189,8 +189,8 @@ load "${SCRIPTS_DIR}/util.sh"
189189
local server_names=(${certificates[my-cert]})
190190
[ ${#server_names[@]} -eq 3 ]
191191
[ "${server_names[0]}" == "*.example.org" ]
192-
[ "${server_names[1]}" == "*.sub.example.org" ]
193-
[ "${server_names[2]}" == "example.org" ]
192+
[ "${server_names[1]}" == "example.org" ]
193+
[ "${server_names[2]}" == "*.sub.example.org" ]
194194
}
195195

196196
@test "parse_config_file ignores regex names" {
@@ -206,11 +206,11 @@ load "${SCRIPTS_DIR}/util.sh"
206206
local server_names=(${certificates[my-cert]})
207207
echo "${certificates[@]}"
208208
[ ${#server_names[@]} -eq 5 ]
209-
[ "${server_names[0]}" == "192.168.0.1" ]
210-
[ "${server_names[1]}" == "1:2:3:4:5:6:7:8" ]
209+
[ "${server_names[0]}" == "example.org" ]
210+
[ "${server_names[1]}" == "www.example.org" ]
211211
[ "${server_names[2]}" == "_" ]
212-
[ "${server_names[3]}" == "example.org" ]
213-
[ "${server_names[4]}" == "www.example.org" ]
212+
[ "${server_names[3]}" == "192.168.0.1" ]
213+
[ "${server_names[4]}" == "1:2:3:4:5:6:7:8" ]
214214
}
215215

216216
@test "parse_config_file works over multiple files (with duplicates)" {
@@ -227,20 +227,20 @@ load "${SCRIPTS_DIR}/util.sh"
227227

228228
local server_names_cert1=(${certificates[my-cert1]})
229229
[ ${#server_names_cert1[@]} -eq 3 ]
230-
[ "${server_names_cert1[0]}" == "anew.example.org" ]
231-
[ "${server_names_cert1[1]}" == "example.org" ]
232-
[ "${server_names_cert1[2]}" == "www.example.org" ]
230+
[ "${server_names_cert1[0]}" == "example.org" ]
231+
[ "${server_names_cert1[1]}" == "www.example.org" ]
232+
[ "${server_names_cert1[2]}" == "anew.example.org" ]
233233

234234
local server_names_cert2=(${certificates[my-cert2]})
235235
[ ${#server_names_cert2[@]} -eq 3 ]
236-
[ "${server_names_cert2[0]}" == "*.example.com" ]
237-
[ "${server_names_cert2[1]}" == "anew.example.org" ]
238-
[ "${server_names_cert2[2]}" == "example.com" ]
236+
[ "${server_names_cert2[0]}" == "anew.example.org" ]
237+
[ "${server_names_cert2[1]}" == "example.com" ]
238+
[ "${server_names_cert2[2]}" == "*.example.com" ]
239239

240240
local server_names_cert3=(${certificates[my-cert3]})
241241
[ ${#server_names_cert3[@]} -eq 4 ]
242-
[ "${server_names_cert3[0]}" == "*.example.net" ]
243-
[ "${server_names_cert3[1]}" == "example.net" ]
244-
[ "${server_names_cert3[2]}" == "new.example.net" ]
245-
[ "${server_names_cert3[3]}" == "www.example.net" ]
242+
[ "${server_names_cert3[0]}" == "example.net" ]
243+
[ "${server_names_cert3[1]}" == "*.example.net" ]
244+
[ "${server_names_cert3[2]}" == "www.example.net" ]
245+
[ "${server_names_cert3[3]}" == "new.example.net" ]
246246
}

0 commit comments

Comments
 (0)