diff --git a/sros2/sros2/api/__init__.py b/sros2/sros2/api/__init__.py index a3f4d7f1..2f01e74b 100644 --- a/sros2/sros2/api/__init__.py +++ b/sros2/sros2/api/__init__.py @@ -14,7 +14,9 @@ from collections import namedtuple import datetime +import errno import os +import pathlib import shutil import sys @@ -324,9 +326,13 @@ def create_key(keystore_path, identity): def list_keys(keystore_path): - for name in os.listdir(keystore_path): - if os.path.isdir(os.path.join(keystore_path, name)): - print(name) + if not os.path.isdir(keystore_path): + raise FileNotFoundError( + errno.ENOENT, os.strerror(errno.ENOENT), keystore_path) + p = pathlib.Path(keystore_path) + key_file_paths = sorted(p.glob('**/key.pem')) + for key_file_path in key_file_paths: + print('/{}'.format(key_file_path.parent.relative_to(keystore_path).as_posix())) return True diff --git a/sros2/test/sros2/commands/security/verbs/test_list_keys.py b/sros2/test/sros2/commands/security/verbs/test_list_keys.py index a38f6622..efaff95c 100644 --- a/sros2/test/sros2/commands/security/verbs/test_list_keys.py +++ b/sros2/test/sros2/commands/security/verbs/test_list_keys.py @@ -20,17 +20,19 @@ def test_list_keys(capsys): + key_names = ['/test_node', '/test_namespace/test_node', '/sky/is/the/limit'] with tempfile.TemporaryDirectory() as keystore_dir: with capsys.disabled(): # First, create the keystore assert create_keystore(keystore_dir) # Now using that keystore, create a keypair - assert create_key(keystore_dir, '/test_node') + for key in key_names: + assert create_key(keystore_dir, key) # Now verify that the key we just created is included in the list assert cli.main(argv=['security', 'list_keys', keystore_dir]) == 0 - assert capsys.readouterr().out.strip() == 'test_node' + assert capsys.readouterr().out.strip() == '\n'.join(sorted(key_names)) def test_list_keys_no_keys(capsys):