Skip to content
This repository was archived by the owner on Feb 6, 2026. It is now read-only.

Commit bd4b528

Browse files
committed
WIP: claude refactor
Signed-off-by: Nick Gerace <nick@systeminit.com>
1 parent 7e12d61 commit bd4b528

14 files changed

Lines changed: 262 additions & 196 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎lib/innit-core/Cargo.toml‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@ rust-version.workspace = true
99
publish.workspace = true
1010

1111
[dependencies]
12-
aws-sdk-ssm = { workspace = true }
1312
config-file = { path = "../../lib/config-file" }
1413
serde = { workspace = true }

‎lib/innit-core/src/lib.rs‎

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
use aws_sdk_ssm::types::{
2-
Parameter as AwsParameter,
3-
ParameterType as AwsParameterType,
4-
};
51
use config_file::parameter_provider::{
62
Parameter as ParameterProviderParameter,
73
ParameterType as ParameterProviderParameterType,
@@ -18,19 +14,6 @@ pub struct Parameter {
1814
pub r#type: Option<ParameterType>,
1915
}
2016

21-
impl From<AwsParameter> for Parameter {
22-
fn from(p: AwsParameter) -> Self {
23-
Self {
24-
name: p.name().unwrap_or_default().to_string(),
25-
value: p.value().map(|s| s.to_string()),
26-
r#type: p.r#type().map(|t| match t {
27-
AwsParameterType::StringList => ParameterType::StringList,
28-
_ => ParameterType::String,
29-
}),
30-
}
31-
}
32-
}
33-
3417
impl From<Parameter> for ParameterProviderParameter {
3518
fn from(p: Parameter) -> Self {
3619
Self {

‎lib/innit-server/BUCK‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ rust_library(
1111
"//lib/si-std:si-std",
1212
"//lib/si-tls:si-tls",
1313
"//lib/telemetry-rs:telemetry",
14+
"//third-party/rust:async-trait",
15+
"//third-party/rust:aws-sdk-ssm",
1416
"//third-party/rust:axum",
1517
"//third-party/rust:dashmap",
1618
"//third-party/rust:derive_builder",

‎lib/innit-server/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ rust-version.workspace = true
99
publish.workspace = true
1010

1111
[dependencies]
12+
async-trait = { workspace = true }
1213
axum = { workspace = true }
1314
buck2-resources = { path = "../../lib/buck2-resources" }
1415
dashmap = { workspace = true }

‎lib/innit-server/src/app_state.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use axum::extract::FromRef;
2-
use si_data_ssm::ParameterStorageBackend;
32
use tokio_util::sync::CancellationToken;
43

5-
use crate::parameter_cache::ParameterCache;
4+
use crate::{
5+
parameter_cache::ParameterCache,
6+
parameter_storage::ParameterStorageBackend,
7+
};
68

79
#[remain::sorted]
810
#[derive(Debug, Eq, PartialEq)]

‎lib/innit-server/src/lib.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod app_state;
33
mod config;
44
mod middleware;
55
mod parameter_cache;
6+
mod parameter_storage;
67
mod routes;
78
mod server;
89

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

‎lib/innit-server/src/routes.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use serde_json::{
1616
Value,
1717
json,
1818
};
19-
use si_data_ssm::ParameterStoreClientError;
2019
use thiserror::Error;
2120

2221
mod clear_cache;
@@ -28,7 +27,10 @@ use super::{
2827
app_state::AppState,
2928
server::ServerError,
3029
};
31-
use crate::api_error::ApiError;
30+
use crate::{
31+
api_error::ApiError,
32+
parameter_storage::ParameterStorageError,
33+
};
3234

3335
pub fn public_routes(state: AppState) -> Router {
3436
Router::new()
@@ -59,8 +61,8 @@ async fn system_status_route() -> Json<Value> {
5961
#[remain::sorted]
6062
#[derive(Debug, Error)]
6163
pub enum AppError {
62-
#[error("parameter store client error: {0}")]
63-
ParameterStoreClient(#[from] ParameterStoreClientError),
64+
#[error("parameter storage error: {0}")]
65+
ParameterStorage(#[from] ParameterStorageError),
6466
#[error("server error: {0}")]
6567
Server(#[from] ServerError),
6668
}

‎lib/innit-server/src/routes/create_parameter.rs‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ use innit_core::{
1010
CreateParameterResponse,
1111
Parameter,
1212
};
13-
use si_data_ssm::ParameterStorage;
1413
use telemetry::tracing::info;
1514

1615
use super::AppError;
17-
use crate::app_state::AppState;
16+
use crate::{
17+
app_state::AppState,
18+
parameter_storage::ParameterStorage,
19+
};
1820

1921
pub async fn create_parameter_route(
2022
Path(name): Path<String>,

0 commit comments

Comments
 (0)