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