diff --git a/.gitignore b/.gitignore index 635d1b5..679b69e 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,8 @@ node_modules/**/* private_node_modules private_node_modules/**/* +package-lock.json + .config .node-gyp .npm diff --git a/CHANGELOG.md b/CHANGELOG.md index bef21cc..29eb54c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [v1.6.0](https://github.com/push2cloud/cf-adapter/compare/v1.5.5...v1.6.0) +- createTunnel +- getCode +- getEnv + ## [v1.5.5](https://github.com/push2cloud/cf-adapter/compare/v1.5.3...v1.5.5) - bindService check if already existing diff --git a/fns/createTunnel.js b/fns/createTunnel.js new file mode 100644 index 0000000..8f500e3 --- /dev/null +++ b/fns/createTunnel.js @@ -0,0 +1,60 @@ +const _ = require('lodash'); +const tunnel = require('tunnel-ssh'); + +module.exports = (api) => { + return (options, callback) => { + options = options || {}; + + if (!api.spaceGuid) { + return callback(new Error('Please provide a space! \n' + JSON.stringify(options, null, 2))); + } + + if (!options.appGuid && options.app && api.actualDeploymentConfig) { + var a = _.find(api.actualDeploymentConfig.apps, { name: options.app }); + if (a) { + options.appGuid = a.guid; + } + } + + if (!options.appGuid) { + return callback(new Error('Please provide an appGuid! \n' + JSON.stringify(options, null, 2))); + } + + if (!options.destinationHost) { + return callback(new Error('Please provide an destinationHost! \n' + JSON.stringify(options, null, 2))); + } + + if (!options.destinationPort) { + return callback(new Error('Please provide an destinationPort! \n' + JSON.stringify(options, null, 2))); + } + + if (!api.targetInfo.app_ssh_endpoint) { + return callback(new Error('No app_ssh_endpoint defined in targetInfo!')); + } + + const sshEndpoint = api.targetInfo.app_ssh_endpoint.split(':'); + const sshHost = sshEndpoint[0]; + const sshPort = parseInt(sshEndpoint[1]); + + options.instance = options.instance || 0; + + api.getCode((err, code) => { + if (err) return callback(err); + + const config = { + username: `cf:${options.appGuid}/${options.instance}`, + password: code, + host: sshHost, + port: sshPort, + dstHost: options.destinationHost, + dstPort: options.destinationPort, + localPort: options.localPort + }; + + tunnel(config, (err, tnl) => { + if (err) return callback(err); + callback(null, tnl); + }); + }); + }; +}; diff --git a/fns/getCode.js b/fns/getCode.js new file mode 100644 index 0000000..20d0842 --- /dev/null +++ b/fns/getCode.js @@ -0,0 +1,46 @@ +const request = require('request'); +const urlModule = require('url'); +const debug = require('debug')('push2cloud-cf-adapter:getCode'); + +module.exports = (api) => { + return (options, callback) => { + if (!callback) { + callback = options; + options = {}; + } + options = options || {}; + + if (!api.targetInfo) { + return callback(new Error('No taget information!')); + } + + if (!api.token) { + return callback(new Error('Please provide a token!')); + } + + request({ + method: 'GET', + baseUrl: api.targetInfo.authorization_endpoint, + uri: '/oauth/authorize', + rejectUnauthorized: api.options.rejectUnauthorized, + // json: true, + headers: { + Authorization: `${api.token.token_type} ${api.token.access_token}` + }, + qs: { + response_type: 'code', + grant_type: 'authorization_code', + client_id: api.targetInfo.app_ssh_oauth_client + }, + followRedirect: false + }, (err, response, result) => { + if (err) return callback(err); + + if (!response || !response.headers || !response.headers.location) return callback(new Error('No code received!')); + + const code = urlModule.parse(response.headers.location).query.substring('code='.length); + + callback(null, code); + }); + }; +}; diff --git a/fns/getEnv.js b/fns/getEnv.js new file mode 100644 index 0000000..16e8082 --- /dev/null +++ b/fns/getEnv.js @@ -0,0 +1,30 @@ +const _ = require('lodash'); + +module.exports = (api) => { + return (options, callback) => { + options = options || {}; + + if (!api.spaceGuid) { + return callback(new Error('Please provide a space! \n' + JSON.stringify(options, null, 2))); + } + + if (!options.appGuid && options.name && api.actualDeploymentConfig) { + var a = _.find(api.actualDeploymentConfig.apps, { name: options.name }); + if (a) { + options.appGuid = a.guid; + } + } + + if (!options.appGuid) { + return callback(new Error('Please provide an appGuid! \n' + JSON.stringify(options, null, 2))); + } + + api.graceRequest({ + method: 'GET', + uri: `/v2/apps/${options.appGuid}/env` + }, (err, response, result) => { + if (err) return callback(err); + callback(null, result); + }); + }; +}; diff --git a/package.json b/package.json index f42d777..e0dca67 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "lodash": "4.17.4", "request": "2.81.0", "semver": "5.3.0", + "tunnel-ssh": "4.1.3", "ws": "3.0.0" }, "devDependencies": {