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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
//! SPIFFE-ID and TrustDomain types compliant with the SPIFFE standard.

use std::convert::TryFrom;
use std::fmt;
use std::fmt::{Display, Formatter};
use std::str::FromStr;

use thiserror::Error;
use url::{ParseError, Url};

const SPIFFE_SCHEME: &str = "spiffe";
const SPIFFE_ID_MAXIMUM_LENGTH: usize = 2048;
const TRUST_DOMAIN_MAXIMUM_LENGTH: usize = 255;

/// Represents a [SPIFFE ID](https://github.com/spiffe/spiffe/blob/main/standards/SPIFFE-ID.md#2-spiffe-identity).
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct SpiffeId {
    trust_domain: TrustDomain,
    path: String,
}

/// Represents a [SPIFFE Trust domain](https://github.com/spiffe/spiffe/blob/main/standards/SPIFFE-ID.md#21-trust-domain)
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct TrustDomain {
    name: String,
}

/// An error that can arise parsing a SPIFFE ID.
#[derive(Debug, Error, PartialEq, Clone)]
#[non_exhaustive]
pub enum SpiffeIdError {
    /// An empty string cannot be parsed as a SPIFFE ID.
    #[error("SPIFFE ID cannot be empty")]
    Empty,

    /// A SPIFFE ID cannot be longer than 2048 characters.
    #[error("SPIFFE ID is too long")]
    IdTooLong,

    /// A SPIFFE ID must have a scheme 'spiffe'.
    #[error("scheme is missing")]
    MissingScheme,

    /// A SPIFFE ID must have a scheme 'spiffe'.
    #[error("invalid scheme")]
    InvalidScheme,

    /// The host component of SPIFFE ID URI cannot be empty.
    #[error("trust domain cannot be empty")]
    MissingTrustDomain,

    /// TrustDomain, i.e. host component in URI cannot be longer than 255 characters.
    #[error("trust domain is too long")]
    TrustDomainTooLong,

    /// A SPIFFE ID URI cannot have a port.
    #[error("port is not allowed")]
    PortNotAllowed,

    /// A SPIFFE ID URI cannot have a query.
    #[error("query is not allowed")]
    QueryNotAllowed,

    /// A SPIFFE ID URI cannot have a fragment.
    #[error("fragment is not allowed")]
    FragmentNotAllowed,

    /// A SPIFFE ID URI cannot have a user info.
    #[error("user info is not allowed")]
    UserInfoNotAllowed,

    /// Error returned by the URI parsing library.
    #[error("failed parsing SPIFFE ID from Uri")]
    CannotParseUri(#[from] ParseError),
}

impl SpiffeId {
    /// Attempts to parse a SPIFFE ID from the given id string.
    ///
    /// # Arguments
    ///
    /// * `id` - A SPIFFE ID, e.g. 'spiffe://example.org/path/subpath'
    ///
    /// # Errors
    ///
    /// If the function cannot parse the input as a SPIFFE ID, a [`SpiffeIdError`] variant will be returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use spiffe::spiffe_id::SpiffeId;
    ///
    /// let spiffe_id = SpiffeId::new("spiffe://example.org/path").unwrap();
    /// assert_eq!("example.org", spiffe_id.trust_domain().to_string());
    /// assert_eq!("/path", spiffe_id.path());
    /// ```
    pub fn new(id: &str) -> Result<Self, SpiffeIdError> {
        let id = id.trim();
        if id.is_empty() {
            return Err(SpiffeIdError::Empty);
        }

        let url = Url::from_str(id)?;
        Self::validate_spiffe_id(&url)?;

        let domain_name = match url.host_str() {
            None => return Err(SpiffeIdError::MissingTrustDomain),
            Some(host) if host.len() > TRUST_DOMAIN_MAXIMUM_LENGTH => {
                return Err(SpiffeIdError::TrustDomainTooLong)
            }
            Some(host) => host.to_lowercase(),
        };

        let trust_domain = TrustDomain { name: domain_name };
        let path = String::from(url.path());
        Ok(SpiffeId { trust_domain, path })
    }

    /// Returns the trust domain of the SPIFFE ID.
    pub fn trust_domain(&self) -> &TrustDomain {
        &self.trust_domain
    }

    /// Returns the path of the SPIFFE ID.
    pub fn path(&self) -> &str {
        &self.path
    }

    /// Returns `true` if this SPIFFE ID has the given TrustDomain.
    pub fn is_member_of(&self, trust_domain: &TrustDomain) -> bool {
        &self.trust_domain == trust_domain
    }

    // Performs the validations to comply with the SPIFFE standard.
    fn validate_spiffe_id(url: &Url) -> Result<(), SpiffeIdError> {
        if url.scheme().is_empty() {
            return Err(SpiffeIdError::MissingScheme);
        }

        if url.scheme() != SPIFFE_SCHEME {
            return Err(SpiffeIdError::InvalidScheme);
        }

        if url.query().is_some() {
            return Err(SpiffeIdError::QueryNotAllowed);
        }

        if url.fragment().is_some() {
            return Err(SpiffeIdError::FragmentNotAllowed);
        }

        if !url.username().is_empty() {
            return Err(SpiffeIdError::UserInfoNotAllowed);
        }

        if url.port().is_some() {
            return Err(SpiffeIdError::PortNotAllowed);
        }

        if url.as_str().len() > SPIFFE_ID_MAXIMUM_LENGTH {
            return Err(SpiffeIdError::IdTooLong);
        }

        Ok(())
    }
}

impl Display for SpiffeId {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}://{}{}", SPIFFE_SCHEME, self.trust_domain, self.path)
    }
}

impl FromStr for SpiffeId {
    type Err = SpiffeIdError;

    fn from_str(id: &str) -> Result<Self, Self::Err> {
        Self::new(id)
    }
}

impl TryFrom<String> for SpiffeId {
    type Error = SpiffeIdError;
    fn try_from(s: String) -> Result<SpiffeId, Self::Error> {
        Self::new(s.as_ref())
    }
}

impl TryFrom<&str> for SpiffeId {
    type Error = SpiffeIdError;
    fn try_from(s: &str) -> Result<SpiffeId, Self::Error> {
        Self::from_str(s)
    }
}

impl TrustDomain {
    /// Attempts to parse a TrustDomain instance from the given name.
    ///
    /// The name is normalized to lowercase and cannot be longer than 255 characters.
    ///
    /// # Arguments
    ///
    /// * `name` - Name of the trust domain, it also can be a SPIFFE ID string from which the domain name
    /// is extracted.
    ///
    /// # Errors
    ///
    /// If the function cannot parse the input as a Trust domain, a [`SpiffeIdError`] variant will be returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use spiffe::spiffe_id::TrustDomain;
    ///
    /// let trust_domain = TrustDomain::new("Domain.Test").unwrap();
    /// assert_eq!("domain.test", trust_domain.to_string());
    /// assert_eq!("spiffe://domain.test", trust_domain.id_string());
    ///
    /// let trust_domain = TrustDomain::new("spiffe://example.org/path").unwrap();
    /// assert_eq!("example.org", trust_domain.to_string());
    /// assert_eq!("spiffe://example.org", trust_domain.id_string());
    /// ```
    pub fn new(name: &str) -> Result<Self, SpiffeIdError> {
        let name = name.trim();
        if name.is_empty() {
            return Err(SpiffeIdError::MissingTrustDomain);
        }

        let mut name = name.to_lowercase();
        if !name.contains("://") {
            name = format!("{}://{}", SPIFFE_SCHEME, name);
        }

        let spiffe_id = SpiffeId::try_from(name)?;

        Ok(spiffe_id.trust_domain)
    }

    /// Returns a string representation of the SPIFFE ID of the trust domain,
    /// e.g. "spiffe://example.org".
    pub fn id_string(&self) -> String {
        format!("{}://{}", SPIFFE_SCHEME, self.name)
    }
}

impl Display for TrustDomain {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl AsRef<str> for TrustDomain {
    fn as_ref(&self) -> &str {
        self.name.as_str()
    }
}

impl FromStr for TrustDomain {
    type Err = SpiffeIdError;

    fn from_str(name: &str) -> Result<Self, Self::Err> {
        TrustDomain::new(name)
    }
}

impl TryFrom<&str> for TrustDomain {
    type Error = SpiffeIdError;

    fn try_from(name: &str) -> Result<Self, Self::Error> {
        Self::new(name)
    }
}

impl TryFrom<String> for TrustDomain {
    type Error = SpiffeIdError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::new(value.as_ref())
    }
}

#[cfg(test)]
mod spiffe_id_tests {
    use std::str::FromStr;

    use url::ParseError;

    use super::*;

    macro_rules! spiffe_id_success_tests {
        ($($name:ident: $value:expr,)*) => {
        $(
            #[test]
            fn $name() {
                let (input, expected) = $value;
                let spiffe_id = SpiffeId::from_str(input).unwrap();
                assert_eq!(spiffe_id, expected);
            }
        )*
        }
    }

    spiffe_id_success_tests! {
        from_valid_uri_str: (
            "spiffe://example.org/path/element",
            SpiffeId {
                trust_domain: TrustDomain::from_str("example.org").unwrap(),
                path: "/path/element".to_string(),
            }
        ),

        from_valid_uri_str_preserve_case_for_path: (
            "spiffe://EXAMPLE.org/PATH/Element",
            SpiffeId {
                trust_domain: TrustDomain::from_str("example.org").unwrap(),
                path: "/PATH/Element".to_string(),
            }
        ),

        from_str_uri_maximum_length: (
            &format!("spiffe://domain.test/{}", "a".repeat(2027)),
            SpiffeId {
                trust_domain: TrustDomain::from_str("domain.test").unwrap(),
                path: format!("/{}","a".repeat(2027)),
            }
        ),
    }

    #[test]
    fn test_is_member_of() {
        let spiffe_id = SpiffeId::from_str("spiffe://example.org").unwrap();
        let trust_domain = TrustDomain::from_str("example.org").unwrap();

        assert!(spiffe_id.is_member_of(&trust_domain));
    }

    #[test]
    fn test_new_from_string() {
        let id_string = String::from("spiffe://example.org/path/element");
        let spiffe_id = SpiffeId::try_from(id_string).unwrap();

        let expected_trust_domain = TrustDomain::from_str("example.org").unwrap();

        assert_eq!(spiffe_id.trust_domain, expected_trust_domain);
        assert_eq!(spiffe_id.path(), "/path/element");
    }

    #[test]
    fn test_to_string() {
        let spiffe_id = SpiffeId::from_str("spiffe://example.org/path/element").unwrap();
        assert_eq!(spiffe_id.to_string(), "spiffe://example.org/path/element");
    }

    #[test]
    fn test_try_from_str() {
        let spiffe_id = SpiffeId::try_from("spiffe://example.org/path").unwrap();

        assert_eq!(
            spiffe_id.trust_domain,
            TrustDomain::from_str("example.org").unwrap()
        );
        assert_eq!(spiffe_id.path, "/path");
    }

    #[test]
    fn test_try_from_string() {
        let spiffe_id = SpiffeId::try_from(String::from("spiffe://example.org/path")).unwrap();

        assert_eq!(
            spiffe_id.trust_domain,
            TrustDomain::from_str("example.org").unwrap()
        );
        assert_eq!(spiffe_id.path, "/path");
    }

    macro_rules! spiffe_id_error_tests {
        ($($name:ident: $value:expr,)*) => {
        $(
            #[test]
            fn $name() {
            let (input, expected_error) = $value;
                let spiffe_id = SpiffeId::from_str(input);
                let error = spiffe_id.unwrap_err();

                assert_eq!(error, expected_error);
            }
        )*
        }
    }

    spiffe_id_error_tests! {
        from_empty_str: ("", SpiffeIdError::Empty),
        from_blank_str: (" ", SpiffeIdError::Empty),
        from_str_invalid_uri_str_contains_ip_address: (
            "192.168.2.2:6688",
            SpiffeIdError::CannotParseUri(ParseError::RelativeUrlWithoutBase),
        ),
        from_str_uri_str_invalid_scheme: (
            "http://domain.test/path/element",
            SpiffeIdError::InvalidScheme,
        ),
        from_str_uri_str_empty_authority: (
            "spiffe:/path/element",
            SpiffeIdError::MissingTrustDomain,
        ),
        from_str_uri_str_empty_authority_after_slashes: (
            "spiffe:///path/element",
            SpiffeIdError::MissingTrustDomain,
        ),
        from_str_uri_str_empty_authority_no_slashes: (
            "spiffe:path/element",
            SpiffeIdError::MissingTrustDomain,
        ),
        from_str_uri_str_with_query: (
            "spiffe://domain.test/path/element?query=1",
            SpiffeIdError::QueryNotAllowed,
        ),
        from_str_uri_str_with_fragment: (
            "spiffe://domain.test/path/element#fragment-1",
            SpiffeIdError::FragmentNotAllowed,
        ),
        from_str_uri_str_with_port: (
            "spiffe://domain.test:8080/path/element",
            SpiffeIdError::PortNotAllowed,
        ),
        from_str_uri_str_with_user_info: (
            "spiffe://user:password@test.org/path/element",
            SpiffeIdError::UserInfoNotAllowed,
        ),
        from_str_uri_exceeds_maximum_length: (
            &format!("spiffe://domain.test/{}", "a".repeat(2028)),
            SpiffeIdError::IdTooLong,
        ),
    }
}

#[cfg(test)]
mod trust_domain_tests {

    use super::*;
    use std::str::FromStr;
    use url::ParseError;

    macro_rules! trust_domain_success_tests {
        ($($name:ident: $value:expr,)*) => {
        $(
            #[test]
            fn $name() {
                let (input, expected) = $value;
                let trust_domain = TrustDomain::new(input).unwrap();
                assert_eq!(trust_domain, expected);
            }
        )*
        }
    }

    trust_domain_success_tests! {
        from_str_domain: ("example.org", TrustDomain{name: "example.org".to_string()}),
        from_str_domain_uppercase: ("  EXAMPLE.org ", TrustDomain{name: "example.org".to_string()}),
        from_str_spiffeid: ("spiffe://other.test", TrustDomain{name: "other.test".to_string()}),
        from_str_spiffeid_with_path: ("spiffe://domain.test/path/element", TrustDomain{name: "domain.test".to_string()}),
        from_str_spiffeid_with_wrapped_uir: ("spiffe://domain.test/spiffe://domain.test:80/path/element", TrustDomain{name: "domain.test".to_string()}),
        from_max_length_str: (&"a".repeat(255), TrustDomain{name: "a".repeat(255)}),
    }

    macro_rules! trust_domain_error_tests {
        ($($name:ident: $value:expr,)*) => {
        $(
            #[test]
            fn $name() {
                let (input, expected_error) = $value;
                let trust_domain = TrustDomain::new(input);
                let error = trust_domain.unwrap_err();
                assert_eq!(error, expected_error);
            }
        )*
        }
    }

    trust_domain_error_tests! {
        from_empty_str: ("", SpiffeIdError::MissingTrustDomain),
        from_empty_blank: ("  ", SpiffeIdError::MissingTrustDomain),
        from_invalid_scheme:  ("other://domain.test", SpiffeIdError::InvalidScheme),
        from_uri_with_port: ("spiffe://domain.test:80", SpiffeIdError::PortNotAllowed),
        from_uri_with_userinfo: ("spiffe://user:pass@domain.test", SpiffeIdError::UserInfoNotAllowed),
        from_uri_with_invalid_domain: ("spiffe:// domain.test", SpiffeIdError::CannotParseUri(ParseError::InvalidDomainCharacter)),
        from_uri_with_empty_scheme: ("://domain.test", SpiffeIdError::CannotParseUri(ParseError::RelativeUrlWithoutBase)),
        from_uri_with_empty_domain: ("spiffe:///path", SpiffeIdError::MissingTrustDomain),
        from_uri_exceeds_maximum_length: (&"a".repeat(256), SpiffeIdError::TrustDomainTooLong),
    }

    #[test]
    fn test_equals() {
        let td_1 = TrustDomain::new("domain.test").unwrap();
        let td_2 = TrustDomain::new("domain.test").unwrap();
        assert_eq!(td_1, td_2);
    }

    #[test]
    fn test_not_equals() {
        let td_1 = TrustDomain::new("domain.test").unwrap();
        let td_2 = TrustDomain::new("other.test").unwrap();
        assert_ne!(td_1, td_2);
    }

    #[test]
    fn test_to_string() {
        let trust_domain = TrustDomain::from_str("example.org").unwrap();
        assert_eq!(trust_domain.to_string(), "example.org");
    }

    #[test]
    fn test_to_id_string() {
        let trust_domain = TrustDomain::from_str("example.org").unwrap();
        assert_eq!(trust_domain.id_string(), "spiffe://example.org");
    }

    #[test]
    fn test_try_from_str() {
        let trust_domain = TrustDomain::try_from("example.org").unwrap();
        assert_eq!(trust_domain.to_string(), "example.org");
    }

    #[test]
    fn test_try_from_string() {
        let trust_domain = TrustDomain::try_from(String::from("example.org")).unwrap();
        assert_eq!(trust_domain.to_string(), "example.org");
    }
}