komodo_client 2.1.0

Client for the Komodo build and deployment system
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
use std::fmt::Write;

use indexmap::IndexSet;
use serde::{Deserialize, Serialize};
use strum::{
  AsRefStr, Display, EnumDiscriminants, EnumString, IntoStaticStr,
  VariantArray,
};
use typeshare::typeshare;

use super::{MongoId, ResourceTarget};

/// Representation of a User or UserGroups permission on a resource.
#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(
  feature = "mongo",
  derive(mongo_indexed::derive::MongoIndexed)
)]
// To query for all permissions on user target
#[cfg_attr(feature = "mongo", doc_index({ "user_target.type": 1, "user_target.id": 1 }))]
// To query for all permissions on a resource target
#[cfg_attr(feature = "mongo", doc_index({ "resource_target.type": 1, "resource_target.id": 1 }))]
// Only one permission allowed per user / resource target
#[cfg_attr(feature = "mongo", unique_doc_index({
  "user_target.type": 1,
  "user_target.id": 1,
  "resource_target.type": 1,
  "resource_target.id": 1
}))]
pub struct Permission {
  /// The id of the permission document
  #[serde(
    default,
    rename = "_id",
    skip_serializing_if = "String::is_empty",
    with = "bson::serde_helpers::hex_string_as_object_id"
  )]
  pub id: MongoId,
  /// The target User / UserGroup
  pub user_target: UserTarget,
  /// The target resource
  pub resource_target: ResourceTarget,
  /// The permission level for the [user_target] on the [resource_target].
  #[serde(default)]
  pub level: PermissionLevel,
  /// Any specific permissions for the [user_target] on the [resource_target].
  #[serde(default)]
  #[cfg_attr(feature = "utoipa", schema(value_type = Vec<SpecificPermission>))]
  pub specific: IndexSet<SpecificPermission>,
}

#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize, EnumDiscriminants)]
#[strum_discriminants(name(UserTargetVariant))]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[cfg_attr(
  not(feature = "utoipa"),
  strum_discriminants(derive(
    PartialOrd,
    Ord,
    Hash,
    Serialize,
    Deserialize,
    Display,
    EnumString,
    AsRefStr
  ))
)]
#[cfg_attr(
  feature = "utoipa",
  strum_discriminants(derive(
    PartialOrd,
    Ord,
    Hash,
    Serialize,
    Deserialize,
    Display,
    EnumString,
    AsRefStr,
    utoipa::ToSchema
  ))
)]
#[serde(tag = "type", content = "id")]
pub enum UserTarget {
  /// User Id
  User(String),
  /// UserGroup Id
  UserGroup(String),
}

impl UserTarget {
  pub fn extract_variant_id(self) -> (UserTargetVariant, String) {
    match self {
      UserTarget::User(id) => (UserTargetVariant::User, id),
      UserTarget::UserGroup(id) => (UserTargetVariant::UserGroup, id),
    }
  }
}

/// The levels of permission that a User or UserGroup can have on a resource.
#[typeshare]
#[derive(
  Serialize,
  Deserialize,
  Debug,
  Display,
  EnumString,
  AsRefStr,
  Hash,
  Clone,
  Copy,
  PartialEq,
  Eq,
  PartialOrd,
  Ord,
  Default,
)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum PermissionLevel {
  /// No permissions.
  #[default]
  None,
  /// Can read resource information and config
  Read,
  /// Can execute actions on the resource
  Execute,
  /// Can update the resource configuration
  Write,
}

impl Default for &PermissionLevel {
  fn default() -> Self {
    &PermissionLevel::None
  }
}

/// The specific types of permission that a User or UserGroup can have on a resource.
#[typeshare]
#[derive(
  Serialize,
  Deserialize,
  Debug,
  Display,
  EnumString,
  AsRefStr,
  IntoStaticStr,
  VariantArray,
  Hash,
  Clone,
  Copy,
  PartialEq,
  Eq,
  PartialOrd,
  Ord,
)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub enum SpecificPermission {
  /// On **Server**
  ///   - Access the terminal apis
  /// On **Stack / Deployment**
  ///   - Access the container exec Apis
  Terminal,
  /// On **Server**
  ///   - Allowed to attach Stacks, Deployments, Repos, Builders to the Server
  /// On **Builder**
  ///   - Allowed to attach Builds to the Builder
  /// On **Build**
  ///   - Allowed to attach Deployments to the Build
  Attach,
  /// On **Server**
  ///   - Access the `container inspect` apis
  /// On **Stack / Deployment**
  ///   - Access `container inspect` apis for associated containers
  Inspect,
  /// On **Server**
  ///   - Read all container logs on the server
  /// On **Stack / Deployment**
  ///   - Read the container logs
  Logs,
  /// On **Server**
  ///   - Read all the processes on the host
  Processes,
}

impl SpecificPermission {
  fn all() -> IndexSet<SpecificPermission> {
    SpecificPermission::VARIANTS.iter().cloned().collect()
  }
}

#[typeshare]
#[derive(Debug, Clone, Default)]
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
pub struct PermissionLevelAndSpecifics {
  pub level: PermissionLevel,
  #[cfg_attr(feature = "utoipa", schema(value_type = Vec<SpecificPermission>))]
  pub specific: IndexSet<SpecificPermission>,
}

impl From<PermissionLevel> for PermissionLevelAndSpecifics {
  fn from(level: PermissionLevel) -> Self {
    Self {
      level,
      specific: IndexSet::new(),
    }
  }
}

impl From<&Permission> for PermissionLevelAndSpecifics {
  fn from(value: &Permission) -> Self {
    Self {
      level: value.level,
      specific: value.specific.clone(),
    }
  }
}

impl PermissionLevel {
  /// Add all possible permissions (for use in admin case)
  pub fn all(self) -> PermissionLevelAndSpecifics {
    PermissionLevelAndSpecifics {
      level: self,
      specific: SpecificPermission::all(),
    }
  }

  pub fn specifics(
    self,
    specific: IndexSet<SpecificPermission>,
  ) -> PermissionLevelAndSpecifics {
    PermissionLevelAndSpecifics {
      level: self,
      specific,
    }
  }

  fn specific(
    self,
    specific: SpecificPermission,
  ) -> PermissionLevelAndSpecifics {
    PermissionLevelAndSpecifics {
      level: self,
      specific: [specific].into_iter().collect(),
    }
  }

  /// Operation requires Terminal permission
  pub fn terminal(self) -> PermissionLevelAndSpecifics {
    self.specific(SpecificPermission::Terminal)
  }

  /// Operation requires Attach permission
  pub fn attach(self) -> PermissionLevelAndSpecifics {
    self.specific(SpecificPermission::Attach)
  }

  /// Operation requires Inspect permission
  pub fn inspect(self) -> PermissionLevelAndSpecifics {
    self.specific(SpecificPermission::Inspect)
  }

  /// Operation requires Logs permission
  pub fn logs(self) -> PermissionLevelAndSpecifics {
    self.specific(SpecificPermission::Logs)
  }

  /// Operation requires Processes permission
  pub fn processes(self) -> PermissionLevelAndSpecifics {
    self.specific(SpecificPermission::Processes)
  }
}

impl PermissionLevelAndSpecifics {
  /// Elevates self by permissions in other:
  /// - If other.level > self.level, set self.level = other.level
  /// - If other includes more specifics, add them.
  pub fn elevate(&mut self, other: &impl HasLevelAndSpecific) {
    let other_level = other.level();
    if other_level > self.level {
      self.level = other_level;
    }
    self.specific.extend(other.specific().iter().cloned());
  }

  /// Joins permissions in self with other to produce a new PermissionsLevelAndSpecifics:
  /// - If other.level > self.level, set self.level = other.level
  /// - If other includes more specifics, add them.
  pub fn join(
    &self,
    other: &impl HasLevelAndSpecific,
  ) -> PermissionLevelAndSpecifics {
    let mut specific = self.specific.clone();
    specific.extend(other.specific().iter().cloned());
    PermissionLevelAndSpecifics {
      level: std::cmp::max(self.level, other.level()),
      specific,
    }
  }

  /// Joins permissions in self with other to produce a new PermissionsLevelAndSpecifics:
  /// - If other.level > self.level, set self.level = other.level
  /// - If other includes more specifics, add them.
  pub fn join_permission(
    &self,
    other: &Permission,
  ) -> PermissionLevelAndSpecifics {
    let mut specific = self.specific.clone();
    specific.extend(other.specific.iter().cloned());
    PermissionLevelAndSpecifics {
      level: std::cmp::max(self.level, other.level),
      specific,
    }
  }

  /// Returns true when self.level >= other.level,
  /// and has all required specific permissions.
  pub fn fulfills(
    &self,
    other: &PermissionLevelAndSpecifics,
  ) -> bool {
    if self.level < other.level {
      return false;
    }
    for specific in other.specific.iter() {
      if !self.specific.contains(specific) {
        return false;
      }
    }
    true
  }

  /// Returns true when self has all required specific permissions.
  pub fn fulfills_specific(
    &self,
    specifics: &IndexSet<SpecificPermission>,
  ) -> bool {
    for specific in specifics.iter() {
      if !self.specific.contains(specific) {
        return false;
      }
    }
    true
  }

  pub fn specifics_for_log(&self) -> String {
    let mut res = String::new();
    for specific in self.specific.iter() {
      if res.is_empty() {
        write!(&mut res, "{specific}").unwrap();
      } else {
        write!(&mut res, ", {specific}").unwrap();
      }
    }
    res
  }

  pub fn specifics(
    mut self,
    specific: IndexSet<SpecificPermission>,
  ) -> PermissionLevelAndSpecifics {
    self.specific = specific;
    self
  }

  fn specific(
    mut self,
    specific: SpecificPermission,
  ) -> PermissionLevelAndSpecifics {
    self.specific.insert(specific);
    PermissionLevelAndSpecifics {
      level: self.level,
      specific: self.specific,
    }
  }

  /// Operation requires Terminal permission
  pub fn terminal(self) -> PermissionLevelAndSpecifics {
    self.specific(SpecificPermission::Terminal)
  }

  /// Operation requires Attach permission
  pub fn attach(self) -> PermissionLevelAndSpecifics {
    self.specific(SpecificPermission::Attach)
  }

  /// Operation requires Inspect permission
  pub fn inspect(self) -> PermissionLevelAndSpecifics {
    self.specific(SpecificPermission::Inspect)
  }

  /// Operation requires Logs permission
  pub fn logs(self) -> PermissionLevelAndSpecifics {
    self.specific(SpecificPermission::Logs)
  }

  /// Operation requires Processes permission
  pub fn processes(self) -> PermissionLevelAndSpecifics {
    self.specific(SpecificPermission::Processes)
  }
}

pub trait HasLevelAndSpecific {
  fn level(&self) -> PermissionLevel;
  fn specific(&self) -> &IndexSet<SpecificPermission>;
}

impl HasLevelAndSpecific for Permission {
  fn level(&self) -> PermissionLevel {
    self.level
  }
  fn specific(&self) -> &IndexSet<SpecificPermission> {
    &self.specific
  }
}

impl HasLevelAndSpecific for PermissionLevelAndSpecifics {
  fn level(&self) -> PermissionLevel {
    self.level
  }
  fn specific(&self) -> &IndexSet<SpecificPermission> {
    &self.specific
  }
}