Skip to content

Commit

Permalink
feat: auto-create workflow during attestation (chainloop-dev#1416)
Browse files Browse the repository at this point in the history
Signed-off-by: Miguel Martinez <[email protected]>
  • Loading branch information
migmartri authored Oct 21, 2024
1 parent ecaf8c2 commit 84729f1
Show file tree
Hide file tree
Showing 13 changed files with 1,025 additions and 499 deletions.
14 changes: 8 additions & 6 deletions app/cli/cmd/attestation_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ import (

func newAttestationInitCmd() *cobra.Command {
var (
force bool
contractRevision int
attestationDryRun bool
workflowName string
projectName string
force bool
contractRevision int
attestationDryRun bool
workflowName string
projectName string
newWorkflowcontractName string
)

cmd := &cobra.Command{
Expand Down Expand Up @@ -59,7 +60,7 @@ func newAttestationInitCmd() *cobra.Command {
}

// Initialize it
attestationID, err := a.Run(cmd.Context(), contractRevision, projectName, workflowName)
attestationID, err := a.Run(cmd.Context(), contractRevision, projectName, workflowName, newWorkflowcontractName)
if err != nil {
if errors.Is(err, action.ErrAttestationAlreadyExist) {
return err
Expand Down Expand Up @@ -110,6 +111,7 @@ func newAttestationInitCmd() *cobra.Command {

cmd.Flags().StringVar(&projectName, "project", "", "name of the project of this workflow")
cobra.CheckErr(cmd.MarkFlagRequired("project"))
cmd.Flags().StringVar(&newWorkflowcontractName, "contract", "", "name of an existing contract to attach it to the auto-created workflow (it doesn't update an existing one)")

return cmd
}
20 changes: 14 additions & 6 deletions app/cli/internal/action/attestation_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func NewAttestationInit(cfg *AttestationInitOpts) (*AttestationInit, error) {
}

// returns the attestation ID
func (action *AttestationInit) Run(ctx context.Context, contractRevision int, projectName, workflowName string) (string, error) {
func (action *AttestationInit) Run(ctx context.Context, contractRevision int, projectName, workflowName, newWorkflowContractName string) (string, error) {
if action.dryRun && action.useRemoteState {
return "", errors.New("remote state is not compatible with dry-run mode")
}
Expand All @@ -85,7 +85,18 @@ func (action *AttestationInit) Run(ctx context.Context, contractRevision int, pr

action.Logger.Debug().Msg("Retrieving attestation definition")
client := pb.NewAttestationServiceClient(action.ActionsOpts.CPConnection)
// get information of the workflow
// 1 - Find or create the workflow
workflowsResp, err := client.FindOrCreateWorkflow(ctx, &pb.FindOrCreateWorkflowRequest{
ProjectName: projectName,
WorkflowName: workflowName,
ContractName: newWorkflowContractName,
})
if err != nil {
return "", err
}
workflow := workflowsResp.GetResult()

// 2 - Get contract
contractResp, err := client.GetContract(ctx, &pb.AttestationServiceGetContractRequest{
ContractRevision: int32(contractRevision),
WorkflowName: workflowName,
Expand All @@ -95,10 +106,7 @@ func (action *AttestationInit) Run(ctx context.Context, contractRevision int, pr
return "", err
}

workflow := contractResp.GetResult().GetWorkflow()
contractVersion := contractResp.Result.GetContract()
contract := contractResp.GetResult().GetContract().GetV1()

workflowMeta := &clientAPI.WorkflowMetadata{
WorkflowId: workflow.GetId(),
Name: workflow.GetName(),
Expand All @@ -110,7 +118,7 @@ func (action *AttestationInit) Run(ctx context.Context, contractRevision int, pr
action.Logger.Debug().Msg("workflow contract and metadata retrieved from the control plane")

// Auto discover the runner context and enforce against the one in the contract if needed
discoveredRunner, err := crafter.DiscoverAndEnforceRunner(contract.GetRunner().GetType(), action.dryRun, action.Logger)
discoveredRunner, err := crafter.DiscoverAndEnforceRunner(contractVersion.GetV1().GetRunner().GetType(), action.dryRun, action.Logger)
if err != nil {
return "", ErrRunnerContextNotFound{err.Error()}
}
Expand Down
Loading

0 comments on commit 84729f1

Please sign in to comment.