Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for signaling an external process via pid file #118

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 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 Expand Up @@ -203,6 +204,7 @@ func NewSidecarConfig(config *Config, log logrus.FieldLogger) *sidecar.Config {
AgentAddress: config.AgentAddress,
Cmd: config.Cmd,
CmdArgs: config.CmdArgs,
PidFileName: config.PidFileName,
CertDir: config.CertDir,
CertFileMode: fs.FileMode(config.CertFileMode), //nolint:gosec,G115
KeyFileMode: fs.FileMode(config.KeyFileMode), //nolint:gosec,G115
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 external process via PID file
PidFileName string

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

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

import (
"bytes"
"context"
"encoding/csv"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -170,10 +172,8 @@ 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")
}

select {
Expand All @@ -185,27 +185,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 != "" {
faisal-memon marked this conversation as resolved.
Show resolved Hide resolved
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
Loading