Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

741 support deployment against authenticated engines #1063

Merged
merged 3 commits into from
Dec 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 0 additions & 88 deletions app/lib/createDeployer.js

This file was deleted.

166 changes: 166 additions & 0 deletions app/lib/deployer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
'use strict';

class Deployer {
constructor({ fetch, fs, FormData }) {
this.fetch = fetch;
this.fs = fs;
this.formDataConstructor = FormData;
}

/**
* Deploy diagram to the given endpoint URL.
*/
async deploy(url, options, cb = noop) {
try {
this.validateDeployParams(url, options);

const requestParams = this.getRequestParams(options);

const serverResponse = await this.fetch(url, requestParams);

if (!serverResponse.ok) {
const error = await getErrorFromResponse(serverResponse);
throw error;
}

let response;

try {
response = await serverResponse.json();
} catch (error) {
response = serverResponse.statusText;
}

return cb(null, response);
} catch (error) {
error.deploymentName = options.deploymentName;

return cb(error);
}
}

validateDeployParams(url, { deploymentName, file }) {
if (!deploymentName) {
throw new Error('Failed to deploy process, deployment name must be provided.');
}

if (!file || !file.name || !file.path) {
throw new Error('Failed to deploy process, file name and path must be provided.');
}

if (!url) {
throw new Error('Failed to deploy process, endpoint url must not be empty.');
}
}

getRequestParams(options) {
const body = this.getBody(options);
const headers = this.getHeaders(options);

return {
body,
headers,
method: 'POST'
};
}

getBody({ deploymentName, tenantId, file = {} }) {
const form = this.getFormData();

form.append('deployment-name', deploymentName);

if (tenantId) {
form.append('tenant-id', tenantId);
}

form.append('deploy-changed-only', 'true');

form.append(file.name, this.fs.createReadStream(file.path));

return form;
}

getHeaders({ auth }) {
const headers = {};

if (auth) {
headers['Authorization'] = this.getAuthHeader(auth);
}

return headers;
}

getAuthHeader(auth) {
const authHeaderBuilder = new AuthHeaderBuilder(auth);

return authHeaderBuilder.build();
}

getFormData() {
return new this.formDataConstructor();
}
}


module.exports = Deployer;



// helpers //////
class AuthHeaderBuilder {
constructor(options) {
this.options = options;
}

build() {
const {
bearer,
password,
username
} = this.options;

if (bearer) {
return this.getBearerHeader(bearer);
}

if (username && password) {
return this.getBasicHeader(username, password);
}

throw new Error('Unknown auth options.');
}

getBearerHeader(bearer) {
return `Bearer ${bearer}`;
}

getBasicHeader(username, password) {
const credentials = btoa(`${username}:${password}`);

return `Basic ${credentials}`;
}
}

function btoa(input) {
return Buffer.from(input, 'utf8').toString('base64');
}

function noop() { }


async function getErrorFromResponse(response) {
const error = new Error();

try {
const body = await response.json();
error.message = body.message;
} catch (_) {
error.message = response.statusText;
}

error.status = response.status;
error.statusText = response.statusText;
error.url = response.url;

return error;
}
7 changes: 5 additions & 2 deletions app/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var Platform = require('./platform'),
Menu = require('./menu'),
Cli = require('./cli'),
Plugins = require('./plugins'),
deploy = require('./createDeployer')({ fetch, fs, FormData });
Deployer = require('./deployer');

var browserOpen = require('./util/browser-open'),
renderer = require('./util/renderer');
Expand Down Expand Up @@ -86,6 +86,9 @@ var fileSystem = new FileSystem({
dialog: dialog
});

// bootstrap deployer
var deployer = new Deployer({ fetch, fs, FormData });


// make app a singleton
if (config.get('single-instance', true)) {
Expand Down Expand Up @@ -409,7 +412,7 @@ app.on('ready', function() {
function handleDeployment(data, done) {
const { endpointUrl } = data;

deploy(endpointUrl, data, function(error, result) {
deployer.deploy(endpointUrl, data, function(error, result) {

if (error) {
console.error('failed to deploy', error);
Expand Down
Loading