question 461 Secure Python · easy
Mask the password in a connection string
Startup logs print the database URL so operators can see which database the app uses. The URL contains the password. Write a function that masks the password and keeps everything else.
Function
mask_url(url)Shape
scheme://user:password@host...; the part before the last @ after :// is user:password, split at the first :Returnsthe same URL with the password replaced by
****; a URL with no @, or with no : in the user part, is returned unchangedExamples
callmask_url('postgresql://market:s3cr3t-Pa55@db.internal:5432/shop')returns'postgresql://market:****@db.internal:5432/shop'
callmask_url('sqlite:///data/shop.db')returns'sqlite:///data/shop.db'
callmask_url('redis://cache:hunter2@10.0.0.7:6379/0')returns'redis://cache:****@10.0.0.7:6379/0'