mz_catalog/durable/upgrade/
v77_to_v78.rs1use crate::durable::upgrade::MigrationAction;
11use crate::durable::upgrade::objects_v77 as v77;
12use base64::prelude::*;
13use mz_ore::now::SYSTEM_TIME;
14use sha2::{Digest, Sha256};
15
16fn migrate_scram_client_to_stored(hash_old: &str) -> Option<String> {
19 let parts: Vec<&str> = hash_old.split('$').collect();
20 if parts.len() != 3 || parts[0] != "SCRAM-SHA-256" {
21 return None;
22 }
23 let iters_salt = parts[1];
24 let key_parts: Vec<&str> = parts[2].split(':').collect();
25 if key_parts.len() != 2 {
26 return None;
27 }
28 let first_key_b64 = key_parts[0];
29 let server_key_b64 = key_parts[1];
30
31 let Ok(first_key) = BASE64_STANDARD.decode(first_key_b64) else {
32 tracing::warn!("failed to decode base64 client key during scram migration");
33 return None;
34 };
35 if first_key.len() != 32 {
36 tracing::warn!("unexpected client key length during scram migration");
37 return None;
38 }
39
40 let stored_key = Sha256::digest(&first_key);
41 let stored_key_b64 = BASE64_STANDARD.encode(stored_key);
42
43 Some(format!(
44 "SCRAM-SHA-256${}${}:{}",
45 iters_salt, stored_key_b64, server_key_b64
46 ))
47}
48
49pub fn upgrade(
50 snapshot: Vec<v77::StateUpdateKind>,
51) -> Vec<MigrationAction<v77::StateUpdateKind, v77::StateUpdateKind>> {
52 let mut migrations = Vec::new();
53 for update in snapshot {
54 match update.kind {
55 Some(v77::state_update_kind::Kind::RoleAuth(old_role_auth)) => {
56 let Some(ref value) = old_role_auth.value else {
57 continue;
58 };
59 let Some(ref hashed_password) = value.password_hash else {
60 continue;
61 };
62 let Some(new_hash) = migrate_scram_client_to_stored(hashed_password) else {
63 continue;
64 };
65
66 let new_role_auth = v77::state_update_kind::RoleAuth {
67 key: old_role_auth.key.clone(),
68 value: Some(v77::RoleAuthValue {
69 password_hash: Some(new_hash),
70 updated_at: Some(v77::EpochMillis {
71 millis: SYSTEM_TIME(),
72 }),
73 }),
74 };
75 let old_role_auth = v77::StateUpdateKind {
76 kind: Some(v77::state_update_kind::Kind::RoleAuth(old_role_auth)),
77 };
78 let new_role_auth = v77::StateUpdateKind {
79 kind: Some(v77::state_update_kind::Kind::RoleAuth(new_role_auth)),
80 };
81 let migration = MigrationAction::Update(old_role_auth, new_role_auth);
82 migrations.push(migration);
83 }
84 _ => {}
85 }
86 }
87 migrations
88}