|
103 | 103 | # don't exist in the array given. That's why we use here `POST`, and
|
104 | 104 | # only if s.override == true then we DELETE the relevant folders
|
105 | 105 | # afterwards.
|
106 |
| - (map (new_cfg: '' |
107 |
| - curl -d ${lib.escapeShellArg (builtins.toJSON new_cfg)} -X POST ${s.baseAddress} |
108 |
| - '')) |
| 106 | + (map (new_cfg: |
| 107 | + let |
| 108 | + isSecret = attr: value: builtins.isString value && attr == "encryptionPassword"; |
| 109 | + |
| 110 | + resolveSecrets = attr: value: |
| 111 | + if builtins.isAttrs value then |
| 112 | + # Attribute set: process each attribute |
| 113 | + builtins.mapAttrs (name: val: resolveSecrets name val) value |
| 114 | + else if builtins.isList value then |
| 115 | + # List: process each element |
| 116 | + map (item: resolveSecrets "" item) value |
| 117 | + else if isSecret attr value then |
| 118 | + # String that looks like a path: replace with placeholder |
| 119 | + let |
| 120 | + varName = "secret_${builtins.hashString "sha256" value}"; |
| 121 | + in |
| 122 | + "\${${varName}}" |
| 123 | + else |
| 124 | + # Other types: return as is |
| 125 | + value; |
| 126 | + |
| 127 | + # Function to collect all file paths from the configuration |
| 128 | + collectPaths = attr: value: |
| 129 | + if builtins.isAttrs value then |
| 130 | + concatMap (name: collectPaths name value.${name}) (builtins.attrNames value) |
| 131 | + else if builtins.isList value then |
| 132 | + concatMap (name: collectPaths "" name) value |
| 133 | + else if isSecret attr value then |
| 134 | + [ value ] |
| 135 | + else |
| 136 | + []; |
| 137 | + |
| 138 | + # Function to generate variable assignments for the secrets |
| 139 | + generateSecretVars = paths: |
| 140 | + concatStringsSep "\n" (map (path: |
| 141 | + let |
| 142 | + varName = "secret_${builtins.hashString "sha256" path}"; |
| 143 | + in |
| 144 | + '' |
| 145 | + if [ ! -r ${path} ]; then |
| 146 | + echo "${path} does not exist" |
| 147 | + exit 1 |
| 148 | + fi |
| 149 | + ${varName}=$(<${path}) |
| 150 | + '' |
| 151 | + ) paths); |
| 152 | + |
| 153 | + resolved_cfg = resolveSecrets "" new_cfg; |
| 154 | + secretPaths = collectPaths "" new_cfg; |
| 155 | + secretVarsScript = generateSecretVars secretPaths; |
| 156 | + |
| 157 | + jsonString = builtins.toJSON resolved_cfg; |
| 158 | + escapedJson = builtins.replaceStrings ["\""] ["\\\""] jsonString; |
| 159 | + in |
| 160 | + '' |
| 161 | + ${secretVarsScript} |
| 162 | +
|
| 163 | + curl -d "${escapedJson}" -X POST ${s.baseAddress} |
| 164 | + '' |
| 165 | + )) |
109 | 166 | (lib.concatStringsSep "\n")
|
110 | 167 | ]
|
111 | 168 | /* If we need to override devices/folders, we iterate all currently configured
|
|
0 commit comments