Skip to content

Commit 966a80e

Browse files
Merge pull request #337 from Ramblurr/feat/systemd-credentials
2 parents f0e65eb + bb97831 commit 966a80e

File tree

10 files changed

+72
-7
lines changed

10 files changed

+72
-7
lines changed

checks/default.nix

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,39 @@ let
115115
imports = [ "${modulesPath}/profiles/hardened.nix" ];
116116
}) ];
117117
} ]
118+
119+
[ {
120+
# no
121+
id = null;
122+
} {
123+
id = "credentials";
124+
modules = [ ({ config, pkgs, ... }: {
125+
# This is the guest vm config
126+
microvm = {
127+
credentialFiles.SECRET_BOOTSTRAP_KEY = "/etc/microvm-bootstrap.secret";
128+
testing.enableTest = builtins.elem config.microvm.hypervisor [
129+
# Hypervisors that support systemd credentials
130+
"qemu"
131+
];
132+
};
133+
# TODO: need to somehow have the test harness check for the success or failure of this service.
134+
systemd.services.test-secret-availability = {
135+
serviceConfig = {
136+
ImportCredential = "SECRET_BOOTSTRAP_KEY";
137+
Restart = "no";
138+
};
139+
path = [ pkgs.gnugrep pkgs.coreutils ];
140+
script = ''
141+
cat $CREDENTIALS_DIRECTORY/SECRET_BOOTSTRAP_KEY | grep -q "i am super secret"
142+
if [ $? -ne 0 ]; then
143+
echo "Secret not found at $CREDENTIALS_DIRECTORY/SECRET_BOOTSTRAP_KEY"
144+
exit 1
145+
fi
146+
'';
147+
};
148+
}) ];
149+
} ]
150+
118151
];
119152

120153
allVariants =

checks/vm.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
# Must be big enough for the store overlay volume
1818
virtualisation.diskSize = 4096;
1919

20+
environment.etc."microvm-bootstrap.secret".text = "i am super secret";
21+
2022
microvm.vms."${system}-${hypervisor}-example".flake = self;
2123
};
2224
testScript = ''

lib/runners/alioth.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let
99
user
1010
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem interfaces volumes shares devices vsock
1111
kernel initrdPath
12-
storeDisk storeOnDisk;
12+
storeDisk storeOnDisk credentialFiles;
1313
in {
1414
command =
1515
if user != null
@@ -22,6 +22,8 @@ in {
2222
then throw "alioth does not support hotplugMem"
2323
else if hotpluggedMem != 0
2424
then throw "alioth does not support hotpluggedMem"
25+
else if credentialFiles != {}
26+
then throw "alioth does not support credentialFiles"
2527
else builtins.concatStringsSep " " (
2628
[
2729
"${pkgs.alioth}/bin/alioth" "run"

lib/runners/cloud-hypervisor.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

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

1313
hasUserConsole = (extractOptValues "--console" extraArgs).values != [];
@@ -147,6 +147,8 @@ in {
147147
command =
148148
if user != null
149149
then throw "cloud-hypervisor will not change user"
150+
else if credentialFiles != {}
151+
then throw "cloud-hypervisor does not support credentialFiles"
150152
else lib.escapeShellArgs (
151153
[
152154
(if graphics.enable

lib/runners/crosvm.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let
99
inherit (pkgs.stdenv) system;
1010
inherit (microvmConfig)
1111
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem user volumes shares
12-
socket devices vsock graphics
12+
socket devices vsock graphics credentialFiles
1313
kernel initrdPath storeDisk storeOnDisk;
1414
inherit (microvmConfig.crosvm) pivotRoot extraArgs;
1515

@@ -53,6 +53,8 @@ in {
5353
then throw "crosvm does not support hotplugMem"
5454
else if hotpluggedMem != 0
5555
then throw "crosvm does not support hotpluggedMem"
56+
else if credentialFiles != {}
57+
then throw "crosvm does not support credentialFiles"
5658
else lib.escapeShellArgs (
5759
[
5860
"${pkgs.crosvm}/bin/crosvm" "run"

lib/runners/firecracker.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let
1010
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem
1111
interfaces volumes shares devices
1212
kernel initrdPath
13-
storeDisk;
13+
storeDisk credentialFiles;
1414
inherit (microvmConfig.firecracker) cpu;
1515

1616
kernelPath = {
@@ -83,6 +83,8 @@ in {
8383
then throw "hotplugMem not implemented for Firecracker"
8484
else if hotpluggedMem != 0
8585
then throw "hotpluggedMem not implemented for Firecracker"
86+
else if credentialFiles != {}
87+
then throw "credentialFiles are not implemented for Firecracker"
8688
else lib.escapeShellArgs [
8789
"${pkgs.firecracker}/bin/firecracker"
8890
"--config-file" configFile

lib/runners/kvmtool.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let
88
inherit (microvmConfig)
99
hostName preStart user
1010
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem interfaces volumes shares devices vsock
11-
kernel initrdPath
11+
kernel initrdPath credentialFiles
1212
storeDisk storeOnDisk;
1313
in {
1414
preStart = ''
@@ -25,6 +25,8 @@ in {
2525
then throw "kvmtool does not support hotplugMem"
2626
else if hotpluggedMem != 0
2727
then throw "kvmtool does not support hotpluggedMem"
28+
else if credentialFiles != {}
29+
then throw "kvmtool does not support credentialFiles"
2830
else builtins.concatStringsSep " " (
2931
[
3032
"${pkgs.kvmtool}/bin/lkvm" "run"

lib/runners/qemu.nix

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ let
4949

5050
qemu = overrideQemu qemuPkg;
5151

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

5555

@@ -155,6 +155,8 @@ let
155155
then "console=ttyAMA0"
156156
else "";
157157

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

159161
in
160162
lib.warnIf (mem == 2048) ''
@@ -188,6 +190,9 @@ lib.warnIf (mem == 2048) ''
188190
"-chardev" "stdio,id=stdio,signal=off"
189191
"-device" "virtio-rng-${devType}"
190192
] ++
193+
lib.optionals (fwCfgOptions != []) [
194+
"-fw_cfg" (lib.concatStringsSep "," fwCfgOptions)
195+
] ++
191196
lib.optionals serialConsole [
192197
"-serial" "chardev:stdio"
193198
] ++

lib/runners/stratovirt.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let
1111
inherit (microvmConfig)
1212
hostName
1313
vcpu mem balloon initialBalloonMem hotplugMem hotpluggedMem interfaces shares socket forwardPorts devices
14-
kernel initrdPath
14+
kernel initrdPath credentialFiles
1515
storeOnDisk storeDisk;
1616

1717
tapMultiQueue = vcpu > 1;
@@ -79,6 +79,8 @@ in {
7979
then throw "stratovirt does not support hotplugMem"
8080
else if hotpluggedMem != 0
8181
then throw "stratovirt does not support hotpluggedMem"
82+
else if credentialFiles != {}
83+
then throw "stratovirt does not support credentialFiles"
8284
else lib.escapeShellArgs (
8385
[
8486
"${pkgs.expect}/bin/unbuffer"

nixos-modules/microvm/options.nix

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,19 @@ in
675675
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.
676676
'';
677677
};
678+
679+
credentialFiles = mkOption {
680+
type = with types; attrsOf path;
681+
default = {};
682+
description = ''
683+
Key-value pairs of credential files that will be loaded into the vm using systemd's io.systemd.credential feature.
684+
'';
685+
example = literalExpression /* nix */ ''
686+
{
687+
SOPS_AGE_KEY = "/run/secrets/guest_microvm_age_key";
688+
}
689+
'';
690+
};
678691
};
679692

680693
imports = [

0 commit comments

Comments
 (0)