secrets.js 4.5 KB

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