Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

8.7 KB
Raw Blame
"""CrewAI platform settings — shared by crewai and crewai-cli."""

from __future__ import annotations

import json
from logging import getLogger
import os
from pathlib import Path
import tempfile
from typing import Any

from pydantic import BaseModel, Field

from crewai_core.constants import (
    CREWAI_ENTERPRISE_DEFAULT_OAUTH2_AUDIENCE,
    CREWAI_ENTERPRISE_DEFAULT_OAUTH2_CLIENT_ID,
    CREWAI_ENTERPRISE_DEFAULT_OAUTH2_DOMAIN,
    CREWAI_ENTERPRISE_DEFAULT_OAUTH2_PROVIDER,
    DEFAULT_CREWAI_ENTERPRISE_URL,
)
from crewai_core.token_manager import TokenManager


logger = getLogger(__name__)

DEFAULT_CONFIG_PATH = Path.home() / ".config" / "crewai" / "settings.json"


def _ensure_dir_mode(directory: Path) -> None:
    """Tighten a dedicated config directory to 0o700.

    Skips directories shared with other users or content (the system temp dir
    and the current working directory), which are used as best-effort fallbacks
    by :func:`get_writable_config_path` and must not be globally chmod'd. Secret
    files written there are still protected by their own 0o600 mode.
    """
    try:
        shared = {Path(tempfile.gettempdir()).resolve(), Path.cwd().resolve()}
        if directory.resolve() in shared:
            return
        directory.chmod(0o700)
    except OSError as e:
        logger.debug(
            "Could not enforce 0o700 on config directory %s (best-effort): %s",
            directory,
            e,
        )


def _write_secure_json(path: Path, data: dict[str, Any]) -> None:
    """Atomically write ``data`` as JSON to ``path`` with owner-only (0o600) mode."""
    fd, tmp = tempfile.mkstemp(dir=path.parent, prefix=f".{path.name}.")
    fd_open = True
    try:
        with os.fdopen(fd, "w") as f:
            fd_open = False
            json.dump(data, f, indent=4)
        os.chmod(tmp, 0o600)
        os.replace(tmp, path)
    except BaseException:
        if fd_open:
            try:
                os.close(fd)
            except OSError as close_error:
                logger.debug(
                    "Could not close temporary settings file descriptor for %s "
                    "(best-effort cleanup): %s",
                    tmp,
                    close_error,
                )
        if os.path.exists(tmp):
            os.unlink(tmp)
        raise


def get_writable_config_path() -> Path | None:
    """Find a writable location for the config file with fallback options.

    Tries in order:
    1. Default: ``~/.config/crewai/settings.json``
    2. Temp directory: ``/tmp/crewai_settings.json`` (or OS equivalent)
    3. Current directory: ``./crewai_settings.json``
    4. In-memory only (returns ``None``)
    """
    fallback_paths = [
        DEFAULT_CONFIG_PATH,
        Path(tempfile.gettempdir()) / "crewai_settings.json",
        Path.cwd() / "crewai_settings.json",
    ]

    for config_path in fallback_paths:
        try:
            config_path.parent.mkdir(parents=True, exist_ok=True)
            _ensure_dir_mode(config_path.parent)
            test_file: Path | None = None
            fd: int | None = None
            try:
                fd, test_file_path = tempfile.mkstemp(
                    dir=config_path.parent,
                    prefix=".crewai_write_test.",
                )
                test_file = Path(test_file_path)
                os.close(fd)
                fd = None
                test_file.unlink()
                logger.info(f"Using config path: {config_path}")
                return config_path
            except Exception:  # noqa: S112
                continue
            finally:
                if fd is not None:
                    os.close(fd)
                if test_file is not None:
                    test_file.unlink(missing_ok=True)

        except Exception:  # noqa: S112
            continue

    return None


USER_SETTINGS_KEYS = [
    "tool_repository_username",
    "tool_repository_password",
    "org_name",
    "org_uuid",
]

CLI_SETTINGS_KEYS = [
    "enterprise_base_url",
    "oauth2_provider",
    "oauth2_audience",
    "oauth2_client_id",
    "oauth2_domain",
    "oauth2_extra",
]

DEFAULT_CLI_SETTINGS: dict[str, Any] = {
    "enterprise_base_url": DEFAULT_CREWAI_ENTERPRISE_URL,
    "oauth2_provider": CREWAI_ENTERPRISE_DEFAULT_OAUTH2_PROVIDER,
    "oauth2_audience": CREWAI_ENTERPRISE_DEFAULT_OAUTH2_AUDIENCE,
    "oauth2_client_id": CREWAI_ENTERPRISE_DEFAULT_OAUTH2_CLIENT_ID,
    "oauth2_domain": CREWAI_ENTERPRISE_DEFAULT_OAUTH2_DOMAIN,
    "oauth2_extra": {},
}

READONLY_SETTINGS_KEYS = [
    "org_name",
    "org_uuid",
]

HIDDEN_SETTINGS_KEYS = [
    "config_path",
    "tool_repository_username",
    "tool_repository_password",
]


class Settings(BaseModel):
    """CrewAI platform settings persisted to ``~/.config/crewai/settings.json``."""

    enterprise_base_url: str | None = Field(
        default=DEFAULT_CLI_SETTINGS["enterprise_base_url"],
        description="Base URL of the CrewAI AMP instance",
    )
    tool_repository_username: str | None = Field(
        None, description="Username for interacting with the Tool Repository"
    )
    tool_repository_password: str | None = Field(
        None, description="Password for interacting with the Tool Repository"
    )
    org_name: str | None = Field(
        None, description="Name of the currently active organization"
    )
    org_uuid: str | None = Field(
        None, description="UUID of the currently active organization"
    )
    config_path: Path = Field(default=DEFAULT_CONFIG_PATH, frozen=True, exclude=True)

    oauth2_provider: str = Field(
        description="OAuth2 provider used for authentication (e.g., workos, okta, auth0).",
        default=DEFAULT_CLI_SETTINGS["oauth2_provider"],
    )

    oauth2_audience: str | None = Field(
        description="OAuth2 audience value, typically used to identify the target API or resource.",
        default=DEFAULT_CLI_SETTINGS["oauth2_audience"],
    )

    oauth2_client_id: str = Field(
        default=DEFAULT_CLI_SETTINGS["oauth2_client_id"],
        description="OAuth2 client ID issued by the provider, used during authentication requests.",
    )

    oauth2_domain: str = Field(
        description="OAuth2 provider's domain (e.g., your-org.auth0.com) used for issuing tokens.",
        default=DEFAULT_CLI_SETTINGS["oauth2_domain"],
    )

    oauth2_extra: dict[str, Any] = Field(
        description="Extra configuration for the OAuth2 provider.",
        default={},
    )

    def __init__(self, config_path: Path | None = None, **data: dict[str, Any]) -> None:
        """Load Settings from config path with fallback support."""
        if config_path is None:
            config_path = get_writable_config_path()

        if config_path is None:
            merged_data = {**data}
            super().__init__(config_path=Path("/dev/null"), **merged_data)
            return

        try:
            config_path.parent.mkdir(parents=True, exist_ok=True)
            _ensure_dir_mode(config_path.parent)
        except Exception:
            merged_data = {**data}
            super().__init__(config_path=Path("/dev/null"), **merged_data)
            return

        file_data = {}
        if config_path.is_file():
            try:
                with config_path.open("r") as f:
                    file_data = json.load(f)
            except Exception:
                file_data = {}

        merged_data = {**file_data, **data}
        super().__init__(config_path=config_path, **merged_data)

    def clear_user_settings(self) -> None:
        """Clear all user settings."""
        self._reset_user_settings()
        self.dump()

    def reset(self) -> None:
        """Reset all settings to default values."""
        self._reset_user_settings()
        self._reset_cli_settings()
        self._clear_auth_tokens()
        self.dump()

    def dump(self) -> None:
        """Save current settings to settings.json."""
        if str(self.config_path) == "/dev/null":
            return

        try:
            if self.config_path.is_file():
                with self.config_path.open("r") as f:
                    existing_data = json.load(f)
            else:
                existing_data = {}

            updated_data = {**existing_data, **self.model_dump(exclude_unset=True)}
            _write_secure_json(self.config_path, updated_data)

        except Exception:  # noqa: S110
            pass

    def _reset_user_settings(self) -> None:
        """Reset all user settings to default values."""
        for key in USER_SETTINGS_KEYS:
            setattr(self, key, None)

    def _reset_cli_settings(self) -> None:
        """Reset all CLI settings to default values."""
        for key in CLI_SETTINGS_KEYS:
            setattr(self, key, DEFAULT_CLI_SETTINGS.get(key))

    def _clear_auth_tokens(self) -> None:
        """Clear all authentication tokens."""
        TokenManager().clear_tokens()