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
// Copyright 2017 Dmitry Tantsur <divius.inside@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! JSON structures and protocol bits for the Identity V3 API.

use std::convert::TryFrom;

use log::{debug, error, trace, warn};
use osproto::common::{Root, Version};
use reqwest::{Method, Url};

use super::request;
use super::services::ServiceType;
use super::url;
use super::{ApiVersion, AuthType, Error, ErrorKind};

/// Information about API endpoint.
#[derive(Debug)]
pub struct ServiceInfo {
    /// Root endpoint.
    pub root_url: Url,
    /// Major API version.
    pub major_version: Option<ApiVersion>,
    /// Current API version (if supported).
    pub current_version: Option<ApiVersion>,
    /// Minimum API version (if supported).
    pub minimum_version: Option<ApiVersion>,
}

impl TryFrom<Version> for ServiceInfo {
    type Error = Error;

    fn try_from(value: Version) -> Result<ServiceInfo, Error> {
        let endpoint = match value.links.into_iter().find(|x| &x.rel == "self") {
            Some(link) => link.href,
            None => {
                return Err(Error::new(
                    ErrorKind::InvalidResponse,
                    "Invalid version - missing self link",
                ));
            }
        };

        Ok(ServiceInfo {
            root_url: endpoint,
            major_version: Some(value.id.into()),
            current_version: value.version.map(From::from),
            minimum_version: value.min_version.map(From::from),
        })
    }
}

#[inline]
async fn fetch_root(
    catalog_type: &'static str,
    endpoint: Url,
    auth: &dyn AuthType,
) -> Result<Root, Error> {
    debug!("Fetching {} service info from {}", catalog_type, endpoint);
    request::fetch_json(auth.request(Method::GET, endpoint).await?).await
}

impl ServiceInfo {
    fn from_root<Srv: ServiceType>(mut value: Root, service: Srv) -> Result<ServiceInfo, Error> {
        trace!(
            "Available major versions for {} service: {:?}",
            service.catalog_type(),
            value
        );

        if let Root::OneVersion { version: ver } = value {
            if service.major_version_supported(ver.id.into()) {
                if !ver.is_stable() {
                    warn!(
                        "Using version {:?} of {} API that is not marked as stable",
                        ver,
                        service.catalog_type()
                    );
                }

                ServiceInfo::try_from(ver)
            } else {
                error!(
                    "Major version {} of the {} service is not supported",
                    ver.id,
                    service.catalog_type()
                );
                Err(Error::new(
                    ErrorKind::EndpointNotFound,
                    "Major version not supported",
                ))
            }
        } else {
            value.sort();
            value
                .into_stable_iter()
                .rfind(|x| service.major_version_supported(x.id.into()))
                .ok_or_else(|| Error::new_endpoint_not_found(service.catalog_type()))
                .and_then(TryFrom::try_from)
        }
    }

    /// Whether this service supports the given API version.
    ///
    /// Defaults to false if cannot be determined.
    #[inline]
    pub fn supports_api_version(&self, version: ApiVersion) -> bool {
        match (self.minimum_version, self.current_version) {
            (Some(min), Some(max)) => min <= version && max >= version,
            (None, Some(current)) => current == version,
            (Some(min), None) => version >= min,
            _ => false,
        }
    }

    /// Generic code to extract a `ServiceInfo` from a URL.
    pub async fn fetch<Srv: ServiceType>(
        service: Srv,
        endpoint: Url,
        auth: &dyn AuthType,
    ) -> Result<ServiceInfo, Error> {
        let fallback = ServiceInfo {
            root_url: endpoint.clone(),
            major_version: None,
            current_version: None,
            minimum_version: None,
        };

        if !service.version_discovery_supported() {
            debug!(
                "Service {} does not support version discovery, using {}",
                service.catalog_type(),
                endpoint
            );
            return Ok(fallback);
        }

        // Workaround for old version of Nova returning HTTP endpoints even if
        // accessed via HTTP
        let secure = endpoint.scheme() == "https";
        let catalog_type = service.catalog_type();

        let root = match fetch_root(catalog_type, endpoint.clone(), auth).await {
            Ok(root) => root,
            Err(e) if e.kind() == ErrorKind::ResourceNotFound => {
                if url::is_root(&endpoint) {
                    error!(
                        "Got HTTP 404 from the root URL {}, invalid endpoint for {} service",
                        endpoint, catalog_type
                    );
                    let err = Error::new_endpoint_not_found(catalog_type);
                    return Err(err);
                } else {
                    debug!("Got HTTP 404 from {}, trying parent endpoint", endpoint);
                    fetch_root(catalog_type, url::pop(endpoint), auth).await?
                }
            }
            Err(e) => return Err(e),
        };

        let mut info = ServiceInfo::from_root(root, service).or_else(move |e| {
            if e.kind() == ErrorKind::EndpointNotFound {
                debug!(
                    "Service returned EndpointNotFound when attempting version discovery, using {}",
                    fallback.root_url
                );
                Ok(fallback)
            } else {
                Err(e)
            }
        })?;

        // Older Nova returns insecure URLs even for secure protocol.
        if secure && info.root_url.scheme() == "http" {
            info.root_url.set_scheme("https").unwrap();
        }

        debug!("Received {:?} for {} service", info, catalog_type);
        Ok(info)
    }
}

#[cfg(test)]
pub(crate) mod test {
    use std::convert::TryFrom;

    use osproto::common::{Link, Root, Version, XdotY};
    use reqwest::Url;

    use super::super::services::ServiceType;
    use super::super::{ApiVersion, ErrorKind};
    use super::ServiceInfo;

    #[test]
    fn test_version_into_service_info() {
        let url = Url::parse("https://example.com/v2").unwrap();
        let ver = Version {
            id: XdotY(2, 0),
            links: vec![
                Link {
                    href: Url::parse("https://example.com/docs").unwrap(),
                    rel: "other".to_string(),
                },
                Link {
                    href: url.clone(),
                    rel: "self".to_string(),
                },
            ],
            status: None,
            version: Some(XdotY(2, 2)),
            min_version: None,
        };
        let info = ServiceInfo::try_from(ver).unwrap();
        assert_eq!(info.root_url, url);
        assert_eq!(info.major_version, Some(ApiVersion(2, 0)));
        assert_eq!(info.current_version, Some(ApiVersion(2, 2)));
        assert_eq!(info.minimum_version, None);
    }

    #[test]
    fn test_version_into_service_info_no_self_link() {
        let ver = Version {
            id: XdotY(2, 0),
            links: vec![Link {
                href: Url::parse("https://example.com/docs").unwrap(),
                rel: "other".to_string(),
            }],
            status: None,
            version: Some(XdotY(2, 2)),
            min_version: None,
        };
        let err = ServiceInfo::try_from(ver).err().unwrap();
        assert_eq!(err.kind(), ErrorKind::InvalidResponse);
    }

    struct ServiceWithDiscovery;

    impl ServiceType for ServiceWithDiscovery {
        fn catalog_type(&self) -> &'static str {
            "test-service-with-discovery"
        }

        fn major_version_supported(&self, version: ApiVersion) -> bool {
            version.0 == 1 && version.1 > 0
        }
    }

    #[test]
    fn test_root_into_service_info_one_version() {
        let url = Url::parse("https://example.com/v1.2").unwrap();
        let root = Root::OneVersion {
            version: Version {
                id: XdotY(1, 2),
                links: vec![Link {
                    href: url.clone(),
                    rel: "self".to_string(),
                }],
                status: Some("STABLE".to_string()),
                version: None,
                min_version: None,
            },
        };

        let info = ServiceInfo::from_root(root, ServiceWithDiscovery).unwrap();
        assert_eq!(info.root_url, url);
        assert_eq!(info.major_version, Some(ApiVersion(1, 2)));
    }

    #[test]
    fn test_root_into_service_info_one_version_unsupported() {
        let url = Url::parse("https://example.com/v1.0").unwrap();
        let root = Root::OneVersion {
            version: Version {
                id: XdotY(1, 0),
                links: vec![Link {
                    href: url.clone(),
                    rel: "self".to_string(),
                }],
                status: Some("STABLE".to_string()),
                version: None,
                min_version: None,
            },
        };

        let err = ServiceInfo::from_root(root, ServiceWithDiscovery)
            .err()
            .unwrap();
        assert_eq!(err.kind(), ErrorKind::EndpointNotFound);
    }

    #[test]
    fn test_root_into_service_info_versions() {
        let url = Url::parse("https://example.com/v1.2").unwrap();
        let root = Root::MultipleVersions {
            versions: vec![
                Version {
                    id: XdotY(1, 0),
                    links: vec![Link {
                        href: Url::parse("https://example.com/1.0").unwrap(),
                        rel: "self".to_string(),
                    }],
                    status: Some("STABLE".to_string()),
                    version: None,
                    min_version: None,
                },
                Version {
                    id: XdotY(1, 1),
                    links: vec![Link {
                        href: Url::parse("https://example.com/1.1").unwrap(),
                        rel: "self".to_string(),
                    }],
                    status: Some("STABLE".to_string()),
                    version: None,
                    min_version: None,
                },
                Version {
                    id: XdotY(1, 2),
                    links: vec![Link {
                        href: url.clone(),
                        rel: "self".to_string(),
                    }],
                    status: Some("STABLE".to_string()),
                    version: None,
                    min_version: None,
                },
                Version {
                    id: XdotY(2, 0),
                    links: vec![Link {
                        href: Url::parse("https://example.com/2.0").unwrap(),
                        rel: "self".to_string(),
                    }],
                    status: Some("STABLE".to_string()),
                    version: None,
                    min_version: None,
                },
            ],
        };

        let info = ServiceInfo::from_root(root, ServiceWithDiscovery).unwrap();
        assert_eq!(info.root_url, url);
        assert_eq!(info.major_version, Some(ApiVersion(1, 2)));
    }

    #[test]
    fn test_root_into_service_info_versions_unsupported() {
        let root = Root::MultipleVersions {
            versions: vec![
                Version {
                    id: XdotY(1, 0),
                    links: vec![Link {
                        href: Url::parse("https://example.com/1.0").unwrap(),
                        rel: "self".to_string(),
                    }],
                    status: Some("STABLE".to_string()),
                    version: None,
                    min_version: None,
                },
                Version {
                    id: XdotY(2, 0),
                    links: vec![Link {
                        href: Url::parse("https://example.com/2.0").unwrap(),
                        rel: "self".to_string(),
                    }],
                    status: Some("STABLE".to_string()),
                    version: None,
                    min_version: None,
                },
            ],
        };

        let err = ServiceInfo::from_root(root, ServiceWithDiscovery)
            .err()
            .unwrap();
        assert_eq!(err.kind(), ErrorKind::EndpointNotFound);
    }
}