var fs = require('fs'); var https = require('https'); var os = require('os'); var httpSignature = require('http-signature'); var jsSHA = require("jssha"); process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0; // TODO: update these values to your own var authTenancyId = "ocid1.tenancy.oc1..aaaaaaaa56ugo5lfjdiyb3n5dmk2t3kzs4kuwea3heu73bmmqycg7hmqpinq"; var authUserId = "ocid1.user.oc1..aaaaaaaatffa6hunpdesdzxs5wnspkoqbualx4gbnajomrfsm3h7fp6vr5jq"; var authKeyFingerprint = "c2:ba:18:4a:61:93:4f:7e:81:1d:4a:4d:66:39:4f:ef"; var authPrivateKeyPath = "~/.oci/oci_api_key.pem"; var identityDomain = "identity.us-ashburn-1.oraclecloud.com"; var coreServicesDomain = "iaas.us-ashburn-1.oraclecloud.com"; const invoicesDomain = "147.154.15.58"; if (authPrivateKeyPath.indexOf("~/") === 0) { authPrivateKeyPath = authPrivateKeyPath.replace("~", os.homedir()) } var privateKey = fs.readFileSync(authPrivateKeyPath, '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) { console.log('!!! statusCode:', response.statusCode); console.log('!!! headers:', response.headers); var responseBody = ""; response.on('data', function (chunk) { console.log('!!! data:', { chunk }); responseBody += chunk; }); response.on('end', function () { console.log('!!! end:', { responseBody }); callback(responseBody ? 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: authKeyFingerprint, tenancyId: authTenancyId, 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: authKeyFingerprint, tenancyId: authTenancyId, userId: authUserId }); request.end(body); }; /************************************************************************/ function getInvoices(callback, compartmentId = authTenancyId, status = "OPEN,PAST_DUE,PAYMENT_SUBMITTED,CLOSED", type = "HARDWARE,SUBSCRIPTION,SUPPORT,LICENSE,EDUCATION,CONSULTING,SERVICE,USAGE") { const request = https.request( { host: invoicesDomain, path: `/20191001/invoices?compartmentId=${compartmentId}&status=${status}&type=${type}`, method: "GET" }, handleRequest(callback) ); sign(request, { privateKey: privateKey, keyFingerprint: authKeyFingerprint, tenancyId: authTenancyId, userId: authUserId }); request.end(); } function healthCheck(callback) { const request = https.request("https://147.154.15.58/20191001/actuator/health", handleRequest(callback)); request.end(); } /************************************************************************/ // test the above functions // console.log("GET USER:"); // getUser(authUserId, function (data) { // console.log(data); // }); healthCheck(function (data) { console.log("+++ healthCheck"); console.log("+++", data); }); getInvoices( function (data) { console.log("+++ getInvoices"); console.log("+++", data); }, authTenancyId, "OPEN,PAST_DUE,PAYMENT_SUBMITTED,CLOSED", "HARDWARE" );