Show More
@@ -1,79 +1,86 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 | import os |
|
19 | import os | |
20 | import sys |
|
20 | import sys | |
|
21 | import time | |||
21 | import logging |
|
22 | import logging | |
22 |
|
23 | |||
23 | import click |
|
24 | import click | |
24 |
|
25 | |||
25 | from pyramid.paster import setup_logging |
|
26 | from pyramid.paster import setup_logging | |
26 |
|
27 | |||
27 | from rhodecode.lib.pyramid_utils import bootstrap |
|
28 | from rhodecode.lib.pyramid_utils import bootstrap | |
|
29 | from rhodecode.lib.statsd_client import StatsdClient | |||
28 | from .backends import SshWrapper |
|
30 | from .backends import SshWrapper | |
29 |
|
31 | |||
30 | log = logging.getLogger(__name__) |
|
32 | log = logging.getLogger(__name__) | |
31 |
|
33 | |||
32 |
|
34 | |||
33 | def setup_custom_logging(ini_path, debug): |
|
35 | def setup_custom_logging(ini_path, debug): | |
34 | if debug: |
|
36 | if debug: | |
35 | # enabled rhodecode.ini controlled logging setup |
|
37 | # enabled rhodecode.ini controlled logging setup | |
36 | setup_logging(ini_path) |
|
38 | setup_logging(ini_path) | |
37 | else: |
|
39 | else: | |
38 | # configure logging in a mode that doesn't print anything. |
|
40 | # configure logging in a mode that doesn't print anything. | |
39 | # in case of regularly configured logging it gets printed out back |
|
41 | # in case of regularly configured logging it gets printed out back | |
40 | # to the client doing an SSH command. |
|
42 | # to the client doing an SSH command. | |
41 | logger = logging.getLogger('') |
|
43 | logger = logging.getLogger('') | |
42 | null = logging.NullHandler() |
|
44 | null = logging.NullHandler() | |
43 | # add the handler to the root logger |
|
45 | # add the handler to the root logger | |
44 | logger.handlers = [null] |
|
46 | logger.handlers = [null] | |
45 |
|
47 | |||
46 |
|
48 | |||
47 | @click.command() |
|
49 | @click.command() | |
48 | @click.argument('ini_path', type=click.Path(exists=True)) |
|
50 | @click.argument('ini_path', type=click.Path(exists=True)) | |
49 | @click.option( |
|
51 | @click.option( | |
50 | '--mode', '-m', required=False, default='auto', |
|
52 | '--mode', '-m', required=False, default='auto', | |
51 | type=click.Choice(['auto', 'vcs', 'git', 'hg', 'svn', 'test']), |
|
53 | type=click.Choice(['auto', 'vcs', 'git', 'hg', 'svn', 'test']), | |
52 | help='mode of operation') |
|
54 | help='mode of operation') | |
53 | @click.option('--user', help='Username for which the command will be executed') |
|
55 | @click.option('--user', help='Username for which the command will be executed') | |
54 | @click.option('--user-id', help='User ID for which the command will be executed') |
|
56 | @click.option('--user-id', help='User ID for which the command will be executed') | |
55 | @click.option('--key-id', help='ID of the key from the database') |
|
57 | @click.option('--key-id', help='ID of the key from the database') | |
56 | @click.option('--shell', '-s', is_flag=True, help='Allow Shell') |
|
58 | @click.option('--shell', '-s', is_flag=True, help='Allow Shell') | |
57 | @click.option('--debug', is_flag=True, help='Enabled detailed output logging') |
|
59 | @click.option('--debug', is_flag=True, help='Enabled detailed output logging') | |
58 | def main(ini_path, mode, user, user_id, key_id, shell, debug): |
|
60 | def main(ini_path, mode, user, user_id, key_id, shell, debug): | |
59 | setup_custom_logging(ini_path, debug) |
|
61 | setup_custom_logging(ini_path, debug) | |
60 |
|
62 | |||
61 | command = os.environ.get('SSH_ORIGINAL_COMMAND', '') |
|
63 | command = os.environ.get('SSH_ORIGINAL_COMMAND', '') | |
62 | if not command and mode not in ['test']: |
|
64 | if not command and mode not in ['test']: | |
63 | raise ValueError( |
|
65 | raise ValueError( | |
64 | 'Unable to fetch SSH_ORIGINAL_COMMAND from environment.' |
|
66 | 'Unable to fetch SSH_ORIGINAL_COMMAND from environment.' | |
65 | 'Please make sure this is set and available during execution ' |
|
67 | 'Please make sure this is set and available during execution ' | |
66 | 'of this script.') |
|
68 | 'of this script.') | |
67 | connection_info = os.environ.get('SSH_CONNECTION', '') |
|
69 | connection_info = os.environ.get('SSH_CONNECTION', '') | |
68 |
|
70 | time_start = time.time() | ||
69 | with bootstrap(ini_path, env={'RC_CMD_SSH_WRAPPER': '1'}) as env: |
|
71 | with bootstrap(ini_path, env={'RC_CMD_SSH_WRAPPER': '1'}) as env: | |
|
72 | statsd = StatsdClient.statsd | |||
70 | try: |
|
73 | try: | |
71 | ssh_wrapper = SshWrapper( |
|
74 | ssh_wrapper = SshWrapper( | |
72 | command, connection_info, mode, |
|
75 | command, connection_info, mode, | |
73 | user, user_id, key_id, shell, ini_path, env) |
|
76 | user, user_id, key_id, shell, ini_path, env) | |
74 | except Exception: |
|
77 | except Exception: | |
75 | log.exception('Failed to execute SshWrapper') |
|
78 | log.exception('Failed to execute SshWrapper') | |
76 | sys.exit(-5) |
|
79 | sys.exit(-5) | |
77 |
|
||||
78 | return_code = ssh_wrapper.wrap() |
|
80 | return_code = ssh_wrapper.wrap() | |
|
81 | operation_took = time.time() - time_start | |||
|
82 | if statsd: | |||
|
83 | operation_took_ms = round(1000.0 * operation_took) | |||
|
84 | statsd.timing("rhodecode_ssh_wrapper_timing.histogram", operation_took_ms, | |||
|
85 | use_decimals=False) | |||
79 | sys.exit(return_code) |
|
86 | sys.exit(return_code) |
General Comments 0
You need to be logged in to leave comments.
Login now