regions.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Copyright 2014 ksyun.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"). You
  4. # may not use this file except in compliance with the License. A copy of
  5. # the License is located at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # or in the "license" file accompanying this file. This file is
  10. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. # ANY KIND, either express or implied. See the License for the specific
  12. # language governing permissions and limitations under the License.
  13. """Resolves regions and endpoints.
  14. This module implements endpoint resolution, including resolving endpoints for a
  15. given service and region and resolving the available endpoints for a service
  16. in a specific KSYUN partition.
  17. """
  18. import logging
  19. import re
  20. from kscore.exceptions import NoRegionError
  21. LOG = logging.getLogger(__name__)
  22. DEFAULT_URI_TEMPLATE = '{service}.{dnsSuffix}'
  23. DEFAULT_SERVICE_DATA = {'endpoints': {}}
  24. class BaseEndpointResolver(object):
  25. """Resolves regions and endpoints. Must be subclassed."""
  26. def construct_endpoint(self, service_name, region_name=None):
  27. """Resolves an endpoint for a service and region combination.
  28. :type service_name: string
  29. :param service_name: Name of the service to resolve an endpoint for
  30. (e.g., s3)
  31. :type region_name: string
  32. :param region_name: Region/endpoint name to resolve (e.g., us-east-1)
  33. if no region is provided, the first found partition-wide endpoint
  34. will be used if available.
  35. :rtype: dict
  36. :return: Returns a dict containing the following keys:
  37. - partition: (string, required) Resolved partition name
  38. - endpointName: (string, required) Resolved endpoint name
  39. - hostname: (string, required) Hostname to use for this endpoint
  40. - sslCommonName: (string) sslCommonName to use for this endpoint.
  41. - credentialScope: (dict) Signature version 4 credential scope
  42. - region: (string) region name override when signing.
  43. - service: (string) service name override when signing.
  44. - signatureVersions: (list<string>) A list of possible signature
  45. versions, including s3, v4, v2, and s3v4
  46. - protocols: (list<string>) A list of supported protocols
  47. (e.g., http, https)
  48. - ...: Other keys may be included as well based on the metadata
  49. """
  50. raise NotImplementedError
  51. def get_available_partitions(self):
  52. """Lists the partitions available to the endpoint resolver.
  53. :return: Returns a list of partition names (e.g., ["ks", "ks-cn"]).
  54. """
  55. raise NotImplementedError
  56. def get_available_endpoints(self, service_name, partition_name='ks',
  57. allow_non_regional=False):
  58. """Lists the endpoint names of a particular partition.
  59. :type service_name: string
  60. :param service_name: Name of a service to list endpoint for (e.g., s3)
  61. :type partition_name: string
  62. :param partition_name: Name of the partition to limit endpoints to.
  63. (e.g., KSYUN for the public KSYUN endpoints, aws-cn for KSYUN China
  64. endpoints, aws-us-gov for KSYUN GovCloud (US) Endpoints, etc.
  65. :type allow_non_regional: bool
  66. :param allow_non_regional: Set to True to include endpoints that are
  67. not regional endpoints (e.g., s3-external-1,
  68. fips-us-gov-west-1, etc).
  69. :return: Returns a list of endpoint names (e.g., ["us-east-1"]).
  70. """
  71. raise NotImplementedError
  72. class EndpointResolver(BaseEndpointResolver):
  73. """Resolves endpoints based on partition endpoint metadata"""
  74. def __init__(self, endpoint_data):
  75. """
  76. :param endpoint_data: A dict of partition data.
  77. """
  78. if 'partitions' not in endpoint_data:
  79. raise ValueError('Missing "partitions" in endpoint data')
  80. self._endpoint_data = endpoint_data
  81. def get_available_partitions(self):
  82. result = []
  83. for partition in self._endpoint_data['partitions']:
  84. result.append(partition['partition'])
  85. return result
  86. def get_available_endpoints(self, service_name, partition_name='ks',
  87. allow_non_regional=False):
  88. result = []
  89. for partition in self._endpoint_data['partitions']:
  90. if partition['partition'] != partition_name:
  91. continue
  92. services = partition['services']
  93. if service_name not in services:
  94. continue
  95. for endpoint_name in services[service_name]['endpoints']:
  96. if allow_non_regional or endpoint_name in partition['regions']:
  97. result.append(endpoint_name)
  98. return result
  99. def construct_endpoint(self, service_name, region_name=None):
  100. # Iterate over each partition until a match is found.
  101. for partition in self._endpoint_data['partitions']:
  102. result = self._endpoint_for_partition(
  103. partition, service_name, region_name)
  104. if result:
  105. return result
  106. def _endpoint_for_partition(self, partition, service_name, region_name):
  107. # Get the service from the partition, or an empty template.
  108. service_data = partition['services'].get(
  109. service_name, DEFAULT_SERVICE_DATA)
  110. # Use the partition endpoint if no region is supplied.
  111. if region_name is None:
  112. if 'partitionEndpoint' in service_data:
  113. region_name = service_data['partitionEndpoint']
  114. else:
  115. raise NoRegionError()
  116. # Attempt to resolve the exact region for this partition.
  117. if region_name in service_data['endpoints']:
  118. return self._resolve(
  119. partition, service_name, service_data, region_name)
  120. # Check to see if the endpoint provided is valid for the partition.
  121. if self._region_match(partition, region_name):
  122. # Use the partition endpoint if set and not regionalized.
  123. partition_endpoint = service_data.get('partitionEndpoint')
  124. is_regionalized = service_data.get('isRegionalized', True)
  125. if partition_endpoint and not is_regionalized:
  126. LOG.debug('Using partition endpoint for %s, %s: %s',
  127. service_name, region_name, partition_endpoint)
  128. return self._resolve(
  129. partition, service_name, service_data, partition_endpoint)
  130. LOG.debug('Creating a regex based endpoint for %s, %s',
  131. service_name, region_name)
  132. return self._resolve(
  133. partition, service_name, service_data, region_name)
  134. def _region_match(self, partition, region_name):
  135. if region_name in partition['regions']:
  136. return True
  137. if 'regionRegex' in partition:
  138. return re.compile(partition['regionRegex']).match(region_name)
  139. return False
  140. def _resolve(self, partition, service_name, service_data, endpoint_name):
  141. result = service_data['endpoints'].get(endpoint_name, {})
  142. result['partition'] = partition['partition']
  143. result['endpointName'] = endpoint_name
  144. # Merge in the service defaults then the partition defaults.
  145. self._merge_keys(service_data.get('defaults', {}), result)
  146. self._merge_keys(partition.get('defaults', {}), result)
  147. hostname = result.get('hostname', DEFAULT_URI_TEMPLATE)
  148. result['hostname'] = self._expand_domain_template(
  149. partition,hostname, service_name)
  150. if 'sslCommonName' in result:
  151. result['sslCommonName'] = self._expand_template(
  152. partition, result['sslCommonName'], service_name,
  153. endpoint_name)
  154. return result
  155. def _merge_keys(self, from_data, result):
  156. for key in from_data:
  157. if key not in result:
  158. result[key] = from_data[key]
  159. def _expand_domain_template(self, partition, template, service_name):
  160. return template.format(
  161. service=service_name, dnsSuffix=partition['dnsSuffix'])
  162. def _expand_template(self, partition, template, service_name,
  163. endpoint_name):
  164. return template.format(
  165. service=service_name, region=endpoint_name,
  166. dnsSuffix=partition['dnsSuffix'])