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

fix: [#4204] Fix remaining eslint warnings - botframework-config #4234

Merged
merged 3 commits into from
Jul 13, 2022
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
12 changes: 6 additions & 6 deletions libraries/botframework-config/etc/botframework-config.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ export class BotRecipe {
export class BotService extends AzureService implements IBotService {
constructor(source?: IBotService);
appId: string;
decrypt(secret: string, decryptString: (value: string, secret: string) => string): void;
encrypt(secret: string, encryptString: (value: string, secret: string) => string): void;
decrypt(_secret: string, _decryptString: (value: string, secret: string) => string): void;
encrypt(_secret: string, _encryptString: (value: string, secret: string) => string): void;
}

// @public @deprecated
export class ConnectedService implements IConnectedService {
constructor(source?: IConnectedService, type?: ServiceTypes);
decrypt(secret: string, decryptString: (value: string, secret: string) => string): void;
encrypt(secret: string, encryptString: (value: string, secret: string) => string): void;
decrypt(_secret: string, _decryptString: (value: string, secret: string) => string): void;
encrypt(_secret: string, _encryptString: (value: string, secret: string) => string): void;
id: string;
name: string;
toJSON(): IConnectedService;
Expand Down Expand Up @@ -131,8 +131,8 @@ export class EndpointService extends ConnectedService implements IEndpointServic
// @public @deprecated
export class FileService extends ConnectedService implements IFileService {
constructor(source?: IFileService);
decrypt(secret: string, decryptString: (value: string, secret: string) => string): void;
encrypt(secret: string, encryptString: (value: string, secret: string) => string): void;
decrypt(_secret: string, _decryptString: (value: string, secret: string) => string): void;
encrypt(_secret: string, _encryptString: (value: string, secret: string) => string): void;
path: string;
}

Expand Down
37 changes: 31 additions & 6 deletions libraries/botframework-config/src/botConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ export class BotConfiguration extends BotConfigurationBase {
private internal: InternalBotConfig = {};

/**
* Returns a new BotConfiguration instance given a JSON based configuration.
* Load the bot configuration from a JSON.
*
* @param source JSON based configuration.
* @returns A new BotConfiguration instance.
*/
static fromJSON(source: Partial<IBotConfiguration> = {}): BotConfiguration {
// tslint:disable-next-line:prefer-const
Expand All @@ -53,11 +55,14 @@ export class BotConfiguration extends BotConfigurationBase {
/**
* Load the bot configuration by looking in a folder and loading the first .bot file in the
* folder.
*
* @param folder (Optional) folder to look for bot files. If not specified the current working directory is used.
* @param secret (Optional) secret used to decrypt the bot file.
* @returns A Promise with the new BotConfiguration instance.
*/
static async loadBotFromFolder(folder?: string, secret?: string): Promise<BotConfiguration> {
folder = folder || process.cwd();
// eslint-disable-next-line security/detect-non-literal-fs-filename
let files: string[] = await fsx.readdir(folder);
files = files.sort();
for (const file of files) {
Expand All @@ -73,11 +78,14 @@ export class BotConfiguration extends BotConfigurationBase {
/**
* Load the bot configuration by looking in a folder and loading the first .bot file in the
* folder. (blocking)
*
* @param folder (Optional) folder to look for bot files. If not specified the current working directory is used.
* @param secret (Optional) secret used to decrypt the bot file.
* @returns A new BotConfiguration instance.
*/
static loadBotFromFolderSync(folder?: string, secret?: string): BotConfiguration {
folder = folder || process.cwd();
// eslint-disable-next-line security/detect-non-literal-fs-filename
let files: string[] = fsx.readdirSync(folder);
files = files.sort();
for (const file of files) {
Expand All @@ -92,8 +100,10 @@ export class BotConfiguration extends BotConfigurationBase {

/**
* Load the configuration from a .bot file.
*
* @param botpath Path to bot file.
* @param secret (Optional) secret used to decrypt the bot file.
* @returns A Promise with the new BotConfiguration instance.
*/
static async load(botpath: string, secret?: string): Promise<BotConfiguration> {
const json: string = await txtfile.read(botpath);
Expand All @@ -105,8 +115,10 @@ export class BotConfiguration extends BotConfigurationBase {

/**
* Load the configuration from a .bot file. (blocking)
*
* @param botpath Path to bot file.
* @param secret (Optional) secret used to decrypt the bot file.
* @returns A new BotConfiguration instance.
*/
static loadSync(botpath: string, secret?: string): BotConfiguration {
const json: string = txtfile.readSync(botpath);
Expand All @@ -118,6 +130,8 @@ export class BotConfiguration extends BotConfigurationBase {

/**
* Generate a new key suitable for encrypting.
*
* @returns Key to use with the [encrypt](xref:botframework-config.BotConfiguration.encrypt) method.
*/
static generateKey(): string {
return encrypt.generateKey();
Expand All @@ -139,12 +153,13 @@ export class BotConfiguration extends BotConfigurationBase {

/**
* Save the configuration to a .bot file.
*
* @param botpath Path to bot file.
* @param secret (Optional) secret used to encrypt the bot file.
*/
async saveAs(botpath: string, secret?: string): Promise<void> {
if (!botpath) {
throw new Error(`missing path`);
throw new Error('missing path');
}

this.internal.location = botpath;
Expand All @@ -165,12 +180,13 @@ export class BotConfiguration extends BotConfigurationBase {

/**
* Save the configuration to a .bot file. (blocking)
*
* @param botpath Path to bot file.
* @param secret (Optional) secret used to encrypt the bot file.
*/
saveAsSync(botpath: string, secret?: string): void {
if (!botpath) {
throw new Error(`missing path`);
throw new Error('missing path');
}
this.internal.location = botpath;

Expand All @@ -191,14 +207,18 @@ export class BotConfiguration extends BotConfigurationBase {

/**
* Save the file with secret.
*
* @param secret (Optional) secret used to encrypt the bot file.
* @returns A promise representing the asynchronous operation.
*/
async save(secret?: string): Promise<void> {
return this.saveAs(this.internal.location, secret);
}

// eslint-disable-next-line jsdoc/require-returns
/**
* Save the file with secret. (blocking)
*
* @param secret (Optional) secret used to encrypt the bot file.
*/
saveSync(secret?: string): void {
Expand All @@ -214,6 +234,7 @@ export class BotConfiguration extends BotConfigurationBase {

/**
* Encrypt all values in the in memory config.
*
* @param secret Secret to encrypt.
*/
encrypt(secret: string): void {
Expand All @@ -226,6 +247,7 @@ export class BotConfiguration extends BotConfigurationBase {

/**
* Decrypt all values in the in memory config.
*
* @param secret Secret to decrypt.
*/
decrypt(secret?: string): void {
Expand Down Expand Up @@ -280,21 +302,24 @@ export class BotConfiguration extends BotConfigurationBase {
}
}
}
} catch (err2) {
} catch {
throw err;
}
}
}

/**
* Return the path that this config was loaded from. .save() will save to this path.
* Gets the path that this config was loaded from. .save() will save to this path.
*
* @returns The path.
*/
getPath(): string {
return this.internal.location;
}

/**
* Make sure secret is correct by decrypting the secretKey with it.
*
* @param secret Secret to use.
*/
validateSecret(secret: string): void {
Expand All @@ -312,7 +337,7 @@ export class BotConfiguration extends BotConfigurationBase {
// validate we can decrypt the padlock, this tells us we have the correct secret for the rest of the file.
encrypt.decryptString(this.padlock, secret);
}
} catch (ex) {
} catch {
throw new Error(
'You are attempting to perform an operation which needs access to the secret and --secret is incorrect.'
);
Expand Down
22 changes: 18 additions & 4 deletions libraries/botframework-config/src/botConfigurationBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ export class BotConfigurationBase implements Partial<IBotConfiguration> {
}

/**
* Returns a ConnectedService instance given a JSON based service configuration.
* Loads a ConnectedService instance given a JSON based service configuration.
*
* @param service JSON based service configuration.
* @returns A new ConnectedService instance.
*/
static serviceFromJSON(service: IConnectedService): ConnectedService {
switch (service.type) {
Expand Down Expand Up @@ -92,8 +94,10 @@ export class BotConfigurationBase implements Partial<IBotConfiguration> {
}

/**
* Returns a new BotConfigurationBase instance given a JSON based configuration.
* Loads a new BotConfigurationBase instance given a JSON based configuration.
*
* @param source JSON based configuration.
* @returns A new BotConfigurationBase instance.
*/
static fromJSON(source: Partial<IBotConfiguration> = {}): BotConfigurationBase {
// tslint:disable-next-line:prefer-const
Expand All @@ -109,7 +113,9 @@ export class BotConfigurationBase implements Partial<IBotConfiguration> {
}

/**
* Returns a JSON based version of the current bot.
* Creates a JSON based version of the current bot.
*
* @returns An IBotConfiguration JSON.
*/
toJSON(): IBotConfiguration {
const newConfig: IBotConfiguration = <IBotConfiguration>{};
Expand All @@ -124,6 +130,7 @@ export class BotConfigurationBase implements Partial<IBotConfiguration> {

/**
* Connect a service to the bot file.
*
* @param newService Service to add.
* @returns Assigned ID for the service.
*/
Expand All @@ -150,7 +157,9 @@ export class BotConfigurationBase implements Partial<IBotConfiguration> {

/**
* Find service by id.
*
* @param id ID of the service to find.
* @returns The IConnectedService based on the provided id.
*/
findService(id: string): IConnectedService {
for (const service of this.services) {
Expand All @@ -164,7 +173,9 @@ export class BotConfigurationBase implements Partial<IBotConfiguration> {

/**
* Find service by name or id.
*
* @param nameOrId Name or ID of the service to find.
* @returns The IConnectedService based on the provided name or id.
*/
findServiceByNameOrId(nameOrId: string): IConnectedService {
for (const service of this.services) {
Expand All @@ -184,7 +195,9 @@ export class BotConfigurationBase implements Partial<IBotConfiguration> {

/**
* Remove service by name or id.
*
* @param nameOrId Name or ID of the service to remove.
* @returns The removed IConnectedService based on the provided name or id.
*/
disconnectServiceByNameOrId(nameOrId: string): IConnectedService {
const { services = [] } = this;
Expand All @@ -200,7 +213,8 @@ export class BotConfigurationBase implements Partial<IBotConfiguration> {

/**
* Remove service by id.
* @param nameOrId ID of the service to remove.
*
* @param id ID of the service to remove.
*/
disconnectService(id: string): void {
const { services = [] } = this;
Expand Down
2 changes: 2 additions & 0 deletions libraries/botframework-config/src/botRecipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class BotRecipe {

/**
* Creates a new [BotRecipe](xref:botframework-config.BotRecipe) instance from a JSON object.
*
* @param source JSON object of [BotRecipe](xref:botframework-config.BotRecipe) type.
* @returns A new [BotRecipe](xref:botframework-config.BotRecipe) instance.
*/
Expand All @@ -110,6 +111,7 @@ export class BotRecipe {

/**
* Creates a JSON object from `this` class instance.
*
* @returns A JSON object of [BotRecipe](xref:botframework-config.BotRecipe) type;
*/
toJSON(): Partial<BotRecipe> {
Expand Down
10 changes: 6 additions & 4 deletions libraries/botframework-config/src/models/appInsightsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AzureService } from './azureService';

/**
* Defines an App Insights service connection.
*
* @deprecated See https://aka.ms/bot-file-basics for more information.
*/
export class AppInsightsService extends AzureService implements IAppInsightsService {
Expand All @@ -29,6 +30,7 @@ export class AppInsightsService extends AzureService implements IAppInsightsServ

/**
* Creates a new AppInsightService instance.
*
* @param source (Optional) JSON based service definition.
*/
constructor(source: IAppInsightsService = {} as IAppInsightsService) {
Expand All @@ -38,34 +40,34 @@ export class AppInsightsService extends AzureService implements IAppInsightsServ

/**
* Encrypt properties on this service.
*
* @param secret Secret to use to encrypt.
* @param encryptString Function called to encrypt an individual value.
*/
encrypt(secret: string, encryptString: (value: string, secret: string) => string): void {
const that: AppInsightsService = this;
if (this.instrumentationKey && this.instrumentationKey.length > 0) {
this.instrumentationKey = encryptString(this.instrumentationKey, secret);
}
if (this.apiKeys) {
Object.keys(this.apiKeys).forEach((prop: string) => {
that.apiKeys[prop] = encryptString(that.apiKeys[prop], secret);
this.apiKeys[prop] = encryptString(this.apiKeys[prop], secret);
});
}
}

/**
* Decrypt properties on this service.
*
* @param secret Secret to use to decrypt.
* @param decryptString Function called to decrypt an individual value.
*/
decrypt(secret: string, decryptString: (value: string, secret: string) => string): void {
const that: AppInsightsService = this;
if (this.instrumentationKey && this.instrumentationKey.length > 0) {
this.instrumentationKey = decryptString(this.instrumentationKey, secret);
}
if (this.apiKeys) {
Object.keys(this.apiKeys).forEach((prop: string) => {
that.apiKeys[prop] = decryptString(that.apiKeys[prop], secret);
this.apiKeys[prop] = decryptString(this.apiKeys[prop], secret);
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions libraries/botframework-config/src/models/azureService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ConnectedService } from './connectedService';

/**
* Base class for all azure service definitions.
*
* @deprecated See https://aka.ms/bot-file-basics for more information.
*/
export class AzureService extends ConnectedService implements IAzureService {
Expand All @@ -34,6 +35,7 @@ export class AzureService extends ConnectedService implements IAzureService {

/**
* Creates a new AzureService instance.
*
* @param source (Optional) JSON based service definition.
* @param type Type of service being defined.
*/
Expand Down
Loading