Skip to content

Commit 3e5d1e7

Browse files
adrianrioboclaude
andcommitted
fix(gitlab): scope dns_servers detection to [containers] section
The previous grep/sed searched the whole file, so a dns_servers key in any other section (e.g. [network]) would trigger a false-positive check and replace the wrong key. Switch to awk that tracks whether the current line is inside [containers] for both the existence check and the in-place replacement. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c0d0b15 commit 3e5d1e7

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

pkg/integrations/gitlab/snippet-linux.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,16 @@ if [ -n "$_dns_servers" ]; then
4646
printf '[containers]\ndns_servers = [%s]\n' "$_toml_list" \
4747
| sudo tee /etc/containers/containers.conf > /dev/null
4848
elif grep -q '^\[containers\]' /etc/containers/containers.conf; then
49-
if grep -q 'dns_servers' /etc/containers/containers.conf; then
50-
sudo sed -i "s|^dns_servers.*|dns_servers = [${_toml_list}]|" \
51-
/etc/containers/containers.conf
49+
# Scope the dns_servers check to the [containers] section only
50+
if awk '/^\[containers\]/{f=1;next} /^\[/{f=0} f && /^dns_servers/{found=1} END{exit !found}' \
51+
/etc/containers/containers.conf; then
52+
# Replace dns_servers only within [containers]
53+
awk -v "val=dns_servers = [${_toml_list}]" \
54+
'/^\[containers\]/{s=1} /^\[/ && !/^\[containers\]/{s=0}
55+
s && /^dns_servers/{$0=val} 1' \
56+
/etc/containers/containers.conf \
57+
| sudo tee /etc/containers/containers.conf.tmp > /dev/null \
58+
&& sudo mv /etc/containers/containers.conf.tmp /etc/containers/containers.conf
5259
else
5360
sudo sed -i "/^\[containers\]/a dns_servers = [${_toml_list}]" \
5461
/etc/containers/containers.conf

0 commit comments

Comments
 (0)