forked from gtema/openstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenstack_async.rs
More file actions
2445 lines (2198 loc) · 95.8 KB
/
Copy pathopenstack_async.rs
File metadata and controls
2445 lines (2198 loc) · 95.8 KB
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
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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 std::collections::HashMap;
use std::convert::TryInto;
use std::fmt::{self, Debug};
use std::ops::{Deref, DerefMut};
use std::sync::{Arc, Weak};
use std::time::{Duration, SystemTime};
use std::{fs::File, io::Read};
use arc_swap::ArcSwap;
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::{HeaderMap, HeaderValue, Response as HttpResponse, StatusCode, header};
use parking_lot::RwLock;
use reqwest::{Certificate, Request, Response};
use secrecy::{ExposeSecret, SecretString};
use tokio_util::codec;
use tokio_util::compat::FuturesAsyncReadCompatExt;
use tracing::{Level, debug, enabled, error, event, info, instrument, trace, warn};
use openstack_sdk_auth_core::{
Auth, AuthError, AuthPluginRegistration, AuthToken, OpenStackAuthType,
authtoken::AuthTokenError,
authtoken_scope::AuthTokenScope,
types::{AuthResponse, Project, ServiceEndpoints},
};
// Private use of auth plugins for token refresh and receipt handling.
use openstack_sdk_auth_receipt as token_receipt;
use openstack_sdk_auth_token as token_auth;
use crate::auth::authtoken::{AuthType, build_token_info_endpoint};
use crate::session;
use openstack_sdk_core::api::{self, AsyncClient, QueryAsync, query, raw};
use openstack_sdk_core::auth::{
AuthState,
auth_helper::{AuthHelper, Dialoguer, Noop},
gather_auth_data,
};
use openstack_sdk_core::catalog::{Catalog, CatalogError, ServiceEndpoint};
use openstack_sdk_core::config::CloudConfig;
use openstack_sdk_core::error::{OpenStackError, OpenStackResult, RestError};
use openstack_sdk_core::session::AuthSnapshot;
use openstack_sdk_core::types::{ApiVersion, BoxedAsyncRead, ServiceType};
use openstack_sdk_core::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_20;
///
/// async fn list_flavors() -> Result<(), OpenStackError> {
/// // Get the builder for the listing Flavors Endpoint
/// let mut ep_builder = list_20::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(())
/// }
/// ```
pub struct AsyncOpenStack {
/// The client to use for API calls.
client: reqwest::Client,
/// Cloud configuration
config: CloudConfig,
/// Session context (auth, catalog, state)
session: Arc<RwLock<session::SessionContext>>,
/// Lock-free snapshot of auth/catalog/region for the per-request hot
/// path, republished by `SessionWriteGuard::drop` on every session write.
auth_snapshot: Arc<ArcSwap<AuthSnapshot>>,
/// Auth helper for re-authentication on 401.
auth_helper: Option<Arc<dyn AuthHelper>>,
/// Max retries on 401.
max_auth_retries: u32,
/// Single-flight guard for re-authentication, shared across all clones.
/// Held for the duration of `handle_401_retry` so concurrent 401s queue
/// on one re-auth instead of each running their own. Also intended to be
/// shared with any future proactive token-renewal task.
reauth_lock: Arc<tokio::sync::Mutex<()>>,
}
impl Clone for AsyncOpenStack {
fn clone(&self) -> Self {
Self {
client: self.client.clone(),
config: self.config.clone(),
session: Arc::clone(&self.session),
auth_snapshot: Arc::clone(&self.auth_snapshot),
auth_helper: self.auth_helper.clone(),
max_auth_retries: self.max_auth_retries,
reauth_lock: Arc::clone(&self.reauth_lock),
}
}
}
impl Debug for AsyncOpenStack {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("OpenStack")
.field("service_endpoints", &self.auth_snapshot.load().catalog)
.finish()
}
}
#[async_trait]
impl api::RestClient for AsyncOpenStack {
type Error = RestError;
fn get_current_project(&self) -> Option<Project> {
let session = self.session.read();
if let Auth::AuthToken(token) = &session.auth {
return token.auth_info.clone().and_then(|x| x.token.project);
}
None
}
}
#[async_trait]
impl api::AsyncClient for AsyncOpenStack {
async fn rest_async(
&self,
request: http::request::Builder,
body: Vec<u8>,
) -> Result<HttpResponse<Bytes>, api::ApiError<<Self as api::RestClient>::Error>> {
let (mut auth, mut generation) = {
let snap = self.auth_snapshot.load();
(snap.auth.clone(), snap.auth_generation)
};
let mut retries = 0;
let orig_method = request.method_ref().cloned().unwrap_or(http::Method::GET);
let orig_uri = request.uri_ref().cloned().unwrap_or_default();
let orig_headers = request.headers_ref().cloned().unwrap_or_default();
let mut request = Some(request);
loop {
let current_request = match request.take() {
Some(r) => r,
None => unreachable!("request missing after retry setup"),
};
let result = self
.rest_with_auth_async(current_request, body.clone(), &auth)
.await;
let is_401 = match &result {
Ok(rsp) => rsp.status() == http::StatusCode::UNAUTHORIZED,
Err(_) => false,
};
if is_401 && retries < self.max_auth_retries {
warn!(
"Received 401 Unauthorized, retry {} of {}",
retries + 1,
self.max_auth_retries
);
match self.handle_401_retry(generation).await {
Ok(_) => {
(auth, generation) = {
let snap = self.auth_snapshot.load();
(snap.auth.clone(), snap.auth_generation)
};
retries += 1;
let mut new_builder = http::Request::builder()
.method(orig_method.clone())
.uri(orig_uri.clone());
for (name, value) in &orig_headers {
new_builder = new_builder.header(name.clone(), value.clone());
}
request = Some(new_builder);
continue;
}
Err(e) => {
warn!("401 retry failed: {}", e);
return result;
}
}
}
return result;
}
}
async fn rest_read_body_async(
&self,
request: http::request::Builder,
body: BoxedAsyncRead,
) -> Result<HttpResponse<Bytes>, api::ApiError<<Self as api::RestClient>::Error>> {
let auth = self.auth_snapshot.load().auth.clone();
self.rest_with_auth_read_body_async(request, body, &auth)
.await
}
async fn download_async(
&self,
request: http::request::Builder,
body: Vec<u8>,
) -> Result<(HeaderMap, BoxedAsyncRead), api::ApiError<<Self as api::RestClient>::Error>> {
let auth = self.auth_snapshot.load().auth.clone();
self.download_with_auth_async(request, body, &auth).await
}
async fn get_service_endpoint(
&self,
service_type: &ServiceType,
version: Option<&ApiVersion>,
) -> Result<ServiceEndpoint, api::ApiError<Self::Error>> {
let lookup = || {
let snap = self.auth_snapshot.load();
let region = snap
.region_name
.as_ref()
.or(self.config.region_name.as_ref());
snap.catalog
.get_service_endpoint(
service_type.to_string(),
version,
region,
self.config.interface.as_ref(),
)
.cloned()
.map_err(api::ApiError::catalog)
};
let ep = match lookup() {
Ok(ep) => return Ok(ep),
Err(api::ApiError::Catalog { source }) => {
if matches!(source, CatalogError::ServiceNotConfigured { .. }) {
info!(
"Service '{}' not in catalog, attempting refresh via auth/catalog",
service_type
);
if let Err(e) = self.refresh_catalog().await {
warn!("Catalog refresh failed: {}", e);
}
match lookup() {
Ok(ep) => {
debug!("Service '{}' found after catalog refresh", service_type);
self.persist_catalog_to_cache();
ep
}
Err(e) => return Err(e),
}
} else {
return Err(api::ApiError::Catalog { source });
}
}
Err(e) => return Err(e),
};
Ok(ep)
}
}
/// Write guard for `SessionContext` that republishes `AuthSnapshot` on drop,
/// while the write lock is still held (the manual `Drop::drop` below runs
/// before the `guard` field's own destructor, so the store happens with the
/// lock still taken). This guarantees a concurrent hot-path reader never
/// sees a released lock with a stale snapshot: every write path that
/// mutates `auth`, `auth_generation`, `catalog`, or `region_name` is
/// automatically reflected, with no per-call-site bookkeeping required.
///
/// Trade-off: writes that only touch `state` (auth-cache bookkeeping, e.g.
/// `get_scope_auth`/`clear_all_auth`) still pay a full `AuthSnapshot`
/// rebuild — including a `Catalog` clone — since the guard can't tell which
/// fields changed. These are auth-flow writes, not per-request, so the cost
/// is bounded; revisit if `Catalog` grows large enough to matter.
struct SessionWriteGuard<'a> {
guard: parking_lot::RwLockWriteGuard<'a, session::SessionContext>,
snapshot: &'a ArcSwap<AuthSnapshot>,
}
impl Deref for SessionWriteGuard<'_> {
type Target = session::SessionContext;
fn deref(&self) -> &Self::Target {
&self.guard
}
}
impl DerefMut for SessionWriteGuard<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.guard
}
}
impl Drop for SessionWriteGuard<'_> {
fn drop(&mut self) {
self.snapshot
.store(Arc::new(AuthSnapshot::from(&*self.guard)));
}
}
/// Handle to a background token-renewal task started by
/// [`AsyncOpenStack::enable_auto_renew`]. Dropping it stops the task; the
/// task also stops on its own once every `AsyncOpenStack` sharing the
/// session it was spawned for has been dropped.
pub struct RenewHandle(tokio::task::JoinHandle<()>);
impl Drop for RenewHandle {
fn drop(&mut self) {
self.0.abort();
}
}
impl AsyncOpenStack {
/// Lock the session for reading. `location` is kept as a parameter for
/// call-site self-documentation (parking_lot never poisons, so it no
/// longer feeds an error message).
#[allow(unused_variables)]
fn session_read(
&self,
location: &'static str,
) -> parking_lot::RwLockReadGuard<'_, session::SessionContext> {
self.session.read()
}
/// Lock the session for writing. Returns a guard that republishes
/// `AuthSnapshot` on drop — see `SessionWriteGuard`.
#[allow(unused_variables)]
fn session_write(&self, location: &'static str) -> SessionWriteGuard<'_> {
SessionWriteGuard {
guard: self.session.write(),
snapshot: &self.auth_snapshot,
}
}
/// Basic constructor — visible to the sync facade.
pub fn new_impl(config: &CloudConfig, auth: Auth) -> OpenStackResult<Self> {
// Ensure auth plugin registries are populated even if the linker
// would otherwise strip crates with no other references.
openstack_sdk_auth_core::anchor_plugins();
let mut client_builder = reqwest::Client::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);
// Pass CloudConfig.auth_cache as override; SessionContext resolves priority chain.
let session_ctx = session::SessionContext::new(config, auth, config.auth_cache)?;
let auth_snapshot = Arc::new(ArcSwap::new(Arc::new(AuthSnapshot::from(&session_ctx))));
Ok(AsyncOpenStack {
client: client_builder.build()?,
config: config.clone(),
session: Arc::new(RwLock::new(session_ctx)),
auth_snapshot,
auth_helper: None,
max_auth_retries: 1,
reauth_lock: Arc::new(tokio::sync::Mutex::new(())),
})
}
/// Create a new OpenStack API session from CloudConfig
#[instrument(name = "connect", level = "trace", skip(config))]
pub async fn new(config: &CloudConfig) -> OpenStackResult<Self> {
let 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<A>(
config: &CloudConfig,
auth_helper: A,
renew_auth: bool,
) -> OpenStackResult<Self>
where
A: AuthHelper + Sync + Send + 'static,
{
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?;
session.auth_helper = Some(Arc::new(auth_helper));
Ok(session)
}
/// Create a new OpenStack API session from `CloudConfig`, loading any
/// already-cached authorization but never performing network I/O to
/// discover endpoints or obtain a fresh token.
///
/// Intended for commands that only need to *report* on locally known
/// auth state (e.g. `osc auth status`) and must keep working even when
/// the cloud is unreachable or the cached token has expired.
#[instrument(name = "connect_cache_only", level = "trace", skip(config))]
pub fn new_cache_only(config: &CloudConfig) -> OpenStackResult<Self> {
let session = Self::new_impl(config, Auth::None)?;
let requested_scope = AuthTokenScope::try_from(&session.config).ok();
let cached = {
let mut inner = session.session_write("new_cache_only: get cache auth");
match &requested_scope {
Some(scope) => inner.state.get_scope_auth(scope),
None => inner.state.get_any_valid_auth(),
}
};
if let Some(auth) = cached {
session.set_auth(Auth::AuthToken(Box::new(auth)), true)?;
}
Ok(session)
}
#[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, Dialoguer::default(), renew_auth).await
}
/// Set the authorization to be used by the client
fn set_auth(&self, auth: Auth, skip_cache_update: bool) -> Result<(), OpenStackError> {
let mut session = self.session_write("set_auth");
session.auth = auth;
session.auth_generation = session.auth_generation.wrapping_add(1);
if !skip_cache_update && let Auth::AuthToken(a) = &session.auth {
let a_clone = a.clone();
let scope = match &a_clone.auth_info {
Some(info) => {
if info.token.application_credential.is_some() {
AuthTokenScope::Unscoped
} else {
a_clone.get_scope()
}
}
_ => a_clone.get_scope(),
};
session.state.set_scope_auth(&scope, &a_clone);
}
Ok(())
}
/// Authorize against the cloud using provided credentials and get the session token.
pub async fn authorize(
&self,
scope: Option<AuthTokenScope>,
interactive: bool,
renew_auth: bool,
) -> Result<(), OpenStackError> {
if interactive {
self.authorize_with_auth_helper(scope, &Dialoguer::default(), renew_auth)
.await
} else {
self.authorize_with_auth_helper(scope, &Noop::default(), renew_auth)
.await
}
}
/// Re-authenticate with the existing auth for the given scope.
async fn reauth(
&self,
auth: &AuthToken,
scope: &AuthTokenScope,
) -> Result<Auth, OpenStackError> {
Ok(token_auth::PLUGIN
.auth(
&self.client,
self.get_service_endpoint(&ServiceType::Identity, Some(&ApiVersion::from((3, 0))))
.await?
.url(),
&HashMap::from([("token".into(), auth.token.clone())]),
Some(scope),
None,
)
.await?)
}
/// Handle 401 Unauthorized by re-authenticating.
///
/// `seen_generation` is the `auth_generation` observed by the caller
/// before it hit the 401. Re-auth is single-flighted via `reauth_lock`:
/// if another concurrent caller already completed a re-auth while this
/// one was waiting for the lock, `auth_generation` will have moved on
/// and this call skips its own re-auth.
async fn handle_401_retry(&self, seen_generation: u64) -> Result<(), OpenStackError> {
let _guard = self.reauth_lock.lock().await;
let current_generation = self.auth_snapshot.load().auth_generation;
if current_generation != seen_generation {
// Another task already re-authed while we waited for the lock.
return Ok(());
}
let scope = AuthTokenScope::try_from(&self.config)?;
// Re-authenticate with current config. First try to reauth with existing token,
// fall back to full authentication if auth_helper is available.
let auth_helper_opt = self.auth_helper.clone();
if let Some(auth_helper) = auth_helper_opt {
{
let mut session = self.session_write("handle_401_retry: clear state");
session.state.clear_all_auth();
}
self.authorize_with_auth_helper(Some(scope), &auth_helper, true)
.await?;
} else {
// No auth helper available - try to reauth with the current token
if let Auth::AuthToken(token) = {
let session = self.session_read("handle_401_retry: read auth");
session.auth.clone()
} && let Ok(new_auth) = self.reauth(&token, &scope).await
{
self.set_auth(new_auth, false)?;
return Ok(());
}
// Reauth failed, clear state and try full auth as fallback
{
let mut session = self.session_write("handle_401_retry: clear state fallback");
session.state.clear_all_auth();
}
self.authorize_with_auth_helper(Some(scope), &Noop::default(), true)
.await?;
}
Ok(())
}
/// Disable authentication caching for this session.
///
/// Clears any cached tokens and prevents further caching — both the
/// in-memory store and the on-disk state file. Useful for scenarios
/// requiring fresh authentication on every request.
pub fn disable_auth_cache(&self) -> &Self {
self.session.write().state.disable_auth_cache();
self
}
/// Spawn a background task that proactively re-authenticates `margin`
/// before the current token expires, so the request hot path rarely
/// hits a 401 to begin with.
///
/// This performs a full, non-interactive re-authentication — *not*
/// token-exchange (`Self::reauth`, method=`token`). Keystone bakes the
/// original token's expiry into any token minted via token-exchange
/// (`keystone/auth/plugins/token.py`: `response_data.setdefault(
/// 'expires_at', token.expires_at)`, deliberately, to stop callers from
/// bumping expiry without reproving identity), so exchanging a token
/// for a token never actually extends the session — it would produce a
/// new token id with the exact same expiry as the one it replaced.
/// Reproving identity is required, so this clears cached auth state and
/// replays the credentials in `CloudConfig` (password / application
/// credential / client credentials) through [`Self::authorize_with_auth_helper`]
/// with [`Noop`] — deliberately never the configured interactive
/// `auth_helper`, since a background task cannot block on a prompt.
/// Clouds that need MFA/SSO/interactive input therefore don't renew
/// here; they keep relying on the interactive 401 retry path when a
/// user is actually present.
///
/// Re-authentication is single-flighted with the 401 retry path through
/// `reauth_lock`, so a concurrent 401 and a renewal wakeup never both
/// re-authenticate at once.
///
/// The task exits once it observes there is no `Auth::AuthToken` to
/// renew (unauthenticated session), once the last `AsyncOpenStack`
/// sharing this session is dropped, or when the returned handle is
/// dropped.
pub fn enable_auto_renew(&self, margin: TimeDelta) -> RenewHandle {
// Only `session` is held weakly — it's the field whose refcount
// marks "is any AsyncOpenStack for this session still alive".
// Everything else is cloned strong: none of it keeps `session`
// alive, and the task needs it to build a short-lived client for
// each wake-up without holding a strong `session` ref across the
// (possibly hours-long) sleep in between.
let weak_session: Weak<RwLock<session::SessionContext>> = Arc::downgrade(&self.session);
let reqwest_client = self.client.clone();
let config = self.config.clone();
let auth_snapshot = Arc::clone(&self.auth_snapshot);
let auth_helper = self.auth_helper.clone();
let max_auth_retries = self.max_auth_retries;
let reauth_lock = Arc::clone(&self.reauth_lock);
let join = tokio::spawn(async move {
// Rebuilds a client around the currently-live `session` Arc, or
// `None` once the last real `AsyncOpenStack` has been dropped.
let upgrade = || {
weak_session.upgrade().map(|session| AsyncOpenStack {
client: reqwest_client.clone(),
config: config.clone(),
session,
auth_snapshot: Arc::clone(&auth_snapshot),
auth_helper: auth_helper.clone(),
max_auth_retries,
reauth_lock: Arc::clone(&reauth_lock),
})
};
loop {
let sleep_for = {
let Some(client) = upgrade() else { return };
let Auth::AuthToken(token) = &client.auth_snapshot.load().auth else {
return;
};
let Some(auth_info) = &token.auth_info else {
return;
};
(auth_info.token.expires_at - margin - chrono::Utc::now())
.to_std()
.unwrap_or(Duration::ZERO)
// `client` (and its strong `session` ref) drops here,
// before the sleep below.
};
tokio::time::sleep(sleep_for).await;
let Some(client) = upgrade() else { return };
let seen_generation = client.auth_snapshot.load().auth_generation;
let _guard = client.reauth_lock.lock().await;
if client.auth_snapshot.load().auth_generation != seen_generation {
// Another task (401 retry or a previous renewal wakeup)
// already refreshed the token while we waited.
continue;
}
let scope = match AuthTokenScope::try_from(&client.config) {
Ok(s) => s,
Err(e) => {
warn!("background token renewal: cannot determine scope: {e}");
return;
}
};
// `authorize_with_auth_helper` re-processes the token
// catalog, which wipes all previously-discovered service
// endpoint versions (identity excepted) since a rescope may
// land on a different project_id. This renewal keeps the
// same scope, so remember what was discovered and redo it
// below rather than silently losing it.
let discovered_services = {
let session =
client.session_read("enable_auto_renew: snapshot discovered services");
session.catalog.discovered_service_types()
};
// Clear cached auth first: `authorize_with_auth_helper`
// would otherwise happily rescope a still-cached token via
// token-exchange, which (see doc comment above) preserves
// the old expiry instead of extending it. Clearing forces
// it down the real-credential path.
{
let mut session = client.session_write("enable_auto_renew: clear state");
session.state.clear_all_auth();
}
match client
.authorize_with_auth_helper(Some(scope), &Noop::default(), true)
.await
{
Ok(()) => {
for service_type in &discovered_services {
if let Err(e) = client
.discover_service_endpoint(&ServiceType::from(
service_type.as_str(),
))
.await
{
warn!(
"background token renewal: failed to re-discover endpoint for {service_type}: {e}"
);
}
}
let expires_at =
if let Auth::AuthToken(t) = &client.auth_snapshot.load().auth {
t.auth_info
.as_ref()
.map(|i| i.token.expires_at.to_rfc3339())
} else {
None
};
event!(
name: "token_renewed",
Level::DEBUG,
expires_at,
"Background token renewal succeeded"
)
}
Err(e) => {
warn!("background token renewal failed: {e}");
// Rate-limit retries; a subsequent 401 still falls
// back to the normal retry path in the meantime.
tokio::time::sleep(Duration::from_secs(30)).await;
}
}
}
});
RenewHandle(join)
}
/// Set the maximum number of retries on 401 responses.
pub fn set_max_auth_retries(&mut self, n: u32) -> &mut Self {
self.max_auth_retries = n;
self
}
/// Set the auth helper used for 401 re-authentication.
pub fn set_auth_helper<A>(&mut self, auth_helper: A)
where
A: AuthHelper + Sync + Send + 'static,
{
self.auth_helper = Some(Arc::new(auth_helper));
}
/// 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>(
&self,
scope: Option<AuthTokenScope>,
auth_helper: &A,
renew_auth: bool,
) -> Result<(), OpenStackError>
where
A: AuthHelper + Sync + Send + 'static,
{
let requested_scope =
scope.map_or_else(|| AuthTokenScope::try_from(&self.config), |v| Ok(v.clone()))?;
let auth_opt = {
let mut session = self.session_write("authorize_with_auth_helper: get cache auth");
session.state.get_scope_auth(&requested_scope)
};
if let Some(auth) = auth_opt
&& !renew_auth
{
// Valid authorization is already available and no renewal is required
trace!("Auth already available");
self.set_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 force_new_auth = matches!(auth_type, AuthType::V3ApplicationCredential);
let available_auth_opt = {
let mut session = self.session_write("authorize_with_auth_helper: reauthz");
session.state.get_any_valid_auth()
};
if let (Some(available_auth), false) = (available_auth_opt, 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 token_auth = self.reauth(&available_auth, &requested_scope).await?;
self.set_auth(token_auth.clone(), false)?;
} else {
// No auth/authz information available or force_new_auth. Proceed with new auth
trace!("No Auth already available. Proceeding with new login");
let auth_type = auth_type.as_str();
// Find authenticator supporting the auth_type
if let Some(authenticator) = inventory::iter::<AuthPluginRegistration>
.into_iter()
.find(|x| x.method.get_supported_auth_methods().contains(&auth_type))
.map(|x| x.method)
{
// authenticate
let auth_hints = self
.config
.auth_methods
.as_ref()
.map(|methods| serde_json::json!({"auth_methods": methods}));
match authenticator
.auth(
&self.client,
self.get_service_endpoint(
&ServiceType::Identity,
Some(&ApiVersion::from(authenticator.api_version())),
)
.await?
.url(),
&gather_auth_data(
&authenticator.requirements(auth_hints.as_ref())?,
&self.config,
auth_helper,
)
.await?,
Some(&requested_scope),
auth_hints.as_ref(),
)
.await
{
Ok(token_auth) => {
self.set_auth(token_auth.clone(), false)?;
}
Err(AuthError::AuthReceipt(receipt)) => {
// Auth Receipt is received
// Find the receipt auth plugin
// Convert the receipt into auth hints
let auth_hints = serde_json::to_value(&receipt)?;
// Authenticate
let token_auth = token_receipt::PLUGIN
.auth(
&self.client,
self.get_service_endpoint(
&ServiceType::Identity,
Some(&ApiVersion::from(authenticator.api_version())),
)
.await?
.url(),
&gather_auth_data(
&token_receipt::PLUGIN.requirements(Some(&auth_hints))?,
&self.config,
auth_helper,
)
.await?,
Some(&requested_scope),
Some(&auth_hints),
)
.await?;
self.set_auth(token_auth.clone(), false)?;
}
Err(other) => {
return Err(other.into());
}
}
} else {
Err(AuthTokenError::IdentityMethod {
auth_type: auth_type.into(),
})?;
}
}
}
let auth_opt = {
let session = self.session_read("authorize_with_auth_helper: get auth");
session.auth.clone()
};
if let Auth::AuthToken(token_auth) = &auth_opt {
// When unscoped auth is requested (maybe by not specifying the scope) we expect that
// the Authenticator returns the necessary scope (i.e. for ApplicationCredentials it is
// not possible to request scope, but the Keystone manages the scope) or unscoped.
// In this case we just save the auth as unscoped (in addition to what was done above).
// Otherwise we should rescope.
if token_auth.auth_info.is_none() {
let mut resolved_token = token_auth.clone();
let token_info = self.fetch_token_info(token_auth.token.clone()).await?;
resolved_token.auth_info = Some(token_info.clone());
let scope = AuthTokenScope::from(&token_info);
// Save unscoped token in the cache
{
let mut session =
self.session_write("authorize_with_auth_helper: set unscoped cache");
session.state.set_scope_auth(&scope, &resolved_token);
}
}
if requested_scope != AuthTokenScope::Unscoped
&& !token_auth
.auth_info
.as_ref()
.map(AuthTokenScope::from)
.is_some_and(|scope| requested_scope.matches(&scope))
{
// And now time to rescope the token
let token_auth = self.reauth(token_auth, &requested_scope).await?;
self.set_auth(token_auth.clone(), false)?;
} 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).
{
let mut session =
self.session_write("authorize_with_auth_helper: set unscoped");
session
.state
.set_scope_auth(&AuthTokenScope::Unscoped, token_auth);
}
}
} else {
Err(OpenStackError::NoAuth)?;
}
{
let mut session = self.session_write("authorize_with_auth_helper: process catalog");
let auth_data = match &session.auth {
Auth::AuthToken(token_data) => match &token_data.auth_info {
Some(ad) => ad.clone(),
_ => return Err(OpenStackError::NoAuth),
},
_ => return Err(OpenStackError::NoAuth),
};
if let Some(project) = &auth_data.token.project {
session.catalog.set_project_id(project.id.clone());
session.catalog.configure(&self.config)?;
}
if let Some(endpoints) = &auth_data.token.catalog {
session.catalog.process_catalog_endpoints(endpoints)?;
} else {
error!("No catalog information");
}
}
Ok(())
}
/// Perform version discovery of a service
#[instrument(skip(self))]
pub async fn discover_service_endpoint(
&self,
service_type: &ServiceType,
) -> Result<(), OpenStackError> {
let ep_opt = {
let session = self.session_read("discover_service_endpoint: get ep");
let region = session
.region_name
.as_ref()
.or(self.config.region_name.as_ref());
session
.catalog
.get_service_endpoint(
service_type.to_string(),
None,
region,
self.config.interface.as_ref(),
)
.ok()
.cloned()
};
let ep = match ep_opt {
Some(e) => e,
None => return Ok(()),