|
1 | 1 | import datetime
|
2 | 2 | import io
|
| 3 | +import ipaddress |
3 | 4 | import json
|
4 | 5 | import logging
|
5 | 6 | import uuid
|
@@ -222,6 +223,92 @@ def save(self, user, domain):
|
222 | 223 | return True
|
223 | 224 |
|
224 | 225 |
|
| 226 | +class IPAccessConfigForm(forms.Form): |
| 227 | + """ |
| 228 | + Form for updating a project's IP Access Configuration |
| 229 | + """ |
| 230 | + country_allowlist = forms.MultipleChoiceField( |
| 231 | + label=_("Allowed Countries"), |
| 232 | + choices=sorted(list(COUNTRIES.items()), key=lambda x: x[1]), |
| 233 | + required=False, |
| 234 | + ) |
| 235 | + |
| 236 | + ip_allowlist = forms.CharField( |
| 237 | + label=_("Allowed IPs"), |
| 238 | + required=False, |
| 239 | + help_text='IPs that will be allowed access to your project, regardless of country of origin. ' |
| 240 | + 'Please configure your list to be comma and space separated, ' |
| 241 | + 'e.g. 192.168.0.1, 192.168.1.1, 192.168.2.1', |
| 242 | + ) |
| 243 | + |
| 244 | + ip_denylist = forms.CharField( |
| 245 | + label=_("Denied IPs"), |
| 246 | + required=False, |
| 247 | + help_text='IPs that will be denied access to your project, regardless of country of origin.', |
| 248 | + ) |
| 249 | + |
| 250 | + comment = forms.CharField( |
| 251 | + label=_("Additional Notes"), |
| 252 | + widget=forms.Textarea(attrs={"class": "vertical-resize"}), |
| 253 | + required=False |
| 254 | + ) |
| 255 | + |
| 256 | + def __init__(self, *args, **kwargs): |
| 257 | + self.current_ip = kwargs.pop('current_ip', None) |
| 258 | + self.current_country = kwargs.pop('current_country', None) |
| 259 | + super(IPAccessConfigForm, self).__init__(*args, **kwargs) |
| 260 | + self.helper = hqcrispy.HQFormHelper(self) |
| 261 | + self.helper.form_id = 'ip-access-config-form' |
| 262 | + self.helper.layout = crispy.Layout( |
| 263 | + crispy.Fieldset( |
| 264 | + _("Edit IP Access Config"), |
| 265 | + "country_allowlist", |
| 266 | + "ip_allowlist", |
| 267 | + "ip_denylist", |
| 268 | + "comment" |
| 269 | + ), |
| 270 | + hqcrispy.FormActions( |
| 271 | + StrictButton( |
| 272 | + _("Update IP Access Config"), |
| 273 | + type="submit", |
| 274 | + css_class='btn-primary', |
| 275 | + ) |
| 276 | + ) |
| 277 | + ) |
| 278 | + |
| 279 | + def clean(self): |
| 280 | + allow_list = self.cleaned_data['ip_allowlist'].split(", ") if self.cleaned_data['ip_allowlist'] else [] |
| 281 | + deny_list = self.cleaned_data['ip_denylist'].split(", ") if self.cleaned_data['ip_denylist'] else [] |
| 282 | + |
| 283 | + # Ensure an IP isn't in both lists |
| 284 | + if (allow_list and deny_list) and set(allow_list).intersection(set(deny_list)): |
| 285 | + self.add_error('ip_allowlist', _("There are IP addresses in both the Allowed and Denied lists. " |
| 286 | + "Please ensure an IP address is only in one list at a time.")) |
| 287 | + |
| 288 | + # Ensure inputs are valid IPs, checks both IPv4 and IPv6 |
| 289 | + for ip in allow_list + deny_list: |
| 290 | + try: |
| 291 | + ipaddress.ip_address(ip) |
| 292 | + except ValueError as e: |
| 293 | + raise ValidationError(e) |
| 294 | + |
| 295 | + self.cleaned_data['ip_allowlist'] = allow_list |
| 296 | + self.cleaned_data['ip_denylist'] = deny_list |
| 297 | + |
| 298 | + # Additional validation |
| 299 | + if self.cleaned_data['country_allowlist']: |
| 300 | + if not settings.MAXMIND_LICENSE_KEY: |
| 301 | + self.add_error('country_allowlist', _("The Allowed Countries field cannot be saved because " |
| 302 | + "MaxMind is not configured for your environment")) |
| 303 | + elif (self.current_country and self.current_country not in self.cleaned_data['country_allowlist'] |
| 304 | + and self.current_ip not in self.cleaned_data['ip_allowlist']): |
| 305 | + self.add_error('country_allowlist', _("Please add your own country or IP to the Allowed IPs field " |
| 306 | + "to avoid being locked out.")) |
| 307 | + if self.current_ip in self.cleaned_data['ip_denylist']: |
| 308 | + self.add_error('ip_denylist', _("You cannot put your current IP address in the Denied IPs field")) |
| 309 | + return self.cleaned_data |
| 310 | + |
| 311 | + |
225 | 312 | class TransferDomainFormErrors(object):
|
226 | 313 | USER_DNE = gettext_lazy('The user being transferred to does not exist')
|
227 | 314 | DOMAIN_MISMATCH = gettext_lazy('Mismatch in domains when confirming')
|
|
0 commit comments