-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make symlink path configurable #461
Open
jdonszelmann
wants to merge
4
commits into
Mic92:master
Choose a base branch
from
jdonszelmann:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -19,7 +19,7 @@ let | |||||
type = types.str; | ||||||
default = config._module.args.name; | ||||||
description = '' | ||||||
Name of the file used in /run/secrets | ||||||
Name of the file used in /run/secrets (or your configured symlinkPath) | ||||||
''; | ||||||
}; | ||||||
key = mkOption { | ||||||
|
@@ -33,15 +33,16 @@ let | |||||
}; | ||||||
path = mkOption { | ||||||
type = types.str; | ||||||
default = if config.neededForUsers then "/run/secrets-for-users/${config.name}" else "/run/secrets/${config.name}"; | ||||||
default = if config.neededForUsers then "/run/secrets-for-users/${config.name}" else "${cfg.symlinkPath}/${config.name}"; | ||||||
defaultText = "/run/secrets-for-users/$name when neededForUsers is set, /run/secrets/$name when otherwise."; | ||||||
description = '' | ||||||
Path where secrets are symlinked to. | ||||||
If the default is kept no symlink is created. | ||||||
If cfg.symlinkPath is set, /run/secrets will become that path | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I am not sure if I understand this description correct |
||||||
''; | ||||||
}; | ||||||
format = mkOption { | ||||||
type = types.enum ["yaml" "json" "binary" "dotenv" "ini"]; | ||||||
type = types.enum [ "yaml" "json" "binary" "dotenv" "ini" ]; | ||||||
default = cfg.defaultSopsFormat; | ||||||
description = '' | ||||||
File format used to decrypt the sops secret. | ||||||
|
@@ -118,9 +119,8 @@ let | |||||
name = "manifest${suffix}.json"; | ||||||
text = builtins.toJSON ({ | ||||||
secrets = builtins.attrValues secrets; | ||||||
# Does this need to be configurable? | ||||||
secretsMountPoint = "/run/secrets.d"; | ||||||
symlinkPath = "/run/secrets"; | ||||||
secretsMountPoint = cfg.secretsMountPoint; | ||||||
symlinkPath = cfg.symlinkPath; | ||||||
keepGenerations = cfg.keepGenerations; | ||||||
gnupgHome = cfg.gnupg.home; | ||||||
sshKeyPaths = cfg.gnupg.sshKeyPaths; | ||||||
|
@@ -138,13 +138,13 @@ let | |||||
''; | ||||||
}; | ||||||
|
||||||
manifest = manifestFor "" regularSecrets {}; | ||||||
manifest = manifestFor "" regularSecrets { }; | ||||||
manifestForUsers = manifestFor "-for-users" secretsForUsers { | ||||||
secretsMountPoint = "/run/secrets-for-users.d"; | ||||||
symlinkPath = "/run/secrets-for-users"; | ||||||
}; | ||||||
|
||||||
withEnvironment = sopsCall: if cfg.environment == {} then sopsCall else '' | ||||||
withEnvironment = sopsCall: if cfg.environment == { } then sopsCall else '' | ||||||
( | ||||||
${concatStringsSep "\n" (mapAttrsToList (n: v: " export ${n}='${v}'") cfg.environment)} | ||||||
${sopsCall} | ||||||
|
@@ -153,14 +153,15 @@ let | |||||
# Skip ssh keys deployed with sops to avoid a catch 22 | ||||||
defaultImportKeys = algo: | ||||||
if config.services.openssh.enable then | ||||||
map (e: e.path) (lib.filter (e: e.type == algo && !(lib.hasPrefix "/run/secrets" e.path)) config.services.openssh.hostKeys) | ||||||
map (e: e.path) (lib.filter (e: e.type == algo && !(lib.hasPrefix cfg.symlinkPath e.path)) config.services.openssh.hostKeys) | ||||||
else | ||||||
[]; | ||||||
in { | ||||||
[ ]; | ||||||
in | ||||||
{ | ||||||
options.sops = { | ||||||
secrets = mkOption { | ||||||
type = types.attrsOf secretType; | ||||||
default = {}; | ||||||
default = { }; | ||||||
description = '' | ||||||
Path where the latest secrets are mounted to. | ||||||
''; | ||||||
|
@@ -181,6 +182,22 @@ in { | |||||
''; | ||||||
}; | ||||||
|
||||||
secretsMountPoint = mkOption { | ||||||
type = types.str; | ||||||
default = "/run/secrets.d"; | ||||||
description = '' | ||||||
The location where the secrets are stored. This is then symlinked to cfg.SymlinkPath (default: /run/secrets) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
''; | ||||||
}; | ||||||
|
||||||
symlinkPath = mkOption { | ||||||
type = types.str; | ||||||
default = "/run/secrets"; | ||||||
description = '' | ||||||
The location where the secrets become available | ||||||
''; | ||||||
}; | ||||||
|
||||||
validateSopsFiles = mkOption { | ||||||
type = types.bool; | ||||||
default = true; | ||||||
|
@@ -206,7 +223,7 @@ in { | |||||
|
||||||
environment = mkOption { | ||||||
type = types.attrsOf (types.either types.str types.path); | ||||||
default = {}; | ||||||
default = { }; | ||||||
description = '' | ||||||
Environment variables to set before calling sops-install-secrets. | ||||||
|
||||||
|
@@ -221,7 +238,7 @@ in { | |||||
|
||||||
package = mkOption { | ||||||
type = types.package; | ||||||
default = (pkgs.callPackage ../.. {}).sops-install-secrets; | ||||||
default = (pkgs.callPackage ../.. { }).sops-install-secrets; | ||||||
defaultText = literalExpression "(pkgs.callPackage ../.. {}).sops-install-secrets"; | ||||||
description = '' | ||||||
sops-install-secrets package to use. | ||||||
|
@@ -232,8 +249,8 @@ in { | |||||
type = types.package; | ||||||
default = | ||||||
if pkgs.stdenv.buildPlatform == pkgs.stdenv.hostPlatform | ||||||
then sops-install-secrets | ||||||
else (pkgs.pkgsBuildHost.callPackage ../.. {}).sops-install-secrets; | ||||||
then sops-install-secrets | ||||||
else (pkgs.pkgsBuildHost.callPackage ../.. { }).sops-install-secrets; | ||||||
defaultText = literalExpression "config.sops.package"; | ||||||
|
||||||
description = '' | ||||||
|
@@ -320,50 +337,55 @@ in { | |||||
(mkRenamedOptionModule [ "sops" "sshKeyPaths" ] [ "sops" "gnupg" "sshKeyPaths" ]) | ||||||
]; | ||||||
config = mkMerge [ | ||||||
(mkIf (cfg.secrets != {}) { | ||||||
(mkIf (cfg.secrets != { }) { | ||||||
assertions = [{ | ||||||
assertion = cfg.gnupg.home != null || cfg.gnupg.sshKeyPaths != [] || cfg.age.keyFile != null || cfg.age.sshKeyPaths != []; | ||||||
assertion = cfg.gnupg.home != null || cfg.gnupg.sshKeyPaths != [ ] || cfg.age.keyFile != null || cfg.age.sshKeyPaths != [ ]; | ||||||
message = "No key source configured for sops. Either set services.openssh.enable or set sops.age.keyFile or sops.gnupg.home"; | ||||||
} { | ||||||
assertion = !(cfg.gnupg.home != null && cfg.gnupg.sshKeyPaths != []); | ||||||
message = "Exactly one of sops.gnupg.home and sops.gnupg.sshKeyPaths must be set"; | ||||||
} { | ||||||
assertion = (filterAttrs (_: v: v.owner != "root" || v.group != "root") secretsForUsers) == {}; | ||||||
message = "neededForUsers cannot be used for secrets that are not root-owned"; | ||||||
}] ++ optionals cfg.validateSopsFiles ( | ||||||
concatLists (mapAttrsToList (name: secret: [{ | ||||||
assertion = builtins.pathExists secret.sopsFile; | ||||||
message = "Cannot find path '${secret.sopsFile}' set in sops.secrets.${strings.escapeNixIdentifier name}.sopsFile"; | ||||||
} { | ||||||
assertion = | ||||||
builtins.isPath secret.sopsFile || | ||||||
(builtins.isString secret.sopsFile && hasPrefix builtins.storeDir secret.sopsFile); | ||||||
message = "'${secret.sopsFile}' is not in the Nix store. Either add it to the Nix store or set sops.validateSopsFiles to false"; | ||||||
}]) cfg.secrets) | ||||||
} | ||||||
{ | ||||||
assertion = !(cfg.gnupg.home != null && cfg.gnupg.sshKeyPaths != [ ]); | ||||||
message = "Exactly one of sops.gnupg.home and sops.gnupg.sshKeyPaths must be set"; | ||||||
} | ||||||
{ | ||||||
assertion = (filterAttrs (_: v: v.owner != "root" || v.group != "root") secretsForUsers) == { }; | ||||||
message = "neededForUsers cannot be used for secrets that are not root-owned"; | ||||||
}] ++ optionals cfg.validateSopsFiles ( | ||||||
concatLists (mapAttrsToList | ||||||
(name: secret: [{ | ||||||
assertion = builtins.pathExists secret.sopsFile; | ||||||
message = "Cannot find path '${secret.sopsFile}' set in sops.secrets.${strings.escapeNixIdentifier name}.sopsFile"; | ||||||
} | ||||||
{ | ||||||
assertion = | ||||||
builtins.isPath secret.sopsFile || | ||||||
(builtins.isString secret.sopsFile && hasPrefix builtins.storeDir secret.sopsFile); | ||||||
message = "'${secret.sopsFile}' is not in the Nix store. Either add it to the Nix store or set sops.validateSopsFiles to false"; | ||||||
}]) | ||||||
cfg.secrets) | ||||||
); | ||||||
|
||||||
sops.environment.SOPS_GPG_EXEC = mkIf (cfg.gnupg.home != null) (mkDefault "${pkgs.gnupg}/bin/gpg"); | ||||||
|
||||||
system.activationScripts = { | ||||||
setupSecretsForUsers = mkIf (secretsForUsers != {}) (stringAfter ([ "specialfs" ] ++ optional cfg.age.generateKey "generate-age-key") '' | ||||||
setupSecretsForUsers = mkIf (secretsForUsers != { }) (stringAfter ([ "specialfs" ] ++ optional cfg.age.generateKey "generate-age-key") '' | ||||||
[ -e /run/current-system ] || echo setting up secrets for users... | ||||||
${withEnvironment "${sops-install-secrets}/bin/sops-install-secrets -ignore-passwd ${manifestForUsers}"} | ||||||
'' // lib.optionalAttrs (config.system ? dryActivationScript) { | ||||||
supportsDryActivation = true; | ||||||
}); | ||||||
|
||||||
users = mkIf (secretsForUsers != {}) { | ||||||
users = mkIf (secretsForUsers != { }) { | ||||||
deps = [ "setupSecretsForUsers" ]; | ||||||
}; | ||||||
|
||||||
setupSecrets = mkIf (regularSecrets != {}) (stringAfter ([ "specialfs" "users" "groups" ] ++ optional cfg.age.generateKey "generate-age-key") '' | ||||||
setupSecrets = mkIf (regularSecrets != { }) (stringAfter ([ "specialfs" "users" "groups" ] ++ optional cfg.age.generateKey "generate-age-key") '' | ||||||
[ -e /run/current-system ] || echo setting up secrets... | ||||||
${withEnvironment "${sops-install-secrets}/bin/sops-install-secrets ${manifest}"} | ||||||
'' // lib.optionalAttrs (config.system ? dryActivationScript) { | ||||||
supportsDryActivation = true; | ||||||
}); | ||||||
|
||||||
generate-age-key = mkIf (cfg.age.generateKey) (stringAfter [] '' | ||||||
generate-age-key = mkIf (cfg.age.generateKey) (stringAfter [ ] '' | ||||||
if [[ ! -f '${cfg.age.keyFile}' ]]; then | ||||||
echo generating machine-specific age key... | ||||||
mkdir -p $(dirname ${cfg.age.keyFile}) | ||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.