Secure Coding & OWASP Top 10 for Backend · レッスン

シークレット管理と安全な設定保存

設定ミスによる認証情報の漏えいを防ぐため、シークレットを安全に保存、ローテーション、アクセスする方法を学びます。環境変数、Vault、シークレットスキャンのパターンも扱います。

レッスン 4/413 ステップ

「シークレット管理と安全な設定保存」はCoddyKit上の無料Secure Coding & OWASP Top 10 for Backendレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSecure Coding & OWASP Top 10 for Backend学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Secure Coding & OWASP Top 10 for Backendコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

The Secrets Problem

Hardcoded passwords, API keys, and tokens are one of the most common security misconfigurations. Once a secret lands in source control, it must be considered compromised forever.

This lesson covers how to keep secrets out of code and store configuration safely.

Never Commit Secrets

The first rule: secrets never live in your repository. Use a .gitignore to exclude files like .env, and prefer injected configuration over baked-in values.

  • No passwords in source code
  • No keys in config files committed to git
  • No secrets in container images

Environment Variables

Environment variables are the simplest way to inject secrets at runtime. The application reads them from the process environment instead of a tracked file.

import os

db_password = os.environ.get('DB_PASSWORD')
if not db_password:
    raise RuntimeError('DB_PASSWORD is not set')

print('Loaded secret of length', len(db_password))

Limits of Env Vars

Env vars are better than hardcoding but have weaknesses: they can leak through crash dumps, child processes, debug endpoints, and logging of the whole environment.

For high-value secrets, prefer a dedicated secrets manager.

Secret Vaults

Tools like HashiCorp Vault, AWS Secrets Manager, and Azure Key Vault store secrets encrypted at rest, control access via fine-grained policies, and provide an audit trail of every read.

  • Centralized storage with access control
  • Automatic encryption at rest and in transit
  • Audit logs of who fetched what and when

Fetching from a Vault

Applications request secrets at startup using a short-lived identity token instead of a static key.

def get_secret(client, path):
    # client is authenticated via a short-lived role token
    response = client.read(path)
    if response is None:
        raise RuntimeError('Secret not found: ' + path)
    return response['data']['value']

# usage: get_secret(vault, 'secret/data/db')

Secret Rotation

Rotation means changing secrets regularly and immediately after suspected exposure. Short-lived, automatically rotated credentials limit the window an attacker can use a stolen key.

Design apps to reload credentials without a full restart so rotation is painless.

Least Privilege for Secrets

Each service should only be able to read the secrets it needs. Scope vault policies and cloud IAM roles tightly so a compromised service cannot harvest unrelated credentials.

Detecting Leaked Secrets

Use secret-scanning tools in CI to block commits that contain credential patterns. Catching a leak before it merges is far cheaper than rotating after exposure.

import re

patterns = [r'AKIA[0-9A-Z]{16}', r'(?i)password\s*=\s*[\'\"]\S+']
line = 'aws_key = AKIAIOSFODNN7EXAMPLE'

for p in patterns:
    if re.search(p, line):
        print('Possible secret detected!')

Encrypting Config at Rest

When config must be stored as files, encrypt them. Tools like SOPS or sealed-secrets let you commit encrypted values safely, decrypting only at deploy time with a managed key.

  • Encrypt before storing
  • Keep the decryption key in a managed KMS
  • Never store the key alongside the data

Auditing Access

Log and review every secret access. Anomalies, like a service reading a secret it never used before, are strong indicators of compromise and feed your monitoring pipeline.

Quick Check

Test your understanding of secrets management.

Recap

You learned to keep secrets out of code, inject them via env vars or a vault, apply rotation and least privilege, scan for leaks in CI, and audit every access. Proper secrets management closes one of the biggest misconfiguration gaps in backend systems.

無料で開始

AI チューターと学ぶ Secure Coding & OWASP Top 10 for Backend — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
12
レッスン
48

よくある質問

「シークレット管理と安全な設定保存」レッスンは無料ですか?

はい。「シークレット管理と安全な設定保存」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Secure Coding & OWASP Top 10 for Backendコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Secure Coding & OWASP Top 10 for Backendコースには全4レッスンが含まれています。

「シークレット管理と安全な設定保存」で何を学びますか?

設定ミスによる認証情報の漏えいを防ぐため、シークレットを安全に保存、ローテーション、アクセスする方法を学びます。環境変数、Vault、シークレットスキャンのパターンも扱います。 ブラウザで直接実行するハンズオンコードでSecure Coding & OWASP Top 10 for Backendを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Secure Coding & OWASP Top 10 for Backendを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSecure Coding & OWASP Top 10 for Backendは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「シークレット管理と安全な設定保存」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSecure Coding & OWASP Top 10 for Backendレッスンでコードを書いて実行できますか?

はい。すべてのSecure Coding & OWASP Top 10 for Backendレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. サーバーとアプリケーション設定のハードニング
  2. 依存関係とライブラリの安全な管理
  3. パッチ管理とソフトウェア更新
  4. シークレット管理と安全な設定保存
← Secure Coding & OWASP Top 10 for Backendに戻る