|
| 1 | +from typing import Any, Dict, Literal, Optional, Union |
| 2 | + |
| 3 | +from pydantic import AnyUrl, BaseModel, Extra, Field, HttpUrl, validator |
| 4 | +from pydantic.types import NonNegativeInt |
| 5 | +from simcore_postgres_database.models.clusters import ClusterType |
| 6 | + |
| 7 | +from .users import GroupID |
| 8 | + |
| 9 | + |
| 10 | +class ClusterAccessRights(BaseModel): |
| 11 | + read: bool = Field(..., description="allows to run pipelines on that cluster") |
| 12 | + write: bool = Field(..., description="allows to modify the cluster") |
| 13 | + delete: bool = Field(..., description="allows to delete a cluster") |
| 14 | + |
| 15 | + class Config: |
| 16 | + extra = Extra.forbid |
| 17 | + |
| 18 | + |
| 19 | +CLUSTER_ADMIN_RIGHTS = ClusterAccessRights(read=True, write=True, delete=True) |
| 20 | +CLUSTER_MANAGER_RIGHTS = ClusterAccessRights(read=True, write=True, delete=False) |
| 21 | +CLUSTER_USER_RIGHTS = ClusterAccessRights(read=True, write=False, delete=False) |
| 22 | +CLUSTER_NO_RIGHTS = ClusterAccessRights(read=False, write=False, delete=False) |
| 23 | + |
| 24 | + |
| 25 | +class BaseAuthentication(BaseModel): |
| 26 | + type: str |
| 27 | + |
| 28 | + class Config: |
| 29 | + extra = Extra.forbid |
| 30 | + |
| 31 | + |
| 32 | +class SimpleAuthentication(BaseAuthentication): |
| 33 | + type: Literal["simple"] = "simple" |
| 34 | + username: str |
| 35 | + password: str |
| 36 | + |
| 37 | + class Config(BaseAuthentication.Config): |
| 38 | + schema_extra = { |
| 39 | + "examples": [ |
| 40 | + { |
| 41 | + "type": "simple", |
| 42 | + "username": "someuser", |
| 43 | + "password": "somepassword", |
| 44 | + }, |
| 45 | + ] |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | +class KerberosAuthentication(BaseAuthentication): |
| 50 | + type: Literal["kerberos"] = "kerberos" |
| 51 | + # NOTE: the entries here still need to be defined |
| 52 | + class Config(BaseAuthentication.Config): |
| 53 | + schema_extra = { |
| 54 | + "examples": [ |
| 55 | + { |
| 56 | + "type": "kerberos", |
| 57 | + }, |
| 58 | + ] |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | +class JupyterHubTokenAuthentication(BaseAuthentication): |
| 63 | + type: Literal["jupyterhub"] = "jupyterhub" |
| 64 | + api_token: str |
| 65 | + |
| 66 | + class Config(BaseAuthentication.Config): |
| 67 | + schema_extra = { |
| 68 | + "examples": [ |
| 69 | + {"type": "jupyterhub", "api_token": "some_jupyterhub_token"}, |
| 70 | + ] |
| 71 | + } |
| 72 | + |
| 73 | + |
| 74 | +class NoAuthentication(BaseAuthentication): |
| 75 | + type: Literal["none"] = "none" |
| 76 | + |
| 77 | + |
| 78 | +InternalClusterAuthentication = NoAuthentication |
| 79 | +ExternalClusterAuthentication = Union[ |
| 80 | + SimpleAuthentication, KerberosAuthentication, JupyterHubTokenAuthentication |
| 81 | +] |
| 82 | +ClusterAuthentication = Union[ |
| 83 | + ExternalClusterAuthentication, |
| 84 | + InternalClusterAuthentication, |
| 85 | +] |
| 86 | + |
| 87 | + |
| 88 | +class BaseCluster(BaseModel): |
| 89 | + name: str = Field(..., description="The human readable name of the cluster") |
| 90 | + description: Optional[str] = None |
| 91 | + type: ClusterType |
| 92 | + owner: GroupID |
| 93 | + thumbnail: Optional[HttpUrl] = Field( |
| 94 | + None, |
| 95 | + description="url to the image describing this cluster", |
| 96 | + examples=["https://placeimg.com/171/96/tech/grayscale/?0.jpg"], |
| 97 | + ) |
| 98 | + endpoint: AnyUrl |
| 99 | + authentication: ClusterAuthentication = Field( |
| 100 | + ..., description="Dask gateway authentication" |
| 101 | + ) |
| 102 | + access_rights: Dict[GroupID, ClusterAccessRights] = Field(default_factory=dict) |
| 103 | + |
| 104 | + class Config: |
| 105 | + extra = Extra.forbid |
| 106 | + use_enum_values = True |
| 107 | + |
| 108 | + def to_clusters_db(self, only_update: bool) -> Dict[str, Any]: |
| 109 | + db_model = self.dict( |
| 110 | + by_alias=True, |
| 111 | + exclude={"id", "access_rights"}, |
| 112 | + exclude_unset=only_update, |
| 113 | + exclude_none=only_update, |
| 114 | + ) |
| 115 | + return db_model |
| 116 | + |
| 117 | + |
| 118 | +class Cluster(BaseCluster): |
| 119 | + id: NonNegativeInt = Field(..., description="The cluster ID") |
| 120 | + |
| 121 | + class Config(BaseCluster.Config): |
| 122 | + schema_extra = { |
| 123 | + "examples": [ |
| 124 | + { |
| 125 | + "id": 432, |
| 126 | + "name": "My awesome cluster", |
| 127 | + "type": ClusterType.ON_PREMISE, |
| 128 | + "owner": 12, |
| 129 | + "endpoint": "https://registry.osparc-development.fake.dev", |
| 130 | + "authentication": { |
| 131 | + "type": "simple", |
| 132 | + "username": "someuser", |
| 133 | + "password": "somepassword", |
| 134 | + }, |
| 135 | + }, |
| 136 | + { |
| 137 | + "id": 432546, |
| 138 | + "name": "My AWS cluster", |
| 139 | + "description": "a AWS cluster administered by me", |
| 140 | + "type": ClusterType.AWS, |
| 141 | + "owner": 154, |
| 142 | + "endpoint": "https://registry.osparc-development.fake.dev", |
| 143 | + "authentication": {"type": "kerberos"}, |
| 144 | + "access_rights": { |
| 145 | + 154: CLUSTER_ADMIN_RIGHTS, |
| 146 | + 12: CLUSTER_MANAGER_RIGHTS, |
| 147 | + 7899: CLUSTER_USER_RIGHTS, |
| 148 | + }, |
| 149 | + }, |
| 150 | + { |
| 151 | + "id": 325436, |
| 152 | + "name": "My AWS cluster", |
| 153 | + "description": "a AWS cluster administered by me", |
| 154 | + "type": ClusterType.AWS, |
| 155 | + "owner": 2321, |
| 156 | + "endpoint": "https://registry.osparc-development.fake2.dev", |
| 157 | + "authentication": { |
| 158 | + "type": "jupyterhub", |
| 159 | + "api_token": "some_fake_token", |
| 160 | + }, |
| 161 | + "access_rights": { |
| 162 | + 154: CLUSTER_ADMIN_RIGHTS, |
| 163 | + 12: CLUSTER_MANAGER_RIGHTS, |
| 164 | + 7899: CLUSTER_USER_RIGHTS, |
| 165 | + }, |
| 166 | + }, |
| 167 | + ] |
| 168 | + } |
| 169 | + |
| 170 | + @validator("access_rights", always=True, pre=True) |
| 171 | + @classmethod |
| 172 | + def check_owner_has_access_rights(cls, v, values): |
| 173 | + owner_gid = values["owner"] |
| 174 | + # check owner is in the access rights, if not add it |
| 175 | + if owner_gid not in v: |
| 176 | + v[owner_gid] = CLUSTER_ADMIN_RIGHTS |
| 177 | + # check owner has full access |
| 178 | + if v[owner_gid] != CLUSTER_ADMIN_RIGHTS: |
| 179 | + raise ValueError("the cluster owner access rights are incorrectly set") |
| 180 | + return v |
0 commit comments