Skip to content

Commit 789b40e

Browse files
committed
fix(vitess): converge interrupted auth rotations
1 parent a886dfb commit 789b40e

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

packages/ts-cloud/src/drivers/shared/vitess-provision.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,12 @@ describe('cluster authentication', () => {
221221
// Static auth is loaded at process start. `enable --now` leaves a running
222222
// process untouched, which made credential rotations fail the health gate.
223223
const script = buildVitessProvisionScript({ ...CLUSTER, username: 'app', password: 'production-secret' }).join('\n')
224-
expect(script).toContain('cmp -s "$VTESS_AUTH_TMP" /etc/vitess/auth.json || VTESS_VTGATE_AUTH_CHANGED=1')
225-
expect(script).toContain('if [ "$VTESS_VTGATE_AUTH_CHANGED" = 1 ]; then systemctl restart vitess-vtgate.service; fi')
224+
expect(script).toContain('VTESS_VTGATE_AUTH_SHA="$(sha256sum "$VTESS_AUTH_TMP"')
225+
expect(script).toContain('/var/lib/vitess/vtgate-auth.sha256')
226+
expect(script).toContain('if [ "$VTESS_VTGATE_AUTH_CHANGED" = 1 ]; then systemctl restart vitess-vtgate.service;')
227+
const restart = script.split('\n').find(line => line.includes('systemctl restart vitess-vtgate.service'))
228+
expect(restart).toContain('printf \'%s')
229+
expect(restart).toContain('"$VTESS_VTGATE_AUTH_SHA" > /var/lib/vitess/vtgate-auth.sha256')
226230
})
227231
})
228232

packages/ts-cloud/src/drivers/shared/vitess-provision.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ export function buildMysqlctldUnit(config: VitessServiceConfig): string {
415415

416416
/** Where the generated vtgate credentials live. */
417417
export const VITESS_AUTH_FILE = '/etc/vitess/auth.json'
418+
/** Last credential document successfully loaded by vtgate. */
419+
export const VITESS_AUTH_APPLIED_FILE = '/var/lib/vitess/vtgate-auth.sha256'
418420

419421
/**
420422
* vtgate's static credentials file.
@@ -439,8 +441,9 @@ export function buildVitessAuthFileScript(config: VitessServiceConfig): string[]
439441
`cat > "$VTESS_AUTH_TMP" <<'TS_CLOUD_VITESS_AUTH_EOF'`,
440442
auth,
441443
'TS_CLOUD_VITESS_AUTH_EOF',
442-
`VTESS_VTGATE_AUTH_CHANGED=0`,
443-
`cmp -s "$VTESS_AUTH_TMP" ${VITESS_AUTH_FILE} || VTESS_VTGATE_AUTH_CHANGED=1`,
444+
`VTESS_VTGATE_AUTH_SHA="$(sha256sum "$VTESS_AUTH_TMP" | awk '{print $1}')"`,
445+
`VTESS_VTGATE_AUTH_CHANGED=1`,
446+
`if [ -f ${VITESS_AUTH_APPLIED_FILE} ] && [ "$(cat ${VITESS_AUTH_APPLIED_FILE})" = "$VTESS_VTGATE_AUTH_SHA" ]; then VTESS_VTGATE_AUTH_CHANGED=0; fi`,
444447
// Contains a password: readable by the daemon, nobody else.
445448
`install -o ${VITESS_USER} -g ${VITESS_USER} -m 0600 "$VTESS_AUTH_TMP" ${VITESS_AUTH_FILE}`,
446449
'rm -f "$VTESS_AUTH_TMP"',
@@ -678,7 +681,7 @@ export function buildVitessProvisionScript(value: boolean | VitessServiceConfig
678681
// changed static-auth file is only read at process start, so rotate it
679682
// before the health gate while avoiding needless router restarts when the
680683
// desired credentials are unchanged.
681-
'if [ "$VTESS_VTGATE_AUTH_CHANGED" = 1 ]; then systemctl restart vitess-vtgate.service; fi',
684+
`if [ "$VTESS_VTGATE_AUTH_CHANGED" = 1 ]; then systemctl restart vitess-vtgate.service; printf '%s\\n' "$VTESS_VTGATE_AUTH_SHA" > ${VITESS_AUTH_APPLIED_FILE}; chown ${VITESS_USER}:${VITESS_USER} ${VITESS_AUTH_APPLIED_FILE}; chmod 0600 ${VITESS_AUTH_APPLIED_FILE}; fi`,
682685
...buildVitessBootstrapScript(config),
683686
...buildVitessHealthCheck(config),
684687
)

0 commit comments

Comments
 (0)