Skip to main content

reqsign_oracle/provide_credential/
config.rs

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#![allow(deprecated)]
19
20use crate::{Config, Credential};
21use log::debug;
22use reqsign_core::time::Timestamp;
23use reqsign_core::{Context, ProvideCredential, Result};
24use std::sync::Arc;
25use std::time::Duration;
26
27/// Static configuration based loader.
28#[derive(Debug)]
29pub struct ConfigCredentialProvider {
30    config: Arc<Config>,
31}
32
33impl ConfigCredentialProvider {
34    /// Create a new ConfigCredentialProvider
35    pub fn new(config: Arc<Config>) -> Self {
36        Self { config }
37    }
38}
39impl ProvideCredential for ConfigCredentialProvider {
40    type Credential = Credential;
41
42    async fn provide_credential(&self, ctx: &Context) -> Result<Option<Self::Credential>> {
43        // Merge with environment config
44        let env_config = Config::from_env(ctx);
45        let config = self.config.as_ref();
46
47        // Use environment values if available, otherwise fall back to config
48        let tenancy = env_config.tenancy.or_else(|| config.tenancy.clone());
49        let user = env_config.user.or_else(|| config.user.clone());
50        let key_file = env_config.key_file.or_else(|| config.key_file.clone());
51        let fingerprint = env_config
52            .fingerprint
53            .or_else(|| config.fingerprint.clone());
54
55        match (&tenancy, &user, &key_file, &fingerprint) {
56            (Some(tenancy), Some(user), Some(key_file), Some(fingerprint)) => {
57                debug!("loading credential from config");
58                Ok(Some(Credential {
59                    tenancy: tenancy.clone(),
60                    user: user.clone(),
61                    key_file: key_file.clone(),
62                    fingerprint: fingerprint.clone(),
63                    // Set expires_in to 10 minutes to enforce re-read
64                    expires_in: Some(Timestamp::now() + Duration::from_secs(600)),
65                }))
66            }
67            _ => {
68                debug!("incomplete config, skipping");
69                Ok(None)
70            }
71        }
72    }
73}