|
| 1 | +# Copyright 2020 Canonical, Ltd. |
| 2 | +# Copyright 2021 Open Source Robotics Foundation, Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +import pathlib |
| 18 | +from tempfile import TemporaryDirectory |
| 19 | +from typing import Optional |
| 20 | +from typing import Tuple |
| 21 | + |
| 22 | +import launch |
| 23 | +from ros2launch.option import OptionExtension |
| 24 | +import sros2.keystore._keystore |
| 25 | + |
| 26 | + |
| 27 | +class NoKeystoreProvidedError(Exception): |
| 28 | + """Exception raised when a keystore is not provided.""" |
| 29 | + |
| 30 | + def __init__(self): |
| 31 | + super().__init__(('--no-create-keystore was specified and ' |
| 32 | + 'no keystore was provided')) |
| 33 | + |
| 34 | + |
| 35 | +class NonexistentKeystoreError(Exception): |
| 36 | + """Exception raised when keystore is not on disk.""" |
| 37 | + |
| 38 | + def __init__(self, keystore_path: pathlib.Path): |
| 39 | + super().__init__(('--no-create-keystore was specified and ' |
| 40 | + f'the keystore "{keystore_path}" ' |
| 41 | + 'does not exist')) |
| 42 | + |
| 43 | + |
| 44 | +class InvalidKeystoreError(Exception): |
| 45 | + """Exception raised when provided keystore isn't valid.""" |
| 46 | + |
| 47 | + def __init__(self, keystore_path: pathlib.Path): |
| 48 | + super().__init__(('--no-create-keystore was specified and ' |
| 49 | + f'the keystore "{keystore_path}" is not initialized. \n\t' |
| 50 | + f'(Try running: ros2 security create_keystore {keystore_path})')) |
| 51 | + |
| 52 | + |
| 53 | +class SecurityOption(OptionExtension): |
| 54 | + |
| 55 | + def add_arguments(self, parser, cli_name, *, argv=None): |
| 56 | + sec_args = parser.add_argument_group( |
| 57 | + title='security', |
| 58 | + description='Security-related arguments' |
| 59 | + ) |
| 60 | + arg = sec_args.add_argument( |
| 61 | + '--secure', |
| 62 | + metavar='keystore', |
| 63 | + nargs='?', |
| 64 | + const='', |
| 65 | + help=('Launch with ROS 2 security features using the specified keystore directory. ' |
| 66 | + 'Will set up an ephemeral keystore if one is not specified.'), |
| 67 | + ) |
| 68 | + try: |
| 69 | + arg.completer = DirectoriesCompleter() # argcomplete is optional |
| 70 | + except NameError: |
| 71 | + pass |
| 72 | + |
| 73 | + sec_args.add_argument( |
| 74 | + '--no-create-keystore', |
| 75 | + dest='create_keystore', |
| 76 | + action='store_false', |
| 77 | + help='Disable automatic keystore creation and/or initialization' |
| 78 | + ) |
| 79 | + |
| 80 | + def prelaunch( |
| 81 | + self, |
| 82 | + launch_description: launch.LaunchDescription, |
| 83 | + args |
| 84 | + ) -> Tuple[launch.LaunchDescription, '_Keystore']: |
| 85 | + if args.secure is None: |
| 86 | + return (launch_description,) |
| 87 | + keystore_path = pathlib.Path(args.secure) if args.secure != '' else None |
| 88 | + keystore = _Keystore(keystore_path=keystore_path, create_keystore=args.create_keystore) |
| 89 | + |
| 90 | + launch_description = launch.LaunchDescription( |
| 91 | + [ |
| 92 | + launch.actions.DeclareLaunchArgument( |
| 93 | + name='__keystore', default_value=str(keystore.path) |
| 94 | + ), |
| 95 | + launch.actions.DeclareLaunchArgument( |
| 96 | + name='__secure', default_value='true' |
| 97 | + ), |
| 98 | + launch.actions.SetEnvironmentVariable( |
| 99 | + name='ROS_SECURITY_KEYSTORE', value=str(keystore.path) |
| 100 | + ), |
| 101 | + launch.actions.SetEnvironmentVariable( |
| 102 | + name='ROS_SECURITY_STRATEGY', |
| 103 | + value='Enforce'), |
| 104 | + launch.actions.SetEnvironmentVariable(name='ROS_SECURITY_ENABLE', value='true'), |
| 105 | + ] |
| 106 | + + launch_description.entities |
| 107 | + ) |
| 108 | + |
| 109 | + return launch_description, keystore |
| 110 | + |
| 111 | + |
| 112 | +class _Keystore: |
| 113 | + """ |
| 114 | + Object that contains a keystore. |
| 115 | +
|
| 116 | + If a transient keystore is created, contains the temporary directory, assuring |
| 117 | + it is destroyed alongside the _Keystore object. |
| 118 | + """ |
| 119 | + |
| 120 | + def __init__(self, *, keystore_path: Optional[pathlib.Path], create_keystore: bool): |
| 121 | + if not create_keystore: |
| 122 | + if keystore_path is None: |
| 123 | + raise NoKeystoreProvidedError() |
| 124 | + if not keystore_path.exists(): |
| 125 | + raise NonexistentKeystoreError(keystore_path) |
| 126 | + if not sros2.keystore._keystore.is_valid_keystore(keystore_path): |
| 127 | + raise InvalidKeystoreError(keystore_path) |
| 128 | + |
| 129 | + # If keystore path is blank, create a transient keystore |
| 130 | + if keystore_path is None: |
| 131 | + self._temp_keystore = TemporaryDirectory() |
| 132 | + self._keystore_path = pathlib.Path(self._temp_keystore.name) |
| 133 | + else: |
| 134 | + self._keystore_path = keystore_path |
| 135 | + self._keystore_path = self._keystore_path.resolve() |
| 136 | + # If keystore is not initialized, create a keystore |
| 137 | + if not self._keystore_path.is_dir(): |
| 138 | + self._keystore_path.mkdir() |
| 139 | + if not sros2.keystore._keystore.is_valid_keystore(self._keystore_path): |
| 140 | + sros2.keystore._keystore.create_keystore(self._keystore_path) |
| 141 | + |
| 142 | + @property |
| 143 | + def path(self) -> pathlib.Path: |
| 144 | + return self._keystore_path |
| 145 | + |
| 146 | + def __str__(self) -> str: |
| 147 | + return str(self._keystore_path) |
0 commit comments