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

Pass secret decrypter to pipedv1 schedulers and planners #5433

Merged
merged 2 commits into from
Dec 18, 2024
Merged
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
9 changes: 8 additions & 1 deletion pkg/app/pipedv1/cmd/piped/piped.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@
// TODO: Implement the drift detector controller.
}

// Initialize secret decrypter.
decrypter, err := p.initializeSecretDecrypter(cfg)
if err != nil {
input.Logger.Error("failed to initialize secret decrypter", zap.Error(err))
return err
}

Check warning on line 379 in pkg/app/pipedv1/cmd/piped/piped.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/cmd/piped/piped.go#L375-L379

Added lines #L375 - L379 were not covered by tests

// Start running deployment controller.
{
c := controller.NewController(
Expand All @@ -380,6 +387,7 @@
deploymentLister,
commandLister,
notifier,
decrypter,

Check warning on line 390 in pkg/app/pipedv1/cmd/piped/piped.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/cmd/piped/piped.go#L390

Added line #L390 was not covered by tests
p.gracePeriod,
input.Logger,
tracerProvider,
Expand Down Expand Up @@ -667,7 +675,6 @@
return plugins, nil
}

// TODO: Remove this once the decryption task by plugin call to the plugin service is implemented.
func (p *piped) initializeSecretDecrypter(cfg *config.PipedSpec) (crypto.Decrypter, error) {
sm := cfg.SecretManagement
if sm == nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/app/pipedv1/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
Notify(event model.NotificationEvent)
}

type secretDecrypter interface {
Decrypt(string) (string, error)
}

type DeploymentController interface {
Run(ctx context.Context) error
}
Expand All @@ -90,6 +94,7 @@
deploymentLister deploymentLister
commandLister commandLister
notifier notifier
secretDecrypter secretDecrypter

// gRPC clients to communicate with plugins.
pluginClients []pluginapi.PluginClient
Expand Down Expand Up @@ -130,6 +135,7 @@
deploymentLister deploymentLister,
commandLister commandLister,
notifier notifier,
secretDecrypter secretDecrypter,
gracePeriod time.Duration,
logger *zap.Logger,
tracerProvider trace.TracerProvider,
Expand All @@ -142,6 +148,7 @@
deploymentLister: deploymentLister,
commandLister: commandLister,
notifier: notifier,
secretDecrypter: secretDecrypter,

Check warning on line 151 in pkg/app/pipedv1/controller/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/controller.go#L151

Added line #L151 was not covered by tests

planners: make(map[string]*planner),
donePlanners: make(map[string]time.Time),
Expand Down Expand Up @@ -443,6 +450,7 @@
c.apiClient,
c.gitClient,
c.notifier,
c.secretDecrypter,

Check warning on line 453 in pkg/app/pipedv1/controller/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/controller.go#L453

Added line #L453 was not covered by tests
c.logger,
c.tracerProvider,
)
Expand Down Expand Up @@ -581,6 +589,7 @@
c.gitClient,
c.stageBasedPluginsMap,
c.notifier,
c.secretDecrypter,

Check warning on line 592 in pkg/app/pipedv1/controller/controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/controller.go#L592

Added line #L592 was not covered by tests
c.logger,
c.tracerProvider,
)
Expand Down
44 changes: 26 additions & 18 deletions pkg/app/pipedv1/controller/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@
notifier notifier
metadataStore metadatastore.MetadataStore

// TODO: Find a way to show log from pluggin's planner
// The secretDecrypter is used to decrypt secrets
// which encrypted using PipeCD built-in secret management.
secretDecrypter secretDecrypter

logger *zap.Logger
tracer trace.Tracer

Expand All @@ -98,6 +101,7 @@
apiClient apiClient,
gitClient gitClient,
notifier notifier,
secretDecrypter secretDecrypter,
logger *zap.Logger,
tracerProvider trace.TracerProvider,
) *planner {
Expand All @@ -121,6 +125,7 @@
gitClient: gitClient,
metadataStore: metadatastore.NewMetadataStore(apiClient, d),
notifier: notifier,
secretDecrypter: secretDecrypter,

Check warning on line 128 in pkg/app/pipedv1/controller/planner.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/planner.go#L128

Added line #L128 was not covered by tests
doneDeploymentStatus: d.Status,
cancelledCh: make(chan *model.ReportableCommand, 1),
nowFunc: time.Now,
Expand Down Expand Up @@ -193,31 +198,34 @@
Branch: p.deployment.GitPath.Repo.Branch,
}

runningDSP := deploysource.NewProvider(
filepath.Join(p.workingDir, "running-deploysource"),
deploysource.NewGitSourceCloner(p.gitClient, repoCfg, "running", p.lastSuccessfulCommitHash),
p.deployment.GetGitPath(), nil, // TODO: pass secret decrypter?
)
rds, err := runningDSP.Get(ctx, io.Discard) // TODO: pass not io.Discard
if err != nil {
// TODO: log error
return fmt.Errorf("error while preparing deploy source data (%v)", err)
}
runningDS = rds.ToPluginDeploySource()

targetDSP := deploysource.NewProvider(
filepath.Join(p.workingDir, "target-deploysource"),
deploysource.NewGitSourceCloner(p.gitClient, repoCfg, "target", p.deployment.Trigger.Commit.Hash),
p.deployment.GetGitPath(), nil, // TODO: pass secret decrypter?
p.deployment.GetGitPath(),
p.secretDecrypter,

Check warning on line 205 in pkg/app/pipedv1/controller/planner.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/planner.go#L204-L205

Added lines #L204 - L205 were not covered by tests
)
tds, err := targetDSP.Get(ctx, io.Discard) // TODO: pass not io.Discard
tds, err := targetDSP.Get(ctx, io.Discard)

Check warning on line 207 in pkg/app/pipedv1/controller/planner.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/planner.go#L207

Added line #L207 was not covered by tests
if err != nil {
// TODO: log error
return fmt.Errorf("error while preparing deploy source data (%v)", err)
p.logger.Error("error while preparing target deploy source data", zap.Error(err))
return err

Check warning on line 210 in pkg/app/pipedv1/controller/planner.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/planner.go#L209-L210

Added lines #L209 - L210 were not covered by tests
}
targetDS = tds.ToPluginDeploySource()

// TODO: Pass running DS as well if need?
if p.lastSuccessfulCommitHash != "" {
runningDSP := deploysource.NewProvider(
filepath.Join(p.workingDir, "running-deploysource"),
deploysource.NewGitSourceCloner(p.gitClient, repoCfg, "running", p.lastSuccessfulCommitHash),
p.deployment.GetGitPath(),
p.secretDecrypter,
)
rds, err := runningDSP.Get(ctx, io.Discard)
if err != nil {
p.logger.Error("error while preparing running deploy source data", zap.Error(err))
return err
}
runningDS = rds.ToPluginDeploySource()

Check warning on line 226 in pkg/app/pipedv1/controller/planner.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/planner.go#L214-L226

Added lines #L214 - L226 were not covered by tests
}

out, err := p.buildPlan(ctx, runningDS, targetDS)

// If the deployment was already cancelled, we ignore the plan result.
Expand Down
42 changes: 24 additions & 18 deletions pkg/app/pipedv1/controller/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@

stageBasedPluginsMap map[string]pluginapi.PluginClient

apiClient apiClient
gitClient gitClient
metadataStore metadatastore.MetadataStore
notifier notifier
apiClient apiClient
gitClient gitClient
metadataStore metadatastore.MetadataStore
notifier notifier
secretDecrypter secretDecrypter

targetDSP deploysource.Provider
runningDSP deploysource.Provider
Expand Down Expand Up @@ -80,6 +81,7 @@
gitClient gitClient,
stageBasedPluginsMap map[string]pluginapi.PluginClient,
notifier notifier,
secretsDecrypter secretDecrypter,
logger *zap.Logger,
tracerProvider trace.TracerProvider,
) *scheduler {
Expand All @@ -99,6 +101,7 @@
gitClient: gitClient,
metadataStore: metadatastore.NewMetadataStore(apiClient, d),
notifier: notifier,
secretDecrypter: secretsDecrypter,

Check warning on line 104 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L104

Added line #L104 was not covered by tests
doneDeploymentStatus: d.Status,
cancelledCh: make(chan *model.ReportableCommand, 1),
logger: logger,
Expand Down Expand Up @@ -165,7 +168,7 @@
}

// Run starts running the scheduler.
// It determines what stage should be executed next by which executor.
// It determines what stage should be executed next by which plugin.
// The returning error does not mean that the pipeline was failed,
// but it means that the scheduler could not finish its job normally.
func (s *scheduler) Run(ctx context.Context) error {
Expand Down Expand Up @@ -193,7 +196,7 @@
}
controllermetrics.UpdateDeploymentStatus(s.deployment, model.DeploymentStatus_DEPLOYMENT_RUNNING)

// notify the deployment started event
// Notify the deployment started event

Check warning on line 199 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L199

Added line #L199 was not covered by tests
users, groups, err := s.getApplicationNotificationMentions(model.NotificationEventType_EVENT_DEPLOYMENT_STARTED)
if err != nil {
s.logger.Error("failed to get the list of users or groups", zap.Error(err))
Expand Down Expand Up @@ -223,16 +226,20 @@
Branch: s.deployment.GitPath.Repo.Branch,
}

s.runningDSP = deploysource.NewProvider(
filepath.Join(s.workingDir, "running-deploysource"),
deploysource.NewGitSourceCloner(s.gitClient, repoCfg, "running", s.deployment.RunningCommitHash),
s.deployment.GetGitPath(), nil, // TODO: pass secret decrypter?
)
if s.deployment.RunningCommitHash != "" {
s.runningDSP = deploysource.NewProvider(
filepath.Join(s.workingDir, "running-deploysource"),
deploysource.NewGitSourceCloner(s.gitClient, repoCfg, "running", s.deployment.RunningCommitHash),
s.deployment.GetGitPath(),
s.secretDecrypter,
)
}

Check warning on line 236 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L229-L236

Added lines #L229 - L236 were not covered by tests

s.targetDSP = deploysource.NewProvider(
filepath.Join(s.workingDir, "target-deploysource"),
deploysource.NewGitSourceCloner(s.gitClient, repoCfg, "target", s.deployment.Trigger.Commit.Hash),
s.deployment.GetGitPath(), nil, // TODO: pass secret decrypter?
s.deployment.GetGitPath(),
s.secretDecrypter,

Check warning on line 242 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L241-L242

Added lines #L241 - L242 were not covered by tests
)

ds, err := s.targetDSP.Get(ctx, io.Discard)
Expand Down Expand Up @@ -469,13 +476,13 @@

rds, err := s.runningDSP.Get(ctx, io.Discard)
if err != nil {
s.logger.Error("failed to get running deployment source", zap.Error(err))
s.logger.Error("failed to get running deployment source", zap.String("stage-name", ps.Name), zap.Error(err))

Check warning on line 479 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L479

Added line #L479 was not covered by tests
return model.StageStatus_STAGE_FAILURE
}

tds, err := s.targetDSP.Get(ctx, io.Discard)
if err != nil {
s.logger.Error("failed to get target deployment source", zap.Error(err))
s.logger.Error("failed to get target deployment source", zap.String("stage-name", ps.Name), zap.Error(err))

Check warning on line 485 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L485

Added line #L485 was not covered by tests
return model.StageStatus_STAGE_FAILURE
}

Expand Down Expand Up @@ -508,16 +515,15 @@
// Find the executor plugin for this stage.
plugin, ok := s.stageBasedPluginsMap[ps.Name]
if !ok {
err := fmt.Errorf("no registered plugin that can perform for stage %s", ps.Name)
s.logger.Error(err.Error())
s.logger.Error("failed to find the plugin for the stage", zap.String("stage-name", ps.Name))
s.reportStageStatus(ctx, ps.Id, model.StageStatus_STAGE_FAILURE, ps.Requires)
return model.StageStatus_STAGE_FAILURE
}

// Load the stage configuration.
stageConfig, stageConfigFound := s.genericApplicationConfig.GetStageByte(ps.Index)
if !stageConfigFound {
s.logger.Error("Unable to find the stage configuration")
s.logger.Error("Unable to find the stage configuration", zap.String("stage-name", ps.Name))

Check warning on line 526 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L526

Added line #L526 was not covered by tests
if err := s.reportStageStatus(ctx, ps.Id, model.StageStatus_STAGE_FAILURE, ps.Requires); err != nil {
s.logger.Error("failed to report stage status", zap.Error(err))
}
Expand All @@ -535,7 +541,7 @@
},
})
if err != nil {
s.logger.Error("failed to execute stage", zap.Error(err))
s.logger.Error("failed to execute stage", zap.String("stage-name", ps.Name), zap.Error(err))

Check warning on line 544 in pkg/app/pipedv1/controller/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/controller/scheduler.go#L544

Added line #L544 was not covered by tests
s.reportStageStatus(ctx, ps.Id, model.StageStatus_STAGE_FAILURE, ps.Requires)
return model.StageStatus_STAGE_FAILURE
}
Expand Down
Loading