Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

SNS: missing field `SigningCertUrl #129

Closed
ymwjbxxq opened this issue Dec 22, 2022 · 5 comments
Closed

SNS: missing field `SigningCertUrl #129

ymwjbxxq opened this issue Dec 22, 2022 · 5 comments

Comments

@ymwjbxxq
Copy link
Contributor

Take this simple Lambda:
https://serverlessland.com/patterns/sns-sqs-lambda-sam-rust (the code is not up to date but 8 months ago it was working)

If you update the code and the libs

use aws_lambda_events::event::{sns::SnsMessage, sqs::SqsEvent};
use futures::future::join_all;
use lambda_runtime::{service_fn, Error, LambdaEvent};
use serde::Deserialize;

#[tokio::main]
async fn main() -> Result<(), Error> {
    tracing_subscriber::fmt()
        .with_ansi(false)
        .without_time()
        .init();

    lambda_runtime::run(service_fn(|event: LambdaEvent<SqsEvent>| execute(event))).await?;

    Ok(())
}

pub async fn execute(event: LambdaEvent<SqsEvent>) -> Result<(), Error> {
    println!("Input {:?}", event);

    let mut tasks = Vec::with_capacity(event.payload.records.len());
    for record in event.payload.records.into_iter() {
        tasks.push(tokio::spawn(async move {
            if let Some(body) = &record.body {
                let sns_message = serde_json::from_str::<SnsMessage>(&body).unwrap();
                let sns_message = sns_message.message;
                let request = serde_json::from_str::<MyStruct>(&sns_message);
                if let Ok(request) = request {
                    // Do something
                    println!("Request {:?}", request);
                }
            }
        }));
    }

    join_all(tasks).await;

    Ok(())
}

#[derive(Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct MyStruct {
    pub name: String,
    pub surname: String,
}

cargo.toml

[package]
name = "sns-sqs-lambda-rust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[[bin]]
name = "handler"
path = "src/bin/handler.rs"

[dependencies]
aws_lambda_events = { version = "0.7.2", default-features = false, features = ["sqs", "sns"] }
futures = "0.3.17"
lambda_runtime = "0.7.2"
serde = {version = "1.0", features = ["derive"] }
serde_json = "1.0.68"
simple-error = "0.2"
tokio = { version = "1", features = ["full"] }
tracing-subscriber = "0.3"

You get an exception:
`Input LambdaEvent { payload: SqsEvent { records: [SqsMessage { message_id: Some("44dec5ec-0128-4661-9c54-a06485ead572"), receipt_handle: Some(".....="), body: Some("{\n "Type" : "Notification",\n "MessageId" : "5a1c0085-ee71-5e38-9d84-5c9bdcb02ad5",\n "TopicArn" : "arn:aws:sns:eu-central-1:xxxxx:sam-app-MySnsTopic-BWiabVkXTYg2",\n "Message" : "{\n \"name\": \"Daniele\",\n \"surname\": \"Frasca\"\n}",\n "Timestamp" : "2022-12-22T10:25:05.377Z",\n "SignatureVersion" : "1",\n "Signature" : ".....==",\n "SigningCertURL" : "https://sns.eu-central-1.amazonaws.com/SimpleNotificationService-56e67fcb41f6fec09b0196692625d385.pem",\n "UnsubscribeURL" : "https://sns.eu-central-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:eu-central-1:xxxxx:sam-app-MySnsTopic-BWiabVkXTYg2:44fb3b0d-6b6a-48cc-9183-f52a3d238175"\n}"), md5_of_body: Some("ab0cac5be52987991d1d1b2b57a08480"), md5_of_message_attributes: None, attributes: {"SentTimestamp": "1671704705405", "SenderId": "AIDAISDDSWNBEXIA6J64K", "ApproximateFirstReceiveTimestamp": "1671704705410", "ApproximateReceiveCount": "1"}, message_attributes: {}, event_source_arn: Some("arn:aws:sqs:eu-central-1:xxxxx:sam-app-MySqsQueue-nDLzmzS8vEaH"), event_source: Some("aws:sqs"), aws_region: Some("eu-central-1") }] }, context: Context { request_id: "d187ab64-f92b-52a2-a81b-f77acb9cde03", deadline: 1671704735726, invoked_function_arn: "arn:aws:lambda:eu-central-1:xxxxxx:function:sam-app-MyFunction-QqyqP0mMMxOS", xray_trace_id: Some("Root=1-63a43081-22df4f205924b3de2b0c3b46;Parent=54651cb434f944d8;Sampled=0"), client_context: None, identity: None, env_config: Config { function_name: "sam-app-MyFunction-QqyqP0mMMxOS", memory: 128, version: "$LATEST", log_stream: "2022/12/22/[$LATEST]7da35658a61a4b0cb323242f7f20526a", log_group: "/aws/lambda/sam-app-MyFunction-QqyqP0mMMxOS" } } }

thread 'tokio-runtime-worker' panicked at 'called Result::unwrap() on an Err value: Error("missing field SigningCertUrl", line: 11, column: 1)', src/bin/handler.rs:29:77

`

Please let me know if it is not clear.

@calavera
Copy link
Owner

calavera commented Dec 22, 2022

oh, that's weird, the field is here:

#[serde(rename = "SigningCertURL")]

I'm guessing case sensitive issues 🤦

@ymwjbxxq
Copy link
Contributor Author

It happens.

SigningCertUrl
SigningCertURL

I did not see it as well at first look.

@ymwjbxxq
Copy link
Contributor Author

On the readme:
We have no desire to manually write event definitions and contributions to that effect will not be accepted. If the [official Go SDK](https://github.com/aws/aws-lambda-go/tree/master/events) is missing an event definition, please file a bug on that project rather than submitting a fix here. Once the official Go SDK has included your changes this project will pick up the new event definitions.

In GO I can see it is correct:
https://github.com/aws/aws-lambda-go/blob/99a9a4bd72b603da8d75d74d77937c146fed0b7c/events/sns.go#L28

I also see that what they call SNSEntity here is called SnsMessage. If it is all generated automatically, I need to find out where the name change is.

Another thing that I see is:
#[serde(rename_all = "PascalCase")]

So, in theory, this SigningCertURL should be SigningCertUrl.

Can you explain to me how to fix it? I think I cannot directly change the aws-lambda-events/aws_lambda_events/src/sns/mod.rs

@calavera
Copy link
Owner

Can you explain to me how to fix it? I think I cannot directly change the aws-lambda-events/aws_lambda_events/src/sns/mod.rs

Yes, you can modify that file directly. Only the ones in the generated directory are generated from Go. Make sure you update the test examples too.

@calavera
Copy link
Owner

I just released 0.8.2 with the fix. Thank you all.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants