|
1 | 1 | use std::ops::Deref; |
2 | 2 | use std::sync::Arc; |
3 | 3 |
|
| 4 | +use crate::controller::Context; |
| 5 | +use crate::remote_watcher::RemoteWatcherKey; |
| 6 | +use crate::resources::{ |
| 7 | + ClusterRef, ClusterResourceRef, ResourceSync, ALLOWED_NAMESPACES_ANNOTATION, |
| 8 | +}; |
| 9 | +use crate::Error::UnauthorizedKubeconfigAccess; |
| 10 | +use crate::{Error, FINALIZER}; |
4 | 11 | use k8s_openapi::api::core::v1::Secret; |
5 | 12 | use kube::api::{ApiResource, DynamicObject}; |
6 | 13 | use kube::discovery::Scope::*; |
7 | 14 | use kube::runtime::reflector::ObjectRef; |
8 | 15 | use kube::{discovery, Api, Client, Config, ResourceExt}; |
| 16 | +use regex::Regex; |
9 | 17 | use tracing::debug; |
10 | 18 |
|
11 | | -use crate::controller::Context; |
12 | | -use crate::remote_watcher::RemoteWatcherKey; |
13 | | -use crate::resources::{ClusterRef, ClusterResourceRef, ResourceSync}; |
14 | | -use crate::{Error, FINALIZER}; |
15 | | - |
16 | 19 | macro_rules! rs_watch { |
17 | 20 | ($fn_name:ident, $method:ident) => { |
18 | 21 | pub async fn $fn_name(&self, ctx: Arc<Context>) { |
@@ -98,53 +101,86 @@ async fn cluster_client( |
98 | 101 | local_ns: &str, |
99 | 102 | client: Client, |
100 | 103 | ) -> crate::Result<Client> { |
101 | | - let client = match cluster_ref { |
102 | | - None => client, |
103 | | - Some(cluster_ref) => { |
104 | | - let secret_ns = cluster_ref |
105 | | - .kube_config |
106 | | - .secret_ref |
107 | | - .namespace |
108 | | - .as_deref() |
109 | | - .unwrap_or(local_ns); |
110 | | - let secrets: Api<Secret> = Api::namespaced(client, secret_ns); |
111 | | - let secret_ref = &cluster_ref.kube_config.secret_ref; |
112 | | - let sec = secrets.get(&secret_ref.name).await?; |
113 | | - |
114 | | - let kube_config = kube::config::Kubeconfig::from_yaml( |
115 | | - std::str::from_utf8( |
116 | | - &sec.data |
117 | | - .unwrap() |
118 | | - .get(&secret_ref.key) |
119 | | - .ok_or_else(|| { |
120 | | - Error::MissingKeyError( |
121 | | - secret_ref.key.clone(), |
122 | | - secret_ref.name.clone(), |
123 | | - secret_ns.to_string(), |
124 | | - ) |
125 | | - })? |
126 | | - .0, |
127 | | - ) |
128 | | - .map_err(Error::KubeconfigUtf8Error)?, |
129 | | - )?; |
130 | | - let mut config = |
131 | | - Config::from_custom_kubeconfig(kube_config, &Default::default()).await?; |
132 | | - |
133 | | - if let Some(ref namespace) = cluster_ref.namespace { |
134 | | - config.default_namespace = namespace.clone(); |
135 | | - } |
| 104 | + let client = |
| 105 | + match cluster_ref { |
| 106 | + None => client, |
| 107 | + Some(cluster_ref) => { |
| 108 | + let secret_ns = cluster_ref |
| 109 | + .kube_config |
| 110 | + .secret_ref |
| 111 | + .namespace |
| 112 | + .as_deref() |
| 113 | + .unwrap_or(local_ns); |
| 114 | + let secrets: Api<Secret> = Api::namespaced(client, secret_ns); |
| 115 | + let secret_ref = &cluster_ref.kube_config.secret_ref; |
| 116 | + let sec = secrets.get(&secret_ref.name).await.map_err(|e| { |
| 117 | + match secret_ns == local_ns { |
| 118 | + true => crate::Error::from(e), |
| 119 | + false => { |
| 120 | + debug!( |
| 121 | + "error accessing kubeconfig secret in remote namespace: {}", |
| 122 | + e |
| 123 | + ); |
| 124 | + UnauthorizedKubeconfigAccess() |
| 125 | + } |
| 126 | + } |
| 127 | + })?; |
136 | 128 |
|
137 | | - debug!(?config.cluster_url, "connecting to remote cluster"); |
138 | | - let remote_client = kube::Client::try_from(config)?; |
139 | | - let version = remote_client.apiserver_version().await?; |
140 | | - debug!(?version, "remote cluster version"); |
| 129 | + if secret_ns != local_ns { |
| 130 | + verify_kubeconfig_secret_access(local_ns, &sec)?; |
| 131 | + } |
141 | 132 |
|
142 | | - remote_client |
143 | | - } |
144 | | - }; |
| 133 | + let kube_config = kube::config::Kubeconfig::from_yaml( |
| 134 | + std::str::from_utf8( |
| 135 | + &sec.data |
| 136 | + .unwrap() |
| 137 | + .get(&secret_ref.key) |
| 138 | + .ok_or_else(|| { |
| 139 | + Error::MissingKeyError( |
| 140 | + secret_ref.key.clone(), |
| 141 | + secret_ref.name.clone(), |
| 142 | + secret_ns.to_string(), |
| 143 | + ) |
| 144 | + })? |
| 145 | + .0, |
| 146 | + ) |
| 147 | + .map_err(Error::KubeconfigUtf8Error)?, |
| 148 | + )?; |
| 149 | + let mut config = |
| 150 | + Config::from_custom_kubeconfig(kube_config, &Default::default()).await?; |
| 151 | + |
| 152 | + if let Some(ref namespace) = cluster_ref.namespace { |
| 153 | + config.default_namespace = namespace.clone(); |
| 154 | + } |
| 155 | + |
| 156 | + debug!(?config.cluster_url, "connecting to remote cluster"); |
| 157 | + let remote_client = kube::Client::try_from(config)?; |
| 158 | + let version = remote_client.apiserver_version().await?; |
| 159 | + debug!(?version, "remote cluster version"); |
| 160 | + |
| 161 | + remote_client |
| 162 | + } |
| 163 | + }; |
145 | 164 | Ok(client) |
146 | 165 | } |
147 | 166 |
|
| 167 | +fn verify_kubeconfig_secret_access(local_ns: &str, sec: &Secret) -> crate::Result<()> { |
| 168 | + let allowed_namespaces = sec |
| 169 | + .metadata |
| 170 | + .annotations |
| 171 | + .as_ref() |
| 172 | + .and_then(|annotations| annotations.get(ALLOWED_NAMESPACES_ANNOTATION)) |
| 173 | + .ok_or(UnauthorizedKubeconfigAccess())?; |
| 174 | + let re = Regex::new(allowed_namespaces).map_err(|e| { |
| 175 | + debug!("invalid regex in allowed namespaces annotation: {}", e); |
| 176 | + UnauthorizedKubeconfigAccess() |
| 177 | + })?; |
| 178 | + match re.is_match(local_ns) { |
| 179 | + true => Ok(()), |
| 180 | + false => Err(UnauthorizedKubeconfigAccess()), |
| 181 | + } |
| 182 | +} |
| 183 | + |
148 | 184 | async fn api_for( |
149 | 185 | cluster_resource_ref: &ClusterResourceRef, |
150 | 186 | local_ns: &str, |
@@ -180,3 +216,146 @@ impl ClusterResourceRef { |
180 | 216 | api_for(self, local_ns, client).await |
181 | 217 | } |
182 | 218 | } |
| 219 | + |
| 220 | +#[cfg(test)] |
| 221 | +mod tests { |
| 222 | + use super::*; |
| 223 | + use futures::future::join_all; |
| 224 | + use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta; |
| 225 | + use rand::{distr::Alphanumeric, rngs::StdRng, Rng, SeedableRng}; |
| 226 | + use rstest::rstest; |
| 227 | + use std::collections::BTreeMap; |
| 228 | + |
| 229 | + fn secret_with_annotation(value: Option<&str>) -> Secret { |
| 230 | + Secret { |
| 231 | + metadata: ObjectMeta { |
| 232 | + annotations: value.map(|v| { |
| 233 | + BTreeMap::from([(ALLOWED_NAMESPACES_ANNOTATION.to_string(), v.to_string())]) |
| 234 | + }), |
| 235 | + ..Default::default() |
| 236 | + }, |
| 237 | + ..Default::default() |
| 238 | + } |
| 239 | + } |
| 240 | + |
| 241 | + #[rstest] |
| 242 | + fn errs_when_annotation_missing() { |
| 243 | + let sec = secret_with_annotation(None); |
| 244 | + |
| 245 | + let res = verify_kubeconfig_secret_access("dev", &sec); |
| 246 | + |
| 247 | + assert!(matches!(res, Err(UnauthorizedKubeconfigAccess()))); |
| 248 | + } |
| 249 | + |
| 250 | + #[rstest] |
| 251 | + fn errs_when_annotation_key_absent() { |
| 252 | + let sec = Secret { |
| 253 | + metadata: ObjectMeta { |
| 254 | + annotations: Some(BTreeMap::from([( |
| 255 | + "other-annotation".to_string(), |
| 256 | + "^.*$".to_string(), |
| 257 | + )])), |
| 258 | + ..Default::default() |
| 259 | + }, |
| 260 | + ..Default::default() |
| 261 | + }; |
| 262 | + |
| 263 | + let res = verify_kubeconfig_secret_access("ns1", &sec); |
| 264 | + |
| 265 | + assert!(matches!(res, Err(UnauthorizedKubeconfigAccess()))); |
| 266 | + } |
| 267 | + |
| 268 | + #[rstest] |
| 269 | + #[case::unbalanced_paren("(")] |
| 270 | + #[case::dangling_escape("\\")] |
| 271 | + fn errs_on_invalid_regex(#[case] pattern: &str) { |
| 272 | + let sec = secret_with_annotation(Some(pattern)); |
| 273 | + |
| 274 | + let res = verify_kubeconfig_secret_access("random", &sec); |
| 275 | + |
| 276 | + assert!(matches!(res, Err(UnauthorizedKubeconfigAccess()))); |
| 277 | + } |
| 278 | + |
| 279 | + #[rstest] |
| 280 | + fn allows_matching_namespace_randomized() { |
| 281 | + let pattern = "^ns-[a-z0-9]{4}$"; |
| 282 | + let sec = secret_with_annotation(Some(pattern)); |
| 283 | + let mut rng = StdRng::seed_from_u64(42); |
| 284 | + |
| 285 | + for _ in 0..8 { |
| 286 | + let tail: String = (0..4) |
| 287 | + .map(|_| rng.sample(Alphanumeric) as char) |
| 288 | + .map(|c| c.to_ascii_lowercase()) |
| 289 | + .collect(); |
| 290 | + let ns = format!("ns-{}", tail); |
| 291 | + |
| 292 | + let res = verify_kubeconfig_secret_access(&ns, &sec); |
| 293 | + |
| 294 | + assert!(res.is_ok(), "{} should match {}", ns, pattern); |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + #[rstest] |
| 299 | + fn denies_non_matching_namespace_randomized() { |
| 300 | + let pattern = "^team-[0-9]{2}$"; |
| 301 | + let sec = secret_with_annotation(Some(pattern)); |
| 302 | + let mut rng = StdRng::seed_from_u64(84); |
| 303 | + |
| 304 | + for _ in 0..6 { |
| 305 | + let ns = format!("proj-{}", rng.random_range(10_u8..99_u8)); |
| 306 | + |
| 307 | + let res = verify_kubeconfig_secret_access(&ns, &sec); |
| 308 | + |
| 309 | + assert!(matches!(res, Err(UnauthorizedKubeconfigAccess()))); |
| 310 | + } |
| 311 | + } |
| 312 | + |
| 313 | + #[tokio::test] |
| 314 | + async fn concurrent_checks_are_independent() { |
| 315 | + let allow_secret = secret_with_annotation(Some("^ok-[a-z]{2}$")); |
| 316 | + let deny_secret = secret_with_annotation(Some("^deny$")); |
| 317 | + let mut rng = StdRng::seed_from_u64(7); |
| 318 | + |
| 319 | + let inputs: Vec<_> = (0..6) |
| 320 | + .map(|i| { |
| 321 | + let expect_ok = i % 2 == 0; |
| 322 | + let sec = if expect_ok { |
| 323 | + allow_secret.clone() |
| 324 | + } else { |
| 325 | + deny_secret.clone() |
| 326 | + }; |
| 327 | + let ns = if expect_ok { |
| 328 | + let part: String = (0..2) |
| 329 | + .map(|_| rng.sample(Alphanumeric) as char) |
| 330 | + .map(|c| c.to_ascii_lowercase()) |
| 331 | + .collect(); |
| 332 | + format!("ok-{}", part) |
| 333 | + } else { |
| 334 | + format!("bad-{}", rng.random::<u8>()) |
| 335 | + }; |
| 336 | + (ns, expect_ok, sec) |
| 337 | + }) |
| 338 | + .collect(); |
| 339 | + |
| 340 | + let handles: Vec<_> = inputs |
| 341 | + .into_iter() |
| 342 | + .map(|(ns, expect_ok, sec)| { |
| 343 | + tokio::spawn(async move { |
| 344 | + let res = verify_kubeconfig_secret_access(&ns, &sec); |
| 345 | + (expect_ok, res) |
| 346 | + }) |
| 347 | + }) |
| 348 | + .collect(); |
| 349 | + |
| 350 | + let outcomes = join_all(handles).await; |
| 351 | + |
| 352 | + for outcome in outcomes { |
| 353 | + let (expect_ok, res) = outcome.expect("task panicked"); |
| 354 | + if expect_ok { |
| 355 | + assert!(res.is_ok(), "expected {:?} to be authorized", res); |
| 356 | + } else { |
| 357 | + assert!(matches!(res, Err(UnauthorizedKubeconfigAccess()))); |
| 358 | + } |
| 359 | + } |
| 360 | + } |
| 361 | +} |
0 commit comments