From 1568875a281d56b413e75b244c9c75311cf353a0 Mon Sep 17 00:00:00 2001 From: Adam King Date: Tue, 15 Mar 2022 16:41:15 -0400 Subject: [PATCH] cephadm: verify config file exists when inferring it Fixes: https://tracker.ceph.com/issues/54571 Signed-off-by: Adam King --- src/cephadm/cephadm | 14 ++++++++++++-- src/cephadm/tests/test_cephadm.py | 20 ++++++++++++++------ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index a1c6c9d093f..0c632510f16 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -1894,16 +1894,26 @@ def infer_config(func: FuncT) -> FuncT: if ctx.config: logger.debug('Using specified config: %s' % ctx.config) return func(ctx) + + def config_path(daemon_type: str, daemon_name: str) -> str: + data_dir = get_data_dir(ctx.fsid, ctx.data_dir, daemon_type, daemon_name) + return os.path.join(data_dir, 'config') + if 'fsid' in ctx and ctx.fsid: name = ctx.name if 'name' in ctx else None if not name: daemon_list = list_daemons(ctx, detail=False) for daemon in daemon_list: - if daemon.get('name', '').startswith('mon.') and daemon.get('fsid', '') == ctx.fsid: + if ( + daemon.get('name', '').startswith('mon.') + and daemon.get('fsid', '') == ctx.fsid + and daemon.get('style', '') == 'cephadm:v1' + and os.path.exists(config_path('mon', daemon['name'].split('.', 1)[1])) + ): name = daemon['name'] break if name: - ctx.config = f'/var/lib/ceph/{ctx.fsid}/{name}/config' + ctx.config = config_path(name.split('.', 1)[0], name.split('.', 1)[1]) if ctx.config: logger.info('Inferring config %s' % ctx.config) elif os.path.exists(SHELL_DEFAULT_CONF): diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index f025fb9b2c9..ab030caa327 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -473,14 +473,21 @@ docker.io/ceph/daemon-base:octopus '00000000-0000-0000-0000-0000deadbeef', None, None, - [{'name': 'mon.a', 'fsid': '00000000-0000-0000-0000-0000deadbeef'}], + [{'name': 'mon.a', 'fsid': '00000000-0000-0000-0000-0000deadbeef', 'style': 'cephadm:v1'}], '/var/lib/ceph/00000000-0000-0000-0000-0000deadbeef/mon.a/config', ), ( '00000000-0000-0000-0000-0000deadbeef', None, None, - [{'name': 'mon.a', 'fsid': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'}], + [{'name': 'mon.a', 'fsid': 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa', 'style': 'cephadm:v1'}], + cd.SHELL_DEFAULT_CONF, + ), + ( + '00000000-0000-0000-0000-0000deadbeef', + None, + None, + [{'name': 'mon.a', 'fsid': '00000000-0000-0000-0000-0000deadbeef', 'style': 'legacy'}], cd.SHELL_DEFAULT_CONF, ), ( @@ -494,7 +501,7 @@ docker.io/ceph/daemon-base:octopus '00000000-0000-0000-0000-0000deadbeef', '/foo/bar.conf', 'mon.a', - [{'name': 'mon.a'}], + [{'name': 'mon.a', 'style': 'cephadm:v1'}], '/foo/bar.conf', ), ( @@ -520,7 +527,8 @@ docker.io/ceph/daemon-base:octopus ), ]) @mock.patch('cephadm.call') - def test_infer_config(self, _call, fsid, config, name, list_daemons, result, cephadm_fs): + @mock.patch('cephadm.logger') + def test_infer_config(self, logger, _call, fsid, config, name, list_daemons, result, cephadm_fs): # build the context ctx = cd.CephadmContext() ctx.fsid = fsid @@ -532,8 +540,8 @@ docker.io/ceph/daemon-base:octopus mock_fn.return_value = 0 infer_config = cd.infer_config(mock_fn) - # mock the shell config - cephadm_fs.create_file(cd.SHELL_DEFAULT_CONF) + # mock the config file + cephadm_fs.create_file(result) # test with mock.patch('cephadm.list_daemons', return_value=list_daemons):