forms.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. from Crypto.Cipher import PKCS1_OAEP
  2. from Crypto.PublicKey import RSA
  3. from django import forms
  4. from dcim.models import Device
  5. from extras.forms import (
  6. AddRemoveTagsForm, CustomFieldBulkEditForm, CustomFieldFilterForm, CustomFieldModelForm, CustomFieldModelCSVForm,
  7. )
  8. from extras.models import Tag
  9. from utilities.forms import (
  10. APISelectMultiple, BootstrapMixin, CSVModelChoiceField, CSVModelForm, DynamicModelChoiceField,
  11. DynamicModelMultipleChoiceField, SlugField, StaticSelect2Multiple, TagFilterField,
  12. )
  13. from .constants import *
  14. from .models import Secret, SecretRole, UserKey
  15. def validate_rsa_key(key, is_secret=True):
  16. """
  17. Validate the format and type of an RSA key.
  18. """
  19. if key.startswith('ssh-rsa '):
  20. raise forms.ValidationError("OpenSSH line format is not supported. Please ensure that your public is in PEM (base64) format.")
  21. try:
  22. key = RSA.importKey(key)
  23. except ValueError:
  24. raise forms.ValidationError("Invalid RSA key. Please ensure that your key is in PEM (base64) format.")
  25. except Exception as e:
  26. raise forms.ValidationError("Invalid key detected: {}".format(e))
  27. if is_secret and not key.has_private():
  28. raise forms.ValidationError("This looks like a public key. Please provide your private RSA key.")
  29. elif not is_secret and key.has_private():
  30. raise forms.ValidationError("This looks like a private key. Please provide your public RSA key.")
  31. try:
  32. PKCS1_OAEP.new(key)
  33. except Exception:
  34. raise forms.ValidationError("Error validating RSA key. Please ensure that your key supports PKCS#1 OAEP.")
  35. #
  36. # Secret roles
  37. #
  38. class SecretRoleForm(BootstrapMixin, forms.ModelForm):
  39. slug = SlugField()
  40. class Meta:
  41. model = SecretRole
  42. fields = [
  43. 'name', 'slug', 'description', 'users', 'groups',
  44. ]
  45. widgets = {
  46. 'users': StaticSelect2Multiple(),
  47. 'groups': StaticSelect2Multiple(),
  48. }
  49. class SecretRoleCSVForm(CSVModelForm):
  50. slug = SlugField()
  51. class Meta:
  52. model = SecretRole
  53. fields = SecretRole.csv_headers
  54. #
  55. # Secrets
  56. #
  57. class SecretForm(BootstrapMixin, CustomFieldModelForm):
  58. device = DynamicModelChoiceField(
  59. queryset=Device.objects.all()
  60. )
  61. plaintext = forms.CharField(
  62. max_length=SECRET_PLAINTEXT_MAX_LENGTH,
  63. required=False,
  64. label='Plaintext',
  65. widget=forms.PasswordInput(
  66. attrs={
  67. 'class': 'requires-session-key',
  68. }
  69. )
  70. )
  71. plaintext2 = forms.CharField(
  72. max_length=SECRET_PLAINTEXT_MAX_LENGTH,
  73. required=False,
  74. label='Plaintext (verify)',
  75. widget=forms.PasswordInput()
  76. )
  77. role = DynamicModelChoiceField(
  78. queryset=SecretRole.objects.all()
  79. )
  80. tags = DynamicModelMultipleChoiceField(
  81. queryset=Tag.objects.all(),
  82. required=False
  83. )
  84. class Meta:
  85. model = Secret
  86. fields = [
  87. 'device', 'role', 'name', 'plaintext', 'plaintext2', 'tags',
  88. ]
  89. def __init__(self, *args, **kwargs):
  90. super().__init__(*args, **kwargs)
  91. # A plaintext value is required when creating a new Secret
  92. if not self.instance.pk:
  93. self.fields['plaintext'].required = True
  94. def clean(self):
  95. # Verify that the provided plaintext values match
  96. if self.cleaned_data['plaintext'] != self.cleaned_data['plaintext2']:
  97. raise forms.ValidationError({
  98. 'plaintext2': "The two given plaintext values do not match. Please check your input."
  99. })
  100. # Validate uniqueness
  101. if Secret.objects.filter(
  102. device=self.cleaned_data['device'],
  103. role=self.cleaned_data['role'],
  104. name=self.cleaned_data['name']
  105. ).exists():
  106. raise forms.ValidationError(
  107. "Each secret assigned to a device must have a unique combination of role and name"
  108. )
  109. class SecretCSVForm(CustomFieldModelCSVForm):
  110. device = CSVModelChoiceField(
  111. queryset=Device.objects.all(),
  112. to_field_name='name',
  113. help_text='Assigned device'
  114. )
  115. role = CSVModelChoiceField(
  116. queryset=SecretRole.objects.all(),
  117. to_field_name='name',
  118. help_text='Assigned role'
  119. )
  120. plaintext = forms.CharField(
  121. help_text='Plaintext secret data'
  122. )
  123. class Meta:
  124. model = Secret
  125. fields = Secret.csv_headers
  126. help_texts = {
  127. 'name': 'Name or username',
  128. }
  129. def save(self, *args, **kwargs):
  130. s = super().save(*args, **kwargs)
  131. s.plaintext = str(self.cleaned_data['plaintext'])
  132. return s
  133. class SecretBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
  134. pk = forms.ModelMultipleChoiceField(
  135. queryset=Secret.objects.all(),
  136. widget=forms.MultipleHiddenInput()
  137. )
  138. role = DynamicModelChoiceField(
  139. queryset=SecretRole.objects.all(),
  140. required=False
  141. )
  142. name = forms.CharField(
  143. max_length=100,
  144. required=False
  145. )
  146. class Meta:
  147. nullable_fields = [
  148. 'name',
  149. ]
  150. class SecretFilterForm(BootstrapMixin, CustomFieldFilterForm):
  151. model = Secret
  152. q = forms.CharField(
  153. required=False,
  154. label='Search'
  155. )
  156. role = DynamicModelMultipleChoiceField(
  157. queryset=SecretRole.objects.all(),
  158. to_field_name='slug',
  159. required=False,
  160. widget=APISelectMultiple(
  161. value_field="slug",
  162. )
  163. )
  164. tag = TagFilterField(model)
  165. #
  166. # UserKeys
  167. #
  168. class UserKeyForm(BootstrapMixin, forms.ModelForm):
  169. class Meta:
  170. model = UserKey
  171. fields = ['public_key']
  172. help_texts = {
  173. 'public_key': "Enter your public RSA key. Keep the private one with you; you'll need it for decryption. "
  174. "Please note that passphrase-protected keys are not supported.",
  175. }
  176. labels = {
  177. 'public_key': ''
  178. }
  179. def clean_public_key(self):
  180. key = self.cleaned_data['public_key']
  181. # Validate the RSA key format.
  182. validate_rsa_key(key, is_secret=False)
  183. return key
  184. class ActivateUserKeyForm(forms.Form):
  185. _selected_action = forms.ModelMultipleChoiceField(
  186. queryset=UserKey.objects.all(),
  187. label='User Keys'
  188. )
  189. secret_key = forms.CharField(
  190. widget=forms.Textarea(
  191. attrs={
  192. 'class': 'vLargeTextField',
  193. }
  194. ),
  195. label='Your private key'
  196. )