def disable_old_secret_versions(secret_client, secret_name):
    """Disable all but the latest version of a secret."""
    try:
        # List all versions of the secret
        versions = list(secret_client.list_properties_of_secret_versions(secret_name))
        
        # Filter out versions with None updated_on and already disabled versions
        valid_versions = [v for v in versions if hasattr(v, 'updated_on') and v.updated_on is not None and v.enabled]
        
        if not valid_versions:
            logging.info(f"No valid enabled versions found for secret {secret_name}")
            return
            
        # Find the latest version by updated_on
        latest_version = max(valid_versions, key=lambda v: v.updated_on)
        logging.info(f"Latest version of {secret_name} is {latest_version.version}")
        
        # Disable all versions except the latest
        for version in valid_versions:
            if version.version != latest_version.version:
                try:
                    logging.info(f"Disabling version {version.version} of {secret_name}")
                    # Update the secret's attributes to disable it
                    updated_properties = secret_client.update_secret_properties(
                        secret_name=secret_name,
                        version=version.version,
                        enabled=False
                    )
                    logging.info(f"Successfully disabled version {version.version} of {secret_name}")
                except Exception as e:
                    logging.warning(f"Failed to disable version {version.version} of {secret_name}: {e}")
    except Exception as e:
        logging.warning(f"Error managing versions for secret {secret_name}: {e}")
Disable all but the latest version of a secret.
Solutions/Commvault Security IQ/Data Connectors/AzureFunctionCommvaultSecurityIQ/main.py