

# SEC02-BP03 安全地存放和使用機密
<a name="sec_identities_secrets"></a>

 工作負載需要能夠自動向資料庫、資源和第三方資源證明其身分。這需使用私密存取憑證來完成，例如 API 存取金鑰、密碼和 OAuth 權杖。使用專用服務來儲存、管理和輪換這些憑證有助於降低該些憑證遭到入侵的可能性。 

**預期成果：**實施一種安全管理應用程式憑證的機制並達到以下目標： 
+  識別工作負載需要何種機密。
+  盡可能以短期憑證取代長期憑證，來減少所需的長期憑證數目。
+  建立安全的存放區並自動輪換其餘的長期憑證。 
+  稽核對存在於工作負載中的機密的存取。
+  持續監控以確認原始程式碼在開發過程中沒有內嵌機密。
+  降低憑證遭意外洩露的可能性。

**常見的反模式：**
+  沒有輪換憑證。
+  將長期憑證存放在原始程式碼或設定檔中。
+  未加密儲存靜態憑證。

 **建立此最佳實務的優勢：**
+  已加密儲存靜態和傳輸中的機密。
+  透過 API 限制憑證的存取 (把這想成是*憑證自動販賣機*)。
+  稽核並記錄對憑證的存取 (包括讀寫)。
+  區隔顧慮：由不同的元件執行憑證輪換，而該元件可與其餘的架構分離。
+  自動將機密隨需散發到軟體元件並集中進行輪換。
+  可以精細的方式控制對憑證的存取。

 **未建立此最佳實務時的風險暴露等級**：高 

## 實作指引
<a name="implementation-guidance"></a>

 以往，憑證用於向資料庫進行驗證，而第三方 API、權杖和其他機密可能內嵌在原始程式碼或環境檔案中。AWS 提供數種機制以安全存放這些憑證，自動輪換並稽核它們的使用情況。 

 著手機密管理的最佳方法是遵循移除、取代和輪換的指引。最安全的憑證是您不用存放、管理或處理的憑證。有些憑證對於工作負載的運作不再是必要的，故能夠安全移除。 

 對於工作負載適當運作仍舊是必要的憑證，可能有機會以臨時或短期憑證取代長期憑證。例如，與其對 AWS 私密存取金鑰進行硬式編碼，考慮使用 IAM 角色以臨時憑證取代長期憑證。 

 部分長期存留的機密可能無法移除或取代。可將這些機密存放在 [AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html) 之類的服務中，進行集中存放、管理和定期輪換。 

 對工作負載的原始程式碼和設定檔的稽核，可能顯現多種類型的憑證。下表概述處理常見憑證類型的策略： 


|  Credential type  |  Description  |  Suggested strategy  | 
| --- | --- | --- | 
|  IAM access keys  |  AWS IAM access and secret keys used to assume IAM roles inside of a workload  |  Replace: Use [IAM 角色](https://docs.aws.amazon.com//latest/UserGuide/id_roles_common-scenarios.html) assigned to the compute instances (such as [Amazon EC2](https://docs.aws.amazon.com//latest/UserGuide/id_roles_use_switch-role-ec2.html) or [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html)) instead. For interoperability with third parties that require access to resources in your AWS 帳戶, ask if they support [AWS 跨帳戶存取權](https://docs.aws.amazon.com//latest/UserGuide/id_roles_common-scenarios_third-party.html). For mobile apps, consider using temporary credentials through [Amazon Cognito 身分集區 (聯合身分)](https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-identity.html). For workloads running outside of AWS, consider [IAM Roles Anywhere](https://docs.aws.amazon.com/rolesanywhere/latest/userguide/introduction.html) or [AWS Systems Manager 混合式啟用](https://docs.aws.amazon.com/systems-manager/latest/userguide/activations.html).  | 
|  SSH keys  |  Secure Shell private keys used to log into Linux EC2 instances, manually or as part of an automated process  |  Replace: Use [AWS Systems Manager](https://aws.amazon.com/blogs/mt/vr-beneficios-session-manager/) or [EC2 執行個體連線](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Connect-using-EC2-Instance-Connect.html) to provide programmatic and human access to EC2 instances using IAM roles.  | 
|  Application and database credentials  |  Passwords – plain text string  |  Rotate: Store credentials in [AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html) and establish automated rotation if possible.  | 
|  Amazon RDS and Aurora Admin Database credentials  |  Passwords – plain text string  |  Replace: Use the [Secrets Manager 與 Amazon RDS 整合](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/rds-secrets-manager.html) or [Amazon Aurora](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/rds-secrets-manager.html). In addition, some RDS database types can use IAM roles instead of passwords for some use cases (for more detail, see [IAM 資料庫身分驗證](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.DBAuth.html)).  | 
|  OAuth tokens  |  Secret tokens – plain text string  |  Rotate: Store tokens in [AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html) and configure automated rotation.  | 
|  API tokens and keys  |  Secret tokens – plain text string  |  Rotate: Store in [AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html) and establish automated rotation if possible.  | 

 常見的反模式是將 IAM 存取金鑰內嵌在原始程式碼、設定檔或行動應用程式內。當需要 IAM 存取金鑰與 AWS 服務通訊時，請使用[臨時 (短期) 安全憑證](https://docs.aws.amazon.com//latest/UserGuide/id_credentials_temp.html)。這些短期憑證可以透過 [IAM 角色 (用於 EC2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html) 執行個體)、[執行角色](https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html) (用於 Lambda 函數)、[Cognito IAM 角色](https://docs.aws.amazon.com/cognito/latest/developerguide/iam-roles.html) (用於行動使用者存取)，以及 [IoT Core 政策](https://docs.aws.amazon.com/iot/latest/developerguide/iot-policies.html) (用於 IoT 裝置) 提供。當與第三方互動時，偏好[委派 IAM 角色的存取權](https://docs.aws.amazon.com//latest/UserGuide/id_roles_common-scenarios_third-party.html)，包含對帳戶資源的必要存取權，而不是設定 IAM 使用者並將其的私密存取金鑰傳送給該第三方。 

 在很多情況下，工作負載需要儲存機密才能與其他服務和資源相互操作。[AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/intro.html) 是專為安全管理這些憑證所打造的，可儲存和輪換 API 權杖、密碼和其他憑證。 

 AWS Secrets Manager 提供五項重要功能以確保敏感憑證的安全存放和處理：[靜態加密](https://docs.aws.amazon.com/secretsmanager/latest/userguide/security-encryption.html)、[傳輸中加密](https://docs.aws.amazon.com/secretsmanager/latest/userguide/data-protection.html)、 [全面性稽核](https://docs.aws.amazon.com/secretsmanager/latest/userguide/monitoring.html)、[精細存取控制](https://docs.aws.amazon.com/secretsmanager/latest/userguide/auth-and-access.html)，以及[可擴充的憑證輪換](https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotating-secrets.html)。來自 AWS 合作夥伴的其他機密管理服務，或本機開發並提供類似功能和保證的解決方案也可接受。 

### 實作步驟
<a name="implementation-steps"></a>

1.  使用 [Amazon CodeGuru](https://aws.amazon.com/codeguru/features/) 等自動工具識別包含硬式編碼憑證的程式碼路徑。 
   +  使用 Amazon CodeGuru 掃描您的程式碼儲存庫。審閱完成後，在 CodeGuru 中篩選 `Type=Secrets` 以尋找有問題的程式碼行。

1.  識別可移除或取代的憑證。 

   1.  識別不再需要的憑證並標示以進行移除。 

   1.  對於內嵌在原始程式碼中的 AWS 機密金鑰，請使用與必要資源相關聯的 IAM 角色加以取代。如果您部分的工作負載位於 AWS 之外但需要 IAM 憑證存取 AWS 資源，請考慮 [IAM Roles Anywhere](https://aws.amazon.com/blogs/security/extend-aws-iam-roles-to-workloads-outside-of-aws-with-iam-roles-anywhere/) 或 [AWS Systems Manager 混合式啟用](https://docs.aws.amazon.com/systems-manager/latest/userguide/activations.html)。 

1.  對於其他第三方長期存留且需要使用輪換策略的機密，將 Secrets Manager 整合至程式碼中以在執行時間擷取第三方機密。 

   1.  CodeGuru 主控台可以使用已探索的憑證自動[在 Secrets Manager 中建立機密](https://aws.amazon.com/blogs/aws/codeguru-reviewer-secrets-detector-identify-hardcoded-secrets/)。 

   1.  將 Secrets Manager 的機密擷取整合至您的應用程式程式碼中。 
      +  無伺服器 Lambda 函數可以使用語言中立的 [Lambda 延伸](https://docs.aws.amazon.com/secretsmanager/latest/userguide/retrieving-secrets_lambda.html)。 
      +  對於 EC2 執行個體或容器，AWS 提供範例[用戶端程式碼，可以數種熱門的程式設計語言從 Secrets Manager 擷取機密](https://docs.aws.amazon.com/secretsmanager/latest/userguide/retrieving-secrets.html)。 

1.  定期審閱您的程式碼基底並重新掃描，以確認程式碼中未加入新的機密。 
   +  考慮使用 [git-secrets](https://github.com/awslabs/git-secrets) 之類的工具以防將新機密認可到您的原始程式碼儲存庫。 

1.  [監控 Secrets Manager 活動](https://docs.aws.amazon.com/secretsmanager/latest/userguide/monitoring.html)以尋找非預期使用、不當私密存取或嘗試刪除機密的跡象。 

1.  減少對憑證的人員接觸。將讀寫和修改憑證的存取權限於專門用於此用途的 IAM 角色，並且只將擔任該角色的存取權提供給一小組可操作的使用者子集。 

## 資源
<a name="resources"></a>

 **相關的最佳實務：** 
+ [SEC02-BP02 使用臨時憑證](sec_identities_unique.md)
+ [SEC02-BP05 定期稽核和輪換憑證](sec_identities_audit.md)

 **相關文件：** 
+  [AWS Secrets Manager 入門](https://docs.aws.amazon.com/secretsmanager/latest/userguide/getting-started.html) 
+  [身分提供者與聯合](https://docs.aws.amazon.com//latest/UserGuide/id_roles_providers.html) 
+  [Amazon CodeGuru 推出機密偵測器](https://aws.amazon.com/blogs/aws/codeguru-reviewer-secrets-detector-identify-hardcoded-secrets/) 
+  [AWS Secrets Manager 如何使用 AWS Key Management Service](https://docs.aws.amazon.com/kms/latest/developerguide/services-secrets-manager.html) 
+  [Secrets Manager 中的機密加密和解密](https://docs.aws.amazon.com/secretsmanager/latest/userguide/security-encryption.html) 
+  [Secrets Manager 部落格文章](https://aws.amazon.com/blogs/security/tag/aws-secrets-manager/) 
+  [Amazon RDS 宣布與 AWS Secrets Manager 整合](https://aws.amazon.com/about-aws/whats-new/2022/12/amazon-rds-integration-aws-secrets-manager/) 

 **相關影片：** 
+  [大規模管理、擷取和輪換密碼的最佳實務](https://youtu.be/qoxxRlwJKZ4) 
+  [使用 Amazon CodeGuru 機密偵測器](https://www.youtube.com/watch?v=ryK3PN--oJs) 
+  [使用 AWS Secrets Manager 保護混合式工作負載的機密](https://www.youtube.com/watch?v=k1YWhogGVF8) 

 **相關研討會：** 
+  [在 AWS Secrets Manager 中儲存、擷取和管理敏感憑證](https://catalog.us-east-1.prod.workshops.aws/workshops/497b4908-169f-4e6f-b80d-ef10be3038d3/en-US) 
+  [AWS Systems Manager 混合式啟用](https://mng.workshop.aws/ssm/capability_hands-on_labs/hybridactivations.html) 