|
9 | 9 | let |
10 | 10 | cfg = config.ghaf.security.spire.server; |
11 | 11 | inherit (lib) |
12 | | - escapeShellArg |
13 | 12 | filterAttrs |
14 | 13 | getExe |
15 | 14 | mkIf |
@@ -108,10 +107,16 @@ let |
108 | 107 | echo "Agent ${vm} already registered, skipping token generation" |
109 | 108 | else |
110 | 109 | echo "Generating new token for ${vm}" |
111 | | - token="$(spire-server token generate \ |
112 | | - -socketPath ${socketPath} \ |
113 | | - -spiffeID spiffe://${cfg.trustDomain}/${vm} \ |
114 | | - | awk '/^Token:/ {print $2}')" |
| 110 | + output=$(spire-server token generate -socketPath "${socketPath}" -spiffeID "spiffe://${cfg.trustDomain}/${vm}") |
| 111 | +
|
| 112 | + # Check if the command actually worked |
| 113 | + if [ $? -ne 0 ]; then |
| 114 | + echo "Error: SPIRE token generation failed!" >&2 |
| 115 | + exit 1 |
| 116 | + fi |
| 117 | +
|
| 118 | + # Extract the token from the successful output |
| 119 | + token=$(echo "$output" | awk '/^Token:/ {print $2}') |
115 | 120 |
|
116 | 121 | printf '%s\n' "$token" > "$tokenFile" |
117 | 122 | chmod 0644 "$tokenFile" |
@@ -157,92 +162,15 @@ let |
157 | 162 | ''; |
158 | 163 | }; |
159 | 164 |
|
160 | | - createEntryFunc = '' |
161 | | - create_entry() { |
162 | | - local parentID="$1" |
163 | | - local spiffeID="$2" |
164 | | - local is_node="$3" |
165 | | - shift 3 |
166 | | - local selectors=("$@") |
167 | | -
|
168 | | - if spire-server entry show -socketPath "$SOCKET" -spiffeID "$spiffeID" >/dev/null 2>&1; then |
169 | | - echo "Entry exists: $spiffeID" |
170 | | - return |
171 | | - fi |
172 | | -
|
173 | | - echo "Creating entry: $spiffeID" |
174 | | - local cmd=(spire-server entry create -socketPath "$SOCKET" -spiffeID "$spiffeID") |
175 | | -
|
176 | | - if [ "$is_node" = "true" ]; then |
177 | | - cmd+=(-node) |
178 | | - else |
179 | | - cmd+=(-parentID "$parentID") |
180 | | - fi |
181 | | -
|
182 | | - for s in "''${selectors[@]}"; do |
183 | | - cmd+=(-selector "$s") |
184 | | - done |
185 | | -
|
186 | | - "''${cmd[@]}" |
187 | | - } |
188 | | - ''; |
189 | | - |
190 | | - createEntries = concatMapStringsSep "\n" ( |
191 | | - vmName: |
192 | | - let |
193 | | - agentCfg = config.ghaf.common.spire.agents.${vmName}; |
194 | | - agentSpiffeID = "spiffe://${config.ghaf.common.spire.server.trustDomain}/${vmName}"; |
195 | | - |
196 | | - nodeEntryCmd = |
197 | | - if (agentCfg.nodeAttestationMode == "x509pop") then |
198 | | - '' |
199 | | - create_entry "" ${escapeShellArg agentSpiffeID} "true" "x509pop:subject:cn:${escapeShellArg vmName}" |
200 | | - '' |
201 | | - else |
202 | | - ""; |
203 | | - |
204 | | - workloadCmds = concatMapStringsSep "\n" ( |
205 | | - workload: |
206 | | - let |
207 | | - workloadSpiffeID = "spiffe://${config.ghaf.common.spire.server.trustDomain}/${vmName}/${workload.name}"; |
208 | | - selectors = concatMapStringsSep " " escapeShellArg workload.selectors; |
209 | | - in |
210 | | - '' |
211 | | - create_entry ${escapeShellArg agentSpiffeID} ${escapeShellArg workloadSpiffeID} "false" ${selectors} |
212 | | - '' |
213 | | - ) agentCfg.workloads; |
214 | | - in |
215 | | - nodeEntryCmd + workloadCmds |
216 | | - ) spireAgentVMs; |
217 | | - |
218 | | - spireCreateWorkloadEntriesApp = pkgs.writeShellApplication { |
219 | | - name = "spire-create-workload-entries"; |
220 | | - runtimeInputs = [ |
221 | | - pkgs.coreutils |
222 | | - pkgs.gawk |
223 | | - pkgs.gnugrep |
| 165 | + spireCreateWorkloadEntriesApp = import ./create-workload-entries.nix { |
| 166 | + inherit |
| 167 | + pkgs |
| 168 | + lib |
| 169 | + config |
224 | 170 | spire-package |
225 | | - ]; |
226 | | - text = '' |
227 | | - SOCKET="${socketPath}" |
228 | | - echo "=== SPIRE Workload Entry Creator ===" |
229 | | -
|
230 | | - # Wait for server |
231 | | - echo "Waiting for SPIRE server..." |
232 | | - while true; do |
233 | | - if spire-server healthcheck -socketPath "$SOCKET" >/dev/null 2>&1; then |
234 | | - echo "Server ready" |
235 | | - break |
236 | | - fi |
237 | | - sleep 2 |
238 | | - done |
239 | | -
|
240 | | - ${createEntryFunc} |
241 | | -
|
242 | | - ${createEntries} |
243 | | -
|
244 | | - echo "Done" |
245 | | - ''; |
| 171 | + socketPath |
| 172 | + spireAgentVMs |
| 173 | + ; |
246 | 174 | }; |
247 | 175 | in |
248 | 176 | { |
|
305 | 233 | after = [ "local-fs.target" ]; |
306 | 234 | serviceConfig = { |
307 | 235 | Type = "oneshot"; |
308 | | - User = "root"; |
309 | | - ExecStart = "${pkgs.rsync}/bin/rsync --chown=root:root --chmod=g+rx /etc/givc/ca-cert.pem ${caCertPath}"; |
| 236 | + ExecStart = "${pkgs.rsync}/bin/rsync --chmod=g+rx /etc/givc/ca-cert.pem ${caCertPath}"; |
310 | 237 | Restart = "no"; |
311 | 238 | }; |
312 | 239 | }; |
|
324 | 251 | serviceConfig = { |
325 | 252 | Type = "oneshot"; |
326 | 253 | RemainAfterExit = true; |
327 | | - User = "root"; |
328 | 254 | ExecStart = getExe spireGenerateJoinTokensApp; |
329 | 255 | }; |
330 | 256 | }; |
|
336 | 262 |
|
337 | 263 | serviceConfig = { |
338 | 264 | Type = "oneshot"; |
339 | | - User = "root"; |
340 | 265 | ExecStart = getExe spirePublishBundleApp; |
341 | 266 | }; |
342 | 267 | }; |
|
352 | 277 | serviceConfig = { |
353 | 278 | Type = "oneshot"; |
354 | 279 | RemainAfterExit = true; |
355 | | - User = "root"; |
356 | 280 | ExecStart = getExe spireCreateWorkloadEntriesApp; |
357 | 281 | }; |
358 | 282 | }; |
|
0 commit comments