Skip to content

Commit

Permalink
check artifact path when creating entry
Browse files Browse the repository at this point in the history
Signed-off-by: Samsondeen Dare <samsondeen.dare@hashicorp.com>
  • Loading branch information
dsa0x committed Aug 11, 2022
1 parent 6882d35 commit f1e4d7f
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 21 deletions.
11 changes: 2 additions & 9 deletions cmd/rekor-cli/app/pflag_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,10 @@ 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, error) {
func CreatePropsFromPflags() *types.ArtifactProperties {
props := &types.ArtifactProperties{}

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

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 @@ -170,7 +163,7 @@ func CreatePropsFromPflags() (*types.ArtifactProperties, error) {
props.AdditionalAuthenticatedData, _ = base64.StdEncoding.DecodeString(b64aad)
}

return props, nil
return props
}

//TODO: add tests for this
Expand Down
5 changes: 1 addition & 4 deletions cmd/rekor-cli/app/pflags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,10 +433,7 @@ func TestArtifactPFlags(t *testing.T) {
if err != nil {
t.Errorf("error parsing typeStr: %v", err)
}
props, err := CreatePropsFromPflags()
if err != nil {
t.Errorf("error creating props: %v", err)
}
props := CreatePropsFromPflags()
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: 1 addition & 4 deletions cmd/rekor-cli/app/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,7 @@ var uploadCmd = &cobra.Command{
return nil, err
}

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

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

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

entry, err := types.NewProposedEntry(context.Background(), typeStr, versionStr, *props)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/types/alpine/v0.0.1/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ func (v V001Entry) CreateFromArtifactProperties(ctx context.Context, props types
artifactBytes := props.ArtifactBytes
if artifactBytes == nil {
var artifactReader io.ReadCloser
if props.ArtifactPath == nil {
return nil, errors.New("path to artifact file must be specified")
}
if props.ArtifactPath.IsAbs() {
artifactReader, err = util.FileOrURLReadCloser(ctx, props.ArtifactPath.String(), nil)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/types/helm/v0.0.1/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,9 @@ func (v V001Entry) CreateFromArtifactProperties(ctx context.Context, props types
artifactBytes := props.ArtifactBytes
if artifactBytes == nil {
var artifactReader io.ReadCloser
if props.ArtifactPath == nil {
return nil, errors.New("path to artifact file must be specified")
}
if props.ArtifactPath.IsAbs() {
artifactReader, err = util.FileOrURLReadCloser(ctx, props.ArtifactPath.String(), nil)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/types/jar/v0.0.1/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ func (v *V001Entry) CreateFromArtifactProperties(ctx context.Context, props type
artifactBytes := props.ArtifactBytes
if artifactBytes == nil {
var artifactReader io.ReadCloser
if props.ArtifactPath == nil {
return nil, errors.New("path to artifact file must be specified")
}
if props.ArtifactPath.IsAbs() {
artifactReader, err = util.FileOrURLReadCloser(ctx, props.ArtifactPath.String(), nil)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/types/rekord/v0.0.1/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ func (v V001Entry) CreateFromArtifactProperties(ctx context.Context, props types
artifactBytes := props.ArtifactBytes
if artifactBytes == nil {
var artifactReader io.ReadCloser
if props.ArtifactPath == nil {
return nil, errors.New("path to artifact file must be specified")
}
if props.ArtifactPath.IsAbs() {
artifactReader, err = util.FileOrURLReadCloser(ctx, props.ArtifactPath.String(), nil)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/types/rpm/v0.0.1/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,9 @@ func (v V001Entry) CreateFromArtifactProperties(ctx context.Context, props types
artifactBytes := props.ArtifactBytes
if artifactBytes == nil {
var artifactReader io.ReadCloser
if props.ArtifactPath == nil {
return nil, errors.New("path to artifact file must be specified")
}
if props.ArtifactPath.IsAbs() {
artifactReader, err = util.FileOrURLReadCloser(ctx, props.ArtifactPath.String(), nil)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/types/tuf/v0.0.1/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ func (v V001Entry) CreateFromArtifactProperties(ctx context.Context, props types
re.TufObj.Metadata = &models.TUFV001SchemaMetadata{}
if artifactBytes == nil {
var artifactReader io.ReadCloser
if props.ArtifactPath == nil {
return nil, errors.New("path to artifact file must be specified")
}
if props.ArtifactPath.IsAbs() {
artifactReader, err = util.FileOrURLReadCloser(ctx, props.ArtifactPath.String(), nil)
if err != nil {
Expand Down

0 comments on commit f1e4d7f

Please sign in to comment.