Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions cmd/comidCreate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 Contributors to the Veraison project.
// Copyright 2021-2025 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0

package cmd
Expand All @@ -10,11 +10,14 @@ import (
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/veraison/corim/comid"
"github.com/veraison/corim/corim"
"github.com/veraison/eat"
)

var (
comidCreateFiles []string
comidCreateDirs []string
comidCreateProfile string
comidCreateOutputDir string
)

Expand All @@ -27,11 +30,13 @@ func NewComidCreateCmd() *cobra.Command {
Long: `create one or more CBOR-encoded CoMID(s) from the supplied JSON template(s)

Create CoMIDs from templates t1.json and t2.json, plus any template found in
the templates/ directory. Save them to the current working directory.
the templates/ directory, with an optional supplied profile.
Save them to the current working directory.

cocli comid create --template=t1.json \
--template=t2.json \
--template-dir=templates
--template-dir=templates \
--profile="tag:arm.com,2024:cca_platform#1.1.0"

Create one CoMID from template t3.json and save it to the comids/ directory.
Note that the output directory must exist.
Expand Down Expand Up @@ -78,6 +83,10 @@ func NewComidCreateCmd() *cobra.Command {
&comidCreateDirs, "template-dir", "T", []string{}, "a directory containing CoMID template files",
)

cmd.Flags().StringVarP(
&comidCreateProfile, "profile", "p", "", "an optional scheme specific profile applicable to all CoMID JSON templates",
)

cmd.Flags().StringVarP(
&comidCreateOutputDir, "output-dir", "o", ".", "directory where the created files are stored",
)
Expand All @@ -96,15 +105,24 @@ func templateToCBOR(tmplFile, outputDir string) (string, error) {
var (
tmplData, cborData []byte
cborFile string
c comid.Comid
c *comid.Comid
p *eat.Profile
err error
)

if tmplData, err = afero.ReadFile(fs, tmplFile); err != nil {
return "", fmt.Errorf("error loading template from %s: %w", tmplFile, err)
}

if err = c.FromJSON(tmplData); err != nil {
if comidCreateProfile != "" {
p, err = eat.NewProfile(comidCreateProfile)
if err != nil {
return "", fmt.Errorf("error creating profile %q for template: %w", comidCreateProfile, err)
}
}

c, err = corim.UnmarshalComidFromJSON(tmplData, p)
if err != nil {
return "", fmt.Errorf("error decoding template from %s: %w", tmplFile, err)
}

Expand Down
45 changes: 44 additions & 1 deletion cmd/comidCreate_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 Contributors to the Veraison project.
// Copyright 2021-2025 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0

package cmd
Expand All @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/veraison/corim/comid"
"github.com/veraison/corim/profiles/tdx"
)

func Test_ComidCreateCmd_unknown_argument(t *testing.T) {
Expand Down Expand Up @@ -126,3 +127,45 @@ func Test_ComidCreateCmd_template_from_dir_to_custom_dir(t *testing.T) {
_, err = fs.Stat(expectedFileName)
assert.NoError(t, err)
}

func Test_ComidCreateCmd_WithProfile(t *testing.T) {
var err error
profile := "--profile=" + testProfile
cmd := NewComidCreateCmd()
fs = afero.NewMemMapFs()
err = afero.WriteFile(fs, "ok.json", []byte(tdx.TDXSeamRefValJSONTemplate), 0644)
require.NoError(t, err)

args := []string{
"--template=ok.json",
profile,
}
cmd.SetArgs(args)

err = cmd.Execute()
assert.NoError(t, err)

expectedFileName := "ok.cbor"

_, err = fs.Stat(expectedFileName)
assert.NoError(t, err)

}

func Test_ComidCreateCmd_InvalidProfile(t *testing.T) {
var err error
profile := "--profile=" + testInvalidProfile
cmd := NewComidCreateCmd()
fs = afero.NewMemMapFs()
err = afero.WriteFile(fs, "ok.json", []byte(tdx.TDXSeamRefValJSONTemplate), 0644)
require.NoError(t, err)

args := []string{
"--template=ok.json",
profile,
}
cmd.SetArgs(args)

err = cmd.Execute()
assert.EqualError(t, err, "1/1 creations(s) failed")
}
21 changes: 17 additions & 4 deletions cmd/comidDisplay.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 Contributors to the Veraison project.
// Copyright 2021-2025 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0

package cmd
Expand All @@ -9,11 +9,13 @@ import (

"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/veraison/eat"
)

var (
comidDisplayFiles []string
comidDisplayDirs []string
comidDisplayFiles []string
comidDisplayDirs []string
comidDisplayProfile string
)

var comidDisplayCmd = NewComidDisplayCmd()
Expand Down Expand Up @@ -65,6 +67,10 @@ func NewComidDisplayCmd() *cobra.Command {
&comidDisplayFiles, "file", "f", []string{}, "a CoMID file (in CBOR format)",
)

cmd.Flags().StringVarP(
&comidDisplayProfile, "profile", "p", "", "an optional, scheme-specific profile applicable to all CoMID files",
)

cmd.Flags().StringArrayVarP(
&comidDisplayDirs, "dir", "d", []string{}, "a directory containing CoMID files (in CBOR format)",
)
Expand All @@ -75,15 +81,22 @@ func NewComidDisplayCmd() *cobra.Command {
func displayComidFile(file string) error {
var (
data []byte
p *eat.Profile
err error
)

if data, err = afero.ReadFile(fs, file); err != nil {
return fmt.Errorf("error loading CoMID from %s: %w", file, err)
}

if comidDisplayProfile != "" {
p, err = eat.NewProfile(comidDisplayProfile)
if err != nil {
return fmt.Errorf("error creating profile %q from template: %w", comidDisplayProfile, err)
}
}
// use file name as heading
return printComid(data, ">> ["+file+"]")
return printComid(data, p, ">> ["+file+"]")
}

func checkComidDisplayArgs() error {
Expand Down
22 changes: 21 additions & 1 deletion cmd/comidDisplay_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Contributors to the Veraison project.
// Copyright 2021-2025 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0

package cmd
Expand All @@ -10,6 +10,7 @@ import (
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/veraison/corim/profiles/tdx"
)

func Test_ComidDisplayCmd_unknown_argument(t *testing.T) {
Expand Down Expand Up @@ -99,3 +100,22 @@ func Test_ComidDisplayCmd_file_with_valid_comid_from_dir(t *testing.T) {
err = cmd.Execute()
assert.NoError(t, err)
}

func Test_ComidDisplayCmd_With_profile_with_valid_comid(t *testing.T) {
var err error
profile := "--profile=" + testProfile
cmd := NewComidDisplayCmd()

fs = afero.NewMemMapFs()
err = afero.WriteFile(fs, "ok.cbor", []byte(tdx.ComidSeamRefVal), 0644)
require.NoError(t, err)

args := []string{
"--file=ok.cbor",
profile,
}
cmd.SetArgs(args)

err = cmd.Execute()
assert.NoError(t, err)
}
26 changes: 20 additions & 6 deletions cmd/comidValidate.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 Contributors to the Veraison project.
// Copyright 2021-2025 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0

package cmd
Expand All @@ -9,12 +9,14 @@ import (

"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/veraison/corim/comid"
"github.com/veraison/corim/corim"
"github.com/veraison/eat"
)

var (
comidValidateFiles []string
comidValidateDirs []string
comidValidateFiles []string
comidValidateDirs []string
comidValidateProfile string
)

var comidValidateCmd = NewComidValidateCmd()
Expand Down Expand Up @@ -67,6 +69,10 @@ func NewComidValidateCmd() *cobra.Command {
&comidValidateFiles, "file", "f", []string{}, "a CoMID file (in CBOR format)",
)

cmd.Flags().StringVarP(
&comidValidateProfile, "profile", "p", "", "an optional, scheme-specific profile applicable to all CoMID files",
)

cmd.Flags().StringArrayVarP(
&comidValidateDirs, "dir", "d", []string{}, "a directory containing CoMID files (in CBOR format)",
)
Expand All @@ -78,14 +84,22 @@ func validateComid(file string) error {
var (
data []byte
err error
c comid.Comid
p *eat.Profile
)

if data, err = afero.ReadFile(fs, file); err != nil {
return fmt.Errorf("error loading CoMID from %s: %w", file, err)
}

if err = c.FromCBOR(data); err != nil {
if comidValidateProfile != "" {
p, err = eat.NewProfile(comidValidateProfile)
if err != nil {
return fmt.Errorf("error creating profile %q for CoMID: %w", comidValidateProfile, err)
}
}

c, err := corim.UnmarshalComidFromCBOR(data, p)
if err != nil {
return fmt.Errorf("error decoding CoMID from %s: %w", file, err)
}

Expand Down
25 changes: 24 additions & 1 deletion cmd/comidValidate_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright 2021 Contributors to the Veraison project.
// Copyright 2021-2025 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0

package cmd

import (
"fmt"
"testing"

"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/veraison/corim/profiles/tdx"
)

func Test_ComidValidateCmd_unknown_argument(t *testing.T) {
Expand Down Expand Up @@ -114,3 +116,24 @@ func Test_ComidValidateCmd_file_with_valid_comid_from_dir(t *testing.T) {
err = cmd.Execute()
assert.NoError(t, err)
}

func Test_ComidValidateCmd_with_valid_comid(t *testing.T) {
var err error
profile := "--profile=" + testProfile
cmd := NewComidValidateCmd()

fs = afero.NewMemMapFs()
err = afero.WriteFile(fs, "ok.cbor", []byte(tdx.ComidSeamRefVal), 0644)
require.NoError(t, err)

args := []string{
"--file=ok.cbor",
profile,
}
cmd.SetArgs(args)

fmt.Printf("%x\n", []byte(tdx.ComidSeamRefVal))

err = cmd.Execute()
assert.NoError(t, err)
}
29 changes: 25 additions & 4 deletions cmd/common.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2024 Contributors to the Veraison project.
// Copyright 2021-2025 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0

package cmd
Expand All @@ -10,8 +10,9 @@ import (
"strings"

"github.com/spf13/afero"
"github.com/veraison/corim/comid"
"github.com/veraison/corim/corim"
"github.com/veraison/corim/cots"
"github.com/veraison/eat"
"github.com/veraison/swid"
)

Expand Down Expand Up @@ -67,8 +68,28 @@ func printJSONFromCBOR(fcl FromCBORLoader, cbor []byte, heading string) error {
return nil
}

func printComid(cbor []byte, heading string) error {
return printJSONFromCBOR(&comid.Comid{}, cbor, heading)
func printComidWithExtensions(cbor []byte, profile *eat.Profile, heading string) error {
var (
err error
j []byte
)
c, err := corim.UnmarshalComidFromCBOR(cbor, profile)
if err != nil {
return fmt.Errorf("error decoding CoMID from CBOR: %w", err)
}

indent := " "
if j, err = json.MarshalIndent(c, "", indent); err != nil {
return fmt.Errorf("JSON encoding failed: %w", err)
}

fmt.Println(heading)
fmt.Println(string(j))
return nil
}

func printComid(cbor []byte, profile *eat.Profile, heading string) error {
return printComidWithExtensions(cbor, profile, heading)
}

func printCoswid(cbor []byte, heading string) error {
Expand Down
Loading
Loading