Skip to content

Commit

Permalink
feat: Introduced restrictive logging of sensitive data and configurat…
Browse files Browse the repository at this point in the history
…ion of logging output

feat: Introduced new prop on ControlHandler and ControlManager : `services` to customize and override default service implementations  on the framework

      `services` can be extended to include and customize logging, i18 resources etc
  • Loading branch information
shreraju-amzn authored and Shreyas-vgr committed Jun 3, 2021
1 parent b194c39 commit 56f78cf
Show file tree
Hide file tree
Showing 39 changed files with 516 additions and 151 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ node_modules
.DS_Store
.nycrc
.nyc_output
coverage/
coverage/
6 changes: 4 additions & 2 deletions demo/ComponentModeAPL/src/buildInteractionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
*/

import { ComponentModeDemo } from '.';
import { ControlServices } from '../../../src/controls/ControlServices';
import { ControlInteractionModelGenerator } from '../../../src/interactionModelGeneration/ControlInteractionModelGenerator';
import { Logger } from '../../../src/logging/Logger';

const log = new Logger('ComponentModeDemo:InteractionModel');
const MODULE_NAME = 'ComponentModeDemo:InteractionModel';
const services = ControlServices.getDefaults();
const log = services.logger.getLogger(MODULE_NAME);

export namespace ComponentModeDemoIM {
export const imGen = new ControlInteractionModelGenerator()
Expand Down
13 changes: 5 additions & 8 deletions demo/ListControl/YesNoMaybe/src/buildInteractionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@
* permissions and limitations under the License.
*/

import { v1 } from 'ask-smapi-model';
import { ListDemo1 } from '.';
import { ControlServices } from '../../../../src/controls/ControlServices';
import { ControlInteractionModelGenerator } from '../../../../src/interactionModelGeneration/ControlInteractionModelGenerator';
import { Logger } from '../../../../src/logging/Logger';
import { filteredYesNoMaybeSlotType, yesNoMaybeSlotType } from './interactionModelTypes';

import SlotType = v1.skill.interactionModel.SlotType;
import TypeValue = v1.skill.interactionModel.TypeValue;
import Intent = v1.skill.interactionModel.Intent;

const log = new Logger('HelloWorld:InteractionModel');
const MODULE_NAME = 'HelloWorld:InteractionModel';
const services = ControlServices.getDefaults();
const log = services.logger.getLogger(MODULE_NAME);

export namespace ListDemo1IM {
export const imGen = new ControlInteractionModelGenerator()
Expand Down Expand Up @@ -49,5 +46,5 @@ export namespace ListDemo1IM {
if (require.main === module) {
// Build and write
ListDemo1IM.imGen.buildAndWrite('en-US-generated.json');
console.log('Wrote ./en-US-generated.json');
log.info('Wrote ./en-US-generated.json');
}
11 changes: 7 additions & 4 deletions demo/MultiValueListControl/src/buildInteractionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
*/

import { MultiValueListDemo } from '.';
import { Logger, ControlInteractionModelGenerator } from '../../../src';
import { ControlInteractionModelGenerator } from '../../../src';
import { ControlServices } from '../../../src/controls/ControlServices';
import {
yesNoMaybeSlotType,
filteredYesNoMaybeSlotType,
yesNoMaybeSlotType,
} from '../../ListControl/YesNoMaybe/src/interactionModelTypes';

const log = new Logger('MultiValueListDemo:InteractionModel');
const MODULE_NAME = 'MultiValueListDemo:InteractionModel';
const services = ControlServices.getDefaults();
const log = services.logger.getLogger(MODULE_NAME);

export namespace MultiValueListDemoIM {
export const imGen = new ControlInteractionModelGenerator()
Expand Down Expand Up @@ -80,5 +83,5 @@ export namespace MultiValueListDemoIM {
if (require.main === module) {
// Build and write
MultiValueListDemoIM.imGen.buildAndWrite('en-US-generated.json');
console.log('Wrote ./en-US-generated.json');
log.info('Wrote ./en-US-generated.json');
}
8 changes: 5 additions & 3 deletions demo/NumberControl/NumberDemo/src/buildInteractionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
*/

import { BasicNumberDemo } from '.';
import { ControlServices } from '../../../../src/controls/ControlServices';
import { ControlInteractionModelGenerator } from '../../../../src/interactionModelGeneration/ControlInteractionModelGenerator';
import { Logger } from '../../../../src/logging/Logger';

const log = new Logger('NumberControlDemo:InteractionModel');
const MODULE_NAME = 'NumberControlDemo:InteractionModel';
const services = ControlServices.getDefaults();
const log = services.logger.getLogger(MODULE_NAME);

export namespace BasicNumberDemoIM {
export const imGen = new ControlInteractionModelGenerator()
Expand All @@ -35,5 +37,5 @@ export namespace BasicNumberDemoIM {
if (require.main === module) {
// Build and write
BasicNumberDemoIM.imGen.buildAndWrite('en-US-generated.json');
console.log('Wrote ./en-US-generated.json');
log.info('Wrote ./en-US-generated.json');
}
31 changes: 31 additions & 0 deletions demo/NumberControl/NumberDemo/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
import { SkillBuilders } from 'ask-sdk-core';
import Debug from 'debug';
import { DefaultLogger } from '../../../../src';
import { NumberControl } from '../../../../src/commonControls/numberControl/NumberControl';
import { Control } from '../../../../src/controls/Control';
import { ControlManager } from '../../../../src/controls/ControlManager';
import { ControlServices } from '../../../../src/controls/ControlServices';
import { ILogger } from '../../../../src/controls/interfaces/ILogger';
import { ILoggerFactory } from '../../../../src/controls/interfaces/ILoggerFactory';
import { ControlHandler } from '../../../../src/runtime/ControlHandler';
import { DemoRootControl } from '../../../Common/src/DemoRootControl';

export class CustomLoggerFactory implements ILoggerFactory {
getLogger(namespace: string): ILogger {
return new CustomLogger(namespace);
}
}

export class CustomLogger extends DefaultLogger {
info(message: string): void {
Debug(`info:Redfox:${this.moduleName}`)(message);
}

sanitize(message: any): string {
if (
process.env.ASK_SDK_RESTRICTIVE_LOGGING !== undefined &&
process.env.ASK_SDK_RESTRICTIVE_LOGGING === 'true'
) {
return '###################';
} else {
return message;
}
}
}

/**
* Simple demonstration of a number control.
*
Expand Down Expand Up @@ -33,6 +61,9 @@ export namespace BasicNumberDemo {
}
}

const services = { logger: new CustomLoggerFactory() };
ControlServices.setDefaults(services);

export const handler = SkillBuilders.custom()
.addRequestHandlers(new ControlHandler(new BasicNumberDemo.DemoControlManager()))
.lambda();
13 changes: 5 additions & 8 deletions demo/QuestionnaireControl/src/buildInteractionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@
* permissions and limitations under the License.
*/

import { v1 } from 'ask-smapi-model';
import { MultipleLists } from '.';
import { ControlServices } from '../../../src/controls/ControlServices';
import { ControlInteractionModelGenerator } from '../../../src/interactionModelGeneration/ControlInteractionModelGenerator';
import { Logger } from '../../../src/logging/Logger';

import SlotType = v1.skill.interactionModel.SlotType;
import TypeValue = v1.skill.interactionModel.TypeValue;
import Intent = v1.skill.interactionModel.Intent;

const log = new Logger('QuestionnaireControlDemo:InteractionModel');
const MODULE_NAME = 'QuestionnaireControlDemo:InteractionModel';
const services = ControlServices.getDefaults();
const log = services.logger.getLogger(MODULE_NAME);

export namespace TwoListsIM {
export const imGen = new ControlInteractionModelGenerator()
Expand Down Expand Up @@ -191,5 +188,5 @@ export namespace TwoListsIM {
if (require.main === module) {
// Build and write
TwoListsIM.imGen.buildAndWrite('en-US-generated.json');
console.log('Wrote ./en-US-generated.json');
log.info('Wrote ./en-US-generated.json');
}
13 changes: 5 additions & 8 deletions demo/TwoLists/src/buildInteractionModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,13 @@
* permissions and limitations under the License.
*/

import { v1 } from 'ask-smapi-model';
import { MultipleLists } from '.';
import { ControlServices } from '../../../src/controls/ControlServices';
import { ControlInteractionModelGenerator } from '../../../src/interactionModelGeneration/ControlInteractionModelGenerator';
import { Logger } from '../../../src/logging/Logger';

import SlotType = v1.skill.interactionModel.SlotType;
import TypeValue = v1.skill.interactionModel.TypeValue;
import Intent = v1.skill.interactionModel.Intent;

const log = new Logger('HelloWorld:InteractionModel');
const MODULE_NAME = 'HelloWorld:InteractionModel';
const services = ControlServices.getDefaults();
const log = services.logger.getLogger(MODULE_NAME);

export namespace TwoListsIM {
export const imGen = new ControlInteractionModelGenerator()
Expand Down Expand Up @@ -134,5 +131,5 @@ export namespace TwoListsIM {
if (require.main === module) {
// Build and write
TwoListsIM.imGen.buildAndWrite('en-US-generated.json');
console.log('Wrote ./en-US-generated.json');
log.info('Wrote ./en-US-generated.json');
}
3 changes: 2 additions & 1 deletion ide/vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@
"env": {
// "DEBUG": "error:*, warn:*, info:*" // uncomment for logs
// "ASK_SDK_CONTROLS_LOG_RESPONSE_FILEPATH": <file_path> // uncomment to write out response for debugging
//"ASK_SDK_RESTRICTIVE_LOGGING": "true" // uncomment to restrict sensitive information from logging such as slot values
},
// "smartStep": true,
"skipFiles": [
"node_modules/**",
"<node_internals>/**"
],
"console": "integratedTerminal",
"console": "integratedTerminal"

}
]
Expand Down
17 changes: 13 additions & 4 deletions src/commonControls/DateControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@ import {
} from '../controls/Control';
import { ControlInput } from '../controls/ControlInput';
import { ControlResultBuilder } from '../controls/ControlResult';
import { ControlServices, ControlServicesProps } from '../controls/ControlServices';
import { ILogger } from '../controls/interfaces/ILogger';
import { InteractionModelContributor } from '../controls/mixins/InteractionModelContributor';
import { evaluateValidationProp, StateValidationFunction, ValidationFailure } from '../controls/Validation';
import { AmazonBuiltInSlotType } from '../intents/AmazonBuiltInSlotType';
import { GeneralControlIntent, unpackGeneralControlIntent } from '../intents/GeneralControlIntent';
import { unpackValueControlIntent, ValueControlIntent } from '../intents/ValueControlIntent';
import { ControlInteractionModelGenerator } from '../interactionModelGeneration/ControlInteractionModelGenerator';
import { ModelData } from '../interactionModelGeneration/ModelTypes';
import { Logger } from '../logging/Logger';
import { ControlResponseBuilder } from '../responseGeneration/ControlResponseBuilder';
import {
InvalidValueAct,
Expand All @@ -48,7 +49,7 @@ import { InputUtil } from '../utils/InputUtil';
import { falseIfGuardFailed, okIf } from '../utils/Predicates';
import { getEndDateOfRange, getStartDateOfRange, getUTCDate } from './dateRangeControl/DateHelper';

const log = new Logger('AskSdkControls:DateControl');
const MODULE_NAME = 'AskSdkControls:DateControl';

/**
* Props for a DateControl.
Expand Down Expand Up @@ -124,6 +125,11 @@ export interface DateControlProps extends ControlProps {
* Default: returns the value unchanged.
*/
valueRenderer?: (value: string, input: ControlInput) => string;

/**
* Props to customize services used by the control.
*/
services?: ControlServicesProps;
}

/**
Expand Down Expand Up @@ -354,12 +360,14 @@ export class DateControl extends Control implements InteractionModelContributor
input: ControlInput,
resultBuilder: ControlResultBuilder,
) => void | Promise<void>;
private log: ILogger;

constructor(props: DateControlProps) {
super(props.id);
this.rawProps = props;
this.props = DateControl.mergeWithDefaultProps(props);
this.state.lastInitiative = {};
this.log = this.props.services.logger.getLogger(MODULE_NAME);
}

/**
Expand Down Expand Up @@ -432,6 +440,7 @@ export class DateControl extends Control implements InteractionModelContributor
validation: [],
confirmationRequired: false,
required: true,
services: props.services ?? ControlServices.getDefaults(),
};

return _.merge(defaults, props);
Expand Down Expand Up @@ -667,7 +676,7 @@ export class DateControl extends Control implements InteractionModelContributor

// tsDoc - see Control
async handle(input: ControlInput, resultBuilder: ControlResultBuilder): Promise<void> {
log.debug(`DateControl[${this.id}]: handle(). Entering`);
this.log.debug(`DateControl[${this.id}]: handle(). Entering`);

if (this.handleFunc === undefined) {
const intent: Intent = (input.request as IntentRequest).intent;
Expand All @@ -694,7 +703,7 @@ export class DateControl extends Control implements InteractionModelContributor
if (this.initiativeFunc === undefined) {
const errorMsg =
'DateControl: takeInitiative called but this.initiativeFunc is not set. canTakeInitiative() should be called first to set this.initiativeFunc.';
log.error(errorMsg);
this.log.error(errorMsg);
throw new Error(errorMsg);
}
await this.initiativeFunc(input, resultBuilder);
Expand Down
6 changes: 4 additions & 2 deletions src/commonControls/LanguageStrings.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Resource } from 'i18next';
import { Strings as $ } from '../constants/Strings';
import { ControlServices } from '../controls/ControlServices';
import { SharedSlotType } from '../interactionModelGeneration/ModelTypes';
import { Logger } from '../logging/Logger';

const log = new Logger('AskSdkControls:i18n');
const MODULE_NAME = 'AskSdkControls:i18n';
const services = ControlServices.getDefaults();
const log = services.logger.getLogger(MODULE_NAME);

//todo: move this somewhere more appropriate. consider splitting prompts/intents

Expand Down
17 changes: 13 additions & 4 deletions src/commonControls/ValueControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
} from '../controls/Control';
import { ControlInput } from '../controls/ControlInput';
import { ControlResultBuilder } from '../controls/ControlResult';
import { ControlServices, ControlServicesProps } from '../controls/ControlServices';
import { ILogger } from '../controls/interfaces/ILogger';
import { ControlStateDiagramming } from '../controls/mixins/ControlStateDiagramming';
import { InteractionModelContributor } from '../controls/mixins/InteractionModelContributor';
import { evaluateValidationProp, StateValidationFunction, ValidationFailure } from '../controls/Validation';
Expand All @@ -32,7 +34,6 @@ import { GeneralControlIntent, unpackGeneralControlIntent } from '../intents/Gen
import { unpackValueControlIntent, ValueControlIntent } from '../intents/ValueControlIntent';
import { ControlInteractionModelGenerator } from '../interactionModelGeneration/ControlInteractionModelGenerator';
import { ModelData } from '../interactionModelGeneration/ModelTypes';
import { Logger } from '../logging/Logger';
import { ControlResponseBuilder } from '../responseGeneration/ControlResponseBuilder';
import {
InvalidValueAct,
Expand All @@ -49,7 +50,7 @@ import { DeepRequired } from '../utils/DeepRequired';
import { InputUtil } from '../utils/InputUtil';
import { falseIfGuardFailed, okIf } from '../utils/Predicates';

const log = new Logger('AskSdkControls:ValueControl');
const MODULE_NAME = 'AskSdkControls:ValueControl';

/**
* Props for a ValueControl.
Expand Down Expand Up @@ -132,6 +133,11 @@ export interface ValueControlProps extends ControlProps {
* Default: returns the value unchanged.
*/
valueRenderer?: (value: string, input: ControlInput) => string;

/**
* Props to customize services used by the control.
*/
services?: ControlServicesProps;
}

/**
Expand Down Expand Up @@ -307,6 +313,7 @@ export class ValueControl extends Control implements InteractionModelContributor
input: ControlInput,
resultBuilder: ControlResultBuilder,
) => void | Promise<void>;
private log: ILogger;

constructor(props: ValueControlProps) {
super(props.id);
Expand All @@ -322,6 +329,7 @@ export class ValueControl extends Control implements InteractionModelContributor
this.rawProps = props;
this.props = ValueControl.mergeWithDefaultProps(props);
this.state.lastInitiative = {};
this.log = this.props.services.logger.getLogger(MODULE_NAME);
}

/**
Expand Down Expand Up @@ -403,6 +411,7 @@ export class ValueControl extends Control implements InteractionModelContributor
customHandlingFuncs: [],
},
valueRenderer: (value: string, input) => value,
services: props.services ?? ControlServices.getDefaults(),
};

return _.merge(defaults, props);
Expand Down Expand Up @@ -453,7 +462,7 @@ export class ValueControl extends Control implements InteractionModelContributor
// tsDoc - see Control
async handle(input: ControlInput, resultBuilder: ControlResultBuilder): Promise<void> {
if (this.handleFunc === undefined) {
log.error(
this.log.error(
'ValueControl: handle called but this.handleFunc is not set. canHandle() should be called first to set this.handleFunc.',
);
const intent: Intent = (input.request as IntentRequest).intent;
Expand Down Expand Up @@ -778,7 +787,7 @@ export class ValueControl extends Control implements InteractionModelContributor
if (this.initiativeFunc === undefined) {
const errorMsg =
'ValueControl: takeInitiative called but this.initiativeFunc is not set. canTakeInitiative() should be called first to set this.initiativeFunc.';
log.error(errorMsg);
this.log.error(errorMsg);
throw new Error(errorMsg);
}
await this.initiativeFunc(input, resultBuilder);
Expand Down
Loading

0 comments on commit 56f78cf

Please sign in to comment.