Skip to content

Commit f9d47e3

Browse files
author
Matthew Fisher
committed
feat(client): check server version
If the client sends a X-Deis-Version header to the server, a django middleware class will validate and ensure that the versions are the same. If they are not, the request is rejected, and a HttpResponse object is returned with the client and server versions displayed.
1 parent 6cef3b0 commit f9d47e3

3 files changed

Lines changed: 35 additions & 2 deletions

File tree

client/deis.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,18 +362,25 @@ def __init__(self):
362362
self._session = Session()
363363
self._settings = Settings()
364364

365-
def _dispatch(self, method, path, body=None,
366-
headers={'content-type': 'application/json'}, **kwargs):
365+
def _dispatch(self, method, path, body=None, **kwargs):
367366
"""
368367
Dispatch an API request to the active Deis controller
369368
"""
369+
headers = {
370+
'content-type': 'application/json',
371+
'X-Deis-Version': __version__.rsplit('.', 1)[0],
372+
}
370373
func = getattr(self._session, method.lower())
371374
controller = self._settings['controller']
372375
if not controller:
373376
raise EnvironmentError(
374377
'No active controller. Use `deis login` or `deis register` to get started.')
375378
url = urlparse.urljoin(controller, path, **kwargs)
376379
response = func(url, data=body, headers=headers)
380+
# check for errors
381+
if response.json().get('error') is not None:
382+
print(response.json()['error'])
383+
sys.exit(1)
377384
return response
378385

379386
def apps(self, args):

controller/api/middleware.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import json
2+
3+
from django.http import HttpResponse
4+
5+
from deis import __version__
6+
7+
8+
class VersionMiddleware:
9+
10+
def process_request(self, request):
11+
# server and client version must match "x.y"
12+
server_version = __version__.rsplit('.', 1)[0]
13+
try:
14+
if request.META['HTTP_X_DEIS_VERSION'] != server_version:
15+
message = {
16+
'error': 'Client and server versions do not match.\n' +
17+
'Client version: {}\n'.format(server_version) +
18+
'Server version: {}'.format(request.META['HTTP_X_DEIS_VERSION'])
19+
}
20+
return HttpResponse(
21+
json.dumps(message),
22+
content_type='application/json'
23+
)
24+
except KeyError:
25+
pass

controller/deis/settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
'django.middleware.csrf.CsrfViewMiddleware',
109109
'django.contrib.auth.middleware.AuthenticationMiddleware',
110110
'django.contrib.messages.middleware.MessageMiddleware',
111+
'api.middleware.VersionMiddleware',
111112
# Uncomment the next line for simple clickjacking protection:
112113
# 'django.middleware.clickjacking.XFrameOptionsMiddleware',
113114
)

0 commit comments

Comments
 (0)