Part 5
Sorry for the delay between part 4 and 5! I was at the Microsoft MVP Summit this past week and didn’t have time to devote towards updating. This series is nearing completion with just a few more parts to go.
In Part 4 we published the AWS Lambda and created the AWS KMS Key that will be used for encrypting and decrypting secrets. In Part 5 we will configured the AWS CodeCommit repository trigger to invoke the AWS Lambda and encrypt our secrets to store in in the cc2af.yml file.
Series Table of Contents
- Part 1 of 6: Project Introduction and Overview
- Part 2 of 6: Project Settings, Authentication, Azure Function App Deployment, and AWS Code Commit Deployment
- Part 3 of 6: Creating AWS IAM User for Git, Generating HTTPS Git Credentials, Configuring Azure Web App External Git Deployment, and Initial Manual Deployment
- Part 4 of 6: Create AWS IAM Role, Deploy the C# .NET Core 2.0 AWS Lambda, and Create an AWS KMS Key
- Part 5 of 6 (This Post): Add AWS CodeCommit Trigger and Encrypt Secrets with AWS KMS
- Part 6 of 6: Generate Configuration File, Automatic deployment from AWS CodeCommit to Azure Functions, Trigger the Azure Function from PowerShell, and Series Conclusion
Grant AWS CodeCommit Repository Execution Permission the AWS Lambda
With the AWS Lambda created we can now link the AWS CodeCommit repository up to execute it. However, before we do that we need to grant the CodeCommit repository rights to invoke the Lambda.
Then we need to verify it with Pester:
And it it was successful you should see:
Add AWS CodeCommit Repository Trigger to Execute AWS Lambda
We want our commits to the AWS CodeCommit repository to trigger an automatic deployment to Azure Functions. To do that we need need to add the trigger to the AWS CodeCommit repo to invoke our AWS Lambda. The following code creates a trigger when any action is taken on the master branch of the repository:
Next we verify the repository trigger was added with Pester:
Which should result in:
At this point, any commit to the master branch of the PBnC CodeCommit repository will trigger an invocation of the TriggerAzureFunctionDeployment Lambda which will then trigger the git deployment on the Azure Functions Web App. If we made a commit now, without a cc2af.yml, the deployment will fail because it does not have the required configuration.
Functions for Encrypting and Decrypting Strings with AWS KMS Key
Before we can create the cc2af.yml file, we need a few helper functions to encrypt and decrypt secrets. We will need to keep these functions around for future use should any of our secrets (the AWS IAM User HTTPS Git Credentials password and the Azure Functions Web Service Deployment credentials password) change. We also need them to create the encrypted strings to store in the cc2af.yml.
KMS does not encrypt and decrypt strings directly. Instead, KMS works with binary data streams. This makes encrypting and decrypting strings a bit inconvenient in PowerShell as the AWSPowerShell module does not provide an easy to use function to do this. Also, when the decrypted binary is returned, it is not a binary representation of a string. The string we want to encrypt needs to be converted to binary, the binary then encrypted with KMS, the returned binary then needs to be Base64 encoded. This means that what we get is a Base64 representation of KMS encrypted binary.
This is the function for encryption:
Decryption just runs the opposite direction. We need to take a Base64 string, convert it to binary, decrypt the binary, then convert the returned decrypted binary to a string.
One unfortunate problem is that the Base64 string representations of the encrypted binary that are returned are rather lengthy. I tried several compression techniques, including gzip, but they did not yield results that made them worth the complexity of implementation. One idea would be combine gzip and base85 encoding. However, AES encrypted binary data doesn’t necessarily compress well, and base85 only offers a 7% reduction in string size over base64. If anyone has a good idea to make these encrypted strings more compact while still using KMS, please let me know.
It’s important to note that the AWS Lambda has a C# implementation of decryption:
Also, you do not require the Key ID to decrypt data with KMS. I’m guessing the encrypted binary includes information on the key used to encrypt it. You (or the role your code assumes) just need to have decryption permissions on the required key.
Encrypt Secrets with AWS KMS
With our helpful functions available we can now encrypt our secrets.
Part 5 End
That’s it for Part 5. Part 6 we will create the cc2af.yml file and perform out first Azure Function deployment from AWS CodeCommit.