How to Become Great at API Key Rotation: Best Practices and Tips (2024)

Best practices

Secret management can be a complex challenge, especially when you are trying to do it in a way that is right for security. Key rotation is a big piece of that puzzle. In this article, we will take you from zero to hero on key rotation.

  • How to Become Great at API Key Rotation: Best Practices and Tips (1)

    Guest Expert

    GitGuardian hires external cybersecurity experts to share their unique experience and knowledge in security on the GitGuardian blog.

    More posts by Guest Expert.

Guest Expert

7 min read

Share
How to Become Great at API Key Rotation: Best Practices and Tips (3)

How to Become Great at API Key Rotation: Best Practices and Tips (4)

C.J. May

Information security professional and passionate programmer
with broad interests encompassing many areas of IT.
Twitter | GitHub

Key rotation 101

What is key rotation?

Key rotation is the practice of changing access keys on a regular schedule to reduce the likelihood that a key can be abused if it gets compromised.

Why rotate API keys?

Over time, secrets sprawl. They end up being exposed in places they shouldn’t be due to human mistakes and operational practices during a product’s lifecycle. Detecting when a secret is exposed is important, but rotating API keys on a regular basis is an essential layer of security to ensure that exposed secrets can’t be used for long and are less likely to work when they are discovered.

What happens if you never rotate an API key?

The longer a key is around, the more chances it has to get leaked or compromised. Not only that, but if an API key is never rotated it is guaranteed to work for any malicious party that finds it. Rotating your keys on a regular basis reduces the window of opportunity in which a key can be abused.

Key rotation best practices

Document from the beginning!

Having well-documented API keys will help you avoid a hard time when you need to rotate a key. The first thing you should do on this front is to record where your keys are being used. If a key is compromised, you’ll want to quickly identify the affected applications and data. Even if it’s not for a critical security issue, it will make the general process of rotating the key a lot smoother.

The next thing to do is record who/what has access to an API key. When a developer leaves the company, even if it was on good terms, they should no longer have access to internal systems. Keeping a detailed record of which keys an individual has access to will make it possible to quickly rotate all the relevant keys.

How often should you rotate API keys?

As a best practice, you should rotate API keys at least every 90 days. If you have a strong automated process for rotating keys, you could rotate much more often than that. We will get into automation later, though.

Important events may require you to rotate keys as well. Example events could be a developer leaving the company or a secret being compromised or exposed in plain text. Having a well-established process for key rotation is especially important for time-sensitive events like these.

How do you rotate a key without downtime?

To rotate a key with zero downtime, you’ll need to create and deploy a new key before revoking the old one. If possible, monitor logs to ensure that the new key is being used after it has been deployed. Once the new key is being used by your application, you can revoke the old key.

What if the service only allows one active key at a time?

If the service you’re using only supports one active key at a time, you have a couple of options. The option you go with will depend on whether you can choose a custom secret to replace your old one:

  • If you can manually set the new key, you can have your application use BOTH the new key and the old one that is being rotated. Just have your app try the new key first, and then fall back to the old one if it fails. Once you have manually rotated it to the new custom value, your app will automatically start using the new key.
  • If you cannot manually set the new key, your only option to avoid downtime is to create a new application or account with its own key on the service side. Keeping track of multiple client credentials for the same application isn’t clean nor desirable, so this should be a last resort.

How do you automate key rotation?

Assuming you know how to rotate a secret manually, it can be automated if the service exposes key management through their API. To enable automated key rotation, the service’s API needs to let you:

  • Create new keys
  • Revoke old keys

The final implementation of your automation is going to depend on the service you are working with and how they do key management. If you know the process and have access to the proper APIs, you can do it! One last thing – don’t forget to validate active and inactive keys as part of your automation!

Key rotation examples

Now that we know what key rotation should look like, let’s look at a couple real examples.

Rotating a GitHub App Token

Let’s start with an easy one. Rotating a GitHub App key is straightforward because GitHub allows apps to have more than one active key at the same time. You can find your registered apps under Settings > Developer settings > GitHub Apps.

How to Become Great at API Key Rotation: Best Practices and Tips (7)

To rotate the key for an app, you need to click the “Edit” button and scroll all the way down until you see the “Private keys” section.

How to Become Great at API Key Rotation: Best Practices and Tips (8)

As you can see, we are able to generate a new private key without revoking the old one. In fact, GitHub doesn’t even allow us to revoke the old key until there is a new one available!

How to Become Great at API Key Rotation: Best Practices and Tips (9)

For scenarios like this, this is what our key rotation process looks like:

  1. Generate a new private key
  2. Deploy the new private key to our application
  3. Revoke the old private key

When we generated the new private key for our app, we didn’t need to worry about our app getting locked out because our old key stayed active. Services that allow multiple active keys are the easiest to work with when rotating secrets.

Rotating an Airbrake project key

Not all key rotations will be that easy, unfortunately, so let’s also walk through a difficult key rotation: Airbrake project API keys. Airbrake is a service that allows you to monitor your app for errors and performance issues.

You can find your Airbrake project’s API key right in the settings page on the right-hand side.

How to Become Great at API Key Rotation: Best Practices and Tips (10)

As you can see from the screenshot, each Airbrake project only has one active key, and the “Regenerate API Key” button will instantly revoke the current key and replace it with a new one. If our goal is to rotate the key with no down time, this puts us between a rock and a hard place.

We can’t choose what the next API key will be, so we can’t preload the next key into our app alongside the old key (the trick that we covered earlier). Our only option to avoid downtime is to create another Airbrake project, and deploy that new project’s ID and key to our application. This gets really messy because the new project won’t have all of the settings we changed in the old one. We could actively maintain two identical projects and switch between the two each time we need to rotate the API key, but that isn’t a good solution either.

In situations like these, our only option may be to regenerate and deploy the new API key as quickly as possible and accept a lapse in coverage for Airbrake monitoring. If you are a developer who is implementing authentication for your service’s API, take this as an example of what not to do to your API consumers. Allow for multiple active keys in the applications you develop.

Conclusion

We’ve covered a lot of ground in this article from the what’s, why’s, and how’s of key rotation to some real-world examples. To wrap it all up, I’ll capture all the advice above into a concise best practices list. If you want to be great at API key rotation, you need to:

  1. Record where your keys are being used
  2. Record who/what has access to an API key
  3. Rotate keys at least every 90 days
  4. Rotate keys when developers leave
  5. Rotate keys when they are leaked or compromised
  6. Create and deploy a new key before revoking the old one
  7. (Try the backup plans if the previous step isn’t straightforward)
  8. Automate rotation if the service exposes key management through their API
  9. Validate that keys are active/inactive as expected
  10. Allow for multiple active keys in the applications you develop

Hopefully at this point you feel like you have a concrete understanding of key rotation, and maybe you’re even starting to think about how to be better at this in your own software development lifecycle.

If you feel like you’re a long way from greatness in this area, just start with good documentation and take it one app at a time. You’ve got this!

How to Become Great at API Key Rotation: Best Practices and Tips (2024)

FAQs

How to Become Great at API Key Rotation: Best Practices and Tips? ›

Delete unneeded API keys: To minimize your exposure to attack, delete any API keys that you no longer need. Rotate your API keys periodically: To rotate your API keys, call the CreateKey method. After the replacement keys are created, update your applications to use the newly-generated keys and delete the old keys.

What is the best practice for key rotation? ›

The best practice is to rotate your keys regularly. Choose a rotation interval between one and 12 months for your root key based on your security needs. After you set a rotation policy for a root key, the clock starts immediately based on the initial creation date for the key.

What is the best practice for API token rotation? ›

As a best practice, you should rotate API keys at least every 90 days. If you have a strong automated process for rotating keys, you could rotate much more often than that. We will get into automation later, though. Important events may require you to rotate keys as well.

How to rotate API keys? ›

How to Rotate API Keys. On the Credentials Details page within the Developer tab, select the Rotate API Keys button. The Choose when to revoke the existing key dialogue box appears. Select the time limit when you would like to rotate your API Keys in the Within field, ranging from Now to Seven Days.

How often should API keys be rotated? ›

The frequency of API key rotation may vary based on factors such as the level of security required, industry best practices, and organizational policies. Typically, API keys are rotated at regular intervals, such a quarterly or annually, depending on the specific requirements and risk tolerance in your company.

What is the key rotation technique? ›

Key rotation in asymmetric encryption involves the following steps:
  1. Step 1: Generate a new key pair. ...
  2. Step 2: Sign the new public key with the old private key. ...
  3. Step 3: Update systems with the new key pair. ...
  4. Step 5: Revoke and delete the old public key.
May 26, 2023

What is the proper method of rotation? ›

The amount of rotation is described in terms of degrees. If the degrees are positive, the rotation is performed counterclockwise; if they are negative, the rotation is clockwise. The figure will not change size or shape, but, unlike a translation, will change direction.

How often should API tokens be rotated? ›

Once you have a system in place to manage your API tokens, it makes sense to start rotating API tokens on a regular basis. Your specific rotation schedule will depend on the use case. For read/write operations in banking or healthcare, rotating every 5 or 10 minutes might be necessary.

What is the best practice for securing API keys? ›

To safeguard them:
  • Store keys away from code, preferably in environmental variables.
  • Use secure storage solutions with encryption.
  • Rotate keys regularly and delete obsolete ones.
  • Monitor key usage and set access limits.
  • Train teams on API key security.
  • Avoid exposing keys in public channels or repositories.
Oct 17, 2023

What is the risk of not rotating API keys? ›

API keys should be rotated to ensure that data cannot be accessed with an old key that might have been lost, cracked, or stolen. 'Regenerating Key' may break existing client connectivity as the client will try to connect with older API keys they have stored on devices.

What is the key rotation policy? ›

Automatic key rotation at a defined period, such as every 90 days, increases security with minimal administrative complexity. You should also manually rotate a key if you suspect that it has been compromised, or when security guidelines require you to migrate an application to a stronger key algorithm.

How to get rapid API key? ›

To get an API Key:
  1. Log in or sign up for your RapidAPI account.
  2. Navigate to any API documentation page by searching for or clicking on one of the collections from the homepage.
  3. Scroll down to the "Header Parameters" section of the API console.
  4. Your API Key should be visible in the "X-RapidAPI-Key" field.

What are the best practices for API key expiry? ›

Constantly rotate API keys to reduce potential vulnerabilities if exposed. Create a security policy that requires changing API keys every 30, 60 or 90 days. Many compliance regulations and frameworks, such as ISO 27001, require regular key rotation. Delete unused API keys.

How do I know if my API key is correct? ›

Double-click the saved HTML file to open it in your web browser. To access developer tools, right-click anywhere on the page and select "Inspect" or "Inspect Element" to open the browser's developer tools. Navigate to the "Console" tab. Here, you will find any errors or messages related to your API key.

How long is the API key best practices? ›

API Key Best Practices

Key Length: An API key is generally more secure if it is longer. You can consider using a key length of about 32 characters, as it is more difficult for attackers to guess. Rate Limiting: Rate limiting can help to prevent the abuse of your API.

How to perform key rotation? ›

The process of key rotation involves four steps: generating new keys, distributing new keys, switching to new keys, and retiring old keys. Each step must be properly planned and executed to prevent data loss or corruption.

How often should keys be rotated? ›

Automatic key rotation at a defined period, such as every 90 days, increases security with minimal administrative complexity. You should also manually rotate a key if you suspect that it has been compromised, or when security guidelines require you to migrate an application to a stronger key algorithm.

What is the best practice for rotating tires? ›

You should rotate your tires approximately every 6,000 to 8,000 miles (about 9,600 to 13,000 km). For some of you, this is the same time as your car's service. The right time may also be when changing from winter to summer tires. Or simply when you buy new tires.

How often should access keys be rotated? ›

Engineers must rotate their access keys and AWS management console password every 90 days.

Top Articles
Why you could see a big credit card charge after buying gas – and why you shouldn't freak out
Paper Money Deposit
Wmaz 13
Everything you need to know about a Sam's Club Membership
Kool Online Offender Lookup
NFL on CBS Schedule 2024 - How To Watch Live Football Games
Site : Storagealamogordo.com Easy Call
Registrar Utd
Jocelyne Mirando
Nashville Tranny
Bullocks Grocery Weekly Ad
Guide to Gold Farming in Guild Wars 2 - MMOPIXEL
Eggy Car Unblocked - Chrome Web Store
Apple Nails & Spa, 3429 Toringdon Way, Charlotte, Reviews and Appointments
Northwell.myexperience
Walmart Listings Near Me
Violent Night Showtimes Near The Riviera Cinema
Arthritis Weather Index
MyChart | University Hospitals
Oklahoma City Municipal Courthouse
2021 Lexus IS 350 F SPORT for sale - Richardson, TX - craigslist
Frontline Education Absence Management Login
Weather Arlington Radar
Zwei-Faktor-Authentifizierung (2FA) für Ihre HubSpot-Anmeldung einrichten
Kidcheck Login
Couches To Curios Photos
Walgreens On Nacogdoches And O'connor
Mybackpack Bolles
Rugged Gentleman Barber Shop Martinsburg Wv
South Louisiana Community College Bookstore
Acnh Picnic Table
Fingerhut Teleflora Promo Code
What is a W-8BEN Form and Why Does It Matter?
Ridgid Pro Tool Storage System
Grand Forks (British Columbia) – Travel guide at Wikivoyage
The dangers of statism | Deirdre McCloskey
Seller Feedback
Jennifer Brabson Cleek
Sirius Satellite Radio Sports Schedule
Daniel And Gabriel Case Images
Honda Fury Forums
Ihop Ralph Ave
Effingham Radio News
11526 Lake Ave Cleveland Oh 44102
Victoria Maneskin Nuda
The Complete Guide to Chicago O'Hare International Airport (ORD)
Thekat103.7
Salon5 – Europa, was geht? – Podcast
Craigslist Antelope Valley General For Sale
Vrlbi Rentals
Fitgirl Starfield
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 6147

Rating: 4.4 / 5 (65 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.