Skip to content

Commit

Permalink
add X-Salt-Version header to cherrypy/saltnado
Browse files Browse the repository at this point in the history
in order for pepper to have some way to determine whether features are
available or bugs are fixed in the master it is talking to, the version
needs to be exposed, this does that.
  • Loading branch information
mattp- committed Oct 19, 2018
1 parent a2e09d7 commit 66bfc6d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 14 deletions.
13 changes: 13 additions & 0 deletions salt/netapi/rest_cherrypy/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,15 @@ def lowdata_fmt():
cherrypy.serving.request.lowstate = data


def default_headers():
'''
set default headers that should be attached to every request
'''

headers = cherrypy.response.headers
headers['X-Salt-Version'] = str(salt.version.__saltstack_version__)


tools_config = {
'on_start_resource': [
('html_override', html_override_tool),
Expand All @@ -1107,6 +1116,9 @@ def lowdata_fmt():
('hypermedia_out', hypermedia_out),
('salt_ip_verify', salt_ip_verify_tool),
],
'before_finalize': [
('default_headers', default_headers),
],
}

for hook, tool_list in tools_config.items():
Expand Down Expand Up @@ -2862,6 +2874,7 @@ def get_conf(self):

'tools.html_override.on': True,
'tools.cors_tool.on': True,
'tools.default_headers.on': True,
},
}

Expand Down
4 changes: 3 additions & 1 deletion salt/netapi/rest_tornado/saltnado.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ def _get_lowstate(self):

def set_default_headers(self):
'''
Set default CORS headers
Set default headers
'''
mod_opts = self.application.mod_opts

Expand All @@ -596,6 +596,8 @@ def set_default_headers(self):
if allowed_origin:
self.set_header("Access-Control-Allow-Origin", allowed_origin)

self.set_header('X-Salt-Version', str(salt.version.__saltstack_version__))

def options(self, *args, **kwargs):
'''
Return CORS headers for preflight requests
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/netapi/rest_cherrypy/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Import salt libs
import salt.utils.json
import salt.utils.stringutils
import salt.version

# Import test support libs
import tests.support.cherrypy_testclasses as cptc
Expand Down Expand Up @@ -61,6 +62,7 @@ def test_good_login(self):
'content-type': 'application/x-www-form-urlencoded'
})
self.assertEqual(response.status, '200 OK')
self.assertTrue(salt.version.SaltStackVersion.parse(response.headers['X-Salt-Version']))
return response

def test_bad_login(self):
Expand All @@ -72,6 +74,7 @@ def test_bad_login(self):
headers={
'content-type': 'application/x-www-form-urlencoded'
})
self.assertTrue(salt.version.SaltStackVersion.parse(response.headers['X-Salt-Version']))
self.assertEqual(response.status, '401 Unauthorized')

def test_logout(self):
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/netapi/rest_tornado/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Import Salt Libs
import salt.utils.json
import salt.utils.stringutils
import salt.version
from salt.netapi.rest_tornado import saltnado
from salt.utils.versions import StrictVersion

Expand Down Expand Up @@ -59,6 +60,7 @@ def test_root(self):
self.assertEqual(sorted(response_obj['clients']),
['local', 'local_async', 'runner', 'runner_async'])
self.assertEqual(response_obj['return'], 'Welcome')
self.assertTrue(salt.version.SaltStackVersion.parse(response.headers['X-Salt-Version']))

def test_post_no_auth(self):
'''
Expand Down
35 changes: 22 additions & 13 deletions tests/unit/netapi/test_rest_tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ def test_deserialize(self):
"client": "local",
"tgt": "*",
"fun": "test.fib",
"arg": ["10"]
"arg": [10]
},
{
"client": "runner",
Expand Down Expand Up @@ -305,7 +305,7 @@ def test_deserialize(self):
self.assertEqual(returned_lowstate['client'], 'local')
self.assertEqual(returned_lowstate['tgt'], '*')
self.assertEqual(returned_lowstate['fun'], 'test.fib')
self.assertEqual(returned_lowstate['arg'], ['10', 'foo'])
self.assertEqual(returned_lowstate['arg'], [10, 'foo'])

# Send json with utf8 charset
response = self.fetch('/',
Expand All @@ -318,49 +318,58 @@ def test_get_lowstate(self):
'''
Test transformations low data of the function _get_lowstate
'''
valid_lowstate = [{
valid_arg_lowstate = [{
u"client": u"local",
u"tgt": u"*",
u"fun": u"test.fib",
u"arg": [10],
u"kwarg": {},
}]

valid_arg_kwarg_lowstate = [{
u"client": u"local",
u"tgt": u"*",
u"fun": u"test.fib",
u"arg": [u"10"]
u"arg": [10],
u"kwarg": {u"yaml": {u"foo": u"bar"}}
}]

# Case 1. dictionary type of lowstate
request_lowstate = {
"client": "local",
"tgt": "*",
"fun": "test.fib",
"arg": ["10"]
u"arg": ["10", "yaml={foo: bar}"]
}

response = self.fetch('/',
method='POST',
body=salt.utils.json.dumps(request_lowstate),
headers={'Content-Type': self.content_type_map['json']})

self.assertEqual(valid_lowstate, salt.utils.json.loads(response.body)['lowstate'])
self.assertEqual(valid_arg_kwarg_lowstate, salt.utils.json.loads(response.body)['lowstate'])

# Case 2. string type of arg
request_lowstate = {
"client": "local",
"tgt": "*",
"fun": "test.fib",
"arg": "10"
u"arg": "10"
}

response = self.fetch('/',
method='POST',
body=salt.utils.json.dumps(request_lowstate),
headers={'Content-Type': self.content_type_map['json']})

self.assertEqual(valid_lowstate, salt.utils.json.loads(response.body)['lowstate'])
self.assertEqual(valid_arg_lowstate, salt.utils.json.loads(response.body)['lowstate'])

# Case 3. Combine Case 1 and Case 2.
request_lowstate = {
"client": "local",
"tgt": "*",
"fun": "test.fib",
"arg": "10"
u"arg": ["10", "yaml={foo: bar}"]
}

# send as json
Expand All @@ -369,21 +378,21 @@ def test_get_lowstate(self):
body=salt.utils.json.dumps(request_lowstate),
headers={'Content-Type': self.content_type_map['json']})

self.assertEqual(valid_lowstate, salt.utils.json.loads(response.body)['lowstate'])
self.assertEqual(valid_arg_kwarg_lowstate, salt.utils.json.loads(response.body)['lowstate'])

# send as yaml
response = self.fetch('/',
method='POST',
body=salt.utils.yaml.safe_dump(request_lowstate),
headers={'Content-Type': self.content_type_map['yaml']})
self.assertEqual(valid_lowstate, salt.utils.json.loads(response.body)['lowstate'])
self.assertEqual(valid_arg_kwarg_lowstate, salt.utils.json.loads(response.body)['lowstate'])

# send as plain text
response = self.fetch('/',
method='POST',
body=salt.utils.json.dumps(request_lowstate),
headers={'Content-Type': self.content_type_map['text']})
self.assertEqual(valid_lowstate, salt.utils.json.loads(response.body)['lowstate'])
self.assertEqual(valid_arg_kwarg_lowstate, salt.utils.json.loads(response.body)['lowstate'])

# send as form-urlencoded
request_form_lowstate = (
Expand All @@ -397,7 +406,7 @@ def test_get_lowstate(self):
method='POST',
body=urlencode(request_form_lowstate),
headers={'Content-Type': self.content_type_map['form']})
self.assertEqual(valid_lowstate, salt.utils.json.loads(response.body)['lowstate'])
self.assertEqual(valid_arg_lowstate, salt.utils.json.loads(response.body)['lowstate'])

def test_cors_origin_wildcard(self):
'''
Expand Down

0 comments on commit 66bfc6d

Please sign in to comment.