From 25febfd9e081eaa275faa786102d59522ddba405 Mon Sep 17 00:00:00 2001 From: Michael Fritch Date: Mon, 26 Jul 2021 16:58:54 -0600 Subject: [PATCH] cephadm: validate fsid during shell command Signed-off-by: Michael Fritch --- src/cephadm/cephadm | 15 +++++++++++++-- src/cephadm/tests/test_cephadm.py | 8 ++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/cephadm/cephadm b/src/cephadm/cephadm index 7d7942ea780..a515cb24c54 100755 --- a/src/cephadm/cephadm +++ b/src/cephadm/cephadm @@ -1643,6 +1643,16 @@ def is_fsid(s): return True +def validate_fsid(func): + @wraps(func) + def _validate_fsid(ctx: CephadmContext): + if 'fsid' in ctx and ctx.fsid: + if not is_fsid(ctx.fsid): + raise Error('not an fsid: %s' % ctx.fsid) + return func(ctx) + return _validate_fsid + + def infer_fsid(func): """ If we only find a single fsid in /var/lib/ceph/*, use that @@ -1650,7 +1660,7 @@ def infer_fsid(func): @infer_config @wraps(func) def _infer_fsid(ctx: CephadmContext): - if ctx.fsid: + if 'fsid' in ctx and ctx.fsid: logger.debug('Using specified fsid: %s' % ctx.fsid) return func(ctx) @@ -1696,7 +1706,7 @@ def infer_config(func): if ctx.config: logger.debug('Using specified config: %s' % ctx.config) return func(ctx) - if ctx.fsid: + 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) @@ -4504,6 +4514,7 @@ def command_run(ctx): @infer_fsid @infer_config @infer_image +@validate_fsid def command_shell(ctx): # type: (CephadmContext) -> int cp = read_config(ctx.config) diff --git a/src/cephadm/tests/test_cephadm.py b/src/cephadm/tests/test_cephadm.py index d34d3c4b02d..9fe381c1260 100644 --- a/src/cephadm/tests/test_cephadm.py +++ b/src/cephadm/tests/test_cephadm.py @@ -1435,6 +1435,14 @@ class TestShell(object): assert retval == 0 assert ctx.fsid == fsid + cmd = ['shell', '--fsid', '00000000-0000-0000-0000-0000deadbeez'] + with with_cephadm_ctx(cmd) as ctx: + err = 'not an fsid' + with pytest.raises(cd.Error, match=err): + retval = cd.command_shell(ctx) + assert retval == 1 + assert ctx.fsid == None + s = get_ceph_conf(fsid=fsid) f = cephadm_fs.create_file('ceph.conf', contents=s) -- 2.39.5