|
| 1 | +//! Encryption sanity checks - verify data is actually encrypted. |
| 2 | +//! |
| 3 | +//! These tests insert data through the proxy, then query DIRECTLY from the database |
| 4 | +//! (bypassing the proxy) to verify the stored value is encrypted (differs from plaintext). |
| 5 | +//! |
| 6 | +//! This catches silent mapping failures where data passes through unencrypted. |
| 7 | +
|
| 8 | +#[cfg(test)] |
| 9 | +mod tests { |
| 10 | + use crate::common::{ |
| 11 | + assert_encrypted_jsonb, assert_encrypted_numeric, assert_encrypted_text, clear, |
| 12 | + connect_with_tls, random_id, trace, PROXY, |
| 13 | + }; |
| 14 | + use chrono::NaiveDate; |
| 15 | + |
| 16 | + #[tokio::test] |
| 17 | + async fn text_encryption_sanity_check() { |
| 18 | + trace(); |
| 19 | + clear().await; |
| 20 | + |
| 21 | + let id = random_id(); |
| 22 | + let plaintext = "hello world"; |
| 23 | + |
| 24 | + // Insert through proxy (should encrypt) |
| 25 | + let client = connect_with_tls(PROXY).await; |
| 26 | + let sql = "INSERT INTO encrypted (id, encrypted_text) VALUES ($1, $2)"; |
| 27 | + client.query(sql, &[&id, &plaintext]).await.unwrap(); |
| 28 | + |
| 29 | + // Verify encryption occurred |
| 30 | + assert_encrypted_text(id, "encrypted_text", plaintext).await; |
| 31 | + |
| 32 | + // Round-trip: query through proxy should decrypt back to original |
| 33 | + let sql = "SELECT encrypted_text FROM encrypted WHERE id = $1"; |
| 34 | + let rows = client.query(sql, &[&id]).await.unwrap(); |
| 35 | + assert_eq!(rows.len(), 1, "Expected exactly one row for round-trip"); |
| 36 | + let decrypted: String = rows[0].get(0); |
| 37 | + assert_eq!( |
| 38 | + decrypted, plaintext, |
| 39 | + "DECRYPTION FAILED: Round-trip value doesn't match original!" |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + #[tokio::test] |
| 44 | + async fn jsonb_encryption_sanity_check() { |
| 45 | + trace(); |
| 46 | + clear().await; |
| 47 | + |
| 48 | + let id = random_id(); |
| 49 | + let plaintext_json = serde_json::json!({"key": "value", "number": 42}); |
| 50 | + |
| 51 | + // Insert through proxy (should encrypt) |
| 52 | + let client = connect_with_tls(PROXY).await; |
| 53 | + let sql = "INSERT INTO encrypted (id, encrypted_jsonb) VALUES ($1, $2)"; |
| 54 | + client.query(sql, &[&id, &plaintext_json]).await.unwrap(); |
| 55 | + |
| 56 | + // Verify encryption occurred |
| 57 | + assert_encrypted_jsonb(id, &plaintext_json).await; |
| 58 | + |
| 59 | + // Round-trip: query through proxy should decrypt back to original |
| 60 | + let sql = "SELECT encrypted_jsonb FROM encrypted WHERE id = $1"; |
| 61 | + let rows = client.query(sql, &[&id]).await.unwrap(); |
| 62 | + assert_eq!(rows.len(), 1, "Expected exactly one row for round-trip"); |
| 63 | + let decrypted: serde_json::Value = rows[0].get(0); |
| 64 | + assert_eq!( |
| 65 | + decrypted, plaintext_json, |
| 66 | + "DECRYPTION FAILED: Round-trip value doesn't match original!" |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + #[tokio::test] |
| 71 | + async fn float8_encryption_sanity_check() { |
| 72 | + trace(); |
| 73 | + clear().await; |
| 74 | + |
| 75 | + let id = random_id(); |
| 76 | + let plaintext: f64 = 123.456; |
| 77 | + |
| 78 | + // Insert through proxy (should encrypt) |
| 79 | + let client = connect_with_tls(PROXY).await; |
| 80 | + let sql = "INSERT INTO encrypted (id, encrypted_float8) VALUES ($1, $2)"; |
| 81 | + client.query(sql, &[&id, &plaintext]).await.unwrap(); |
| 82 | + |
| 83 | + // Verify encryption occurred |
| 84 | + assert_encrypted_numeric(id, "encrypted_float8", plaintext).await; |
| 85 | + |
| 86 | + // Round-trip: query through proxy should decrypt back to original |
| 87 | + let sql = "SELECT encrypted_float8 FROM encrypted WHERE id = $1"; |
| 88 | + let rows = client.query(sql, &[&id]).await.unwrap(); |
| 89 | + assert_eq!(rows.len(), 1, "Expected exactly one row for round-trip"); |
| 90 | + let decrypted: f64 = rows[0].get(0); |
| 91 | + assert!( |
| 92 | + (decrypted - plaintext).abs() < f64::EPSILON, |
| 93 | + "DECRYPTION FAILED: Round-trip value doesn't match original!" |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + #[tokio::test] |
| 98 | + async fn bool_encryption_sanity_check() { |
| 99 | + trace(); |
| 100 | + clear().await; |
| 101 | + |
| 102 | + let id = random_id(); |
| 103 | + let plaintext: bool = true; |
| 104 | + |
| 105 | + // Insert through proxy (should encrypt) |
| 106 | + let client = connect_with_tls(PROXY).await; |
| 107 | + let sql = "INSERT INTO encrypted (id, encrypted_bool) VALUES ($1, $2)"; |
| 108 | + client.query(sql, &[&id, &plaintext]).await.unwrap(); |
| 109 | + |
| 110 | + // Verify encryption occurred |
| 111 | + assert_encrypted_text(id, "encrypted_bool", &plaintext.to_string()).await; |
| 112 | + |
| 113 | + // Round-trip: query through proxy should decrypt back to original |
| 114 | + let sql = "SELECT encrypted_bool FROM encrypted WHERE id = $1"; |
| 115 | + let rows = client.query(sql, &[&id]).await.unwrap(); |
| 116 | + assert_eq!(rows.len(), 1, "Expected exactly one row for round-trip"); |
| 117 | + let decrypted: bool = rows[0].get(0); |
| 118 | + assert_eq!( |
| 119 | + decrypted, plaintext, |
| 120 | + "DECRYPTION FAILED: Round-trip value doesn't match original!" |
| 121 | + ); |
| 122 | + } |
| 123 | + |
| 124 | + #[tokio::test] |
| 125 | + async fn date_encryption_sanity_check() { |
| 126 | + trace(); |
| 127 | + clear().await; |
| 128 | + |
| 129 | + let id = random_id(); |
| 130 | + let plaintext = NaiveDate::from_ymd_opt(2024, 6, 15).unwrap(); |
| 131 | + |
| 132 | + // Insert through proxy (should encrypt) |
| 133 | + let client = connect_with_tls(PROXY).await; |
| 134 | + let sql = "INSERT INTO encrypted (id, encrypted_date) VALUES ($1, $2)"; |
| 135 | + client.query(sql, &[&id, &plaintext]).await.unwrap(); |
| 136 | + |
| 137 | + // Verify encryption occurred |
| 138 | + assert_encrypted_text(id, "encrypted_date", &plaintext.to_string()).await; |
| 139 | + |
| 140 | + // Round-trip: query through proxy should decrypt back to original |
| 141 | + let sql = "SELECT encrypted_date FROM encrypted WHERE id = $1"; |
| 142 | + let rows = client.query(sql, &[&id]).await.unwrap(); |
| 143 | + assert_eq!(rows.len(), 1, "Expected exactly one row for round-trip"); |
| 144 | + let decrypted: NaiveDate = rows[0].get(0); |
| 145 | + assert_eq!( |
| 146 | + decrypted, plaintext, |
| 147 | + "DECRYPTION FAILED: Round-trip value doesn't match original!" |
| 148 | + ); |
| 149 | + } |
| 150 | + |
| 151 | + #[tokio::test] |
| 152 | + async fn int2_encryption_sanity_check() { |
| 153 | + trace(); |
| 154 | + clear().await; |
| 155 | + |
| 156 | + let id = random_id(); |
| 157 | + let plaintext: i16 = 42; |
| 158 | + |
| 159 | + // Insert through proxy (should encrypt) |
| 160 | + let client = connect_with_tls(PROXY).await; |
| 161 | + let sql = "INSERT INTO encrypted (id, encrypted_int2) VALUES ($1, $2)"; |
| 162 | + client.query(sql, &[&id, &plaintext]).await.unwrap(); |
| 163 | + |
| 164 | + // Verify encryption occurred |
| 165 | + assert_encrypted_numeric(id, "encrypted_int2", plaintext).await; |
| 166 | + |
| 167 | + // Round-trip: query through proxy should decrypt back to original |
| 168 | + let sql = "SELECT encrypted_int2 FROM encrypted WHERE id = $1"; |
| 169 | + let rows = client.query(sql, &[&id]).await.unwrap(); |
| 170 | + assert_eq!(rows.len(), 1, "Expected exactly one row for round-trip"); |
| 171 | + let decrypted: i16 = rows[0].get(0); |
| 172 | + assert_eq!( |
| 173 | + decrypted, plaintext, |
| 174 | + "DECRYPTION FAILED: Round-trip value doesn't match original!" |
| 175 | + ); |
| 176 | + } |
| 177 | + |
| 178 | + #[tokio::test] |
| 179 | + async fn int4_encryption_sanity_check() { |
| 180 | + trace(); |
| 181 | + clear().await; |
| 182 | + |
| 183 | + let id = random_id(); |
| 184 | + let plaintext: i32 = 12345; |
| 185 | + |
| 186 | + // Insert through proxy (should encrypt) |
| 187 | + let client = connect_with_tls(PROXY).await; |
| 188 | + let sql = "INSERT INTO encrypted (id, encrypted_int4) VALUES ($1, $2)"; |
| 189 | + client.query(sql, &[&id, &plaintext]).await.unwrap(); |
| 190 | + |
| 191 | + // Verify encryption occurred |
| 192 | + assert_encrypted_numeric(id, "encrypted_int4", plaintext).await; |
| 193 | + |
| 194 | + // Round-trip: query through proxy should decrypt back to original |
| 195 | + let sql = "SELECT encrypted_int4 FROM encrypted WHERE id = $1"; |
| 196 | + let rows = client.query(sql, &[&id]).await.unwrap(); |
| 197 | + assert_eq!(rows.len(), 1, "Expected exactly one row for round-trip"); |
| 198 | + let decrypted: i32 = rows[0].get(0); |
| 199 | + assert_eq!( |
| 200 | + decrypted, plaintext, |
| 201 | + "DECRYPTION FAILED: Round-trip value doesn't match original!" |
| 202 | + ); |
| 203 | + } |
| 204 | + |
| 205 | + #[tokio::test] |
| 206 | + async fn int8_encryption_sanity_check() { |
| 207 | + trace(); |
| 208 | + clear().await; |
| 209 | + |
| 210 | + let id = random_id(); |
| 211 | + let plaintext: i64 = 9876543210; |
| 212 | + |
| 213 | + // Insert through proxy (should encrypt) |
| 214 | + let client = connect_with_tls(PROXY).await; |
| 215 | + let sql = "INSERT INTO encrypted (id, encrypted_int8) VALUES ($1, $2)"; |
| 216 | + client.query(sql, &[&id, &plaintext]).await.unwrap(); |
| 217 | + |
| 218 | + // Verify encryption occurred |
| 219 | + assert_encrypted_numeric(id, "encrypted_int8", plaintext).await; |
| 220 | + |
| 221 | + // Round-trip: query through proxy should decrypt back to original |
| 222 | + let sql = "SELECT encrypted_int8 FROM encrypted WHERE id = $1"; |
| 223 | + let rows = client.query(sql, &[&id]).await.unwrap(); |
| 224 | + assert_eq!(rows.len(), 1, "Expected exactly one row for round-trip"); |
| 225 | + let decrypted: i64 = rows[0].get(0); |
| 226 | + assert_eq!( |
| 227 | + decrypted, plaintext, |
| 228 | + "DECRYPTION FAILED: Round-trip value doesn't match original!" |
| 229 | + ); |
| 230 | + } |
| 231 | +} |
0 commit comments