This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathfetch_service_config.py
More file actions
executable file
·240 lines (199 loc) · 9.07 KB
/
Copy pathfetch_service_config.py
File metadata and controls
executable file
·240 lines (199 loc) · 9.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/python
#
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import certifi
import json
import logging
import urllib3
from oauth2client.service_account import ServiceAccountCredentials
# Service management service
SERVICE_MGMT_ROLLOUTS_URL_TEMPLATE = (
"{}/v1/services/{}/rollouts?filter=status=SUCCESS")
_GOOGLE_API_SCOPE = (
"https://www.googleapis.com/auth/service.management.readonly")
# Metadata service path
_METADATA_PATH = "/computeMetadata/v1/instance"
_METADATA_SERVICE_NAME = "endpoints-service-name"
_METADATA_SERVICE_CONFIG_ID = "endpoints-service-config-id"
_METADATA_ROLLOUT_STRATEGY = "endpoints-rollout-strategy"
class FetchError(Exception):
"""Error class for fetching and validation errors."""
def __init__(self, code, message):
self.code = code
self.message = message
def __str__(self):
return self.message
def fetch_service_config_rollout_strategy(metadata):
"""Fetch service config rollout strategy from metadata URL."""
url = metadata + _METADATA_PATH + "/attributes/" + \
_METADATA_ROLLOUT_STRATEGY
headers = {"Metadata-Flavor": "Google"}
client = urllib3.PoolManager(ca_certs=certifi.where())
try:
response = client.request("GET", url, headers=headers)
except:
logging.info("Failed to fetch service config rollout strategy " + \
"from the metadata server: " + url);
return None
status_code = response.status
if status_code != 200:
# Fetching rollout strategy is optional. No need to leave log
return None
rollout_strategy = response.data
logging.info("Service config rollout strategy: " + rollout_strategy)
return rollout_strategy
def fetch_service_name(metadata):
"""Fetch service name from metadata URL."""
url = metadata + _METADATA_PATH + "/attributes/" + _METADATA_SERVICE_NAME
headers = {"Metadata-Flavor": "Google"}
client = urllib3.PoolManager(ca_certs=certifi.where())
try:
response = client.request("GET", url, headers=headers)
except:
raise FetchError(1,
"Failed to fetch service name from the metadata server: " + url)
status_code = response.status
if status_code != 200:
message_template = "Fetching service name failed (url {}, status code {})"
raise FetchError(1, message_template.format(url, status_code))
name = response.data
logging.info("Service name: " + name)
return name
# config_id from metadata is optional. Returns None instead of raising error
def fetch_service_config_id(metadata):
"""Fetch service config ID from metadata URL."""
url = metadata + _METADATA_PATH + "/attributes/" + _METADATA_SERVICE_CONFIG_ID
headers = {"Metadata-Flavor": "Google"}
client = urllib3.PoolManager(ca_certs=certifi.where())
try:
response = client.request("GET", url, headers=headers)
if response.status != 200:
# Fetching service config id is optional. No need to leave log
raise None
except:
url = metadata + _METADATA_PATH + "/attributes/endpoints-service-version"
try:
response = client.request("GET", url, headers=headers)
except:
logging.info("Failed to fetch service config ID from the metadata server: " + url)
return None
if response.status != 200:
message_template = "Fetching service config ID failed (url {}, status code {})"
logging.info(message_template.format(url, response.status))
return None
version = response.data
logging.info("Service config ID:" + version)
return version
def make_access_token(secret_token_json):
"""Construct an access token from service account token."""
logging.info("Constructing an access token with scope " + _GOOGLE_API_SCOPE)
credentials = ServiceAccountCredentials.from_json_keyfile_name(
secret_token_json,
scopes=[_GOOGLE_API_SCOPE])
logging.info("Service account email: " + credentials.service_account_email)
token = credentials.get_access_token().access_token
return token
def fetch_access_token(metadata):
"""Fetch access token from metadata URL."""
access_token_url = metadata + _METADATA_PATH + "/service-accounts/default/token"
headers = {"Metadata-Flavor": "Google"}
client = urllib3.PoolManager(ca_certs=certifi.where())
try:
response = client.request("GET", access_token_url, headers=headers)
except:
raise FetchError(1,
"Failed to fetch access token from the metadata server: " + access_token_url)
status_code = response.status
if status_code != 200:
message_template = "Fetching access token failed (url {}, status code {})"
raise FetchError(1, message_template.format(access_token_url, status_code))
token = json.loads(response.data)["access_token"]
return token
def fetch_latest_rollout(management_service, service_name, access_token):
"""Fetch rollouts"""
if access_token is None:
headers = {}
else:
headers = {"Authorization": "Bearer {}".format(access_token)}
client = urllib3.PoolManager(ca_certs=certifi.where())
service_mgmt_url = SERVICE_MGMT_ROLLOUTS_URL_TEMPLATE.format(management_service,
service_name)
try:
response = client.request("GET", service_mgmt_url, headers=headers)
except:
raise FetchError(1, "Failed to fetch rollouts")
status_code = response.status
if status_code != 200:
message_template = ("Fetching rollouts failed "\
"(status code {}, reason {}, url {})")
raise FetchError(1, message_template.format(status_code,
response.reason,
service_mgmt_url))
rollouts = json.loads(response.data)
# No valid rollouts
if rollouts is None or \
'rollouts' not in rollouts or \
len(rollouts["rollouts"]) == 0 or \
"rolloutId" not in rollouts["rollouts"][0] or \
"trafficPercentStrategy" not in rollouts["rollouts"][0] or \
"percentages" not in rollouts["rollouts"][0]["trafficPercentStrategy"]:
message_template = ("Invalid rollouts response (url {}, data {})")
raise FetchError(1, message_template.format(service_mgmt_url,
response.data))
return rollouts["rollouts"][0]
def fetch_service_json(service_mgmt_url, access_token):
"""Fetch service config."""
if access_token is None:
headers = {}
else:
headers = {"Authorization": "Bearer {}".format(access_token)}
client = urllib3.PoolManager(ca_certs=certifi.where())
try:
response = client.request("GET", service_mgmt_url, headers=headers)
except:
raise FetchError(1, "Failed to fetch service config")
status_code = response.status
if status_code != 200:
message_template = "Fetching service config failed (status code {}, reason {}, url {})"
raise FetchError(1, message_template.format(status_code, response.reason, service_mgmt_url))
service_config = json.loads(response.data)
return service_config
def validate_service_config(service_config, expected_service_name,
expected_service_version):
"""Validate service config."""
service_name = service_config.get("name", None)
if not service_name:
raise FetchError(2, "No service name in the service config")
if service_name != expected_service_name:
message_template = "Unexpected service name in service config: {}"
raise FetchError(2, message_template.format(service_name))
service_version = service_config.get("id", None)
if not service_version:
raise FetchError(2, "No service config ID in the service config")
if service_version != expected_service_version:
message_template = "Unexpected service config ID in service config: {}"
raise FetchError(2, message_template.format(service_version))
# WARNING: sandbox migration workaround
control = service_config.get("control", None)
if not control:
raise FetchError(2, "No control section in the service config")
environment = control.get("environment", None)
if not environment:
raise FetchError(2, "Missing control environment")
if environment == "endpoints-servicecontrol.sandbox.googleapis.com":
logging.warning("Replacing sandbox control environment in the service config")
service_config["control"]["environment"] = (
"servicecontrol.googleapis.com")