format.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python
  2. """
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing, software
  12. distributed under the License is distributed on an "AS IS" BASIS,
  13. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. See the License for the specific language governing permissions and
  15. limitations under the License.
  16. Ambari Agent
  17. """
  18. __all__ = ["format"]
  19. import sys
  20. from string import Formatter
  21. from resource_management.core.exceptions import Fail
  22. from resource_management.core.utils import checked_unite
  23. from resource_management.core.environment import Environment
  24. from resource_management.core.logger import Logger
  25. from resource_management.core.shell import quote_bash_args
  26. class ConfigurationFormatter(Formatter):
  27. """
  28. Flags:
  29. !e - escape bash properties flag
  30. !h - hide sensitive information from the logs
  31. !p - password flag, !p=!s+!e. Has both !e, !h effect
  32. """
  33. def format(self, format_string, *args, **kwargs):
  34. env = Environment.get_instance()
  35. variables = kwargs
  36. params = env.config.params
  37. # don't use checked_unite for this as it would interfere with reload(module)
  38. # for things like params and status_params; instead, start out copying
  39. # the environment parameters and add in any locally declared variables to
  40. # override existing env parameters
  41. all_params = params.copy()
  42. all_params.update(variables)
  43. self.convert_field = self.convert_field_protected
  44. result_protected = self.vformat(format_string, args, all_params)
  45. self.convert_field = self.convert_field_unprotected
  46. result_unprotected = self.vformat(format_string, args, all_params)
  47. if result_protected != result_unprotected:
  48. Logger.sensitive_strings[result_unprotected] = result_protected
  49. return result_unprotected
  50. def convert_field_unprotected(self, value, conversion):
  51. return self._convert_field(value, conversion, False)
  52. def convert_field_protected(self, value, conversion):
  53. """
  54. Enable masking sensitive information like
  55. passwords from logs via !p (password) format flag.
  56. """
  57. return self._convert_field(value, conversion, True)
  58. def _convert_field(self, value, conversion, is_protected):
  59. if conversion == 'e':
  60. return quote_bash_args(unicode(value))
  61. elif conversion == 'h':
  62. return "[PROTECTED]" if is_protected else value
  63. elif conversion == 'p':
  64. return "[PROTECTED]" if is_protected else self._convert_field(value, 'e', is_protected)
  65. return super(ConfigurationFormatter, self).convert_field(value, conversion)
  66. def format(format_string, *args, **kwargs):
  67. variables = sys._getframe(1).f_locals
  68. result = checked_unite(kwargs, variables)
  69. result.pop("self", None) # self kwarg would result in an error
  70. return ConfigurationFormatter().format(format_string, args, **result)