secrets.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. $(document).ready(function() {
  2. // Unlocking a secret
  3. $('button.unlock-secret').click(function(event) {
  4. var secret_id = $(this).attr('secret-id');
  5. unlock_secret(secret_id);
  6. event.preventDefault();
  7. });
  8. // Locking a secret
  9. $('button.lock-secret').click(function(event) {
  10. var secret_id = $(this).attr('secret-id');
  11. lock_secret(secret_id);
  12. event.preventDefault();
  13. });
  14. // Adding/editing a secret
  15. $('form.requires-session-key').submit(function(event) {
  16. if ($('#id_plaintext').val() && document.cookie.indexOf('session_key') == -1) {
  17. $('#privkey_modal').modal('show');
  18. event.preventDefault();
  19. }
  20. });
  21. // Retrieve a session key
  22. $('#request_session_key').click(function() {
  23. var private_key_field = $('#user_privkey');
  24. var private_key = private_key_field.val();
  25. get_session_key(private_key);
  26. private_key_field.val("");
  27. });
  28. // Retrieve a secret via the API
  29. function unlock_secret(secret_id) {
  30. $.ajax({
  31. url: netbox_api_path + 'secrets/secrets/' + secret_id + '/',
  32. type: 'GET',
  33. dataType: 'json',
  34. success: function (response, status) {
  35. if (response.plaintext) {
  36. console.log("Secret retrieved successfully");
  37. $('#secret_' + secret_id).html(response.plaintext);
  38. $('button.unlock-secret[secret-id=' + secret_id + ']').hide();
  39. $('button.lock-secret[secret-id=' + secret_id + ']').show();
  40. } else {
  41. console.log("Secret was not decrypted. Prompt user for private key.");
  42. $('#privkey_modal').modal('show');
  43. }
  44. },
  45. error: function (xhr, ajaxOptions, thrownError) {
  46. console.log("Error: " + xhr.responseText);
  47. if (xhr.status == 403) {
  48. alert("Permission denied");
  49. } else {
  50. alert(xhr.responseText);
  51. }
  52. }
  53. });
  54. }
  55. // Remove secret data from the DOM
  56. function lock_secret(secret_id) {
  57. var secret_div = $('#secret_' + secret_id);
  58. secret_div.html('********');
  59. $('button.lock-secret[secret-id=' + secret_id + ']').hide();
  60. $('button.unlock-secret[secret-id=' + secret_id + ']').show();
  61. }
  62. // Request a session key via the API
  63. function get_session_key(private_key) {
  64. var csrf_token = $('input[name=csrfmiddlewaretoken]').val();
  65. $.ajax({
  66. url: netbox_api_path + 'secrets/get-session-key/',
  67. type: 'POST',
  68. data: {
  69. private_key: private_key
  70. },
  71. dataType: 'json',
  72. beforeSend: function(xhr, settings) {
  73. xhr.setRequestHeader("X-CSRFToken", csrf_token);
  74. },
  75. success: function (response, status) {
  76. console.log("Received a new session key");
  77. alert('Session key received! You may now unlock secrets.');
  78. },
  79. error: function (xhr, ajaxOptions, thrownError) {
  80. if (xhr.status == 403) {
  81. alert("Permission denied");
  82. } else {
  83. var json = jQuery.parseJSON(xhr.responseText);
  84. alert("Failed to retrieve a session key: " + json['error']);
  85. }
  86. }
  87. });
  88. }
  89. // Generate a new public/private key pair via the API
  90. $('#generate_keypair').click(function() {
  91. $('#new_keypair_modal').modal('show');
  92. $.ajax({
  93. url: netbox_api_path + 'secrets/generate-rsa-key-pair/',
  94. type: 'GET',
  95. dataType: 'json',
  96. success: function (response, status) {
  97. var public_key = response.public_key;
  98. var private_key = response.private_key;
  99. $('#new_pubkey').val(public_key);
  100. $('#new_privkey').val(private_key);
  101. },
  102. error: function (xhr, ajaxOptions, thrownError) {
  103. alert("There was an error generating a new key pair.");
  104. }
  105. });
  106. });
  107. // Accept a new RSA key pair generated via the API
  108. $('#use_new_pubkey').click(function() {
  109. var new_pubkey = $('#new_pubkey');
  110. if (new_pubkey.val()) {
  111. $('#id_public_key').val(new_pubkey.val());
  112. }
  113. });
  114. });