git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/utils: Handle arbitrary secrets in JSON output files

Introduce new functions which allows modules to define options where, if the input is an attrset and the output is JSON, the user can define arbitrary secrets.

commit64358cb0e9dce1d2032e4feaae02f8f59d892b9d authortalyz <kim.lindberger@gmail.com> dateFri 6 Sep 2019, 16:38 (7 years ago) parentb351454
1 file changed+112
nixos/lib/utils.nix+112
nixos/lib/utils.nix+112
@@ -24,4 +24,116 @@ rec {
2424 throw "${shell} is not a shell package"
2525 else
2626 shell;
27+
28+ /* Recurse into a list or an attrset, searching for attrs named like
29+ the value of the "attr" parameter, and return an attrset where the
30+ names are the corresponding jq path where the attrs were found and
31+ the values are the values of the attrs.
32+
33+ Example:
34+ recursiveGetAttrWithJqPrefix {
35+ example = [
36+ {
37+ irrelevant = "not interesting";
38+ }
39+ {
40+ ignored = "ignored attr";
41+ relevant = {
42+ secret = {
43+ _secret = "/path/to/secret";
44+ };
45+ };
46+ }
47+ ];
48+ } "_secret" -> { ".example[1].relevant.secret" = "/path/to/secret"; }
49+ */
50+ recursiveGetAttrWithJqPrefix = item: attr:
51+ let
52+ recurse = prefix: item:
53+ if item ? ${attr} then
54+ nameValuePair prefix item.${attr}
55+ else if isAttrs item then
56+ map (name: recurse (prefix + "." + name) item.${name}) (attrNames item)
57+ else if isList item then
58+ imap0 (index: item: recurse (prefix + "[${toString index}]") item) item
59+ else
60+ [];
61+ in listToAttrs (flatten (recurse "" item));
62+
63+ /* Takes an attrset and a file path and generates a bash snippet that
64+ outputs a JSON file at the file path with all instances of
65+
66+ { _secret = "/path/to/secret" }
67+
68+ in the attrset replaced with the contents of the file
69+ "/path/to/secret" in the output JSON.
70+
71+ When a configuration option accepts an attrset that is finally
72+ converted to JSON, this makes it possible to let the user define
73+ arbitrary secret values.
74+
75+ Example:
76+ If the file "/path/to/secret" contains the string
77+ "topsecretpassword1234",
78+
79+ genJqSecretsReplacementSnippet {
80+ example = [
81+ {
82+ irrelevant = "not interesting";
83+ }
84+ {
85+ ignored = "ignored attr";
86+ relevant = {
87+ secret = {
88+ _secret = "/path/to/secret";
89+ };
90+ };
91+ }
92+ ];
93+ } "/path/to/output.json"
94+
95+ would generate a snippet that, when run, outputs the following
96+ JSON file at "/path/to/output.json":
97+
98+ {
99+ "example": [
100+ {
101+ "irrelevant": "not interesting"
102+ },
103+ {
104+ "ignored": "ignored attr",
105+ "relevant": {
106+ "secret": "topsecretpassword1234"
107+ }
108+ }
109+ ]
110+ }
111+ */
112+ genJqSecretsReplacementSnippet = genJqSecretsReplacementSnippet' "_secret";
113+
114+ # Like genJqSecretsReplacementSnippet, but allows the name of the
115+ # attr which identifies the secret to be changed.
116+ genJqSecretsReplacementSnippet' = attr: set: output:
117+ let
118+ secrets = recursiveGetAttrWithJqPrefix set attr;
119+ in ''
120+ if [[ -h '${output}' ]]; then
121+ rm '${output}'
122+ fi
123+ ''
124+ + concatStringsSep
125+ "\n"
126+ (imap1 (index: name: "export secret${toString index}=$(<'${secrets.${name}}')")
127+ (attrNames secrets))
128+ + "\n"
129+ + "${pkgs.jq}/bin/jq >'${output}' '"
130+ + concatStringsSep
131+ " | "
132+ (imap1 (index: name: ''${name} = $ENV.secret${toString index}'')
133+ (attrNames secrets))
134+ + ''
135+ ' <<'EOF'
136+ ${builtins.toJSON set}
137+ EOF
138+ '';
27139}