domain.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright (c) 2020 xuyaming
  2. # Copyright 2020 ksyun.com, Inc. or its affiliates. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"). You
  5. # may not use this file except in compliance with the License. A copy of
  6. # the License is located at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # or in the "license" file accompanying this file. This file is
  11. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  12. # ANY KIND, either express or implied. See the License for the specific
  13. # language governing permissions and limitations under the License.
  14. import os
  15. import logging
  16. import kscore.configloader
  17. from kscore.exceptions import ConfigNotFound, PartialCredentialsError
  18. logger = logging.getLogger(__name__)
  19. def create_domain_resolver(session):
  20. providers = [
  21. KsDomainProvider()
  22. ]
  23. return DomainResolver(providers=providers)
  24. class KsDomainProvider(object):
  25. METHOD = 'ksc-config'
  26. KSC_CONFIG_ENV = 'KSC_CONFIG'
  27. DEFAULT_CONFIG_FILENAMES = ['/etc/kscore.cfg', './.kscore.cfg', 'C:\\kscore.cfg']
  28. KS_DOMAIN = 'ks_domain'
  29. def __init__(self, environ=None, ini_parser=None):
  30. if environ is None:
  31. environ = os.environ
  32. if ini_parser is None:
  33. ini_parser = kscore.configloader.raw_config_parse
  34. self._environ = environ
  35. self._ini_parser = ini_parser
  36. def _extract_domain_from_mapping(self, mapping, *key_names):
  37. found = []
  38. for key_name in key_names:
  39. try:
  40. found.append(mapping[key_name])
  41. except KeyError:
  42. raise PartialCredentialsError(provider=self.METHOD,
  43. cred_var=key_name)
  44. return found
  45. def load(self):
  46. if self.KSC_CONFIG_ENV in self._environ:
  47. potential_locations = [self._environ[self.KSC_CONFIG_ENV]]
  48. else:
  49. potential_locations = self.DEFAULT_CONFIG_FILENAMES
  50. for filename in potential_locations:
  51. try:
  52. config = self._ini_parser(filename)
  53. except ConfigNotFound:
  54. # Move on to the next potential config file name.
  55. continue
  56. if 'Domain' in config:
  57. domain = config['Domain']
  58. if self.KS_DOMAIN in domain:
  59. logger.info("Found Domain in ksc config file: %s",
  60. filename)
  61. ks_domain = self._extract_domain_from_mapping(
  62. domain, self.KS_DOMAIN)
  63. return Domain(ks_domain[0])
  64. class Domain(object):
  65. def __init__(self, ks_domain=None):
  66. self.ks_domain = ks_domain
  67. class DomainResolver(object):
  68. def __init__(self, providers):
  69. self.providers = providers
  70. def load_domain(self):
  71. """
  72. Goes through the credentials chain, returning the first ``Credentials``
  73. that could be loaded.
  74. """
  75. # First provider to return a non-None response wins.
  76. for provider in self.providers:
  77. logger.debug("Looking for domain via: %s", provider.METHOD)
  78. domain = provider.load()
  79. if domain is not None:
  80. return domain
  81. # If we got here, no credentials could be found.
  82. # This feels like it should be an exception, but historically, ``None``
  83. # is returned.
  84. #
  85. # +1
  86. # -js
  87. return None