switchboard_common/env/
function_manager.rs

1use crate::SbError;
2use serde::Deserialize;
3
4fn default_heartbeat_interval() -> i64 {
5    30
6}
7
8#[derive(Deserialize, Debug, Default)]
9pub struct FunctionManagerEnvironment {
10    pub chain: String,
11    #[serde(default)]
12    pub chain_id: String, // will convert to u64 basedon CHAIN
13    pub quote_key: String,
14    pub rpc_url: String,
15    #[serde(default = "default_heartbeat_interval")]
16    pub heartbeat_interval: i64,
17
18    // Required to post a quote for verification
19    pub ipfs_url: String,
20    pub ipfs_key: String,
21    pub ipfs_secret: String,
22
23    #[serde(default)]
24    pub queue: String, // optional
25
26    // One of the keypair configs required
27    #[serde(default)]
28    pub payer_secret: String,
29    #[serde(default)]
30    pub fs_payer_secret_path: String,
31    #[serde(default)]
32    pub google_payer_secret_path: String,
33    #[serde(default)]
34    pub google_application_credentials: String,
35
36    // Does this do anything? We never fetch from private repos
37    #[serde(default)]
38    pub docker_user: String,
39    #[serde(default)]
40    pub docker_key: String,
41
42    // EVM
43    #[serde(default)]
44    pub contract_address: String, // evm only
45}
46impl FunctionManagerEnvironment {
47    pub fn parse() -> Result<Self, SbError> {
48        match envy::from_env::<FunctionManagerEnvironment>() {
49            Ok(env) => Ok(env),
50            Err(error) => Err(SbError::CustomMessage(format!(
51                "failed to decode environment variables: {}",
52                error
53            ))),
54        }
55    }
56
57    /// Gets the payer secret from the provided environment variables
58    /// 1. PAYER_SECRET
59    /// 2. FS_PAYER_SECRET_PATH
60    /// 3. GOOGLE_PAYER_SECRET_PATH
61    /// 4. FS Injection '/pod-data/out'
62    pub fn get_payer(&self) -> Result<String, SbError> {
63        // Check if PAYER_SECRET was provided
64        if !self.payer_secret.is_empty() {
65            return Ok(self.payer_secret.clone().trim().to_string());
66        }
67
68        if !self.fs_payer_secret_path.is_empty() {
69            return crate::utils::read_and_trim_file(&self.fs_payer_secret_path);
70        }
71
72        // if !self.google_payer_secret_path.is_empty() {
73        //     let google_payer_secret_path = self.google_payer_secret_path.clone();
74        //     let mut buf = [0; 512];
75        //     let mut payer_len: usize = buf.len();
76
77        //     unsafe {
78        //         gsm_get_secret(
79        //             google_payer_secret_path.as_ptr(),
80        //             google_payer_secret_path.len(),
81        //             buf.as_mut_ptr(),
82        //             &mut payer_len,
83        //         )
84        //     };
85        //     return Ok(ptr_to_string(buf.as_ptr(), payer_len));
86        // }
87
88        Ok(std::fs::read_to_string("/pod-data/out")
89            .expect("Error reading fs secret")
90            .trim()
91            .to_string())
92
93        // Err(Error::CustomMessage(
94        //     "Failed to yield payer secret from provided ENV variables".to_string(),
95        // ))
96    }
97}