Skip to content

Commit

Permalink
fix null-pointer error when artifact-hash is passed but artifact is not
Browse files Browse the repository at this point in the history
  • Loading branch information
dsa0x committed Aug 11, 2022
1 parent 921c478 commit 84d4590
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
11 changes: 9 additions & 2 deletions cmd/rekor-cli/app/pflag_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,14 @@ func validateArtifactPFlags(uuidValid, indexValid bool) error {
return errors.New("either 'entry' or 'artifact' or 'artifact-hash' must be specified")
}

if viper.GetString("artifact-hash") != "" && viper.GetString("artifact") == "" {
return errors.New("'artifact-hash' can only be used with 'artifact'")
}

return nil
}

func CreatePropsFromPflags() *types.ArtifactProperties {
func CreatePropsFromPflags() (*types.ArtifactProperties, error) {
props := &types.ArtifactProperties{}

artifactString := viper.GetString("artifact")
Expand All @@ -138,6 +142,9 @@ func CreatePropsFromPflags() *types.ArtifactProperties {
}

props.ArtifactHash = viper.GetString("artifact-hash")
if props.ArtifactHash != "" && props.ArtifactPath == nil {
return nil, errors.New("'artifact-hash' can only be used with 'artifact'")
}

signatureString := viper.GetString("signature")
if signatureString != "" {
Expand All @@ -163,7 +170,7 @@ func CreatePropsFromPflags() *types.ArtifactProperties {
props.AdditionalAuthenticatedData, _ = base64.StdEncoding.DecodeString(b64aad)
}

return props
return props, nil
}

//TODO: add tests for this
Expand Down
5 changes: 4 additions & 1 deletion cmd/rekor-cli/app/pflags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,10 @@ func TestArtifactPFlags(t *testing.T) {
if err != nil {
t.Errorf("error parsing typeStr: %v", err)
}
props := CreatePropsFromPflags()
props, err := CreatePropsFromPflags()
if err != nil {
t.Errorf("error creating props: %v", err)
}
if _, err := types.NewProposedEntry(context.Background(), typeStr, versionStr, *props); err != nil {
t.Errorf("unexpected result in '%v' building entry: %v", tc.caseDesc, err)
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/rekor-cli/app/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ var uploadCmd = &cobra.Command{
return nil, err
}

props := CreatePropsFromPflags()
props, err := CreatePropsFromPflags()
if err != nil {
return nil, err
}

entry, err = types.NewProposedEntry(context.Background(), typeStr, versionStr, *props)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion cmd/rekor-cli/app/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ var verifyCmd = &cobra.Command{
return nil, err
}

props := CreatePropsFromPflags()
props, err := CreatePropsFromPflags()
if err != nil {
return nil, err
}

entry, err := types.NewProposedEntry(context.Background(), typeStr, versionStr, *props)
if err != nil {
Expand Down

0 comments on commit 84d4590

Please sign in to comment.