botserver/packages/admin.gbapp/services/GBAdminService.ts

261 lines
11 KiB
TypeScript
Raw Normal View History

/*****************************************************************************\
| ( )_ _ |
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ |
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) |
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
| | | ( )_) | |
| (_) \___/' |
| |
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
| Licensed under the AGPL-3.0. |
2018-11-11 19:09:18 -02: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, |
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
| 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';
import { AuthenticationContext, TokenResponse } from 'adal-node';
2022-12-14 08:23:39 -03:00
import { GBMinInstance, IGBAdminService, IGBCoreService, IGBDeployer, IGBInstance } from 'botlib';
2022-01-03 13:11:21 -03:00
import { FindOptions } from 'sequelize/types';
2022-11-19 23:34:58 -03:00
import urlJoin from 'url-join';
import { AzureDeployerService } from '../../azuredeployer.gbapp/services/AzureDeployerService.js';
import { GuaribasInstance } from '../../core.gbapp/models/GBModel.js';
import { GBConfigService } from '../../core.gbapp/services/GBConfigService.js';
import { GBDeployer } from '../../core.gbapp/services/GBDeployer.js';
import { GBImporter } from '../../core.gbapp/services/GBImporterService.js';
import { GBSharePointService } from '../../sharepoint.gblib/services/SharePointService.js';
import { GuaribasAdmin } from '../models/AdminModel.js';
import msRestAzure from 'ms-rest-azure';
import Path from 'path';
import { caseSensitive_Numbs_SpecialCharacters_PW, lowercase_PW } from 'super-strong-password-generator'
2022-11-19 23:34:58 -03:00
import crypto from 'crypto';
import Fs from 'fs';
2022-12-23 08:23:08 -03:00
import { GBServer } from '../../../src/app.js';
/**
* Services for server administration.
*/
export class GBAdminService implements IGBAdminService {
public static GB_PROMPT: string = 'GeneralBots: ';
public static masterBotInstanceId = 0;
public static StrongRegex = new RegExp('^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*+_-])(?=.{8,})');
public core: IGBCoreService;
constructor(core: IGBCoreService) {
this.core = core;
}
public static generateUuid(): string {
return crypto.randomUUID();
}
public static getNodeVersion() {
const packageJson = urlJoin(process.cwd(), 'package.json');
const pkg = JSON.parse(Fs.readFileSync(packageJson, 'utf8'));
return pkg.engines.node.replace('=', '');
}
public static async getADALTokenFromUsername(username: string, password: string) {
const credentials = await GBAdminService.getADALCredentialsFromUsername(username, password);
return (credentials as any).tokenCache._entries[0].accessToken;
}
public static async getADALCredentialsFromUsername(username: string, password: string) {
2022-11-19 23:34:58 -03:00
return await msRestAzure.loginWithUsernamePassword(username, password);
}
public static getMobileCode() {
return Math.trunc(Math.random() * 6);
}
public static getRndPassword(): string {
2022-12-29 14:47:19 -03:00
let password = caseSensitive_Numbs_SpecialCharacters_PW(15);
password = password.replace(/[\@\[\=\:\;\?\"\'\#]/gi, '*');
return password;
}
public static getRndReadableIdentifier(): string {
2022-12-29 14:47:19 -03:00
return lowercase_PW(14);
2020-12-04 13:44:33 -03:00
}
public static getNumberIdentifier(size: number = 14): string {
2022-12-29 14:47:19 -03:00
return Math.trunc(Math.random() * size).toString();
}
/**
* @see https://stackoverflow.com/a/52171480
*/
public static getHash(str: string, seed = 0) {
2022-11-19 23:34:58 -03:00
let h1 = 0xdeadbeef ^ seed,
h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < str.length; i++) {
ch = str.charCodeAt(i);
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
}
public static async undeployPackageCommand(text: string, min: GBMinInstance) {
2020-12-31 15:36:19 -03:00
const packageName = text.split(' ')[1];
const importer = new GBImporter(min.core);
const deployer = new GBDeployer(min.core, importer);
const localFolder = Path.join('work', `${min.instance.botId}.gbai`, Path.basename(packageName));
await deployer.undeployPackageFromLocalPath(min.instance, localFolder);
}
public static isSharePointPath(path: string) {
return path.indexOf('sharepoint.com') !== -1;
2020-12-31 15:36:19 -03:00
}
public static async deployPackageCommand(min: GBMinInstance, text: string, deployer: IGBDeployer) {
2020-12-31 15:36:19 -03:00
const packageName = text.split(' ')[1];
if (!this.isSharePointPath(packageName)) {
const additionalPath = GBConfigService.get('ADDITIONAL_DEPLOY_PATH');
if (additionalPath === undefined) {
throw new Error('ADDITIONAL_DEPLOY_PATH is not set and deployPackage was called.');
}
await deployer.deployPackage(min, urlJoin(additionalPath, packageName));
} else {
const siteName = text.split(' ')[1];
const folderName = text.split(' ')[2];
const localFolder = Path.join('work', `${min.instance.botId}.gbai`, Path.basename(folderName));
// .gbot packages are handled using storage API, so no download
// of local resources is required.
2022-11-19 23:34:58 -03:00
await deployer['downloadFolder'](min, Path.join('work', `${min.instance.botId}.gbai`), Path.basename(folderName));
2020-12-31 15:36:19 -03:00
await deployer.deployPackage(min, localFolder);
}
}
public static async rebuildIndexPackageCommand(min: GBMinInstance, deployer: GBDeployer) {
const service = await AzureDeployerService.createInstance(deployer);
2020-12-31 15:36:19 -03:00
await deployer.rebuildIndex(
min.instance,
service.getKBSearchSchema(min.instance.searchIndex)
2020-12-31 15:36:19 -03:00
);
}
public static async syncBotServerCommand(min: GBMinInstance, deployer: GBDeployer) {
2020-12-31 15:36:19 -03:00
const serverName = `${min.instance.botId}-server`;
const service = await AzureDeployerService.createInstance(deployer);
2020-12-31 15:36:19 -03:00
service.syncBotServerRepository(min.instance.botId, serverName);
}
public async setValue(instanceId: number, key: string, value: string) {
2022-01-03 13:11:21 -03:00
const options = <FindOptions>{ where: {} };
options.where = { key: key };
let admin = await GuaribasAdmin.findOne(options);
if (admin === null) {
admin = new GuaribasAdmin();
admin.key = key;
}
admin.value = value;
admin.instanceId = instanceId;
await admin.save();
}
public async updateSecurityInfo(
instanceId: number,
authenticatorTenant: string,
authenticatorAuthorityHostUrl: string
): Promise<IGBInstance> {
2022-01-03 13:11:21 -03:00
const options = <FindOptions>{ where: {} };
options.where = { instanceId: instanceId };
const item = await GuaribasInstance.findOne(options);
item.authenticatorTenant = authenticatorTenant;
item.authenticatorAuthorityHostUrl = authenticatorAuthorityHostUrl;
return item.save();
}
public async getValue(instanceId: number, key: string): Promise<string> {
2022-01-03 13:11:21 -03:00
const options = <FindOptions>{ where: {} };
options.where = { key: key, instanceId: instanceId };
const obj = await GuaribasAdmin.findOne(options);
2020-08-19 13:00:21 -03:00
return obj.value;
}
public async acquireElevatedToken(instanceId: number): Promise<string> {
const minBoot = GBServer.globals.minBoot;
2022-12-27 12:55:22 -03:00
instanceId = minBoot.instance.instanceId;
return new Promise<string>(async (resolve, reject) => {
const instance = await this.core.loadInstanceById(instanceId);
const expiresOn = new Date(await this.getValue(instanceId, 'expiresOn'));
if (expiresOn.getTime() > new Date().getTime()) {
const accessToken = await this.getValue(instanceId, 'accessToken');
resolve(accessToken);
} else {
const authorizationUrl = urlJoin(
instance.authenticatorAuthorityHostUrl,
instance.authenticatorTenant,
'/oauth2/authorize'
);
const refreshToken = await this.getValue(instanceId, 'refreshToken');
const resource = 'https://graph.microsoft.com';
const authenticationContext = new AuthenticationContext(authorizationUrl);
authenticationContext.acquireTokenWithRefreshToken(
refreshToken,
instance.marketplaceId,
instance.marketplacePassword,
resource,
async (err, res) => {
if (err !== null) {
reject(err);
} else {
const token = res as TokenResponse;
try {
await this.setValue(instanceId, 'accessToken', token.accessToken);
await this.setValue(instanceId, 'refreshToken', token.refreshToken);
await this.setValue(instanceId, 'expiresOn', token.expiresOn.toString());
resolve(token.accessToken);
} catch (error) {
reject(err);
}
}
}
);
}
});
}
public async publish(min: GBMinInstance, packageName: string, republish: boolean): Promise<void> { }
}