PageRenderTime 502ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/qa/tasks/cephfs/test_multifs_auth.py

https://github.com/ceph/ceph
Python | 308 lines | 191 code | 67 blank | 50 comment | 18 complexity | fc9b34c512fcec5910d899357a347ea2 MD5 | raw file
  1. """
  2. Test for Ceph clusters with multiple FSs.
  3. """
  4. import logging
  5. from os.path import join as os_path_join
  6. # CapsHelper is subclassed from CephFSTestCase
  7. from tasks.cephfs.caps_helper import CapsHelper
  8. from teuthology.exceptions import CommandFailedError
  9. log = logging.getLogger(__name__)
  10. class TestMultiFS(CapsHelper):
  11. client_id = 'testuser'
  12. client_name = 'client.' + client_id
  13. # one dedicated for each FS
  14. MDSS_REQUIRED = 2
  15. CLIENTS_REQUIRED = 2
  16. def setUp(self):
  17. super(TestMultiFS, self).setUp()
  18. # we might have it - the client - if the same cluster was used for a
  19. # different vstart_runner.py run.
  20. self.run_cluster_cmd(f'auth rm {self.client_name}')
  21. self.fs1 = self.fs
  22. self.fs2 = self.mds_cluster.newfs(name='cephfs2', create=True)
  23. # we'll reassign caps to client.1 so that it can operate with cephfs2
  24. self.run_cluster_cmd(f'auth caps client.{self.mount_b.client_id} mon '
  25. f'"allow r" osd "allow rw '
  26. f'pool={self.fs2.get_data_pool_name()}" mds allow')
  27. self.mount_b.remount(cephfs_name=self.fs2.name)
  28. class TestMONCaps(TestMultiFS):
  29. def test_moncap_with_one_fs_names(self):
  30. moncap = f'allow r fsname={self.fs1.name}'
  31. keyring = self.setup_test_env(moncap)
  32. self.run_mon_cap_tests(moncap, keyring)
  33. def test_moncap_with_multiple_fs_names(self):
  34. moncap = (f'allow r fsname={self.fs1.name}, '
  35. f'allow r fsname={self.fs2.name}')
  36. keyring = self.setup_test_env(moncap)
  37. self.run_mon_cap_tests(moncap, keyring)
  38. def test_moncap_with_blanket_allow(self):
  39. moncap = 'allow r'
  40. keyring = self.setup_test_env(moncap)
  41. self.run_mon_cap_tests(moncap, keyring)
  42. def setup_test_env(self, moncap):
  43. return self.create_client(self.client_id, moncap)
  44. #TODO: add tests for capsecs 'p' and 's'.
  45. class TestMDSCaps(TestMultiFS):
  46. """
  47. 0. Have 2 FSs on Ceph cluster.
  48. 1. Create new files on both FSs.
  49. 2. Create a new client that has authorization for both FSs.
  50. 3. Remount the current mounts with this new client.
  51. 4. Test read and write on both FSs.
  52. """
  53. def test_rw_with_fsname_and_no_path_in_cap(self):
  54. perm = 'rw'
  55. filepaths, filedata, mounts = self.setup_test_env(perm, True)
  56. self.run_mds_cap_tests(filepaths, filedata, mounts, perm)
  57. def test_r_with_fsname_and_no_path_in_cap(self):
  58. perm = 'r'
  59. filepaths, filedata, mounts = self.setup_test_env(perm, True)
  60. self.run_mds_cap_tests(filepaths, filedata, mounts, perm)
  61. def test_rw_with_fsname_and_path_in_cap(self):
  62. perm = 'rw'
  63. filepaths, filedata, mounts = self.setup_test_env(perm, True,'dir1')
  64. self.run_mds_cap_tests(filepaths, filedata, mounts, perm)
  65. def test_r_with_fsname_and_path_in_cap(self):
  66. perm = 'r'
  67. filepaths, filedata, mounts = self.setup_test_env(perm, True, 'dir1')
  68. self.run_mds_cap_tests(filepaths, filedata, mounts, perm)
  69. # XXX: this tests the backward compatibility; "allow rw path=<dir1>" is
  70. # treated as "allow rw fsname=* path=<dir1>"
  71. def test_rw_with_no_fsname_and_path_in_cap(self):
  72. perm = 'rw'
  73. filepaths, filedata, mounts = self.setup_test_env(perm, False, 'dir1')
  74. self.run_mds_cap_tests(filepaths, filedata, mounts, perm)
  75. # XXX: this tests the backward compatibility; "allow r path=<dir1>" is
  76. # treated as "allow r fsname=* path=<dir1>"
  77. def test_r_with_no_fsname_and_path_in_cap(self):
  78. perm = 'r'
  79. filepaths, filedata, mounts = self.setup_test_env(perm, False, 'dir1')
  80. self.run_mds_cap_tests(filepaths, filedata, mounts, perm)
  81. def test_rw_with_no_fsname_and_no_path(self):
  82. perm = 'rw'
  83. filepaths, filedata, mounts = self.setup_test_env(perm)
  84. self.run_mds_cap_tests(filepaths, filedata, mounts, perm)
  85. def test_r_with_no_fsname_and_no_path(self):
  86. perm = 'r'
  87. filepaths, filedata, mounts = self.setup_test_env(perm)
  88. self.run_mds_cap_tests(filepaths, filedata, mounts, perm)
  89. def tearDown(self):
  90. self.mount_a.umount_wait()
  91. self.mount_b.umount_wait()
  92. super(type(self), self).tearDown()
  93. def setup_test_env(self, perm, fsname=False, cephfs_mntpt='/'):
  94. """
  95. Creates the cap string, files on both the FSs and then creates the
  96. new client with the cap and remounts both the FSs with newly created
  97. client.
  98. """
  99. filenames = ('file_on_fs1', 'file_on_fs2')
  100. filedata = ('some data on first fs', 'some data on second fs')
  101. mounts = (self.mount_a, self.mount_b)
  102. self.setup_fs_contents(cephfs_mntpt, filenames, filedata)
  103. keyring_paths = self.create_client_and_keyring_file(perm, fsname,
  104. cephfs_mntpt)
  105. filepaths = self.remount_with_new_client(cephfs_mntpt, filenames,
  106. keyring_paths)
  107. return filepaths, filedata, mounts
  108. def generate_caps(self, perm, fsname, cephfs_mntpt):
  109. moncap = 'allow r'
  110. osdcap = (f'allow {perm} tag cephfs data={self.fs1.name}, '
  111. f'allow {perm} tag cephfs data={self.fs2.name}')
  112. if fsname:
  113. if cephfs_mntpt == '/':
  114. mdscap = (f'allow {perm} fsname={self.fs1.name}, '
  115. f'allow {perm} fsname={self.fs2.name}')
  116. else:
  117. mdscap = (f'allow {perm} fsname={self.fs1.name} '
  118. f'path=/{cephfs_mntpt}, '
  119. f'allow {perm} fsname={self.fs2.name} '
  120. f'path=/{cephfs_mntpt}')
  121. else:
  122. if cephfs_mntpt == '/':
  123. mdscap = f'allow {perm}'
  124. else:
  125. mdscap = f'allow {perm} path=/{cephfs_mntpt}'
  126. return moncap, osdcap, mdscap
  127. def create_client_and_keyring_file(self, perm, fsname, cephfs_mntpt):
  128. moncap, osdcap, mdscap = self.generate_caps(perm, fsname,
  129. cephfs_mntpt)
  130. keyring = self.create_client(self.client_id, moncap, osdcap, mdscap)
  131. keyring_paths = []
  132. for mount_x in (self.mount_a, self.mount_b):
  133. keyring_paths.append(mount_x.client_remote.mktemp(data=keyring))
  134. return keyring_paths
  135. def setup_fs_contents(self, cephfs_mntpt, filenames, filedata):
  136. filepaths = []
  137. iter_on = zip((self.mount_a, self.mount_b), filenames, filedata)
  138. for mount_x, filename, data in iter_on:
  139. if cephfs_mntpt != '/' :
  140. mount_x.run_shell(args=['mkdir', cephfs_mntpt])
  141. filepaths.append(os_path_join(mount_x.hostfs_mntpt,
  142. cephfs_mntpt, filename))
  143. else:
  144. filepaths.append(os_path_join(mount_x.hostfs_mntpt, filename))
  145. mount_x.write_file(filepaths[-1], data)
  146. def remount_with_new_client(self, cephfs_mntpt, filenames,
  147. keyring_paths):
  148. if isinstance(cephfs_mntpt, str) and cephfs_mntpt != '/' :
  149. cephfs_mntpt = '/' + cephfs_mntpt
  150. self.mount_a.remount(client_id=self.client_id,
  151. client_keyring_path=keyring_paths[0],
  152. client_remote=self.mount_a.client_remote,
  153. cephfs_name=self.fs1.name,
  154. cephfs_mntpt=cephfs_mntpt,
  155. hostfs_mntpt=self.mount_a.hostfs_mntpt,
  156. wait=True)
  157. self.mount_b.remount(client_id=self.client_id,
  158. client_keyring_path=keyring_paths[1],
  159. client_remote=self.mount_b.client_remote,
  160. cephfs_name=self.fs2.name,
  161. cephfs_mntpt=cephfs_mntpt,
  162. hostfs_mntpt=self.mount_b.hostfs_mntpt,
  163. wait=True)
  164. return (os_path_join(self.mount_a.hostfs_mntpt, filenames[0]),
  165. os_path_join(self.mount_b.hostfs_mntpt, filenames[1]))
  166. class TestClientsWithoutAuth(TestMultiFS):
  167. def setUp(self):
  168. super(TestClientsWithoutAuth, self).setUp()
  169. # TODO: When MON and OSD caps for a Ceph FS are assigned to a
  170. # client but MDS caps are not, mount.ceph prints "permission
  171. # denied". But when MON caps are not assigned and MDS and OSD
  172. # caps are, mount.ceph prints "no mds server or cluster laggy"
  173. # instead of "permission denied".
  174. #
  175. # Before uncommenting the following line a fix would be required
  176. # for latter case to change "no mds server is up or the cluster is
  177. # laggy" to "permission denied".
  178. self.kernel_errmsgs = ('permission denied', 'no mds server is up or '
  179. 'the cluster is laggy', 'no such file or '
  180. 'directory',
  181. 'input/output error')
  182. # TODO: When MON and OSD caps are assigned for a Ceph FS to a
  183. # client but MDS caps are not, ceph-fuse prints "operation not
  184. # permitted". But when MON caps are not assigned and MDS and OSD
  185. # caps are, ceph-fuse prints "no such file or directory" instead
  186. # of "operation not permitted".
  187. #
  188. # Before uncommenting the following line a fix would be required
  189. # for the latter case to change "no such file or directory" to
  190. # "operation not permitted".
  191. #self.assertIn('operation not permitted', retval[2].lower())
  192. self.fuse_errmsgs = ('operation not permitted', 'no such file or '
  193. 'directory')
  194. if 'kernel' in str(type(self.mount_a)).lower():
  195. self.errmsgs = self.kernel_errmsgs
  196. elif 'fuse' in str(type(self.mount_a)).lower():
  197. self.errmsgs = self.fuse_errmsgs
  198. else:
  199. raise RuntimeError('strange, the client was neither based on '
  200. 'kernel nor FUSE.')
  201. def check_that_mount_failed_for_right_reason(self, stderr):
  202. stderr = stderr.lower()
  203. for errmsg in self.errmsgs:
  204. if errmsg in stderr:
  205. break
  206. else:
  207. raise AssertionError('can\'t find expected set of words in the '
  208. f'stderr\nself.errmsgs - {self.errmsgs}\n'
  209. f'stderr - {stderr}')
  210. def test_mount_all_caps_absent(self):
  211. # setup part...
  212. keyring = self.fs1.authorize(self.client_id, ('/', 'rw'))
  213. keyring_path = self.mount_a.client_remote.mktemp(data=keyring)
  214. # mount the FS for which client has no auth...
  215. retval = self.mount_a.remount(client_id=self.client_id,
  216. client_keyring_path=keyring_path,
  217. cephfs_name=self.fs2.name,
  218. check_status=False)
  219. # tests...
  220. self.assertIsInstance(retval, tuple)
  221. self.assertEqual(len(retval), 3)
  222. self.assertIsInstance(retval[0], CommandFailedError)
  223. self.check_that_mount_failed_for_right_reason(retval[2])
  224. def test_mount_mon_and_osd_caps_present_mds_caps_absent(self):
  225. # setup part...
  226. moncap = f'allow rw fsname={self.fs1.name}, allow rw fsname={self.fs2.name}'
  227. mdscap = f'allow rw fsname={self.fs1.name}'
  228. osdcap = (f'allow rw tag cephfs data={self.fs1.name}, allow rw tag '
  229. f'cephfs data={self.fs2.name}')
  230. keyring = self.create_client(self.client_id, moncap, osdcap, mdscap)
  231. keyring_path = self.mount_a.client_remote.mktemp(data=keyring)
  232. # mount the FS for which client has no auth...
  233. retval = self.mount_a.remount(client_id=self.client_id,
  234. client_keyring_path=keyring_path,
  235. cephfs_name=self.fs2.name,
  236. check_status=False)
  237. # tests...
  238. self.assertIsInstance(retval, tuple)
  239. self.assertEqual(len(retval), 3)
  240. self.assertIsInstance(retval[0], CommandFailedError)
  241. self.check_that_mount_failed_for_right_reason(retval[2])