htsget-config 0.23.0

Used to configure htsget-rs by using a config file or reading environment variables.
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
//! Authorization response data structures for JWT authorization.
//!
//! This module provides data structures for parsing and validating authorization
//! responses from external authorization services.
//!

use crate::config::location::{Location, Locations};
use crate::error::Error::BuilderError;
use crate::error::{Error, Result};
use crate::types::{Format, Interval};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

/// Authorization restrictions from an external authorization service.
#[derive(JsonSchema, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct AuthorizationRestrictions {
  /// The version of the schema.
  #[validate(range(min = 1))]
  version: u32,
  /// The authorization rules.
  #[serde(rename = "htsgetAuth")]
  #[validate(length(min = 1))]
  htsget_auth: Vec<AuthorizationRule>,
}

/// Individual authorization rule defining access permissions.
#[derive(JsonSchema, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct AuthorizationRule {
  /// The location that the authorization applies to.
  #[serde(alias = "backend")]
  location: Location,
  /// The reference name restrictions to apply to this path.
  #[serde(skip_serializing_if = "Option::is_none")]
  rules: Option<Vec<ReferenceNameRestriction>>,
}

/// Restriction on genomic reference names and coordinate ranges.
#[derive(JsonSchema, Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct ReferenceNameRestriction {
  /// The reference name to allow. Allows all reference names if unspecified.
  #[serde(rename = "referenceName", skip_serializing_if = "Option::is_none")]
  reference_name: Option<String>,
  /// The format to allow. Allows all formats if unspecified.
  #[serde(skip_serializing_if = "Option::is_none")]
  format: Option<Format>,
  /// The interval to allow. Allows all intervals if unspecified.
  #[serde(flatten)]
  interval: Interval,
}

impl AuthorizationRestrictions {
  /// Get the version of the authorization response format.
  pub fn version(&self) -> u32 {
    self.version
  }

  /// Get the authorization rules.
  pub fn htsget_auth(&self) -> &[AuthorizationRule] {
    &self.htsget_auth
  }

  /// Get the authorization rules as an owned vec.
  pub fn into_rules(self) -> Vec<AuthorizationRule> {
    self.htsget_auth
  }

  /// Get the possible locations from the remote authorization service that would be valid to
  /// search with. I.e. locations with backends that are not defaulted.
  pub fn into_remote_locations(self) -> Locations {
    self
      .into_rules()
      .into_iter()
      .flat_map(|r| {
        let location = r.into_location();
        if location.backend().is_defaulted() {
          None
        } else {
          Some(location)
        }
      })
      .collect::<Vec<_>>()
      .into()
  }
}

impl AuthorizationRule {
  /// The location of the rule.
  pub fn location(&self) -> &Location {
    &self.location
  }

  /// Get the owned location.
  pub fn into_location(self) -> Location {
    self.location
  }

  /// Get the optional rules on reference names and genomic coordinates.
  pub fn rules(&self) -> Option<&[ReferenceNameRestriction]> {
    self.rules.as_deref()
  }

  /// Get the optional rules on reference names and genomic coordinates as a mutable reference.
  pub fn rules_mut(&mut self) -> Option<&mut [ReferenceNameRestriction]> {
    self.rules.as_deref_mut()
  }
}

impl ReferenceNameRestriction {
  /// Get the name of the reference sequence.
  pub fn reference_name(&self) -> Option<&str> {
    self.reference_name.as_deref()
  }

  /// Get the optional format restriction.
  pub fn format(&self) -> Option<Format> {
    self.format
  }

  /// Get the interval to allow.
  pub fn interval(&self) -> &Interval {
    &self.interval
  }

  /// Set the interval.
  pub fn set_interval(&mut self, interval: Interval) {
    self.interval = interval;
  }
}

/// Builder for `AuthorizationRestrictions`.
#[derive(JsonSchema, Debug, Clone, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct AuthorizationRestrictionsBuilder {
  /// The version of the schema.
  #[validate(range(min = 1))]
  version: Option<u32>,
  /// The authorization rules.
  #[serde(rename = "htsgetAuth")]
  #[validate(length(min = 1))]
  htsget_auth: Vec<AuthorizationRule>,
}

impl AuthorizationRestrictionsBuilder {
  /// Set the version of the authorization response format.
  pub fn version(mut self, version: u32) -> Self {
    self.version = Some(version);
    self
  }

  /// Add an authorization rule.
  pub fn rule(mut self, rule: AuthorizationRule) -> Self {
    self.htsget_auth.push(rule);
    self
  }

  /// Add multiple authorization rules.
  pub fn rules(mut self, rules: Vec<AuthorizationRule>) -> Self {
    self.htsget_auth.extend(rules);
    self
  }

  /// Build the `AuthorizationRestrictions`.
  pub fn build(self) -> Result<AuthorizationRestrictions> {
    if self.htsget_auth.is_empty() {
      return Err(BuilderError("empty htsget authorization".to_string()));
    }
    if self.version.is_some_and(|version| version < 1) {
      return Err(BuilderError("version must be greater than 1".to_string()));
    }

    Ok(AuthorizationRestrictions {
      version: self.version.unwrap_or(1),
      htsget_auth: self.htsget_auth,
    })
  }
}

/// Builder for `AuthorizationRule`.
#[derive(JsonSchema, Debug, Clone, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct AuthorizationRuleBuilder {
  /// The location that the authorization applies to.
  #[serde(alias = "backend")]
  location: Option<Location>,
  /// The reference name restrictions to apply to this path.
  #[serde(skip_serializing_if = "Vec::is_empty")]
  reference_names: Vec<ReferenceNameRestriction>,
}

impl AuthorizationRuleBuilder {
  /// Set the location for this rule.
  pub fn location(mut self, location_either: Location) -> Self {
    self.location = Some(location_either);
    self
  }

  /// Add a reference name restriction.
  pub fn reference_name(mut self, restriction: ReferenceNameRestriction) -> Self {
    self.reference_names.push(restriction);
    self
  }

  /// Add multiple reference name restrictions.
  pub fn reference_names(mut self, restrictions: Vec<ReferenceNameRestriction>) -> Self {
    self.reference_names.extend(restrictions);
    self
  }

  /// Build the `AuthorizationRule`.
  pub fn build(self) -> Result<AuthorizationRule> {
    let location = self
      .location
      .ok_or_else(|| BuilderError("location not set".to_string()))?;
    if location
      .as_simple()
      .is_ok_and(|simple| simple.prefix_or_id().is_none())
    {
      return Err(BuilderError("A prefix or id must be set".to_string()));
    }

    Ok(AuthorizationRule {
      location,
      rules: if self.reference_names.is_empty() {
        None
      } else {
        Some(self.reference_names)
      },
    })
  }
}

/// Builder for `ReferenceNameRestriction`.
#[derive(JsonSchema, Debug, Clone, Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ReferenceNameRestrictionBuilder {
  /// The reference name to allow. Allows all reference names if unspecified.
  #[serde(rename = "referenceName", skip_serializing_if = "Option::is_none")]
  name: Option<String>,
  /// The format to allow. Allows all formats if unspecified.
  #[serde(skip_serializing_if = "Option::is_none")]
  format: Option<Format>,
  /// The start interval to allow. Allows any start interval if unspecified.
  start: Option<u32>,
  /// The end interval to allow. Allows any end interval if unspecified.
  end: Option<u32>,
}

impl ReferenceNameRestrictionBuilder {
  /// Set the reference name.
  pub fn name<S: Into<String>>(mut self, name: S) -> Self {
    self.name = Some(name.into());
    self
  }

  /// Set the format restriction.
  pub fn format(mut self, format: Format) -> Self {
    self.format = Some(format);
    self
  }

  /// Set the start position.
  pub fn start(mut self, start: u32) -> Self {
    self.start = Some(start);
    self
  }

  /// Set the end position.
  pub fn end(mut self, end: u32) -> Self {
    self.end = Some(end);
    self
  }

  /// Build the `ReferenceNameRestriction`.
  pub fn build(self) -> Result<ReferenceNameRestriction> {
    if let (Some(ref start), Some(ref end)) = (self.start, self.end)
      && start >= end
    {
      return Err(BuilderError("start must be less than end".to_string()));
    }

    Ok(ReferenceNameRestriction {
      reference_name: self.name,
      format: self.format,
      interval: Interval::new(self.start, self.end),
    })
  }
}

impl TryFrom<AuthorizationRestrictionsBuilder> for AuthorizationRestrictions {
  type Error = Error;

  fn try_from(builder: AuthorizationRestrictionsBuilder) -> Result<Self> {
    builder.build()
  }
}

impl TryFrom<AuthorizationRuleBuilder> for AuthorizationRule {
  type Error = Error;

  fn try_from(builder: AuthorizationRuleBuilder) -> Result<Self> {
    builder.build()
  }
}

impl TryFrom<ReferenceNameRestrictionBuilder> for ReferenceNameRestriction {
  type Error = Error;

  fn try_from(builder: ReferenceNameRestrictionBuilder) -> Result<Self> {
    builder.build()
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::config::location::{PrefixOrId, SimpleLocation};
  use serde_json;

  #[test]
  fn test_authorization_response_deserialization() {
    let json_value = serde_json::json!({
      "version": 1,
      "htsgetAuth": [{
        "location": {
          "id": "path/to/file"
        },
        "rules": [{
          "referenceName": "chr1",
          "start": 1000,
          "end": 2000,
          "format": "BAM"
        }]
      }]
    });
    let response: AuthorizationRestrictions = serde_json::from_value(json_value).unwrap();

    assert_eq!(response.version(), 1);
    assert_eq!(response.htsget_auth().len(), 1);
    assert_eq!(
      response.htsget_auth()[0]
        .location()
        .as_simple()
        .unwrap()
        .prefix_or_id()
        .unwrap()
        .as_id()
        .unwrap(),
      "path/to/file"
    );

    let restrictions = response.htsget_auth()[0].rules().unwrap();
    assert_eq!(restrictions.len(), 1);
    assert_eq!(restrictions[0].reference_name(), Some("chr1"));
    assert_eq!(restrictions[0].interval().start(), Some(1000));
    assert_eq!(restrictions[0].interval().end(), Some(2000));
    assert_eq!(restrictions[0].format(), Some(Format::Bam));

    let no_restrictions_value = serde_json::json!({
      "version": 1,
      "htsgetAuth": [{
        "location": {
          "id": "path/to/file"
        }
      }]
    });
    let no_restrictions_response: AuthorizationRestrictions =
      serde_json::from_value(no_restrictions_value).unwrap();
    assert_eq!(no_restrictions_response.version(), 1);
    assert_eq!(no_restrictions_response.htsget_auth().len(), 1);
    assert_eq!(
      no_restrictions_response.htsget_auth()[0]
        .location()
        .as_simple()
        .unwrap()
        .prefix_or_id()
        .unwrap()
        .as_id()
        .unwrap(),
      "path/to/file"
    );
    assert!(no_restrictions_response.htsget_auth()[0].rules().is_none());
  }

  #[test]
  fn test_reference_name_restriction_builder() {
    let restriction = ReferenceNameRestrictionBuilder::default()
      .name("chr1")
      .format(Format::Bam)
      .start(3000)
      .end(2000)
      .build();
    assert!(restriction.is_err());

    let restriction = ReferenceNameRestrictionBuilder::default()
      .format(Format::Bam)
      .start(2000)
      .end(3000)
      .build();
    assert!(restriction.is_ok());
  }

  #[test]
  fn test_authorization_rule_builder() {
    let rule = AuthorizationRuleBuilder::default()
      .location(Location::Simple(Box::new(SimpleLocation::new(
        Default::default(),
        "".to_string(),
        Some(PrefixOrId::Id("sample1".to_string())),
      ))))
      .build()
      .unwrap();
    assert_eq!(
      rule
        .location()
        .as_simple()
        .unwrap()
        .prefix_or_id()
        .unwrap()
        .as_id()
        .unwrap(),
      "sample1"
    );
    assert!(rule.rules().is_none());

    let rule = AuthorizationRuleBuilder::default().build();
    assert!(rule.is_err());

    let rule = AuthorizationRuleBuilder::default()
      .location(Location::Simple(Box::new(SimpleLocation::new(
        Default::default(),
        "".to_string(),
        None,
      ))))
      .build();
    assert!(rule.is_err());
  }

  #[test]
  fn test_authorization_restrictions_builder() {
    let rule = AuthorizationRestrictionsBuilder::default()
      .version(0)
      .build();
    assert!(rule.is_err());
    let rule = AuthorizationRestrictionsBuilder::default()
      .version(1)
      .build();
    assert!(rule.is_err());
  }
}