spiffe 0.13.0

Core SPIFFE identity types and Workload API sources
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
//! SPIFFE endpoint parsing and validation.
//!
//! Defines a SPIFFE-specific endpoint abstraction used by SPIFFE-related APIs
//! (SPIFFE Workload API, SPIRE Agent Admin API). Not a general-purpose networking endpoint.

use std::net::IpAddr;
use std::path::PathBuf;
use std::str::FromStr;

use thiserror::Error;
use url::Url;

const TCP_SCHEME: &str = "tcp";
const UNIX_SCHEME: &str = "unix";

/// Parsed SPIFFE endpoint.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Endpoint {
    /// UNIX domain socket endpoint (POSIX systems).
    Unix(PathBuf),

    /// TCP endpoint (host must be an IP address).
    Tcp {
        /// IP address of the endpoint.
        host: IpAddr,
        /// TCP port of the endpoint.
        port: u16,
    },
}

/// Errors returned by [`Endpoint::parse`].
#[derive(Debug, Error, PartialEq, Eq)]
pub enum EndpointError {
    /// The input could not be parsed as a valid URI.
    #[error("endpoint socket is not a valid URI")]
    Parse(#[from] url::ParseError),

    /// The URI scheme is not supported.
    #[error("endpoint socket URI scheme must be unix: or tcp:")]
    InvalidScheme,

    /// User info (`user:pass@...`) is not allowed.
    #[error("endpoint socket URI must not include user info")]
    HasUserInfo,

    /// Query values are not allowed.
    #[error("endpoint socket URI must not include query values")]
    HasQuery,

    /// Fragments are not allowed.
    #[error("endpoint socket URI must not include a fragment")]
    HasFragment,

    /// UNIX endpoints must not include an authority/host.
    #[error("unix: endpoint socket URI must not include an authority")]
    UnixAuthorityNotAllowed,

    /// UNIX endpoints must include a non-empty path.
    #[error("unix: endpoint socket URI must include a path")]
    UnixMissingPath,

    /// TCP endpoints must use an IP address (not a hostname).
    #[error("tcp: endpoint socket URI host must be an IP address")]
    TcpHostNotIp,

    /// TCP endpoints must include a port.
    #[error("tcp: endpoint socket URI must include a port")]
    TcpMissingPort,

    /// TCP endpoints must not include a non-empty path.
    #[error("tcp: endpoint socket URI must not include a path")]
    TcpUnexpectedPath,
}

impl Endpoint {
    /// Parse and validate a SPIFFE endpoint URI.
    ///
    /// ## Accepted formats
    ///
    /// - `unix:///path/to/socket`
    /// - `unix:/path/to/socket` (accepted in practice)
    /// - `tcp://1.2.3.4:8081`
    /// - `tcp:1.2.3.4:8081` (accepted in practice)
    ///
    /// ## Errors
    ///
    /// Returns an [`EndpointError`] if:
    /// - the input is not a valid URI,
    /// - the URI scheme is not supported,
    /// - the URI contains user info, query values, or a fragment,
    /// - the endpoint does not satisfy the validation rules for its scheme.
    pub fn parse(input: &str) -> Result<Self, EndpointError> {
        let normalized = normalize_endpoint_uri(input);
        let url = Url::parse(&normalized)?;

        if !url.username().is_empty() || url.password().is_some() {
            return Err(EndpointError::HasUserInfo);
        }
        if url.query().is_some() {
            return Err(EndpointError::HasQuery);
        }
        if url.fragment().is_some() {
            return Err(EndpointError::HasFragment);
        }

        match url.scheme() {
            UNIX_SCHEME => {
                if url.host_str().is_some() {
                    return Err(EndpointError::UnixAuthorityNotAllowed);
                }

                let path = url.path();
                if path.is_empty() || path == "/" {
                    return Err(EndpointError::UnixMissingPath);
                }

                // Require absolute paths (must start with "/")
                // This ensures `unix:tmp/sock` fails deterministically.
                if !path.starts_with('/') {
                    return Err(EndpointError::UnixMissingPath);
                }

                Ok(Self::Unix(PathBuf::from(path)))
            }

            TCP_SCHEME => {
                let host = match url.host() {
                    Some(url::Host::Ipv4(ipv4)) => IpAddr::V4(ipv4),
                    Some(url::Host::Ipv6(ipv6)) => IpAddr::V6(ipv6),
                    Some(url::Host::Domain(domain)) => {
                        // Try parsing as IP address (IPv4 might be parsed as Domain by url crate)
                        IpAddr::from_str(domain).map_err(
                            |std::net::AddrParseError { .. }| EndpointError::TcpHostNotIp,
                        )?
                    }
                    None => return Err(EndpointError::TcpHostNotIp),
                };
                let port = url.port().ok_or(EndpointError::TcpMissingPort)?;

                let path = url.path();
                if !path.is_empty() && path != "/" {
                    return Err(EndpointError::TcpUnexpectedPath);
                }

                Ok(Self::Tcp { host, port })
            }

            _ => Err(EndpointError::InvalidScheme),
        }
    }
}

impl FromStr for Endpoint {
    type Err = EndpointError;

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

fn normalize_endpoint_uri(input: &str) -> String {
    // Accept the shorthand `unix:/path` by rewriting it into a valid URL.
    if let Some(input) = input.strip_prefix("unix:/") {
        if !input.starts_with('/') {
            return format!("unix:///{input}");
        }
    }

    // Accept the shorthand `tcp:IP:PORT` by rewriting it into a valid URL.
    if let Some(input) = input.strip_prefix("tcp:") {
        if !input.starts_with("//") {
            return format!("tcp://{input}");
        }
    }

    input.to_owned()
}

#[cfg(test)]
mod tests {
    use super::{Endpoint, EndpointError};
    use std::net::IpAddr;
    use std::path::PathBuf;

    #[test]
    fn parse_correct_unix_address_triple_slash() {
        let ep = Endpoint::parse("unix:///foo").unwrap();
        assert_eq!(ep, Endpoint::Unix(PathBuf::from("/foo")));
    }

    #[test]
    fn parse_correct_unix_address_single_slash() {
        let ep = Endpoint::parse("unix:/tmp/spire-agent/public/api.sock").unwrap();
        assert_eq!(
            ep,
            Endpoint::Unix(PathBuf::from("/tmp/spire-agent/public/api.sock"))
        );
    }

    #[test]
    fn parse_correct_tcp_address() {
        let ep = Endpoint::parse("tcp://1.2.3.4:80").unwrap();
        let expected_host: IpAddr = "1.2.3.4".parse().unwrap();

        assert_eq!(
            ep,
            Endpoint::Tcp {
                host: expected_host,
                port: 80
            }
        );
    }

    #[test]
    fn from_str_delegates_to_parse() {
        use std::str::FromStr as _;
        let ep1 = Endpoint::parse("unix:///tmp/sock").unwrap();
        let ep2 = Endpoint::from_str("unix:///tmp/sock").unwrap();
        assert_eq!(ep1, ep2);

        let ep3 = Endpoint::parse("tcp://127.0.0.1:8080").unwrap();
        let ep4 = Endpoint::from_str("tcp://127.0.0.1:8080").unwrap();
        assert_eq!(ep3, ep4);
    }

    #[test]
    fn parse_correct_tcp_address_shorthand() {
        let ep = Endpoint::parse("tcp:127.0.0.1:8081").unwrap();
        let expected_host: IpAddr = "127.0.0.1".parse().unwrap();

        assert_eq!(
            ep,
            Endpoint::Tcp {
                host: expected_host,
                port: 8081
            }
        );
    }

    #[test]
    fn parse_correct_tcp_address_ipv6() {
        let ep = Endpoint::parse("tcp://[::1]:8080").unwrap();
        let expected_host: IpAddr = "::1".parse().unwrap();

        assert_eq!(
            ep,
            Endpoint::Tcp {
                host: expected_host,
                port: 8080
            }
        );
    }

    #[test]
    fn parse_correct_tcp_address_ipv6_shorthand() {
        let ep = Endpoint::parse("tcp:[::1]:8080").unwrap();
        let expected_host: IpAddr = "::1".parse().unwrap();

        assert_eq!(
            ep,
            Endpoint::Tcp {
                host: expected_host,
                port: 8080
            }
        );
    }

    #[test]
    fn parse_errors_are_stable_across_url_versions() {
        for input in [" ", "foo"] {
            let err = Endpoint::parse(input).unwrap_err();
            assert!(matches!(err, EndpointError::Parse(_)));
            assert_eq!(err.to_string(), "endpoint socket is not a valid URI");
        }
    }

    macro_rules! parse_error_tests {
        ($($name:ident: $value:expr_2021,)*) => {
            $(
                #[test]
                fn $name() {
                    let (input, expected_error, expected_message) = $value;

                    let err = Endpoint::parse(input).unwrap_err();

                    assert_eq!(err, expected_error);
                    assert_eq!(err.to_string(), expected_message);
                }
            )*
        }
    }

    parse_error_tests! {
        parse_invalid_scheme: (
            "other:///path",
            EndpointError::InvalidScheme,
            "endpoint socket URI scheme must be unix: or tcp:",
        ),

        parse_unix_uri_empty_path: (
            "unix://",
            EndpointError::UnixMissingPath,
            "unix: endpoint socket URI must include a path",
        ),
        parse_unix_uri_empty_path_slash: (
            "unix:///",
            EndpointError::UnixMissingPath,
            "unix: endpoint socket URI must include a path",
        ),
        parse_unix_uri_with_query_values: (
            "unix:///foo?whatever",
            EndpointError::HasQuery,
            "endpoint socket URI must not include query values",
        ),
        parse_unix_uri_with_fragment: (
            "unix:///foo#whatever",
            EndpointError::HasFragment,
            "endpoint socket URI must not include a fragment",
        ),
        parse_unix_uri_with_user_info: (
            "unix://john:doe@foo/path",
            EndpointError::HasUserInfo,
            "endpoint socket URI must not include user info",
        ),
        parse_unix_uri_with_authority: (
            "unix://tmp/spire-agent/public/api.sock",
            EndpointError::UnixAuthorityNotAllowed,
            "unix: endpoint socket URI must not include an authority",
        ),

        parse_tcp_uri_non_empty_path: (
            "tcp://1.2.3.4:80/path",
            EndpointError::TcpUnexpectedPath,
            "tcp: endpoint socket URI must not include a path",
        ),
        parse_tcp_uri_with_query_values: (
            "tcp://1.2.3.4:80?whatever",
            EndpointError::HasQuery,
            "endpoint socket URI must not include query values",
        ),
        parse_tcp_uri_with_fragment: (
            "tcp://1.2.3.4:80#whatever",
            EndpointError::HasFragment,
            "endpoint socket URI must not include a fragment",
        ),
        parse_tcp_uri_with_user_info: (
            "tcp://john:doe@1.2.3.4:80",
            EndpointError::HasUserInfo,
            "endpoint socket URI must not include user info",
        ),
        parse_tcp_uri_with_password_only_user_info: (
            "tcp://:secret@127.0.0.1:8080",
            EndpointError::HasUserInfo,
            "endpoint socket URI must not include user info",
        ),

        parse_tcp_uri_no_ip: (
            "tcp://foo:80",
            EndpointError::TcpHostNotIp,
            "tcp: endpoint socket URI host must be an IP address",
        ),
        parse_tcp_uri_no_port: (
            "tcp://1.2.3.4",
            EndpointError::TcpMissingPort,
            "tcp: endpoint socket URI must include a port",
        ),
    }

    #[test]
    fn parse_unix_missing_slash_after_scheme() {
        // `unix:tmp/sock` (missing slash after scheme) should fail deterministically
        // because the path is not absolute (doesn't start with "/").
        let err = Endpoint::parse("unix:tmp/sock").unwrap_err();
        assert_eq!(err, EndpointError::UnixMissingPath);
        assert_eq!(
            err.to_string(),
            "unix: endpoint socket URI must include a path"
        );
    }

    #[test]
    fn parse_tcp_with_root_path() {
        // `tcp://127.0.0.1:8080/` should be accepted (path "/")
        let ep = Endpoint::parse("tcp://127.0.0.1:8080/").unwrap();
        let expected_host: IpAddr = "127.0.0.1".parse().unwrap();
        assert_eq!(
            ep,
            Endpoint::Tcp {
                host: expected_host,
                port: 8080
            }
        );
    }

    #[test]
    fn parse_tcp_shorthand_missing_port() {
        // `tcp:127.0.0.1` should return TcpMissingPort
        let err = Endpoint::parse("tcp:127.0.0.1").unwrap_err();
        assert_eq!(err, EndpointError::TcpMissingPort);
        assert_eq!(
            err.to_string(),
            "tcp: endpoint socket URI must include a port"
        );
    }

    #[test]
    fn parse_tcp_ipv6_missing_port() {
        // `tcp://[::1]` should return TcpMissingPort
        let err = Endpoint::parse("tcp://[::1]").unwrap_err();
        assert_eq!(err, EndpointError::TcpMissingPort);
        assert_eq!(
            err.to_string(),
            "tcp: endpoint socket URI must include a port"
        );
    }
}