1+ use crate :: redaction:: main:: redact_error_text:: redact_error_text;
12use crate :: redaction:: main:: redact_text:: redact_text;
23use crate :: redaction:: tests:: test_types:: RedactTestCase ;
34
@@ -19,6 +20,61 @@ fn given_diagnostic_text_when_redacting_then_matches_python_substitutions() {
1920 text : "nothing to see" ,
2021 expected_text : "nothing to see" ,
2122 } ,
23+ RedactTestCase {
24+ description : "a key glued to a prefix with a separator" ,
25+ text : "db_password=orders-pass host=warehouse" ,
26+ expected_text : "db_password=[REDACTED] host=warehouse" ,
27+ } ,
28+ RedactTestCase {
29+ description : "a key glued to a prefix without a separator" ,
30+ text : "PGPASSWORD=orders-pass psql" ,
31+ expected_text : "PGPASSWORD=[REDACTED] psql" ,
32+ } ,
33+ RedactTestCase {
34+ description : "near miss: a key that only starts with a secret word keeps its value" ,
35+ text : "password_policy=strict max_tokens=5 tokenizer: words passwords: 3 tries" ,
36+ expected_text : "password_policy=strict max_tokens=5 tokenizer: words passwords: 3 tries" ,
37+ } ,
38+ RedactTestCase {
39+ description : "near miss: pwd stays a whole word, so working directories are kept" ,
40+ text : "OLDPWD=/srv/orders cwd=/srv/orders pwd=orders-pass" ,
41+ expected_text : "OLDPWD=/srv/orders cwd=/srv/orders pwd=[REDACTED]" ,
42+ } ,
43+ RedactTestCase {
44+ description : "a quoted PASSWORD in DDL" ,
45+ text : "CREATE ROLE orders_app WITH LOGIN PASSWORD 'orders-pass' VALID UNTIL 'infinity'" ,
46+ expected_text : "CREATE ROLE orders_app WITH LOGIN PASSWORD [REDACTED] VALID UNTIL 'infinity'" ,
47+ } ,
48+ RedactTestCase {
49+ description : "IDENTIFIED BY with a quoted and a bare password" ,
50+ text : "CREATE USER orders IDENTIFIED BY 'orders-pass'; ALTER USER stock IDENTIFIED BY stockpass" ,
51+ expected_text : "CREATE USER orders IDENTIFIED BY [REDACTED]; ALTER USER stock IDENTIFIED BY [REDACTED]" ,
52+ } ,
53+ RedactTestCase {
54+ description : "IDENTIFIED WITH a plugin BY a password, as ClickHouse writes it" ,
55+ text : "CREATE USER orders IDENTIFIED WITH sha256_password BY 'orders-pass' HOST ANY" ,
56+ expected_text : "CREATE USER orders IDENTIFIED WITH sha256_password BY [REDACTED] HOST ANY" ,
57+ } ,
58+ RedactTestCase {
59+ description : "near miss: prose about passwords and IDENTIFIED WITH keep their words" ,
60+ text : "password authentication failed for user \" orders\" ; IDENTIFIED WITH caching_sha2_password" ,
61+ expected_text : "password authentication failed for user \" orders\" ; IDENTIFIED WITH caching_sha2_password" ,
62+ } ,
63+ RedactTestCase {
64+ description : "an unencoded @ inside a URI password" ,
65+ text : "url=postgres://orders:p@ss@warehouse.example.test/orders" ,
66+ expected_text : "url=postgres://orders:[REDACTED]@warehouse.example.test/orders" ,
67+ } ,
68+ RedactTestCase {
69+ description : "near miss: an @ after the host belongs to the path or query, not the password" ,
70+ text : "postgres://orders:pw@warehouse.example.test/orders?contact=support@example.com, https://api.example.test/v1" ,
71+ expected_text : "postgres://orders:[REDACTED]@warehouse.example.test/orders?contact=support@example.com, https://api.example.test/v1" ,
72+ } ,
73+ RedactTestCase {
74+ description : "two URIs in a list each lose their password" ,
75+ text : "postgres://orders:one@h1.example.test,postgres://stock:two@h2.example.test" ,
76+ expected_text : "postgres://orders:[REDACTED]@h1.example.test,postgres://stock:[REDACTED]@h2.example.test" ,
77+ } ,
2278 ] ;
2379
2480 for test_case in test_cases {
@@ -30,3 +86,43 @@ fn given_diagnostic_text_when_redacting_then_matches_python_substitutions() {
3086 ) ;
3187 }
3288}
89+
90+ #[ test]
91+ fn given_error_text_with_cut_uris_when_redacting_then_position_tells_a_port_from_a_password ( ) {
92+ let test_cases = [
93+ RedactTestCase {
94+ description : "an all-digit password in a URI cut before its host" ,
95+ text : "could not parse 'postgres://orders:1234" ,
96+ expected_text : "could not parse 'postgres://orders:[REDACTED]" ,
97+ } ,
98+ RedactTestCase {
99+ description : "a password cut at a closing quote" ,
100+ text : "dsn 'postgres://orders:orders-pass' rejected" ,
101+ expected_text : "dsn 'postgres://orders:[REDACTED]' rejected" ,
102+ } ,
103+ RedactTestCase {
104+ description : "a cut password containing / is not a port" ,
105+ text : "cut 'postgres://orders:pa/ss" ,
106+ expected_text : "cut 'postgres://orders:[REDACTED]/ss" ,
107+ } ,
108+ RedactTestCase {
109+ description : "near miss: a port the authority ends after stays" ,
110+ text : "GET http://warehouse.example.test:8080/health failed; retry http://warehouse.example.test:8443?attempt=2" ,
111+ expected_text : "GET http://warehouse.example.test:8080/health failed; retry http://warehouse.example.test:8443?attempt=2" ,
112+ } ,
113+ RedactTestCase {
114+ description : "a complete URI is redacted once and a second pass changes nothing" ,
115+ text : "postgres://orders:[REDACTED]@warehouse.example.test:5432/orders" ,
116+ expected_text : "postgres://orders:[REDACTED]@warehouse.example.test:5432/orders" ,
117+ } ,
118+ ] ;
119+
120+ for test_case in test_cases {
121+ assert_eq ! (
122+ redact_error_text( test_case. text) ,
123+ test_case. expected_text,
124+ "{}" ,
125+ test_case. description
126+ ) ;
127+ }
128+ }
0 commit comments