Skip to content

Commit

Permalink
Add support for signaling an external process via pid file
Browse files Browse the repository at this point in the history
Signed-off-by: Kevin Fox <[email protected]>
  • Loading branch information
kfox1111 authored and faisal-memon committed Sep 24, 2024
1 parent e2a7dd9 commit 8742213
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 22 deletions.
1 change: 1 addition & 0 deletions cmd/spiffe-helper/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Config struct {
AgentAddressDeprecated string `hcl:"agentAddress"`
Cmd string `hcl:"cmd"`
CmdArgs string `hcl:"cmd_args"`
PidFileName string `hcl:"pid_file_name"`
CmdArgsDeprecated string `hcl:"cmdArgs"`
CertDir string `hcl:"cert_dir"`
CertDirDeprecated string `hcl:"certDir"`
Expand Down
3 changes: 3 additions & 0 deletions pkg/sidecar/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type Config struct {
// The arguments of the process to launch.
CmdArgs string

// Signal extral process via PID file
PidFileName string

// The directory name to store the x509s and/or JWTs.
CertDir string

Expand Down
68 changes: 46 additions & 22 deletions pkg/sidecar/sidecar.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package sidecar

import (
"bytes"
"context"
"encoding/csv"
"fmt"
"os"
"os/exec"
"path"

Check failure on line 10 in pkg/sidecar/sidecar.go

View workflow job for this annotation

GitHub Actions / build (linux)

"path" imported and not used

Check failure on line 10 in pkg/sidecar/sidecar.go

View workflow job for this annotation

GitHub Actions / lint (linux)

"path" imported and not used) (typecheck)

Check failure on line 10 in pkg/sidecar/sidecar.go

View workflow job for this annotation

GitHub Actions / lint (linux)

"path" imported and not used) (typecheck)

Check failure on line 10 in pkg/sidecar/sidecar.go

View workflow job for this annotation

GitHub Actions / lint (linux)

"path" imported and not used (typecheck)

Check failure on line 10 in pkg/sidecar/sidecar.go

View workflow job for this annotation

GitHub Actions / unit-test (ubuntu-22.04)

"path" imported and not used

Check failure on line 10 in pkg/sidecar/sidecar.go

View workflow job for this annotation

GitHub Actions / unit-test (macos-latest)

"path" imported and not used
"strconv"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -170,10 +173,12 @@ func (s *Sidecar) updateCertificates(svidResponse *workloadapi.X509Context) {
}
s.config.Log.Info("X.509 certificates updated")

if s.config.Cmd != "" {
if err := s.signalProcess(); err != nil {
s.config.Log.WithError(err).Error("Unable to signal process")
}
if err := s.signalProcess(); err != nil {
s.config.Log.WithError(err).Error("Unable to signal process")
}

if s.config.ExitWhenReady {
os.Exit(0)
}

select {
Expand All @@ -185,27 +190,46 @@ func (s *Sidecar) updateCertificates(svidResponse *workloadapi.X509Context) {
// signalProcess sends the configured Renew signal to the process running the proxy
// to reload itself so that the proxy uses the new SVID
func (s *Sidecar) signalProcess() (err error) {
if s.config.PidFileName != "" {
byts, err := os.ReadFile(s.config.PidFileName)
if err != nil {
return fmt.Errorf("failed to read pid file: %s\n%w", s.config.PidFileName, err)
}
pid, err := strconv.Atoi(string(bytes.TrimSpace(byts)))
if err != nil {
return fmt.Errorf("failed to parse pid file: %s\n%w", s.config.PidFileName, err)
}
s.process, err = os.FindProcess(pid)
if err != nil {
return fmt.Errorf("failed to find process: %d\n%w", pid, err)
}
if err := s.SignalProcess(); err != nil {
return err
}
}
// TODO: is ReloadExternalProcess still used?
switch s.config.ReloadExternalProcess {
case nil:
if atomic.LoadInt32(&s.processRunning) == 0 {
cmdArgs, err := getCmdArgs(s.config.CmdArgs)
if err != nil {
return fmt.Errorf("error parsing cmd arguments: %w", err)
}

cmd := exec.Command(s.config.Cmd, cmdArgs...) // #nosec
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
return fmt.Errorf("error executing process: %v\n%w", s.config.Cmd, err)
}
s.process = cmd.Process
go s.checkProcessExit()
} else {
if err := s.SignalProcess(); err != nil {
return err
if s.config.Cmd != "" {
if atomic.LoadInt32(&s.processRunning) == 0 {
cmdArgs, err := getCmdArgs(s.config.CmdArgs)
if err != nil {
return fmt.Errorf("error parsing cmd arguments: %w", err)
}

cmd := exec.Command(s.config.Cmd, cmdArgs...) // #nosec
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
return fmt.Errorf("error executing process: %v\n%w", s.config.Cmd, err)
}
s.process = cmd.Process
go s.checkProcessExit()
} else {
if err := s.SignalProcess(); err != nil {
return err
}
}
}

Expand Down

0 comments on commit 8742213

Please sign in to comment.