[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-21。"],[[["\u003cp\u003eService account keys should be rotated regularly, ideally at least every 90 days, to mitigate the risks associated with leaked or stolen keys.\u003c/p\u003e\n"],["\u003cp\u003eRotating service account keys involves creating new keys, replacing existing ones in applications, disabling the old keys, and then deleting the replaced keys after confirmation that the applications are working correctly.\u003c/p\u003e\n"],["\u003cp\u003eCentralized secret management services like HashiCorp Vault can be used for automatic key rotation, but cloud-based secret managers are not recommended if the application already has an identity.\u003c/p\u003e\n"],["\u003cp\u003eA custom notification system can be implemented to alert teams when service account keys need rotation, leveraging Cloud Asset Inventory to identify keys based on their creation time.\u003c/p\u003e\n"],["\u003cp\u003eExpiring service account keys are not advised for key rotation due to the potential for outages if not managed properly; instead, using the rotation process is preferred.\u003c/p\u003e\n"]]],[],null,["# Service account key rotation\n\n[Service account keys](/iam/docs/service-account-creds#key-types) are private keys that let you\nauthenticate as a service account.\nKey rotation is the process of replacing your existing keys with new keys and\nthen invalidating the replaced keys. We recommend that you routinely\nrotate all keys that you manage, including your service account keys.\n\nRotating service account keys can help reduce the risk posed by leaked or stolen\nkeys. If a key is leaked, it might take bad actors days or weeks to discover the\nkey. If you regularly rotate your service account keys, there's a higher chance\nthat the leaked keys will be invalid by the time a bad actor gets them.\n\nHaving an established process for rotating service account keys also helps you\nact quickly if you suspect that a service account key has been compromised.\n| **Note:** Service account keys are a security risk if not managed correctly. You should [choose a more secure alternative to service account keys](/docs/authentication#auth-decision-tree) whenever possible. If you must authenticate with a service account key, you are responsible for the security of the private key and for other operations described by [Best practices for managing service account keys](/iam/docs/best-practices-for-managing-service-account-keys). If you are prevented from creating a service account key, service account key creation might be disabled for your organization. For more information, see [Managing secure-by-default organization resources](/resource-manager/docs/secure-by-default-organizations).\n|\n|\n| If you acquired the service account key from an external source, you must validate it before use.\n| For more information, see [Security requirements for externally sourced credentials](/docs/authentication/external/externally-sourced-credentials).\n\nHow often to rotate keys\n------------------------\n\nWe recommend rotating your keys at least every 90 days to reduce\nthe risk posed by leaked keys.\n\nIf you believe that a service account key has been compromised, we recommend\nthat you rotate it immediately.\n\nKey rotation process\n--------------------\n\nTo rotate service account keys, do the following:\n\n1. Identify the service account keys that need to be rotated.\n2. Create new keys for the same service accounts.\n3. Replace the existing keys with the new keys across all applications.\n4. Disable the replaced keys and monitor the applications to confirm that they work as expected.\n5. Delete the service account keys that were replaced.\n\nYou can complete these steps by using a centralized secret management service,\nor by using a custom notification system.\n\n### Centralized secret management service\n\nMany centralized secret management services, like [HashiCorp Vault](https://www.vaultproject.io), provide automatic secret rotation. You can use these services\nto store and rotate your service account keys.\nWe don't recommend using Google Cloud's Secret Manager to store and rotate service account keys. This is because, to access Secret Manager secrets, your application needs an identity that Google Cloud can recognize. If your application already has an identity that Google Cloud can recognize, then your application can use that identity to authenticate to Google Cloud instead of using a service account key.\n\n\u003cbr /\u003e\n\nThe same concept applies for other cloud-based secret management services, like\nAzure KeyVault and AWS Secret Manager. If an application already has an identity\nthat these cloud providers can recognize, your application would be able to use\nthat identity to authenticate to Google Cloud instead of using a service\naccount key.\n\n### Custom notification system\n\nAnother approach to service account key rotation is to create a system that\nsends notifications when keys need to be rotated. For example, you could create\na system that sends alerts when it detects keys that were created more than\n90 days ago.\n\nFirst, you need to identify the keys that need to be rotated. To identify these\nkeys, we recommend using Cloud Asset Inventory to search for all service account\nkeys that were created before a certain time.\n\nFor example, the following command lists all service account keys that were\ncreated before `2023-03-10 00:00:00 UTC` in the organization with the ID\n`123456789012`: \n\n```\ngcloud asset search-all-resources \\\n --scope=\"organizations/123456789012\" \\\n --query=\"createTime \u003c 2023-03-10\" \\\n --asset-types=\"iam.googleapis.com/ServiceAccountKey\" \\\n --order-by=\"createTime\"\n```\n\nTo learn more about searching resources in Cloud Asset Inventory, see [Searching\nresources](/asset-inventory/docs/searching-resources).\nAfter identifying the keys that need to be rotated, you can send out\nnotifications to the appropriate teams.\n\nWhen someone is notified to rotate a key, they should do the following:\n\n1. [Create a new key](/iam/docs/keys-create-delete#creating) for the same service account.\n2. Replace the existing key with the new key across all applications.\n3. [Disable the key](/iam/docs/keys-disable-enable#disabling) that they replaced and monitor the applications to confirm that they work as expected.\n4. After they confirm that the applications are working as expected, [delete the\n replaced key](/iam/docs/keys-create-delete#deleting).\n\nExpiring service account keys\n-----------------------------\n\nWe don't recommend using expiring service account keys for key rotation. This is\nbecause expiring keys can cause outages if they aren't rotated properly. For\nmore information about the use cases for expiring service account keys, see\n[expiry times for user-managed keys](/iam/docs/service-account-creds#key-expiry).\n\nWhat's next\n-----------\n\n- Use Cloud Asset Inventory to [search for resources](/asset-inventory/docs/searching-resources), including service account keys, by creation time.\n- [Create](/iam/docs/keys-create-delete#creating), [disable](/iam/docs/keys-disable-enable#disabling), and [delete](/iam/docs/keys-create-delete#deleting) service account keys."]]