|
|
@@ -0,0 +1,155 @@
|
|
|
+var fs = require('fs');
|
|
|
+var https = require('https');
|
|
|
+var os = require('os');
|
|
|
+var httpSignature = require('http-signature');
|
|
|
+var jsSHA = require("jssha");
|
|
|
+
|
|
|
+
|
|
|
+// TODO: update these values to your own
|
|
|
+var tenancyId = "ocid1.tenancy.oc1..aaaaaaaa56ugo5lfjdiyb3n5dmk2t3kzs4kuwea3heu73bmmqycg7hmqpinq";
|
|
|
+var authUserId = "ocid1.user.oc1..aaaaaaaatffa6hunpdesdzxs5wnspkoqbualx4gbnajomrfsm3h7fp6vr5jq";
|
|
|
+var keyFingerprint = "c2:ba:18:4a:61:93:4f:7e:81:1d:4a:4d:66:39:4f:ef";
|
|
|
+var privateKeyPath = "~/.oci/oci_api_key.pem";
|
|
|
+
|
|
|
+
|
|
|
+var identityDomain = "identity.us-ashburn-1.oraclecloud.com";
|
|
|
+var coreServicesDomain = "iaas.us-ashburn-1.oraclecloud.com";
|
|
|
+
|
|
|
+
|
|
|
+if (privateKeyPath.indexOf("~/") === 0) {
|
|
|
+ privateKeyPath = privateKeyPath.replace("~", os.homedir())
|
|
|
+}
|
|
|
+var privateKey = fs.readFileSync(privateKeyPath, 'ascii');
|
|
|
+
|
|
|
+
|
|
|
+// signing function as described at https://docs.cloud.oracle.com/Content/API/Concepts/signingrequests.htm
|
|
|
+function sign(request, options) {
|
|
|
+
|
|
|
+ var apiKeyId = options.tenancyId + "/" + options.userId + "/" + options.keyFingerprint;
|
|
|
+
|
|
|
+ var headersToSign = [
|
|
|
+ "host",
|
|
|
+ "date",
|
|
|
+ "(request-target)"
|
|
|
+ ];
|
|
|
+
|
|
|
+ var methodsThatRequireExtraHeaders = ["POST", "PUT"];
|
|
|
+
|
|
|
+ if (methodsThatRequireExtraHeaders.indexOf(request.method.toUpperCase()) !== -1) {
|
|
|
+ options.body = options.body || "";
|
|
|
+
|
|
|
+ var shaObj = new jsSHA("SHA-256", "TEXT");
|
|
|
+ shaObj.update(options.body);
|
|
|
+
|
|
|
+ request.setHeader("Content-Length", options.body.length);
|
|
|
+ request.setHeader("x-content-sha256", shaObj.getHash('B64'));
|
|
|
+
|
|
|
+ headersToSign = headersToSign.concat([
|
|
|
+ "content-type",
|
|
|
+ "content-length",
|
|
|
+ "x-content-sha256"
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ httpSignature.sign(request, {
|
|
|
+ key: options.privateKey,
|
|
|
+ keyId: apiKeyId,
|
|
|
+ headers: headersToSign
|
|
|
+ });
|
|
|
+
|
|
|
+ var newAuthHeaderValue = request.getHeader("Authorization").replace("Signature ", "Signature version=\"1\",");
|
|
|
+ request.setHeader("Authorization", newAuthHeaderValue);
|
|
|
+}
|
|
|
+
|
|
|
+// generates a function to handle the https.request response object
|
|
|
+function handleRequest(callback) {
|
|
|
+
|
|
|
+ return function (response) {
|
|
|
+ var responseBody = "";
|
|
|
+
|
|
|
+ response.on('data', function (chunk) {
|
|
|
+ responseBody += chunk;
|
|
|
+ });
|
|
|
+
|
|
|
+ response.on('end', function () {
|
|
|
+ callback(JSON.parse(responseBody));
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// gets the user with the specified id
|
|
|
+function getUser(userId, callback) {
|
|
|
+
|
|
|
+ var options = {
|
|
|
+ host: identityDomain,
|
|
|
+ path: "/20160918/users/" + encodeURIComponent(userId),
|
|
|
+ };
|
|
|
+
|
|
|
+ var request = https.request(options, handleRequest(callback));
|
|
|
+
|
|
|
+ sign(request, {
|
|
|
+ privateKey: privateKey,
|
|
|
+ keyFingerprint: keyFingerprint,
|
|
|
+ tenancyId: tenancyId,
|
|
|
+ userId: authUserId
|
|
|
+ });
|
|
|
+
|
|
|
+ request.end();
|
|
|
+};
|
|
|
+
|
|
|
+// creates a Oracle Cloud Infrastructure VCN in the specified compartment
|
|
|
+function createVCN(compartmentId, displayName, cidrBlock, callback) {
|
|
|
+
|
|
|
+ var body = JSON.stringify({
|
|
|
+ compartmentId: compartmentId,
|
|
|
+ displayName: displayName,
|
|
|
+ cidrBlock: cidrBlock
|
|
|
+ });
|
|
|
+
|
|
|
+ var options = {
|
|
|
+ host: coreServicesDomain,
|
|
|
+ path: '/20160918/vcns',
|
|
|
+ method: 'POST',
|
|
|
+ headers: {
|
|
|
+ "Content-Type": "application/json",
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ var request = https.request(options, handleRequest(callback));
|
|
|
+
|
|
|
+ sign(request, {
|
|
|
+ body: body,
|
|
|
+ privateKey: privateKey,
|
|
|
+ keyFingerprint: keyFingerprint,
|
|
|
+ tenancyId: tenancyId,
|
|
|
+ userId: authUserId
|
|
|
+ });
|
|
|
+
|
|
|
+ request.end(body);
|
|
|
+};
|
|
|
+
|
|
|
+/************************************************************************/
|
|
|
+
|
|
|
+function getStates(callback) {
|
|
|
+ const request = https.request("https://130.35.55.238:7002/wl/oc_gateway/20190701/states", handleRequest(callback));
|
|
|
+ sign(request, {
|
|
|
+ privateKey: privateKey,
|
|
|
+ keyFingerprint: keyFingerprint,
|
|
|
+ tenancyId: tenancyId,
|
|
|
+ userId: authUserId
|
|
|
+ });
|
|
|
+ request.end();
|
|
|
+}
|
|
|
+
|
|
|
+/************************************************************************/
|
|
|
+
|
|
|
+// test the above functions
|
|
|
+// console.log("GET USER:");
|
|
|
+
|
|
|
+// getUser(authUserId, function (data) {
|
|
|
+// console.log(data);
|
|
|
+// });
|
|
|
+
|
|
|
+getStates(function (data) {
|
|
|
+ console.log(data);
|
|
|
+});
|