Skip to content

Commit 86ae009

Browse files
authored
refactor: move attestation initialization server side (#2193)
Signed-off-by: Miguel Martinez <[email protected]>
1 parent 7da19ab commit 86ae009

16 files changed

+574
-434
lines changed

app/cli/cmd/workflow_create.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ func newWorkflowCreateCmd() *cobra.Command {
5151
// Try to find it by name
5252
c, err := action.NewWorkflowContractDescribe(actionOpts).Run(contractRef, 0)
5353
if err != nil || c == nil {
54+
_, err := action.LoadFileOrURL(contractRef)
55+
if err != nil {
56+
return fmt.Errorf("%q is not an existing contract name nor references a valid contract file or URL", contractRef)
57+
}
58+
5459
createResp, err := action.NewWorkflowContractCreate(actionOpts).Run(fmt.Sprintf("%s-%s", project, workflowName), nil, contractRef)
5560
if err != nil {
5661
return err

app/cli/internal/action/attestation_init.go

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ import (
2828
clientAPI "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/api/attestation/v1"
2929
"github.com/chainloop-dev/chainloop/pkg/policies"
3030
"github.com/rs/zerolog"
31-
"google.golang.org/grpc/codes"
32-
"google.golang.org/grpc/status"
3331
)
3432

3533
type AttestationInitOpts struct {
@@ -102,39 +100,24 @@ func (action *AttestationInit) Run(ctx context.Context, opts *AttestationInitRun
102100
action.Logger.Debug().Msg("Retrieving attestation definition")
103101
client := pb.NewAttestationServiceClient(action.ActionsOpts.CPConnection)
104102

105-
// 0 - find or create the contract if we are creating the workflow (if any)
106-
contractRef := opts.NewWorkflowContractRef
107-
_, err := NewWorkflowDescribe(action.ActionsOpts).Run(ctx, opts.WorkflowName, opts.ProjectName)
108-
if err != nil && status.Code(err) == codes.NotFound {
109-
// Not found, let's see if we need to create the contract
110-
if contractRef != "" {
111-
// Try to find it by name
112-
_, err := NewWorkflowContractDescribe(action.ActionsOpts).Run(contractRef, 0)
113-
// An invalid argument might be raised if we use a file or URL in the "name" field, which must be DNS-1123
114-
// TODO: validate locally before doing the query
115-
if err != nil && (status.Code(err) == codes.NotFound || status.Code(err) == codes.InvalidArgument) {
116-
// Check if it is a valid file or URL before trying to create it
117-
_, err := loadFileOrURL(contractRef)
118-
if err != nil {
119-
return "", fmt.Errorf("%q is not an existing contract name nor references a valid contract file or URL", contractRef)
120-
}
121-
122-
createResp, err := NewWorkflowContractCreate(action.ActionsOpts).Run(fmt.Sprintf("%s-%s", opts.ProjectName, opts.WorkflowName), nil, contractRef)
123-
if err != nil {
124-
return "", err
125-
}
126-
127-
contractRef = createResp.Name
128-
}
103+
req := &pb.FindOrCreateWorkflowRequest{
104+
ProjectName: opts.ProjectName,
105+
WorkflowName: opts.WorkflowName,
106+
}
107+
108+
// contractRef can be either the name of an existing contract or a file or URL of a contract to be created or updated
109+
// we'll try to figure out which one of those cases we are dealing with
110+
if opts.NewWorkflowContractRef != "" {
111+
raw, err := LoadFileOrURL(opts.NewWorkflowContractRef)
112+
if err != nil {
113+
req.ContractName = opts.NewWorkflowContractRef
114+
} else {
115+
req.ContractBytes = raw
129116
}
130117
}
131118

132119
// 1 - Find or create the workflow
133-
workflowsResp, err := client.FindOrCreateWorkflow(ctx, &pb.FindOrCreateWorkflowRequest{
134-
ProjectName: opts.ProjectName,
135-
WorkflowName: opts.WorkflowName,
136-
ContractName: contractRef,
137-
})
120+
workflowsResp, err := client.FindOrCreateWorkflow(ctx, req)
138121
if err != nil {
139122
return "", err
140123
}

app/cli/internal/action/plugin_actions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ func (action *PluginInstall) Run(_ context.Context, opts *PluginInstallOptions)
191191
}
192192
defer tempFile.Close()
193193

194-
rawFileContents, err := loadFileOrURL(opts.File)
194+
rawFileContents, err := LoadFileOrURL(opts.File)
195195
if err != nil {
196196
return nil, fmt.Errorf("failed to load file from a given URL: %w", err)
197197
}

app/cli/internal/action/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ import (
2424
"strings"
2525
)
2626

27-
// loadFileOrURL loads a file from a local path or a URL
28-
func loadFileOrURL(fileRef string) ([]byte, error) {
27+
// LoadFileOrURL loads a file from a local path or a URL
28+
func LoadFileOrURL(fileRef string) ([]byte, error) {
2929
parts := strings.SplitAfterN(fileRef, "://", 2)
3030
if len(parts) == 2 {
3131
scheme := parts[0]

app/cli/internal/action/workflow_contract_create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (action *WorkflowContractCreate) Run(name string, description *string, cont
3737
}
3838

3939
if contractPath != "" {
40-
rawContract, err := loadFileOrURL(contractPath)
40+
rawContract, err := LoadFileOrURL(contractPath)
4141
if err != nil {
4242
action.cfg.Logger.Debug().Err(err).Msg("loading the contract")
4343
return nil, err

app/cli/internal/action/workflow_contract_update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (action *WorkflowContractUpdate) Run(name string, description *string, cont
3434

3535
request := &pb.WorkflowContractServiceUpdateRequest{Name: name, Description: description}
3636
if contractPath != "" {
37-
rawContract, err := loadFileOrURL(contractPath)
37+
rawContract, err := LoadFileOrURL(contractPath)
3838
if err != nil {
3939
action.cfg.Logger.Debug().Err(err).Msg("loading the contract")
4040
return nil, err

0 commit comments

Comments
 (0)