2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' _ `\ /'_`\ |
|
2019-03-09 16:59:31 -03:00
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) |
|
2018-04-21 02:59:30 -03:00
|
|
|
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
|
|
|
|
| | | ( )_) | |
|
|
|
|
| (_) \___/' |
|
|
|
|
| |
|
|
|
|
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
|
|
|
|
| Licensed under the AGPL-3.0. |
|
2018-11-11 19:09:18 -02:00
|
|
|
| |
|
2018-04-21 02:59:30 -03:00
|
|
|
| According to our dual licensing model, this program can be used either |
|
|
|
|
| under the terms of the GNU Affero General Public License, version 3, |
|
|
|
|
| or under a proprietary license. |
|
|
|
|
| |
|
|
|
|
| The texts of the GNU Affero General Public License with an additional |
|
|
|
|
| permission and of our proprietary license can be found at and |
|
|
|
|
| in the LICENSE file you have received along with this program. |
|
|
|
|
| |
|
|
|
|
| This program is distributed in the hope that it will be useful, |
|
2018-09-09 18:11:41 -03:00
|
|
|
| but WITHOUT ANY WARRANTY without even the implied warranty of |
|
2018-04-21 02:59:30 -03:00
|
|
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
| GNU Affero General Public License for more details. |
|
|
|
|
| |
|
|
|
|
| "General Bots" is a registered trademark of Pragmatismo.io. |
|
|
|
|
| The licensing of the program under the AGPLv3 does not imply a |
|
|
|
|
| trademark license. Therefore any rights, title and interest in |
|
|
|
|
| our trademarks remain entirely with us. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
2018-11-11 19:09:18 -02:00
|
|
|
/**
|
|
|
|
* @fileoverview General Bots server core.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
2018-09-16 17:00:17 -03:00
|
|
|
|
2019-08-21 21:04:55 +00:00
|
|
|
const crypto = require('crypto');
|
2019-03-08 06:37:13 -03:00
|
|
|
import { WaterfallDialog } from 'botbuilder-dialogs';
|
2019-11-10 16:20:15 -03:00
|
|
|
import { GBMinInstance, IGBDialog, GBLog } from 'botlib';
|
2019-03-09 16:59:31 -03:00
|
|
|
import urlJoin = require('url-join');
|
2018-11-12 12:20:44 -02:00
|
|
|
import { GBConfigService } from '../../core.gbapp/services/GBConfigService';
|
|
|
|
import { GBDeployer } from '../../core.gbapp/services/GBDeployer';
|
2018-11-26 14:09:09 -02:00
|
|
|
import { GBImporter } from '../../core.gbapp/services/GBImporterService';
|
2018-11-12 12:20:44 -02:00
|
|
|
import { Messages } from '../strings';
|
2020-04-01 15:42:57 -03:00
|
|
|
import { GBAdminService } from '../services/GBAdminService';
|
2020-05-14 12:46:57 -03:00
|
|
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
|
|
|
|
2018-10-14 19:58:54 -03:00
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
/**
|
|
|
|
* Dialogs for administration tasks.
|
|
|
|
*/
|
2018-04-21 02:59:30 -03:00
|
|
|
export class AdminDialog extends IGBDialog {
|
2020-01-26 14:50:24 -03:00
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
/**
|
|
|
|
* Setup dialogs flows and define services call.
|
2018-09-16 17:00:17 -03:00
|
|
|
*
|
2018-09-09 14:39:37 -03:00
|
|
|
* @param bot The bot adapter.
|
|
|
|
* @param min The minimal bot instance data.
|
|
|
|
*/
|
2019-03-08 19:13:00 -03:00
|
|
|
public static setup(min: GBMinInstance) {
|
2018-09-09 14:39:37 -03:00
|
|
|
// Setup services.
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const importer = new GBImporter(min.core);
|
|
|
|
const deployer = new GBDeployer(min.core, importer);
|
2020-04-01 15:42:57 -03:00
|
|
|
const adminService = new GBAdminService(min.core);
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2019-01-31 11:32:33 -02:00
|
|
|
AdminDialog.setupSecurityDialogs(min);
|
|
|
|
|
2018-11-01 21:06:11 -03:00
|
|
|
min.dialogs.add(
|
2018-11-12 12:20:44 -02:00
|
|
|
new WaterfallDialog('/admin', [
|
2018-11-01 21:06:11 -03:00
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
const prompt = Messages[locale].authenticate;
|
2018-12-06 10:16:28 -02:00
|
|
|
|
|
|
|
return await step.prompt('textPrompt', prompt);
|
2020-03-08 09:24:28 -03:00
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
const sensitive = step.result;
|
2018-12-06 10:16:28 -02:00
|
|
|
|
2020-04-15 05:08:50 +00:00
|
|
|
if (sensitive === min.instance.adminPass) {
|
2020-03-08 09:24:28 -03:00
|
|
|
await step.context.sendActivity(Messages[locale].welcome);
|
2018-12-06 10:16:28 -02:00
|
|
|
|
2020-03-08 09:24:28 -03:00
|
|
|
return await step.prompt('textPrompt', Messages[locale].which_task);
|
|
|
|
} else {
|
|
|
|
await step.context.sendActivity(Messages[locale].wrong_password);
|
2018-12-06 10:16:28 -02:00
|
|
|
|
2020-03-08 09:24:28 -03:00
|
|
|
return await step.endDialog();
|
|
|
|
}
|
2018-11-01 21:06:11 -03:00
|
|
|
},
|
|
|
|
async step => {
|
2019-03-08 19:13:00 -03:00
|
|
|
const locale: string = step.context.activity.locale;
|
|
|
|
// tslint:disable-next-line:no-unsafe-any
|
|
|
|
const text: string = step.result;
|
2018-11-12 12:20:44 -02:00
|
|
|
const cmdName = text.split(' ')[0];
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2019-07-18 20:42:08 -03:00
|
|
|
await step.context.sendActivity(Messages[locale].working(cmdName));
|
2018-11-01 21:06:11 -03:00
|
|
|
let unknownCommand = false;
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2019-08-23 14:36:47 -03:00
|
|
|
try {
|
|
|
|
|
|
|
|
|
|
|
|
if (text === 'quit') {
|
|
|
|
return await step.replaceDialog('/');
|
2020-05-12 09:06:47 -03:00
|
|
|
} else if (cmdName === 'deployPackage' || cmdName === 'dp') {
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.deployPackageCommand(min, text, deployer);
|
2019-08-23 14:36:47 -03:00
|
|
|
|
2019-11-10 16:20:15 -03:00
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
2020-05-12 09:06:47 -03:00
|
|
|
} else if (cmdName === 'redeployPackage' || cmdName === 'rp') {
|
2019-08-23 14:36:47 -03:00
|
|
|
await step.context.sendActivity('The package is being *unloaded*...');
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.undeployPackageCommand(text, min);
|
2019-08-23 14:36:47 -03:00
|
|
|
await step.context.sendActivity('Now, *deploying* package...');
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.deployPackageCommand(min, text, deployer);
|
2019-08-23 14:36:47 -03:00
|
|
|
await step.context.sendActivity('Package deployed. Just need to rebuild the index... Doing it right now.');
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.rebuildIndexPackageCommand(min, deployer);
|
2019-08-23 14:36:47 -03:00
|
|
|
await step.context.sendActivity('Finished importing of that .gbkb package. Thanks.');
|
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
2020-05-12 09:06:47 -03:00
|
|
|
} else if (cmdName === 'undeployPackage' || cmdName === 'up') {
|
2019-08-23 14:36:47 -03:00
|
|
|
await step.context.sendActivity('The package is being *undeployed*...');
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.undeployPackageCommand(text, min);
|
2019-08-23 14:36:47 -03:00
|
|
|
await step.context.sendActivity('Package *undeployed*.');
|
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
2020-05-12 09:06:47 -03:00
|
|
|
} else if (cmdName === 'rebuildIndex' || cmdName === 'ri') {
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.rebuildIndexPackageCommand(min, deployer);
|
2019-08-23 14:36:47 -03:00
|
|
|
|
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
|
|
|
} else if (cmdName === 'syncBotServer') {
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.syncBotServerCommand(min, deployer);
|
2019-08-23 14:36:47 -03:00
|
|
|
|
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
|
|
|
} else if (cmdName === 'setupSecurity') {
|
|
|
|
return await step.beginDialog('/setupSecurity');
|
|
|
|
} else {
|
|
|
|
unknownCommand = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unknownCommand) {
|
|
|
|
await step.context.sendActivity(Messages[locale].unknown_command);
|
|
|
|
} else {
|
|
|
|
await step.context.sendActivity(Messages[locale].finished_working);
|
|
|
|
}
|
2020-03-08 09:24:28 -03:00
|
|
|
|
2019-08-23 14:36:47 -03:00
|
|
|
} catch (error) {
|
|
|
|
await step.context.sendActivity(error.message);
|
2018-11-01 21:06:11 -03:00
|
|
|
}
|
2019-08-23 14:36:47 -03:00
|
|
|
await step.replaceDialog('/ask', { isReturning: true });
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2019-08-23 14:36:47 -03:00
|
|
|
|
2018-11-01 21:06:11 -03:00
|
|
|
])
|
|
|
|
);
|
2020-05-12 09:06:47 -03:00
|
|
|
|
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog('/publish', [
|
|
|
|
async step => {
|
2020-05-15 14:07:30 -03:00
|
|
|
const botId = min.instance.botId;
|
2020-05-12 09:06:47 -03:00
|
|
|
const locale = step.context.activity.locale;
|
2020-05-14 12:46:57 -03:00
|
|
|
await step.context.sendActivity(Messages[locale].working('Publishing'));
|
2020-05-15 14:07:30 -03:00
|
|
|
|
2020-05-14 12:46:57 -03:00
|
|
|
step.activeDialog.state.options.args = (step.options as any).args;
|
|
|
|
let args = step.activeDialog.state.options.args.split(' ');
|
|
|
|
let filename = args[0];
|
|
|
|
const packages = [];
|
|
|
|
if (filename === null) {
|
|
|
|
await step.context.sendActivity(`Starting publishing for all bot packages...`);
|
2020-05-15 14:07:30 -03:00
|
|
|
packages.push(`${botId}.gbkb`);
|
|
|
|
packages.push(`${botId}.gbdialog`);
|
|
|
|
packages.push(`${botId}.gbot`);
|
|
|
|
packages.push(`${botId}.gbtheme`);
|
|
|
|
packages.push(`${botId}.gbapp`);
|
|
|
|
packages.push(`${botId}.gblib`);
|
2020-05-14 12:46:57 -03:00
|
|
|
} else {
|
|
|
|
await step.context.sendActivity(`Starting publishing for ${filename}...`);
|
|
|
|
packages.push(filename);
|
|
|
|
}
|
2020-05-12 09:06:47 -03:00
|
|
|
|
|
|
|
try {
|
|
|
|
|
2020-05-14 12:46:57 -03:00
|
|
|
await CollectionUtil.asyncForEach(packages, async packageName => {
|
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
const cmd1 = `deployPackage ${process.env.STORAGE_SITE} /${process.env.STORAGE_LIBRARY}/${botId}.gbai/${packageName}`;
|
2020-05-14 12:46:57 -03:00
|
|
|
|
|
|
|
if (await (deployer as any).getStoragePackageByName(min.instance.instanceId,
|
|
|
|
packageName) !== null) { // TODO: Move to interface.
|
|
|
|
const cmd2 = `undeployPackage ${packageName}`;
|
|
|
|
await GBAdminService.undeployPackageCommand(cmd2, min);
|
|
|
|
}
|
|
|
|
await GBAdminService.deployPackageCommand(min, cmd1, deployer);
|
|
|
|
if (packageName.endsWith('.gbkb')) {
|
|
|
|
await step.context.sendActivity('Rebuilding my own index, wait a minute, please...');
|
|
|
|
await GBAdminService.rebuildIndexPackageCommand(min, deployer);
|
|
|
|
}
|
|
|
|
await step.context.sendActivity(`Finished publishing ${packageName}.`);
|
|
|
|
});
|
2020-05-12 09:06:47 -03:00
|
|
|
|
2020-05-15 14:07:30 -03:00
|
|
|
return await step.replaceDialog('/ask', { isReturning: true });
|
2020-05-12 09:06:47 -03:00
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
await step.context.sendActivity(error.message);
|
|
|
|
}
|
|
|
|
await step.replaceDialog('/ask', { isReturning: true });
|
|
|
|
|
|
|
|
}]));
|
2018-09-16 17:00:17 -03:00
|
|
|
}
|
|
|
|
|
2020-05-12 09:06:47 -03:00
|
|
|
|
2019-02-23 13:17:21 -03:00
|
|
|
private static setupSecurityDialogs(min: GBMinInstance) {
|
2019-01-31 11:32:33 -02:00
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog('/setupSecurity', [
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
const prompt = Messages[locale].enter_authenticator_tenant;
|
|
|
|
|
|
|
|
return await step.prompt('textPrompt', prompt);
|
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
step.activeDialog.state.authenticatorTenant = step.result;
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
const prompt = Messages[locale].enter_authenticator_authority_host_url;
|
|
|
|
|
|
|
|
return await step.prompt('textPrompt', prompt);
|
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
step.activeDialog.state.authenticatorAuthorityHostUrl = step.result;
|
|
|
|
|
|
|
|
await min.adminService.updateSecurityInfo(
|
|
|
|
min.instance.instanceId,
|
|
|
|
step.activeDialog.state.authenticatorTenant,
|
2020-05-14 17:16:27 -03:00
|
|
|
step.activeDialog.state.authenticatorAuthorityHostUrl
|
2019-01-31 11:32:33 -02:00
|
|
|
);
|
|
|
|
|
|
|
|
const locale = step.context.activity.locale;
|
2019-08-21 21:04:55 +00:00
|
|
|
const buf = Buffer.alloc(16);
|
|
|
|
const state = `${min.instance.instanceId}${crypto.randomFillSync(buf).toString('hex')}`;
|
2020-05-12 09:06:47 -03:00
|
|
|
|
2019-03-08 19:13:00 -03:00
|
|
|
min.adminService.setValue(min.instance.instanceId, 'AntiCSRFAttackState', state);
|
2019-01-31 11:32:33 -02:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
const url = `https://login.microsoftonline.com/${
|
|
|
|
min.instance.authenticatorTenant
|
2020-05-14 17:16:27 -03:00
|
|
|
}/oauth2/authorize?client_id=${min.instance.marketplaceId}&response_type=code&redirect_uri=${urlJoin(
|
2019-05-15 22:30:14 -03:00
|
|
|
min.instance.botEndpoint,
|
|
|
|
min.instance.botId,
|
|
|
|
'/token'
|
|
|
|
)}&state=${state}&response_mode=query`;
|
2019-01-31 11:32:33 -02:00
|
|
|
|
|
|
|
await step.context.sendActivity(Messages[locale].consent(url));
|
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
return await step.replaceDialog('/ask', { isReturning: true });
|
2019-01-31 11:32:33 -02:00
|
|
|
}
|
|
|
|
])
|
|
|
|
);
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
}
|