app.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. console.log('!!! statusCode:', response.statusCode);
  51. console.log('!!! headers:', response.headers);
  52. var responseBody = "";
  53. response.on('data', function (chunk) {
  54. console.log('!!! data:', { chunk });
  55. responseBody += chunk;
  56. });
  57. response.on('end', function () {
  58. console.log('!!! end:', { responseBody });
  59. callback(responseBody ? JSON.parse(responseBody) : "");
  60. });
  61. }
  62. }
  63. // gets the user with the specified id
  64. function getUser(userId, callback) {
  65. var options = {
  66. host: identityDomain,
  67. path: "/20160918/users/" + encodeURIComponent(userId),
  68. };
  69. var request = https.request(options, handleRequest(callback));
  70. sign(request, {
  71. privateKey: privateKey,
  72. keyFingerprint: keyFingerprint,
  73. tenancyId: tenancyId,
  74. userId: authUserId
  75. });
  76. request.end();
  77. };
  78. // creates a Oracle Cloud Infrastructure VCN in the specified compartment
  79. function createVCN(compartmentId, displayName, cidrBlock, callback) {
  80. var body = JSON.stringify({
  81. compartmentId: compartmentId,
  82. displayName: displayName,
  83. cidrBlock: cidrBlock
  84. });
  85. var options = {
  86. host: coreServicesDomain,
  87. path: '/20160918/vcns',
  88. method: 'POST',
  89. headers: {
  90. "Content-Type": "application/json",
  91. }
  92. };
  93. var request = https.request(options, handleRequest(callback));
  94. sign(request, {
  95. body: body,
  96. privateKey: privateKey,
  97. keyFingerprint: keyFingerprint,
  98. tenancyId: tenancyId,
  99. userId: authUserId
  100. });
  101. request.end(body);
  102. };
  103. /************************************************************************/
  104. function getInvoices(callback) {
  105. const request = https.request("https://147.154.15.58/20191001/invoices?compartmentId=ocid1.tenancy.oc1..aaaaaaaa56ugo5lfjdiyb3n5dmk2t3kzs4kuwea3heu73bmmqycg7hmqpinq", handleRequest(callback));
  106. sign(request, {
  107. privateKey: privateKey,
  108. keyFingerprint: keyFingerprint,
  109. tenancyId: tenancyId,
  110. userId: authUserId
  111. });
  112. request.end();
  113. }
  114. function healthCheck(callback) {
  115. const request = https.request("https://147.154.15.58/20191001/actuator/health", handleRequest(callback));
  116. request.end();
  117. }
  118. /************************************************************************/
  119. // test the above functions
  120. // console.log("GET USER:");
  121. // getUser(authUserId, function (data) {
  122. // console.log(data);
  123. // });
  124. healthCheck(function (data) {
  125. console.log("+++ healthCheck");
  126. console.log("+++", data);
  127. });
  128. getInvoices(function (data) {
  129. console.log("+++ getInvoices");
  130. console.log("+++", data);
  131. });