Skip to content

Commit

Permalink
Refactors util files.
Browse files Browse the repository at this point in the history
Signed-off-by: JU4N98 <juanpablocabana2@gmail.com>
  • Loading branch information
JU4N98 committed Nov 10, 2023
1 parent 86c00af commit 083b39f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 89 deletions.
52 changes: 52 additions & 0 deletions pkg/sidecar/util_common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package sidecar

import (
"context"
"sync"

"github.com/spiffe/go-spiffe/v2/workloadapi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// RunDaemon starts the main loop
// Starts the workload API client to listen for new SVID updates
// When a new SVID is received on the updateChan, the SVID certificates
// are stored in disk and a restart signal is sent to the proxy's process
func (s *Sidecar) RunDaemon(ctx context.Context) error {
var wg sync.WaitGroup

if s.config.SvidFileName != "" && s.config.SvidKeyFileName != "" && s.config.SvidBundleFileName != "" {
wg.Add(1)
go func() {
defer wg.Done()
err := workloadapi.WatchX509Context(ctx, &x509Watcher{sidecar: s}, s.getWorkloadAPIAdress())
if err != nil && status.Code(err) != codes.Canceled {
s.config.Log.Errorf("Error watching X.509 context: %v", err)
}
}()
}

if s.config.JWTBundleFilename != "" {
wg.Add(1)
go func() {
defer wg.Done()
err := workloadapi.WatchJWTBundles(ctx, &JWTBundlesWatcher{sidecar: s}, s.getWorkloadAPIAdress())
if err != nil && status.Code(err) != codes.Canceled {
s.config.Log.Errorf("Error watching JWT bundle updates: %v", err)
}
}()
}

if s.config.JWTSvidFilename != "" && s.config.JWTAudience != "" {
wg.Add(1)
go func() {
defer wg.Done()
s.updateJWTSVID(ctx, s.getWorkloadAPIAdress())
}()
}

wg.Wait()

return nil
}
47 changes: 2 additions & 45 deletions pkg/sidecar/util_posix.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,14 @@
package sidecar

import (
"context"
"fmt"
"sync"

"github.com/spiffe/go-spiffe/v2/workloadapi"
"golang.org/x/sys/unix"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// RunDaemon starts the main loop
// Starts the workload API client to listen for new SVID updates
// When a new SVID is received on the updateChan, the SVID certificates
// are stored in disk and a restart signal is sent to the proxy's process
func (s *Sidecar) RunDaemon(ctx context.Context) error {
var wg sync.WaitGroup
socket := "unix://" + s.config.AgentAddress

if s.config.SvidFileName != "" && s.config.SvidKeyFileName != "" && s.config.SvidBundleFileName != "" {
wg.Add(1)
go func() {
defer wg.Done()
err := workloadapi.WatchX509Context(ctx, &x509Watcher{sidecar: s}, workloadapi.WithAddr(socket))
if err != nil && status.Code(err) != codes.Canceled {
s.config.Log.Errorf("Error watching X.509 context: %v", err)
}
}()
}

if s.config.JWTBundleFilename != "" {
wg.Add(1)
go func() {
defer wg.Done()
err := workloadapi.WatchJWTBundles(ctx, &JWTBundlesWatcher{sidecar: s}, workloadapi.WithAddr(socket))
if err != nil && status.Code(err) != codes.Canceled {
s.config.Log.Errorf("Error watching JWT bundle updates: %v", err)
}
}()
}

if s.config.JWTSvidFilename != "" && s.config.JWTAudience != "" {
wg.Add(1)
go func() {
defer wg.Done()
s.updateJWTSVID(ctx, workloadapi.WithAddr(socket))
}()
}

wg.Wait()

return nil
func (s *Sidecar) getWorkloadAPIAdress() workloadapi.ClientOption {
return workloadapi.WithAddr("unix://" + s.config.AgentAddress)
}

func (s *Sidecar) SignalProcess() error {
Expand Down
46 changes: 2 additions & 44 deletions pkg/sidecar/util_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,13 @@
package sidecar

import (
"context"
"errors"
"sync"

"github.com/spiffe/go-spiffe/v2/workloadapi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// RunDaemon starts the main loop
// Starts the workload API client to listen for new SVID updates
// When a new SVID is received on the updateChan, the SVID certificates
// are stored in disk and a restart signal is sent to the proxy's process
func (s *Sidecar) RunDaemon(ctx context.Context) error {
var wg sync.WaitGroup

if s.config.SvidFileName != "" && s.config.SvidKeyFileName != "" && s.config.SvidBundleFileName != "" {
wg.add(1)
go func() {
defer wg.Done()
err := workloadapi.WatchX509Context(ctx, &x509Watcher{sidecar: s}, workloadapi.WithNamedPipeName(s.config.AgentAddress))
if err != nil && status.Code(err) != codes.Canceled {
s.config.Log.Errorf("Error watching X.509 context: %v", err)
}
}()
}

if s.config.JWTBundleFilename != "" {
wg.Add(1)
go func() {
defer wg.Done()
err := workloadapi.WatchJWTBundles(ctx, &JWTBundlesWatcher{sidecar: s}, workloadapi.WithNamedPipeName(s.config.AgentAddress))
if err != nil && status.Code(err) != codes.Canceled {
s.config.Log.Errorf("Error watching JWT bundle updates: %v", err)
}
}()
}

if s.config.JWTSvidFilename != "" && s.config.JWTAudience != "" {
wg.Add(1)
go func() {
defer wg.Done()
s.updateJWTSVID(ctx, workloadapi.WithNamedPipeName(s.config.AgentAddress))
}()
}

wg.Wait()

return nil
func (s *Sidecar) getWorkloadAPIAdress() workloadapi.ClientOption {
return workloadapi.WithNamedPipeName(s.config.AgentAddress)
}

func (s *Sidecar) SignalProcess() error {
Expand Down

0 comments on commit 083b39f

Please sign in to comment.