Skip to content

Commit

Permalink
Merge pull request #176 from Victorblsilveira/refactoring
Browse files Browse the repository at this point in the history
Refactoring - Naming, spacments and some TODOs
  • Loading branch information
Victorblsilveira authored May 31, 2023
2 parents 1d808f7 + c8d6c7a commit fec14ce
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 99 deletions.
1 change: 1 addition & 0 deletions cmd/server/util/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func (c *galadrielAdminClient) CreateRelationship(ctx context.Context, rel *enti

return relationship, nil
}

func (c *galadrielAdminClient) GetRelationships(ctx context.Context, consentStatus api.ConsentStatus, trustDomainName api.TrustDomainName) (*entity.Relationship, error) {
payload := &admin.GetRelationshipsParams{Status: &consentStatus, TrustDomainName: &trustDomainName}

Expand Down
8 changes: 4 additions & 4 deletions pkg/common/telemetry/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ const (
// Endpoints represents functionality related to agent/server endpoints.
Endpoints = "endpoints"

// FederadBundlesSyncer represents the Federated Bundles Syncer subsystem.
FederadBundlesSyncer = "federated_bundles_syncer"
// FederatedBundlesSynchronizer represents the Federated Bundles Synchronizer subsystem.
FederatedBundlesSynchronizer = "federated_bundles_synchronizer"

// GaladrielServer represents the Galadriel server subsystem.
GaladrielServer = "galadriel_server"

// Network represents a network name ("tcp", "udp").
Network = "network"

// SpireBundleSyncer represents the SPIRE Bundle Syncer subsystem.
SpireBundleSyncer = "spire_bundle_syncer"
// SpireBundleSynchronizer represents the SPIRE Bundle Synchronizer subsystem.
SpireBundleSynchronizer = "spire_bundle_synchronizer"

// SubsystemName represents a field for some subsystem name, such as an API or module.
SubsystemName = "subsystem_name"
Expand Down
30 changes: 15 additions & 15 deletions pkg/harvester/bundlemanager/fedbundles.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"google.golang.org/grpc/codes"
)

// FederatedSyncer is responsible for periodically syncing the federated bundles in SPIRE Server
// FederatedBundlesSynchronizer is responsible for periodically syncing the federated bundles in SPIRE Server
// with the Federated bundles fetched from Galadriel Server.
// This process involves:
// 1. Fetching the federated bundles from Galadriel Server.
Expand All @@ -28,7 +28,7 @@ import (
// 4. Deleting the bundles from SPIRE Server for relationships that no longer exist.
// The deletion of bundles in SPIRE Server is done using the DISSOCIATE mode deletes the federated bundles
// dissociating the registration entries from the federated trust domain.
type FederatedSyncer struct {
type FederatedBundlesSynchronizer struct {
spireClient spireclient.Client
galadrielClient galadrielclient.Client
bundleVerifiers []integrity.Verifier
Expand All @@ -39,17 +39,17 @@ type FederatedSyncer struct {
lastFederatesBundlesDigests map[spiffeid.TrustDomain][]byte
}

// FederatedSyncerConfig holds the configuration for FederatedSyncer.
type FederatedSyncerConfig struct {
// FederatedBundlesSynchronizerConfig holds the configuration for FederatedBundlesSynchronizer.
type FederatedBundlesSynchronizerConfig struct {
SpireClient spireclient.Client
GaladrielClient galadrielclient.Client
BundleVerifiers []integrity.Verifier
SyncInterval time.Duration
Logger logrus.FieldLogger
}

func NewFederatedSyncer(config *FederatedSyncerConfig) *FederatedSyncer {
return &FederatedSyncer{
func NewFederatedBundlesSynchronizer(config *FederatedBundlesSynchronizerConfig) *FederatedBundlesSynchronizer {
return &FederatedBundlesSynchronizer{
spireClient: config.SpireClient,
galadrielClient: config.GaladrielClient,
bundleVerifiers: config.BundleVerifiers,
Expand All @@ -59,8 +59,8 @@ func NewFederatedSyncer(config *FederatedSyncerConfig) *FederatedSyncer {
}

// Run starts the synchronization process.
func (s *FederatedSyncer) Run(ctx context.Context) error {
s.logger.Info("Federated Bundles Syncer started")
func (s *FederatedBundlesSynchronizer) Run(ctx context.Context) error {
s.logger.Info("Federated Bundles Synchronizer started")

ticker := time.NewTicker(s.syncInterval)
defer ticker.Stop()
Expand All @@ -72,13 +72,13 @@ func (s *FederatedSyncer) Run(ctx context.Context) error {
s.logger.Errorf("Failed to sync federated bundles with Galadriel Server: %v", err)
}
case <-ctx.Done():
s.logger.Info("Federated Bundles Syncer stopped")
s.logger.Info("Federated Bundles Synchronizer stopped")
return nil
}
}
}

func (s *FederatedSyncer) syncFederatedBundles(ctx context.Context) error {
func (s *FederatedBundlesSynchronizer) syncFederatedBundles(ctx context.Context) error {
s.logger.Debug("Syncing federated bundles with Galadriel Server")

spireCtx, spireCancel := context.WithTimeout(ctx, spireCallTimeout)
Expand Down Expand Up @@ -153,7 +153,7 @@ func (s *FederatedSyncer) syncFederatedBundles(ctx context.Context) error {
}

// getTrustDomainsToDelete returns a slice of trust domains to delete based on the provided bundles and digests map.
func (s *FederatedSyncer) getTrustDomainsToDelete(bundles []*entity.Bundle, digests map[spiffeid.TrustDomain][]byte) []spiffeid.TrustDomain {
func (s *FederatedBundlesSynchronizer) getTrustDomainsToDelete(bundles []*entity.Bundle, digests map[spiffeid.TrustDomain][]byte) []spiffeid.TrustDomain {
trustDomainsToDelete := make([]spiffeid.TrustDomain, 0)
for _, b := range bundles {
if _, ok := digests[b.TrustDomainName]; !ok {
Expand All @@ -166,7 +166,7 @@ func (s *FederatedSyncer) getTrustDomainsToDelete(bundles []*entity.Bundle, dige

// verifyBundle verifies the bundle using the given verifiers.
// If one of the verifiers can verify the bundle, it returns nil.
func (s *FederatedSyncer) verifyBundle(bundle *entity.Bundle) error {
func (s *FederatedBundlesSynchronizer) verifyBundle(bundle *entity.Bundle) error {
var certChain []*x509.Certificate
if len(bundle.SigningCertificate) > 0 {
var err error
Expand All @@ -187,7 +187,7 @@ func (s *FederatedSyncer) verifyBundle(bundle *entity.Bundle) error {
return fmt.Errorf("no verifier could verify the bundle")
}

func (s *FederatedSyncer) fetchSPIREFederatedBundles(ctx context.Context) ([]*entity.Bundle, error) {
func (s *FederatedBundlesSynchronizer) fetchSPIREFederatedBundles(ctx context.Context) ([]*entity.Bundle, error) {
bundles, err := s.spireClient.GetFederatedBundles(ctx)
if err != nil {
return nil, err
Expand All @@ -205,7 +205,7 @@ func (s *FederatedSyncer) fetchSPIREFederatedBundles(ctx context.Context) ([]*en
return entBundles, nil
}

func (s *FederatedSyncer) logFederatedBundleSetStatuses(federatedBundleStatuses []*spireclient.BatchSetFederatedBundleStatus) {
func (s *FederatedBundlesSynchronizer) logFederatedBundleSetStatuses(federatedBundleStatuses []*spireclient.BatchSetFederatedBundleStatus) {
for _, status := range federatedBundleStatuses {
if status.Status.Code != codes.OK {
s.logger.WithFields(logrus.Fields{
Expand All @@ -218,7 +218,7 @@ func (s *FederatedSyncer) logFederatedBundleSetStatuses(federatedBundleStatuses
}
}

func (s *FederatedSyncer) logFederatedBundleDeleteStatuses(deleteStatuses []*spireclient.BatchDeleteFederatedBundleStatus) {
func (s *FederatedBundlesSynchronizer) logFederatedBundleDeleteStatuses(deleteStatuses []*spireclient.BatchDeleteFederatedBundleStatus) {
for _, status := range deleteStatuses {
if status.Status.Code != codes.OK {
s.logger.WithFields(logrus.Fields{
Expand Down
20 changes: 10 additions & 10 deletions pkg/harvester/bundlemanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const (

// BundleManager is responsible for managing the synchronization and watching of bundles.
type BundleManager struct {
federatedBundlesSyncer *FederatedSyncer
spireBundleSyncer *SpireBundleSyncer
federatedBundlesSynchronizer *FederatedBundlesSynchronizer
spireBundleSynchronizer *SpireBundleSynchronizer
}

// Config holds the configuration for BundleManager.
Expand All @@ -50,33 +50,33 @@ func NewBundleManager(c *Config) *BundleManager {
c.SpireBundlePollInterval = defaultSpireBundlesPollInterval
}

spireBundleSync := NewSpireSyncer(&SpireSyncerConfig{
spireBundleSync := NewSpireSynchronizer(&SpireSynchronizerConfig{
GaladrielClient: c.GaladrielClient,
SpireClient: c.SpireClient,
BundleSigner: c.BundleSigner,
SyncInterval: c.SpireBundlePollInterval,
Logger: c.Logger.WithField(telemetry.SubsystemName, telemetry.SpireBundleSyncer),
Logger: c.Logger.WithField(telemetry.SubsystemName, telemetry.SpireBundleSynchronizer),
})

fedBundlesSync := NewFederatedSyncer(&FederatedSyncerConfig{
fedBundlesSync := NewFederatedBundlesSynchronizer(&FederatedBundlesSynchronizerConfig{
GaladrielClient: c.GaladrielClient,
SpireClient: c.SpireClient,
BundleVerifiers: c.BundleVerifiers,
SyncInterval: c.FederatedBundlesPollInterval,
Logger: c.Logger.WithField(telemetry.SubsystemName, telemetry.FederadBundlesSyncer),
Logger: c.Logger.WithField(telemetry.SubsystemName, telemetry.FederatedBundlesSynchronizer),
})

return &BundleManager{
federatedBundlesSyncer: fedBundlesSync,
spireBundleSyncer: spireBundleSync,
federatedBundlesSynchronizer: fedBundlesSync,
spireBundleSynchronizer: spireBundleSync,
}
}

// Run runs the bundle synchronization processes.
func (bm *BundleManager) Run(ctx context.Context) error {
tasks := []func(ctx context.Context) error{
bm.federatedBundlesSyncer.Run,
bm.spireBundleSyncer.Run,
bm.federatedBundlesSynchronizer.Run,
bm.spireBundleSynchronizer.Run,
}

err := util.RunTasks(ctx, tasks...)
Expand Down
26 changes: 13 additions & 13 deletions pkg/harvester/bundlemanager/spirebundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
"github.com/spiffe/go-spiffe/v2/bundle/spiffebundle"
)

// SpireBundleSyncer is responsible for periodically fetching the bundle from the SPIRE Server,
// SpireBundleSynchronizer is responsible for periodically fetching the bundle from the SPIRE Server,
// signing it, and uploading it to the Galadriel Server.
type SpireBundleSyncer struct {
type SpireBundleSynchronizer struct {
spireClient spireclient.Client
galadrielClient galadrielclient.Client
bundleSigner integrity.Signer
Expand All @@ -26,18 +26,18 @@ type SpireBundleSyncer struct {
lastSpireBundle *spiffebundle.Bundle // last bundle fetched from the SPIRE Server and uploaded to the Galadriel Server
}

// SpireSyncerConfig holds the configuration for SpireBundleSyncer.
type SpireSyncerConfig struct {
// SpireSynchronizerConfig holds the configuration for SpireBundleSynchronizer.
type SpireSynchronizerConfig struct {
SpireClient spireclient.Client
GaladrielClient galadrielclient.Client
BundleSigner integrity.Signer
SyncInterval time.Duration
Logger logrus.FieldLogger
}

// NewSpireSyncer creates a new SpireBundleSyncer instance.
func NewSpireSyncer(config *SpireSyncerConfig) *SpireBundleSyncer {
return &SpireBundleSyncer{
// NewSpireSynchronizer creates a new SpireBundleSynchronizer instance.
func NewSpireSynchronizer(config *SpireSynchronizerConfig) *SpireBundleSynchronizer {
return &SpireBundleSynchronizer{
spireClient: config.SpireClient,
galadrielClient: config.GaladrielClient,
bundleSigner: config.BundleSigner,
Expand All @@ -46,9 +46,9 @@ func NewSpireSyncer(config *SpireSyncerConfig) *SpireBundleSyncer {
}
}

// Run starts the SPIRE bundle syncer process.
func (s *SpireBundleSyncer) Run(ctx context.Context) error {
s.logger.Info("SPIRE Bundle Syncer started")
// Run starts the SPIRE bundle Synchronizer process.
func (s *SpireBundleSynchronizer) Run(ctx context.Context) error {
s.logger.Info("SPIRE Bundle Synchronizer started")

ticker := time.NewTicker(s.syncInterval)
defer ticker.Stop()
Expand All @@ -61,14 +61,14 @@ func (s *SpireBundleSyncer) Run(ctx context.Context) error {
s.logger.Errorf("Failed to sync SPIRE bundle: %v", err)
}
case <-ctx.Done():
s.logger.Info("SPIRE Bundle Syncer stopped")
s.logger.Info("SPIRE Bundle Synchronizer stopped")
return nil
}
}
}

// syncSPIREBundle periodically checks the SPIRE Server for a new bundle, signs it, and uploads the signed bundle to the Galadriel Server.
func (s *SpireBundleSyncer) syncSPIREBundle(ctx context.Context) error {
func (s *SpireBundleSynchronizer) syncSPIREBundle(ctx context.Context) error {
s.logger.Debug("Checking SPIRE Server for a new bundle")

spireCtx, spireCancel := context.WithTimeout(ctx, spireCallTimeout)
Expand Down Expand Up @@ -115,7 +115,7 @@ func (s *SpireBundleSyncer) syncSPIREBundle(ctx context.Context) error {

// generateBundleToUpload creates an entity.Bundle using the provided SPIRE bundle.
// It marshals the SPIRE bundle, generates the bundle signature, and calculates the digest.
func (s *SpireBundleSyncer) generateBundleToUpload(spireBundle *spiffebundle.Bundle) (*entity.Bundle, error) {
func (s *SpireBundleSynchronizer) generateBundleToUpload(spireBundle *spiffebundle.Bundle) (*entity.Bundle, error) {
bundleBytes, err := spireBundle.Marshal()
if err != nil {
return nil, fmt.Errorf("failed to marshal SPIRE bundle: %w", err)
Expand Down
Loading

0 comments on commit fec14ce

Please sign in to comment.