登录
首页 >  Golang >  Go问答

请求未包含 methodArn,无法授权 lambda 调用

来源:stackoverflow

时间:2024-02-28 15:06:28 393浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《请求未包含 methodArn,无法授权 lambda 调用》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我在从我定义的授权者 lambda 中的请求对象获取 methodarn 时遇到问题。我正在将无服务器与 golang 结合使用。这是我所拥有的代码的经过编辑的设置。

ma​​in.go - 授权处理函数

func handler(request events.apigatewaycustomauthorizerrequesttyperequest) (events.apigatewaycustomauthorizerresponse, error) {
  // i have tried logging the entire request to cloud watch, see further down for the sample

  // i have code here to verify a jwt token
  return nil, nil
}

serverless.yml - 我的无服务器设置,用于注册函数并定义授权者

functions:
  permission_verify:
    handler: bin/handlers/permission/verify

package:
  patterns:
    - "!./**"
    - ./bin/**

provider:
  httpapi:
    authorizers:
      custom:
        functionname: permission_verify
        type: request

通过上述设置,我可以在其他微服务中使用授权者来验证 jwt 令牌,但现在我想向授权者添加更多功能,因此我需要请求中存在 methodarn

以下是访问使用授权者的端点后云监视的输出

云手表 - 请求 json

{
    "type": "REQUEST",
    "methodArn": "",
    "resource": "",
    "path": "",
    "httpMethod": "",
    "headers": {
        "accept": "*/*",
        "accept-encoding": "gzip, deflate, br",
        "authorization": "redacted",
        "content-length": "redacted",
        "content-type": "application/json",
        "host": "redacted",
        "postman-token": "redacted",
        "user-agent": "PostmanRuntime/7.31.3",
        "x-amzn-trace-id": "redacted",
        "x-forwarded-for": "redacted",
        "x-forwarded-port": "443",
        "x-forwarded-proto": "https"
    },
    "queryStringParameters": null,
    "pathParameters": null,
    "stageVariables": null,
    "requestContext": {
        "path": "",
        "accountId": "redacted",
        "resourceId": "",
        "stage": "$default",
        "requestId": "redacted",
        "identity": {
            "apiKey": "",
            "sourceIp": ""
        },
        "resourcePath": "",
        "httpMethod": "",
        "apiId": "redacted"
    }
}

我在互联网上搜索了没有关于请求中调用 lambda 的信息的解释和原因,但无济于事。已经有一段时间了,任何帮助将不胜感激。


正确答案


我找到了一个使用 rest 而不是 http 的解决方案。我必须重做所有功能并定义无服务器,如下所示;

serverless.yaml

custom:
  authorizer:
    type: token
    identitysource: method.request.header.authorization
    identityvalidationexpression: bearer (.*)
    name: permission_verify
    # if this is in a different service, you can replace the name property with 'arn' having the arn of the function

functions:
  permission_verify:
    handler: bin/handlers/permission/verify

  permission_read:
    events:
      -http: # i had httpapi before
        method: get
        path: /
        authorizer: ${self:custom.authorizer}

这使我能够将函数签名更改为如下所示,并且在检查云监视日志后,我能够获取方法 arn。

func handler(request events.apigatewaycustomauthorizerrequest) (events.apigatewaycustomauthorizerresponse, error)

这是我在云手表中获得的示例;

{
    "type": "TOKEN",
    "authorizationToken": "Bearer ",
    "methodArn": "arn:aws:execute-api:us-east-1:redacted/PUT/"
}

总而言之,造成这种情况的原因只是不一致的无服务器配置,不符合我所需的目的。我想说的是,在我看来,人们很容易认为无服务器是定义基础设施的一种万无一失的方法,但事实并非如此,并且最好了解您想要什么以及所提供的内容。

今天关于《请求未包含 methodArn,无法授权 lambda 调用》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>