Skip to content

Commit

Permalink
add option to add build meta data
Browse files Browse the repository at this point in the history
  • Loading branch information
sujankota committed Jan 8, 2025
1 parent ac4073d commit e0ba076
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion sdk/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Manifest struct {
EncryptionInformation `json:"encryptionInformation"`
Payload `json:"payload"`
Assertions []Assertion `json:"assertions,omitempty"`
TDFVersion string `json:"tdf_spec_version,omitempty"`
TDFVersion string `json:"schemaVersion,omitempty"`
}

type attributeObject struct {
Expand Down
11 changes: 9 additions & 2 deletions sdk/tdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,17 @@ func (s SDK) prepareManifest(ctx context.Context, t *TDFObject, tdfConfig TDFCon

version, err := semver.NewVersion(sdkVersion)
if err != nil {
return fmt.Errorf("ReadVersion failed:%w", err)
return fmt.Errorf("failed to parse semantic version %q: %w", sdkVersion, err)
}

manifest.TDFVersion = version.String()
// Only attempt to set metadata if it's provided
if metadata := strings.TrimSpace(tdfConfig.buildMetadata); metadata != "" {
versionWithMetadata, err := version.SetMetadata(metadata)
if err != nil {
return fmt.Errorf("failed to set build metadata %q: %w", metadata, err)
}
manifest.TDFVersion = versionWithMetadata.String()
}

if len(tdfConfig.splitPlan) == 0 && len(tdfConfig.kasInfoList) == 0 {
return fmt.Errorf("%w: no key access template specified or inferred", errInvalidKasInfo)
Expand Down
9 changes: 9 additions & 0 deletions sdk/tdf_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type TDFConfig struct {
tdfPrivateKey string
metaData string
mimeType string
buildMetadata string
integrityAlgorithm IntegrityAlgorithm
segmentIntegrityAlgorithm IntegrityAlgorithm
assertions []AssertionConfig
Expand Down Expand Up @@ -172,6 +173,14 @@ func WithMetaData(metaData string) TDFOption {
}
}

// WithBuildMetadata returns an Option that add build metadata to TDF.
func WithBuildMetadata(buildMetadata string) TDFOption {
return func(c *TDFConfig) error {
c.buildMetadata = buildMetadata
return nil
}
}

func WithMimeType(mimeType string) TDFOption {
return func(c *TDFConfig) error {
c.mimeType = mimeType
Expand Down
10 changes: 9 additions & 1 deletion sdk/tdf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"
"time"

"github.com/Masterminds/semver/v3"
"github.com/lestrrat-go/jwx/v2/jwt"
"github.com/opentdf/platform/lib/ocrypto"
kaspb "github.com/opentdf/platform/protocol/go/kas"
Expand Down Expand Up @@ -259,6 +260,7 @@ func TestTDF(t *testing.T) {

func (s *TDFSuite) Test_SimpleTDF() {
metaData := []byte(`{"displayName" : "openTDF go sdk"}`)
buildMetadata := "test.01"
attributes := []string{
"https://example.com/attr/Classification/value/S",
"https://example.com/attr/Classification/value/X",
Expand Down Expand Up @@ -290,13 +292,14 @@ func (s *TDFSuite) Test_SimpleTDF() {
WithKasInformation(kasURLs...),
WithMetaData(string(metaData)),
WithDataAttributes(attributes...),
WithBuildMetadata(buildMetadata),
)

s.Require().NoError(err)
s.InDelta(float64(expectedTdfSize), float64(tdfObj.size), 32.0)
}

// test meta data
// test meta data and build meta data
{
readSeeker, err := os.Open(tdfFilename)
s.Require().NoError(err)
Expand All @@ -323,6 +326,11 @@ func (s *TDFSuite) Test_SimpleTDF() {
payloadKey, err := r.UnsafePayloadKeyRetrieval()
s.Require().NoError(err)
s.Len(payloadKey, kKeySize)

version, err := semver.NewVersion(r.manifest.TDFVersion)
s.Require().NoError(err)

s.EqualValues(buildMetadata, version.Metadata())
}

// test reader
Expand Down

0 comments on commit e0ba076

Please sign in to comment.