app.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. var fs = require('fs');
  2. var https = require('https');
  3. var os = require('os');
  4. var httpSignature = require('http-signature');
  5. var jsSHA = require("jssha");
  6. process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
  7. // TODO: update these values to your own
  8. var tenancyId = "ocid1.tenancy.oc1..aaaaaaaa56ugo5lfjdiyb3n5dmk2t3kzs4kuwea3heu73bmmqycg7hmqpinq";
  9. var authUserId = "ocid1.user.oc1..aaaaaaaatffa6hunpdesdzxs5wnspkoqbualx4gbnajomrfsm3h7fp6vr5jq";
  10. var keyFingerprint = "c2:ba:18:4a:61:93:4f:7e:81:1d:4a:4d:66:39:4f:ef";
  11. var privateKeyPath = "~/.oci/oci_api_key.pem";
  12. var identityDomain = "identity.us-ashburn-1.oraclecloud.com";
  13. var coreServicesDomain = "iaas.us-ashburn-1.oraclecloud.com";
  14. if (privateKeyPath.indexOf("~/") === 0) {
  15. privateKeyPath = privateKeyPath.replace("~", os.homedir())
  16. }
  17. var privateKey = fs.readFileSync(privateKeyPath, 'ascii');
  18. // signing function as described at https://docs.cloud.oracle.com/Content/API/Concepts/signingrequests.htm
  19. function sign(request, options) {
  20. var apiKeyId = options.tenancyId + "/" + options.userId + "/" + options.keyFingerprint;
  21. var headersToSign = [
  22. "host",
  23. "date",
  24. "(request-target)"
  25. ];
  26. var methodsThatRequireExtraHeaders = ["POST", "PUT"];
  27. if (methodsThatRequireExtraHeaders.indexOf(request.method.toUpperCase()) !== -1) {
  28. options.body = options.body || "";
  29. var shaObj = new jsSHA("SHA-256", "TEXT");
  30. shaObj.update(options.body);
  31. request.setHeader("Content-Length", options.body.length);
  32. request.setHeader("x-content-sha256", shaObj.getHash('B64'));
  33. headersToSign = headersToSign.concat([
  34. "content-type",
  35. "content-length",
  36. "x-content-sha256"
  37. ]);
  38. }
  39. httpSignature.sign(request, {
  40. key: options.privateKey,
  41. keyId: apiKeyId,
  42. headers: headersToSign
  43. });
  44. var newAuthHeaderValue = request.getHeader("Authorization").replace("Signature ", "Signature version=\"1\",");
  45. request.setHeader("Authorization", newAuthHeaderValue);
  46. }
  47. // generates a function to handle the https.request response object
  48. function handleRequest(callback) {
  49. return function (response) {
  50. var responseBody = "";
  51. response.on('data', function (chunk) {
  52. responseBody += chunk;
  53. });
  54. response.on('end', function () {
  55. callback(JSON.parse(responseBody));
  56. });
  57. }
  58. }
  59. // gets the user with the specified id
  60. function getUser(userId, callback) {
  61. var options = {
  62. host: identityDomain,
  63. path: "/20160918/users/" + encodeURIComponent(userId),
  64. };
  65. var request = https.request(options, handleRequest(callback));
  66. sign(request, {
  67. privateKey: privateKey,
  68. keyFingerprint: keyFingerprint,
  69. tenancyId: tenancyId,
  70. userId: authUserId
  71. });
  72. request.end();
  73. };
  74. // creates a Oracle Cloud Infrastructure VCN in the specified compartment
  75. function createVCN(compartmentId, displayName, cidrBlock, callback) {
  76. var body = JSON.stringify({
  77. compartmentId: compartmentId,
  78. displayName: displayName,
  79. cidrBlock: cidrBlock
  80. });
  81. var options = {
  82. host: coreServicesDomain,
  83. path: '/20160918/vcns',
  84. method: 'POST',
  85. headers: {
  86. "Content-Type": "application/json",
  87. }
  88. };
  89. var request = https.request(options, handleRequest(callback));
  90. sign(request, {
  91. body: body,
  92. privateKey: privateKey,
  93. keyFingerprint: keyFingerprint,
  94. tenancyId: tenancyId,
  95. userId: authUserId
  96. });
  97. request.end(body);
  98. };
  99. /************************************************************************/
  100. function getInvoices(callback) {
  101. const request = https.request("https://147.154.15.58/20191001/invoices?compartmentId=ocid1.tenancy.oc1..aaaaaaaa56ugo5lfjdiyb3n5dmk2t3kzs4kuwea3heu73bmmqycg7hmqpinq", handleRequest(callback));
  102. sign(request, {
  103. privateKey: privateKey,
  104. keyFingerprint: keyFingerprint,
  105. tenancyId: tenancyId,
  106. userId: authUserId
  107. });
  108. request.end();
  109. }
  110. function healthCheck(callback) {
  111. const request = https.request("https://147.154.15.58/20191001/actuator/health", handleRequest(callback));
  112. request.end();
  113. }
  114. /************************************************************************/
  115. // test the above functions
  116. // console.log("GET USER:");
  117. // getUser(authUserId, function (data) {
  118. // console.log(data);
  119. // });
  120. getInvoices(function (data) {
  121. console.log(data);
  122. });
  123. // healthCheck(function (data) {
  124. // console.log(data);
  125. // });