Skip to content
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
1 change: 0 additions & 1 deletion app/controlplane/cmd/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 17 additions & 3 deletions app/controlplane/internal/service/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,10 @@ func (s *AttestationService) GetUploadCreds(ctx context.Context, req *cpAPI.Atte
return nil, errors.NotFound("not found", "workflow run not found")
}

if err := checkWorkflowScope(ctx, wRun.Workflow); err != nil {
return nil, err
}

// Apply RBAC on the project
if _, err = s.userHasPermissionOnProject(ctx, robotAccount.OrgID, &cpAPI.IdentityReference{Name: &wRun.Workflow.Project}, authz.PolicyWorkflowRunCreate); err != nil {
return nil, err
Expand Down Expand Up @@ -791,10 +795,20 @@ func (s *AttestationService) findWorkflowFromTokenOrNameOrRunID(ctx context.Cont
return nil, biz.NewErrValidationStr("workflowName or workflowRunId must be provided")
}

if err := checkWorkflowScope(ctx, wf); err != nil {
return nil, err
}

return wf, nil
}

// checkWorkflowScope denies access when the caller's credential is pinned to a workflow other
// than wf.
func checkWorkflowScope(ctx context.Context, wf *biz.Workflow) error {
// Workflow-scoped API tokens may only operate on their own workflow.
if apiToken := entities.CurrentAPIToken(ctx); apiToken != nil && apiToken.WorkflowID != nil {
if wf.ID != *apiToken.WorkflowID {
return nil, errors.Forbidden("forbidden", "API token is scoped to a different workflow")
return errors.Forbidden("forbidden", "API token is scoped to a different workflow")
}
}

Expand All @@ -803,11 +817,11 @@ func (s *AttestationService) findWorkflowFromTokenOrNameOrRunID(ctx context.Cont
// only a populated one identifies a robot account and constrains the caller.
if robotAccount := usercontext.CurrentRobotAccount(ctx); robotAccount != nil && robotAccount.WorkflowID != "" {
if wf.ID.String() != robotAccount.WorkflowID {
return nil, errors.Forbidden("forbidden", "robot account is scoped to a different workflow")
return errors.Forbidden("forbidden", "robot account is scoped to a different workflow")
}
}

return wf, nil
return nil
}

func checkAuthRequirements(attToken *usercontext.RobotAccount, workflowName string) error {
Expand Down
36 changes: 16 additions & 20 deletions app/controlplane/internal/service/attestationstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,11 @@ type AttestationStateService struct {
*service

attestationStateUseCase *biz.AttestationStateUseCase
workflowUseCase *biz.WorkflowUseCase
wrUseCase *biz.WorkflowRunUseCase
}

type NewAttestationStateServiceOpt struct {
AttestationStateUseCase *biz.AttestationStateUseCase
WorkflowUseCase *biz.WorkflowUseCase
WorkflowRunUseCase *biz.WorkflowRunUseCase
Opts []NewOpt
}
Expand All @@ -52,7 +50,6 @@ func NewAttestationStateService(opts *NewAttestationStateServiceOpt) *Attestatio
return &AttestationStateService{
service: newService(opts.Opts...),
attestationStateUseCase: opts.AttestationStateUseCase,
workflowUseCase: opts.WorkflowUseCase,
wrUseCase: opts.WorkflowRunUseCase,
}
}
Expand All @@ -63,7 +60,7 @@ func (s *AttestationStateService) Initialized(ctx context.Context, req *cpAPI.At
return nil, errors.NotFound("not found", "robot account not found")
}

wf, err := s.findWorkflowFromTokenOrRunID(ctx, robotAccount.OrgID, robotAccount.WorkflowID, req.GetWorkflowRunId())
wf, err := s.findWorkflowFromRunID(ctx, robotAccount.OrgID, req.GetWorkflowRunId())
if err != nil {
return nil, handleUseCaseErr(ctx, err, s.log)
}
Expand All @@ -90,7 +87,7 @@ func (s *AttestationStateService) Save(ctx context.Context, req *cpAPI.Attestati
return nil, errors.NotFound("not found", "robot account not found")
}

wf, err := s.findWorkflowFromTokenOrRunID(ctx, robotAccount.OrgID, robotAccount.WorkflowID, req.GetWorkflowRunId())
wf, err := s.findWorkflowFromRunID(ctx, robotAccount.OrgID, req.GetWorkflowRunId())
if err != nil {
return nil, handleUseCaseErr(ctx, err, s.log)
}
Expand Down Expand Up @@ -123,7 +120,7 @@ func (s *AttestationStateService) Read(ctx context.Context, req *cpAPI.Attestati
return nil, errors.NotFound("not found", "robot account not found")
}

wf, err := s.findWorkflowFromTokenOrRunID(ctx, robotAccount.OrgID, robotAccount.WorkflowID, req.GetWorkflowRunId())
wf, err := s.findWorkflowFromRunID(ctx, robotAccount.OrgID, req.GetWorkflowRunId())
if err != nil {
return nil, handleUseCaseErr(ctx, err, s.log)
}
Expand Down Expand Up @@ -157,7 +154,7 @@ func (s *AttestationStateService) Reset(ctx context.Context, req *cpAPI.Attestat
return nil, errors.NotFound("not found", "robot account not found")
}

wf, err := s.findWorkflowFromTokenOrRunID(ctx, robotAccount.OrgID, robotAccount.WorkflowID, req.GetWorkflowRunId())
wf, err := s.findWorkflowFromRunID(ctx, robotAccount.OrgID, req.GetWorkflowRunId())
if err != nil {
return nil, handleUseCaseErr(ctx, err, s.log)
}
Expand Down Expand Up @@ -204,26 +201,25 @@ func encryptionPassphrase(ctx context.Context) (string, error) {
return hex.EncodeToString(hash[:]), nil
}

// Cascade-based way of retrieving the workflow from the robot-account or the run_ID
func (s *AttestationStateService) findWorkflowFromTokenOrRunID(ctx context.Context, orgID string, workflowID, runID string) (*biz.Workflow, error) {
// findWorkflowFromRunID returns the workflow of the run, and denies access when the caller's
// credential is pinned to a different workflow.
func (s *AttestationStateService) findWorkflowFromRunID(ctx context.Context, orgID, runID string) (*biz.Workflow, error) {
if orgID == "" {
return nil, biz.NewErrValidationStr("orgID must be provided")
}

// This is the case of the workflowID encoded in the robot account
if workflowID != "" {
return s.workflowUseCase.FindByIDInOrg(ctx, orgID, workflowID)
if runID == "" {
return nil, biz.NewErrValidationStr("workflowRunId must be provided")
}

// This is the case when the workflow is found by its reference to the run
if runID != "" {
run, err := s.wrUseCase.GetByIDInOrg(ctx, orgID, runID)
if err != nil {
return nil, fmt.Errorf("error retrieving the workflow run: %w", err)
}
run, err := s.wrUseCase.GetByIDInOrg(ctx, orgID, runID)
if err != nil {
return nil, fmt.Errorf("error retrieving the workflow run: %w", err)
}

return run.Workflow, nil
if err := checkWorkflowScope(ctx, run.Workflow); err != nil {
return nil, err
}

return nil, biz.NewErrValidationStr("workflowRunId must be provided")
return run.Workflow, nil
}
Loading
Loading