Skip to content

Commit

Permalink
Fixes #5408: Fix updating secrets without setting new plaintext
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Dec 3, 2020
1 parent d75696b commit 584b810
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions docs/release-notes/version-2.9.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [#5383](https://github.com/netbox-community/netbox/issues/5383) - Fix setting user password via REST API
* [#5396](https://github.com/netbox-community/netbox/issues/5396) - Fix uniqueness constraint for virtual machine names
* [#5407](https://github.com/netbox-community/netbox/issues/5407) - Add direct link to secret on secrets list
* [#5408](https://github.com/netbox-community/netbox/issues/5408) - Fix updating secrets without setting new plaintext

---

Expand Down
26 changes: 13 additions & 13 deletions netbox/secrets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ def post(self, request, *args, **kwargs):

if form.is_valid():
logger.debug("Form validation was successful")
secret = form.save(commit=False)

# We must have a session key in order to create a secret or update the plaintext of an existing secret
if (form.cleaned_data['plaintext'] or secret.pk is None) and session_key is None:
# We must have a session key in order to set the plaintext of a Secret
if form.cleaned_data['plaintext'] and session_key is None:
logger.debug("Unable to proceed: No session key was provided with the request")
form.add_error(None, "No session key was provided with the request. Unable to encrypt secret data.")

else:
elif form.cleaned_data['plaintext']:
master_key = None
try:
sk = SessionKey.objects.get(userkey__user=request.user)
Expand All @@ -125,19 +126,18 @@ def post(self, request, *args, **kwargs):

if master_key is not None:
logger.debug("Successfully resolved master key for encryption")
secret = form.save(commit=False)
if form.cleaned_data['plaintext']:
secret.plaintext = str(form.cleaned_data['plaintext'])
secret.plaintext = str(form.cleaned_data['plaintext'])
secret.encrypt(master_key)
secret.save()
form.save_m2m()

msg = '{} secret'.format('Created' if not form.instance.pk else 'Modified')
logger.info(f"{msg} {secret} (PK: {secret.pk})")
msg = '{} <a href="{}">{}</a>'.format(msg, secret.get_absolute_url(), escape(secret))
messages.success(request, mark_safe(msg))
secret.save()
form.save_m2m()

msg = '{} secret'.format('Created' if not form.instance.pk else 'Modified')
logger.info(f"{msg} {secret} (PK: {secret.pk})")
msg = f'{msg} <a href="{secret.get_absolute_url()}">{escape(secret)}</a>'
messages.success(request, mark_safe(msg))

return redirect(self.get_return_url(request, secret))
return redirect(self.get_return_url(request, secret))

else:
logger.debug("Form validation failed")
Expand Down

0 comments on commit 584b810

Please sign in to comment.