Consume an Event From Event Hub
Before we can start, let’s figure out how to change our name so we can see if everything works.
Go to my.byu.edu https://my.byu.edu/uPortal/f/u26l1s4/normal/render.uP and sign in.
Find and click “Update My Personal Information”
Click Names, and here you can change you preferred name as much as you want.
You might want to change it back when you are done.
Remember that security key from your webhook? We are going to need that now. Store it in the Parameter Store as HMAC_SECURITY_KEY (If you have forgotten how to do this, go look at Build Oracle DB/Tables or Practice Using WSO2)
So, because Event Hub doesn’t send a JWT, we will need to take care of it before the byu-jwt middleware. The nice thing is that because we will be handling everything before we get to the enforcer, we aren’t going to make any changes to swagger. This makes sense, though, because this feature is not something we want users to be able to mess with.
Let’s start by importing a function we haven’t written yet into the index.js file.
Then, add middleware to catch the Event Hub request and test if it is sending a JSON.
HMAC verification requires the string that the is sent, so we can’t use the body-parser module to format it, so we will have to do it ourselves. We have to do it this way because the JSON is sent in chunks instead of all at once. Then call the function we are about to write.
Now let’s go to our favoritecolor.js controller.
Nodejs comes with a module called crypto that allows us to check the HMAC sent with the the event and make sure it is legit. Require it.
Then add the security key you put into the parameter store to the variables that you want to get.
At the end of your file, you are ready to start your “changedName” function.
Check if the HMAC is valid.
When we get a name we are going to want to see if it is in our DynamoDB, if it isn’t in our database we can ignore it, and if it is we want to update it. Either way, we want to send a 200 back to tell Event Hub that we have processed the data.
First, let’s see if we have the data
Then let’s handle the situation where the name change doesn’t effect us.
And if the person is in our database, let’s change their name!
You are going to need to change things in the Recorder so it can update the name there too. Let’s go the Recorder’s index.js and write a function for it to update the name.
Push it to Git Hub, watch it deploy and hope that it works! You can’t really test it with actual data while it is local, because the Event Hub sends the info to your API.
And then go to your DynamoDB table and see if it has changed too.
If it works, you did it!