Skip to content

Commit be2b8a4

Browse files
committed
Add container restart after trust installation
Added STEP_CA_TRUST_RESTART environment variable support. Updated docker-gen template to handle container restart option. Updated documentation with new functionality and examples. Enables applications requiring restart to properly load new trust certificates when runtime certificate store updates are insufficient for proper SSL/TLS functionality.
1 parent e831714 commit be2b8a4

File tree

3 files changed

+77
-4
lines changed

3 files changed

+77
-4
lines changed

README.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ services:
7070
image: alpine:3.18
7171
environment:
7272
- STEP_CA_TRUST=true
73+
- STEP_CA_TRUST_RESTART=true # Restart after certificate installation
7374
command: |
7475
sh -c "
7576
apk add --no-cache curl &&
@@ -436,7 +437,8 @@ graph TB
436437

437438
For containers that need trust certificates:
438439

439-
- `STEP_CA_TRUST`: Set to `true` to install step-ca trust certificate
440+
- `STEP_CA_TRUST`: Set to `true` to install step-ca trust certificate bundle
441+
- `STEP_CA_TRUST_RESTART`: Set to `true` to restart container after certificate installation
440442

441443
## 🔍 Automatic step-ca Discovery
442444

@@ -507,7 +509,32 @@ services:
507509
my-app:
508510
image: nginx:alpine
509511
environment:
510-
STEP_CA_TRUST: "true" # Enables automatic trust certificate installation
512+
STEP_CA_TRUST: "true" # Enables automatic trust certificate installation
513+
STEP_CA_TRUST_RESTART: "true" # Restart container after certificate installation
514+
networks:
515+
- step-ca-network
516+
```
517+
518+
### Certificate Bundle Details
519+
520+
The trust certificate installation now includes both root and intermediate certificates:
521+
522+
- **Root Certificate**: `/home/step/certs/root_ca.crt` from step-ca container
523+
- **Intermediate Certificate**: `/home/step/certs/intermediate_ca.crt` from step-ca container
524+
- **Bundle**: Combined certificate file containing both certificates for complete PKI trust chain
525+
526+
### Container Restart Option
527+
528+
Some applications may require a restart to properly load new trust certificates. Use `STEP_CA_TRUST_RESTART=true` to automatically restart the container after certificate installation:
529+
530+
```yaml
531+
services:
532+
# Application that needs restart after certificate installation
533+
secure-app:
534+
image: myapp:latest
535+
environment:
536+
STEP_CA_TRUST: "true"
537+
STEP_CA_TRUST_RESTART: "true" # Container will be restarted after certificate installation
511538
networks:
512539
- step-ca-network
513540
```
@@ -529,8 +556,9 @@ services:
529556
3. **OS Detection**: Automatically detects container operating system
530557
4. **Certificate Retrieval**: Gets step-ca root and intermediate certificates bundle
531558
5. **Package Installation**: Installs `ca-certificates` package if needed
532-
6. **Trust Installation**: Copies certificate and updates trust store
533-
7. **Verification**: Tests HTTPS connectivity to step-ca
559+
6. **Trust Installation**: Copies certificate bundle and updates trust store
560+
7. **Container Restart**: Optionally restarts container if `STEP_CA_TRUST_RESTART=true`
561+
8. **Verification**: Tests HTTPS connectivity to step-ca
534562

535563
### Example: Microservices with Trust
536564

app/scripts/trust-functions.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,40 @@ verify_trust_installation() {
352352
fi
353353
}
354354

355+
# Restart container after certificate installation
356+
restart_container_after_trust() {
357+
local container_id="$1"
358+
local container_name="$2"
359+
360+
log_trust "Restarting container $container_name after certificate installation..."
361+
362+
# Check if container is still running before restart
363+
if ! is_container_running "$container_id"; then
364+
log_trust "WARNING: Container $container_name is not running, skipping restart"
365+
return 1
366+
fi
367+
368+
# Restart the container
369+
if docker restart "$container_id" >/dev/null 2>&1; then
370+
log_trust "Successfully restarted container $container_name"
371+
372+
# Wait a moment for container to be ready after restart
373+
sleep 2
374+
375+
# Verify container is running after restart
376+
if is_container_running "$container_id"; then
377+
log_trust "Container $container_name is running after restart"
378+
return 0
379+
else
380+
log_trust "WARNING: Container $container_name failed to start after restart"
381+
return 1
382+
fi
383+
else
384+
log_trust "ERROR: Failed to restart container $container_name"
385+
return 1
386+
fi
387+
}
388+
355389
# Process trust installation for containers with STEP_CA_TRUST=true
356390
process_trust_containers() {
357391
log_trust "Processing trust certificate installation for containers..."

app/templates/trust-containers.tmpl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ log "Processing trust certificate for container: {{$container.Name}} (ID: {{$con
2121
install_trust_certificate "{{$container.ID}}" "{{$container.Name}}"
2222
if [ $? -eq 0 ]; then
2323
log "Trust certificate successfully installed in container: {{$container.Name}}"
24+
{{if $container.Env.STEP_CA_TRUST_RESTART}}
25+
log "Container {{$container.Name}} has STEP_CA_TRUST_RESTART=true, restarting..."
26+
restart_container_after_trust "{{$container.ID}}" "{{$container.Name}}"
27+
if [ $? -eq 0 ]; then
28+
log "Container {{$container.Name}} successfully restarted after certificate installation"
29+
else
30+
log "WARNING: Failed to restart container {{$container.Name}} after certificate installation"
31+
fi
32+
{{else}}
33+
log "Container {{$container.Name}} does not require restart (STEP_CA_TRUST_RESTART not set)"
34+
{{end}}
2435
else
2536
log "Failed to install trust certificate in container: {{$container.Name}}"
2637
fi

0 commit comments

Comments
 (0)