機密情報をプレーンテキストで保存しない
Contents
Never store sensitive information (e.g., API keys) in plain text within the project. Fetch them at runtime from environment variables or external services such as SSM Parameter Store. Encrypted storage is acceptable, but do not bundle the decryption key in the project. (機密情報(APIキーなど)をプロジェクト内にプレーンテキストで保存しない。実行時に環境変数やSSM Parameter Storeなどの外部サービスから取得する。暗号化ストレージは許容されるが、復号化キーをプロジェクトに含めてはいけない)
解説
APIキーやパスワードなどの機密情報をソースコードに直接記述することは、重大なセキュリティリスクです。コードリポジトリが漏洩した場合、即座に悪用される可能性があります。環境変数や外部の秘密管理サービスを使用することで、機密情報とコードを分離し、アクセス制御が可能になります。暗号化する場合も、復号化キーを同じリポジトリに含めては意味がありません。
具体例
// 悪い例
const APIKey = "sk_live_1234567890abcdef" // ハードコード
func callExternalAPI() {
client := NewAPIClient(APIKey)
client.Request()
}
// 良い例
func callExternalAPI() {
apiKey := os.Getenv("API_KEY") // 環境変数から取得
if apiKey == "" {
log.Fatal("API_KEY not set")
}
client := NewAPIClient(apiKey)
client.Request()
}
// さらに良い例(AWS SSM使用)
func getAPIKey() (string, error) {
sess := session.Must(session.NewSession())
svc := ssm.New(sess)
param, err := svc.GetParameter(&ssm.GetParameterInput{
Name: aws.String("/myapp/api_key"),
WithDecryption: aws.Bool(true),
})
if err != nil {
return "", err
}
return *param.Parameter.Value, nil
}
参考リンク
機密情報をプレーンテキストで保存しない https://www.tricrow.com/core/coding-standard/no-plain-text-secrets.html

