|
| 1 | +// ============================================================= |
| 2 | +// @module netbackup |
| 3 | +// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 4 | +// This file contains modules to call NetBackup APIs. |
| 5 | +// ============================================================= |
| 6 | + |
| 7 | +var fs = require('fs'); |
| 8 | +var https = require('https'); |
| 9 | +var pem = require('pem'); |
| 10 | +var stdio = require('stdio'); |
| 11 | + |
| 12 | +var exportedMethod = {}; |
| 13 | + |
| 14 | +// To disable certificate/ssl verification, uncomment following line |
| 15 | +// process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; |
| 16 | + |
| 17 | +var CA_CERT_PATH |
| 18 | + = isWindows() ? "C:\\temp\\cacert.pem" : "\temp\cacert.pem"; |
| 19 | + |
| 20 | +exportedMethod.getCACertificate = function (server, port, contentType, verbose) { |
| 21 | + var getCAURI = "/netbackup/security/cacert"; |
| 22 | + |
| 23 | + var optionsForCACert = { |
| 24 | + method: "GET", |
| 25 | + hostname: server, |
| 26 | + port: port, |
| 27 | + path: getCAURI, |
| 28 | + rejectUnauthorized: false // To avoid an error, because the cert is unauthorized |
| 29 | + }; |
| 30 | + |
| 31 | + if (fs.existsSync(CA_CERT_PATH)) { |
| 32 | + stdio.question('CA certificate is already present, Do you want to override certificate? ', ['y', 'n'], function (err, answer) { |
| 33 | + if (answer === 'y') { |
| 34 | + deployCACertificate(optionsForCACert, verbose); |
| 35 | + } else { |
| 36 | + return; |
| 37 | + } |
| 38 | + }); |
| 39 | + } else { |
| 40 | + deployCACertificate(optionsForCACert, verbose); |
| 41 | + } |
| 42 | +} // End of Get CA Certificate API |
| 43 | + |
| 44 | +exportedMethod.loginWithUser = function (server, port, username, password, domainname, domaintype, contentType, verbose, returnedRes) { |
| 45 | + |
| 46 | + if (!fs.existsSync(CA_CERT_PATH)) { |
| 47 | + var error = {errorCode:404, errorMessage:'Login Failed: CA certificate is not present. Please run get_ca_cert.js.'}; |
| 48 | + returnedRes(error); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + var loginURI = "/netbackup/login"; |
| 53 | + |
| 54 | + // create the JSON object |
| 55 | + loginPayload = JSON.stringify({ |
| 56 | + "userName": username, |
| 57 | + "password": password, |
| 58 | + "domainName": domainname, |
| 59 | + "domainType": domaintype |
| 60 | + }); |
| 61 | + printOnConsole('\nLogin payload: ' + loginPayload, verbose); |
| 62 | + |
| 63 | + var optionForLoginURI = { |
| 64 | + method: 'POST', |
| 65 | + host: server, |
| 66 | + port: port, |
| 67 | + path: loginURI, |
| 68 | + ca: [fs.readFileSync(CA_CERT_PATH, { encoding: 'utf-8' })], |
| 69 | + rejectUnauthorized: true, |
| 70 | + requestCert: true, |
| 71 | + agent: false, |
| 72 | + "headers": { |
| 73 | + 'content-type': contentType, |
| 74 | + 'Content-Length': loginPayload.length |
| 75 | + } |
| 76 | + }; |
| 77 | + |
| 78 | + printOnConsole('\nWaiting for Login request to complete...', verbose); |
| 79 | + performRequest(optionForLoginURI, loginPayload, verbose, returnedRes); |
| 80 | + |
| 81 | +} // End of Login API |
| 82 | + |
| 83 | +exportedMethod.getHostDetails = function (server, port, jwt, contentType, verbose, returnedRes) { |
| 84 | + var hostsListURI = "/netbackup/config/hosts/hostmappings"; |
| 85 | + |
| 86 | + if (!fs.existsSync(CA_CERT_PATH)) { |
| 87 | + var error = {errorCode:404, errorMessage:'Get Host List Failed: CA certificate is not present. Please run get_ca_cert.js.'}; |
| 88 | + returnedRes(error); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + var optionForHostsListURI = { |
| 93 | + method: 'GET', |
| 94 | + host: server, |
| 95 | + port: port, |
| 96 | + path: hostsListURI, |
| 97 | + ca: [fs.readFileSync(CA_CERT_PATH, { encoding: 'utf-8' })], |
| 98 | + rejectUnauthorized: true, |
| 99 | + requestCert: true, |
| 100 | + agent: false, |
| 101 | + "headers": { |
| 102 | + 'content-type': contentType, |
| 103 | + 'authorization': jwt |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + printOnConsole("\nFetching hosts...", verbose); |
| 108 | + performRequest(optionForHostsListURI, null, verbose, returnedRes); |
| 109 | + |
| 110 | +} // END of getHostDetails |
| 111 | + |
| 112 | +exportedMethod.getNBImages = function (server, port, jwt, contentType, verbose, returnedRes) { |
| 113 | + var hostsListURI = "/netbackup/catalog/images"; |
| 114 | + |
| 115 | + if (!fs.existsSync(CA_CERT_PATH)) { |
| 116 | + var error = {errorCode:404, errorMessage:'Get NB Images Failed: CA certificate is not present. Please run get_ca_cert.js.'}; |
| 117 | + returnedRes(error); |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + var optionForHostsListURI = { |
| 122 | + method: 'GET', |
| 123 | + host: server, |
| 124 | + port: port, |
| 125 | + path: hostsListURI, |
| 126 | + ca: [fs.readFileSync(CA_CERT_PATH, { encoding: 'utf-8' })], |
| 127 | + rejectUnauthorized: true, |
| 128 | + requestCert: true, |
| 129 | + agent: false, |
| 130 | + "headers": { |
| 131 | + 'content-type': contentType, |
| 132 | + 'authorization': jwt |
| 133 | + } |
| 134 | + }; |
| 135 | + printOnConsole("\nFetching NB Images...", verbose); |
| 136 | + performRequest(optionForHostsListURI, null, verbose, returnedRes); |
| 137 | + |
| 138 | +} // END of getNBImages |
| 139 | + |
| 140 | +exportedMethod.printOnConsole = function (stmt, verbose) { |
| 141 | + printOnConsole(stmt, verbose); |
| 142 | +} |
| 143 | + |
| 144 | +function deployCACertificate(option, verbose) { |
| 145 | + printOnConsole("Deploying CA Certificate...", verbose); |
| 146 | + performRequest(option, null, verbose, saveCACertificate); |
| 147 | +} |
| 148 | + |
| 149 | +function saveCACertificate(data) { |
| 150 | + var answer = pem.getFingerprint(data.webRootCert, 'sha1', function (err, result) { |
| 151 | + console.info(result.fingerprint); |
| 152 | + stdio.question('Do you want to trust this fingerprint? ', ['y', 'n'], function (err, answer) { |
| 153 | + if (answer === 'y') { |
| 154 | + fs.writeFileSync(CA_CERT_PATH, data.webRootCert); |
| 155 | + console.info("CA Certificate is saved successfully."); |
| 156 | + } else { |
| 157 | + console.info("CA Certificate operation failed."); |
| 158 | + } |
| 159 | + }); |
| 160 | + } ); |
| 161 | +} |
| 162 | + |
| 163 | +function performRequest(option, payload, verbose, returnedRes) { |
| 164 | + var req = https.request(option, function (response) { |
| 165 | + printOnConsole('\nStatusCode: ' + response.statusCode, verbose); |
| 166 | + response.setEncoding('utf-8'); |
| 167 | + var data; |
| 168 | + response.on('data', function (resBody) { |
| 169 | + printOnConsole("\nRaw response body: " + resBody, verbose); |
| 170 | + data = JSON.parse(resBody); |
| 171 | + }); |
| 172 | + response.on('end', function () { |
| 173 | + returnedRes(data); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + if(payload !=null) { |
| 178 | + req.write(payload); |
| 179 | + } |
| 180 | + req.end(); |
| 181 | + req.on('error', function (errorData) { |
| 182 | + console.info('Error ocuured : \n'); |
| 183 | + console.error(errorData); |
| 184 | + }); |
| 185 | +} |
| 186 | + |
| 187 | +function isWindows(){ |
| 188 | + if (/^win/i.test(process.platform)) { |
| 189 | + return true; |
| 190 | + } else { |
| 191 | + return false; |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +function printOnConsole(stmt, verbose) { |
| 196 | + if (verbose) { |
| 197 | + console.info(stmt); |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +module.exports = exportedMethod; |
0 commit comments