Skip to content

Commit

Permalink
simplify func params
Browse files Browse the repository at this point in the history
Signed-off-by: Meredith Lancaster <[email protected]>
  • Loading branch information
malancas committed Dec 19, 2024
1 parent 5402e20 commit ce6150d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
12 changes: 6 additions & 6 deletions pkg/cmd/attestation/verify/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func newEnforcementCriteria(opts *Options) (verification.EnforcementCriteria, er
signedRepoRegex := expandToGitHubURLRegex(opts.Tenant, opts.SignerRepo)
c.SANRegex = signedRepoRegex
} else if opts.SignerWorkflow != "" {
validatedWorkflowRegex, err := validateSignerWorkflow(opts)
validatedWorkflowRegex, err := validateSignerWorkflow(opts.Hostname, opts.SignerWorkflow)
if err != nil {
return verification.EnforcementCriteria{}, err
}
Expand Down Expand Up @@ -140,23 +140,23 @@ func buildSigstoreVerifyPolicy(c verification.EnforcementCriteria, a artifact.Di
return policy, nil
}

func validateSignerWorkflow(opts *Options) (string, error) {
func validateSignerWorkflow(hostname, signerWorkflow string) (string, error) {
// we expect a provided workflow argument be in the format [HOST/]/<OWNER>/<REPO>/path/to/workflow.yml
// if the provided workflow does not contain a host, set the host
match, err := regexp.MatchString(hostRegex, opts.SignerWorkflow)
match, err := regexp.MatchString(hostRegex, signerWorkflow)
if err != nil {
return "", err
}

if match {
return fmt.Sprintf("^https://%s", opts.SignerWorkflow), nil
return fmt.Sprintf("^https://%s", signerWorkflow), nil
}

// if the provided workflow did not match the expect format
// we move onto creating a signer workflow using the provided host name
if opts.Hostname == "" {
if hostname == "" {
return "", errors.New("unknown host")
}

return fmt.Sprintf("^https://%s/%s", opts.Hostname, opts.SignerWorkflow), nil
return fmt.Sprintf("^https://%s/%s", hostname, signerWorkflow), nil
}
9 changes: 1 addition & 8 deletions pkg/cmd/attestation/verify/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/cli/cli/v2/pkg/cmd/attestation/verification"
"github.com/cli/cli/v2/pkg/cmd/factory"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -263,14 +262,8 @@ func TestValidateSignerWorkflow(t *testing.T) {
}

for _, tc := range testcases {
opts := &Options{
Config: factory.New("test").Config,
SignerWorkflow: tc.providedSignerWorkflow,
}

// All host resolution is done verify.go:RunE
opts.Hostname = tc.host
workflowRegex, err := validateSignerWorkflow(opts)
workflowRegex, err := validateSignerWorkflow(tc.host, tc.providedSignerWorkflow)
require.Equal(t, tc.expectedWorkflowRegex, workflowRegex)

if tc.expectErr {
Expand Down

0 comments on commit ce6150d

Please sign in to comment.