Chris Newton, Author at Peak https://peak.ai Mon, 19 Feb 2024 09:03:51 +0000 en-GB hourly 1 https://wordpress.org/?v=6.8.3 https://assets.peak.ai/app/uploads/2022/05/25155608/cropped-Peak-Favicon-Black%401x-32x32.png Chris Newton, Author at Peak https://peak.ai 32 32 Access key rotation: Converting a PoC to production-ready code https://peak.ai/hub/blog/access-key-rotation-converting-a-poc-to-production-ready-code/ Wed, 08 Jul 2020 10:28:44 +0000 http://peak.ai/?post_type=blog&p=9361 Our new engineering blog focuses on access key rotation in Amazon Web Services (AWS). Our aim is to teach you to convert a Poc into production ready code.

The post Access key rotation: Converting a PoC to production-ready code appeared first on Peak.

]]>
Access Key Rotation
Portrait of author Chris Newton
Chris Newton

Software Engineer (DevOps)

See All Posts

Author: Chris Newton

By Chris Newton on July 8, 2020

In software development, a Proof of Concept (PoC) is a great way to validate your idea, by building a small project based around a simple use-case. However, once you have completed the PoC, you need to decide what to do next.

Note: this follows on from my previous article automating secret rotation in AWS. If you haven’t already, you can read that article here.

Does the PoC meet the initial use-case? Is it worth spending any more time on the idea? Is there a better solution?

You have to consider all of these questions to ensure the feature you are going to spend time and effort on will deliver the value you expect to your users. In our case, we reviewed the PoC and found that it was a feature we wanted to develop further, but this, unfortunately, wasn’t as simple as just deploying the code from the PoC into Production and walking away happy.

In my previous article, I explained how we built the Proof of Concept to see if it would be possible to automate the rotation of our access tokens and secret keys. When we set up the use-case for this PoC, we were trying to rotate one secret value used in one CloudFormation stack. However, in a production environment, the use-case would be much more complicated than this. We could feasibly have a situation where we need to rotate six secret values, used by over a hundred CloudFormation stacks. In this scenario, our PoC would fail straight away, and I will quickly summarise the process to explain why:

  1. We update a secret value in Secrets Manager
  2. A CloudWatch Event then triggers a Step Function
  3. The Step Function first finds all the CloudFormation stacks, which need to be updated, and then update them one by one, by removing and then recreating the resources in the stack

Updating two values shortly after each other, using the above process fails because the Step Functions run simultaneously and as a result, try to update the same CloudFormation stacks at the same time as each other. As a result, the CloudFormation stacks ended up in a failed state, because the second Step Function would be attempting to delete resources which had already been removed by the first Step Function.

The easiest way to solve this problem was to wait in between updating each secret value to ensure the Step Function had updated all of the CloudFormation stacks successfully. However, this defeated the purpose of what we were ultimately trying to achieve by having an automated solution. What we needed was a way to ensure only one Step Function would be running at once. To accomplish this, we decided to use an SQS queue to control when a Step Function was triggered. The new process would be as follows:

  1. We update a secret value in Secrets Manager
  2. A CloudWatch Event then add a ticket to the queue
  3. A Lambda function checks the SQS queue for tickets
  4. A Lambda function picks up the ticket and checks to see if a Step Function is currently running. One of two things will then happen: a) If no Step Function is running the Lambda function will trigger one. b) If a Step Function is running, the ticket will be left in the SQS queue and be picked up on the next check

By adding a queue into the process, it meant we had complete confidence that, no matter how many times a secret value was updated, only one Step Function would run at once.

Now we had a feature which, following thorough testing, was ready to be deployed to our Production environment. However, this is not the end of the development for this feature. In software development, you should always try and embrace the idea of Inspect and Adapt. So let’s break down what I mean by this.

Inspect

Inspecting is all about gathering feedback from your users. Finding out what works well, what doesn’t, and what is missing. We want to ensure that with everything we do, we are delivering value to our users and to do this; we must get their feedback.

Adapt

Adapting is all about taking that feedback and using it to develop improvements to the product.

In the case of our Secret Key Rotation tool, through user feedback, we have identified three areas to improve:

  1. Currently, if a stack fails to update the entire process will exit and reset itself. For example, if stack 50 of 65 fails to update the next time the Step Function runs, all 65 stacks will need to be updated again. By improving the logic of the Step Function, we could skip stack 50 and continue to update the remaining CloudFormation stacks. Stack 50 could then be updated manually by the user, which would save time.
  2. We could generate a report detailing if any CloudFormation stacks failed to update. Therefore, saving the user from having to go through logs to find which stack failed.
  3. We should include the ability to send the report directly to a user, via platforms like Slack. With this update, the user could change a value in Secrets Manager and then carry on with their work. They would no longer need to keep checking back to ensure the process has executed successfully

Now, we could have held off deploying this product to Production until we had implemented these three updates. However, by releasing when we did, we were able to gather user feedback to see which improvement is most important. Once the next update is released, we will again go through the process of Inspect and Adapt to see how we can next deliver the most value to our users.

Software development is all about releasing little and often so that we can quickly realise value and generate user feedback. We use this feedback to help us make the right changes to our product maximise the experience of our users.

We are looking into the possibility of open-sourcing this project in the future, so if you would be interested in this or any of our other open-source projects please have a look at our Github page.

If you enjoyed our blog on access key rotation, check out these other Peak product articles:

Sign up to the Peak newsletter

Get the latest Peak news and AI insights delivered straight to your inbox

The post Access key rotation: Converting a PoC to production-ready code appeared first on Peak.

]]>
Automating secret rotation in AWS https://peak.ai/hub/blog/automating-secret-rotation-in-aws/ Fri, 06 Mar 2020 13:30:39 +0000 http://peak.ai/?post_type=blog&p=6816 Peak's Software Engineer Chris Newton explains how he and the team were able to better manage secrets by automating secret rotation in AWS.

The post Automating secret rotation in AWS appeared first on Peak.

]]>
Our blog looks at automating secret rotation in aws
Portrait of author Chris Newton
Chris Newton

Software Engineer (DevOps)

See All Posts

Author: Chris Newton

By Chris Newton on March 6, 2020

At some point when writing code you will have to deal with secrets, such as API keys or access tokens. Coming up with an effective way of storing and accessing these secrets can be difficult, especially if you want to do it in a secure and scalable way.

On top of this you also need an easy way to rotate these secret values to ensure they stay secure and to prevent them from being compromised.

At Peak, we wanted to find a way of simplifying this process of managing secrets and automating secret rotation in AWS. But before I go into how we did this, let me explain why this was so important.

The problem

At Peak, most of our cloud architecture is hosted on AWS. Therefore, we use AWS CloudFormation extensively to deploy and manage our architecture. In fact, we have over 1300 CloudFormation stacks across all of our environments. Using CloudFormation is a great way to deploy and manage your infrastructure stacks, however, trying to manage the access tokens used by all of these stacks can be a nightmare.

In AWS there are currently two options when it comes to storing access tokens. You can either use AWS Secrets Manager or AWS Parameter Store (where you have the option of storing values as secure strings.) The problem with both of these options, we found, was that there was no easy way to pass the secrets to the resources being created by the CloudFormation stacks, such as AWS CodeBuild. The only way to achieve this was by either:

1. Storing the access token as a stack parameter
2. Writing a custom function to retrieve the access token which could be read by the CloudFormation stack during deployment

Unfortunately, there were issues with both of these options. With the first option, when it became time to rotate an access token you would have to manually find and update each stack individually. For us, this would be a massive undertaking and just wasn’t scalable.

The second option of using a custom resource did solve the issues around using stack parameters, however, it actually creates more problems than it solves from a security perspective. The main issue being that this process takes an access token value, which is stored securely, and then writes it as plain text to an S3 file. This file is then read by the CloudFormation stack during deployment. This means that if you don’t have the correct permissions set up in S3, your access token could be visible to anyone.

The saving grace

Thankfully, AWS has now introduced a third option. In CloudFormation you can now import values straight from AWS Secrets Manager into your stacks using the following command:

{{resolve:secretsmanager:{secret-name}:SecretString:{secret-key}}}

This meant that we could pass our access tokens from AWS Secrets Manager into our CloudFormation stacks without having to enter them as parameters or write them to S3. Perfect! Well…not quite.

In CloudFormation you can only update a stack if there has been a change to the stack template. Unfortunately, because the above line of code doesn’t change, it isn’t possible to rotate the access token used by your resources simply by performing a stack update.

The second issue is that, ideally, you want the process of rotating an access token to be as quick as possible. For instance, if one of your access keys becomes compromised, you need to be able to rotate that access token as quickly as possible. You can’t afford to spend all day manually finding and updating stacks. As I mentioned earlier, at Peak we have over 1300 CloudFormation stacks across all of our environments. We therefore needed a way of automating this entire process.

The solution

To build our solution we decided to use AWS Step Functions. Using a step function allowed us to build a simple workflow which could be reused.

There were two main parts to this problem:
1. Find all the stacks which need to be updated
2. Update all the required stacks so that the new access token would be pulled through to the resources

Firstly, to each CloudFormation stack which would need to use the access token, we added a parameter called RotateToken which was set to true. Then, using the AWS SDK, we listed all of our CloudFormation stacks and then filtered them based on whether they had RotateToken as a parameter.

The second task was a bit trickier because, as I mentioned earlier, updating the stacks once wouldn’t work because there has been no change to the template. To get around this problem we added Conditions to the any of the resources in the CloudFormation template that would be using the access token value. CloudFormation Conditions allow us to define the circumstances under which resources are created or configured. Basically, if the condition equates to true the resource is created, if it is false the resource is not created. We defined the condition as follows using the value of the RotateToken parameter, which has already been added to the stack, like so:

Conditions:
RotateToken:
!Equals [ !Ref RotateToken, “true” ]

You then just need to add the following statement to any resource which will be using the token:

Condition: RotateToken

In the Step Function we we would then update the stacks twice. The first update would set the `RotateToken` parameter to be `false` therefore effectively deleting any resource which uses the access token value. The second update would then set the `RotateToken` value to be `true`. This would recreate all the previously deleted resources and force them to pull through the updated access token value from Secrets Manager.

Finally, we configured the Step Function so that it would be triggered by an update to the secret in Secrets Manager. This was easily done using a Cloudwatch Event rule.

{
“detail-type”: [
“AWS API Call via CloudTrail”
],
“source”: [
“aws.secretsmanager”
],
“detail”: {
“eventSource”: [
“secretsmanager.amazonaws.com”
],
“eventName”: [
“PutSecretValue”,
“UpdateSecret”,
“CreateSecret”
]
}
}

All of this meant that by simply updating the access token value in Secrets Manager, all of the CloudFormation stacks which use that token would automatically be updated, saving countless hours of manual work.

This isn’t the whole story though. Once we proved that the solution was viable, we then had to make it ready for production and integrate it into our actual code base. To see how we accomplished this, read the next article in this series – Access Key Rotation: Converting a POC to Production Ready Code.

Enjoy this article on automating secret rotation in AWS? Make sure you’re following our Product Medium account for more engineering and data science insights.

Sign up to the Peak newsletter

Get the latest Peak news and AI insights delivered straight to your inbox

The post Automating secret rotation in AWS appeared first on Peak.

]]>