|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import builtins |
| 4 | +from typing import TYPE_CHECKING, Literal |
| 5 | + |
| 6 | +from ....errors import MountConfigError |
| 7 | +from ..base import DockerVolumeMountStrategy |
| 8 | +from ..patterns import MountPattern, MountPatternConfig, RcloneMountPattern |
| 9 | +from .base import _ConfiguredMount |
| 10 | + |
| 11 | +if TYPE_CHECKING: |
| 12 | + from ....session.base_sandbox_session import BaseSandboxSession |
| 13 | + |
| 14 | + |
| 15 | +class BoxMount(_ConfiguredMount): |
| 16 | + """Mount a Box folder using rclone. |
| 17 | +
|
| 18 | + See Box's JWT setup guide (https://developer.box.com/guides/authentication/jwt/jwt-setup/) |
| 19 | + and rclone's Box guide (https://rclone.org/box/). Non-interactive mounts require |
| 20 | + a minted `token` or `access_token`. |
| 21 | + """ |
| 22 | + |
| 23 | + type: Literal["box_mount"] = "box_mount" |
| 24 | + path: str | None = None |
| 25 | + client_id: str | None = None |
| 26 | + client_secret: str | None = None |
| 27 | + access_token: str | None = None |
| 28 | + token: str | None = None |
| 29 | + box_config_file: str | None = None |
| 30 | + config_credentials: str | None = None |
| 31 | + box_sub_type: Literal["user", "enterprise"] = "user" |
| 32 | + root_folder_id: str | None = None |
| 33 | + impersonate: str | None = None |
| 34 | + owned_by: str | None = None |
| 35 | + |
| 36 | + def supported_in_container_patterns(self) -> tuple[builtins.type[MountPattern], ...]: |
| 37 | + return (RcloneMountPattern,) |
| 38 | + |
| 39 | + def supported_docker_volume_drivers(self) -> frozenset[str]: |
| 40 | + return frozenset({"rclone"}) |
| 41 | + |
| 42 | + def build_docker_volume_driver_config( |
| 43 | + self, |
| 44 | + strategy: DockerVolumeMountStrategy, |
| 45 | + ) -> tuple[str, dict[str, str], bool]: |
| 46 | + options: dict[str, str] = {"type": "box", "path": self._remote_path()} |
| 47 | + if self.client_id is not None: |
| 48 | + options["box-client-id"] = self.client_id |
| 49 | + if self.client_secret is not None: |
| 50 | + options["box-client-secret"] = self.client_secret |
| 51 | + if self.access_token is not None: |
| 52 | + options["box-access-token"] = self.access_token |
| 53 | + if self.token is not None: |
| 54 | + options["box-token"] = self.token |
| 55 | + if self.box_config_file is not None: |
| 56 | + options["box-box-config-file"] = self.box_config_file |
| 57 | + if self.config_credentials is not None: |
| 58 | + options["box-config-credentials"] = self.config_credentials |
| 59 | + if self.box_sub_type != "user": |
| 60 | + options["box-box-sub-type"] = self.box_sub_type |
| 61 | + if self.root_folder_id is not None: |
| 62 | + options["box-root-folder-id"] = self.root_folder_id |
| 63 | + if self.impersonate is not None: |
| 64 | + options["box-impersonate"] = self.impersonate |
| 65 | + if self.owned_by is not None: |
| 66 | + options["box-owned-by"] = self.owned_by |
| 67 | + return strategy.driver, options | strategy.driver_options, self.read_only |
| 68 | + |
| 69 | + async def build_in_container_mount_config( |
| 70 | + self, |
| 71 | + session: BaseSandboxSession, |
| 72 | + pattern: MountPattern, |
| 73 | + *, |
| 74 | + include_config_text: bool, |
| 75 | + ) -> MountPatternConfig: |
| 76 | + if isinstance(pattern, RcloneMountPattern): |
| 77 | + return await self._build_rclone_config( |
| 78 | + session=session, |
| 79 | + pattern=pattern, |
| 80 | + remote_kind="box", |
| 81 | + remote_path=self._remote_path(), |
| 82 | + required_lines=self._rclone_required_lines( |
| 83 | + pattern.resolve_remote_name( |
| 84 | + session_id=self._require_session_id_hex(session, self.type), |
| 85 | + remote_kind="box", |
| 86 | + mount_type=self.type, |
| 87 | + ) |
| 88 | + ), |
| 89 | + include_config_text=include_config_text, |
| 90 | + ) |
| 91 | + raise MountConfigError( |
| 92 | + message="invalid mount_pattern type", |
| 93 | + context={"type": self.type}, |
| 94 | + ) |
| 95 | + |
| 96 | + def _remote_path(self) -> str: |
| 97 | + if self.path is None: |
| 98 | + return "" |
| 99 | + return self.path.lstrip("/") |
| 100 | + |
| 101 | + def _rclone_required_lines(self, remote_name: str) -> list[str]: |
| 102 | + lines = [ |
| 103 | + f"[{remote_name}]", |
| 104 | + "type = box", |
| 105 | + ] |
| 106 | + if self.client_id is not None: |
| 107 | + lines.append(f"client_id = {self.client_id}") |
| 108 | + if self.client_secret is not None: |
| 109 | + lines.append(f"client_secret = {self.client_secret}") |
| 110 | + if self.access_token is not None: |
| 111 | + lines.append(f"access_token = {self.access_token}") |
| 112 | + if self.token is not None: |
| 113 | + lines.append(f"token = {self.token}") |
| 114 | + if self.box_config_file is not None: |
| 115 | + lines.append(f"box_config_file = {self.box_config_file}") |
| 116 | + if self.config_credentials is not None: |
| 117 | + lines.append(f"config_credentials = {self.config_credentials}") |
| 118 | + if self.box_sub_type != "user": |
| 119 | + lines.append(f"box_sub_type = {self.box_sub_type}") |
| 120 | + if self.root_folder_id is not None: |
| 121 | + lines.append(f"root_folder_id = {self.root_folder_id}") |
| 122 | + if self.impersonate is not None: |
| 123 | + lines.append(f"impersonate = {self.impersonate}") |
| 124 | + if self.owned_by is not None: |
| 125 | + lines.append(f"owned_by = {self.owned_by}") |
| 126 | + return lines |
0 commit comments