Agenix: Difference between revisions
imported>Dafitt Alternate way to access secrets inside container |
m →Choose a Public/Private Key: link to ssh pages |
||
(2 intermediate revisions by 2 users not shown) | |||
Line 37: | Line 37: | ||
== Configuration == | == Configuration == | ||
First create a directory where secrets are going to be stored. In this example we | === Choose a Public/Private Key === | ||
First, we have to decide which [[SSH public key authentication|SSH public key]] to use to encrypt the secrets. (The private key will be used to decrypt the secrets when loading the NixOS configuration.) | |||
Assuming that you have [[SSH]] already installed on your NixOS server (with i.e. <code>services.openssh.enable = true;</code>), you will already have two different SSH keypairs that are intended to be used by the system itself, located at: | |||
* <code>/etc/ssh/ssh_host_rsa_key</code> / <code>/etc/ssh/ssh_host_rsa_key.pub</code> | |||
* <code>/etc/ssh/ssh_host_ed25519_key</code> / <code>/etc/ssh/ssh_host_ed25519_key.pub</code> | |||
If you load your NixOS config using the root user, then you can use these public keys to encrypt your secrets. | |||
However, if you load your NixOS config using some other user, then you will have to use <code>ssh-keygen</code> to generate a keypair for that user, which typically lives in: | |||
* <code>~/.ssh/id_rsa</code> / <code>~/.ssh/id_rsa.pub</code> | |||
* <code>~/.ssh/id_ed25519</code> / <code>~/.ssh/id_ed25519.pub</code> | |||
For more information, see [[SSH_public_key_authentication|the SSH public key authentication page]]. | |||
=== Create the Secrets === | |||
Next, create a directory where secrets are going to be stored. In this example we are creating the directory <code>secrets</code> inside the NixOS system configuration path <code>/etc/nixos</code> | |||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 57: | Line 77: | ||
} | } | ||
</nowiki>}} | </nowiki>}} | ||
== Usage == | == Usage == | ||
Line 89: | Line 107: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Here, the service [[Nextcloud]] requires a password for the administrator account. In this case, the password is stored in an age-encrypted file, so no plaintext passwords will be copied into your world-readable Nix-store. We configure <code>owner</code> and <code>group</code> names to <code>nextcloud</code> so that the webservice has the permissions to read the password | Here, the service [[Nextcloud]] requires a password for the administrator account. In this case, the password is stored in an age-encrypted file, so no plaintext passwords will be copied into your world-readable Nix-store. We configure <code>owner</code> and <code>group</code> names to <code>nextcloud</code> so that the webservice has the permissions to read the password file. | ||
Secrets can be also deployed as file with specific permissions to a target path. In this example the secret is sourced to <code>/home/myuser/.netrc</code> and permissions are set that only <code>myuser</code> is able to read and write the file | Secrets can be also deployed as file with specific permissions to a target path. In this example the secret is sourced to <code>/home/myuser/.netrc</code> and permissions are set that only <code>myuser</code> is able to read and write the file. | ||
<syntaxhighlight lang="nix"> | <syntaxhighlight lang="nix"> | ||
Line 172: | Line 190: | ||
For this workaround you'll have to rebuild twice and reference the secret <code>/etc/initrd-hostkey</code> only after the file is created. | For this workaround you'll have to rebuild twice and reference the secret <code>/etc/initrd-hostkey</code> only after the file is created. | ||
=== Agenix with Impermanence === | |||
If your system is configured to be [[Impermanence|impermanent]], then it's possible the system's ssh keys won't yet be available during boot to decrypt secrets. The solution is to manually set <code>age.identityPaths</code> to the persistent paths of your keys. | |||
<syntaxhighlight lang="nix"> | |||
# Direct path to persistent location of system ssh keys | |||
age.identityPaths = [ | |||
"/persist/etc/ssh/ssh_host_ed25519_key" | |||
"/persist/etc/ssh/ssh_host_rsa_key" | |||
]; | |||
</syntaxhighlight> | |||
== See also == | == See also == |