Show More
@@ -1,92 +1,98 b'' | |||||
1 | # Copyright (C) 2016-2023 RhodeCode GmbH |
|
1 | # Copyright (C) 2016-2023 RhodeCode GmbH | |
2 | # |
|
2 | # | |
3 | # This program is free software: you can redistribute it and/or modify |
|
3 | # This program is free software: you can redistribute it and/or modify | |
4 | # it under the terms of the GNU Affero General Public License, version 3 |
|
4 | # it under the terms of the GNU Affero General Public License, version 3 | |
5 | # (only), as published by the Free Software Foundation. |
|
5 | # (only), as published by the Free Software Foundation. | |
6 | # |
|
6 | # | |
7 | # This program is distributed in the hope that it will be useful, |
|
7 | # This program is distributed in the hope that it will be useful, | |
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
10 | # GNU General Public License for more details. |
|
10 | # GNU General Public License for more details. | |
11 | # |
|
11 | # | |
12 | # You should have received a copy of the GNU Affero General Public License |
|
12 | # You should have received a copy of the GNU Affero General Public License | |
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
14 | # |
|
14 | # | |
15 | # This program is dual-licensed. If you wish to learn more about the |
|
15 | # This program is dual-licensed. If you wish to learn more about the | |
16 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
16 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
18 |
|
18 | |||
19 | """ |
|
19 | """ | |
20 | WARNING: be really carefully with changing ANY imports in this file |
|
20 | WARNING: be really carefully with changing ANY imports in this file | |
21 | # This script is to mean as really fast executable, doing some imports here that would yield an import chain change |
|
21 | # This script is to mean as really fast executable, doing some imports here that would yield an import chain change | |
22 | # can affect execution times... |
|
22 | # can affect execution times... | |
23 | # This can be easily debugged using such command:: |
|
23 | # This can be easily debugged using such command:: | |
24 | # time PYTHONPROFILEIMPORTTIME=1 rc-ssh-wrapper-v2 --debug --mode=test .dev/dev.ini |
|
24 | # time PYTHONPROFILEIMPORTTIME=1 rc-ssh-wrapper-v2 --debug --mode=test .dev/dev.ini | |
25 | """ |
|
25 | """ | |
26 |
|
26 | |||
27 | import os |
|
27 | import os | |
28 | import sys |
|
28 | import sys | |
29 | import time |
|
29 | import time | |
30 | import logging |
|
30 | import logging | |
31 |
|
31 | |||
32 | import click |
|
32 | import click | |
33 |
|
33 | |||
34 | from rhodecode.config.config_maker import sanitize_settings_and_apply_defaults |
|
34 | from rhodecode.config.config_maker import sanitize_settings_and_apply_defaults | |
|
35 | from rhodecode.lib.request import Request | |||
|
36 | from rhodecode.lib.utils2 import AttributeDict | |||
35 | from rhodecode.lib.statsd_client import StatsdClient |
|
37 | from rhodecode.lib.statsd_client import StatsdClient | |
36 | from rhodecode.lib.config_utils import get_app_config_lightweight |
|
38 | from rhodecode.lib.config_utils import get_app_config_lightweight | |
37 |
|
39 | |||
38 | from .utils import setup_custom_logging |
|
40 | from .utils import setup_custom_logging | |
39 | from .backends import SshWrapperStandalone |
|
41 | from .backends import SshWrapperStandalone | |
40 |
|
42 | |||
41 | log = logging.getLogger(__name__) |
|
43 | log = logging.getLogger(__name__) | |
42 |
|
44 | |||
43 |
|
45 | |||
44 | @click.command() |
|
46 | @click.command() | |
45 | @click.argument('ini_path', type=click.Path(exists=True)) |
|
47 | @click.argument('ini_path', type=click.Path(exists=True)) | |
46 | @click.option( |
|
48 | @click.option( | |
47 | '--mode', '-m', required=False, default='auto', |
|
49 | '--mode', '-m', required=False, default='auto', | |
48 | type=click.Choice(['auto', 'vcs', 'git', 'hg', 'svn', 'test']), |
|
50 | type=click.Choice(['auto', 'vcs', 'git', 'hg', 'svn', 'test']), | |
49 | help='mode of operation') |
|
51 | help='mode of operation') | |
50 | @click.option('--user', help='Username for which the command will be executed') |
|
52 | @click.option('--user', help='Username for which the command will be executed') | |
51 | @click.option('--user-id', help='User ID for which the command will be executed') |
|
53 | @click.option('--user-id', help='User ID for which the command will be executed') | |
52 | @click.option('--key-id', help='ID of the key from the database') |
|
54 | @click.option('--key-id', help='ID of the key from the database') | |
53 | @click.option('--shell', '-s', is_flag=True, help='Allow Shell') |
|
55 | @click.option('--shell', '-s', is_flag=True, help='Allow Shell') | |
54 | @click.option('--debug', is_flag=True, help='Enabled detailed output logging') |
|
56 | @click.option('--debug', is_flag=True, help='Enabled detailed output logging') | |
55 | def main(ini_path, mode, user, user_id, key_id, shell, debug): |
|
57 | def main(ini_path, mode, user, user_id, key_id, shell, debug): | |
56 |
|
58 | |||
57 | time_start = time.time() |
|
59 | time_start = time.time() | |
58 | setup_custom_logging(ini_path, debug) |
|
60 | setup_custom_logging(ini_path, debug) | |
59 |
|
61 | |||
60 | command = os.environ.get('SSH_ORIGINAL_COMMAND', '') |
|
62 | command = os.environ.get('SSH_ORIGINAL_COMMAND', '') | |
61 | if not command and mode not in ['test']: |
|
63 | if not command and mode not in ['test']: | |
62 | raise ValueError( |
|
64 | raise ValueError( | |
63 | 'Unable to fetch SSH_ORIGINAL_COMMAND from environment.' |
|
65 | 'Unable to fetch SSH_ORIGINAL_COMMAND from environment.' | |
64 | 'Please make sure this is set and available during execution ' |
|
66 | 'Please make sure this is set and available during execution ' | |
65 | 'of this script.') |
|
67 | 'of this script.') | |
66 |
|
68 | |||
67 | # initialize settings and get defaults |
|
69 | # initialize settings and get defaults | |
68 | settings = get_app_config_lightweight(ini_path) |
|
70 | settings = get_app_config_lightweight(ini_path) | |
69 | settings = sanitize_settings_and_apply_defaults({'__file__': ini_path}, settings) |
|
71 | settings = sanitize_settings_and_apply_defaults({'__file__': ini_path}, settings) | |
70 |
|
72 | |||
71 | # init and bootstrap StatsdClient |
|
73 | # init and bootstrap StatsdClient | |
72 | StatsdClient.setup(settings) |
|
74 | StatsdClient.setup(settings) | |
73 | statsd = StatsdClient.statsd |
|
75 | statsd = StatsdClient.statsd | |
74 |
|
76 | |||
75 | try: |
|
77 | try: | |
76 | connection_info = os.environ.get('SSH_CONNECTION', '') |
|
78 | connection_info = os.environ.get('SSH_CONNECTION', '') | |
77 | env = {'RC_CMD_SSH_WRAPPER': '1'} |
|
79 | request = Request.blank('/', base_url=settings['app.base_url']) | |
|
80 | request.user = AttributeDict({'username': user, | |||
|
81 | 'user_id': user_id, | |||
|
82 | 'ip_addr': connection_info.split(' ')[0] if connection_info else None}) | |||
|
83 | env = {'RC_CMD_SSH_WRAPPER': '1', 'request': request} | |||
78 | ssh_wrapper = SshWrapperStandalone( |
|
84 | ssh_wrapper = SshWrapperStandalone( | |
79 | command, connection_info, mode, |
|
85 | command, connection_info, mode, | |
80 | user, user_id, key_id, shell, ini_path, settings, env) |
|
86 | user, user_id, key_id, shell, ini_path, settings, env) | |
81 | except Exception: |
|
87 | except Exception: | |
82 | log.exception('Failed to execute SshWrapper') |
|
88 | log.exception('Failed to execute SshWrapper') | |
83 | sys.exit(-5) |
|
89 | sys.exit(-5) | |
84 |
|
90 | |||
85 | return_code = ssh_wrapper.wrap() |
|
91 | return_code = ssh_wrapper.wrap() | |
86 | operation_took = time.time() - time_start |
|
92 | operation_took = time.time() - time_start | |
87 | if statsd: |
|
93 | if statsd: | |
88 | operation_took_ms = round(1000.0 * operation_took) |
|
94 | operation_took_ms = round(1000.0 * operation_took) | |
89 | statsd.timing("rhodecode_ssh_wrapper_timing.histogram", operation_took_ms, |
|
95 | statsd.timing("rhodecode_ssh_wrapper_timing.histogram", operation_took_ms, | |
90 | use_decimals=False) |
|
96 | use_decimals=False) | |
91 |
|
97 | |||
92 | sys.exit(return_code) |
|
98 | sys.exit(return_code) |
General Comments 0
You need to be logged in to leave comments.
Login now