2013-07-18 20:42:21 +00:00
|
|
|
# coding: utf-8
|
2014-08-24 17:13:45 +00:00
|
|
|
|
2014-11-21 19:05:13 +00:00
|
|
|
# Import python libs
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2017-03-22 21:09:56 +00:00
|
|
|
# Import salt libs
|
2017-12-22 19:54:53 +00:00
|
|
|
import salt.utils.json
|
Use explicit unicode strings + break up salt.utils
This PR is part of what will be an ongoing effort to use explicit
unicode strings in Salt. Because Python 3 does not suport Python 2's raw
unicode string syntax (i.e. `ur'\d+'`), we must use
`salt.utils.locales.sdecode()` to ensure that the raw string is unicode.
However, because of how `salt/utils/__init__.py` has evolved into the
hulking monstrosity it is today, this means importing a large module in
places where it is not needed, which could negatively impact
performance. For this reason, this PR also breaks out some of the
functions from `salt/utils/__init__.py` into new/existing modules under
`salt/utils/`. The long term goal will be that the modules within this
directory do not depend on importing `salt.utils`.
A summary of the changes in this PR is as follows:
* Moves the following functions from `salt.utils` to new locations
(including a deprecation warning if invoked from `salt.utils`):
`to_bytes`, `to_str`, `to_unicode`, `str_to_num`, `is_quoted`,
`dequote`, `is_hex`, `is_bin_str`, `rand_string`,
`contains_whitespace`, `clean_kwargs`, `invalid_kwargs`, `which`,
`which_bin`, `path_join`, `shlex_split`, `rand_str`, `is_windows`,
`is_proxy`, `is_linux`, `is_darwin`, `is_sunos`, `is_smartos`,
`is_smartos_globalzone`, `is_smartos_zone`, `is_freebsd`, `is_netbsd`,
`is_openbsd`, `is_aix`
* Moves the functions already deprecated by @rallytime to the bottom of
`salt/utils/__init__.py` for better organization, so we can keep the
deprecated ones separate from the ones yet to be deprecated as we
continue to break up `salt.utils`
* Updates `salt/*.py` and all files under `salt/client/` to use explicit
unicode string literals.
* Gets rid of implicit imports of `salt.utils` (e.g. `from salt.utils
import foo` becomes `import salt.utils.foo as foo`).
* Renames the `test.rand_str` function to `test.random_hash` to more
accurately reflect what it does
* Modifies `salt.utils.stringutils.random()` (née `salt.utils.rand_string()`)
such that it returns a string matching the passed size. Previously
this function would get `size` bytes from `os.urandom()`,
base64-encode it, and return the result, which would in most cases not
be equal to the passed size.
2017-07-25 01:47:15 +00:00
|
|
|
import salt.utils.stringutils
|
2017-03-22 21:09:56 +00:00
|
|
|
|
2017-02-24 18:06:53 +00:00
|
|
|
# Import test support libs
|
|
|
|
import tests.support.cherrypy_testclasses as cptc
|
2013-07-18 20:42:21 +00:00
|
|
|
|
2014-06-19 12:00:38 +00:00
|
|
|
# Import 3rd-party libs
|
2017-02-24 18:06:53 +00:00
|
|
|
from salt.ext.six.moves.urllib.parse import urlencode # pylint: disable=no-name-in-module,import-error
|
|
|
|
|
|
|
|
|
|
|
|
class TestAuth(cptc.BaseRestCherryPyTest):
|
2013-07-18 20:42:21 +00:00
|
|
|
def test_get_root_noauth(self):
|
|
|
|
'''
|
|
|
|
GET requests to the root URL should not require auth
|
|
|
|
'''
|
|
|
|
request, response = self.request('/')
|
|
|
|
self.assertEqual(response.status, '200 OK')
|
|
|
|
|
|
|
|
def test_post_root_auth(self):
|
|
|
|
'''
|
|
|
|
POST requests to the root URL redirect to login
|
|
|
|
'''
|
2014-06-27 08:07:45 +00:00
|
|
|
request, response = self.request('/', method='POST', data={})
|
|
|
|
self.assertEqual(response.status, '401 Unauthorized')
|
2013-07-18 20:42:21 +00:00
|
|
|
|
|
|
|
def test_login_noauth(self):
|
|
|
|
'''
|
|
|
|
GET requests to the login URL should not require auth
|
|
|
|
'''
|
|
|
|
request, response = self.request('/login')
|
|
|
|
self.assertEqual(response.status, '200 OK')
|
|
|
|
|
2014-04-08 19:48:57 +00:00
|
|
|
def test_webhook_auth(self):
|
|
|
|
'''
|
|
|
|
Requests to the webhook URL require auth by default
|
|
|
|
'''
|
2014-06-27 08:07:45 +00:00
|
|
|
request, response = self.request('/hook', method='POST', data={})
|
|
|
|
self.assertEqual(response.status, '401 Unauthorized')
|
2014-04-08 19:48:57 +00:00
|
|
|
|
2014-06-19 12:00:38 +00:00
|
|
|
|
2017-02-24 18:06:53 +00:00
|
|
|
class TestLogin(cptc.BaseRestCherryPyTest):
|
2013-07-18 20:42:21 +00:00
|
|
|
auth_creds = (
|
|
|
|
('username', 'saltdev'),
|
|
|
|
('password', 'saltdev'),
|
|
|
|
('eauth', 'auto'))
|
|
|
|
|
|
|
|
def test_good_login(self):
|
|
|
|
'''
|
|
|
|
Test logging in
|
|
|
|
'''
|
2014-11-19 22:24:33 +00:00
|
|
|
body = urlencode(self.auth_creds)
|
2013-07-18 20:42:21 +00:00
|
|
|
request, response = self.request('/login', method='POST', body=body,
|
|
|
|
headers={
|
|
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
|
|
})
|
|
|
|
self.assertEqual(response.status, '200 OK')
|
2014-08-15 12:30:48 +00:00
|
|
|
return response
|
2013-07-18 20:42:21 +00:00
|
|
|
|
|
|
|
def test_bad_login(self):
|
|
|
|
'''
|
|
|
|
Test logging in
|
|
|
|
'''
|
2014-11-19 22:24:33 +00:00
|
|
|
body = urlencode({'totally': 'invalid_creds'})
|
2013-07-18 20:42:21 +00:00
|
|
|
request, response = self.request('/login', method='POST', body=body,
|
|
|
|
headers={
|
|
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
|
|
})
|
|
|
|
self.assertEqual(response.status, '401 Unauthorized')
|
2014-04-08 19:48:57 +00:00
|
|
|
|
2014-08-15 12:30:48 +00:00
|
|
|
def test_logout(self):
|
|
|
|
ret = self.test_good_login()
|
|
|
|
token = ret.headers['X-Auth-Token']
|
|
|
|
|
2014-11-19 22:24:33 +00:00
|
|
|
body = urlencode({})
|
2014-08-15 12:30:48 +00:00
|
|
|
request, response = self.request('/logout', method='POST', body=body,
|
|
|
|
headers={
|
|
|
|
'content-type': 'application/x-www-form-urlencoded',
|
|
|
|
'X-Auth-Token': token,
|
|
|
|
})
|
|
|
|
self.assertEqual(response.status, '200 OK')
|
|
|
|
|
2014-06-19 12:00:38 +00:00
|
|
|
|
2017-02-24 18:06:53 +00:00
|
|
|
class TestRun(cptc.BaseRestCherryPyTest):
|
2014-07-14 23:11:08 +00:00
|
|
|
auth_creds = (
|
2015-09-08 21:33:41 +00:00
|
|
|
('username', 'saltdev_auto'),
|
2014-07-14 23:11:08 +00:00
|
|
|
('password', 'saltdev'),
|
|
|
|
('eauth', 'auto'))
|
|
|
|
|
|
|
|
low = (
|
|
|
|
('client', 'local'),
|
|
|
|
('tgt', '*'),
|
|
|
|
('fun', 'test.ping'),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_run_good_login(self):
|
|
|
|
'''
|
|
|
|
Test the run URL with good auth credentials
|
|
|
|
'''
|
|
|
|
cmd = dict(self.low, **dict(self.auth_creds))
|
2014-11-19 22:24:33 +00:00
|
|
|
body = urlencode(cmd)
|
2014-07-14 23:11:08 +00:00
|
|
|
|
2015-09-08 21:33:41 +00:00
|
|
|
request, response = self.request('/run', method='POST', body=body,
|
|
|
|
headers={
|
|
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
|
|
})
|
2014-07-14 23:11:08 +00:00
|
|
|
self.assertEqual(response.status, '200 OK')
|
|
|
|
|
|
|
|
def test_run_bad_login(self):
|
|
|
|
'''
|
|
|
|
Test the run URL with bad auth credentials
|
|
|
|
'''
|
|
|
|
cmd = dict(self.low, **{'totally': 'invalid_creds'})
|
2014-11-19 22:24:33 +00:00
|
|
|
body = urlencode(cmd)
|
2014-07-14 23:11:08 +00:00
|
|
|
|
2015-09-08 21:33:41 +00:00
|
|
|
request, response = self.request('/run', method='POST', body=body,
|
|
|
|
headers={
|
|
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
|
|
})
|
2014-07-14 23:11:08 +00:00
|
|
|
self.assertEqual(response.status, '401 Unauthorized')
|
|
|
|
|
|
|
|
|
2017-02-24 18:06:53 +00:00
|
|
|
class TestWebhookDisableAuth(cptc.BaseRestCherryPyTest):
|
2017-04-18 10:16:09 +00:00
|
|
|
|
|
|
|
def __get_opts__(self):
|
|
|
|
return {
|
|
|
|
'rest_cherrypy': {
|
|
|
|
'port': 8000,
|
|
|
|
'debug': True,
|
|
|
|
'webhook_disable_auth': True,
|
|
|
|
},
|
|
|
|
}
|
2014-04-08 19:48:57 +00:00
|
|
|
|
|
|
|
def test_webhook_noauth(self):
|
|
|
|
'''
|
|
|
|
Auth can be disabled for requests to the webhook URL
|
|
|
|
'''
|
2014-11-19 22:24:33 +00:00
|
|
|
body = urlencode({'foo': 'Foo!'})
|
2014-04-08 19:48:57 +00:00
|
|
|
request, response = self.request('/hook', method='POST', body=body,
|
|
|
|
headers={
|
|
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
|
|
})
|
|
|
|
self.assertEqual(response.status, '200 OK')
|
2016-05-05 14:31:08 +00:00
|
|
|
|
|
|
|
|
2017-02-24 18:06:53 +00:00
|
|
|
class TestArgKwarg(cptc.BaseRestCherryPyTest):
|
2016-05-05 14:31:08 +00:00
|
|
|
auth_creds = (
|
|
|
|
('username', 'saltdev'),
|
|
|
|
('password', 'saltdev'),
|
|
|
|
('eauth', 'auto'))
|
|
|
|
|
|
|
|
low = (
|
|
|
|
('client', 'runner'),
|
|
|
|
('fun', 'test.arg'),
|
|
|
|
# use singular form for arg and kwarg
|
|
|
|
('arg', [1234]),
|
|
|
|
('kwarg', {'ext_source': 'redis'}),
|
|
|
|
)
|
|
|
|
|
|
|
|
def _token(self):
|
|
|
|
'''
|
|
|
|
Return the token
|
|
|
|
'''
|
|
|
|
body = urlencode(self.auth_creds)
|
|
|
|
request, response = self.request(
|
|
|
|
'/login',
|
|
|
|
method='POST',
|
|
|
|
body=body,
|
|
|
|
headers={
|
|
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return response.headers['X-Auth-Token']
|
|
|
|
|
|
|
|
def test_accepts_arg_kwarg_keys(self):
|
|
|
|
'''
|
|
|
|
Ensure that (singular) arg and kwarg keys (for passing parameters)
|
|
|
|
are supported by runners.
|
|
|
|
'''
|
|
|
|
cmd = dict(self.low)
|
2017-12-22 19:54:53 +00:00
|
|
|
body = salt.utils.json.dumps(cmd)
|
2016-05-05 14:31:08 +00:00
|
|
|
|
|
|
|
request, response = self.request(
|
|
|
|
'/',
|
|
|
|
method='POST',
|
|
|
|
body=body,
|
|
|
|
headers={
|
|
|
|
'content-type': 'application/json',
|
|
|
|
'X-Auth-Token': self._token(),
|
|
|
|
'Accept': 'application/json',
|
|
|
|
}
|
|
|
|
)
|
2017-12-22 19:54:53 +00:00
|
|
|
resp = salt.utils.json.loads(salt.utils.stringutils.to_str(response.body[0]))
|
2016-05-05 14:31:08 +00:00
|
|
|
self.assertEqual(resp['return'][0]['args'], [1234])
|
|
|
|
self.assertEqual(resp['return'][0]['kwargs'],
|
|
|
|
{'ext_source': 'redis'})
|
2017-05-05 18:42:36 +00:00
|
|
|
|
|
|
|
|
2017-05-11 15:09:40 +00:00
|
|
|
class TestJobs(cptc.BaseRestCherryPyTest):
|
2017-05-05 18:42:36 +00:00
|
|
|
auth_creds = (
|
|
|
|
('username', 'saltdev_auto'),
|
|
|
|
('password', 'saltdev'),
|
|
|
|
('eauth', 'auto'))
|
|
|
|
|
|
|
|
low = (
|
|
|
|
('client', 'local'),
|
|
|
|
('tgt', '*'),
|
|
|
|
('fun', 'test.ping'),
|
|
|
|
)
|
|
|
|
|
|
|
|
def _token(self):
|
|
|
|
'''
|
|
|
|
Return the token
|
|
|
|
'''
|
|
|
|
body = urlencode(self.auth_creds)
|
|
|
|
request, response = self.request(
|
|
|
|
'/login',
|
|
|
|
method='POST',
|
|
|
|
body=body,
|
|
|
|
headers={
|
|
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return response.headers['X-Auth-Token']
|
|
|
|
|
|
|
|
def _add_job(self):
|
|
|
|
'''
|
|
|
|
Helper function to add a job to the job cache
|
|
|
|
'''
|
|
|
|
cmd = dict(self.low, **dict(self.auth_creds))
|
|
|
|
body = urlencode(cmd)
|
|
|
|
|
|
|
|
request, response = self.request('/run', method='POST', body=body,
|
|
|
|
headers={
|
|
|
|
'content-type': 'application/x-www-form-urlencoded'
|
|
|
|
})
|
|
|
|
self.assertEqual(response.status, '200 OK')
|
|
|
|
|
|
|
|
def test_all_jobs(self):
|
|
|
|
'''
|
|
|
|
test query to /jobs returns job data
|
|
|
|
'''
|
|
|
|
self._add_job()
|
|
|
|
|
|
|
|
request, response = self.request('/jobs', method='GET',
|
|
|
|
headers={
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'X-Auth-Token': self._token(),
|
|
|
|
})
|
|
|
|
|
2017-12-22 19:54:53 +00:00
|
|
|
resp = salt.utils.json.loads(salt.utils.stringutils.to_str(response.body[0]))
|
2017-05-05 18:42:36 +00:00
|
|
|
self.assertIn('test.ping', str(resp['return']))
|
|
|
|
self.assertEqual(response.status, '200 OK')
|