Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions checks/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,39 @@ let
imports = [ "${modulesPath}/profiles/hardened.nix" ];
}) ];
} ]

[ {
# no
id = null;
} {
id = "credentials";
modules = [ ({ config, pkgs, ... }: {
# This is the guest vm config
microvm = {
credentialFiles.SECRET_BOOTSTRAP_KEY = "/etc/microvm-bootstrap.secret";
testing.enableTest = builtins.elem config.microvm.hypervisor [
# Hypervisors that support systemd credentials
"qemu"
];
};
# TODO: need to somehow have the test harness check for the success or failure of this service.
systemd.services.test-secret-availability = {
serviceConfig = {
ImportCredential = "SECRET_BOOTSTRAP_KEY";
Restart = "no";
};
path = [ pkgs.gnugrep pkgs.coreutils ];
script = ''
cat $CREDENTIALS_DIRECTORY/SECRET_BOOTSTRAP_KEY | grep -q "i am super secret"
if [ $? -ne 0 ]; then
echo "Secret not found at $CREDENTIALS_DIRECTORY/SECRET_BOOTSTRAP_KEY"
exit 1
fi
'';
};
}) ];
} ]

];

allVariants =
Expand Down
2 changes: 2 additions & 0 deletions checks/vm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
# Must be big enough for the store overlay volume
virtualisation.diskSize = 4096;

environment.etc."microvm-bootstrap.secret".text = "i am super secret";

microvm.vms."${system}-${hypervisor}-example".flake = self;
};
testScript = ''
Expand Down
4 changes: 3 additions & 1 deletion lib/runners/alioth.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let
user
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem interfaces volumes shares devices vsock
kernel initrdPath
storeDisk storeOnDisk;
storeDisk storeOnDisk credentialFiles;
in {
command =
if user != null
Expand All @@ -22,6 +22,8 @@ in {
then throw "alioth does not support hotplugMem"
else if hotpluggedMem != 0
then throw "alioth does not support hotpluggedMem"
else if credentialFiles != {}
then throw "alioth does not support credentialFiles"
else builtins.concatStringsSep " " (
[
"${pkgs.alioth}/bin/alioth" "run"
Expand Down
4 changes: 3 additions & 1 deletion lib/runners/cloud-hypervisor.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

let
inherit (pkgs) lib;
inherit (microvmConfig) vcpu mem balloon initialBalloonMem deflateOnOOM hotplugMem hotpluggedMem user interfaces volumes shares socket devices hugepageMem graphics storeDisk storeOnDisk kernel initrdPath;
inherit (microvmConfig) vcpu mem balloon initialBalloonMem deflateOnOOM hotplugMem hotpluggedMem user interfaces volumes shares socket devices hugepageMem graphics storeDisk storeOnDisk kernel initrdPath credentialFiles;
inherit (microvmConfig.cloud-hypervisor) platformOEMStrings extraArgs;

hasUserConsole = (extractOptValues "--console" extraArgs).values != [];
Expand Down Expand Up @@ -147,6 +147,8 @@ in {
command =
if user != null
then throw "cloud-hypervisor will not change user"
else if credentialFiles != {}
then throw "cloud-hypervisor does not support credentialFiles"
else lib.escapeShellArgs (
[
(if graphics.enable
Expand Down
4 changes: 3 additions & 1 deletion lib/runners/crosvm.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ let
inherit (pkgs.stdenv) system;
inherit (microvmConfig)
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem user volumes shares
socket devices vsock graphics
socket devices vsock graphics credentialFiles
kernel initrdPath storeDisk storeOnDisk;
inherit (microvmConfig.crosvm) pivotRoot extraArgs;

Expand Down Expand Up @@ -53,6 +53,8 @@ in {
then throw "crosvm does not support hotplugMem"
else if hotpluggedMem != 0
then throw "crosvm does not support hotpluggedMem"
else if credentialFiles != {}
then throw "crosvm does not support credentialFiles"
else lib.escapeShellArgs (
[
"${pkgs.crosvm}/bin/crosvm" "run"
Expand Down
4 changes: 3 additions & 1 deletion lib/runners/firecracker.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem
interfaces volumes shares devices
kernel initrdPath
storeDisk;
storeDisk credentialFiles;
inherit (microvmConfig.firecracker) cpu;

kernelPath = {
Expand Down Expand Up @@ -83,6 +83,8 @@ in {
then throw "hotplugMem not implemented for Firecracker"
else if hotpluggedMem != 0
then throw "hotpluggedMem not implemented for Firecracker"
else if credentialFiles != {}
then throw "credentialFiles are not implemented for Firecracker"
else lib.escapeShellArgs [
"${pkgs.firecracker}/bin/firecracker"
"--config-file" configFile
Expand Down
4 changes: 3 additions & 1 deletion lib/runners/kvmtool.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let
inherit (microvmConfig)
hostName preStart user
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem interfaces volumes shares devices vsock
kernel initrdPath
kernel initrdPath credentialFiles
storeDisk storeOnDisk;
in {
preStart = ''
Expand All @@ -25,6 +25,8 @@ in {
then throw "kvmtool does not support hotplugMem"
else if hotpluggedMem != 0
then throw "kvmtool does not support hotpluggedMem"
else if credentialFiles != {}
then throw "kvmtool does not support credentialFiles"
else builtins.concatStringsSep " " (
[
"${pkgs.kvmtool}/bin/lkvm" "run"
Expand Down
7 changes: 6 additions & 1 deletion lib/runners/qemu.nix
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ let

qemu = overrideQemu qemuPkg;

inherit (microvmConfig) hostName vcpu mem balloon initialBalloonMem deflateOnOOM hotplugMem hotpluggedMem user interfaces shares socket forwardPorts devices vsock graphics storeOnDisk kernel initrdPath storeDisk;
inherit (microvmConfig) hostName vcpu mem balloon initialBalloonMem deflateOnOOM hotplugMem hotpluggedMem user interfaces shares socket forwardPorts devices vsock graphics storeOnDisk kernel initrdPath storeDisk credentialFiles;
inherit (microvmConfig.qemu) machine extraArgs serialConsole;


Expand Down Expand Up @@ -155,6 +155,8 @@ let
then "console=ttyAMA0"
else "";

systemdCredentialStrings = lib.mapAttrsToList (name: path: "name=opt/io.systemd.credentials/${name},file=${path}" ) credentialFiles;
fwCfgOptions = systemdCredentialStrings;

in
lib.warnIf (mem == 2048) ''
Expand Down Expand Up @@ -188,6 +190,9 @@ lib.warnIf (mem == 2048) ''
"-chardev" "stdio,id=stdio,signal=off"
"-device" "virtio-rng-${devType}"
] ++
lib.optionals (fwCfgOptions != []) [
"-fw_cfg" (lib.concatStringsSep "," fwCfgOptions)
] ++
lib.optionals serialConsole [
"-serial" "chardev:stdio"
] ++
Expand Down
4 changes: 3 additions & 1 deletion lib/runners/stratovirt.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let
inherit (microvmConfig)
hostName
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem interfaces shares socket forwardPorts devices
kernel initrdPath
kernel initrdPath credentialFiles
storeOnDisk storeDisk;

tapMultiQueue = vcpu > 1;
Expand Down Expand Up @@ -79,6 +79,8 @@ in {
then throw "stratovirt does not support hotplugMem"
else if hotpluggedMem != 0
then throw "stratovirt does not support hotpluggedMem"
else if credentialFiles != {}
then throw "stratovirt does not support credentialFiles"
else lib.escapeShellArgs (
[
"${pkgs.expect}/bin/unbuffer"
Expand Down
13 changes: 13 additions & 0 deletions nixos-modules/microvm/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,19 @@ in
This is required for commands like `microvm -l` to function but removes reference to the uncompressed store content when using a disk image for the nix store.
'';
};

credentialFiles = mkOption {
type = with types; attrsOf path;
default = {};
description = ''
Key-value pairs of credential files that will be loaded into the vm using systemd's io.systemd.credential feature.
'';
example = literalExpression /* nix */ ''
{
SOPS_AGE_KEY = "/run/secrets/guest_microvm_age_key";
}
'';
};
};

imports = [
Expand Down