dsh_api 0.9.0

DSH resource management API client
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! # Parsers for formatted strings
//!
//! Module that contains parse functions for selected formatted strings as used in the DSH and
//! the DSH resource management API.
use itertools::Itertools;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use std::sync::LazyLock;

/// Enum that describes an auth string. Auth strings are used in the `exposedPorts` section
/// of a service definition file and are deserialized into the  `auth` field of the
/// [`PortMapping`](crate::types::PortMapping) data structure.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum AuthString {
  /// Represents a basic authentication string, like
  /// `"basic-auth@<realm>:<username>:<password-hash>"` or one of the older formats
  /// that are still supported for backwards compatibility.
  /// The enum fields contain the optional realm string and the username.
  Basic { realm: Option<String>, username: String },
  /// Represents a forward authentication string, like
  /// `"fwd-auth@<auth service endpoint>@<auth response headers>"`.
  /// The enum field contains the authentication service endpoint and the optional headers string.
  Fwd { authentication_service_endpoint: String, headers: Option<String> },
  /// Represents a tenant authentication string, like
  /// `"system-fwd-auth@<accepted roles>"`.
  /// The enum field contains the accepted roles.
  SystemFwd { accepted_roles: String },
}

impl AuthString {
  /// # Create an `AuthString::Basic`
  ///
  /// # Parameters
  /// * `realm` - optional realm
  /// * `username` - mandatory username
  pub fn basic<T, U>(realm: Option<T>, username: U) -> Self
  where
    T: Into<String>,
    U: Into<String>,
  {
    Self::Basic { realm: realm.map(Into::<String>::into), username: username.into() }
  }

  /// # Create an `AuthString::Fwd`
  ///
  /// # Parameters
  /// * `endpoint` - the authentication service endpoint
  pub fn fwd<T, U>(endpoint: T, headers: Option<U>) -> Self
  where
    T: Into<String>,
    U: Into<String>,
  {
    Self::Fwd { authentication_service_endpoint: endpoint.into(), headers: headers.map(Into::into) }
  }

  /// # Create an `AuthString::SystemFwd`
  ///
  /// # Parameters
  /// * `roles` - comma separated list of accepted roles
  pub fn system_fwd<T>(roles: T) -> Self
  where
    T: Into<String>,
  {
    Self::SystemFwd { accepted_roles: roles.into() }
  }
}

impl FromStr for AuthString {
  type Err = String;

  /// # Parse auth string
  ///
  /// # Example
  ///
  /// ```
  /// # use std::str::FromStr;
  /// # use dsh_api::parse::AuthString;
  /// assert_eq!(
  ///   AuthString::from_str("basic-auth@my-realm:my-username:$password-hash/"),
  ///   Ok(AuthString::basic(Some("my-realm"), "my-username"))
  /// );
  /// assert_eq!(
  ///   AuthString::from_str("fwd-auth@https://my-authentication-service.com@my-headers"),
  ///   Ok(AuthString::fwd("https://my-authentication-service.com", Some("my-headers")))
  /// );
  /// assert_eq!(
  ///   AuthString::from_str("system-fwd-auth@view,manage"),
  ///   Ok(AuthString::system_fwd("view,manage"))
  /// );
  /// ```
  ///
  /// # Parameters
  /// * `auth_string` - the auth string to be parsed
  ///
  /// # Returns
  /// When the provided string is valid, the method returns an instance of the `AuthString`
  /// struct, describing the auth string.
  fn from_str(auth_string: &str) -> Result<Self, Self::Err> {
    if let Some(basic_authentication_string) = auth_string.strip_prefix("basic-auth@") {
      parse_basic_authentication_string(basic_authentication_string)
    } else if let Some(fwd_auth_string) = auth_string.strip_prefix("fwd-auth@") {
      let split_string = fwd_auth_string.split("@").collect_vec();
      match split_string.first() {
        Some(authentication_service) => Ok(Self::fwd(*authentication_service, split_string.get(1).map(|headers| headers.to_string()))),
        None => Err(format!("invalid forward authentication string (\"{}\")", auth_string)),
      }
    } else if let Some(roles) = auth_string.strip_prefix("system-fwd-auth@") {
      Ok(Self::system_fwd(roles))
    } else {
      parse_basic_authentication_string(auth_string)
    }
  }
}

impl Display for AuthString {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::Basic { realm, username } => match realm {
        Some(realm) => write!(f, "basic@{}:{}", realm, username),
        None => write!(f, "basic@{}", username),
      },
      Self::Fwd { authentication_service_endpoint, headers } => match headers {
        Some(headers) => write!(f, "fwd@{}@{}", authentication_service_endpoint, headers),
        None => write!(f, "fwd@{}", authentication_service_endpoint),
      },
      Self::SystemFwd { accepted_roles } => write!(f, "sys-fwd@{}", accepted_roles),
    }
  }
}

/// Struct that describes an image string for a registry image.
#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Serialize)]
pub struct RegistryImage {
  /// Tenant
  pub tenant: String,
  /// Image identifier
  pub id: String,
  /// Image version
  pub version: String,
}

/// Struct that describes an image string for an app catalog image.
#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Serialize)]
pub struct AppImage {
  /// Stage of development (`draft` or `release`)
  pub stage: String,
  /// Supplier of the image (`klarrio` or `kpn`)
  pub supplier: String,
  /// Tenant
  pub tenant: String,
  /// Image identifier
  pub id: String,
  /// Image version
  pub version: String,
}

/// Enum that describes an image string. Image strings are used in the
/// service definition file and are deserialized into the `image` field of the
/// [`Application`](crate::types::Application) data structure.
#[derive(Clone, Debug, Deserialize, Hash, PartialEq, Serialize)]
pub enum ImageString {
  Registry { image: RegistryImage },
  App { image: AppImage },
  Unrecognized { image: String },
}

impl ImageString {
  /// # Create an `ImageString::Registry`
  ///
  /// # Parameters
  /// * `registry` - registry that contains the image (`app` or `registry`)
  /// * `tenant` - tenant
  /// * `id` - image identifier
  /// * `version` - image version
  pub fn registry(tenant: String, id: String, version: String) -> Self {
    Self::Registry { image: RegistryImage { tenant, id, version } }
  }

  /// # Create an `ImageString::App`
  ///
  /// # Parameters
  /// * `stage` - stage of development (`draft` or `release`)
  /// * `supplier` - supplier of the image (`klarrio` or `kpn`)
  /// * `tenant` - tenant
  /// * `id` - image identifier
  /// * `version` - image version
  pub fn app(stage: String, supplier: String, tenant: String, id: String, version: String) -> Self {
    Self::App { image: AppImage { stage, supplier, tenant, id, version } }
  }

  /// Get the image id
  pub fn id(&self) -> String {
    match self {
      ImageString::Registry { image } => image.id.clone(),
      ImageString::App { image } => image.id.clone(),
      ImageString::Unrecognized { image } => image.to_string(),
    }
  }

  /// Get the image id
  pub fn source(&self) -> &str {
    match self {
      ImageString::Registry { .. } => "harbor",
      ImageString::App { .. } => "app-catalog",
      ImageString::Unrecognized { .. } => "",
    }
  }

  /// Get the image tenant
  pub fn tenant(&self) -> String {
    match self {
      ImageString::Registry { image } => image.tenant.clone(),
      ImageString::App { image } => image.tenant.clone(),
      ImageString::Unrecognized { .. } => "".to_string(),
    }
  }

  /// Get the image version
  pub fn version(&self) -> String {
    match self {
      ImageString::Registry { image } => image.version.clone(),
      ImageString::App { image } => image.version.clone(),
      ImageString::Unrecognized { .. } => "".to_string(),
    }
  }
}

impl From<&str> for ImageString {
  /// # Parse image string
  ///
  /// # Example
  ///
  /// ```
  /// # use dsh_api::parse::ImageString;
  /// assert_eq!(
  ///   ImageString::from(
  ///     "APPCATALOG_REGISTRY/dsh-appcatalog/tenant/my-tenant/\
  ///      1234/1234/draft/kpn/my-image:0.0.1"
  ///   ),
  ///   ImageString::app(
  ///     "draft".to_string(),
  ///     "kpn".to_string(),
  ///     "my-tenant".to_string(),
  ///     "my-image".to_string(),
  ///     "0.0.1".to_string()
  ///   )
  /// );
  /// assert_eq!(
  ///   ImageString::from("registry.cp.kpn-dsh.com/my-tenant/my-image:0.0.1"),
  ///   ImageString::registry("my-tenant".to_string(), "my-image".to_string(), "0.0.1".to_string())
  /// );
  /// ```
  ///
  /// # Parameters
  /// * `image_string` - the image string to be parsed
  ///
  /// # Returns
  /// When the provided string is valid, the method returns an `ImageString::App` or
  /// `ImageString::Registry` instance. When the string is invalid,
  /// a `ImageString::Unrecognized` will be returned.
  fn from(image_string: &str) -> Self {
    static APP_CATALOG_IMAGE_REGEX: LazyLock<Regex> = LazyLock::new(|| {
      Regex::new(r"APPCATALOG_REGISTRY/dsh-appcatalog/tenant/([a-z0-9-_]+)/([0-9]+)/([0-9]+)/(release|draft)/(klarrio|kpn)/([a-zA-Z][a-zA-Z0-9-_]*):([a-zA-Z0-9-_.]*)").unwrap()
    });
    static REGISTRY_IMAGE_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"registry.cp.kpn-dsh.com/([a-z0-9-_]+)/([a-zA-Z][a-zA-Z0-9-_]*):([a-zA-Z0-9-_.]*)").unwrap());
    match APP_CATALOG_IMAGE_REGEX.captures(image_string) {
      Some(captures) => Self::app(
        captures.get(4).map(|stage| stage.as_str().to_string()).unwrap_or_default(),
        captures.get(5).map(|supplier| supplier.as_str().to_string()).unwrap_or_default(),
        captures.get(1).map(|tenant| tenant.as_str().to_string()).unwrap_or_default(),
        captures.get(6).map(|id| id.as_str().to_string()).unwrap_or_default(),
        captures.get(7).map(|version| version.as_str().to_string()).unwrap_or_default(),
      ),
      None => match REGISTRY_IMAGE_REGEX.captures(image_string) {
        Some(registry_captures) => Self::registry(
          registry_captures.get(1).map(|tenant| tenant.as_str().to_string()).unwrap_or_default(),
          registry_captures.get(2).map(|id| id.as_str().to_string()).unwrap_or_default(),
          registry_captures.get(3).map(|version| version.as_str().to_string()).unwrap_or_default(),
        ),
        None => ImageString::Unrecognized { image: image_string.to_string() },
      },
    }
  }
}

impl Display for ImageString {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    match self {
      ImageString::Registry { image } => write!(f, "registry:{}:{}:{}", image.tenant, image.id, image.version),
      ImageString::App { image } => write!(f, "app:{}:{}:{}:{}:{}", image.stage, image.supplier, image.tenant, image.id, image.version),
      ImageString::Unrecognized { image } => write!(f, "{}", image),
    }
  }
}

#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum TopicString<'a> {
  Internal { name: &'a str, tenant: &'a str },
  Scratch { name: &'a str, tenant: &'a str },
  Stream { name: &'a str, tenant: &'a str },
}

impl<'a> TopicString<'a> {
  pub fn internal(name: &'a str, tenant: &'a str) -> Self {
    Self::Internal { name, tenant }
  }

  pub fn scratch(name: &'a str, tenant: &'a str) -> Self {
    Self::Scratch { name, tenant }
  }

  pub fn stream(name: &'a str, tenant: &'a str) -> Self {
    Self::Stream { name, tenant }
  }

  pub fn name(&self) -> &str {
    match self {
      TopicString::Internal { name, .. } => name,
      TopicString::Scratch { name, .. } => name,
      TopicString::Stream { name, .. } => name,
    }
  }

  pub fn tenant(&self) -> &str {
    match self {
      TopicString::Internal { tenant, .. } => tenant,
      TopicString::Scratch { tenant, .. } => tenant,
      TopicString::Stream { tenant, .. } => tenant,
    }
  }
}

impl<'a> TryFrom<&'a str> for TopicString<'a> {
  type Error = String;

  fn try_from(topic_string: &'a str) -> Result<Self, Self::Error> {
    parse_topic_string(topic_string)
  }
}

impl Display for TopicString<'_> {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    match self {
      TopicString::Internal { name, tenant } => write!(f, "internal.{}.{}", name, tenant),
      TopicString::Scratch { name, tenant } => write!(f, "scratch.{}.{}", name, tenant),
      TopicString::Stream { name, tenant } => write!(f, "stream.{}.{}", name, tenant),
    }
  }
}

// Parse basic authentication string
pub fn parse_basic_authentication_string(basic_authentication_string: &str) -> Result<AuthString, String> {
  let parts = basic_authentication_string.split(":").collect_vec();
  if parts.len() == 2 {
    Ok(AuthString::basic(None::<String>, *parts.first().unwrap()))
  } else if parts.len() == 3 {
    Ok(AuthString::basic(Some(*parts.first().unwrap()), *parts.get(1).unwrap()))
  } else {
    Err(format!("invalid basic authentication string (\"{}\")", basic_authentication_string))
  }
}

/// # Parse function string
///
/// # Example
///
/// ```
/// # use dsh_api::parse::parse_function1;
/// assert_eq!(parse_function1("{ function('parameter') }", "function"), Ok("parameter"));
/// ```
///
/// # Parameters
/// * `string` - the function string to be parsed
/// * `f_name` - the name of the function to match
///
/// # Returns
/// When the provided string is valid, the method returns the function parameter value
pub fn parse_function1<'a>(string: &'a str, f_name: &str) -> Result<&'a str, String> {
  static FUNCTION1_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\{\s*([a-z][a-z0-9_]*)\(\s*'([a-zA-Z0-9_.-]*)'\s*\)\s*}").unwrap());
  match FUNCTION1_REGEX.captures(string).map(|captures| {
    (
      captures.get(1).map(|first_match| first_match.as_str()),
      captures.get(2).map(|second_match| second_match.as_str()),
    )
  }) {
    Some((Some(function), Some(par))) if function == f_name => Ok(par),
    _ => Err(format!("invalid {} string (\"{}\")", f_name, string)),
  }
}

/// # Parse function string with two parameters
///
/// # Example
///
/// ```
/// # use dsh_api::parse::parse_function2;
/// assert_eq!(
///   parse_function2("{ function2('parameter1', 'parameter2') }", "function2"),
///   Ok(("parameter1", "parameter2"))
/// );
/// ```
///
/// # Parameters
/// * `string` - the function string to be parsed
/// * `f_name` - the name of the function to match
///
/// # Returns
/// When the provided string is valid, the method returns the two function parameter values
pub fn parse_function2<'a>(string: &'a str, f_name: &str) -> Result<(&'a str, &'a str), String> {
  static FUNCTION2_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\{\s*([a-z][a-z0-9_]*)\(\s*'([a-zA-Z0-9_.-]*)'\s*,\s*'([a-zA-Z0-9_.-]*)'\s*\)\s*}").unwrap());
  match FUNCTION2_REGEX.captures(string).map(|captures| {
    (
      captures.get(1).map(|first_match| first_match.as_str()),
      captures.get(2).map(|second_match| second_match.as_str()),
      captures.get(3).map(|second_match| second_match.as_str()),
    )
  }) {
    Some((Some(function), Some(par1), Some(par2))) if function == f_name => Ok((par1, par2)),
    _ => Err(format!("invalid {} string (\"{}\")", f_name, string)),
  }
}

/// # Parse function string with one or two parameters
///
/// # Example
///
/// ```
/// # use dsh_api::parse::parse_function;
/// assert_eq!(parse_function("{ function1('parameter') }", "function1"), Ok(("parameter", None)));
/// assert_eq!(
///   parse_function("{ function2('parameter1', 'parameter2') }", "function2"),
///   Ok(("parameter1", Some("parameter2")))
/// );
/// ```
///
/// # Parameters
/// * `string` - the function string to be parsed
/// * `f_name` - the name of the function to match
///
/// # Returns
/// When the provided string is valid, the method returns the two function parameter values,
/// the second of which can be `None`
pub fn parse_function<'a>(string: &'a str, f_name: &str) -> Result<(&'a str, Option<&'a str>), String> {
  match parse_function2(string, f_name) {
    Ok((first_parameter, second_parameter)) => Ok((first_parameter, Some(second_parameter))),
    Err(_) => match parse_function1(string, f_name) {
      Ok(parameter) => Ok((parameter, None)),
      Err(_) => Err(format!("invalid {} string (\"{}\")", f_name, string)),
    },
  }
}

/// # Parse volume string
///
/// # Example
///
/// ```
/// # use std::str::FromStr;
/// # use dsh_api::parse::parse_volume_string;
/// assert_eq!(parse_volume_string("{ volume('my_volume') }"), Ok("my_volume"));
/// ```
///
/// # Parameters
/// * `volume_string` - the volume string to be parsed
///
/// # Returns
/// When the provided string is valid, the method returns the volume name
pub fn parse_volume_string(volume_string: &str) -> Result<&str, String> {
  parse_function1(volume_string, "volume")
}

/// # Parse topic string
///
/// # Example
///
/// ```
/// # use std::str::FromStr;
/// # use dsh_api::parse::{parse_topic_string, TopicString};
/// assert_eq!(
///   parse_topic_string("scratch.topic-name.my-tenant"),
///   Ok(TopicString::scratch("topic-name", "my-tenant"))
/// );
/// ```
///
/// # Parameters
/// * `volume_string` - the volume string to be parsed
///
/// # Returns
/// When the provided string is valid, the method returns the volume name
pub fn parse_topic_string<'a>(topic_string: &'a str) -> Result<TopicString<'a>, String> {
  static TOPIC_STRING_REGEX: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(internal|stream|scratch)\.([a-z][a-z0-9-]*)\.([a-z][a-z0-9-]*)").unwrap());
  match TOPIC_STRING_REGEX.captures(topic_string) {
    Some(registry_captures) => {
      let name = registry_captures.get(2).map(|name| name.as_str()).unwrap();
      let tenant = registry_captures.get(3).map(|tenant| tenant.as_str()).unwrap();
      match registry_captures.get(1).map(|kind| kind.as_str()) {
        Some("internal") => Ok(TopicString::internal(name, tenant)),
        Some("stream") => Ok(TopicString::stream(name, tenant)),
        Some("scratch") => Ok(TopicString::scratch(name, tenant)),
        _ => unreachable!(),
      }
    }
    None => Err(format!("illegal topic name {}", topic_string)),
  }
}