【问题标题】:Serverless Framework - Restricting permissions with API gateway无服务器框架 - 使用 API 网关限制权限
【发布时间】:2022-01-22 00:57:31
【问题描述】:

让云形成模板和无服务器按预期工作是多么乏味和复杂,我一直在苦苦挣扎。

我目前有一个 API 网关,其中特定端点调用不同的 lambda。

我是 /subscribe 向世界开放,但 /users 只能通过一个特定资源访问。我该怎么办?

functions:
  createEmailEntry:
    handler: src/Email.addUser
    events:
      - http:
          method: POST
          path: /subscribe
  retrieveAllSubscribers:
    handler: src/Email.getUsers
    events:
      - http:
          method: GET
          path: /users

这会为 API 网关自动生成一个策略(内联)以命中检索端点。仅允许 some ARN 访问/users 端点的正确语法是什么?

【问题讨论】:

    标签: aws-lambda serverless serverless-framework


    【解决方案1】:

    这是一个语法示例,仅允许从一个特定的 Arn 访问“用户”(同时允许每个人都调用 subscribe)。您需要将 resourcePolicy 添加到 provider.apiGateway 部分,在“Principal”下指定允许的 Arn,并允许调用特定的 API 路径/资源。

    您还想将 aws_iam 类型的授权方添加到相关路径中。

    provider:
      name: aws
      runtime: nodejs12.x
      lambdaHashingVersion: 20201221
      apiGateway:
        resourcePolicy:
          - Effect: Allow
            Principal:
              AWS:
                  - /* write allowed Arn here */
            Action: execute-api:Invoke
            Resource:
              - execute-api:/stagename/*/users
          - Effect: Allow
            Principal: '*'
            Action: execute-api:Invoke
            Resource:
              - execute-api:/stagename/*/subscribe
    
    functions:
      createEmailEntry:
        handler: src/Email.addUser
        events:
          - http: 
              method: POST
              path: subscribe
      retrieveAllSubscribers:
        handler: src/Email.getUsers
        events:
          - http:
              method: GET
              path: users
              authorizer: 
                type: aws_iam
    

    【讨论】:

      猜你喜欢
      • 2021-05-11
      • 2010-12-27
      • 2011-07-31
      • 2020-11-08
      • 2021-04-30
      • 2011-10-22
      • 1970-01-01
      • 2021-05-06
      • 2019-10-07
      相关资源
      最近更新 更多