Skip to content

Commit

Permalink
createTunnel
Browse files Browse the repository at this point in the history
  • Loading branch information
adrai committed Jun 27, 2017
1 parent b4aedf5 commit ee36ee2
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ node_modules/**/*
private_node_modules
private_node_modules/**/*

package-lock.json

.config
.node-gyp
.npm
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
60 changes: 60 additions & 0 deletions fns/createTunnel.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
};
};
46 changes: 46 additions & 0 deletions fns/getCode.js
Original file line number Diff line number Diff line change
@@ -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);
});
};
};
30 changes: 30 additions & 0 deletions fns/getEnv.js
Original file line number Diff line number Diff line change
@@ -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);
});
};
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit ee36ee2

Please sign in to comment.