app.js 5.2 KB

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