difftreelog
refactor drop unnecessary async/await
in: trunk
15 files changed
cmds/fleet/src/cmds/build_systems.rsdiffbeforeafterboth--- a/cmds/fleet/src/cmds/build_systems.rs
+++ b/cmds/fleet/src/cmds/build_systems.rs
@@ -30,9 +30,9 @@
async fn build_task(config: Config, hostname: String, build_attr: &str) -> Result<PathBuf> {
info!("building");
- let host = config.host(&hostname).await?;
+ let host = config.host(&hostname)?;
// let action = Action::from(self.subcommand.clone());
- let nixos = host.nixos_config().await?;
+ let nixos = host.nixos_config()?;
let drv = nix_go!(nixos.system.build[{ build_attr }]);
let out_output = spawn_blocking(move || drv.build("out"))
.await
@@ -59,7 +59,7 @@
impl BuildSystems {
pub async fn run(self, config: &Config, opts: &FleetOpts) -> Result<()> {
- let hosts = opts.filter_skipped(config.list_hosts().await?).await?;
+ let hosts = opts.filter_skipped(config.list_hosts()?)?;
let set = LocalSet::new();
let build_attr = self.build_attr.clone();
for host in hosts {
@@ -95,20 +95,20 @@
impl Deploy {
pub async fn run(self, config: &Config, opts: &FleetOpts) -> Result<()> {
- let hosts = opts.filter_skipped(config.list_hosts().await?).await?;
+ let hosts = opts.filter_skipped(config.list_hosts()?)?;
let set = LocalSet::new();
for host in hosts.into_iter() {
let config = config.clone();
let span = info_span!("deploy", host = field::display(&host.name));
let hostname = host.name.clone();
let opts = opts.clone();
- if let Some(deploy_kind) = opts.action_attr::<DeployKind>(&host, "deploy_kind").await? {
+ if let Some(deploy_kind) = opts.action_attr::<DeployKind>(&host, "deploy_kind")? {
host.set_deploy_kind(deploy_kind);
};
- if let Some(destination) = opts.action_attr::<String>(&host, "dest").await? {
+ if let Some(destination) = opts.action_attr::<String>(&host, "dest")? {
host.set_session_destination(destination);
};
- if let Some(legacy) = opts.action_attr::<bool>(&host, "legacy_ssh_store").await? {
+ if let Some(legacy) = opts.action_attr::<bool>(&host, "legacy_ssh_store")? {
host.set_legacy_ssh_store(legacy);
};
@@ -153,7 +153,7 @@
self.action,
&host,
remote_path,
- match opts.action_attr(&host, "specialisation").await {
+ match opts.action_attr(&host, "specialisation") {
Ok(v) => v,
_ => {
error!("unreachable? failed to get specialization");
cmds/fleet/src/cmds/info.rsdiffbeforeafterboth--- a/cmds/fleet/src/cmds/info.rs
+++ b/cmds/fleet/src/cmds/info.rs
@@ -35,7 +35,7 @@
let mut data = Vec::new();
match self.cmd {
InfoCmd::ListHosts { ref tagged } => {
- 'host: for host in config.list_hosts().await? {
+ 'host: for host in config.list_hosts()? {
if !tagged.is_empty() {
let config = &config.config_field;
let host_name = &host.name;
@@ -59,7 +59,7 @@
"at leas one of --external or --internal must be set"
);
let mut out = <BTreeSet<String>>::new();
- let host = config.system_config(&host).await?;
+ let host = config.system_config(&host)?;
if external {
let data: Vec<String> = nix_go_json!(host.network.externalIps);
out.extend(data);
cmds/fleet/src/cmds/rollback.rsdiffbeforeafterboth--- a/cmds/fleet/src/cmds/rollback.rs
+++ b/cmds/fleet/src/cmds/rollback.rs
@@ -75,7 +75,7 @@
impl RollbackSingle {
pub(crate) async fn run(&self, config: &Config, _opts: &FleetOpts) -> Result<()> {
- let host = config.host(&self.machine).await?;
+ let host = config.host(&self.machine)?;
match &self.action {
RollbackAction::ListTargets => {
let generations = list_all_generations(&host, config).await;
cmds/fleet/src/cmds/secrets/mod.rsdiffbeforeafterboth1use std::{2 collections::{BTreeMap, BTreeSet, HashSet},3 io::{self, Read, Write, stdin, stdout},4 path::PathBuf,5};67use anyhow::{Context, Result, anyhow, bail, ensure};8use chrono::{DateTime, Utc};9use clap::Parser;10use fleet_base::{11 fleetdata::{FleetSecretData, FleetSecretDistribution, FleetSecretPart, encrypt_secret_data},12 host::Config,13 opts::FleetOpts,14 secret::{Expectations, RegenerationReason, secret_needs_regeneration},15};16use fleet_shared::SecretData;17use nix_eval::{NixType, Value, nix_go, nix_go_json};18use serde::Deserialize;19use tabled::{Table, Tabled};20use tokio::{fs::read, task::spawn_blocking};21use tracing::{Instrument, error, info, info_span, warn};2223#[derive(Parser)]24pub enum Secret {25 AddManager,26 /// Force load host keys for all defined hosts27 ForceKeys,28 /// Read secret from remote host, requires sudo on one of the owning hosts29 Read {30 /// Secret name to read31 name: String,3233 /// Distribution with what machine to read34 /// If not shared between multiple - defaults to single owner35 #[clap(short = 'm', long)]36 machine: Option<String>,3738 /// Which private secret part to read39 #[clap(short = 'p', long, default_value = "secret")]40 part: String,4142 /// Which host should we use to decrypt, in case if reencryption is required, without43 /// regeneration44 #[clap(long)]45 prefer_identities: Vec<String>,46 },47 Regenerate {48 /// Which host should we use to decrypt, in case if reencryption is required, without49 /// regeneration50 #[clap(long)]51 prefer_identities: Vec<String>,52 /// Only regenerate shared secrets53 #[clap(long)]54 skip_hosts: bool,55 },56 List {},57 Edit {58 name: String,59 #[clap(short = 'm', long)]60 machine: String,6162 #[clap(long)]63 add: bool,6465 /// Which private secret part to read66 #[clap(short = 'p', long, default_value = "secret")]67 part: String,68 },69}7071/*72#[allow(clippy::too_many_arguments)]73#[tracing::instrument(skip(config, secret, definition, prefer_identities))]74async fn maybe_regenerate_shared_secret(75 secret_name: &str,76 config: &Config,77 mut secret: FleetSecretDistribution,78 definition: SharedSecretDefinition,79 prefer_identities: &[String],80 expectations: &Expectations,81) -> Result<FleetSecretDistribution> {82 let reason = secret_needs_regeneration(&secret.secret, &secret.owners, expectations);83 let value = definition.definition_value();8485 let (should_reencrypt, reason) = match reason {86 Some(RegenerationReason::OwnersAdded(_)) => {87 // Secret always needs to be reencrypted for new owners to be able to read it88 (89 true,90 if nix_go_json!(value.regenerateOnOwnerAdded) {91 reason92 } else {93 None94 },95 )96 }97 Some(RegenerationReason::OwnersRemoved(_)) => {98 // No need to reencrypt, we can just leave stanzas in place.99 if nix_go_json!(value.regenerateOnOwnerRemoved) {100 (true, reason)101 } else {102 (false, None)103 }104 }105 Some(_) => (true, reason),106 None => (false, None),107 };108109 if let Some(reason) = reason {110 info!("secret needs to be regenerated: {reason}");111 let generated = generate_shared(config, secret_name, definition, expectations).await?;112 Ok(generated)113 } else if should_reencrypt {114 info!("secret needs to be reencrypted");115 let identity_holder = if !prefer_identities.is_empty() {116 prefer_identities117 .iter()118 .find(|i| secret.owners.iter().any(|s| s == *i))119 } else {120 secret.owners.first()121 };122 let Some(identity_holder) = identity_holder else {123 bail!("no available holder found");124 };125126 for (part_name, part) in secret.secret.parts.iter_mut() {127 let _span = info_span!("part reencryption", part_name);128 if !part.raw.encrypted {129 continue;130 }131 let host = config.host(identity_holder).await?;132 let encrypted = host133 .reencrypt(134 part.raw.clone(),135 expectations.owners.iter().cloned().collect(),136 )137 .await?;138 part.raw = encrypted;139 }140 secret.owners = expectations.owners.clone();141 Ok(secret)142 } else {143 Ok(secret)144 }145}146*/147148#[derive(Deserialize)]149#[serde(rename_all = "camelCase")]150enum GeneratorKind {151 Impure,152 Pure,153}154155async fn generate_pure(156 _config: &Config,157 _display_name: &str,158 _secret: Value,159 _default_generator: Value,160 _expectations: &Expectations,161) -> Result<FleetSecretData> {162 bail!("pure generators are broken for now")163}164async fn generate_impure(165 config: &Config,166 _display_name: &str,167 secret: Value,168 default_generator: Value,169 expectations: &Expectations,170) -> Result<FleetSecretData> {171 let generator = nix_go!(secret.generator);172 let on: Option<String> = nix_go_json!(default_generator.impureOn);173174 let nixpkgs = &config.nixpkgs;175176 let host = if let Some(on) = &on {177 config.host(on).await?178 } else {179 config.local_host()180 };181 let on_pkgs = host.pkgs().await?;182 let mk_secret_generators = nix_go!(on_pkgs.mkSecretGenerators);183184 let mut recipients = Vec::new();185 for owner in &expectations.owners {186 let key = config.key(owner).await?;187 recipients.push(key);188 }189 let generators = nix_go!(mk_secret_generators(Obj { recipients }));190 let pkgs_and_generators = on_pkgs.attrs_update(generators)?;191192 let call_package = nix_go!(nixpkgs.lib.callPackageWith(pkgs_and_generators));193194 let generator = nix_go!(call_package(generator)(Obj {}));195196 let generator = spawn_blocking(move || generator.build("out"))197 .await198 .expect("nix build shouldn't fail")?;199 let generator = host.remote_derivation(&generator).await?;200201 let out_parent = host.mktemp_dir().await?;202 let out = format!("{out_parent}/out");203204 let mut r#gen = host.cmd(generator).await?;205 r#gen.env("out", &out);206 if on.is_none() {207 // This path is local, thus we can feed `OsString` directly to env var... But I don't think that's necessary to handle.208 let project_path: String = config209 .directory210 .clone()211 .into_os_string()212 .into_string()213 .map_err(|s| anyhow!("fleet project path is not utf-8: {s:?}"))?;214 r#gen.env("FLEET_PROJECT", project_path);215 }216 r#gen.run().await.context("impure generator")?;217218 {219 let marker = host.read_file_text(format!("{out}/marker")).await?;220 ensure!(marker == "SUCCESS", "generation not succeeded");221 }222223 let mut parts = BTreeMap::new();224 for part in host.read_dir(&out).await? {225 if part == "created_at" || part == "expires_at" || part == "marker" {226 continue;227 }228 let contents: SecretData = host229 .read_file_text(format!("{out}/{part}"))230 .await?231 .parse()232 .map_err(|e| anyhow!("failed to decode secret {out:?} part {part:?}: {e}"))?;233 parts.insert(part.to_owned(), FleetSecretPart { raw: contents });234 }235236 let created_at = host.read_file_value(format!("{out}/created_at")).await?;237 let expires_at = host.read_file_value(format!("{out}/expires_at")).await.ok();238239 let new_data = FleetSecretData {240 created_at,241 expires_at,242 parts,243 generation_data: expectations.generation_data.clone(),244 };245246 if let Some(reason) = secret_needs_regeneration(&new_data, &expectations.owners, expectations) {247 bail!("newly generated secret needs to be regenerated: {reason}")248 }249250 Ok(new_data)251}252253async fn generate(254 config: &Config,255 display_name: &str,256 secret: Value,257 expectations: &Expectations,258) -> Result<FleetSecretData> {259 let generator = nix_go!(secret.generator);260 // Can't properly check on nix module system level261 {262 let gen_ty = generator.type_of();263 if matches!(gen_ty, NixType::Null) {264 bail!("secret has no generator defined, can't automatically generate it.");265 }266 if matches!(gen_ty, NixType::Attrs) {267 if !generator.has_field("__functor")? {268 bail!("generator should be functor, got {gen_ty:?}");269 }270 } else if matches!(gen_ty, NixType::Function) {271 bail!("generator should be functor, got {gen_ty:?}");272 }273 }274 let nixpkgs = &config.nixpkgs;275 let default_pkgs = &config.default_pkgs;276 let default_mk_secret_generators = nix_go!(default_pkgs.mkSecretGenerators);277 // Generators provide additional information in passthru, to access278 // passthru we should call generator, but information about where this generator is supposed to build279 // is located in passthru... Thus evaluating generator on host.280 //281 // Maybe it is also possible to do some magic with __functor?282 //283 // I don't want to make modules always responsible for additional secret data anyway,284 // so it should be in derivation, and not in the secret data itself.285 let generators = nix_go!(default_mk_secret_generators(Obj {286 recipients: <Vec<String>>::new(),287 }));288 let pkgs_and_generators = default_pkgs.clone().attrs_update(generators)?;289290 let call_package = nix_go!(nixpkgs.lib.callPackageWith(pkgs_and_generators));291 let default_generator = nix_go!(call_package(generator)(Obj {}));292293 let kind: GeneratorKind = nix_go_json!(default_generator.generatorKind);294295 match kind {296 GeneratorKind::Impure => {297 generate_impure(298 config,299 display_name,300 secret,301 default_generator,302 expectations,303 )304 .await305 }306 GeneratorKind::Pure => {307 generate_pure(308 config,309 display_name,310 secret,311 default_generator,312 expectations,313 )314 .await315 }316 }317}318/*319async fn generate_shared(320 config: &Config,321 display_name: &str,322 secret: SharedSecretDefinition,323 expectations: &Expectations,324) -> Result<FleetSecretDistribution> {325 // let owners: Vec<String> = nix_go_json!(secret.expectedOwners);326 Ok(FleetSecretDistribution {327 managed: Some(true),328 secret: generate(329 config,330 display_name,331 secret.definition_value(),332 expectations,333 )334 .await?,335 owners: expectations.owners.clone(),336 })337}*/338339async fn parse_public(340 public: Option<String>,341 public_file: Option<PathBuf>,342) -> Result<Option<SecretData>> {343 Ok(match (public, public_file) {344 (Some(v), None) => Some(SecretData {345 data: v.into(),346 encrypted: false,347 }),348 (None, Some(v)) => Some(SecretData {349 data: read(v).await?,350 encrypted: false,351 }),352 (Some(_), Some(_)) => {353 bail!("only public or public_file should be set")354 }355 (None, None) => None,356 })357}358359async fn parse_secret() -> Result<Option<Vec<u8>>> {360 let mut input = vec![];361 stdin().read_to_end(&mut input)?;362 if input.is_empty() {363 Ok(None)364 } else {365 Ok(Some(input))366 }367}368369fn parse_machines(370 initial: BTreeSet<String>,371 machines: Option<Vec<String>>,372 mut add_machines: Vec<String>,373 mut remove_machines: Vec<String>,374) -> Result<BTreeSet<String>> {375 if machines.is_none() && add_machines.is_empty() && remove_machines.is_empty() {376 bail!("no operation");377 }378379 let initial_machines = initial.clone();380 let mut target_machines = initial;381 info!("Currently encrypted for {initial_machines:?}");382383 if let Some(machines) = machines {384 ensure!(385 add_machines.is_empty() && remove_machines.is_empty(),386 "can't combine --machines and --add-machines/--remove-machines"387 );388 let target = initial_machines.iter().collect::<HashSet<_>>();389 let source = machines.iter().collect::<HashSet<_>>();390 for removed in target.difference(&source) {391 remove_machines.push((*removed).clone());392 }393 for added in source.difference(&target) {394 add_machines.push((*added).clone());395 }396 }397398 for machine in &remove_machines {399 if !target_machines.remove(machine) {400 warn!("secret is not enabled for {machine}");401 }402 }403 for machine in &add_machines {404 if !target_machines.insert(machine.to_owned()) {405 warn!("secret is already added to {machine}");406 }407 }408 if !remove_machines.is_empty() {409 // TODO: maybe force secret regeneration?410 // Not that useful without revokation.411 warn!(412 "secret will not be regenerated for removed machines, and until host rebuild, they will still possess the ability to decode secret"413 );414 }415 Ok(target_machines)416}417impl Secret {418 pub async fn run(self, config: &Config, opts: &FleetOpts) -> Result<()> {419 match self {420 Secret::AddManager => {421 todo!("part of fleet-pusher")422 }423 Secret::ForceKeys => {424 for host in config.list_hosts().await? {425 if opts.should_skip(&host).await? {426 continue;427 }428 config.key(&host.name).await?;429 }430 }431 Secret::Read {432 name,433 machine,434 part: part_name,435 mut prefer_identities,436 } => {437 let Some(secret) = config.shared_secret(&name) else {438 bail!("secret doesn't exists");439 };440441 let dist = if secret.len() == 1 {442 &secret[0]443 } else if let Some(machine) = machine {444 let dist = secret.get(&machine);445 let Some(dist) = dist else {446 bail!("machine {machine} has no distribution of secret {name}");447 };448 prefer_identities.push(machine);449 dist450 } else {451 bail!(452 "secret {name} has shares, but no --machine specified for specifing which do you need"453 )454 };455456 let Some(part) = dist.secret.parts.get(&part_name) else {457 bail!("no part {part_name} in secret {name}");458 };459 let data = if part.raw.encrypted {460 let identity_holder = if !prefer_identities.is_empty() {461 prefer_identities462 .iter()463 .find(|i| dist.owners.iter().any(|s| s == *i))464 } else {465 dist.owners.first()466 };467 let Some(identity_holder) = identity_holder else {468 bail!("no available holder found");469 };470 let host = config.host(identity_holder).await?;471 host.decrypt(part.raw.clone()).await?472 } else {473 part.raw.data.clone()474 };475 stdout().write_all(&data)?;476 }477 Secret::Regenerate {478 prefer_identities,479 skip_hosts,480 } => {481 /*482 info!("checking for secrets to regenerate");483 let expected_shared_set = config484 .list_configured_shared()485 .await?486 .into_iter()487 .collect::<HashSet<_>>();488 let stored_shared_set = config.list_secrets().into_iter().collect::<HashSet<_>>();489 {490 // Generate missing shared491 let _span = info_span!("shared").entered();492 for missing in expected_shared_set.difference(&stored_shared_set) {493 let definition = config.shared_secret_definition(missing)?;494 if !definition.is_managed()? {495 info!("skipping unmanaged secret: {missing}");496 continue;497 }498 let expectations = definition499 .expectations()500 .with_context(|| format!("expectations for shared {missing:?}"))?;501 info!("generating secret: {missing}");502 let shared = generate_shared(config, missing, definition, &expectations)503 .in_current_span()504 .await?;505 config.replace_shared(missing.to_string(), shared)506 }507 }508 if !skip_hosts {509 for host in config.list_hosts().await? {510 if opts.should_skip(&host).await? {511 continue;512 }513514 let _span = info_span!("host", host = host.name).entered();515 let expected_set = host516 .list_defined_secrets()?517 .into_iter()518 .collect::<HashSet<_>>();519 let stored_set = config520 .list_secrets_for_owner(&host.name)521 .into_iter()522 .collect::<HashSet<_>>();523 for missing_secret in expected_set.difference(&stored_set) {524 let secret = host.secret_definition(missing_secret)?;525 if secret.is_shared()? {526 continue;527 }528 info!("generating missing secret: {missing_secret}");529 let expectations = secret.expectations().with_context(|| {530 format!("expectations for {missing_secret:?} of {:?}", host.name)531 })?;532 let generated = match generate(533 config,534 missing_secret,535 secret.definition_value()?,536 &expectations,537 )538 .in_current_span()539 .await540 {541 Ok(v) => v,542 Err(e) => {543 error!("{e:?}");544 continue;545 }546 };547 config.insert_secret(host.name, missing_secret.to_string(), generated)548 }549 for known_secret in stored_set.intersection(&expected_set) {550 let secret = host.secret_definition(known_secret)?;551 if secret.is_shared()? {552 continue;553 }554 info!("updating secret: {known_secret}");555 let data = config.host_secret(&host.name, known_secret)?;556 let expectations = secret.expectations()?;557 if let Some(regen_reason) = data.needs_regeneration(&expectations) {558 info!("needs regeneration: {regen_reason}");559 let generated = match generate(560 config,561 known_secret,562 secret.definition_value()?,563 &expectations,564 )565 .in_current_span()566 .await567 {568 Ok(v) => v,569 Err(e) => {570 error!("{e:?}");571 continue;572 }573 };574 config.insert_secret(575 &host.name,576 known_secret.to_string(),577 FleetLegacyHostSecret {578 managed: Some(true),579 secret: generated,580 },581 )582 }583 }584 for removed_secret in stored_set.difference(&expected_set) {585 let definition = host.secret_definition(removed_secret)?;586 if definition.is_shared()? {587 continue;588 }589 info!("removing secret: {removed_secret}");590 config.remove_secret(&host.name, removed_secret);591 }592 }593 }594 for known_secret in stored_shared_set.intersection(&expected_shared_set) {595 info!("updating shared secret: {known_secret}");596 let data = config.shared_secret(known_secret)?.expect("exists");597598 let definition = config.shared_secret_definition(known_secret)?;599 let expectations = definition.expectations()?;600 config.replace_shared(601 known_secret.to_owned(),602 maybe_regenerate_shared_secret(603 known_secret,604 config,605 data,606 definition,607 &prefer_identities,608 &expectations,609 )610 .await?,611 );612 }613 for removed_secret in stored_shared_set.difference(&expected_shared_set) {614 info!("removing shared secret: {removed_secret}");615 config.remove_shared(removed_secret);616 }617 */618 todo!()619 }620 Secret::List {} => {621 let _span = info_span!("loading secrets").entered();622 let configured = config.list_configured_shared().await?;623 #[derive(Tabled)]624 struct SecretDisplay {625 #[tabled(rename = "Name")]626 name: String,627 #[tabled(rename = "Owners")]628 owners: String,629 }630 // let mut table = vec![];631 for name in configured.iter().cloned() {632 let config = config.clone();633 let data = config.shared_secret(&name).expect("exists");634 /*635 let definition = config.shared_secret_definition(&name)?;636 let expectations = definition.expectations()?;637 let owners = data638 .owners()639 .map(|o| {640 if expectations.owners.contains(o) {641 o.green().to_string()642 } else {643 o.red().to_string()644 }645 })646 .collect::<Vec<_>>();647 table.push(SecretDisplay {648 owners: owners.join(", "),649 name,650 })651 */652 }653 // info!("loaded\n{}", Table::new(table).to_string())654 }655 Secret::Edit {656 name,657 machine,658 part,659 add,660 } => {661 let secret = config662 .host_secret(&machine, &name)663 .context("secret not found")?;664 if let Some(data) = secret.secret.parts.get(&part) {665 let host = config.host(&machine).await?;666 let secret = host.decrypt(data.raw.clone()).await?;667 String::from_utf8(secret).context("secret is not utf8")?668 } else if add {669 String::new()670 } else {671 bail!("part {part} not found in secret {name}. Did you mean to `--add` it?");672 };673 }674 }675 Ok(())676 }677}678679/*680async fn edit_temp_file(681 builder: tempfile::Builder<'_, '_>,682 r: Vec<u8>,683 header: &str,684 comment: &str,685) -> Result<(Vec<u8>, Option<String>), anyhow::Error> {686 if !stdin().is_tty() {687 // TODO: Also try to open /dev/tty directly?688 bail!("stdin is not tty, can't open editor");689 }690691 use std::fmt::Write;692 let mut file = builder.tempfile()?;693694 let mut full_header = String::new();695 let mut had = false;696 for line in header.trim_end().lines() {697 had = true;698 writeln!(&mut full_header, "{comment}{line}")?;699 }700 if had {701 writeln!(&mut full_header, "{}", comment.trim_end())?;702 }703 writeln!(704 &mut full_header,705 "{comment}Do not touch this header! It will be removed automatically"706 )?;707708 file.write_all(full_header.as_bytes())?;709 file.write_all(&r)?;710711 let abs_path = file.into_temp_path();712 let editor = std::env::var_os("VISUAL")713 .or_else(|| std::env::var_os("EDITOR"))714 .unwrap_or_else(|| "vi".into());715 let editor_args = shlex::bytes::split(editor.as_encoded_bytes())716 .ok_or_else(|| anyhow!("EDITOR env var has wrong syntax"))?;717 let editor_args = editor_args718 .into_iter()719 .map(|v| {720 // Only ASCII subsequences are replaced721 unsafe { OsString::from_encoded_bytes_unchecked(v) }722 })723 .collect_vec();724 let Some((editor, args)) = editor_args.split_first() else {725 bail!("EDITOR env var has no command");726 };727 let mut command = Command::new(editor);728 command.args(args);729730 let path_arg = abs_path.canonicalize()?;731732 // TODO: Save full state, using tcget/_getmode/_setmode733 let was_raw = terminal::is_raw_mode_enabled()?;734 terminal::enable_raw_mode()?;735736 let status = command.arg(path_arg).status().await;737738 if !was_raw {739 terminal::disable_raw_mode()?;740 }741742 let success = match status {743 Ok(s) => s.success(),744 Err(e) if e.kind() == io::ErrorKind::NotFound => {745 bail!("editor not found")746 }747 Err(e) => bail!("editor spawn error: {e}"),748 };749750 let mut file = std::fs::read(&abs_path).context("read editor output")?;751 let Some(v) = file.strip_prefix(full_header.as_bytes()) else {752 todo!();753 };754 todo!();755756 // Ok((success, abs_path))757}758*/cmds/fleet/src/main.rsdiffbeforeafterboth--- a/cmds/fleet/src/main.rs
+++ b/cmds/fleet/src/main.rs
@@ -216,13 +216,10 @@
.map(|a| extra_args::parse_os(&a))
.transpose()?
.unwrap_or_default();
- let config = opts
- .fleet_opts
- .build(
- nix_args,
- matches!(opts.command, Opts::Deploy(_) | Opts::BuildSystems(_)),
- )
- .await?;
+ let config = opts.fleet_opts.build(
+ nix_args,
+ matches!(opts.command, Opts::Deploy(_) | Opts::BuildSystems(_)),
+ )?;
match run_command(&config, opts.fleet_opts, opts.command).await {
Ok(()) => {
crates/fleet-base/src/fleetdata.rsdiffbeforeafterboth--- a/crates/fleet-base/src/fleetdata.rs
+++ b/crates/fleet-base/src/fleetdata.rs
@@ -421,3 +421,14 @@
}
}
}
+
+#[derive(Debug)]
+pub struct Expectations {
+ pub owners: BTreeSet<String>,
+ pub generation_data: serde_json::Value,
+ pub parts: BTreeMap<String, GeneratorPart>,
+}
+#[derive(Deserialize, Debug, Clone)]
+pub struct GeneratorPart {
+ pub encrypted: bool,
+}
crates/fleet-base/src/host.rsdiffbeforeafterboth--- a/crates/fleet-base/src/host.rs
+++ b/crates/fleet-base/src/host.rs
@@ -471,10 +471,13 @@
cmd.run().await
}
}
+
+struct HostSecretDefinition(Value);
+
impl ConfigHost {
// TOCTOU is possible here in case if config is changed, but this case is not handled anywhere anyway,
// assuming getting tags always returns the same value.
- pub async fn tags(&self) -> Result<Vec<String>> {
+ pub fn tags(&self) -> Result<Vec<String>> {
if let Some(v) = self.groups.get() {
return Ok(v.clone());
}
@@ -487,7 +490,7 @@
Ok(tags)
}
- pub async fn nixos_config(&self) -> Result<Value> {
+ pub fn nixos_config(&self) -> Result<Value> {
if let Some(v) = self.nixos_config.get() {
return Ok(v.clone());
}
@@ -495,7 +498,7 @@
bail!("local host has no nixos_config");
};
let nixos_config = nix_go!(host_config.nixos.config);
- assert_warn("nixos config evaluation", &nixos_config).await?;
+ assert_warn("nixos config evaluation", &nixos_config)?;
let _ = self.nixos_config.set(nixos_config.clone());
@@ -522,7 +525,7 @@
}
/// Packages for this host, resolved with nixpkgs overlays
- pub async fn pkgs(&self) -> Result<Value> {
+ pub fn pkgs(&self) -> Result<Value> {
if let Some(value) = &self.pkgs_override {
return Ok(value.clone());
}
@@ -534,17 +537,29 @@
}
}
+pub struct SharedSecretDefinition(Value);
+impl SharedSecretDefinition {
+ pub fn expected_owners(&self) -> Result<BTreeSet<String>> {
+ let secret = &self.0;
+ Ok(nix_go_json!(secret.expectedOwners))
+ }
+ pub fn generator(&self) -> Result<Value> {
+ let secret = &self.0;
+ Ok(nix_go!(secret.generator))
+ }
+}
+
impl Config {
- pub async fn tagged_hostnames(&self, tag: &str) -> Result<Vec<String>> {
+ pub fn tagged_hostnames(&self, tag: &str) -> Result<Vec<String>> {
let config = &self.config_field;
let tagged: Vec<String> = nix_go_json!(config.taggedWith[{ tag }]);
Ok(tagged)
}
- pub async fn expand_owner_set(&self, owners: Vec<String>) -> Result<BTreeSet<String>> {
+ pub fn expand_owner_set(&self, owners: Vec<String>) -> Result<BTreeSet<String>> {
let mut out = BTreeSet::new();
for owner in owners {
if let Some(tag) = owner.strip_prefix('@') {
- let hosts = self.tagged_hostnames(tag).await?;
+ let hosts = self.tagged_hostnames(tag)?;
out.extend(hosts);
} else {
out.insert(owner);
@@ -574,7 +589,7 @@
}
}
- pub async fn host(&self, name: &str) -> Result<ConfigHost> {
+ pub fn host(&self, name: &str) -> Result<ConfigHost> {
let config = &self.config_field;
let host_config = nix_go!(config.hosts[{ name }]);
@@ -595,23 +610,23 @@
legacy_ssh_store: OnceCell::new(),
})
}
- pub async fn list_hosts(&self) -> Result<Vec<ConfigHost>> {
+ pub fn list_hosts(&self) -> Result<Vec<ConfigHost>> {
let config = &self.config_field;
let names = nix_go!(config.hosts).list_fields()?;
let mut out = vec![];
for name in names {
- out.push(self.host(&name).await?);
+ out.push(self.host(&name)?);
}
Ok(out)
}
// TODO: Replace usages with .host().nixos_config
- pub async fn system_config(&self, host: &str) -> Result<Value> {
+ pub fn system_config(&self, host: &str) -> Result<Value> {
let fleet_field = &self.config_field;
Ok(nix_go!(fleet_field.hosts[{ host }].nixos.config))
}
/// Shared secrets configured in fleet.nix or in flake
- pub async fn list_configured_shared(&self) -> Result<Vec<String>> {
+ pub fn list_configured_shared(&self) -> Result<Vec<String>> {
let config_field = &self.config_field;
nix_go!(config_field.sharedSecrets).list_fields()
}
@@ -659,6 +674,17 @@
data.secrets.get(secret).cloned()
}
+ pub fn secret_definition(&self, secret: &str) -> Result<Option<SharedSecretDefinition>> {
+ let config = &self.config_field;
+ let shared_secrets = nix_go!(config.secrets);
+ if !shared_secrets.has_field(secret)? {
+ return Ok(None);
+ }
+ Ok(Some(SharedSecretDefinition(nix_go!(
+ shared_secrets[secret]
+ ))))
+ }
+
// TODO: Should this be something modifiable from other processes?
// E.g terraform provider might want to update FleetData (e.g secrets),
// and current implementation assumes only one process holds current fleet.nix
crates/fleet-base/src/keys.rsdiffbeforeafterboth--- a/crates/fleet-base/src/keys.rs
+++ b/crates/fleet-base/src/keys.rs
@@ -12,10 +12,10 @@
pub fn cached_key(&self, host: &str) -> Option<String> {
let data = self.data();
let key = data.hosts.get(host).map(|h| &h.encryption_key);
- if let Some(key) = key {
- if key.is_empty() {
- return None;
- }
+ if let Some(key) = key
+ && key.is_empty()
+ {
+ return None;
}
key.cloned()
}
@@ -30,7 +30,7 @@
Ok(key)
} else {
warn!("Loading key for {}", host);
- let host = self.host(host).await?;
+ let host = self.host(host)?;
let mut cmd = host.cmd("cat").await?;
cmd.arg("/etc/ssh/ssh_host_ed25519_key.pub");
let key = cmd.run_string().await?;
@@ -47,7 +47,7 @@
}
pub async fn recipients(&self, hosts: Vec<String>) -> Result<Vec<Box<dyn Recipient>>> {
- let hosts = self.expand_owner_set(hosts).await?;
+ let hosts = self.expand_owner_set(hosts)?;
futures::stream::iter(hosts.iter())
.then(|m| self.recipient(m.as_ref()))
.try_collect::<Vec<_>>()
@@ -57,12 +57,7 @@
#[allow(dead_code)]
pub async fn orphaned_data(&self) -> Result<Vec<String>> {
let mut out = Vec::new();
- let host_names = self
- .list_hosts()
- .await?
- .into_iter()
- .map(|h| h.name)
- .collect_vec();
+ let host_names = self.list_hosts()?.into_iter().map(|h| h.name).collect_vec();
for hostname in self
.data()
.hosts
crates/fleet-base/src/opts.rsdiffbeforeafterboth--- a/crates/fleet-base/src/opts.rs
+++ b/crates/fleet-base/src/opts.rs
@@ -104,20 +104,20 @@
}
impl FleetOpts {
- pub async fn filter_skipped(
+ pub fn filter_skipped(
&self,
hosts: impl IntoIterator<Item = ConfigHost>,
) -> Result<Vec<ConfigHost>> {
let mut out = Vec::new();
for host in hosts {
- if self.should_skip(&host).await? {
+ if self.should_skip(&host)? {
continue;
}
out.push(host);
}
Ok(out)
}
- pub async fn should_skip(&self, host: &ConfigHost) -> Result<bool> {
+ pub fn should_skip(&self, host: &ConfigHost) -> Result<bool> {
if self.skip.iter().any(|h| h as &str == host.name) {
return Ok(true);
}
@@ -137,7 +137,7 @@
}
}
if have_group_matches {
- let host_tags = host.tags().await?;
+ let host_tags = host.tags()?;
for item in self.only.iter() {
match item {
HostItem::Tag { name, .. } if host_tags.contains(name) => {
@@ -149,15 +149,15 @@
}
Ok(true)
}
- pub async fn action_attr<T: FromStr>(&self, host: &ConfigHost, attr: &str) -> Result<Option<T>>
+ pub fn action_attr<T: FromStr>(&self, host: &ConfigHost, attr: &str) -> Result<Option<T>>
where
T::Err: Sync,
anyhow::Error: From<T::Err>,
{
- let str = self.action_attr_str(host, attr).await?;
+ let str = self.action_attr_str(host, attr)?;
Ok(str.map(|v| T::from_str(&v)).transpose()?)
}
- pub async fn action_attr_str(&self, host: &ConfigHost, attr: &str) -> Result<Option<String>> {
+ pub fn action_attr_str(&self, host: &ConfigHost, attr: &str) -> Result<Option<String>> {
if self.only.is_empty() {
return Ok(None);
}
@@ -176,7 +176,7 @@
}
}
if have_group_matches {
- let host_tags = host.tags().await?;
+ let host_tags = host.tags()?;
for item in self.only.iter() {
match item {
HostItem::Tag { name, attrs }
@@ -195,7 +195,7 @@
}
// TODO: Config should be detached from opts.
- pub async fn build(&self, nix_args: Vec<OsString>, assert: bool) -> Result<Config> {
+ pub fn build(&self, nix_args: Vec<OsString>, assert: bool) -> Result<Config> {
let cwd = current_dir()?;
let mut directory = cwd.clone();
let mut fleet_data_path = directory.join("fleet.nix");
@@ -248,7 +248,6 @@
if assert {
assert_warn("fleet config evaluation", &config_field)
- .await
.context("failed to verify assertions")?;
}
crates/fleet-base/src/primops.rsdiffbeforeafterboth--- a/crates/fleet-base/src/primops.rs
+++ b/crates/fleet-base/src/primops.rs
@@ -1,38 +1,168 @@
-use std::cell::OnceCell;
-use std::collections::{BTreeMap, HashMap};
-use std::sync::{Arc, Mutex, OnceLock};
+use std::collections::{BTreeMap, BTreeSet, HashMap};
+use std::sync::OnceLock;
-use anyhow::{Context, bail};
+use anyhow::{Context, bail, ensure};
+use fleet_shared::SecretData;
use itertools::Itertools;
use nix_eval::{NativeFn, Value, nix_go, nix_go_json};
use serde::Deserialize;
use tracing::{info, warn};
-use crate::fleetdata::{FleetData, FleetSecrets};
-use crate::host::Config;
+use crate::fleetdata::{
+ Expectations, FleetSecretData, FleetSecretDistribution, FleetSecretPart, GeneratorPart,
+};
+use crate::host::{Config, ConfigHost};
+use crate::secret::{RegenerationReason, secret_needs_regeneration};
+use anyhow::{Result, anyhow};
#[derive(thiserror::Error, Debug)]
enum Error {}
-struct Parts {
- encrypted: Vec<String>,
- public: Vec<String>,
+pub static PRIMOPS_DATA: OnceLock<Config> = OnceLock::new();
+
+#[derive(Deserialize)]
+#[serde(rename_all = "camelCase")]
+enum GeneratorKind {
+ Impure,
+ Pure,
}
-trait SecretsBackend {
- fn has_shared(&self, name: &str);
- fn has_host(&self, host: &str, name: &str);
- fn shared_parts(&self, name: &str) -> Parts;
- fn host_parts(&self, host: &str, name: &str) -> Parts;
+pub fn get_pkgs_and_generators(host_on: &ConfigHost, recipients: Vec<String>) -> Result<Value> {
+ info!("get pkgs");
+ let pkgs = host_on.pkgs()?;
+ let default_mk_secret_generators = nix_go!(pkgs.mkSecretGenerators);
+ let generators = nix_go!(default_mk_secret_generators(Obj { recipients }));
+ Ok(pkgs.clone().attrs_update(generators)?)
+}
+pub fn get_default_pkgs_and_generators(config: &Config) -> Result<Value> {
+ let host_on = config.local_host();
+ get_pkgs_and_generators(&host_on, vec![])
}
+pub fn call_package(config: &Config, pkgs: &Value, package: &Value) -> Result<Value> {
+ ensure!(
+ package.is_function(),
+ "package should be a function to be called with callPackage"
+ );
+ // No need to use nixpkgs.buildUsing, as only nixpkgs-lib is used.
+ let nixpkgs = &config.nixpkgs;
+ let call_package = nix_go!(nixpkgs.lib.callPackageWith(pkgs));
+ Ok(nix_go!(call_package(package)(Obj {})))
+}
+
+pub fn get_default_generator_drv(config: &Config, generator: &Value) -> Result<Value> {
+ let default_pkgs_and_generators = get_default_pkgs_and_generators(config)?;
+ let default_generator_drv = call_package(config, &default_pkgs_and_generators, generator)
+ .context("failed to initialize generator to get metadata")?;
+
+ Ok(default_generator_drv)
+}
+
+pub async fn generate(
+ config: &Config,
+ expectations: Expectations,
+ generator: &Value,
+ default_generator_drv: &Value,
+) -> Result<FleetSecretDistribution> {
+ let kind: GeneratorKind = nix_go_json!(default_generator_drv.generatorKind);
+
+ match kind {
+ GeneratorKind::Impure => {
+ let impure_on: Option<String> = nix_go_json!(default_generator_drv.impureOn);
-struct FsSecretsBackend {}
+ let host_on = if let Some(on) = &impure_on {
+ config
+ .host(on)
+ .context("failed to get secret generation target host")?
+ } else {
+ config.local_host()
+ };
+ let pkgs_and_generators =
+ get_pkgs_and_generators(&host_on, expectations.owners.iter().cloned().collect())
+ .context("failed to get pkgs for target host")?;
+ let generator = call_package(config, &pkgs_and_generators, generator)
+ .context("failed to evaluate generator for target host")?;
-pub static PRIMOPS_DATA: OnceLock<Config> = OnceLock::new();
+ let generator = generator
+ .build("out")
+ .context("failed to build generator for target host")?;
-#[derive(Deserialize, Debug)]
-struct GeneratorPart {
- encrypted: bool,
+ let generator = host_on
+ .remote_derivation(&generator)
+ .await
+ .context("failed to copy generator to target host")?;
+
+ // TODO: Remove destdir after everything is done
+ let out_parent = host_on
+ .mktemp_dir()
+ .await
+ .context("failed to prepare generator output dir on target host")?;
+ let out = format!("{out_parent}/out");
+ let mut generator_cmd = host_on.cmd(generator).await?;
+ generator_cmd.env("out", &out);
+ if impure_on.is_none() {
+ let project_path: String = config
+ .directory
+ .clone()
+ .into_os_string()
+ .into_string()
+ .map_err(|e| anyhow!("fleet project path is not utf-8: {e:?}"))?;
+ generator_cmd.env("FLEET_PROJECT", project_path);
+ };
+ generator_cmd
+ .run()
+ .await
+ .context("failed to run impure generator")?;
+
+ {
+ let marker = host_on.read_file_text(format!("{out}/marker")).await?;
+ ensure!(
+ marker == "SUCCESS",
+ "impure generator ended prematurely, secret generation failed"
+ );
+ }
+
+ let mut parts = BTreeMap::new();
+ for part in host_on.read_dir(&out).await? {
+ if part == "created_at" || part == "expires_at" || part == "marker" {
+ continue;
+ }
+ let contents: SecretData = host_on
+ .read_file_text(format!("{out}/{part}"))
+ .await?
+ .parse()
+ .map_err(|e| anyhow!("failed to decode secret {out:?} part {part:?}: {e}"))?;
+ parts.insert(part.to_owned(), FleetSecretPart { raw: contents });
+ }
+
+ let created_at = host_on.read_file_value(format!("{out}/created_at")).await?;
+ let expires_at = host_on
+ .read_file_value(format!("{out}/expires_at"))
+ .await
+ .ok();
+
+ let new_data = FleetSecretData {
+ created_at,
+ expires_at,
+ parts,
+ generation_data: expectations.generation_data.clone(),
+ };
+
+ let new_data = FleetSecretDistribution {
+ secret: new_data,
+ owners: expectations.owners.clone(),
+ _deprecated_managed: true,
+ };
+
+ if let Some(reason) = secret_needs_regeneration(&new_data, &expectations) {
+ bail!("newly generated secret needs to be regenerated: {reason}")
+ }
+
+ Ok(new_data)
+ }
+ GeneratorKind::Pure => {
+ bail!("pure generators are disabled for now")
+ }
+ }
}
pub fn init_primops() {
@@ -52,52 +182,61 @@
.get()
.expect("primops data should be set on init");
- info!("get pkgs");
- let nixpkgs = &config.nixpkgs;
- let default_pkgs = &config.default_pkgs;
- let default_mk_secret_generators = nix_go!(default_pkgs.mkSecretGenerators);
- let generators = nix_go!(default_mk_secret_generators(Obj {
- recipients: <Vec<String>>::new(),
- }));
- let pkgs_and_generators = default_pkgs.clone().attrs_update(generators)?;
+ let shared_def = config.secret_definition(&secret).context("failed to get shared secret definition")?;
- info!("call package");
- let call_package = nix_go!(nixpkgs.lib.callPackageWith(pkgs_and_generators));
- let default_generator = call_package
- .call(generator.clone())
- .context("calling callPackage with generator")?
- .call(Value::new_attrs(HashMap::new()))
- .context("providing extra callPackage args")?;
+ let (shared, generator, expected_owners) = if generator.is_string() {
+ assert_eq!(generator.to_string()?, "shared", "asserted by nixos type system");
+ let Some(shared_def) = shared_def else {
+ bail!("secret {secret} is defined on host {host} as shared, but there is no shared secret with same name defined at fleetConfiguration.secrets.{secret}.generator")
+ };
+ let expected_owners = shared_def.expected_owners()?;
+
+ ensure!(expected_owners.contains(&host), "secret {secret} does not define {host} as expected owner");
- info!("get parts");
- let mut parts: BTreeMap<String, GeneratorPart> = nix_go_json!(default_generator.parts);
- info!("got parts: {parts:?}");
+ (true, shared_def.generator()?, expected_owners)
+ } else {
+ if shared_def.is_some() {
+ bail!("hosts can only have their own generators for non-shared secrets, either set host secret generator to \"shared\", or remove shared secret generator at fleetConfiguration.secrets.{secret}.generator")
+ }
- let Some(existing) = config
- .host_secret(&host, &secret) else {
- bail!("missing secret {secret} for host {host}; secret needs regeneration")
+ (false, generator.clone(), BTreeSet::from_iter([host.clone()]))
};
- info!("got existing: {existing:?}");
+ let default_generator_drv = get_default_generator_drv(config, &generator).context("failed to evaluate default generator")?;
+ let expectations = Expectations {
+ parts: nix_go_json!(default_generator_drv.parts),
+ generation_data: nix_go_json!(default_generator_drv.generationData),
+ owners: expected_owners,
+ };
+
+ let reason: RegenerationReason = 'regenerate: {
+ let Some(existing) = config
+ .host_secret(&host, &secret) else {
+ break 'regenerate RegenerationReason::Missing;
+ };
+ if let Some(reason) = secret_needs_regeneration(&existing, &expectations) {
+ break 'regenerate reason;
+ }
- let mut out = HashMap::new();
+ let mut parts = expectations.parts.clone();
- for (part_name, part) in &existing.secret.parts {
- let Some(definition) = parts.remove(part_name) else {
- warn!("secret {secret} part {part_name} is stored, but not defined in nixos config, it will not be passed to nix");
- continue;
- };
- if definition.encrypted != part.raw.encrypted {
- bail!("secret {secret} part {part_name} is supposed to be {}, but it is {}; secret needs regeneration", if definition.encrypted {"encrypted"} else {"unencrypted"}, if part.raw.encrypted {"encrypted"} else {"unencrypted"});
+ let mut out = HashMap::new();
+ for (part_name, part) in &existing.secret.parts {
+ let Some(definition) = parts.remove(part_name) else {
+ warn!("secret {secret} part {part_name} is stored, but not defined in nixos config, it will not be passed to nix");
+ continue;
+ };
+ assert!(definition.encrypted != part.raw.encrypted, "encryption status is checked by secret_needs_regeneration");
+ out.insert(part_name.as_str(), Value::new_attrs(HashMap::from_iter([("raw", Value::new_str(&part.raw.to_string()))])));
}
- out.insert(part_name.as_str(), Value::new_attrs(HashMap::from_iter([("raw", Value::new_str(&part.raw.to_string()))])));
- }
- if !parts.is_empty(){
- let defs = parts.keys().collect_vec();
- bail!("secret parts are defined, but not stored: {defs:?}, secret needs regeneration")
- }
+ assert!(parts.is_empty(), "secret part is missing, secret_needs_regeneration should check that");
- Ok(Value::new_attrs(out))
+ return Ok(Value::new_attrs(out))
+ };
+
+ todo!()
+
+
},
)
.register();
crates/fleet-base/src/secret.rsdiffbeforeafterboth--- a/crates/fleet-base/src/secret.rs
+++ b/crates/fleet-base/src/secret.rs
@@ -1,16 +1,8 @@
-use std::collections::BTreeSet;
+use std::collections::{BTreeMap, BTreeSet};
use chrono::{DateTime, Utc};
-use crate::fleetdata::FleetSecretData;
-
-#[derive(Debug)]
-pub struct Expectations {
- pub owners: BTreeSet<String>,
- pub generation_data: serde_json::Value,
- pub public_parts: BTreeSet<String>,
- pub private_parts: BTreeSet<String>,
-}
+use crate::fleetdata::{Expectations, FleetSecretData, FleetSecretDistribution, GeneratorPart};
#[derive(thiserror::Error, Debug)]
pub enum RegenerationReason {
@@ -34,56 +26,62 @@
ExpectedPublic(String),
#[error("secret is expired at {0}")]
Expired(DateTime<Utc>),
+
+ #[error("secret is not generated for this host")]
+ Missing,
}
pub fn secret_needs_regeneration(
- secret: &FleetSecretData,
- owners: &BTreeSet<String>,
+ secret: &FleetSecretDistribution,
expectations: &Expectations,
) -> Option<RegenerationReason> {
- if !owners.is_empty() {
- let added: BTreeSet<String> = expectations.owners.difference(owners).cloned().collect();
- if !added.is_empty() {
- return Some(RegenerationReason::OwnersAdded(added));
- }
+ let added: BTreeSet<String> = expectations
+ .owners
+ .difference(&secret.owners)
+ .cloned()
+ .collect();
+ if !added.is_empty() {
+ return Some(RegenerationReason::OwnersAdded(added));
+ }
- let removed: BTreeSet<String> = owners.difference(&expectations.owners).cloned().collect();
- if !removed.is_empty() {
- return Some(RegenerationReason::OwnersRemoved(removed));
- }
+ let removed: BTreeSet<String> = secret
+ .owners
+ .difference(&expectations.owners)
+ .cloned()
+ .collect();
+ if !removed.is_empty() {
+ return Some(RegenerationReason::OwnersRemoved(removed));
}
- if secret.generation_data != expectations.generation_data {
+ if secret.secret.generation_data != expectations.generation_data {
return Some(RegenerationReason::GenerationData {
expected: expectations.generation_data.clone(),
- found: secret.generation_data.clone(),
+ found: secret.secret.generation_data.clone(),
});
}
- if !expectations.public_parts.is_empty() || !expectations.private_parts.is_empty() {
- let expected: BTreeSet<String> = expectations
- .public_parts
- .union(&expectations.private_parts)
- .cloned()
- .collect();
- let found: BTreeSet<String> = secret.parts.keys().cloned().collect();
+ let expected: BTreeSet<String> = expectations.parts.keys().cloned().collect();
+ let found: BTreeSet<String> = secret.secret.parts.keys().cloned().collect();
- if found != expected {
- return Some(RegenerationReason::PartList { expected, found });
- }
+ if found != expected {
+ return Some(RegenerationReason::PartList { expected, found });
+ }
- for (name, value) in secret.parts.iter() {
- if value.raw.encrypted {
- if !expectations.private_parts.contains(name) {
- return Some(RegenerationReason::ExpectedPrivate(name.clone()));
- }
- } else if !expectations.public_parts.contains(name) {
- return Some(RegenerationReason::ExpectedPublic(name.clone()));
+ for (name, value) in secret.secret.parts.iter() {
+ let expectation = expectations
+ .parts
+ .get(name)
+ .expect("found == expected checked");
+ if value.raw.encrypted {
+ if !expectation.encrypted {
+ return Some(RegenerationReason::ExpectedPrivate(name.clone()));
}
+ } else if expectation.encrypted {
+ return Some(RegenerationReason::ExpectedPublic(name.clone()));
}
}
- if let Some(expiration) = secret.expires_at {
+ if let Some(expiration) = secret.secret.expires_at {
// TODO: Leeway?
if expiration < Utc::now() {
return Some(RegenerationReason::Expired(expiration));
crates/nix-eval/src/lib.rsdiffbeforeafterboth--- a/crates/nix-eval/src/lib.rs
+++ b/crates/nix-eval/src/lib.rs
@@ -731,6 +731,10 @@
}
pub fn has_field(&self, field: &str) -> Result<bool> {
+ if !matches!(self.type_of(), NixType::Attrs) {
+ bail!("invalid type: expected attrs");
+ }
+
let f = init_field_name(field);
with_default_context(|c, es| unsafe { has_attr_byname(c, self.0, es, f.as_ptr().cast()) })
}
@@ -881,6 +885,12 @@
pub fn is_null(&self) -> bool {
matches!(self.type_of(), NixType::Null)
}
+ pub fn is_string(&self) -> bool {
+ matches!(self.type_of(), NixType::String)
+ }
+ pub fn is_attrs(&self) -> bool {
+ matches!(self.type_of(), NixType::Attrs)
+ }
}
impl From<String> for Value {
crates/nix-eval/src/util.rsdiffbeforeafterboth--- a/crates/nix-eval/src/util.rs
+++ b/crates/nix-eval/src/util.rs
@@ -1,23 +1,15 @@
use std::time::Instant;
use anyhow::bail;
-use serde::Deserialize;
use tracing::{debug, warn};
use crate::{Value, nix_go_json};
-
-#[derive(Deserialize, Debug)]
-struct Assertion {
- assertion: bool,
- message: String,
-}
#[tracing::instrument(level = "info", skip(val))]
-pub async fn assert_warn(action: &str, val: &Value) -> anyhow::Result<()> {
+pub fn assert_warn(action: &str, val: &Value) -> anyhow::Result<()> {
let before_errors = Instant::now();
let errors: Vec<String> = nix_go_json!(val.errors);
- // let assertions: Vec<Assertion> = nix_go_json!(val.assertions);
- debug!("errors evaluation took {:?} {errors:?} ", before_errors.elapsed());
+ debug!("errors evaluation took {:?}", before_errors.elapsed());
if !errors.is_empty() {
bail!(
"failed with error{}{}",
lib/default.nixdiffbeforeafterboth--- a/lib/default.nix
+++ b/lib/default.nix
@@ -160,7 +160,7 @@
mkImpureSecretGenerator,
}:
mkImpureSecretGenerator {
- # TODO: Escape prompt?
+ # TODO: Escape prompt/part (preferrably just use env) to prevent shell injection
script = ''
${kdePackages.kdialog}/bin/kdialog --inputbox "${prompt}" | gh private -o $out/${part}
'';
modules/secrets.nixdiffbeforeafterboth--- a/modules/secrets.nix
+++ b/modules/secrets.nix
@@ -89,6 +89,7 @@
# If set - script will be run on remote machine, otherwise it will be run with fleet project in CWD
# (Some secrets-encryption-in-git/managed PKI solution is expected)
impureOn ? null,
+ generationData ? null,
parts,
}:
(prev.writeShellScript "impureGenerator.sh" ''
@@ -117,7 +118,7 @@
'').overrideAttrs
(old: {
passthru = {
- inherit impureOn parts;
+ inherit impureOn parts generationData;
generatorKind = "impure";
};
});