使用无凭据的AWS SDK确认SNS订阅-->未配置RegionEndpoint或ServiceURL [英] Use AWS SDK without credentials to confirm a SNS subscription --> No RegionEndpoint or ServiceURL configured

查看:20
本文介绍了使用无凭据的AWS SDK确认SNS订阅-->未配置RegionEndpoint或ServiceURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有AWS帐户。我确实有一个HTTP POST网络挂钩,它应该接收到SNS主题的HTTPS端点。尝试确认订阅时,日志中出现以下错误消息:

未配置RegionEndpoint或ServiceURL

我使用官方的AWS SDK for.NET调用ConfirmSubscriptionAsync方法。我使用SDK的目的是让它自己处理签名验证,正如AWS文档中所建议的那样。但是,如果我没有为自己提供AWS帐户,则无法做到这一点。这是预期的吗?

以下是应处理传入请求的代码

    public class Func
    {
        [FunctionName(nameof(Func))]
        public async Task Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "/receive/sns")] HttpRequest req, ILogger log)
        {
            if (req.Headers.TryGetValue("x-amz-sns-message-type" , out var msgType))
            {
                if (msgType == "SubscriptionConfirmation")
                {
                    var snsClient = new AmazonSimpleNotificationServiceClient();
                    var subscriptionRequest = await JsonSerializer.DeserializeAsync<ConfirmSubscriptionRequest>(req.Body);
                    var subscriptionResponse = await snsClient.ConfirmSubscriptionAsync(subscriptionRequest);
                    log.LogTrace(Newtonsoft.Json.JsonConvert.SerializeObject(subscriptionResponse));
                }
                else if (msgType == "Notification")
                {
                    var notification = await JsonSerializer.DeserializeAsync<AwsSnsNotification>(req.Body);
                    log.LogTrace(JsonSerializer.Serialize(notification));
                }
                else
                {
                    throw new ArgumentOutOfRangeException("`x-amz-sns-message-type header`", msgType, "Allowed as per AWS doc: `SubscriptionConfirmation`, `Notification`");
                }
            }
            else
            {
                throw new ArgumentNullException("x-amz-sns-message-type header not set");
            }
        }
    }

推荐答案

无需创建客户端实例即可解决。消息模型公开静电ParseMessage方法。然后,在消息实例上,我可以调用IsMessageSignatureValid方法。随后,我只需通过HTTP获得订阅确认url并完成。在这里发帖,希望这对其他人也有帮助。

    public class Func
    {
        [FunctionName(nameof(Func))]
        public async Task Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "receive/sns")] HttpRequest req, ILogger log)
        {
            if (req.Headers.TryGetValue("x-amz-sns-message-type" , out var msgType))
            {
                var reqBody = await new StreamReader(req.Body, Encoding.UTF8).ReadToEndAsync();
                var msg = Message.ParseMessage(reqBody);
                if (!msg.IsMessageSignatureValid())
                {
                    throw new UnauthorizedAccessException("Message signature invalid");
                }
                else
                {
                    if (msgType == "SubscriptionConfirmation")
                    {
                        await Http.GetAsync(msg.SubscribeURL);
                    }
                    else if (msgType == "Notification")
                    {
                        log.LogTrace(msg.MessageText);
                    }
                    else
                    {
                        throw new ArgumentOutOfRangeException("`x-amz-sns-message-type header`", msgType, "Allowed as per AWS doc: `SubscriptionConfirmation`, `Notification`");
                    }
                }
            }
            else
            {
                throw new ArgumentNullException("x-amz-sns-message-type header not set");
            }
        }
    }

这篇关于使用无凭据的AWS SDK确认SNS订阅--&gt;未配置RegionEndpoint或ServiceURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆