1. import json
  2. def replace_token_versions(config_file, target_version):
  3. """
  4. Replaces authentication token values in a configuration file
  5. for isolated environments with support for older versions.
  6. Args:
  7. config_file (str): Path to the configuration file (JSON).
  8. target_version (str): The version to replace tokens with.
  9. """
  10. try:
  11. with open(config_file, 'r') as f:
  12. config = json.load(f)
  13. # Iterate through the config and replace tokens. Adjust keys as needed.
  14. for env in config.get('environments', []):
  15. if env.get('name') == 'isolated': # Check for the isolated environment
  16. for key, value in env.items():
  17. if isinstance(value, str) and 'token' in key.lower(): # Check for token-related keys
  18. env[key] = f"token_{target_version}"
  19. # Write the modified config back to the file
  20. with open(config_file, 'w') as f:
  21. json.dump(config, f, indent=4)
  22. print(f"Tokens in {config_file} updated to version {target_version} for isolated environment.")
  23. except FileNotFoundError:
  24. print(f"Error: Configuration file not found: {config_file}")
  25. except json.JSONDecodeError:
  26. print(f"Error: Invalid JSON format in {config_file}")
  27. except Exception as e:
  28. print(f"An unexpected error occurred: {e}")
  29. if __name__ == '__main__':
  30. # Example usage:
  31. replace_token_versions('config.json', 'v1')

Add your comment