2016-03-01 11:23:03 -05:00
|
|
|
from Crypto.Cipher import PKCS1_OAEP
|
|
|
|
from Crypto.PublicKey import RSA
|
|
|
|
from django import forms
|
2018-05-10 12:53:11 -04:00
|
|
|
from taggit.forms import TagField
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2016-03-21 11:42:42 -04:00
|
|
|
from dcim.models import Device
|
2018-07-17 09:43:57 -04:00
|
|
|
from extras.forms import AddRemoveTagsForm, CustomFieldBulkEditForm, CustomFieldFilterForm, CustomFieldForm
|
2019-01-09 23:46:45 -05:00
|
|
|
from utilities.forms import (
|
|
|
|
APISelect, APISelectMultiple, BootstrapMixin, FilterChoiceField, FlexibleModelChoiceField, SlugField,
|
2020-01-13 20:16:13 +00:00
|
|
|
StaticSelect2Multiple, TagFilterField
|
2019-01-09 23:46:45 -05:00
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
from .models import Secret, SecretRole, UserKey
|
|
|
|
|
|
|
|
|
|
|
|
def validate_rsa_key(key, is_secret=True):
|
|
|
|
"""
|
|
|
|
Validate the format and type of an RSA key.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
key = RSA.importKey(key)
|
|
|
|
except ValueError:
|
|
|
|
raise forms.ValidationError("Invalid RSA key. Please ensure that your key is in PEM (base64) format.")
|
|
|
|
except Exception as e:
|
|
|
|
raise forms.ValidationError("Invalid key detected: {}".format(e))
|
|
|
|
if is_secret and not key.has_private():
|
|
|
|
raise forms.ValidationError("This looks like a public key. Please provide your private RSA key.")
|
|
|
|
elif not is_secret and key.has_private():
|
|
|
|
raise forms.ValidationError("This looks like a private key. Please provide your public RSA key.")
|
|
|
|
try:
|
|
|
|
PKCS1_OAEP.new(key)
|
2018-06-27 17:23:32 +02:00
|
|
|
except Exception:
|
2016-03-01 11:23:03 -05:00
|
|
|
raise forms.ValidationError("Error validating RSA key. Please ensure that your key supports PKCS#1 OAEP.")
|
|
|
|
|
|
|
|
|
2016-05-16 12:07:12 -04:00
|
|
|
#
|
|
|
|
# Secret roles
|
|
|
|
#
|
|
|
|
|
2016-12-21 14:15:18 -05:00
|
|
|
class SecretRoleForm(BootstrapMixin, forms.ModelForm):
|
2016-05-20 15:32:17 -04:00
|
|
|
slug = SlugField()
|
2016-05-16 12:07:12 -04:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = SecretRole
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
|
|
|
'name', 'slug', 'users', 'groups',
|
|
|
|
]
|
2019-01-09 23:46:45 -05:00
|
|
|
widgets = {
|
|
|
|
'users': StaticSelect2Multiple(),
|
|
|
|
'groups': StaticSelect2Multiple(),
|
|
|
|
}
|
2016-05-16 12:07:12 -04:00
|
|
|
|
|
|
|
|
2017-10-09 15:56:17 -04:00
|
|
|
class SecretRoleCSVForm(forms.ModelForm):
|
|
|
|
slug = SlugField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = SecretRole
|
2018-02-02 14:26:16 -05:00
|
|
|
fields = SecretRole.csv_headers
|
2017-10-09 15:56:17 -04:00
|
|
|
help_texts = {
|
|
|
|
'name': 'Name of secret role',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
#
|
|
|
|
# Secrets
|
|
|
|
#
|
|
|
|
|
2018-07-17 09:43:57 -04:00
|
|
|
class SecretForm(BootstrapMixin, CustomFieldForm):
|
2018-03-07 11:20:10 -05:00
|
|
|
plaintext = forms.CharField(
|
|
|
|
max_length=65535,
|
|
|
|
required=False,
|
|
|
|
label='Plaintext',
|
2018-11-27 11:57:29 -05:00
|
|
|
widget=forms.PasswordInput(
|
|
|
|
attrs={
|
|
|
|
'class': 'requires-session-key',
|
|
|
|
}
|
|
|
|
)
|
2018-03-07 11:20:10 -05:00
|
|
|
)
|
|
|
|
plaintext2 = forms.CharField(
|
|
|
|
max_length=65535,
|
|
|
|
required=False,
|
|
|
|
label='Plaintext (verify)',
|
|
|
|
widget=forms.PasswordInput()
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
tags = TagField(
|
|
|
|
required=False
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Secret
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
|
|
|
'role', 'name', 'plaintext', 'plaintext2', 'tags',
|
|
|
|
]
|
2019-01-09 23:46:45 -05:00
|
|
|
widgets = {
|
|
|
|
'role': APISelect(
|
|
|
|
api_url="/api/secrets/secret-roles/"
|
|
|
|
)
|
|
|
|
}
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2018-03-07 11:20:10 -05:00
|
|
|
def __init__(self, *args, **kwargs):
|
2018-11-27 10:52:24 -05:00
|
|
|
super().__init__(*args, **kwargs)
|
2018-03-07 11:20:10 -05:00
|
|
|
|
|
|
|
# A plaintext value is required when creating a new Secret
|
|
|
|
if not self.instance.pk:
|
|
|
|
self.fields['plaintext'].required = True
|
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
def clean(self):
|
2016-10-21 15:39:13 -04:00
|
|
|
|
2018-03-07 11:20:10 -05:00
|
|
|
# Verify that the provided plaintext values match
|
2016-10-21 15:39:13 -04:00
|
|
|
if self.cleaned_data['plaintext'] != self.cleaned_data['plaintext2']:
|
|
|
|
raise forms.ValidationError({
|
|
|
|
'plaintext2': "The two given plaintext values do not match. Please check your input."
|
|
|
|
})
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
|
2017-06-02 17:23:41 -04:00
|
|
|
class SecretCSVForm(forms.ModelForm):
|
2017-06-07 15:51:11 -04:00
|
|
|
device = FlexibleModelChoiceField(
|
2017-06-02 17:23:41 -04:00
|
|
|
queryset=Device.objects.all(),
|
|
|
|
to_field_name='name',
|
2017-06-07 15:51:11 -04:00
|
|
|
help_text='Device name or ID',
|
2017-06-02 17:23:41 -04:00
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Device not found.',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
role = forms.ModelChoiceField(
|
|
|
|
queryset=SecretRole.objects.all(),
|
|
|
|
to_field_name='name',
|
|
|
|
help_text='Name of assigned role',
|
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Invalid secret role.',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
plaintext = forms.CharField(
|
|
|
|
help_text='Plaintext secret data'
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Secret
|
2018-02-02 14:26:16 -05:00
|
|
|
fields = Secret.csv_headers
|
2017-06-07 15:51:11 -04:00
|
|
|
help_texts = {
|
|
|
|
'name': 'Name or username',
|
|
|
|
}
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2016-03-21 11:42:42 -04:00
|
|
|
def save(self, *args, **kwargs):
|
2018-11-27 10:52:24 -05:00
|
|
|
s = super().save(*args, **kwargs)
|
2016-03-21 11:42:42 -04:00
|
|
|
s.plaintext = str(self.cleaned_data['plaintext'])
|
|
|
|
return s
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2016-03-21 11:42:42 -04:00
|
|
|
|
2018-07-17 09:43:57 -04:00
|
|
|
class SecretBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
|
2018-11-27 11:57:29 -05:00
|
|
|
pk = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=Secret.objects.all(),
|
|
|
|
widget=forms.MultipleHiddenInput()
|
|
|
|
)
|
|
|
|
role = forms.ModelChoiceField(
|
|
|
|
queryset=SecretRole.objects.all(),
|
2019-01-09 23:46:45 -05:00
|
|
|
required=False,
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/secrets/secret-roles/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
|
|
|
name = forms.CharField(
|
|
|
|
max_length=100,
|
|
|
|
required=False
|
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2016-09-30 16:17:41 -04:00
|
|
|
class Meta:
|
2018-11-27 11:57:29 -05:00
|
|
|
nullable_fields = [
|
|
|
|
'name',
|
|
|
|
]
|
2016-09-30 16:17:41 -04:00
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2018-07-17 09:43:57 -04:00
|
|
|
class SecretFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|
|
|
model = Secret
|
2018-11-27 11:57:29 -05:00
|
|
|
q = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
label='Search'
|
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
role = FilterChoiceField(
|
2019-01-10 17:32:23 -05:00
|
|
|
queryset=SecretRole.objects.all(),
|
2019-01-09 23:46:45 -05:00
|
|
|
to_field_name='slug',
|
|
|
|
widget=APISelectMultiple(
|
|
|
|
api_url="/api/secrets/secret-roles/",
|
|
|
|
value_field="slug",
|
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2016-03-01 11:23:03 -05:00
|
|
|
|
2020-01-13 20:16:13 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.fields['tag'] = TagFilterField(self.model)
|
|
|
|
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
#
|
|
|
|
# UserKeys
|
|
|
|
#
|
|
|
|
|
2016-12-21 14:15:18 -05:00
|
|
|
class UserKeyForm(BootstrapMixin, forms.ModelForm):
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = UserKey
|
|
|
|
fields = ['public_key']
|
|
|
|
help_texts = {
|
2018-07-18 11:03:22 -04:00
|
|
|
'public_key': "Enter your public RSA key. Keep the private one with you; you'll need it for decryption. "
|
|
|
|
"Please note that passphrase-protected keys are not supported.",
|
2016-03-01 11:23:03 -05:00
|
|
|
}
|
2019-09-18 15:39:26 -04:00
|
|
|
labels = {
|
|
|
|
'public_key': ''
|
|
|
|
}
|
2016-03-01 11:23:03 -05:00
|
|
|
|
|
|
|
def clean_public_key(self):
|
|
|
|
key = self.cleaned_data['public_key']
|
|
|
|
|
|
|
|
# Validate the RSA key format.
|
|
|
|
validate_rsa_key(key, is_secret=False)
|
|
|
|
|
|
|
|
return key
|
|
|
|
|
|
|
|
|
|
|
|
class ActivateUserKeyForm(forms.Form):
|
2018-11-27 11:57:29 -05:00
|
|
|
_selected_action = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=UserKey.objects.all(),
|
|
|
|
label='User Keys'
|
|
|
|
)
|
|
|
|
secret_key = forms.CharField(
|
|
|
|
widget=forms.Textarea(
|
|
|
|
attrs={
|
|
|
|
'class': 'vLargeTextField',
|
|
|
|
}
|
|
|
|
),
|
|
|
|
label='Your private key'
|
|
|
|
)
|