Skip to content

Commit

Permalink
bug: Fix incorrect input validation (OpenSearch following Pydantic up…
Browse files Browse the repository at this point in the history
…date) + Added missing CMK use (Alarm topic) (#600)
  • Loading branch information
charles-marion authored Oct 31, 2024
1 parent bd520f6 commit 6c64064
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/aws-genai-llm-chatbot-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ export class AwsGenAILLMChatbotStack extends cdk.Stack {

const monitoringStack = new cdk.NestedStack(this, "MonitoringStack");
const monitoringConstruct = new Monitoring(monitoringStack, "Monitoring", {
shared,
prefix: props.config.prefix,
advancedMonitoring: props.config.advancedMonitoring === true,
appsycnApi: chatBotApi.graphqlApi,
Expand Down
10 changes: 7 additions & 3 deletions lib/chatbot-api/functions/api-handler/routes/workspaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ class CreateWorkspaceOpenSearchRequest(BaseModel):
kind: str = SAFE_SHORT_STR_VALIDATION
name: str = Field(min_length=1, max_length=100, pattern=name_regex)
embeddingsModelProvider: str = SAFE_SHORT_STR_VALIDATION
embeddingsModelName: str = SAFE_SHORT_STR_VALIDATION
crossEncoderModelProvider: Optional[str] = SAFE_SHORT_STR_VALIDATION
crossEncoderModelName: Optional[str] = SAFE_SHORT_STR_VALIDATION
embeddingsModelName: str = Field(
min_length=0, max_length=500, pattern=r"^[A-Za-z0-9-_. /]*$", default=None
)
crossEncoderModelProvider: Optional[str] = SAFE_SHORT_STR_VALIDATION_OPTIONAL
crossEncoderModelName: Optional[str] = Field(
min_length=0, max_length=500, pattern=r"^[A-Za-z0-9-_. /]*$", default=None
)
languages: List[Annotated[str, SAFE_SHORT_STR_VALIDATION]]
hybridSearch: bool
chunkingStrategy: str = SAFE_SHORT_STR_VALIDATION
Expand Down
16 changes: 16 additions & 0 deletions lib/monitoring/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import { FilterPattern, ILogGroup, MetricFilter } from "aws-cdk-lib/aws-logs";
import { IDistribution } from "aws-cdk-lib/aws-cloudfront";
import { ITopic, Topic } from "aws-cdk-lib/aws-sns";
import { NagSuppressions } from "cdk-nag";
import { Shared } from "../shared";
import { PolicyStatement, ServicePrincipal } from "aws-cdk-lib/aws-iam";

export interface MonitoringProps {
prefix: string;
readonly shared: Shared;
advancedMonitoring: boolean;
appsycnApi: IGraphqlApi;
appsyncResolversLogGroups: ILogGroup[];
Expand Down Expand Up @@ -261,8 +264,21 @@ export class Monitoring extends Construct {
const onAlarmTopic = new Topic(this, "CompositeAlarmTopic", {
displayName: props.prefix + "CompositeAlarmTopic",
enforceSSL: true,
masterKey: props.shared.kmsKey,
});

if (props.shared.kmsKey) {
// Following the guide
// https://docs.aws.amazon.com/sns/latest/dg/sns-key-management.html#sns-what-permissions-for-sse
props.shared.kmsKey.addToResourcePolicy(
new PolicyStatement({
actions: ["kms:GenerateDataKey*", "kms:Decrypt"],
principals: [new ServicePrincipal("cloudwatch.amazonaws.com")],
resources: ["*"],
})
);
}

/**
* CDK NAG suppression
*/
Expand Down
52 changes: 52 additions & 0 deletions tests/monitoring/__snapshots__/monitoring-contruct.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,52 @@ exports[`snapshot test 1`] = `
"Type": "AWS::SQS::Queue",
"UpdateReplacePolicy": "Delete",
},
"ExampleA925490C": {
"DeletionPolicy": "Retain",
"Properties": {
"KeyPolicy": {
"Statement": [
{
"Action": "kms:*",
"Effect": "Allow",
"Principal": {
"AWS": {
"Fn::Join": [
"",
[
"arn:",
{
"Ref": "AWS::Partition",
},
":iam::",
{
"Ref": "AWS::AccountId",
},
":root",
],
],
},
},
"Resource": "*",
},
{
"Action": [
"kms:GenerateDataKey*",
"kms:Decrypt",
],
"Effect": "Allow",
"Principal": {
"Service": "cloudwatch.amazonaws.com",
},
"Resource": "*",
},
],
"Version": "2012-10-17",
},
},
"Type": "AWS::KMS::Key",
"UpdateReplacePolicy": "Retain",
},
"Index": {
"Properties": {
"Edition": "edition",
Expand All @@ -121,6 +167,12 @@ exports[`snapshot test 1`] = `
},
"Properties": {
"DisplayName": "CompositeAlarmTopic",
"KmsMasterKeyId": {
"Fn::GetAtt": [
"ExampleA925490C",
"Arn",
],
},
},
"Type": "AWS::SNS::Topic",
},
Expand Down
3 changes: 3 additions & 0 deletions tests/monitoring/monitoring-contruct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
import { Function } from "aws-cdk-lib/aws-lambda";
import { StateMachine } from "aws-cdk-lib/aws-stepfunctions";
import { LogGroup } from "aws-cdk-lib/aws-logs";
import { Shared } from "../../lib/shared";
import { Key } from "aws-cdk-lib/aws-kms";

jest.spyOn(console, "log").mockImplementation(() => {});

Expand All @@ -36,6 +38,7 @@ new Queue(stack, "Queue", {

new Monitoring(stack, "Monitoring", {
prefix: "",
shared: { kmsKey: new Key(stack, "Example") } as Shared,
advancedMonitoring: true,
appsycnApi: GraphqlApi.fromGraphqlApiAttributes(stack, "GraphQL", {
graphqlApiId: "graphqlApiId",
Expand Down

0 comments on commit 6c64064

Please sign in to comment.