rohitg00/ai-engineering-from-scratch · error · RuntimeError

required secret is not configured

Error message

required secret {name} is not configured

What it means

Raised in EnvironmentSecrets.require when a declared required secret has no value in the environment (unset or empty). This is a fail-fast startup guard: the application refuses to run without its required credentials rather than starting up and failing later at first use.

Solutions

  1. Set the missing variable in the deployment environment (export in shell, secrets manager, or .env loaded before startup)
  2. Prune the variable from the required list if it is no longer needed
  3. Provide a test fixture that sets all required secrets so unit tests do not depend on the host environment
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at certifications/claude/lessons/13-application-security-and-secrets/code/main.py:110 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/3cd2c684040164bd. Report an issue: GitHub.

Appendix: source

Thrown at certifications/claude/lessons/13-application-security-and-secrets/code/main.py:110

        if parsed.scheme != "https" or parsed.hostname not in self.allowed_hosts:
            return Decision(False, "network destination is not allowed", ("network_allowlist",))
        return Decision(True, "read-only request to allowed host", ("network_allowlist", "response_size_limit"))


def redact(value: str) -> str:
    """Remove common secret assignments before a string reaches logs."""
    return SECRET_VALUE_PATTERN.sub(lambda match: f"{match.group(1)}=[REDACTED]", value)


class EnvironmentSecrets:
    """Return secret values to trusted code without logging or serialization helpers."""

    def require(self, name: str) -> str:
        if not re.fullmatch(r"[A-Z][A-Z0-9_]+", name):
            raise ValueError("secret name must be an uppercase environment variable")
        value = os.environ.get(name)
        if not value:
            raise RuntimeError(f"required secret {name} is not configured")
        return value


def demo() -> dict[str, Any]:
    gate = PolicyGate(["/workspace/project"], ["api.example.test"])
    actions = [
        Action("read_file", {"path": "/workspace/project/README.md"}),
        Action("read_file", {"path": "/workspace/project/.env"}),
        Action("run_command", {"command": "rm -rf /workspace/project"}, approved=True),
        Action("http_get", {"url": "https://api.example.test/status"}),
    ]
    return {"decisions": [decision.__dict__ for decision in map(gate.evaluate, actions)]}


if __name__ == "__main__":
    print(json.dumps(demo(), indent=2))

View on GitHub (pinned to 39ea8a1c6d)