app.js 4.2 KB

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