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

(stepfunctions): SqsSendMessage is not adding policy sqs:sendMessage since v2.127.0 #29203

Closed
orekav opened this issue Feb 21, 2024 · 9 comments
Labels
@aws-cdk/aws-stepfunctions Related to AWS StepFunctions bug This issue is a bug. p2

Comments

@orekav
Copy link
Contributor

orekav commented Feb 21, 2024

Describe the bug

Permission missing for step function to perform SQS's send message action when using SqsSendMessage construct together with @aws-solutions-constructs/aws-s3-stepfunctions.
It happens to any @aws-solutions-constructs that uses buildStateMachine

Expected Behavior

CloudFormation output for the State Machine should contain

"S3ToStepfunctionsS3ToStepfunctionseventrulestepfunctionconstructStateMachineRoleDefaultPolicy56D2BF03": {
   "Type": "AWS::IAM::Policy",
   "Properties": {
    "PolicyDocument": {
     "Statement": [
      {
       "Action": [
        "logs:CreateLogDelivery",
        "logs:GetLogDelivery",
        "logs:UpdateLogDelivery",
        "logs:DeleteLogDelivery",
        "logs:ListLogDeliveries"
       ],
       "Effect": "Allow",
       "Resource": "*"
      },
      {
       "Action": "sqs:SendMessage",
       "Effect": "Allow",
       "Resource": {
        "Fn::GetAtt": [
         "MySqsQueue317E6770",
         "Arn"
        ]
       }
      },
      {
       "Action": [
        "logs:DescribeLogGroups",
        "logs:DescribeResourcePolicies",
        "logs:PutResourcePolicy"
       ],
       "Effect": "Allow",
       "Resource": {
        "Fn::Join": [
         "",
         [
          "arn:",
          {
           "Ref": "AWS::Partition"
          },
          ":logs:",
          {
           "Ref": "AWS::Region"
          },
          ":",
          {
           "Ref": "AWS::AccountId"
          },
          ":*"
         ]
        ]
       }
      }
     ],
     "Version": "2012-10-17"
    },
    "PolicyName": "S3ToStepfunctionsS3ToStepfunctionseventrulestepfunctionconstructStateMachineRoleDefaultPolicy56D2BF03",
    "Roles": [
     {
      "Ref": "S3ToStepfunctionsS3ToStepfunctionseventrulestepfunctionconstructStateMachineRoleB52EB61B"
     }
    ]
   },
   "Metadata": {
    "aws:cdk:path": "MainStack/S3ToStepfunctions/S3ToStepfunctions-event-rule-step-function-construct/StateMachine/Role/DefaultPolicy/Resource",
    "cfn_nag": {
     "rules_to_suppress": [
      {
       "id": "W12",
       "reason": "The 'LogDelivery' actions do not support resource-level authorizations"
      }
     ]
    }
   }
  },

Current Behavior

CloudFormation output for the State Machine doesn't contain

      {
       "Action": "sqs:SendMessage",
       "Effect": "Allow",
       "Resource": {
        "Fn::GetAtt": [
         "MySqsQueue317E6770",
         "Arn"
        ]
       }
      },
"S3ToStepfunctionsS3ToStepfunctionseventrulestepfunctionconstructStateMachineRoleDefaultPolicy56D2BF03": {
      "Type": "AWS::IAM::Policy",
      "Properties": {
       "PolicyDocument": {
        "Statement": [
         {
          "Action": [
           "logs:CreateLogDelivery",
           "logs:GetLogDelivery",
           "logs:UpdateLogDelivery",
           "logs:DeleteLogDelivery",
           "logs:ListLogDeliveries"
          ],
          "Effect": "Allow",
          "Resource": {
           "Fn::GetAtt": [
            "MySqsQueue317E6770",
            "Arn"
           ]
          }
         },
         {
          "Action": [
           "logs:CreateLogDelivery",
           "logs:DeleteLogDelivery",
           "logs:DescribeLogGroups",
           "logs:DescribeResourcePolicies",
           "logs:GetLogDelivery",
           "logs:ListLogDeliveries",
           "logs:PutResourcePolicy",
           "logs:UpdateLogDelivery"
          ],
          "Effect": "Allow",
          "Resource": "*"
         },
         {
          "Action": [
           "logs:DescribeLogGroups",
           "logs:DescribeResourcePolicies",
           "logs:PutResourcePolicy"
          ],
          "Effect": "Allow",
          "Resource": {
           "Fn::Join": [
            "",
            [
             "arn:",
             {
              "Ref": "AWS::Partition"
             },
             ":logs:",
             {
              "Ref": "AWS::Region"
             },
             ":",
             {
              "Ref": "AWS::AccountId"
             },
             ":*"
            ]
           ]
          }
         }
        ],
        "Version": "2012-10-17"
       },
       "PolicyName": "S3ToStepfunctionsS3ToStepfunctionseventrulestepfunctionconstructStateMachineRoleDefaultPolicy56D2BF03",
       "Roles": [
        {
         "Ref": "S3ToStepfunctionsS3ToStepfunctionseventrulestepfunctionconstructStateMachineRoleB52EB61B"
        }
       ]
      },
      "Metadata": {
       "aws:cdk:path": "MainStack/S3ToStepfunctions/S3ToStepfunctions-event-rule-step-function-construct/StateMachine/Role/DefaultPolicy/Resource",
       "cfn_nag": {
        "rules_to_suppress": [
         {
          "id": "W12",
          "reason": "The 'LogDelivery' actions do not support resource-level authorizations"
         }
        ]
       }
      }
     },

Difference

Screenshot 2024-02-21 at 18 46 44

Reproduction Steps

Create a step function using SqsSendMessage from

import type { Construct } from 'constructs';
import * as cdk from 'aws-cdk-lib';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as sfnTasks from 'aws-cdk-lib/aws-stepfunctions-tasks';
import { S3ToStepfunctions } from '@aws-solutions-constructs/aws-s3-stepfunctions';
import { buildStateMachine } from "@aws-solutions-constructs/core";

export class MainStack extends cdk.Stack {
  constructor(scope: Construct, id: string, private props: cdk.StackProps) {
    super(scope, id, props);

    const mySqsQueue = new sqs.Queue(this, 'MySqsQueue', {});

    const chainable = new sfnTasks.SqsSendMessage(this, 'SQS Send', {
          comment: 'Send message to SQS',
          queue: mySqsQueue,
          messageBody: sfn.TaskInput.fromText('Hello, BUG!'),
          resultPath: sfn.JsonPath.DISCARD,
        });

    const definitionBody = sfn.DefinitionBody.fromChainable(chainable)

    // new sfn.StateMachine(this, 'MyStateMachine', {
    //   definitionBody,
    // });

    buildStateMachine(this, { definitionBody })

    // const { stateMachine } = new S3ToStepfunctions(this, S3ToStepfunctions.name, {
    //     deployCloudTrail: false,
    //     createCloudWatchAlarms: false,
    //     stateMachineProps: {
    //       stateMachineType: sfn.StateMachineType.EXPRESS,
    //       definitionBody,
    //     },
    //   });
  }
}

package.json

{
  "name": "app",
  "version": "0.1.0",
  "bin": {
    "app": "bin/app.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "cdk": "cdk"
  },
  "devDependencies": {
    "@aws-appsync/eslint-plugin": "^1.6.0",
    "@aws-appsync/utils": "^1.7.0",
    "@aws-solutions-constructs/aws-s3-stepfunctions": "2.52.1",
    "@types/jest": "^29.5.12",
    "@types/node": "20.11.16",
    "esbuild": "0.20.0",
    "cdk-appsync-typescript-resolver": "^0.0.24",
    "jest": "^29.7.0",
    "ts-jest": "^29.1.2",
    "aws-cdk": "2.127.0",
    "aws-cdk-lib": "2.127.0",
    "ts-node": "10.9.2",
    "typescript": "5.3.3"
  },
  "dependencies": {
    "constructs": "10.3.0",
    "source-map-support": "^0.5.21"
  },
  "engines": {
    "node": "20"
  }
}

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.127.0

Framework Version

No response

Node.js Version

20.11.1

OS

MacOS

Language

TypeScript

Language Version

No response

Other information

No response

@orekav orekav added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 21, 2024
@github-actions github-actions bot added the @aws-cdk/aws-stepfunctions Related to AWS StepFunctions label Feb 21, 2024
@orekav
Copy link
Contributor Author

orekav commented Feb 21, 2024

Furthermore, if I add this

const { stateMachine }  = buildStateMachine(this, { definitionBody });
console.log(JSON.stringify((stateMachine.node.children[0] as any).defaultPolicy.document.statements, null, 2));

I get

[
  {
    "Action": "sqs:SendMessage",
    "Effect": "Allow",
    "Resource": "${Token[TOKEN.632]}"
  },
  {
    "Action": [
      "logs:CreateLogDelivery",
      "logs:GetLogDelivery",
      "logs:UpdateLogDelivery",
      "logs:DeleteLogDelivery",
      "logs:ListLogDeliveries",
      "logs:PutResourcePolicy",
      "logs:DescribeResourcePolicies",
      "logs:DescribeLogGroups"
    ],
    "Effect": "Allow",
    "Resource": "*"
  },
  {
    "Action": [
      "logs:PutResourcePolicy",
      "logs:DescribeResourcePolicies",
      "logs:DescribeLogGroups"
    ],
    "Effect": "Allow",
    "Resource": "arn:${Token[AWS.Partition.4]}:logs:${Token[AWS.Region.5]}:${Token[AWS.AccountId.1]}:*"
  }
]

Which mean that at the moment of creation it exists, but it doesn't end up in the CloudFormation Template

@pahud
Copy link
Contributor

pahud commented Feb 21, 2024

Sounds like this issue should go to aws-solutions-constructs ?

@pahud pahud added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p2 and removed needs-triage This issue or PR still needs to be triaged. labels Feb 21, 2024
@orekav
Copy link
Contributor Author

orekav commented Feb 22, 2024

Sounds like this issue should go to aws-solutions-constructs ?

I was thinking the same but, why does it happen with v2.127.0 or higher and not with v2.126.0 and lower?

@orekav
Copy link
Contributor Author

orekav commented Feb 22, 2024

This is the piece of code that generates the conflict but I still don't see how is this affected by CDK >= v.2.127.0
Screenshot 2024-02-22 at 01 01 29

As soon as I remove that highlighted code, the error goes away.

@orekav
Copy link
Contributor Author

orekav commented Feb 22, 2024

I have checked a bit further the v2.127.0 release
The changes on 97e3827 to packages/aws-cdk-lib/aws-stepfunctions/lib/state-machine.ts are the ones provoking this issue.

As soon as I rollback this file and use as it was in v2.126.0, it starts working again.

@orekav
Copy link
Contributor Author

orekav commented Feb 22, 2024

With the changes in v2.127.0 when we bind ChainDefinitionBody, now we are no longer using this.role, we use this (StateMachine).

Fragment A
Screenshot 2024-02-22 at 01 55 14

Fragment B
Screenshot 2024-02-22 at 01 56 39

Sounds like this issue should go to aws-solutions-constructs ?

@orekav
Copy link
Contributor Author

orekav commented Feb 22, 2024

By moving back the statement for (const statement of graph.policyStatements) { , this issue stops happening

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Feb 22, 2024
@orekav orekav closed this as completed Feb 22, 2024
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@biffgaut
Copy link
Contributor

Yes, this is a Solutions Constructs issue and should be remedied when we address awslabs/aws-solutions-constructs#1077. While we hesitate to give an exact ETA, it is imminent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-stepfunctions Related to AWS StepFunctions bug This issue is a bug. p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants