feat: harden community production foundation through story 1.5
This commit is contained in:
1 parent
c30461cc92
commit
f6fc2e5c9b
161 files changed
+16758
-2515
No files matched your search
@@ -0,0 +1,110 @@
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{FieldMode, FieldSpec, Sensitivity, deployment_field_registry, field_registry};
|
||||
|
||||
pub const BEGIN_MARKER: &str = "# BEGIN GENERATED CRANK RUNTIME CONFIG";
|
||||
pub const END_MARKER: &str = "# END GENERATED CRANK RUNTIME CONFIG";
|
||||
pub const DOC_BEGIN_MARKER: &str = "<!-- BEGIN GENERATED CRANK RUNTIME CONFIG -->";
|
||||
pub const DOC_END_MARKER: &str = "<!-- END GENERATED CRANK RUNTIME CONFIG -->";
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct RuntimeContract<'a> {
|
||||
schema_version: u32,
|
||||
generated_by: &'static str,
|
||||
fields: &'a [FieldSpec],
|
||||
deployment_only_fields: &'static [&'static str],
|
||||
}
|
||||
|
||||
pub fn schema_json() -> String {
|
||||
let contract = RuntimeContract {
|
||||
schema_version: 1,
|
||||
generated_by: "crank-config",
|
||||
fields: field_registry(),
|
||||
deployment_only_fields: deployment_field_registry(),
|
||||
};
|
||||
let mut rendered = serde_json::to_string_pretty(&contract).expect("static contract serializes");
|
||||
rendered.push('\n');
|
||||
rendered
|
||||
}
|
||||
|
||||
pub fn env_section(production: bool) -> String {
|
||||
let mut output = String::new();
|
||||
output.push_str(BEGIN_MARKER);
|
||||
output.push('\n');
|
||||
for field in field_registry()
|
||||
.iter()
|
||||
.filter(|field| field.mode == FieldMode::Effective)
|
||||
{
|
||||
let value = example_value(field, production);
|
||||
output.push_str(field.env_name);
|
||||
output.push('=');
|
||||
output.push_str(&value);
|
||||
output.push('\n');
|
||||
}
|
||||
output.push_str(END_MARKER);
|
||||
output.push('\n');
|
||||
output
|
||||
}
|
||||
|
||||
pub fn reference_section() -> String {
|
||||
let mut output = String::new();
|
||||
output.push_str(DOC_BEGIN_MARKER);
|
||||
output.push_str("\n\n| Environment | Semantic path | Process | Type/unit | Default | Bounds | Sensitivity | Mode |\n");
|
||||
output.push_str("|---|---|---|---|---|---|---|---|\n");
|
||||
for field in field_registry() {
|
||||
let unit = field.unit.unwrap_or("-");
|
||||
let default = match (field.sensitivity, field.default, field.required) {
|
||||
(Sensitivity::Secret, Some(_), _) => "configured",
|
||||
(Sensitivity::Secret, None, true) => "required/blank",
|
||||
(Sensitivity::Secret, None, false) => "blank",
|
||||
(_, Some(default), _) => default,
|
||||
(_, None, true) => "required/blank",
|
||||
(_, None, false) => "blank",
|
||||
};
|
||||
let bounds = match (field.minimum, field.maximum) {
|
||||
(Some(minimum), Some(maximum)) => format!("{minimum}..={maximum}"),
|
||||
_ => "-".to_owned(),
|
||||
};
|
||||
output.push_str(&format!(
|
||||
"| `{}` | `{}` | `{:?}` | `{}/{}` | `{}` | `{}` | `{:?}` | `{:?}` |\n",
|
||||
field.env_name,
|
||||
field.semantic_path,
|
||||
field.process,
|
||||
field.value_type,
|
||||
unit,
|
||||
default,
|
||||
bounds,
|
||||
field.sensitivity,
|
||||
field.mode,
|
||||
));
|
||||
}
|
||||
output.push('\n');
|
||||
output.push_str(DOC_END_MARKER);
|
||||
output.push('\n');
|
||||
output
|
||||
}
|
||||
|
||||
pub fn replace_marked(content: &str, begin: &str, end: &str, replacement: &str) -> Option<String> {
|
||||
let start = content.find(begin)?;
|
||||
let tail = &content[start..];
|
||||
let end_offset = tail.find(end)? + end.len();
|
||||
let suffix_start = start + end_offset;
|
||||
let mut rendered = String::with_capacity(content.len() + replacement.len());
|
||||
rendered.push_str(&content[..start]);
|
||||
rendered.push_str(replacement.trim_end());
|
||||
rendered.push_str(&content[suffix_start..]);
|
||||
Some(rendered)
|
||||
}
|
||||
|
||||
fn example_value(field: &FieldSpec, production: bool) -> String {
|
||||
if field.sensitivity == Sensitivity::Secret {
|
||||
return String::new();
|
||||
}
|
||||
match (field.env_name, production) {
|
||||
("CRANK_ENVIRONMENT", true) => "production".to_owned(),
|
||||
("CRANK_BASE_URL", _) => "http://localhost:3000".to_owned(),
|
||||
("POSTGRES_HOST", true) => "postgres".to_owned(),
|
||||
("CRANK_TRUST_FORWARDED_HEADERS", true) => "true".to_owned(),
|
||||
_ => field.default.unwrap_or("").to_owned(),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user