Skip to content

Commit 2269c74

Browse files
authored
Update the cache used by pinniped proxy to include the k8s api server url. (#5913)
Signed-off-by: Michael Nelson <[email protected]> <!-- Before you open the request please review the following guidelines and tips to help it be more easily integrated: - Describe the scope of your change - i.e. what the change does. - Describe any known limitations with your change. - Please run any tests or examples that can exercise your modified code. Thank you for contributing! --> ### Description of the change Fixes #5912 by including the k8s server url in the cache key. <!-- Describe the scope of your change - i.e. what the change does. --> ### Benefits <!-- What benefits will be realized by the code change? --> Fixes an issue that was causing logouts when switching clusters. ### Possible drawbacks <!-- Describe any known limitations with your change --> ### Applicable issues <!-- Enter any applicable Issues here (You can reference an issue using #) --> - fixes #5912 ### Additional information <!-- If there's anything else that's important and relevant to your pull request, mention that information here.--> Signed-off-by: Michael Nelson <[email protected]>
1 parent b4b347d commit 2269c74

File tree

1 file changed

+54
-11
lines changed

1 file changed

+54
-11
lines changed

cmd/pinniped-proxy/src/pinniped.rs

+54-11
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,10 @@ pub struct TokenCredentialRequestStatus {
115115
///
116116
/// The cache takes care of pruning expired tokens whenever a write lock is
117117
/// acquired to set a new value.
118-
pub type CredentialCache = Arc<PruningCache<TokenCredentialRequest, TokenCredentialRequest>>;
118+
/// The Cache key is a 2-tuple of the api endpoint url and the
119+
/// TokenCredentialRequest.
120+
pub type CredentialCache =
121+
Arc<PruningCache<(String, TokenCredentialRequest), TokenCredentialRequest>>;
119122

120123
/// Return a new CredentialCache.
121124
pub fn new_credential_cache() -> CredentialCache {
@@ -258,7 +261,7 @@ async fn prepare_and_call_pinniped_exchange(
258261

259262
// If the credential already exists in the cache (the cache handles expired
260263
// creds), then return that.
261-
match credential_cache.get(&cred_data) {
264+
match credential_cache.get(&(k8s_api_server_url.into(), cred_data.clone())) {
262265
Some(cached_cred) => Ok(cached_cred),
263266
None => {
264267
let client = get_client_config(
@@ -267,7 +270,7 @@ async fn prepare_and_call_pinniped_exchange(
267270
pinniped_namespace.clone(),
268271
)?;
269272
let cred = call_pinniped(pinniped_namespace, client, cred_data.clone()).await?;
270-
credential_cache.insert(cred_data, cred.clone());
273+
credential_cache.insert((k8s_api_server_url.into(), cred_data), cred.clone());
271274
Ok(cred)
272275
}
273276
}
@@ -690,14 +693,16 @@ mZu9A/ivt37pOQXm/HOX6tHB
690693
Ok(())
691694
}
692695

696+
const KUBEAPPS_LOCAL: &str = "https://kubeapps.local";
697+
693698
#[test]
694699
fn test_credential_cache_cannot_add_expired_token() {
695700
let cc = new_credential_cache();
696701

697702
let key_tcr = make_token_credential_request();
698703
assert!(key_tcr.is_expired());
699704

700-
cc.insert(key_tcr.clone(), key_tcr);
705+
cc.insert((KUBEAPPS_LOCAL.into(), key_tcr.clone()), key_tcr);
701706

702707
assert_eq!(cc.len(), 0);
703708
}
@@ -719,10 +724,45 @@ mZu9A/ivt37pOQXm/HOX6tHB
719724
});
720725
assert!(!val_tcr.is_expired());
721726

722-
cc.insert(key_tcr.clone(), val_tcr.clone());
727+
cc.insert((KUBEAPPS_LOCAL.into(), key_tcr.clone()), val_tcr.clone());
723728

724729
assert_eq!(cc.len(), 1);
725-
assert_eq!(cc.get(&key_tcr), Some(val_tcr));
730+
assert_eq!(cc.get(&(KUBEAPPS_LOCAL.into(), key_tcr)), Some(val_tcr));
731+
}
732+
733+
#[test]
734+
fn test_credential_cache_key_includes_api_server_url() {
735+
// More for documentation given that the key is whatever we define
736+
// it to be, but to document https://github.com/vmware-tanzu/kubeapps/issues/5912
737+
let cc = new_credential_cache();
738+
739+
let key_tcr = make_token_credential_request();
740+
let mut val_tcr = key_tcr.clone();
741+
val_tcr.status = Some(TokenCredentialRequestStatus {
742+
credential: Some(ClusterCredential {
743+
expiration_timestamp: metav1::Time(Utc::now() + Duration::days(1)),
744+
client_certificate_data: String::from("cert data"),
745+
client_key_data: String::from("key data"),
746+
token: Some(String::from("token")),
747+
}),
748+
message: None,
749+
});
750+
assert!(!val_tcr.is_expired());
751+
752+
// Adding the same key twice does not result in two cached values:
753+
cc.insert((KUBEAPPS_LOCAL.into(), key_tcr.clone()), val_tcr.clone());
754+
cc.insert((KUBEAPPS_LOCAL.into(), key_tcr.clone()), val_tcr.clone());
755+
756+
assert_eq!(cc.len(), 1);
757+
758+
// Adding the same TCR with a different k8s api url *does* result in a
759+
// separately cached value:
760+
cc.insert(
761+
("https://172.168.0.3:6443".into(), key_tcr.clone()),
762+
val_tcr.clone(),
763+
);
764+
765+
assert_eq!(cc.len(), 2);
726766
}
727767

728768
#[test]
@@ -741,7 +781,7 @@ mZu9A/ivt37pOQXm/HOX6tHB
741781
}),
742782
message: None,
743783
});
744-
cc.insert(key_tcr.clone(), val_tcr.clone());
784+
cc.insert((KUBEAPPS_LOCAL.into(), key_tcr.clone()), val_tcr.clone());
745785

746786
assert!(!val_tcr.is_expired());
747787
assert_eq!(cc.len(), 1);
@@ -751,7 +791,7 @@ mZu9A/ivt37pOQXm/HOX6tHB
751791
// Now the credential will still be present (as there hasn't been a write
752792
// operation), but will not be returned as it has expired.
753793
assert_eq!(cc.len(), 1);
754-
assert_eq!(cc.get(&key_tcr), None);
794+
assert_eq!(cc.get(&(KUBEAPPS_LOCAL.into(), key_tcr.clone())), None);
755795

756796
// But if we update the cache (where a write lock will be used) the expired
757797
// one will be removed.
@@ -772,10 +812,13 @@ mZu9A/ivt37pOQXm/HOX6tHB
772812
}),
773813
message: None,
774814
});
775-
cc.insert(key_tcr_2.clone(), val_tcr_2.clone());
815+
cc.insert(
816+
(KUBEAPPS_LOCAL.into(), key_tcr_2.clone()),
817+
val_tcr_2.clone(),
818+
);
776819

777820
assert_eq!(cc.len(), 1);
778-
assert_eq!(cc.get(&key_tcr), None);
779-
assert_eq!(cc.get(&key_tcr_2), Some(val_tcr_2));
821+
assert_eq!(cc.get(&(KUBEAPPS_LOCAL.into(), key_tcr)), None);
822+
assert_eq!(cc.get(&(KUBEAPPS_LOCAL.into(), key_tcr_2)), Some(val_tcr_2));
780823
}
781824
}

0 commit comments

Comments
 (0)