Skip to content

Commit

Permalink
Merge pull request #268 from tri-adam/get-set-metadata
Browse files Browse the repository at this point in the history
feat: custom metadata support
  • Loading branch information
tri-adam authored Mar 6, 2023
2 parents a29fb45 + 6e2497e commit 5a9464b
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ linters:
- typecheck
- unconvert
- unparam
- unused
# - unused
- whitespace

linters-settings:
Expand Down
4 changes: 2 additions & 2 deletions pkg/sif/create.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved.
// Copyright (c) 2017, SingularityWare, LLC. All rights reserved.
// Copyright (c) 2017, Yannick Cote <yhcote@gmail.com> All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
Expand Down Expand Up @@ -69,7 +69,7 @@ func (f *FileImage) writeDataObject(i int, di DescriptorInput, t time.Time) erro

// If this is a primary partition, verify there isn't another primary partition, and update the
// architecture in the global header.
if p, ok := di.opts.extra.(partition); ok && p.Parttype == PartPrimSys {
if p, ok := di.opts.md.(partition); ok && p.Parttype == PartPrimSys {
if ds, err := f.GetDescriptors(WithPartitionType(PartPrimSys)); err == nil && len(ds) > 0 {
return errPrimaryPartition
}
Expand Down
79 changes: 59 additions & 20 deletions pkg/sif/descriptor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2018-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2018-2023, Sylabs Inc. All rights reserved.
// Copyright (c) 2017, SingularityWare, LLC. All rights reserved.
// Copyright (c) 2017, Yannick Cote <yhcote@gmail.com> All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
Expand All @@ -10,6 +10,7 @@ package sif
import (
"bytes"
"crypto"
"encoding"
"encoding/binary"
"errors"
"fmt"
Expand Down Expand Up @@ -44,6 +45,11 @@ type partition struct {
Arch archType
}

// MarshalBinary encodes p into binary format.
func (p partition) MarshalBinary() ([]byte, error) {
return binaryMarshaler{p}.MarshalBinary()
}

// signature represents the SIF signature data object descriptor.
type signature struct {
Hashtype hashType
Expand All @@ -61,6 +67,26 @@ type sbom struct {
Format SBOMFormat
}

// The binaryMarshaler type is an adapter that allows a type suitable for use with the
// encoding/binary package to be used as an encoding.BinaryMarshaler.
type binaryMarshaler struct{ any }

// MarshalBinary encodes m into binary format.
func (m binaryMarshaler) MarshalBinary() ([]byte, error) {
var b bytes.Buffer
err := binary.Write(&b, binary.LittleEndian, m.any)
return b.Bytes(), err
}

// The binaryUnmarshaler type is an adapter that allows a type suitable for use with the
// encoding/binary package to be used as an encoding.BinaryUnmarshaler.
type binaryUnmarshaler struct{ any }

// UnmarshalBinary decodes b into u.
func (u binaryUnmarshaler) UnmarshalBinary(b []byte) error {
return binary.Read(bytes.NewReader(b), binary.LittleEndian, u.any)
}

var errNameTooLarge = errors.New("name value too large")

// setName encodes name into the name field of d.
Expand All @@ -78,28 +104,33 @@ func (d *rawDescriptor) setName(name string) error {

var errExtraTooLarge = errors.New("extra value too large")

// setExtra encodes v into the extra field of d.
func (d *rawDescriptor) setExtra(v interface{}) error {
if v == nil {
// setExtra marshals metadata from md into the "extra" field of d.
func (d *rawDescriptor) setExtra(md encoding.BinaryMarshaler) error {
if md == nil {
return nil
}

if binary.Size(v) > len(d.Extra) {
return errExtraTooLarge
extra, err := md.MarshalBinary()
if err != nil {
return err
}

b := new(bytes.Buffer)
if err := binary.Write(b, binary.LittleEndian, v); err != nil {
return err
if len(extra) > len(d.Extra) {
return errExtraTooLarge
}

for i := copy(d.Extra[:], b.Bytes()); i < len(d.Extra); i++ {
for i := copy(d.Extra[:], extra); i < len(d.Extra); i++ {
d.Extra[i] = 0
}

return nil
}

// getExtra unmarshals metadata from the "extra" field of d into md.
func (d *rawDescriptor) getExtra(md encoding.BinaryUnmarshaler) error {
return md.UnmarshalBinary(d.Extra[:])
}

// getPartitionMetadata gets metadata for a partition data object.
func (d rawDescriptor) getPartitionMetadata() (FSType, PartType, string, error) {
if got, want := d.DataType, DataPartition; got != want {
Expand All @@ -108,9 +139,8 @@ func (d rawDescriptor) getPartitionMetadata() (FSType, PartType, string, error)

var p partition

b := bytes.NewReader(d.Extra[:])
if err := binary.Read(b, binary.LittleEndian, &p); err != nil {
return 0, 0, "", fmt.Errorf("%w", err)
if err := d.getExtra(binaryUnmarshaler{&p}); err != nil {
return 0, 0, "", err
}

return p.Fstype, p.Parttype, p.Arch.GoArch(), nil
Expand Down Expand Up @@ -168,11 +198,23 @@ func (d Descriptor) ModifiedAt() time.Time { return time.Unix(d.raw.ModifiedAt,
// Name returns the name of the data object.
func (d Descriptor) Name() string { return strings.TrimRight(string(d.raw.Name[:]), "\000") }

// GetMetadata unmarshals metadata from the "extra" field of d into md.
func (d Descriptor) GetMetadata(md encoding.BinaryUnmarshaler) error {
if err := d.raw.getExtra(md); err != nil {
return fmt.Errorf("%w", err)
}
return nil
}

// PartitionMetadata gets metadata for a partition data object.
//
//nolint:nonamedreturns // Named returns effective as documentation.
func (d Descriptor) PartitionMetadata() (fs FSType, pt PartType, arch string, err error) {
return d.raw.getPartitionMetadata()
fs, pt, arch, err = d.raw.getPartitionMetadata()
if err != nil {
return 0, 0, "", fmt.Errorf("%w", err)
}
return fs, pt, arch, err
}

var errHashUnsupported = errors.New("hash algorithm unsupported")
Expand Down Expand Up @@ -204,8 +246,7 @@ func (d Descriptor) SignatureMetadata() (ht crypto.Hash, fp []byte, err error) {

var s signature

b := bytes.NewReader(d.raw.Extra[:])
if err := binary.Read(b, binary.LittleEndian, &s); err != nil {
if err := d.raw.getExtra(binaryUnmarshaler{&s}); err != nil {
return ht, fp, fmt.Errorf("%w", err)
}

Expand All @@ -232,8 +273,7 @@ func (d Descriptor) CryptoMessageMetadata() (FormatType, MessageType, error) {

var m cryptoMessage

b := bytes.NewReader(d.raw.Extra[:])
if err := binary.Read(b, binary.LittleEndian, &m); err != nil {
if err := d.raw.getExtra(binaryUnmarshaler{&m}); err != nil {
return 0, 0, fmt.Errorf("%w", err)
}

Expand All @@ -248,8 +288,7 @@ func (d Descriptor) SBOMMetadata() (SBOMFormat, error) {

var s sbom

b := bytes.NewReader(d.raw.Extra[:])
if err := binary.Read(b, binary.LittleEndian, &s); err != nil {
if err := d.raw.getExtra(binaryUnmarshaler{&s}); err != nil {
return 0, fmt.Errorf("%w", err)
}

Expand Down
26 changes: 18 additions & 8 deletions pkg/sif/descriptor_input.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2021-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand All @@ -7,6 +7,7 @@ package sif

import (
"crypto"
"encoding"
"errors"
"fmt"
"io"
Expand All @@ -19,7 +20,7 @@ type descriptorOpts struct {
linkID uint32
alignment int
name string
extra interface{}
md encoding.BinaryMarshaler
t time.Time
}

Expand Down Expand Up @@ -92,6 +93,14 @@ func OptObjectTime(t time.Time) DescriptorInputOpt {
}
}

// OptMetadata marshals metadata from md into the "extra" field of d.
func OptMetadata(md encoding.BinaryMarshaler) DescriptorInputOpt {
return func(t DataType, opts *descriptorOpts) error {
opts.md = md
return nil
}
}

type unexpectedDataTypeError struct {
got DataType
want []DataType
Expand Down Expand Up @@ -155,7 +164,7 @@ func OptCryptoMessageMetadata(ft FormatType, mt MessageType) DescriptorInputOpt
Messagetype: mt,
}

opts.extra = m
opts.md = binaryMarshaler{m}
return nil
}
}
Expand Down Expand Up @@ -184,7 +193,7 @@ func OptPartitionMetadata(fs FSType, pt PartType, arch string) DescriptorInputOp
Arch: sifarch,
}

opts.extra = p
opts.md = p
return nil
}
}
Expand Down Expand Up @@ -221,7 +230,7 @@ func OptSignatureMetadata(ht crypto.Hash, fp []byte) DescriptorInputOpt {
}
copy(s.Entity[:], fp)

opts.extra = s
opts.md = binaryMarshaler{s}
return nil
}
}
Expand All @@ -239,7 +248,7 @@ func OptSBOMMetadata(f SBOMFormat) DescriptorInputOpt {
Format: f,
}

opts.extra = s
opts.md = binaryMarshaler{s}
return nil
}
}
Expand All @@ -259,7 +268,8 @@ const DefaultObjectGroup = 1
//
// It is possible (and often necessary) to store additional metadata related to certain types of
// data objects. Consider supplying options such as OptCryptoMessageMetadata, OptPartitionMetadata,
// OptSignatureMetadata, and OptSBOMMetadata for this purpose.
// OptSignatureMetadata, and OptSBOMMetadata for this purpose. To set custom metadata, use
// OptMetadata.
//
// By default, the data object will be placed in the default data object group (1). To override
// this behavior, use OptNoGroup or OptGroupID. To link this data object, use OptLinkedID or
Expand Down Expand Up @@ -317,5 +327,5 @@ func (di DescriptorInput) fillDescriptor(t time.Time, d *rawDescriptor) error {
return err
}

return d.setExtra(di.opts.extra)
return d.setExtra(di.opts.md)
}
9 changes: 8 additions & 1 deletion pkg/sif/descriptor_input_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021-2022, Sylabs Inc. All rights reserved.
// Copyright (c) 2021-2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE file distributed with the sources of this project regarding your
// rights to use or distribute this software.
Expand Down Expand Up @@ -118,6 +118,13 @@ func TestNewDescriptorInput(t *testing.T) {
OptObjectTime(time.Unix(946702800, 0)),
},
},
{
name: "OptMetadata",
t: DataGeneric,
opts: []DescriptorInputOpt{
OptMetadata(testMetadata{100}),
},
},
{
name: "OptCryptoMessageMetadataUnexpectedDataType",
t: DataGeneric,
Expand Down
Loading

0 comments on commit 5a9464b

Please sign in to comment.