Skip to content

Commit c24d121

Browse files
committed
fix: update wrappers server options post-upgrade
Wrappers were previously using `vault.secrets.key_id`, which will no longer work with new Vault; we migrate it to use `vault.secrets.id` instead.
1 parent 61ff753 commit c24d121

File tree

1 file changed

+37
-0
lines changed
  • ansible/files/admin_api_scripts/pg_upgrade_scripts

1 file changed

+37
-0
lines changed

ansible/files/admin_api_scripts/pg_upgrade_scripts/complete.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,43 @@ EOF
152152
run_sql -c "update pg_extension set extowner = 'postgres'::regrole where extname = 'pgmq';"
153153
fi
154154

155+
# If upgrading to pgsodium-less Vault, Wrappers need to be updated so that
156+
# foreign servers use `vault.secrets.id` instead of `vault.secrets.key_id`
157+
UPDATE_WRAPPERS_SERVER_OPTIONS_QUERY=$(cat <<EOF
158+
DO \$\$
159+
DECLARE
160+
server_rec RECORD;
161+
option_rec RECORD;
162+
vault_secrets RECORD;
163+
BEGIN
164+
IF EXISTS (SELECT FROM pg_available_extension_versions WHERE name = 'wrappers' AND version = '0.4.6')
165+
AND EXISTS (SELECT FROM pg_extension WHERE extname = 'wrappers')
166+
THEN
167+
FOR server_rec IN
168+
SELECT srvname, srvoptions
169+
FROM pg_foreign_server
170+
LOOP
171+
FOR option_rec IN
172+
SELECT split_part(srvoption, '=', 1) AS option_name, split_part(srvoption, '=', 2) AS option_value
173+
FROM UNNEST(server_rec.srvoptions) AS srvoption
174+
LOOP
175+
IF EXISTS (SELECT FROM vault.secrets WHERE option_rec.option_value IN (id::text, key_id::text)) THEN
176+
EXECUTE format(
177+
'ALTER SERVER %I OPTIONS (SET %I %L)',
178+
server_rec.srvname,
179+
option_rec.option_name,
180+
(SELECT id FROM vault.secrets WHERE option_rec.option_value IN (id::text, key_id::text))
181+
);
182+
END IF;
183+
END LOOP;
184+
END LOOP;
185+
END IF;
186+
END;
187+
\$\$;
188+
EOF
189+
)
190+
run_sql -c "$UPDATE_WRAPPERS_SERVER_OPTIONS_QUERY"
191+
155192
# Patch to handle upgrading to pgsodium-less Vault
156193
REENCRYPT_VAULT_SECRETS_QUERY=$(cat <<EOF
157194
DO \$\$

0 commit comments

Comments
 (0)