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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
// 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.
//
// SPDX-License-Identifier: Apache-2.0
//! Asynchronous OpenStack client
use async_trait::async_trait;
use bytes::Bytes;
use chrono::TimeDelta;
use futures::io::{Error as IoError, ErrorKind as IoErrorKind};
use futures::stream::TryStreamExt;
use http::{header, HeaderMap, HeaderValue, Response as HttpResponse, StatusCode};
use reqwest::{Body, Certificate, Client as AsyncClient, Request, Response};
use std::convert::TryInto;
use std::fmt::{self, Debug};
use std::time::{Duration, SystemTime};
use std::{fs::File, io::Read};
use tokio_util::codec;
use tokio_util::compat::FuturesAsyncReadCompatExt;
use tracing::{debug, enabled, error, event, info, instrument, trace, warn, Level};
use crate::api;
use crate::api::query;
#[cfg(feature = "keystone_ng")]
use crate::api::query::QueryAsync;
use crate::api::query::RawQueryAsync;
use crate::api::RestClient;
use crate::auth::{
self,
auth_helper::{AuthHelper, Dialoguer, Noop},
authtoken::{self, AuthTokenError, AuthType},
Auth, AuthError, AuthState,
};
use crate::catalog::{Catalog, CatalogError, ServiceEndpoint};
use crate::config::CloudConfig;
use crate::config::{get_config_identity_hash, ConfigFile};
use crate::error::{OpenStackError, OpenStackResult, RestError};
use crate::state;
use crate::types::identity::v3::{AuthReceiptResponse, AuthResponse, Project, ServiceEndpoints};
use crate::types::{ApiVersion, BoxedAsyncRead, ServiceType};
use crate::utils::expand_tilde;
/// Asynchronous client for the OpenStack API for a single user
///
/// Separate Identity (not the scope) should use separate instances of this.
/// ```rust
/// use openstack_sdk::api::{paged, Pagination, QueryAsync};
/// use openstack_sdk::{AsyncOpenStack, config::ConfigFile, OpenStackError};
/// use openstack_sdk::types::ServiceType;
/// use openstack_sdk::api::compute::v2::flavor::list;
///
/// async fn list_flavors() -> Result<(), OpenStackError> {
/// // Get the builder for the listing Flavors Endpoint
/// let mut ep_builder = list::Request::builder();
/// // Set the `min_disk` query param
/// ep_builder.min_disk("15");
/// let ep = ep_builder.build().unwrap();
///
/// let cfg = ConfigFile::new().unwrap();
/// // Get connection config from clouds.yaml/secure.yaml
/// let profile = cfg.get_cloud_config("devstack").unwrap().unwrap();
/// // Establish connection
/// let mut session = AsyncOpenStack::new(&profile).await?;
///
/// // Invoke service discovery when desired.
/// session.discover_service_endpoint(&ServiceType::Compute).await?;
///
/// // Execute the call with pagination limiting maximum amount of entries to 1000
/// let data: Vec<serde_json::Value> = paged(ep, Pagination::Limit(1000))
/// .query_async(&session)
/// .await.unwrap();
///
/// println!("Data = {:?}", data);
/// Ok(())
/// }
/// ```
#[derive(Clone)]
pub struct AsyncOpenStack {
/// The client to use for API calls.
client: reqwest::Client,
/// Cloud configuration
config: CloudConfig,
/// The authentication information to use when communicating with OpenStack.
auth: Auth,
/// Endpoints catalog
catalog: Catalog,
/// Session state.
///
/// In order to save authentication roundtrips save/load authentication
/// information in the file (similar to how other cli tools are doing)
/// and check auth expiration upon load.
state: state::State,
}
impl Debug for AsyncOpenStack {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("OpenStack")
.field("service_endpoints", &self.catalog)
.finish()
}
}
#[async_trait]
impl api::RestClient for AsyncOpenStack {
type Error = RestError;
/// Get service endpoint from the catalog
fn get_service_endpoint(
&self,
service_type: &ServiceType,
version: Option<&ApiVersion>,
) -> Result<&ServiceEndpoint, api::ApiError<Self::Error>> {
Ok(self
.catalog
.get_service_endpoint(service_type.to_string(), version, None::<String>)?)
}
/// Get project id from the current scope
fn get_current_project(&self) -> Option<Project> {
if let Auth::AuthToken(token) = &self.auth {
return token.auth_info.clone().and_then(|x| x.token.project);
}
None
}
}
#[async_trait]
impl api::AsyncClient for AsyncOpenStack {
// Perform REST request
async fn rest_async(
&self,
request: http::request::Builder,
body: Vec<u8>,
) -> Result<HttpResponse<Bytes>, api::ApiError<<Self as api::RestClient>::Error>> {
self.rest_with_auth_async(request, body, &self.auth).await
}
/// Perform REST request with the body read from AsyncRead
async fn rest_read_body_async(
&self,
request: http::request::Builder,
body: BoxedAsyncRead,
) -> Result<HttpResponse<Bytes>, api::ApiError<<Self as api::RestClient>::Error>> {
self.rest_with_auth_read_body_async(request, body, &self.auth)
.await
}
/// Download result of HTTP operation.
async fn download_async(
&self,
request: http::request::Builder,
body: Vec<u8>,
) -> Result<(HeaderMap, BoxedAsyncRead), api::ApiError<<Self as api::RestClient>::Error>> {
self.download_with_auth_async(request, body, &self.auth)
.await
}
}
impl AsyncOpenStack {
/// Basic constructor
fn new_impl(config: &CloudConfig, auth: Auth) -> OpenStackResult<Self> {
let mut client_builder = AsyncClient::builder();
if let Some(cacert) = &config.cacert {
let mut buf = Vec::new();
File::open(expand_tilde(cacert).unwrap_or(cacert.into()))
.map_err(|e| OpenStackError::IOWithPath {
source: e,
path: cacert.into(),
})?
.read_to_end(&mut buf)
.map_err(|e| OpenStackError::IOWithPath {
source: e,
path: cacert.into(),
})?;
for cert in Certificate::from_pem_bundle(&buf)? {
client_builder = client_builder.add_root_certificate(cert);
}
}
if let Some(false) = &config.verify {
warn!(
"SSL Verification is disabled! Please consider using `cacert` for adding custom certificate instead."
);
client_builder = client_builder.danger_accept_invalid_certs(true);
}
client_builder = client_builder.pool_max_idle_per_host(10);
client_builder = client_builder.pool_idle_timeout(Duration::from_secs(30));
client_builder = client_builder.timeout(Duration::from_secs(
config
.options
.get("api_timeout")
.and_then(|val| val.clone().into_uint().ok())
.unwrap_or(30),
));
client_builder = client_builder.connect_timeout(Duration::from_secs(5));
client_builder = client_builder.tcp_keepalive(Duration::from_secs(60));
client_builder = client_builder.gzip(true);
client_builder = client_builder.deflate(true);
let mut session = AsyncOpenStack {
client: client_builder.build()?,
config: config.clone(),
auth,
catalog: Catalog::default(),
state: state::State::new(),
};
let auth_data = session
.config
.auth
.as_ref()
.ok_or(AuthTokenError::MissingAuthData)?;
let identity_service_url = auth_data
.auth_url
.as_ref()
.ok_or(AuthTokenError::MissingAuthUrl)?;
session.catalog.register_catalog_endpoint(
"identity",
identity_service_url,
config.region_name.as_ref(),
Some("public"),
)?;
session.catalog.configure(config)?;
session
.state
.set_auth_hash_key(get_config_identity_hash(config))
.enable_auth_cache(ConfigFile::new()?.is_auth_cache_enabled());
Ok(session)
}
/// Create a new OpenStack API session from CloudConfig
#[instrument(name = "connect", level = "trace", skip(config))]
pub async fn new(config: &CloudConfig) -> OpenStackResult<Self> {
let mut session = Self::new_impl(config, Auth::None)?;
// Ensure we resolve identity endpoint using version discovery
session
.discover_service_endpoint(&ServiceType::Identity)
.await?;
session.authorize(None, false, false).await?;
Ok(session)
}
/// Create a new OpenStack API session from CloudConfig
#[instrument(name = "connect", level = "trace", skip(config, auth_helper))]
pub async fn new_with_authentication_helper(
config: &CloudConfig,
auth_helper: &mut impl AuthHelper,
renew_auth: bool,
) -> OpenStackResult<Self> {
let mut session = Self::new_impl(config, Auth::None)?;
// Ensure we resolve identity endpoint using version discovery
session
.discover_service_endpoint(&ServiceType::Identity)
.await?;
session
.authorize_with_auth_helper(None, auth_helper, renew_auth)
.await?;
Ok(session)
}
/// Create a new OpenStack API session from CloudConfig
#[instrument(name = "connect", level = "trace", skip(config))]
#[deprecated(
since = "0.22.0",
note = "please use `new_with_authentication_helper` instead"
)]
pub async fn new_interactive(config: &CloudConfig, renew_auth: bool) -> OpenStackResult<Self> {
Self::new_with_authentication_helper(config, &mut Dialoguer::default(), renew_auth).await
}
/// Set the authorization to be used by the client
fn set_auth(&mut self, auth: auth::Auth, skip_cache_update: bool) -> &mut Self {
self.auth = auth;
if !skip_cache_update {
if let Auth::AuthToken(auth) = &self.auth {
// For app creds we should save auth as unscoped since:
// - on request it is disallowed to specify scope
// - response contain fixed scope
// With this it is not possible to find auth in the cache if we use the real
// scope
let scope = match &auth.auth_info {
Some(info) => {
if info.token.application_credential.is_some() {
authtoken::AuthTokenScope::Unscoped
} else {
auth.get_scope()
}
}
_ => auth.get_scope(),
};
self.state.set_scope_auth(&scope, auth);
}
}
self
}
/// Set TokenAuth as current authorization
fn set_token_auth(&mut self, token: String, token_info: Option<AuthResponse>) -> &mut Self {
let token_auth = authtoken::AuthToken {
token,
auth_info: token_info,
};
self.set_auth(Auth::AuthToken(Box::new(token_auth.clone())), false);
self
}
/// Authorize against the cloud using provided credentials and get the session token.
pub async fn authorize(
&mut self,
scope: Option<authtoken::AuthTokenScope>,
interactive: bool,
renew_auth: bool,
) -> Result<(), OpenStackError> {
if interactive {
self.authorize_with_auth_helper(scope, &mut Dialoguer::default(), renew_auth)
.await
} else {
self.authorize_with_auth_helper(scope, &mut Noop::default(), renew_auth)
.await
}
}
/// Authorize against the cloud using provided credentials and get the session token with the
/// auth helper that may be invoked to interactively ask for the credentials.
pub async fn authorize_with_auth_helper<A>(
&mut self,
scope: Option<authtoken::AuthTokenScope>,
auth_helper: &mut A,
renew_auth: bool,
) -> Result<(), OpenStackError>
where
A: AuthHelper,
{
let requested_scope = scope.map_or_else(
|| authtoken::AuthTokenScope::try_from(&self.config),
|v| Ok(v.clone()),
)?;
if let (Some(auth), false) = (self.state.get_scope_auth(&requested_scope), renew_auth) {
// Valid authorization is already available and no renewal is required
trace!("Auth already available");
self.set_auth(auth::Auth::AuthToken(Box::new(auth.clone())), true);
} else {
// No valid authorization data is available in the state or
// renewal is requested
let auth_type = AuthType::from_cloud_config(&self.config)?;
let mut force_new_auth = renew_auth;
if let AuthType::V3ApplicationCredential = auth_type {
// application_credentials token can not be used to get new token without again
// supplying application credentials (bug in Keystone?)
// So for AppCred we just force a brand new auth
force_new_auth = true;
}
let mut rsp;
if let (Some(available_auth), false) = (self.state.get_any_valid_auth(), force_new_auth)
{
// State contain valid authentication for different scope/unscoped. It is possible
// to request new authz using this other auth
trace!("Valid Auth is available for reauthz: {:?}", available_auth);
let auth_ep = authtoken::build_reauth_request(&available_auth, &requested_scope)?;
rsp = auth_ep.raw_query_async_ll(self, Some(false)).await?;
// Check whether re-auth was successful
api::check_response_error::<Self>(&rsp, None)?;
} else {
// No auth/authz information available. Proceed with new auth
trace!("No Auth already available. Proceeding with new login");
match auth_type {
AuthType::V3ApplicationCredential
| AuthType::V3Password
| AuthType::V3Token
| AuthType::V3Totp
| AuthType::V3Multifactor => {
let identity =
authtoken::build_identity_data_from_config(&self.config, auth_helper)
.await?;
let auth_ep = authtoken::build_auth_request_with_identity_and_scope(
&identity,
&requested_scope,
)?;
rsp = auth_ep.raw_query_async_ll(self, Some(false)).await?;
// Handle the MFA
if let StatusCode::UNAUTHORIZED = rsp.status() {
if let Some(receipt) = rsp.headers().get("openstack-auth-receipt") {
let receipt_data: AuthReceiptResponse =
serde_json::from_slice(rsp.body())?;
let auth_endpoint = authtoken::build_auth_request_from_receipt(
&self.config,
receipt.clone(),
&receipt_data,
&requested_scope,
auth_helper,
)
.await?;
rsp = auth_endpoint.raw_query_async(self).await?;
}
}
api::check_response_error::<Self>(&rsp, None)?;
}
AuthType::V3OidcAccessToken => {
let auth_ep =
auth::v3oidcaccesstoken::get_auth_ep(&self.config, auth_helper).await?;
rsp = auth_ep.raw_query_async(self).await?;
let token = rsp
.headers()
.get("x-subject-token")
.ok_or(AuthError::AuthTokenNotInResponse)?
.to_str()
.map_err(|_| AuthError::AuthTokenNotString)?;
// Set retrieved token as current auth
let token_info: AuthResponse = serde_json::from_slice(rsp.body())?;
let token_auth = authtoken::AuthToken {
token: token.to_string(),
auth_info: Some(token_info),
};
self.set_auth(Auth::AuthToken(Box::new(token_auth.clone())), false);
// And now time to rescope the token
let auth_ep =
authtoken::build_reauth_request(&token_auth, &requested_scope)?;
rsp = auth_ep.raw_query_async(self).await?;
}
AuthType::V3WebSso => {
let auth_url = auth::v3websso::get_auth_url(&self.config)?;
let identity_ep = self.get_service_endpoint(
&ServiceType::Identity,
Some(&ApiVersion::new(3, 0)),
)?;
let mut url = identity_ep.build_request_url(&auth_url)?;
let mut token_auth = auth::v3websso::get_token_auth(&mut url).await?;
// Set retrieved token as current auth
self.set_auth(auth::Auth::AuthToken(Box::new(token_auth.clone())), true);
// Get the token info (for the expiration)
let token_info = self.fetch_token_info(token_auth.token.clone()).await?;
token_auth.auth_info = Some(token_info.clone());
let scope = authtoken::AuthTokenScope::from(&token_info);
// Save unscoped token in the cache
self.state.set_scope_auth(&scope, &token_auth);
// And now time to rescope the token
let auth_ep =
authtoken::build_reauth_request(&token_auth, &requested_scope)?;
rsp = auth_ep.raw_query_async(self).await?;
}
#[cfg(feature = "keystone_ng")]
AuthType::V4Federation => {
// Construct request for initializing authentication (POST call to keystone
// `/federation/identity_providers/{idp_id}/auth`) to get the IDP url
// client would need to contact.
// TODO: If we know the scope we can request it from the very beginning
// saving 1 call.
let callback_addr = std::net::SocketAddr::from(([127, 0, 0, 1], 8050));
let init_auth_ep =
auth::v4federation::get_auth_ep(&self.config, callback_addr.port())?;
let auth_info: auth::v4federation::FederationAuthRequestResponse =
init_auth_ep.query_async(self).await?;
// Perform the magic directing user's browser at the IDP url and waiting
// for the callback to be invoked with the authorization code
let oauth2_code =
auth::v4federation::get_auth_code(&auth_info.auth_url, callback_addr)
.await?;
// Construct the request to Keystone to finish the authorization exchanging
// received authorization code for the (unscoped) token
let mut oidc_callback_builder =
auth::v4federation::OauthCallbackRequestBuilder::default();
if let (Some(code), Some(state)) = (oauth2_code.code, oauth2_code.state) {
oidc_callback_builder.code(code.clone());
oidc_callback_builder.state(state.clone());
let oidc_callback_ep = oidc_callback_builder
.build()
.map_err(auth::v4federation::FederationError::from)?;
rsp = oidc_callback_ep.raw_query_async(self).await?;
} else {
return Err(OpenStackError::NoAuth);
}
}
#[cfg(feature = "keystone_ng")]
AuthType::V4Jwt => {
let auth_ep = auth::v4jwt::get_auth_ep(&self.config, auth_helper).await?;
rsp = auth_ep.raw_query_async(self).await?;
api::check_response_error::<Self>(&rsp, None)?;
let token_info: AuthResponse = serde_json::from_slice(rsp.body())?;
let received_scope = authtoken::AuthTokenScope::from(&token_info);
tracing::debug!(
"Requested: {:?}, received: {:?}",
requested_scope,
received_scope
);
let token = rsp
.headers()
.get("x-subject-token")
.ok_or(AuthError::AuthTokenNotInResponse)?
.to_str()
.map_err(|_| AuthError::AuthTokenNotString)?;
// Set retrieved token as current auth
let token_auth = authtoken::AuthToken {
token: token.to_string(),
auth_info: Some(token_info),
};
self.set_auth(Auth::AuthToken(Box::new(token_auth.clone())), false);
if requested_scope != authtoken::AuthTokenScope::Unscoped
&& requested_scope != received_scope
{
// TODO: check if rescope is necessary and use v4 api
// And now time to rescope the token
let auth_ep =
authtoken::build_reauth_request(&token_auth, &requested_scope)?;
rsp = auth_ep.raw_query_async(self).await?;
} else {
// Client may not specify the target scope expecting the mapping to set
// the proper token. Save the auth as unscope (similarly to the AppCred
// handling).
self.state
.set_scope_auth(&authtoken::AuthTokenScope::Unscoped, &token_auth);
}
}
#[cfg(feature = "passkey")]
AuthType::V4Passkey => {
let auth_ep =
auth::v4passkey::get_init_auth_ep(&self.config, auth_helper).await?;
let req: auth::v4passkey::PasskeyAuthenticationStartResponse =
auth_ep.query_async(self).await?;
use webauthn_authenticator_rs::prelude::Url;
use webauthn_authenticator_rs::WebauthnAuthenticator;
let mut auth = WebauthnAuthenticator::new(
webauthn_authenticator_rs::mozilla::MozillaAuthenticator::new(),
);
let passkey_auth = auth
.do_authentication(
Url::parse("http://localhost:8080")?,
req.try_into()?,
)
.map_err(auth::v4passkey::PasskeyError::from)?;
let finish_ep = auth::v4passkey::get_finish_auth_ep(
&self.config,
passkey_auth,
auth_helper,
)
.await?;
rsp = finish_ep.raw_query_async(self).await?;
let token = rsp
.headers()
.get("x-subject-token")
.ok_or(AuthError::AuthTokenNotInResponse)?
.to_str()
.map_err(|_| AuthError::AuthTokenNotString)?;
// Set retrieved token as current auth
let token_info: AuthResponse = serde_json::from_slice(rsp.body())?;
let token_auth = authtoken::AuthToken {
token: token.to_string(),
auth_info: Some(token_info),
};
self.set_auth(Auth::AuthToken(Box::new(token_auth.clone())), false);
// And now time to rescope the token
let auth_ep =
authtoken::build_reauth_request(&token_auth, &requested_scope)?;
rsp = auth_ep.raw_query_async(self).await?;
}
}
};
let data: AuthResponse = serde_json::from_slice(rsp.body())?;
debug!("Auth token is {:?}", data);
let token = rsp
.headers()
.get("x-subject-token")
.ok_or(AuthError::AuthTokenNotInResponse)?
.to_str()
.map_err(|_| AuthError::AuthTokenNotString)?;
self.set_token_auth(token.into(), Some(data));
}
if let auth::Auth::AuthToken(token_data) = &self.auth {
match &token_data.auth_info {
Some(auth_data) => {
if let Some(project) = &auth_data.token.project {
self.catalog.set_project_id(project.id.clone());
// Reconfigure catalog since we know now the project_id
self.catalog.configure(&self.config)?;
}
if let Some(endpoints) = &auth_data.token.catalog {
self.catalog
.process_catalog_endpoints(endpoints, Some("public"))?;
} else {
error!("No catalog information");
}
}
_ => return Err(OpenStackError::NoAuth),
}
}
// TODO: without AuthToken authorization we may want to read catalog separately
Ok(())
}
/// Perform version discovery of a service
#[instrument(skip(self))]
pub async fn discover_service_endpoint(
&mut self,
service_type: &ServiceType,
) -> Result<(), OpenStackError> {
if let Ok(ep) = self.catalog.get_service_endpoint(
service_type.to_string(),
None,
self.config.region_name.as_ref(),
) {
if self.catalog.discovery_allowed(service_type.to_string()) {
info!("Performing `{}` endpoint version discovery", service_type);
let orig_url = ep.url().clone();
let mut try_url = ep.url().clone();
// Version discovery document must logically end with "/" since API url goes even
// deeper.
try_url
.path_segments_mut()
.map_err(|_| CatalogError::cannot_be_base(ep.url()))?
.pop_if_empty()
.push("");
let mut max_depth = 10;
loop {
let req = http::Request::builder()
.method(http::Method::GET)
.uri(query::url_to_http_uri(try_url.clone())?);
match self.rest_with_auth_async(req, Vec::new(), &self.auth).await {
Ok(rsp) => {
if rsp.status() != StatusCode::NOT_FOUND
&& self
.catalog
.process_endpoint_discovery(
service_type,
&try_url,
rsp.body(),
self.config.region_name.as_ref(),
)
.is_ok()
{
debug!(
"Finished service version discovery at {}",
try_url.as_str()
);
return Ok(());
}
}
Err(err) => {
error!(
"Error querying {} for the version discovery. It is most likely a misconfiguration on the cloud side. {}",
try_url.as_str(),
err
);
}
};
if try_url.path() != "/" {
// We are not at the root yet and have not found a
// valid version document so far, try one level up
try_url
.path_segments_mut()
.map_err(|_| CatalogError::cannot_be_base(&orig_url))?
.pop();
} else {
return Err(OpenStackError::Discovery {
service: service_type.to_string(),
url: orig_url.into(),
msg: match service_type {
ServiceType::Identity => "Service is not working.".into(),
_ => "No Version document found. Either service is not supporting version discovery, or API is not working".into(),
}
});
}
max_depth -= 1;
if max_depth == 0 {
break;
}
}
return Err(OpenStackError::Discovery {
service: service_type.to_string(),
url: orig_url.into(),
msg: "Unknown".into(),
});
}
return Ok(());
}
Ok(())
}
// TODO(gtema): rename to `get_catalog`)
/// Return catalog information given in the token
pub fn get_token_catalog(&self) -> Option<Vec<ServiceEndpoints>> {
self.catalog.get_token_catalog()
}
/// Return current authentication information
pub fn get_auth_info(&self) -> Option<AuthResponse> {
if let Auth::AuthToken(token) = &self.auth {
return token.auth_info.clone();
}
None
}
/// Return current authentication status
///
/// Offset can be used to calculate imminent expiration.
pub fn get_auth_state(&self, offset: Option<TimeDelta>) -> Option<AuthState> {
if let Auth::AuthToken(token) = &self.auth {
return Some(token.get_state(offset));
}
None
}
/// Return current authentication token
pub fn get_auth_token(&self) -> Option<String> {
if let Auth::AuthToken(token) = &self.auth {
return Some(token.token.clone());
}
None
}
/// Perform token introspection call
pub async fn fetch_token_info<S: AsRef<str>>(
&self,
token: S,
) -> Result<AuthResponse, OpenStackError> {
let auth_ep = auth::authtoken::build_token_info_endpoint(token)?;
let rsp = auth_ep.raw_query_async(self).await?;
let data: AuthResponse = serde_json::from_slice(rsp.body())?;
Ok(data)
}
/// Perform HTTP request with given request and return raw response.
#[instrument(name="request", skip_all, fields(http.uri = request.url().as_str(), http.method = request.method().as_str(), openstack.ver=request.headers().get("openstack-api-version").map(|v| v.to_str().unwrap_or(""))))]
async fn execute_request(&self, request: Request) -> Result<Response, reqwest::Error> {
info!("Sending request {:?}", request);
let url = request.url().clone();
let method = request.method().clone();
if enabled!(Level::TRACE)
&& request.headers().get(header::CONTENT_TYPE)
== Some(&HeaderValue::from_static("application/json"))
{
// Body may contain sensitive info - censor it but only when trace is on
request
.body()
.and_then(|body| body.as_bytes())
.and_then(|bytes| String::from_utf8(bytes.to_vec()).ok())
.inspect(|rq| {
let censored = self
.config
.get_sensitive_values()
.iter()
.fold(rq.clone(), |sanitized, &secret| {
sanitized.replace(secret, "<CENSORED>")
});
trace!("Request Body: {:?}", censored);
});
}
let start = SystemTime::now();
let rsp = self.client.execute(request).await?;
let elapsed = SystemTime::now().duration_since(start).unwrap_or_default();
event!(
name: "http_request",
Level::INFO,
url=url.as_str(),
duration_ms=elapsed.as_millis(),
status=rsp.status().as_u16(),
method=method.as_str(),
request_id=rsp.headers().get("x-openstack-request-id").map(|v| v.to_str().unwrap_or("")),
"Request completed with status {}",
rsp.status(),
);
Ok(rsp)
}
/// Perform a REST query with a given auth.
async fn rest_with_auth_async(
&self,
mut request: http::request::Builder,
body: Vec<u8>,
auth: &Auth,
) -> Result<HttpResponse<Bytes>, api::ApiError<<Self as api::RestClient>::Error>> {
use futures_util::TryFutureExt;
let call = || async {
if let Some(headers) = request.headers_mut() {
auth.set_header(headers)?;
}
let http_request = request.body(body)?;
let request = http_request.try_into()?;
let rsp = self.execute_request(request).await?;
let mut http_rsp = HttpResponse::builder()
.status(rsp.status())
.version(rsp.version());
if let Some(headers) = http_rsp.headers_mut() {
headers.extend(rsp.headers().clone())
}
Ok(http_rsp.body(rsp.bytes().await?)?)
};
call().map_err(api::ApiError::client).await
}
/// Perform a REST query with a given auth.
async fn rest_with_auth_read_body_async(
&self,
mut request: http::request::Builder,
body_read: BoxedAsyncRead,
auth: &Auth,
) -> Result<HttpResponse<Bytes>, api::ApiError<<Self as api::RestClient>::Error>> {
use futures_util::TryFutureExt;
let call = || async {
if let Some(headers) = request.headers_mut() {
auth.set_header(headers)?;
}
let stream = codec::FramedRead::new(body_read.compat(), codec::BytesCodec::new())
.map_ok(|b| b.freeze());
let http_request = request.body(Body::wrap_stream(stream))?;
let request = http_request.try_into()?;
let rsp = self.execute_request(request).await?;
let mut http_rsp = HttpResponse::builder()
.status(rsp.status())
.version(rsp.version());
if let Some(headers) = http_rsp.headers_mut() {
headers.extend(rsp.headers().clone())
}
Ok(http_rsp.body(rsp.bytes().await?)?)
};
call().map_err(api::ApiError::client).await
}
/// Perform a REST query with a given auth and return AsyncRead of the body.
async fn download_with_auth_async(
&self,
mut request: http::request::Builder,
body: Vec<u8>,
auth: &Auth,
) -> Result<(HeaderMap, BoxedAsyncRead), api::ApiError<<Self as api::RestClient>::Error>> {
use futures_util::TryFutureExt;
let call = || async {
if let Some(headers) = request.headers_mut() {
auth.set_header(headers)?;
}
let http_request = request.body(body)?;
let request = http_request.try_into()?;
let rsp = self.execute_request(request).await?;
let mut headers = HeaderMap::new();
for (key, value) in rsp.headers() {
headers.insert(key, value.clone());
}
let boxed_async_read = BoxedAsyncRead::new(
rsp.bytes_stream()
.map_err(|orig| {
let kind = if orig.is_timeout() {
IoErrorKind::TimedOut
} else {
IoErrorKind::Other
};
IoError::new(kind, orig)
})
.into_async_read(),
);
Ok((headers, boxed_async_read))
};
call().map_err(api::ApiError::client).await
}
}