Show More
@@ -1,152 +1,152 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2011-2018 RhodeCode GmbH |
|
3 | # Copyright (C) 2011-2018 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | import os |
|
21 | import os | |
22 | import re |
|
22 | import re | |
23 | import logging |
|
23 | import logging | |
24 |
|
24 | |||
25 |
|
25 | |||
26 | import ipaddress |
|
26 | import ipaddress | |
27 | import colander |
|
27 | import colander | |
28 |
|
28 | |||
29 | from rhodecode.translation import _ |
|
29 | from rhodecode.translation import _ | |
30 | from rhodecode.lib.utils2 import glob2re, safe_unicode |
|
30 | from rhodecode.lib.utils2 import glob2re, safe_unicode | |
31 | from rhodecode.lib.ext_json import json |
|
31 | from rhodecode.lib.ext_json import json | |
32 |
|
32 | |||
33 | log = logging.getLogger(__name__) |
|
33 | log = logging.getLogger(__name__) | |
34 |
|
34 | |||
35 |
|
35 | |||
36 | def ip_addr_validator(node, value): |
|
36 | def ip_addr_validator(node, value): | |
37 | try: |
|
37 | try: | |
38 | # this raises an ValueError if address is not IpV4 or IpV6 |
|
38 | # this raises an ValueError if address is not IpV4 or IpV6 | |
39 | ipaddress.ip_network(safe_unicode(value), strict=False) |
|
39 | ipaddress.ip_network(safe_unicode(value), strict=False) | |
40 | except ValueError: |
|
40 | except ValueError: | |
41 | msg = _(u'Please enter a valid IPv4 or IpV6 address') |
|
41 | msg = _(u'Please enter a valid IPv4 or IpV6 address') | |
42 | raise colander.Invalid(node, msg) |
|
42 | raise colander.Invalid(node, msg) | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | class IpAddrValidator(object): |
|
45 | class IpAddrValidator(object): | |
46 | def __init__(self, strict=True): |
|
46 | def __init__(self, strict=True): | |
47 | self.strict = strict |
|
47 | self.strict = strict | |
48 |
|
48 | |||
49 | def __call__(self, node, value): |
|
49 | def __call__(self, node, value): | |
50 | try: |
|
50 | try: | |
51 | # this raises an ValueError if address is not IpV4 or IpV6 |
|
51 | # this raises an ValueError if address is not IpV4 or IpV6 | |
52 | ipaddress.ip_network(safe_unicode(value), strict=self.strict) |
|
52 | ipaddress.ip_network(safe_unicode(value), strict=self.strict) | |
53 | except ValueError: |
|
53 | except ValueError: | |
54 | msg = _(u'Please enter a valid IPv4 or IpV6 address') |
|
54 | msg = _(u'Please enter a valid IPv4 or IpV6 address') | |
55 | raise colander.Invalid(node, msg) |
|
55 | raise colander.Invalid(node, msg) | |
56 |
|
56 | |||
57 |
|
57 | |||
58 | def glob_validator(node, value): |
|
58 | def glob_validator(node, value): | |
59 | try: |
|
59 | try: | |
60 | re.compile('^' + glob2re(value) + '$') |
|
60 | re.compile('^' + glob2re(value) + '$') | |
61 | except Exception: |
|
61 | except Exception: | |
62 | msg = _(u'Invalid glob pattern') |
|
62 | msg = _(u'Invalid glob pattern') | |
63 | raise colander.Invalid(node, msg) |
|
63 | raise colander.Invalid(node, msg) | |
64 |
|
64 | |||
65 |
|
65 | |||
66 | def valid_name_validator(node, value): |
|
66 | def valid_name_validator(node, value): | |
67 | from rhodecode.model.validation_schema import types |
|
67 | from rhodecode.model.validation_schema import types | |
68 | if value is types.RootLocation: |
|
68 | if value is types.RootLocation: | |
69 | return |
|
69 | return | |
70 |
|
70 | |||
71 | msg = _('Name must start with a letter or number. Got `{}`').format(value) |
|
71 | msg = _('Name must start with a letter or number. Got `{}`').format(value) | |
72 | if not re.match(r'^[a-zA-z0-9]{1,}', value): |
|
72 | if not re.match(r'^[a-zA-z0-9]{1,}', value): | |
73 | raise colander.Invalid(node, msg) |
|
73 | raise colander.Invalid(node, msg) | |
74 |
|
74 | |||
75 |
|
75 | |||
76 | class InvalidCloneUrl(Exception): |
|
76 | class InvalidCloneUrl(Exception): | |
77 | allowed_prefixes = () |
|
77 | allowed_prefixes = () | |
78 |
|
78 | |||
79 |
|
79 | |||
80 | def url_validator(url, repo_type, config): |
|
80 | def url_validator(url, repo_type, config): | |
81 | from rhodecode.lib.vcs.backends.hg import MercurialRepository |
|
81 | from rhodecode.lib.vcs.backends.hg import MercurialRepository | |
82 | from rhodecode.lib.vcs.backends.git import GitRepository |
|
82 | from rhodecode.lib.vcs.backends.git import GitRepository | |
83 | from rhodecode.lib.vcs.backends.svn import SubversionRepository |
|
83 | from rhodecode.lib.vcs.backends.svn import SubversionRepository | |
84 |
|
84 | |||
85 | if repo_type == 'hg': |
|
85 | if repo_type == 'hg': | |
86 | allowed_prefixes = ('http', 'svn+http', 'git+http') |
|
86 | allowed_prefixes = ('http', 'svn+http', 'git+http') | |
87 |
|
87 | |||
88 | if 'http' in url[:4]: |
|
88 | if 'http' in url[:4]: | |
89 | # initially check if it's at least the proper URL |
|
89 | # initially check if it's at least the proper URL | |
90 | # or does it pass basic auth |
|
90 | # or does it pass basic auth | |
91 |
|
91 | |||
92 | MercurialRepository.check_url(url, config) |
|
92 | return MercurialRepository.check_url(url, config) | |
93 | elif 'svn+http' in url[:8]: # svn->hg import |
|
93 | elif 'svn+http' in url[:8]: # svn->hg import | |
94 | SubversionRepository.check_url(url, config) |
|
94 | SubversionRepository.check_url(url, config) | |
95 | elif 'git+http' in url[:8]: # git->hg import |
|
95 | elif 'git+http' in url[:8]: # git->hg import | |
96 | raise NotImplementedError() |
|
96 | raise NotImplementedError() | |
97 | else: |
|
97 | else: | |
98 | exc = InvalidCloneUrl('Clone from URI %s not allowed. ' |
|
98 | exc = InvalidCloneUrl('Clone from URI %s not allowed. ' | |
99 | 'Allowed url must start with one of %s' |
|
99 | 'Allowed url must start with one of %s' | |
100 | % (url, ','.join(allowed_prefixes))) |
|
100 | % (url, ','.join(allowed_prefixes))) | |
101 | exc.allowed_prefixes = allowed_prefixes |
|
101 | exc.allowed_prefixes = allowed_prefixes | |
102 | raise exc |
|
102 | raise exc | |
103 |
|
103 | |||
104 | elif repo_type == 'git': |
|
104 | elif repo_type == 'git': | |
105 | allowed_prefixes = ('http', 'svn+http', 'hg+http') |
|
105 | allowed_prefixes = ('http', 'svn+http', 'hg+http') | |
106 | if 'http' in url[:4]: |
|
106 | if 'http' in url[:4]: | |
107 | # initially check if it's at least the proper URL |
|
107 | # initially check if it's at least the proper URL | |
108 | # or does it pass basic auth |
|
108 | # or does it pass basic auth | |
109 | GitRepository.check_url(url, config) |
|
109 | return GitRepository.check_url(url, config) | |
110 | elif 'svn+http' in url[:8]: # svn->git import |
|
110 | elif 'svn+http' in url[:8]: # svn->git import | |
111 | raise NotImplementedError() |
|
111 | raise NotImplementedError() | |
112 | elif 'hg+http' in url[:8]: # hg->git import |
|
112 | elif 'hg+http' in url[:8]: # hg->git import | |
113 | raise NotImplementedError() |
|
113 | raise NotImplementedError() | |
114 | else: |
|
114 | else: | |
115 | exc = InvalidCloneUrl('Clone from URI %s not allowed. ' |
|
115 | exc = InvalidCloneUrl('Clone from URI %s not allowed. ' | |
116 | 'Allowed url must start with one of %s' |
|
116 | 'Allowed url must start with one of %s' | |
117 | % (url, ','.join(allowed_prefixes))) |
|
117 | % (url, ','.join(allowed_prefixes))) | |
118 | exc.allowed_prefixes = allowed_prefixes |
|
118 | exc.allowed_prefixes = allowed_prefixes | |
119 | raise exc |
|
119 | raise exc | |
120 | elif repo_type == 'svn': |
|
120 | elif repo_type == 'svn': | |
121 | # no validation for SVN yet |
|
121 | # no validation for SVN yet | |
122 | return |
|
122 | return | |
123 |
|
123 | |||
124 |
raise InvalidCloneUrl(' |
|
124 | raise InvalidCloneUrl('Invalid repo type specified: `{}`'.format(repo_type)) | |
125 |
|
125 | |||
126 |
|
126 | |||
127 | class CloneUriValidator(object): |
|
127 | class CloneUriValidator(object): | |
128 | def __init__(self, repo_type): |
|
128 | def __init__(self, repo_type): | |
129 | self.repo_type = repo_type |
|
129 | self.repo_type = repo_type | |
130 |
|
130 | |||
131 | def __call__(self, node, value): |
|
131 | def __call__(self, node, value): | |
132 |
|
132 | |||
133 | from rhodecode.lib.utils import make_db_config |
|
133 | from rhodecode.lib.utils import make_db_config | |
134 | try: |
|
134 | try: | |
135 | config = make_db_config(clear_session=False) |
|
135 | config = make_db_config(clear_session=False) | |
136 | url_validator(value, self.repo_type, config) |
|
136 | url_validator(value, self.repo_type, config) | |
137 | except InvalidCloneUrl as e: |
|
137 | except InvalidCloneUrl as e: | |
138 | log.warning(e) |
|
138 | log.warning(e) | |
139 | raise colander.Invalid(node, e.message) |
|
139 | raise colander.Invalid(node, e.message) | |
140 | except Exception: |
|
140 | except Exception: | |
141 | log.exception('Url validation failed') |
|
141 | log.exception('Url validation failed') | |
142 | msg = _(u'invalid clone url for {repo_type} repository').format( |
|
142 | msg = _(u'invalid clone url for {repo_type} repository').format( | |
143 | repo_type=self.repo_type) |
|
143 | repo_type=self.repo_type) | |
144 | raise colander.Invalid(node, msg) |
|
144 | raise colander.Invalid(node, msg) | |
145 |
|
145 | |||
146 |
|
146 | |||
147 | def json_validator(node, value): |
|
147 | def json_validator(node, value): | |
148 | try: |
|
148 | try: | |
149 | json.loads(value) |
|
149 | json.loads(value) | |
150 | except (Exception,): |
|
150 | except (Exception,): | |
151 | msg = _(u'Please enter a valid json object') |
|
151 | msg = _(u'Please enter a valid json object') | |
152 | raise colander.Invalid(node, msg) |
|
152 | raise colander.Invalid(node, msg) |
General Comments 0
You need to be logged in to leave comments.
Login now