|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use crate::client::metadata::Metadata; |
| 19 | +use crate::error::{Error, Result}; |
| 20 | +use crate::rpc::message::GetSecurityTokenRequest; |
| 21 | +use crate::rpc::RpcClient; |
| 22 | +use parking_lot::RwLock; |
| 23 | +use serde::Deserialize; |
| 24 | +use std::collections::HashMap; |
| 25 | +use std::sync::Arc; |
| 26 | +use std::time::{Duration, Instant}; |
| 27 | + |
| 28 | +const CACHE_TTL: Duration = Duration::from_secs(3600); |
| 29 | + |
| 30 | +#[derive(Debug, Deserialize)] |
| 31 | +struct Credentials { |
| 32 | + access_key_id: String, |
| 33 | + access_key_secret: String, |
| 34 | + security_token: Option<String>, |
| 35 | +} |
| 36 | + |
| 37 | +struct CachedToken { |
| 38 | + access_key_id: String, |
| 39 | + secret_access_key: String, |
| 40 | + security_token: Option<String>, |
| 41 | + addition_infos: HashMap<String, String>, |
| 42 | + cached_at: Instant, |
| 43 | +} |
| 44 | + |
| 45 | +impl CachedToken { |
| 46 | + fn to_s3_props(&self) -> HashMap<String, String> { |
| 47 | + let mut props = HashMap::new(); |
| 48 | + |
| 49 | + props.insert("access_key_id".to_string(), self.access_key_id.clone()); |
| 50 | + props.insert( |
| 51 | + "secret_access_key".to_string(), |
| 52 | + self.secret_access_key.clone(), |
| 53 | + ); |
| 54 | + |
| 55 | + if let Some(token) = &self.security_token { |
| 56 | + props.insert("security_token".to_string(), token.clone()); |
| 57 | + } |
| 58 | + |
| 59 | + for (key, value) in &self.addition_infos { |
| 60 | + if let Some((opendal_key, transform)) = convert_hadoop_key_to_opendal(key) { |
| 61 | + let final_value = if transform { |
| 62 | + // Invert boolean value (path_style_access -> enable_virtual_host_style) |
| 63 | + if value == "true" { "false".to_string() } else { "true".to_string() } |
| 64 | + } else { |
| 65 | + value.clone() |
| 66 | + }; |
| 67 | + props.insert(opendal_key, final_value); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + props |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// Returns (opendal_key, needs_inversion) |
| 76 | +/// needs_inversion is true for path_style_access -> enable_virtual_host_style conversion |
| 77 | +fn convert_hadoop_key_to_opendal(hadoop_key: &str) -> Option<(String, bool)> { |
| 78 | + match hadoop_key { |
| 79 | + // Standard S3A keys |
| 80 | + "fs.s3a.endpoint" => Some(("endpoint".to_string(), false)), |
| 81 | + "fs.s3a.endpoint.region" => Some(("region".to_string(), false)), |
| 82 | + // path.style.access = false means virtual_host_style = true (inverted) |
| 83 | + "fs.s3a.path.style.access" => Some(("enable_virtual_host_style".to_string(), true)), |
| 84 | + "fs.s3a.connection.ssl.enabled" => None, |
| 85 | + // Red-S3 keys (Fluss custom format) |
| 86 | + "fs.red-s3.endpoint" => Some(("endpoint".to_string(), false)), |
| 87 | + "fs.red-s3.region" => Some(("region".to_string(), false)), |
| 88 | + "fs.red-s3.path-style-access" => Some(("enable_virtual_host_style".to_string(), true)), |
| 89 | + "fs.red-s3.connection.ssl.enabled" => None, |
| 90 | + _ => None, |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +pub struct CredentialsCache { |
| 95 | + inner: RwLock<Option<CachedToken>>, |
| 96 | +} |
| 97 | + |
| 98 | +impl CredentialsCache { |
| 99 | + pub fn new() -> Self { |
| 100 | + Self { |
| 101 | + inner: RwLock::new(None), |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + pub async fn get_or_refresh( |
| 106 | + &self, |
| 107 | + rpc_client: &Arc<RpcClient>, |
| 108 | + metadata: &Arc<Metadata>, |
| 109 | + ) -> Result<HashMap<String, String>> { |
| 110 | + { |
| 111 | + let guard = self.inner.read(); |
| 112 | + if let Some(cached) = guard.as_ref() { |
| 113 | + if cached.cached_at.elapsed() < CACHE_TTL { |
| 114 | + return Ok(cached.to_s3_props()); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + self.refresh_from_server(rpc_client, metadata).await |
| 120 | + } |
| 121 | + |
| 122 | + async fn refresh_from_server( |
| 123 | + &self, |
| 124 | + rpc_client: &Arc<RpcClient>, |
| 125 | + metadata: &Arc<Metadata>, |
| 126 | + ) -> Result<HashMap<String, String>> { |
| 127 | + let cluster = metadata.get_cluster(); |
| 128 | + let server_node = cluster |
| 129 | + .get_coordinator_server() |
| 130 | + .or_else(|| Some(cluster.get_one_available_server())) |
| 131 | + .expect("no available server to fetch security token"); |
| 132 | + let conn = rpc_client.get_connection(server_node).await?; |
| 133 | + |
| 134 | + let request = GetSecurityTokenRequest::new(); |
| 135 | + let response = conn.request(request).await?; |
| 136 | + |
| 137 | + let credentials: Credentials = serde_json::from_slice(&response.token) |
| 138 | + .map_err(|e| Error::JsonSerdeError(e.to_string()))?; |
| 139 | + |
| 140 | + let mut addition_infos = HashMap::new(); |
| 141 | + for kv in &response.addition_info { |
| 142 | + addition_infos.insert(kv.key.clone(), kv.value.clone()); |
| 143 | + } |
| 144 | + |
| 145 | + let cached = CachedToken { |
| 146 | + access_key_id: credentials.access_key_id, |
| 147 | + secret_access_key: credentials.access_key_secret, |
| 148 | + security_token: credentials.security_token, |
| 149 | + addition_infos, |
| 150 | + cached_at: Instant::now(), |
| 151 | + }; |
| 152 | + |
| 153 | + let props = cached.to_s3_props(); |
| 154 | + *self.inner.write() = Some(cached); |
| 155 | + |
| 156 | + Ok(props) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +impl Default for CredentialsCache { |
| 161 | + fn default() -> Self { |
| 162 | + Self::new() |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | + |
| 171 | + |
0 commit comments