difftreelog
feat unify shared and host secret handling
in: trunk
8 files changed
cmds/fleet/src/cmds/secrets/mod.rsdiffbeforeafterboth--- a/cmds/fleet/src/cmds/secrets/mod.rs
+++ b/cmds/fleet/src/cmds/secrets/mod.rs
@@ -8,9 +8,7 @@
use chrono::{DateTime, Utc};
use clap::Parser;
use fleet_base::{
- fleetdata::{
- FleetHostSecret, FleetSecretData, FleetSecretPart, FleetSharedSecret, encrypt_secret_data,
- },
+ fleetdata::{FleetSecretData, FleetSecretDistribution, FleetSecretPart, encrypt_secret_data},
host::Config,
opts::FleetOpts,
secret::{Expectations, RegenerationReason, SharedSecretDefinition, secret_needs_regeneration},
@@ -28,101 +26,25 @@
AddManager,
/// Force load host keys for all defined hosts
ForceKeys,
- /// Add secret, data should be provided in stdin
- AddShared {
- /// Secret name
+ /// Read secret from remote host, requires sudo on one of the owning hosts
+ Read {
+ /// Secret name to read
name: String,
- /// Secret owners
- #[clap(long, short)]
- machines: Vec<String>,
- /// Override secret if already present
- #[clap(long)]
- force: bool,
- /// Secret public part
- #[clap(long)]
- public: Option<String>,
- /// Load public part from specified file
- #[clap(long)]
- public_file: Option<PathBuf>,
-
- /// Create a notification on secret expiration
- #[clap(long)]
- expires_at: Option<DateTime<Utc>>,
- /// Secret with this name already exists, override its value while keeping the same owners.
- #[clap(long)]
- re_add: bool,
-
- /// How to name public secret part
- #[clap(long, short = 'p', default_value = "public")]
- public_part: String,
- /// How to name private secret part
- #[clap(short = 's', long, default_value = "secret")]
- part: String,
- },
- /// Add secret, data should be provided in stdin
- Add {
- /// Secret name
- name: String,
- /// Secret owner
+ /// Distribution with what machine to read
+ /// If not shared between multiple - defaults to single owner
#[clap(short = 'm', long)]
- machine: String,
- /// Replace secret if already present
- #[clap(long)]
- replace: bool,
- /// Add new parts to existing secret
- #[clap(long)]
- merge: bool,
- /// Secret public part
- #[clap(long)]
- public: Option<String>,
- /// Load public part from specified file
- #[clap(long)]
- public_file: Option<PathBuf>,
+ machine: Option<String>,
- /// How to name public secret part
- #[clap(short = 'p', long, default_value = "public")]
- public_part: String,
- /// How to name private secret part
- #[clap(short = 's', long, default_value = "secret")]
- part: String,
- },
- /// Read secret from remote host, requires sudo on said host
- Read {
- name: String,
- #[clap(short = 'm', long)]
- machine: String,
-
/// Which private secret part to read
#[clap(short = 'p', long, default_value = "secret")]
part: String,
- },
- /// Read secret from remote host, requires sudo on said host
- ReadShared {
- name: String,
- /// Which private secret part to read
- #[clap(short = 'p', long, default_value = "secret")]
- part: String,
+
/// Which host should we use to decrypt, in case if reencryption is required, without
/// regeneration
#[clap(long)]
prefer_identities: Vec<String>,
},
- UpdateShared {
- name: String,
-
- #[clap(short = 'm', long)]
- machine: Option<Vec<String>>,
-
- #[clap(long)]
- add_machine: Vec<String>,
- #[clap(long)]
- remove_machine: Vec<String>,
-
- /// Which host should we use to decrypt
- #[clap(long)]
- prefer_identities: Vec<String>,
- },
Regenerate {
/// Which host should we use to decrypt, in case if reencryption is required, without
/// regeneration
@@ -152,11 +74,11 @@
async fn maybe_regenerate_shared_secret(
secret_name: &str,
config: &Config,
- mut secret: FleetSharedSecret,
+ mut secret: FleetSecretDistribution,
definition: SharedSecretDefinition,
prefer_identities: &[String],
expectations: &Expectations,
-) -> Result<FleetSharedSecret> {
+) -> Result<FleetSecretDistribution> {
let reason = secret_needs_regeneration(&secret.secret, &secret.owners, expectations);
let value = definition.definition_value();
@@ -397,9 +319,9 @@
display_name: &str,
secret: SharedSecretDefinition,
expectations: &Expectations,
-) -> Result<FleetSharedSecret> {
+) -> Result<FleetSecretDistribution> {
// let owners: Vec<String> = nix_go_json!(secret.expectedOwners);
- Ok(FleetSharedSecret {
+ Ok(FleetSecretDistribution {
managed: Some(true),
secret: generate(
config,
@@ -504,177 +426,41 @@
config.key(&host.name).await?;
}
}
- Secret::AddShared {
- machines,
- name,
- force,
- public,
- public_part: public_name,
- public_file,
- expires_at,
- re_add,
- part: part_name,
- } => {
- let mut machines: BTreeSet<String> = machines.into_iter().collect();
- // TODO: Forbid updating secrets with set expectedOwners (= not user-managed).
-
- if let Some(old_shared) = config.shared_secret(&name)? {
- if !force && !re_add {
- bail!("secret already defined");
- };
- if old_shared.managed.unwrap_or(false) {
- bail!("secret is marked as managed, should not be updated manually");
- };
- if re_add {
- // Fixme: use clap to limit this usage
- ensure!(!force, "--force and --readd are not compatible");
- ensure!(
- machines.is_empty(),
- "you can't use machines argument for --readd"
- );
- machines = old_shared.owners;
- }
- } else if re_add {
- bail!("secret doesn't exists");
- };
-
- let recipients = config
- .recipients(machines.iter().cloned().collect())
- .await?;
-
- let mut parts = BTreeMap::new();
-
- let mut input = vec![];
- io::stdin().read_to_end(&mut input)?;
-
- if !input.is_empty() {
- let encrypted = encrypt_secret_data(recipients.iter(), input)
- .ok_or_else(|| anyhow!("no recipients provided"))?;
- parts.insert(part_name, FleetSecretPart { raw: encrypted });
- }
-
- if let Some(public) = parse_public(public, public_file).await? {
- parts.insert(public_name, FleetSecretPart { raw: public });
- }
-
- config.replace_shared(
- name,
- FleetSharedSecret {
- managed: Some(false),
- owners: machines,
- secret: FleetSecretData {
- created_at: Utc::now(),
- expires_at,
- parts,
- generation_data: serde_json::Value::Null,
- },
- },
- );
- }
- Secret::Add {
- machine,
- name,
- replace,
- merge,
- public,
- public_part: public_name,
- public_file,
- part: part_name,
- } => {
- if config.has_secret(&machine, &name) && !replace && !merge {
- bail!(
- "secret already defined.\nUse --replace to override, or --merge to add new parts to existing secret"
- );
- }
-
- let mut out = if merge && !replace {
- config
- .host_secret(&machine, &name)
- .context("failed to read existing secret for --merge")?
- } else {
- FleetHostSecret {
- managed: Some(false),
- secret: FleetSecretData {
- created_at: Utc::now(),
- expires_at: None,
- parts: BTreeMap::new(),
- generation_data: serde_json::Value::Null,
- },
- }
- };
- if out.managed.unwrap_or(false) {
- bail!("secret is managed by fleet and should not be updated manually");
- }
- out.managed = Some(false);
-
- if let Some(secret) = parse_secret().await? {
- let recipient = config.recipient(&machine).await?;
- let encrypted =
- encrypt_secret_data([&recipient], secret).expect("recipient provided");
- if out
- .secret
- .parts
- .insert(part_name.clone(), FleetSecretPart { raw: encrypted })
- .is_some() && !replace
- {
- bail!(
- "part {part_name:?} is already defined, use --replace if you wish to replace it"
- );
- }
- }
-
- if let Some(public) = parse_public(public, public_file).await? {
- if out
- .secret
- .parts
- .insert(public_name.clone(), FleetSecretPart { raw: public })
- .is_some() && !replace
- {
- bail!(
- "part {public_name:?} is already defined, use --replace if you wish to replace it"
- );
- }
- };
-
- config.insert_secret(&machine, name, out);
- }
- #[allow(clippy::await_holding_refcell_ref)]
Secret::Read {
name,
machine,
part: part_name,
+ mut prefer_identities,
} => {
- let secret = config.host_secret(&machine, &name)?;
- let Some(secret) = secret.secret.parts.get(&part_name) else {
- bail!("no part {part_name} in secret {name}");
+ let Some(secret) = config.shared_secret(&name) else {
+ bail!("secret doesn't exists");
};
- let data = if secret.raw.encrypted {
- let host = config.host(&machine).await?;
- host.decrypt(secret.raw.clone()).await?
+
+ let dist = if secret.len() == 1 {
+ &secret[0]
+ } else if let Some(machine) = machine {
+ let dist = secret.get(&machine);
+ let Some(dist) = dist else {
+ bail!("machine {machine} has no distribution of secret {name}");
+ };
+ prefer_identities.push(machine);
+ dist
} else {
- secret.raw.data.clone()
+ bail!(
+ "secret {name} has shares, but no --machine specified for specifing which do you need"
+ )
};
- stdout().write_all(&data)?;
- }
- Secret::ReadShared {
- name,
- part: part_name,
- prefer_identities,
- } => {
- let Some(secret) = config.shared_secret(&name)? else {
- bail!("secret doesn't exists");
- };
- let Some(part) = secret.secret.parts.get(&part_name) else {
+ let Some(part) = dist.secret.parts.get(&part_name) else {
bail!("no part {part_name} in secret {name}");
};
let data = if part.raw.encrypted {
let identity_holder = if !prefer_identities.is_empty() {
prefer_identities
.iter()
- .find(|i| secret.owners.iter().any(|s| s == *i))
+ .find(|i| dist.owners.iter().any(|s| s == *i))
} else {
- secret.owners.first()
+ dist.owners.first()
};
let Some(identity_holder) = identity_holder else {
bail!("no available holder found");
@@ -686,198 +472,148 @@
};
stdout().write_all(&data)?;
}
- Secret::UpdateShared {
- name,
- machine,
- add_machine,
- remove_machine,
- prefer_identities,
- } => {
- // TODO: Forbid updating secrets with set expectedOwners (= not user-managed).
-
- let Some(secret) = config.shared_secret(&name)? else {
- bail!("secret doesn't exists");
- };
- if secret.secret.parts.values().all(|v| !v.raw.encrypted) {
- bail!("no secret");
- }
-
- let initial_machines = secret.owners.clone();
- let target_machines = parse_machines(
- initial_machines.clone(),
- machine,
- add_machine,
- remove_machine,
- )?;
-
- if target_machines.is_empty() {
- info!("no machines left for secret, removing it");
- config.remove_shared(&name);
- return Ok(());
- }
-
- let definition = config.shared_secret_definition(&name)?;
- let expectations = definition
- .expectations()
- .with_context(|| format!("expectations for shared {name:?}"))?;
-
- let updated = maybe_regenerate_shared_secret(
- &name,
- config,
- secret,
- definition,
- &prefer_identities,
- &expectations,
- )
- .await?;
- config.replace_shared(name, updated);
- }
Secret::Regenerate {
prefer_identities,
skip_hosts,
} => {
- info!("checking for secrets to regenerate");
- let expected_shared_set = config
- .list_configured_shared()
- .await?
- .into_iter()
- .collect::<HashSet<_>>();
- let stored_shared_set = config.list_shared().into_iter().collect::<HashSet<_>>();
- {
- // Generate missing shared
- let _span = info_span!("shared").entered();
- for missing in expected_shared_set.difference(&stored_shared_set) {
- let definition = config.shared_secret_definition(missing)?;
- if !definition.is_managed()? {
- info!("skipping unmanaged secret: {missing}");
- continue;
- }
- let expectations = definition
- .expectations()
- .with_context(|| format!("expectations for shared {missing:?}"))?;
- info!("generating secret: {missing}");
- let shared = generate_shared(config, missing, definition, &expectations)
- .in_current_span()
- .await?;
- config.replace_shared(missing.to_string(), shared)
- }
- }
- if !skip_hosts {
- for host in config.list_hosts().await? {
- if opts.should_skip(&host).await? {
- continue;
- }
+ /*
+ info!("checking for secrets to regenerate");
+ let expected_shared_set = config
+ .list_configured_shared()
+ .await?
+ .into_iter()
+ .collect::<HashSet<_>>();
+ let stored_shared_set = config.list_secrets().into_iter().collect::<HashSet<_>>();
+ {
+ // Generate missing shared
+ let _span = info_span!("shared").entered();
+ for missing in expected_shared_set.difference(&stored_shared_set) {
+ let definition = config.shared_secret_definition(missing)?;
+ if !definition.is_managed()? {
+ info!("skipping unmanaged secret: {missing}");
+ continue;
+ }
+ let expectations = definition
+ .expectations()
+ .with_context(|| format!("expectations for shared {missing:?}"))?;
+ info!("generating secret: {missing}");
+ let shared = generate_shared(config, missing, definition, &expectations)
+ .in_current_span()
+ .await?;
+ config.replace_shared(missing.to_string(), shared)
+ }
+ }
+ if !skip_hosts {
+ for host in config.list_hosts().await? {
+ if opts.should_skip(&host).await? {
+ continue;
+ }
- let _span = info_span!("host", host = host.name).entered();
- let expected_set = host
- .list_defined_secrets()?
- .into_iter()
- .collect::<HashSet<_>>();
- let stored_set = config
- .list_secrets(&host.name)
- .into_iter()
- .collect::<HashSet<_>>();
- for missing_secret in expected_set.difference(&stored_set) {
- let secret = host.secret_definition(missing_secret)?;
- if secret.is_shared()? {
- continue;
- }
- info!("generating missing secret: {missing_secret}");
- let expectations = secret.expectations().with_context(|| {
- format!("expectations for {missing_secret:?} of {:?}", host.name)
- })?;
- let generated = match generate(
- config,
- missing_secret,
- secret.definition_value()?,
- &expectations,
- )
- .in_current_span()
- .await
- {
- Ok(v) => v,
- Err(e) => {
- error!("{e:?}");
- continue;
- }
- };
- config.insert_secret(
- &host.name,
- missing_secret.to_string(),
- FleetHostSecret {
- managed: Some(true),
- secret: generated,
- },
- )
- }
- for known_secret in stored_set.intersection(&expected_set) {
- let secret = host.secret_definition(known_secret)?;
- if secret.is_shared()? {
- continue;
- }
- info!("updating secret: {known_secret}");
- let data = config.host_secret(&host.name, known_secret)?;
- let expectations = secret.expectations()?;
- if let Some(regen_reason) = data.needs_regeneration(&expectations) {
- info!("needs regeneration: {regen_reason}");
- let generated = match generate(
- config,
- known_secret,
- secret.definition_value()?,
- &expectations,
- )
- .in_current_span()
- .await
- {
- Ok(v) => v,
- Err(e) => {
- error!("{e:?}");
- continue;
+ let _span = info_span!("host", host = host.name).entered();
+ let expected_set = host
+ .list_defined_secrets()?
+ .into_iter()
+ .collect::<HashSet<_>>();
+ let stored_set = config
+ .list_secrets_for_owner(&host.name)
+ .into_iter()
+ .collect::<HashSet<_>>();
+ for missing_secret in expected_set.difference(&stored_set) {
+ let secret = host.secret_definition(missing_secret)?;
+ if secret.is_shared()? {
+ continue;
+ }
+ info!("generating missing secret: {missing_secret}");
+ let expectations = secret.expectations().with_context(|| {
+ format!("expectations for {missing_secret:?} of {:?}", host.name)
+ })?;
+ let generated = match generate(
+ config,
+ missing_secret,
+ secret.definition_value()?,
+ &expectations,
+ )
+ .in_current_span()
+ .await
+ {
+ Ok(v) => v,
+ Err(e) => {
+ error!("{e:?}");
+ continue;
+ }
+ };
+ config.insert_secret(host.name, missing_secret.to_string(), generated)
+ }
+ for known_secret in stored_set.intersection(&expected_set) {
+ let secret = host.secret_definition(known_secret)?;
+ if secret.is_shared()? {
+ continue;
+ }
+ info!("updating secret: {known_secret}");
+ let data = config.host_secret(&host.name, known_secret)?;
+ let expectations = secret.expectations()?;
+ if let Some(regen_reason) = data.needs_regeneration(&expectations) {
+ info!("needs regeneration: {regen_reason}");
+ let generated = match generate(
+ config,
+ known_secret,
+ secret.definition_value()?,
+ &expectations,
+ )
+ .in_current_span()
+ .await
+ {
+ Ok(v) => v,
+ Err(e) => {
+ error!("{e:?}");
+ continue;
+ }
+ };
+ config.insert_secret(
+ &host.name,
+ known_secret.to_string(),
+ FleetLegacyHostSecret {
+ managed: Some(true),
+ secret: generated,
+ },
+ )
+ }
+ }
+ for removed_secret in stored_set.difference(&expected_set) {
+ let definition = host.secret_definition(removed_secret)?;
+ if definition.is_shared()? {
+ continue;
+ }
+ info!("removing secret: {removed_secret}");
+ config.remove_secret(&host.name, removed_secret);
+ }
}
- };
- config.insert_secret(
- &host.name,
- known_secret.to_string(),
- FleetHostSecret {
- managed: Some(true),
- secret: generated,
- },
- )
- }
- }
- for removed_secret in stored_set.difference(&expected_set) {
- let definition = host.secret_definition(removed_secret)?;
- if definition.is_shared()? {
- continue;
- }
- info!("removing secret: {removed_secret}");
- config.remove_secret(&host.name, removed_secret);
- }
- }
- }
- for known_secret in stored_shared_set.intersection(&expected_shared_set) {
- info!("updating shared secret: {known_secret}");
- let data = config.shared_secret(known_secret)?.expect("exists");
+ }
+ for known_secret in stored_shared_set.intersection(&expected_shared_set) {
+ info!("updating shared secret: {known_secret}");
+ let data = config.shared_secret(known_secret)?.expect("exists");
- let definition = config.shared_secret_definition(known_secret)?;
- let expectations = definition.expectations()?;
- config.replace_shared(
- known_secret.to_owned(),
- maybe_regenerate_shared_secret(
- known_secret,
- config,
- data,
- definition,
- &prefer_identities,
- &expectations,
- )
- .await?,
- );
- }
- for removed_secret in stored_shared_set.difference(&expected_shared_set) {
- info!("removing shared secret: {removed_secret}");
- config.remove_shared(removed_secret);
- }
+ let definition = config.shared_secret_definition(known_secret)?;
+ let expectations = definition.expectations()?;
+ config.replace_shared(
+ known_secret.to_owned(),
+ maybe_regenerate_shared_secret(
+ known_secret,
+ config,
+ data,
+ definition,
+ &prefer_identities,
+ &expectations,
+ )
+ .await?,
+ );
+ }
+ for removed_secret in stored_shared_set.difference(&expected_shared_set) {
+ info!("removing shared secret: {removed_secret}");
+ config.remove_shared(removed_secret);
+ }
+ */
+ todo!()
}
Secret::List {} => {
let _span = info_span!("loading secrets").entered();
@@ -892,12 +628,11 @@
let mut table = vec![];
for name in configured.iter().cloned() {
let config = config.clone();
- let data = config.shared_secret(&name)?.expect("exists");
+ let data = config.shared_secret(&name).expect("exists");
let definition = config.shared_secret_definition(&name)?;
let expectations = definition.expectations()?;
let owners = data
- .owners
- .iter()
+ .owners()
.map(|o| {
if expectations.owners.contains(o) {
o.green().to_string()
@@ -919,7 +654,9 @@
part,
add,
} => {
- let secret = config.host_secret(&machine, &name)?;
+ let secret = config
+ .host_secret(&machine, &name)
+ .context("secret not found")?;
if let Some(data) = secret.secret.parts.get(&part) {
let host = config.host(&machine).await?;
let secret = host.decrypt(data.raw.clone()).await?;
crates/fleet-base/src/fleetdata.rsdiffbeforeafterboth--- a/crates/fleet-base/src/fleetdata.rs
+++ b/crates/fleet-base/src/fleetdata.rs
@@ -1,6 +1,10 @@
use std::{
- collections::{BTreeMap, BTreeSet},
+ collections::{
+ BTreeMap, BTreeSet,
+ btree_map::{self, Entry},
+ },
io::{self, Cursor},
+ ops::Deref,
};
use age::Recipient;
@@ -10,10 +14,12 @@
distr::{Alphanumeric, SampleString as _},
rng,
};
-use serde::{Deserialize, Serialize, de::Error};
+use serde::{
+ Deserialize, Serialize,
+ de::{self, Error},
+};
use serde_json::Value;
-
-use crate::secret::{Expectations, RegenerationReason, secret_needs_regeneration};
+use tracing::info;
#[derive(Serialize, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
@@ -72,17 +78,29 @@
#[serde(default)]
pub hosts: BTreeMap<String, HostData>,
+
+ #[serde(default, alias = "shared_secrets")]
+ pub secrets: FleetSecrets,
+
+ // extra_name => anything
#[serde(default)]
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
- pub shared_secrets: BTreeMap<String, FleetSharedSecret>,
- #[serde(default)]
- #[serde(skip_serializing_if = "BTreeMap::is_empty")]
- pub host_secrets: BTreeMap<String, BTreeMap<String, FleetHostSecret>>,
+ pub extra: BTreeMap<String, Value>,
- // extra_name => anything
#[serde(default)]
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
- pub extra: BTreeMap<String, Value>,
+ host_secrets: BTreeMap<String, BTreeMap<String, FleetSecretDistribution>>,
+}
+impl FleetData {
+ pub fn from_str(s: &str) -> anyhow::Result<Self> {
+ let mut data: Self = nixlike::parse_str(s)?;
+ if !data.host_secrets.is_empty() {
+ info!("migrating host secrets into shared secrets structure");
+ data.secrets
+ .merge_from_hosts(std::mem::take(&mut data.host_secrets));
+ }
+ Ok(data)
+ }
}
/// Returns None if recipients.is_empty()
@@ -129,27 +147,276 @@
#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[must_use]
-pub struct FleetHostSecret {
+pub struct FleetSecretDistribution {
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub managed: Option<bool>,
+ #[serde(default)]
+ pub owners: BTreeSet<String>,
#[serde(flatten)]
pub secret: FleetSecretData,
}
-impl FleetHostSecret {
- pub fn needs_regeneration(&self, expectations: &Expectations) -> Option<RegenerationReason> {
- secret_needs_regeneration(&self.secret, &expectations.owners, expectations)
+
+#[derive(Clone)]
+#[must_use]
+pub struct FleetSecretDistributions(Vec<FleetSecretDistribution>);
+
+impl Deref for FleetSecretDistributions {
+ type Target = [FleetSecretDistribution];
+
+ fn deref(&self) -> &Self::Target {
+ self.0.as_slice()
+ }
+}
+
+impl FleetSecretDistributions {
+ pub fn owners(&self) -> impl Iterator<Item = &String> {
+ self.0.iter().flat_map(|v| v.owners.iter())
+ }
+ #[allow(
+ clippy::len_without_is_empty,
+ reason = "should not be empty for a long time"
+ )]
+ pub fn len(&self) -> usize {
+ self.0.len()
+ }
+
+ pub fn get(&self, owner: &str) -> Option<&FleetSecretDistribution> {
+ self.0.iter().find(|d| d.owners.contains(owner))
+ }
+ fn entry(&mut self, owner: String) -> DistEntry<'_> {
+ let Some(idx) = self.0.iter().position(|d| d.owners.contains(&owner)) else {
+ return DistEntry::Vacant(VacantDistEntry {
+ distributions: self,
+ owner,
+ });
+ };
+ DistEntry::Occupied(OccupiedDistEntry {
+ distributions: self,
+ idx,
+ owner,
+ })
+ }
+ fn extend(&mut self, dist: FleetSecretDistribution) {
+ for owner in &dist.owners {
+ self.entry(owner.to_owned()).remove();
+ }
+ self.0.push(dist);
+ }
+ pub fn contains(&self, owner: &str) -> bool {
+ self.0.iter().any(|d| d.owners.contains(owner))
+ }
+}
+
+struct OccupiedDistEntry<'d> {
+ distributions: &'d mut FleetSecretDistributions,
+ idx: usize,
+ owner: String,
+}
+impl<'d> OccupiedDistEntry<'d> {
+ fn remove(self) -> VacantDistEntry<'d> {
+ let dist = &mut self.distributions.0[self.idx];
+ assert!(
+ dist.owners.remove(&self.owner),
+ "entry exists, as we have its reference"
+ );
+ if dist.owners.is_empty() {
+ self.distributions.0.remove(self.idx);
+ }
+ VacantDistEntry {
+ distributions: self.distributions,
+ owner: self.owner,
+ }
+ }
+ fn set(self, secret: FleetSecretData) -> Self {
+ self.remove().set(secret)
}
}
+struct VacantDistEntry<'d> {
+ distributions: &'d mut FleetSecretDistributions,
+ owner: String,
+}
+impl<'d> VacantDistEntry<'d> {
+ fn set(self, secret: FleetSecretData) -> OccupiedDistEntry<'d> {
+ let Self {
+ distributions,
+ owner,
+ } = self;
+ let idx = distributions.0.len();
+ distributions.0.push(FleetSecretDistribution {
+ managed: None,
+ owners: BTreeSet::from_iter([owner.clone()]),
+ secret,
+ });
+ OccupiedDistEntry {
+ distributions,
+ owner,
+ idx,
+ }
+ }
+}
-#[derive(Serialize, Deserialize, Clone)]
-#[serde(rename_all = "camelCase")]
-#[must_use]
-pub struct FleetSharedSecret {
- #[serde(default)]
- #[serde(skip_serializing_if = "Option::is_none")]
- pub managed: Option<bool>,
- pub owners: BTreeSet<String>,
- #[serde(flatten)]
- pub secret: FleetSecretData,
+enum DistEntry<'d> {
+ Vacant(VacantDistEntry<'d>),
+ Occupied(OccupiedDistEntry<'d>),
+}
+impl DistEntry<'_> {
+ fn remove(self) -> Self {
+ match self {
+ DistEntry::Vacant(_) => self,
+ DistEntry::Occupied(o) => Self::Vacant(o.remove()),
+ }
+ }
+ fn set(self, secret: FleetSecretData) -> Self {
+ Self::Occupied(match self {
+ DistEntry::Vacant(e) => e.set(secret),
+ DistEntry::Occupied(e) => e.set(secret),
+ })
+ }
+}
+
+impl Serialize for FleetSecretDistributions {
+ fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
+ where
+ S: serde::Serializer,
+ {
+ let mut found_hosts = BTreeSet::new();
+ for ele in self.0.iter() {
+ if ele.owners.is_empty() {
+ panic!("consistency: secret distribution has no defined owners");
+ }
+ for ele in ele.owners.iter() {
+ if !found_hosts.insert(ele) {
+ panic!(
+ "consistency: secret distribution contains duplicate entry for the same host",
+ );
+ }
+ }
+ }
+ match self.0.len() {
+ 0 => panic!("consistency: empty distributions"),
+ 1 => self.0[0].serialize(serializer),
+ _ => self.0.serialize(serializer),
+ }
+ }
+}
+impl<'de> Deserialize<'de> for FleetSecretDistributions {
+ fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
+ where
+ D: serde::Deserializer<'de>,
+ {
+ #[derive(Deserialize)]
+ #[serde(untagged)]
+ enum Distributions {
+ One(FleetSecretDistribution),
+ Many(Vec<FleetSecretDistribution>),
+ }
+ let d = Distributions::deserialize(deserializer)?;
+ let ds = match d {
+ Distributions::One(d) => vec![d],
+ Distributions::Many(ds) => ds,
+ };
+ if ds.is_empty() {
+ return Err(de::Error::custom("consistency: empty distributions"));
+ }
+ let mut found_hosts = BTreeSet::new();
+ for ele in ds.iter() {
+ if ele.owners.is_empty() {
+ return Err(de::Error::custom(
+ "consistency: secret distribution has no defined owners",
+ ));
+ }
+ for ele in ele.owners.iter() {
+ if !found_hosts.insert(ele) {
+ return Err(de::Error::custom(
+ "consistency: secret distribution contains duplicate entry for the same host",
+ ));
+ }
+ }
+ }
+ Ok(Self(ds))
+ }
+}
+
+#[derive(Serialize, Deserialize, Default)]
+pub struct FleetSecrets(BTreeMap<String, FleetSecretDistributions>);
+
+impl FleetSecrets {
+ pub fn keys(&self) -> btree_map::Keys<String, FleetSecretDistributions> {
+ self.0.keys()
+ }
+
+ pub fn keys_for_owner(&self, owner: &str) -> impl Iterator<Item = &String> {
+ self.0
+ .iter()
+ .filter(|(_, d)| d.contains(owner))
+ .map(|(n, _)| n)
+ }
+
+ pub fn drop_owner_no_reencrypt(&mut self, secret: &str, owner: &str) -> bool {
+ let Entry::Occupied(mut dists) = self.0.entry(secret.to_owned()) else {
+ return false;
+ };
+ let DistEntry::Occupied(dist) = dists.get_mut().entry(owner.to_owned()) else {
+ return false;
+ };
+
+ dist.remove();
+
+ if dists.get().0.is_empty() {
+ dists.remove();
+ };
+
+ true
+ }
+ pub fn set_single_data(&mut self, secret: String, owner: String, data: FleetSecretData) {
+ let e = self
+ .0
+ .entry(secret.to_owned())
+ .or_insert_with(|| FleetSecretDistributions(Default::default()));
+ e.entry(owner.to_owned()).set(data);
+ }
+ pub fn set_data(&mut self, secret: String, data: FleetSecretDistribution) {
+ match self.0.entry(secret) {
+ Entry::Vacant(e) => {
+ e.insert(FleetSecretDistributions(vec![data]));
+ }
+ Entry::Occupied(mut e) => {
+ let dists = e.get_mut();
+ dists.extend(data)
+ }
+ }
+ }
+ pub fn get_single(&self, secret: &str, owner: &str) -> Option<&FleetSecretDistribution> {
+ let secret = self.0.get(secret)?;
+ secret.get(owner)
+ }
+ pub fn get(&self, secret: &str) -> Option<&FleetSecretDistributions> {
+ self.0.get(secret)
+ }
+
+ pub fn contains_for_owner(&self, secret: &str, owner: &str) -> bool {
+ let Some(secret) = self.0.get(secret) else {
+ return false;
+ };
+ secret.contains(owner)
+ }
+ pub fn contains(&self, secret: &str) -> bool {
+ self.0.contains_key(secret)
+ }
+ pub fn remove(&mut self, secret: &str) {
+ self.0.remove(secret);
+ }
+
+ fn merge_from_hosts(
+ &mut self,
+ host_secrets: BTreeMap<String, BTreeMap<String, FleetSecretDistribution>>,
+ ) {
+ for (host, host_secrets) in host_secrets {
+ for (secret_name, mut secret_data) in host_secrets {
+ secret_data.owners.insert(host.clone());
+ self.set_data(secret_name, secret_data);
+ }
+ }
+ }
}
crates/fleet-base/src/host.rsdiffbeforeafterboth1use std::{2 cell::OnceCell,3 collections::BTreeSet,4 ffi::{OsStr, OsString},5 fmt::Display,6 io::Write,7 ops::Deref,8 path::PathBuf,9 str::FromStr,10 sync::{Arc, Mutex, MutexGuard, OnceLock},11};1213use anyhow::{Context, Result, anyhow, bail, ensure};14use fleet_shared::SecretData;15use nix_eval::{Value, nix_go, nix_go_json, util::assert_warn};16use openssh::{ControlPersist, SessionBuilder};17use serde::de::DeserializeOwned;18use tabled::Tabled;19use tempfile::NamedTempFile;20use time::{UtcDateTime, format_description};21use tracing::warn;2223use crate::{24 command::MyCommand,25 fleetdata::{FleetData, FleetSecretData, FleetSecretDistribution, FleetSecretDistributions},26 secret::{HostSecretDefinition, SharedSecretDefinition},27};2829pub struct FleetConfigInternals {30 /// Fleet project directory, containing fleet.nix file.31 pub directory: PathBuf,32 /// builtins.currentSystem33 pub local_system: String,34 pub data: Mutex<FleetData>,35 pub nix_args: Vec<OsString>,36 /// fleet_config.config37 pub config_field: Value,38 /// flake.output39 pub flake_outputs: Value,40 // TODO: Remove with connectivity refactor41 pub localhost: String,4243 /// import nixpkgs {system = local};44 pub default_pkgs: Value,45 /// inputs.nixpkgs46 pub nixpkgs: Value,47}4849// TODO: Make field not pub50#[derive(Clone)]51pub struct Config(pub Arc<FleetConfigInternals>);5253impl Deref for Config {54 type Target = FleetConfigInternals;5556 fn deref(&self) -> &Self::Target {57 &self.058 }59}6061#[derive(Clone, Copy, Debug)]62pub enum EscalationStrategy {63 Sudo,64 Run0,65 Su,66}6768#[derive(Clone, PartialEq, Copy, Debug)]69pub enum DeployKind {70 /// NixOS => NixOS managed by fleet71 UpgradeToFleet,72 /// NixOS managed by fleet => NixOS managed by fleet73 Fleet,74 /// Remote host has /mnt, /mnt/boot mounted,75 /// generated config is added to fleet configuration.76 NixosInstall,77 /// Remote host has some system and nix installed in multi-user mode (/nix is owned by root),78 /// generated config is added to fleet configuration,79 /// and /etc/NIXOS_LUSTRATE exists, fleet will perform the rest.80 NixosLustrate,81}8283impl FromStr for DeployKind {84 type Err = anyhow::Error;85 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {86 match s {87 "upgrade-to-fleet" => Ok(Self::UpgradeToFleet),88 "fleet" => Ok(Self::Fleet),89 "nixos-install" => Ok(Self::NixosInstall),90 "nixos-lustrate" => Ok(Self::NixosLustrate),91 v => bail!(92 "unknown deploy_kind: {v}; expected on of \"upgrade-to-fleet\", \"fleet\", \"nixos-install\", \"nixos-lustrate\""93 ),94 }95 }96}97pub struct ConfigHost {98 config: Config,99 pub name: String,100 groups: OnceCell<Vec<String>>,101102 // TODO: Both of those values are taken from host opts, there should be a cleaner way to specify it103 deploy_kind: OnceCell<DeployKind>,104 session_destination: OnceCell<String>,105 legacy_ssh_store: OnceCell<bool>,106107 pub host_config: Option<Value>,108 pub nixos_config: OnceCell<Value>,109 pub nixos_unchecked_config: OnceCell<Value>,110 pub pkgs_override: Option<Value>,111112 // TODO: Move command helpers away with connectivity refactor113 pub local: bool,114 pub session: OnceLock<Arc<openssh::Session>>,115}116117#[derive(Debug, Clone, Copy)]118pub enum GenerationStorage {119 Deployer,120 Machine,121 Pusher,122}123impl GenerationStorage {124 fn prefix(&self) -> &'static str {125 match self {126 GenerationStorage::Deployer => "deployer.",127 GenerationStorage::Machine => "",128 GenerationStorage::Pusher => "pusher.",129 }130 }131}132133#[derive(Tabled, Debug)]134pub struct Generation {135 #[tabled(rename = "ID", format("{}", self.rollback_id()))]136 pub id: u32,137 #[tabled(rename = "Current")]138 pub current: bool,139 #[tabled(rename = "Created at")]140 pub datetime: UtcDateTime,141 #[tabled(format = "{:?}")]142 pub store_path: PathBuf,143 #[tabled(skip)]144 pub location: GenerationStorage,145}146impl Generation {147 pub fn rollback_id(&self) -> String {148 format!("{}{}", self.location.prefix(), self.id)149 }150}151152fn parse_generation_line(g: &str) -> Option<Generation> {153 let mut parts = g.split_whitespace();154 let id = parts.next()?;155 let id: u32 = id.parse().ok()?;156 let date = parts.next()?;157 let time = parts.next()?;158 let current = if let Some(current) = parts.next() {159 if current == "(current)" {160 Some(true)161 } else {162 None163 }164 } else {165 Some(false)166 };167 let current = current?;168 if parts.next().is_some() {169 warn!("unexpected text after generation: {g}");170 }171172 let format = format_description::parse("[year]-[month]-[day] [hour]:[minute]:[second]")173 .expect("valid format");174 let datetime = UtcDateTime::parse(&format!("{date} {time}"), &format).ok()?;175176 Some(Generation {177 id,178 current,179 datetime,180 store_path: PathBuf::new(),181 location: GenerationStorage::Machine,182 })183}184// TODO: Move command helpers away with connectivity refactor185impl ConfigHost {186 pub async fn list_generations(&self, profile: &str) -> Result<Vec<Generation>> {187 let mut cmd = self.cmd("nix-env").await?;188 cmd.comparg("--profile", format!("/nix/var/nix/profiles/{profile}"))189 .arg("--list-generations")190 .env("TZ", "UTC");191 // Sudo is required because --list-generations tries to acquire profile lock192 let data = cmd.sudo().run_string().await?;193 let mut generations = data194 .split('\n')195 .map(|e| e.trim())196 .filter(|&l| !l.is_empty())197 .filter_map(|g| {198 let generation = parse_generation_line(g);199 if generation.is_none() {200 warn!("bad generation: {g}");201 };202 generation203 })204 .collect::<Vec<_>>();205 for ele in generations.iter_mut() {206 let mut cmd = self.cmd("readlink").await?;207 cmd.arg("--")208 .arg(format!("/nix/var/nix/profiles/{profile}-{}-link", ele.id));209 let path = cmd.run_string().await?;210 ele.store_path = PathBuf::from(path.trim_end_matches("\n"));211 }212213 Ok(generations)214 }215216 pub fn set_session_destination(&self, dest: String) {217 self.session_destination218 .set(dest)219 .expect("session destination is already set")220 }221 pub fn set_deploy_kind(&self, kind: DeployKind) {222 self.deploy_kind223 .set(kind)224 .expect("deploy kind is already set");225 }226 pub fn set_legacy_ssh_store(&self, legacy: bool) {227 self.legacy_ssh_store228 .set(legacy)229 .expect("legacy ssh store is already set")230 }231 pub async fn deploy_kind(&self) -> Result<DeployKind> {232 if let Some(kind) = self.deploy_kind.get() {233 return Ok(*kind);234 }235 let is_fleet_managed = match self.file_exists("/etc/FLEET_HOST").await {236 Ok(v) => v,237 Err(e) => {238 bail!("failed to query remote system kind: {e}");239 }240 };241 if !is_fleet_managed {242 bail!(243 "{}",244 indoc::indoc! {"245 host is not marked as managed by fleet246 if you're not trying to lustrate/install system from scratch,247 you should either248 1. manually create /etc/FLEET_HOST file on the target host,249 2. use ?deploy_kind=fleet host argument if you're upgrading from older version of fleet250 3. use ?deploy_kind=upgrade_to_fleet if you're upgrading from plain nixos to fleet-managed nixos251 "}252 );253 }254 // TOCTOU is possible255 let _ = self.deploy_kind.set(DeployKind::Fleet);256 Ok(*self.deploy_kind.get().expect("deploy kind is just set"))257 }258 pub async fn escalation_strategy(&self) -> Result<EscalationStrategy> {259 // Prefer sudo, as run0 has some gotchas with polkit260 // and too many repeating prompts.261 if (self.find_in_path("sudo").await).is_ok() {262 return Ok(EscalationStrategy::Sudo);263 }264 if (self.find_in_path("run0").await).is_ok() {265 return Ok(EscalationStrategy::Run0);266 }267 Ok(EscalationStrategy::Su)268 }269 async fn open_session(&self) -> Result<Arc<openssh::Session>> {270 assert!(!self.local, "do not open ssh connection to local session");271 // FIXME: TOCTOU272 if let Some(session) = &self.session.get() {273 return Ok((*session).clone());274 };275 let mut session = SessionBuilder::default();276 session.control_persist(ControlPersist::ClosedAfterInitialConnection);277278 let dest = self.session_destination.get().unwrap_or(&self.name);279 let session = session280 .connect(&dest)281 .await282 .map_err(|e| anyhow!("ssh error while connecting to {}: {e:#?}", self.name))?;283 let session = Arc::new(session);284 self.session.set(session.clone()).expect("TOCTOU happened");285 Ok(session)286 }287 pub async fn mktemp_dir(&self) -> Result<String> {288 let mut cmd = self.cmd("mktemp").await?;289 cmd.arg("-d");290 let path = cmd.run_string().await?;291 Ok(path.trim_end().to_owned())292 }293 pub async fn file_exists(&self, path: impl AsRef<OsStr>) -> Result<bool> {294 let mut cmd = self.cmd("sh").await?;295 cmd.arg("-c")296 .arg("test -e \"$1\" && echo true || echo false")297 .arg("_")298 .arg(path);299 cmd.run_value().await300 }301 pub async fn read_file_bin(&self, path: impl AsRef<OsStr>) -> Result<Vec<u8>> {302 let mut cmd = self.cmd("cat").await?;303 cmd.arg(path);304 cmd.run_bytes().await305 }306 pub async fn read_file_text(&self, path: impl AsRef<OsStr>) -> Result<String> {307 let mut cmd = self.cmd("cat").await?;308 cmd.arg(path);309 cmd.run_string().await310 }311 pub async fn read_dir(&self, path: impl AsRef<OsStr>) -> Result<Vec<String>> {312 let mut cmd = self.cmd("ls").await?;313 cmd.arg(path);314 let out = cmd.run_string().await?;315 let mut lines = out.split('\n');316 if let Some(last) = lines.next_back() {317 ensure!(last.is_empty(), "output of ls should end with newline");318 }319 Ok(lines.map(ToOwned::to_owned).collect())320 }321 #[allow(dead_code)]322 pub async fn read_file_json<D: DeserializeOwned>(&self, path: impl AsRef<OsStr>) -> Result<D> {323 let text = self.read_file_text(path).await?;324 Ok(serde_json::from_str(&text)?)325 }326 pub async fn read_env(&self, env: &str) -> Result<String> {327 let mut cmd = self.cmd("printenv").await?;328 cmd.arg(env);329 cmd.run_string().await330 }331 pub async fn find_in_path(&self, command: &str) -> Result<String> {332 // // `which` is not a part of coreutils, and it might not exist on machine.333 // let path = self.read_env("PATH").await?;334 // // Assuming delimiter is :, we don't work with windows host, this check will be much335 // // more sophisticated in remowt backend (and quicker, since actual PATH search will be done on remote machine)336 // for ele in path.split(':') {337 // let test_path = format!("{ele}/{cmd}");338 // test -x etc339 // }340 // let mut cmd = self.cmd("printenv").await?;341 // cmd.arg(env);342 // Ok(cmd.run_string().await?)343 // Assuming this is an environment issue if which doesn't exist, will be fixed with remowt.344 let mut cmd = self345 .cmd_escalation(346 // Not used347 EscalationStrategy::Su,348 "which",349 )350 .await?;351 cmd.arg(command);352 cmd.run_string().await353 }354 pub async fn read_file_value<D: FromStr>(&self, path: impl AsRef<OsStr>) -> Result<D>355 where356 <D as FromStr>::Err: Display,357 {358 let text = self.read_file_text(path).await?;359 D::from_str(&text).map_err(|e| anyhow!("failed to parse value: {e}"))360 }361 pub async fn cmd(&self, cmd: impl AsRef<OsStr>) -> Result<MyCommand> {362 self.cmd_escalation(self.escalation_strategy().await?, cmd)363 .await364 }365 pub async fn cmd_escalation(366 &self,367 escalation: EscalationStrategy,368 cmd: impl AsRef<OsStr>,369 ) -> Result<MyCommand> {370 if self.local {371 Ok(MyCommand::new(escalation, cmd))372 } else {373 let session = self.open_session().await?;374 Ok(MyCommand::new_on(escalation, cmd, session))375 }376 }377 pub async fn nix_cmd(&self) -> Result<MyCommand> {378 let mut nix = self.cmd("nix").await?;379 nix.args([380 "--extra-experimental-features",381 "nix-command",382 "--extra-experimental-features",383 "flakes",384 ]);385 Ok(nix)386 }387388 pub async fn decrypt(&self, data: SecretData) -> Result<Vec<u8>> {389 ensure!(data.encrypted, "secret is not encrypted");390 let mut cmd = self.cmd("fleet-install-secrets").await?;391 cmd.arg("decrypt").eqarg("--secret", data.to_string());392 let encoded = cmd393 .sudo()394 .run_string()395 .await396 .context("failed to call remote host for decrypt")?;397 let data: SecretData = encoded.parse().map_err(|e| anyhow!("{e}"))?;398 ensure!(!data.encrypted, "secret came out encrypted");399 Ok(data.data)400 }401 pub async fn reencrypt(&self, data: SecretData, targets: Vec<String>) -> Result<SecretData> {402 ensure!(data.encrypted, "secret is not encrypted");403 let mut cmd = self.cmd("fleet-install-secrets").await?;404 cmd.arg("reencrypt").eqarg("--secret", data.to_string());405 for target in targets {406 let key = self.config.key(&target).await?;407 cmd.eqarg("--targets", key);408 }409 let encoded = cmd410 .sudo()411 .run_string()412 .await413 .context("failed to call remote host for decrypt")?;414 let data: SecretData = encoded.parse().map_err(|e| anyhow!("{e}"))?;415 ensure!(data.encrypted, "secret came out not encrypted");416 Ok(data)417 }418 /// Returns path for futureproofing, as path might change i.e on conversion to CA419 pub async fn remote_derivation(&self, path: &PathBuf) -> Result<PathBuf> {420 if self.local {421 // Path is located locally, thus already trusted.422 return Ok(path.to_owned());423 }424 let mut nix = MyCommand::new(425 // Not used426 EscalationStrategy::Su,427 "nix",428 );429 nix.arg("copy").arg("--substitute-on-destination");430431 let proto = if self.legacy_ssh_store.get().cloned().unwrap_or(false) {432 "ssh"433 } else {434 "ssh-ng"435 };436437 match self.deploy_kind().await? {438 DeployKind::Fleet | DeployKind::UpgradeToFleet | DeployKind::NixosLustrate => {439 nix.comparg("--to", format!("{proto}://{}", self.name));440 }441 DeployKind::NixosInstall => {442 nix443 // Signature checking makes no sense with remote-store store argument set, as we're not even interacting with remote nix daemon444 .arg("--no-check-sigs")445 .comparg(446 "--to",447 format!("{proto}://root@{}?remote-store=/mnt", self.name),448 );449 }450 }451 nix.arg(path);452 nix.run_nix().await.context("nix copy")?;453 Ok(path.to_owned())454 }455 pub async fn systemctl_stop(&self, name: &str) -> Result<()> {456 let mut cmd = self.cmd("systemctl").await?;457 cmd.arg("stop").arg(name);458 cmd.sudo().run().await459 }460 pub async fn systemctl_start(&self, name: &str) -> Result<()> {461 let mut cmd = self.cmd("systemctl").await?;462 cmd.arg("start").arg(name);463 cmd.sudo().run().await464 }465466 pub async fn rm_file(&self, path: impl AsRef<OsStr>, sudo: bool) -> Result<()> {467 let mut cmd = self.cmd("rm").await?;468 cmd.arg("-f").arg(path);469 if sudo {470 cmd = cmd.sudo()471 }472 cmd.run().await473 }474}475impl ConfigHost {476 // TOCTOU is possible here in case if config is changed, but this case is not handled anywhere anyway,477 // assuming getting tags always returns the same value.478 pub async fn tags(&self) -> Result<Vec<String>> {479 if let Some(v) = self.groups.get() {480 return Ok(v.clone());481 }482 let Some(host_config) = &self.host_config else {483 return Ok(vec![]);484 };485 let tags: Vec<String> = nix_go_json!(host_config.tags);486487 let _ = self.groups.set(tags.clone());488489 Ok(tags)490 }491 pub async fn nixos_config(&self) -> Result<Value> {492 if let Some(v) = self.nixos_config.get() {493 return Ok(v.clone());494 }495 let Some(host_config) = &self.host_config else {496 bail!("local host has no nixos_config");497 };498 let nixos_config = nix_go!(host_config.nixos.config);499 assert_warn("nixos config evaluation", &nixos_config).await?;500501 let _ = self.nixos_config.set(nixos_config.clone());502503 Ok(nixos_config)504 }505 pub fn nixos_unchecked_config(&self) -> Result<Value> {506 if let Some(v) = self.nixos_unchecked_config.get() {507 return Ok(v.clone());508 }509 let Some(host_config) = &self.host_config else {510 bail!("local host has no nixos_config");511 };512 let nixos_config = nix_go!(host_config.nixos_unchecked.config);513514 let _ = self.nixos_unchecked_config.set(nixos_config.clone());515516 Ok(nixos_config)517 }518519 pub fn list_defined_secrets(&self) -> Result<Vec<String>> {520 let nixos = self.nixos_unchecked_config()?;521 let secrets = nix_go!(nixos.secrets);522 secrets.list_fields()523 }524 pub fn secret_definition(&self, name: &str) -> Result<HostSecretDefinition> {525 let nixos = self.nixos_unchecked_config()?;526 Ok(HostSecretDefinition(527 self.name.clone(),528 nix_go!(nixos.secrets[{ name }]),529 ))530 }531532 /// Packages for this host, resolved with nixpkgs overlays533 pub async fn pkgs(&self) -> Result<Value> {534 if let Some(value) = &self.pkgs_override {535 return Ok(value.clone());536 }537 let Some(host_config) = &self.host_config else {538 bail!("local host has no host_config");539 };540 // TODO: Should nixos.options be cached?541 Ok(nix_go!(host_config.nixos.options._module.args.value.pkgs))542 }543}544545impl Config {546 pub async fn tagged_hostnames(&self, tag: &str) -> Result<Vec<String>> {547 let config = &self.config_field;548 let tagged: Vec<String> = nix_go_json!(config.taggedWith[{ tag }]);549 Ok(tagged)550 }551 pub async fn expand_owner_set(&self, owners: Vec<String>) -> Result<BTreeSet<String>> {552 let mut out = BTreeSet::new();553 for owner in owners {554 if let Some(tag) = owner.strip_prefix('@') {555 let hosts = self.tagged_hostnames(tag).await?;556 out.extend(hosts);557 } else {558 out.insert(owner);559 }560 }561 Ok(out)562 }563 pub fn local_host(&self) -> ConfigHost {564 ConfigHost {565 config: self.clone(),566 name: "<virtual localhost>".to_owned(),567 host_config: None,568 nixos_config: OnceCell::new(),569 nixos_unchecked_config: OnceCell::new(),570 groups: {571 let cell = OnceCell::new();572 let _ = cell.set(vec![]);573 cell574 },575 pkgs_override: Some(self.default_pkgs.clone()),576577 local: true,578 session: OnceLock::new(),579 deploy_kind: OnceCell::new(),580 session_destination: OnceCell::new(),581 legacy_ssh_store: OnceCell::new(),582 }583 }584585 pub async fn host(&self, name: &str) -> Result<ConfigHost> {586 let config = &self.config_field;587 let host_config = nix_go!(config.hosts[{ name }]);588589 Ok(ConfigHost {590 config: self.clone(),591 name: name.to_owned(),592 host_config: Some(host_config),593 nixos_config: OnceCell::new(),594 nixos_unchecked_config: OnceCell::new(),595 groups: OnceCell::new(),596 pkgs_override: None,597598 // TODO: Remove with connectivit refactor599 local: self.localhost == name,600 session: OnceLock::new(),601 deploy_kind: OnceCell::new(),602 session_destination: OnceCell::new(),603 legacy_ssh_store: OnceCell::new(),604 })605 }606 pub async fn list_hosts(&self) -> Result<Vec<ConfigHost>> {607 let config = &self.config_field;608 let names = nix_go!(config.hosts).list_fields()?;609 let mut out = vec![];610 for name in names {611 out.push(self.host(&name).await?);612 }613 Ok(out)614 }615 // TODO: Replace usages with .host().nixos_config616 pub async fn system_config(&self, host: &str) -> Result<Value> {617 let fleet_field = &self.config_field;618 Ok(nix_go!(fleet_field.hosts[{ host }].nixos.config))619 }620621 /// Shared secrets configured in fleet.nix or in flake622 pub async fn list_configured_shared(&self) -> Result<Vec<String>> {623 let config_field = &self.config_field;624 nix_go!(config_field.sharedSecrets).list_fields()625 }626 pub fn has_shared(&self, name: &str) -> bool {627 let data = self.data();628 data.secrets.contains(name)629 }630 pub fn replace_shared(&self, name: String, shared: FleetSecretDistribution) {631 let mut data = self.data_mut();632 data.secrets.set_data(name, shared);633 }634 pub fn remove_shared(&self, secret: &str) {635 let mut data = self.data_mut();636 data.secrets.remove(secret);637 }638639 pub fn list_secrets_for_owner(&self, host: &str) -> Vec<String> {640 let data = self.data_mut();641 data.secrets.keys_for_owner(host).cloned().collect()642 }643 pub fn list_secrets(&self) -> Vec<String> {644 let data = self.data_mut();645 data.secrets.keys().cloned().collect()646 }647648 pub fn has_secret(&self, host: &str, secret: &str) -> bool {649 let data = self.data();650 data.secrets.contains_for_owner(secret, host)651 }652 pub fn insert_secret(&self, host: String, secret: String, value: FleetSecretData) {653 let mut data = self.data_mut();654 data.secrets.set_single_data(secret, host, value);655 }656 pub fn remove_secret(&self, host: &str, secret: &str) {657 let mut data = self.data_mut();658 data.secrets.drop_owner_no_reencrypt(secret, host);659 }660661 pub fn host_secret(&self, host: &str, secret: &str) -> Option<FleetSecretDistribution> {662 let data = self.data();663 data.secrets.get_single(secret, host).cloned()664 }665 pub fn shared_secret(&self, secret: &str) -> Option<FleetSecretDistributions> {666 let data = self.data();667 data.secrets.get(secret).cloned()668 }669 pub fn shared_secret_definition(&self, secret: &str) -> Result<SharedSecretDefinition> {670 let config_field = &self.config_field;671 Ok(SharedSecretDefinition(nix_go!(672 config_field.sharedSecrets[{ secret }]673 )))674 }675676 // TODO: Should this be something modifiable from other processes?677 // E.g terraform provider might want to update FleetData (e.g secrets),678 // and current implementation assumes only one process holds current fleet.nix679 // Given that it is no longer needs to be a file for nix evaluation,680 // maybe it can be a .nix file for persistence, but accessible only681 // thru some shared state controller? Might it be stored in terraform682 // state provider?683 pub fn data(&'_ self) -> MutexGuard<'_, FleetData> {684 self.data.lock().unwrap()685 }686 pub fn data_mut(&'_ self) -> MutexGuard<'_, FleetData> {687 self.data.lock().unwrap()688 }689 pub fn save(&self) -> Result<()> {690 let mut tempfile = NamedTempFile::new_in(self.directory.clone()).context("failed to create updated version of fleet.nix in the same directory as original.\nDo you have write access to it? Access only to the fleet.nix won't be enough, the directory is used for atomic overwrite operation.\nIt is not recommended to use fleet by root anyway, move fleet project to your home directory.")?;691 let data = nixlike::serialize(&self.data() as &FleetData)?;692 tempfile.write_all(693 format!(694 "# This file contains fleet state and shouldn't be edited by hand\n\n{data}\n\n# vim: ts=2 et nowrap\n"695 )696 .as_bytes(),697 )?;698 let mut fleet_data_path = self.directory.clone();699 fleet_data_path.push("fleet.nix");700 tempfile.persist(fleet_data_path)?;701 Ok(())702 }703}crates/fleet-base/src/opts.rsdiffbeforeafterboth--- a/crates/fleet-base/src/opts.rs
+++ b/crates/fleet-base/src/opts.rs
@@ -211,7 +211,7 @@
}
let bytes =
std::fs::read_to_string(&fleet_data_path).context("reading fleet state (fleet.nix)")?;
- let data: Mutex<FleetData> = nixlike::parse_str(&bytes)?;
+ let data = Mutex::new(FleetData::from_str(&bytes)?);
let mut fetch_settings = FetchSettings::new();
fetch_settings.set(c"warn-dirty", c"false");
flake.lockdiffbeforeafterboth--- a/flake.lock
+++ b/flake.lock
@@ -2,10 +2,10 @@
"nodes": {
"crane": {
"locked": {
- "lastModified": 1766181779,
+ "lastModified": 1767461147,
"owner": "ipetkov",
"repo": "crane",
- "rev": "0263f510ba38bee5b7f817498066adaad694e50b",
+ "rev": "7d59256814085fd9666a2ae3e774dc5ee216b630",
"type": "github"
},
"original": {
@@ -37,10 +37,10 @@
]
},
"locked": {
- "lastModified": 1765835352,
+ "lastModified": 1767609335,
"owner": "hercules-ci",
"repo": "flake-parts",
- "rev": "a34fae9c08a15ad73f295041fec82323541400a9",
+ "rev": "250481aafeb741edfe23d29195671c19b36b6dca",
"type": "github"
},
"original": {
@@ -126,10 +126,10 @@
},
"nixpkgs": {
"locked": {
- "lastModified": 1766181714,
+ "lastModified": 1767657734,
"owner": "nixos",
"repo": "nixpkgs",
- "rev": "ff2da5fee8b3248cac330f14eac98228620beab0",
+ "rev": "d4ccebf51ee4dbeb9df364dce1fe9848635c1258",
"type": "github"
},
"original": {
@@ -190,10 +190,10 @@
]
},
"locked": {
- "lastModified": 1766112155,
+ "lastModified": 1767667566,
"owner": "oxalica",
"repo": "rust-overlay",
- "rev": "2a6db3fc1c27ae77f9caa553d7609b223cb770b5",
+ "rev": "056ce5b125ab32ffe78c7d3e394d9da44733c95e",
"type": "github"
},
"original": {
@@ -223,10 +223,10 @@
]
},
"locked": {
- "lastModified": 1766000401,
+ "lastModified": 1767468822,
"owner": "numtide",
"repo": "treefmt-nix",
- "rev": "42d96e75aa56a3f70cab7e7dc4a32868db28e8fd",
+ "rev": "d56486eb9493ad9c4777c65932618e9c2d0468fc",
"type": "github"
},
"original": {
flake.nixdiffbeforeafterboth--- a/flake.nix
+++ b/flake.nix
@@ -128,11 +128,6 @@
overlays = [
(inputs.rust-overlay.overlays.default)
(final: prev: {
- boehmgc = prev.boehmgc.overrideAttrs (prevAttrs: {
- configureFlags = prevAttrs.configureFlags ++ [
- "--enable-gc-assertions"
- ];
- });
# Libsecret is stupidly huge
# https://github.com/oxalica/rust-overlay/issues/211
libsecret = final.stdenv.mkDerivation {
modules/secrets-data.nixdiffbeforeafterboth--- a/modules/secrets-data.nix
+++ b/modules/secrets-data.nix
@@ -1,7 +1,6 @@
{
lib,
fleetLib,
- config,
...
}:
let
@@ -15,15 +14,7 @@
submodule
bool
unspecified
- ;
- inherit (lib.attrsets)
- mapAttrsToList
- mapAttrs
- filterAttrs
- genAttrs
;
- inherit (lib.lists) sort unique concatLists;
- inherit (lib.strings) toJSON;
secretDataValue = {
options = {
@@ -71,35 +62,8 @@
default = null;
};
};
- config = { };
};
- hostSecretData = {
- freeformType = attrsOf (submodule secretDataValue);
- options = {
- createdAt = mkOption {
- type = str;
- description = "Timestamp of secret generation/last rotation.";
- default = null;
- };
- expiresAt = mkOption {
- type = nullOr str;
- description = "Expiration timestamp triggering mandatory secret rotation.";
- default = null;
- };
- shared = mkOption {
- type = bool;
- description = "Indicates if secret is a shared secret, so other hosts might have the same piece of secret data.";
- default = false;
- };
- generationData = mkOption {
- type = unspecified;
- description = "Contextual metadata associated with secret part.";
- default = null;
- };
- };
- config = { };
- };
managerKey = {
options = {
name = mkOption {
@@ -121,49 +85,11 @@
managerKeys = mkOption {
type = listOf (submodule managerKey);
};
- sharedSecrets = mkOption {
- type = attrsOf (submodule sharedSecretData);
+ secrets = mkOption {
+ type = attrsOf (listOf submodule sharedSecretData);
default = { };
description = "Shared secret data.";
- };
- hostSecrets = mkOption {
- type = attrsOf (attrsOf (submodule hostSecretData));
- default = { };
- description = "Host-specific secrets.";
- internal = true;
};
};
- config.hostSecrets =
- let
- hostsWithSharedSecrets = unique (
- concatLists (mapAttrsToList (_: s: s.owners) config.sharedSecrets)
- );
- secretsHavingHost = host: filterAttrs (_: secret: lib.elem host secret.owners) config.sharedSecrets;
- toHostSecret = _: secret: (removeAttrs secret [ "owners" ]) // { shared = true; };
- in
- genAttrs hostsWithSharedSecrets (host: mapAttrs toHostSecret (secretsHavingHost host));
});
- config = {
- assertions =
- (mapAttrsToList (name: secret: {
- assertion =
- secret.expectedOwners == null
- ||
- sort (a: b: a < b) (config.data.sharedSecrets.${name} or { owners = [ ]; }).owners
- == sort (a: b: a < b) secret.expectedOwners;
- message = "Shared secret ${name} is expected to be encrypted for ${toJSON secret.expectedOwners}, but it is encrypted for ${
- toJSON (config.data.sharedSecrets.${name} or { owners = [ ]; }).owners
- }. Run fleet secrets regenerate to fix";
- }) config.sharedSecrets)
-
- ++ (mapAttrsToList (name: secret: {
- # TODO: Same assertion should be in host secrets
- assertion =
- (config.data.sharedSecrets.${name} or { generationData = null; }).generationData
- == secret.expectedGenerationData;
- message = "Shared secret ${name} has unexpected generation data ${toJSON secret.expectedGenerationData} != ${
- toJSON (config.data.sharedSecrets.${name} or { generationData = null; }).generationData
- }. Run fleet secrets regenerate to fix";
- }) config.sharedSecrets);
- };
}
modules/secrets.nixdiffbeforeafterboth--- a/modules/secrets.nix
+++ b/modules/secrets.nix
@@ -1,6 +1,5 @@
{
lib,
- config,
...
}:
let
@@ -18,7 +17,6 @@
uniq
;
inherit (lib.strings) concatStringsSep;
- inherit (lib.attrsets) mapAttrs;
sharedSecret =
{ config, ... }:
@@ -54,6 +52,12 @@
Set to false if host permissions are revoked through alternative mechanisms like firewall rules.
'';
};
+ allowDifferent = mkOption {
+ type = bool;
+ description = ''
+ When adding owner, do not update secret value for other owners, instead creating a new distribution
+ '';
+ };
generator = mkOption {
type = uniq (nullOr (functionTo package));
description = ''
@@ -84,32 +88,13 @@
in
{
options = {
- sharedSecrets = mkOption {
+ secrets = mkOption {
type = attrsOf (submodule sharedSecret);
default = { };
description = "Collection of secrets shared across multiple hosts with configurable ownership";
};
};
config = {
- hosts = mapAttrs (
- _: secretMap:
- let
- partsOf =
- s:
- removeAttrs s [
- "createdAt"
- "expiresAt"
- "generationData"
- ];
-
- in
- {
- nixos.data.secrets = mapAttrs (_: s: partsOf s) secretMap;
- # nixos.secrets = mapAttrs (
- # _: s: mapAttrs (_: _: {}) (partsOf s)
- # ) secretMap;
- }
- ) config.data.hostSecrets;
nixpkgs.overlays = [
(final: prev: {
mkSecretGenerators =