38 lines
806 B
Python
38 lines
806 B
Python
"""
|
|
Name Sanitization Utility
|
|
|
|
Sanitizes names for use in Azure Key Vault.
|
|
"""
|
|
|
|
import re
|
|
|
|
|
|
def sanitize_name(name: str) -> str:
|
|
"""
|
|
Sanitize a name for use in Azure Key Vault.
|
|
Key Vault secret names can only contain alphanumeric characters and hyphens.
|
|
|
|
Args:
|
|
name: The name to sanitize
|
|
|
|
Returns:
|
|
str: Sanitized name
|
|
|
|
Example:
|
|
>>> sanitize_name("My App (Production) #2024")
|
|
'My-App-Production-2024'
|
|
"""
|
|
if not name:
|
|
return name
|
|
|
|
# Replace any non-alphanumeric character (except hyphens) with hyphen
|
|
sanitized = re.sub(r'[^0-9a-zA-Z-]', '-', name)
|
|
|
|
# Remove consecutive hyphens
|
|
sanitized = re.sub(r'-+', '-', sanitized)
|
|
|
|
# Remove leading/trailing hyphens
|
|
sanitized = sanitized.strip('-')
|
|
|
|
return sanitized
|