|
| 1 | +use aws_sdk_ssm::types::{Parameter, ParameterType}; |
| 2 | +use si_data_ssm::{ParameterStoreClient, ParameterStoreClientError}; |
| 3 | +use telemetry::prelude::*; |
| 4 | +use thiserror::Error; |
| 5 | + |
| 6 | +#[remain::sorted] |
| 7 | +#[derive(Debug, Error)] |
| 8 | +pub enum ParameterStorageError { |
| 9 | + #[error("Parameter path invalid, must start with /: {0}")] |
| 10 | + InvalidPath(String), |
| 11 | + #[error("Parameter not found: {0}")] |
| 12 | + ParameterNotFound(String), |
| 13 | + #[error("Parameter store client error: {0}")] |
| 14 | + ParameterStoreClient(#[from] ParameterStoreClientError), |
| 15 | + #[error("Parameter path not found: {0}")] |
| 16 | + PathNotFound(String), |
| 17 | + #[error("Write operations not supported in environment variable mode")] |
| 18 | + WriteNotSupported, |
| 19 | +} |
| 20 | + |
| 21 | +pub type ParameterStorageResult<T> = Result<T, ParameterStorageError>; |
| 22 | + |
| 23 | +/// Trait for abstracting parameter storage backends |
| 24 | +#[async_trait::async_trait] |
| 25 | +pub trait ParameterStorage: std::fmt::Debug + Send + Sync { |
| 26 | + /// Gets a specific parameter by name |
| 27 | + async fn get_parameter(&self, name: String) -> ParameterStorageResult<Parameter>; |
| 28 | + |
| 29 | + /// Gets all parameters under a path |
| 30 | + async fn parameters_by_path(&self, path: String) -> ParameterStorageResult<Vec<Parameter>>; |
| 31 | + |
| 32 | + /// Creates or updates a string parameter |
| 33 | + async fn create_string_parameter( |
| 34 | + &self, |
| 35 | + name: String, |
| 36 | + value: String, |
| 37 | + ) -> ParameterStorageResult<()>; |
| 38 | +} |
| 39 | + |
| 40 | +/// Environment variable-based parameter storage |
| 41 | +#[derive(Debug, Clone)] |
| 42 | +pub struct EnvParameterStorage; |
| 43 | + |
| 44 | +impl EnvParameterStorage { |
| 45 | + /// Creates a new environment parameter storage |
| 46 | + pub fn new() -> Self { |
| 47 | + Self |
| 48 | + } |
| 49 | + |
| 50 | + /// Converts a parameter path to an environment variable name |
| 51 | + /// Example: `/si/prod/database/password` -> `SI_PROD_DATABASE_PASSWORD` |
| 52 | + fn path_to_env_var(path: &str) -> String { |
| 53 | + path.trim_start_matches('/') |
| 54 | + .replace('/', "_") |
| 55 | + .to_uppercase() |
| 56 | + } |
| 57 | + |
| 58 | + /// Checks if an environment variable key matches a path prefix |
| 59 | + fn env_var_matches_path(env_key: &str, path: &str) -> bool { |
| 60 | + let normalized = Self::path_to_env_var(path); |
| 61 | + // Match exact or with underscore separator to avoid false matches |
| 62 | + // e.g., SI_PROD matches SI_PROD_DB but not SI_PRODUCTION |
| 63 | + env_key == normalized || env_key.starts_with(&format!("{}_", normalized)) |
| 64 | + } |
| 65 | + |
| 66 | + /// Converts an environment variable name back to a parameter path |
| 67 | + /// Example: `SI_PROD_DATABASE_PASSWORD` -> `/si/prod/database/password` |
| 68 | + fn env_var_to_path(env_var: &str) -> String { |
| 69 | + format!("/{}", env_var.replace('_', "/").to_lowercase()) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// Implementation of ParameterStorage for EnvParameterStorage (environment variable backend) |
| 74 | +#[async_trait::async_trait] |
| 75 | +impl ParameterStorage for EnvParameterStorage { |
| 76 | + async fn get_parameter(&self, name: String) -> ParameterStorageResult<Parameter> { |
| 77 | + let env_var = Self::path_to_env_var(&name); |
| 78 | + |
| 79 | + #[allow(clippy::disallowed_methods)] |
| 80 | + let value = std::env::var(&env_var) |
| 81 | + .map_err(|_| ParameterStorageError::ParameterNotFound(name.clone()))?; |
| 82 | + |
| 83 | + Ok(Parameter::builder() |
| 84 | + .name(name) |
| 85 | + .value(value) |
| 86 | + .r#type(ParameterType::String) |
| 87 | + .build()) |
| 88 | + } |
| 89 | + |
| 90 | + async fn parameters_by_path(&self, path: String) -> ParameterStorageResult<Vec<Parameter>> { |
| 91 | + if !path.starts_with("/") { |
| 92 | + return Err(ParameterStorageError::InvalidPath(path)); |
| 93 | + } |
| 94 | + |
| 95 | + let mut parameters = Vec::new(); |
| 96 | + |
| 97 | + #[allow(clippy::disallowed_methods)] |
| 98 | + for (key, value) in std::env::vars() { |
| 99 | + if Self::env_var_matches_path(&key, &path) { |
| 100 | + let param_name = Self::env_var_to_path(&key); |
| 101 | + parameters.push( |
| 102 | + Parameter::builder() |
| 103 | + .name(param_name) |
| 104 | + .value(value) |
| 105 | + .r#type(ParameterType::String) |
| 106 | + .build(), |
| 107 | + ); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if parameters.is_empty() { |
| 112 | + return Err(ParameterStorageError::PathNotFound(path)); |
| 113 | + } |
| 114 | + |
| 115 | + Ok(parameters) |
| 116 | + } |
| 117 | + |
| 118 | + async fn create_string_parameter( |
| 119 | + &self, |
| 120 | + _name: String, |
| 121 | + _value: String, |
| 122 | + ) -> ParameterStorageResult<()> { |
| 123 | + Err(ParameterStorageError::WriteNotSupported) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +/// Implementation of ParameterStorage for ParameterStoreClient (AWS SSM backend) |
| 128 | +#[async_trait::async_trait] |
| 129 | +impl ParameterStorage for ParameterStoreClient { |
| 130 | + async fn get_parameter(&self, name: String) -> ParameterStorageResult<Parameter> { |
| 131 | + self.get_parameter(name).await.map_err(Into::into) |
| 132 | + } |
| 133 | + |
| 134 | + async fn parameters_by_path(&self, path: String) -> ParameterStorageResult<Vec<Parameter>> { |
| 135 | + self.parameters_by_path(path).await.map_err(Into::into) |
| 136 | + } |
| 137 | + |
| 138 | + async fn create_string_parameter( |
| 139 | + &self, |
| 140 | + name: String, |
| 141 | + value: String, |
| 142 | + ) -> ParameterStorageResult<()> { |
| 143 | + self.create_string_parameter(name, value) |
| 144 | + .await |
| 145 | + .map_err(Into::into) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +/// Enum wrapper for different parameter storage backends |
| 150 | +#[remain::sorted] |
| 151 | +#[derive(Debug, Clone)] |
| 152 | +pub enum ParameterStorageBackend { |
| 153 | + /// Environment variable backend |
| 154 | + Env(EnvParameterStorage), |
| 155 | + /// AWS SSM Parameter Store backend |
| 156 | + Ssm(ParameterStoreClient), |
| 157 | +} |
| 158 | + |
| 159 | +/// Implementation of ParameterStorage for ParameterStorageBackend (delegates to inner backend) |
| 160 | +#[async_trait::async_trait] |
| 161 | +impl ParameterStorage for ParameterStorageBackend { |
| 162 | + async fn get_parameter(&self, name: String) -> ParameterStorageResult<Parameter> { |
| 163 | + match self { |
| 164 | + Self::Env(storage) => storage.get_parameter(name).await, |
| 165 | + Self::Ssm(client) => <ParameterStoreClient as ParameterStorage>::get_parameter(client, name).await, |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + async fn parameters_by_path(&self, path: String) -> ParameterStorageResult<Vec<Parameter>> { |
| 170 | + match self { |
| 171 | + Self::Env(storage) => storage.parameters_by_path(path).await, |
| 172 | + Self::Ssm(client) => <ParameterStoreClient as ParameterStorage>::parameters_by_path(client, path).await, |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + async fn create_string_parameter( |
| 177 | + &self, |
| 178 | + name: String, |
| 179 | + value: String, |
| 180 | + ) -> ParameterStorageResult<()> { |
| 181 | + match self { |
| 182 | + Self::Env(storage) => storage.create_string_parameter(name, value).await, |
| 183 | + Self::Ssm(client) => <ParameterStoreClient as ParameterStorage>::create_string_parameter(client, name, value).await, |
| 184 | + } |
| 185 | + } |
| 186 | +} |
0 commit comments