Skip to main content

reqsign_oracle/
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::constants::*;
21use ini::Ini;
22use reqsign_core::Context;
23use reqsign_core::Result;
24use reqsign_core::utils::Redact;
25use std::fmt::{Debug, Formatter};
26
27/// Config for Oracle Cloud Infrastructure services.
28#[derive(Clone, Default)]
29#[deprecated(
30    since = "0.1.0",
31    note = "Config is no longer needed. Use specific credential providers instead"
32)]
33pub struct Config {
34    /// UserID for Oracle Cloud Infrastructure.
35    pub user: Option<String>,
36    /// TenancyID for Oracle Cloud Infrastructure.
37    pub tenancy: Option<String>,
38    /// Region for Oracle Cloud Infrastructure.
39    pub region: Option<String>,
40    /// Private key file path for Oracle Cloud Infrastructure.
41    pub key_file: Option<String>,
42    /// Fingerprint for the key_file.
43    pub fingerprint: Option<String>,
44    /// Config file path to load credentials.
45    pub config_file: Option<String>,
46    /// Profile name in the config file.
47    pub profile: Option<String>,
48}
49
50impl Debug for Config {
51    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
52        f.debug_struct("Config")
53            .field("user", &self.user)
54            .field("tenancy", &self.tenancy)
55            .field("region", &self.region)
56            .field("key_file", &Redact::from(&self.key_file))
57            .field("fingerprint", &self.fingerprint)
58            .field("config_file", &self.config_file)
59            .field("profile", &self.profile)
60            .finish()
61    }
62}
63
64impl Config {
65    /// Load config from environment variables.
66    pub fn from_env(ctx: &Context) -> Self {
67        Self {
68            user: ctx.env_var(ORACLE_USER),
69            tenancy: ctx.env_var(ORACLE_TENANCY),
70            region: ctx.env_var(ORACLE_REGION),
71            key_file: ctx.env_var(ORACLE_KEY_FILE),
72            fingerprint: ctx.env_var(ORACLE_FINGERPRINT),
73            config_file: ctx.env_var(ORACLE_CONFIG_FILE),
74            profile: ctx.env_var(ORACLE_PROFILE),
75        }
76    }
77
78    /// Load config from Oracle config file.
79    pub async fn from_config_file(ctx: &Context, path: &str, profile: &str) -> Result<Self> {
80        let content = ctx.file_read_as_string(path).await?;
81        let ini = Ini::read_from(&mut content.as_bytes()).map_err(|e| {
82            reqsign_core::Error::config_invalid(format!("Failed to parse config file: {e}"))
83        })?;
84        let section = ini.section(Some(profile)).ok_or_else(|| {
85            reqsign_core::Error::config_invalid(format!(
86                "Profile {profile} not found in config file"
87            ))
88        })?;
89
90        Ok(Self {
91            user: section.get("user").map(|s| s.to_string()),
92            tenancy: section.get("tenancy").map(|s| s.to_string()),
93            region: section.get("region").map(|s| s.to_string()),
94            key_file: section.get("key_file").map(|s| s.to_string()),
95            fingerprint: section.get("fingerprint").map(|s| s.to_string()),
96            config_file: Some(path.to_string()),
97            profile: Some(profile.to_string()),
98        })
99    }
100}