繁体   English   中英

AWS Lambda function 无法访问 AppSync GraphQL API 权限被拒绝

[英]AWS Lambda function unable to access AppSync GraphQL API - permission denied

我无法通过 AWS Lambda function 调用 AppSync GraphQL 查询。 我一直在使用本文中的代码,特别是使用IAM权限的后半部分: https://docs.amplify.aws/lib/graphqlapi/graphql-from-nodejs/q/platform/js#signing-a-request -来自-lambda

 const https = require("https"); const AWS = require("aws-sdk"); const urlParse = require("url").URL; const appsyncUrl = process.env.API_MYAPP_GRAPHQLAPIENDPOINTOUTPUT; const region = process.env.REGION; const endpoint = new urlParse(appsyncUrl).hostname.toString(); const graphqlQuery = require("./query.js").query; exports.handler = async (event) => { const req = new AWS.HttpRequest(appsyncUrl, region); req.method = "POST"; req.path = "/graphql"; req.headers.host = endpoint; req.headers["Content-Type"] = "application/json"; req.body = JSON.stringify({ query: graphqlQuery, operationName: "list", }); const signer = new AWS.Signers.V4(req, "appsync", true); signer.addAuthorization(AWS.config.credentials, AWS.util.date.getDate()); const data = await new Promise((resolve, reject) => { const httpRequest = https.request({...req, host: endpoint }, (result) => { result.on("data", (data) => { resolve(JSON.parse(data.toString())); }); }); httpRequest.write(req.body); httpRequest.end(); }); return { statusCode: 200, body: data, }; };

我正在使用 Amplify CLI。 我使用 CLI 创建了 function 并确保它可以访问 GraphQL API。

我在 Lambda 中得到的具体错误是:

      {
  "statusCode": 200,
  "body": {
    "errors": [
      {
        "errorType": "UnauthorizedException",
        "message": "Permission denied"
      }
    ]
  }
}

GraphQL 设置为使用 Cognito 用户池作为身份验证,我通过 Amplify CLI 添加了 IAM 作为辅助身份验证机制。 AWS GraphQL 控制台显示我将 Cognito 作为主要身份验证机制,将 IAM 作为辅助。

The Lambda function appears to be permissioned okay as it shows 4 resources (create, update, delete, read) corresponding to the API and Allow: appsync:GraphQL as the Action.

如果我使用amplify mock function myfunction ,那么它可以正常执行,并且 GraphQL 查询的结果正确返回。

当我选择 IAM 作为身份验证机制时,我还可以通过 AppSync UI 成功运行相同的查询。

我正在访问的表在我的 schema.graphql 中定义为:

 type Business
  @model
  @auth(
    rules: [
      { allow: owner }
      { allow: groups, groups: ["Admin"] }
      { allow: private, provider: iam }
    ]
  ) {
  id: ID!
  owner: String!
  name: String!
  emailSuffix: String!
  shortCode: String!
}

我已经从 model 中删除了 Auth,这并没有什么不同。

我已经删除了 function 并重新创建它,以防权限以某种方式搞砸了。

所以我不认为这是代码,似乎是权限错误。 我不知道问题出在哪里

更新我已经在 IAM 管理器中修改了 amplify-lambda-execution 策略的权限策略,这似乎已经解决了这个问题。

Amplify 最初添加的权限策略采用以下形式:

arn:aws:appsync:MYREGION:MYID:apis/MYAPIID/types/create/*
arn:aws:appsync:MYREGION:MYID:apis/MYAPIID/types/read/*
arn:aws:appsync:MYREGION:MYID:apis/MYAPIID/types/edit/*
arn:aws:appsync:MYREGION:MYID:apis/MYAPIID/types/delete/*

将此修改为:

arn:aws:appsync:MYREGION:MYID:apis/MYAPIID/types/*/fields/* 
arn:aws:appsync:MYREGION:MYID:apis/MYAPIID

允许 lambda function 在我的表上执行并成功执行 GraphQL 查询。 因此,Amplify 添加到 function 的权限似乎存在问题。 手动覆盖这些不是一个很好的解决方案。

安装后问题开始 - 放大cli“4.45.2”。 lambda cloudformation 模板中自动生成权限的旧代码如下所示。

            {
          "Effect": "Allow",
          "Action": [
            "appsync:Create*",
            "appsync:StartSchemaCreation",
            "appsync:GraphQL",
            "appsync:Get*",
            "appsync:List*",
            "appsync:Update*",
            "appsync:Delete*"
          ],
          "Resource": [
            {
              "Fn::Join": [
                "",
                [
                  "arn:aws:appsync:",
                  {
                    "Ref": "AWS::Region"
                  },
                  ":",
                  {
                    "Ref": "AWS::AccountId"
                  },
                  ":apis/",
                  {
                    "Ref": "myApiGraphQLAPIIdOutput"
                  },
                  "/*"
                ]
              ]
            }
          ]
        }

但是在我开始放大 cli "4.45.2" 并为 lambda 授予 appsync 权限之后。 它产生了:

    {
          "Effect": "Allow",
          "Action": [
            "appsync:GraphQL"
          ],
          "Resource": [
            {
              "Fn::Join": [
                "",
                [
                  "arn:aws:appsync:",
                  {
                    "Ref": "AWS::Region"
                  },
                  ":",
                  {
                    "Ref": "AWS::AccountId"
                  },
                  ":apis/",
                  {
                    "Ref": "myApiGraphQLAPIIdOutput"
                  },
                  "/types/create/*"
                ]
              ]
            },
            {
              "Fn::Join": [
                "",
                [
                  "arn:aws:appsync:",
                  {
                    "Ref": "AWS::Region"
                  },
                  ":",
                  {
                    "Ref": "AWS::AccountId"
                  },
                  ":apis/",
                  {
                    "Ref": "myApiGraphQLAPIIdOutput"
                  },
                  "/types/read/*"
                ]
              ]
            },
            {
              "Fn::Join": [
                "",
                [
                  "arn:aws:appsync:",
                  {
                    "Ref": "AWS::Region"
                  },
                  ":",
                  {
                    "Ref": "AWS::AccountId"
                  },
                  ":apis/",
                  {
                    "Ref": "myApiGraphQLAPIIdOutput"
                  },
                  "/types/update/*"
                ]
              ]
            }
          ]
        }


enter code here

尝试从 lambda 访问 appsync 时出现错误“权限被拒绝”。

我认为新 cli 在为 lambda 生成 appsync 权限时存在一些错误。

我手动移回 lambda cloudformation 文件中生成的旧代码,它工作正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM