Skip to content

Commit

Permalink
NOISSUE - Rename backend info to attestation policy (#314)
Browse files Browse the repository at this point in the history
* attestation policy field

Signed-off-by: Sammy Oina <[email protected]>

* fix tests

Signed-off-by: Sammy Oina <[email protected]>

* fmt

Signed-off-by: Sammy Oina <[email protected]>

---------

Signed-off-by: Sammy Oina <[email protected]>
  • Loading branch information
SammyOina authored Nov 21, 2024
1 parent 9c11f4b commit 760c9bb
Show file tree
Hide file tree
Showing 38 changed files with 407 additions and 388 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 2
updates:
- package-ecosystem: "cargo"
directory: "/scripts/backend_info"
directory: "/scripts/attestation_policy"
schedule:
interval: "weekly"
day: "monday"
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ on:
branches:
- main
paths:
- "scripts/backend_info/**"
- "scripts/attestation_policy/**"
- ".github/workflows/rust.yaml"
pull_request:
branches:
- main
paths:
- "scripts/backend_info/**"
- "scripts/attestation_policy/**"
- ".github/workflows/rust.yaml"

env:
Expand All @@ -22,7 +22,7 @@ jobs:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./scripts/backend_info
working-directory: ./scripts/attestation_policy

steps:
- name: Checkout Code
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BUILD_DIR = build
SERVICES = manager agent cli
BACKEND_INFO = backend_info
ATTESTATION_POLICY = attestation_policy
CGO_ENABLED ?= 1
GOARCH ?= amd64
VERSION ?= $(shell git describe --abbrev=0 --tags --always)
Expand All @@ -23,15 +23,15 @@ define compile_service
-o ${BUILD_DIR}/cocos-$(1) cmd/$(1)/main.go
endef

.PHONY: all $(SERVICES) $(BACKEND_INFO) install clean
.PHONY: all $(SERVICES) $(ATTESTATION_POLICY) install clean

all: $(SERVICES)

$(SERVICES):
$(call compile_service,$@)

$(BACKEND_INFO):
$(MAKE) -C ./scripts/backend_info
$(ATTESTATION_POLICY):
$(MAKE) -C ./scripts/attestation_policy

protoc:
protoc -I. --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative agent/agent.proto
Expand Down
38 changes: 19 additions & 19 deletions cli/backend_info.go → cli/attestation_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ const (
)

var (
errDecode = errors.New("base64 string could not be decoded")
errDataLength = errors.New("data does not have an adequate length")
errReadingBackendInfoFile = errors.New("error while reading the backend information file")
errUnmarshalJSON = errors.New("failed to unmarshal json")
errMarshalJSON = errors.New("failed to marshal json")
errWriteFile = errors.New("failed to write to file")
errBackendField = errors.New("the specified field type does not exist in the backend information")
errDecode = errors.New("base64 string could not be decoded")
errDataLength = errors.New("data does not have an adequate length")
errReadingAttestationPolicyFile = errors.New("error while reading the attestation policy file")
errUnmarshalJSON = errors.New("failed to unmarshal json")
errMarshalJSON = errors.New("failed to marshal json")
errWriteFile = errors.New("failed to write to file")
errAttestationPolicyField = errors.New("the specified field type does not exist in the attestation policy")
)

func (cli *CLI) NewBackendCmd() *cobra.Command {
func (cli *CLI) NewAttestationPolicyCmd() *cobra.Command {
return &cobra.Command{
Use: "backend [command]",
Short: "Change backend information",
Use: "policy [command]",
Short: "Change attestation policy",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("Change backend information\n\n")
fmt.Printf("Change attestation policy\n\n")
fmt.Printf("Usage:\n %s [command]\n\n", cmd.CommandPath())
fmt.Printf("Available Commands:\n")

Expand Down Expand Up @@ -72,8 +72,8 @@ func (cli *CLI) NewBackendCmd() *cobra.Command {
func (cli *CLI) NewAddMeasurementCmd() *cobra.Command {
return &cobra.Command{
Use: "measurement",
Short: "Add measurement to the backend info file. The value should be in base64. The second parameter is backend_info.json file",
Example: "measurement <measurement> <backend_info.json>",
Short: "Add measurement to the attestation policy file. The value should be in base64. The second parameter is attestation_policy.json file",
Example: "measurement <measurement> <attestation_policy.json>",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
if err := changeAttestationConfiguration(args[1], args[0], measurementLength, measurementField); err != nil {
Expand All @@ -87,8 +87,8 @@ func (cli *CLI) NewAddMeasurementCmd() *cobra.Command {
func (cli *CLI) NewAddHostDataCmd() *cobra.Command {
return &cobra.Command{
Use: "hostdata",
Short: "Add host data to the backend info file. The value should be in base64. The second parameter is backend_info.json file",
Example: "hostdata <host-data> <backend_info.json>",
Short: "Add host data to the attestation policy file. The value should be in base64. The second parameter is attestation_policy.json file",
Example: "hostdata <host-data> <attestation_policy.json>",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
if err := changeAttestationConfiguration(args[1], args[0], hostDataLength, hostDataField); err != nil {
Expand All @@ -111,12 +111,12 @@ func changeAttestationConfiguration(fileName, base64Data string, expectedLength

ac := check.Config{Policy: &check.Policy{}, RootOfTrust: &check.RootOfTrust{}}

backendInfo, err := os.ReadFile(fileName)
attestationPolicy, err := os.ReadFile(fileName)
if err != nil {
return errors.Wrap(errReadingBackendInfoFile, err)
return errors.Wrap(errReadingAttestationPolicyFile, err)
}

if err = protojson.Unmarshal(backendInfo, &ac); err != nil {
if err = protojson.Unmarshal(attestationPolicy, &ac); err != nil {
return errors.Wrap(errUnmarshalJSON, err)
}

Expand All @@ -126,7 +126,7 @@ func changeAttestationConfiguration(fileName, base64Data string, expectedLength
case hostDataField:
ac.Policy.HostData = data
default:
return errBackendField
return errAttestationPolicyField
}

fileJson, err := protojson.Marshal(&ac)
Expand Down
20 changes: 10 additions & 10 deletions cli/backend_info_test.go → cli/attestation_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

func TestChangeAttestationConfiguration(t *testing.T) {
tmpfile, err := os.CreateTemp("", "backend_info.json")
tmpfile, err := os.CreateTemp("", "attestation_policy.json")
require.NoError(t, err)
defer os.Remove(tmpfile.Name())

Expand Down Expand Up @@ -69,7 +69,7 @@ func TestChangeAttestationConfiguration(t *testing.T) {
expectedLength: measurementLength,
field: fieldType(999),
expectError: true,
errorType: errBackendField,
errorType: errAttestationPolicyField,
},
}

Expand Down Expand Up @@ -101,12 +101,12 @@ func TestChangeAttestationConfiguration(t *testing.T) {
}
}

func TestNewBackendCmd(t *testing.T) {
func TestNewAttestationPolicyCmd(t *testing.T) {
cli := &CLI{}
cmd := cli.NewBackendCmd()
cmd := cli.NewAttestationPolicyCmd()

assert.Equal(t, "backend [command]", cmd.Use)
assert.Equal(t, "Change backend information", cmd.Short)
assert.Equal(t, "policy [command]", cmd.Use)
assert.Equal(t, "Change attestation policy", cmd.Short)
assert.NotNil(t, cmd.Run)
}

Expand All @@ -115,8 +115,8 @@ func TestNewAddMeasurementCmd(t *testing.T) {
cmd := cli.NewAddMeasurementCmd()

assert.Equal(t, "measurement", cmd.Use)
assert.Equal(t, "Add measurement to the backend info file. The value should be in base64. The second parameter is backend_info.json file", cmd.Short)
assert.Equal(t, "measurement <measurement> <backend_info.json>", cmd.Example)
assert.Equal(t, "Add measurement to the attestation policy file. The value should be in base64. The second parameter is attestation_policy.json file", cmd.Short)
assert.Equal(t, "measurement <measurement> <attestation_policy.json>", cmd.Example)
assert.NotNil(t, cmd.Run)
}

Expand All @@ -125,7 +125,7 @@ func TestNewAddHostDataCmd(t *testing.T) {
cmd := cli.NewAddHostDataCmd()

assert.Equal(t, "hostdata", cmd.Use)
assert.Equal(t, "Add host data to the backend info file. The value should be in base64. The second parameter is backend_info.json file", cmd.Short)
assert.Equal(t, "hostdata <host-data> <backend_info.json>", cmd.Example)
assert.Equal(t, "Add host data to the attestation policy file. The value should be in base64. The second parameter is attestation_policy.json file", cmd.Short)
assert.Equal(t, "hostdata <host-data> <attestation_policy.json>", cmd.Example)
assert.NotNil(t, cmd.Run)
}
2 changes: 1 addition & 1 deletion cli/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (cli *CLI) NewCABundleCmd(fileSavePath string) *cobra.Command {
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
attestationConfiguration := check.Config{Policy: &check.Policy{}, RootOfTrust: &check.RootOfTrust{}}
err := grpc.ReadBackendInfo(args[0], &attestationConfiguration)
err := grpc.ReadAttestationPolicy(args[0], &attestationConfiguration)
if err != nil {
printError(cmd, "Error while reading manifest: %v ❌ ", err)
return
Expand Down
10 changes: 5 additions & 5 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ func main() {

keysCmd := cliSVC.NewKeysCmd()
attestationCmd := cliSVC.NewAttestationCmd()
backendCmd := cliSVC.NewBackendCmd()
attestationPolicyCmd := cliSVC.NewAttestationPolicyCmd()

// Agent Commands
rootCmd.AddCommand(cliSVC.NewAlgorithmCmd())
rootCmd.AddCommand(cliSVC.NewDatasetsCmd())
rootCmd.AddCommand(cliSVC.NewResultsCmd())
rootCmd.AddCommand(attestationCmd)
rootCmd.AddCommand(cliSVC.NewFileHashCmd())
rootCmd.AddCommand(backendCmd)
rootCmd.AddCommand(attestationPolicyCmd)
rootCmd.AddCommand(keysCmd)
rootCmd.AddCommand(cliSVC.NewCABundleCmd(directoryCachePath))

Expand All @@ -136,9 +136,9 @@ func main() {
"User Key type",
)

// Backend information commands
backendCmd.AddCommand(cliSVC.NewAddMeasurementCmd())
backendCmd.AddCommand(cliSVC.NewAddHostDataCmd())
// Attestation Policy commands
attestationPolicyCmd.AddCommand(cliSVC.NewAddMeasurementCmd())
attestationPolicyCmd.AddCommand(cliSVC.NewAddHostDataCmd())

if err := rootCmd.Execute(); err != nil {
logErrorCmd(*rootCmd, err)
Expand Down
18 changes: 9 additions & 9 deletions cmd/manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ const (
)

type config struct {
LogLevel string `env:"MANAGER_LOG_LEVEL" envDefault:"info"`
JaegerURL url.URL `env:"COCOS_JAEGER_URL" envDefault:"http://localhost:4318"`
TraceRatio float64 `env:"COCOS_JAEGER_TRACE_RATIO" envDefault:"1.0"`
InstanceID string `env:"MANAGER_INSTANCE_ID" envDefault:""`
BackendMeasurementBinary string `env:"MANAGER_BACKEND_MEASUREMENT_BINARY" envDefault:"../../build"`
EosVersion string `env:"MANAGER_EOS_VERSION" envDefault:""`
LogLevel string `env:"MANAGER_LOG_LEVEL" envDefault:"info"`
JaegerURL url.URL `env:"COCOS_JAEGER_URL" envDefault:"http://localhost:4318"`
TraceRatio float64 `env:"COCOS_JAEGER_TRACE_RATIO" envDefault:"1.0"`
InstanceID string `env:"MANAGER_INSTANCE_ID" envDefault:""`
AttestationPolicyBinary string `env:"MANAGER_ATTESTATION_POLICY_BINARY" envDefault:"../../build"`
EosVersion string `env:"MANAGER_EOS_VERSION" envDefault:""`
}

func main() {
Expand Down Expand Up @@ -115,7 +115,7 @@ func main() {
}

eventsChan := make(chan *manager.ClientStreamMessage, clientBufferSize)
svc, err := newService(logger, tracer, qemuCfg, eventsChan, cfg.BackendMeasurementBinary, cfg.EosVersion)
svc, err := newService(logger, tracer, qemuCfg, eventsChan, cfg.AttestationPolicyBinary, cfg.EosVersion)
if err != nil {
logger.Error(err.Error())
exitCode = 1
Expand Down Expand Up @@ -157,8 +157,8 @@ func main() {
}
}

func newService(logger *slog.Logger, tracer trace.Tracer, qemuCfg qemu.Config, eventsChan chan *manager.ClientStreamMessage, backendMeasurementPath string, eosVersion string) (manager.Service, error) {
svc, err := manager.New(qemuCfg, backendMeasurementPath, logger, eventsChan, qemu.NewVM, eosVersion)
func newService(logger *slog.Logger, tracer trace.Tracer, qemuCfg qemu.Config, eventsChan chan *manager.ClientStreamMessage, attestationPolicyPath string, eosVersion string) (manager.Service, error) {
svc, err := manager.New(qemuCfg, attestationPolicyPath, logger, eventsChan, qemu.NewVM, eosVersion)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion cocos-manager.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COCOS_JAEGER_TRACE_RATIO=1.0

# Manager Service Configuration
MANAGER_INSTANCE_ID=
MANAGER_BACKEND_MEASUREMENT_BINARY=../../build
MANAGER_ATTESTATION_POLICY_BINARY=../../build
MANAGER_GRPC_CLIENT_CERT=
MANAGER_GRPC_CLIENT_KEY=
MANAGER_GRPC_SERVER_CA_CERTS=
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module github.com/ultravioletrs/cocos

go 1.22.5
toolchain go1.22.9
go 1.22.7

toolchain go1.23.1

require (
github.com/absmach/magistrala v0.14.1-0.20240709113739-04c359462746
Expand Down
2 changes: 1 addition & 1 deletion manager/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The service is configured using the environment variables from the following tab
| COCOS_JAEGER_URL | The URL for the Jaeger tracing endpoint. | http://localhost:4318 |
| COCOS_JAEGER_TRACE_RATIO | The ratio of traces to sample. | 1.0 |
| MANAGER_INSTANCE_ID | The instance ID for the manager service. | |
| MANAGER_BACKEND_MEASUREMENT_BINARY | The file path for the backend measurement binary. | ../../build |
| MANAGER_ATTESTATION_POLICY_BINARY | The file path for the attestation policy binary. | ../../build |
| MANAGER_GRPC_CLIENT_CERT | The file path for the client certificate. | |
| MANAGER_GRPC_CLIENT_KEY | The file path for the client private key. | |
| MANAGER_GRPC_SERVER_CA_CERTS | The file path for the server CA certificate(s). | |
Expand Down
19 changes: 10 additions & 9 deletions manager/api/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ func (client ManagerClient) processIncomingMessage(ctx context.Context, req *man
return client.handleTerminateReq(mes)
case *manager.ServerStreamMessage_StopComputation:
go client.handleStopComputation(ctx, mes)
case *manager.ServerStreamMessage_BackendInfoReq:
go client.handleBackendInfoReq(ctx, mes)
case *manager.ServerStreamMessage_AttestationPolicyReq:
go client.handleAttestationPolicyReq(ctx, mes)
case *manager.ServerStreamMessage_SvmInfoReq:
go client.handleSVMInfoReq(ctx)
go client.handleSVMInfoReq(ctx, mes)
default:
return errors.New("unknown message type")
}
Expand Down Expand Up @@ -135,22 +135,22 @@ func (client ManagerClient) handleStopComputation(ctx context.Context, mes *mana
client.sendMessage(&manager.ClientStreamMessage{Message: msg})
}

func (client ManagerClient) handleBackendInfoReq(ctx context.Context, mes *manager.ServerStreamMessage_BackendInfoReq) {
res, err := client.svc.FetchBackendInfo(ctx, mes.BackendInfoReq.Id)
func (client ManagerClient) handleAttestationPolicyReq(ctx context.Context, mes *manager.ServerStreamMessage_AttestationPolicyReq) {
res, err := client.svc.FetchAttestationPolicy(ctx, mes.AttestationPolicyReq.Id)
if err != nil {
client.logger.Warn(err.Error())
return
}
info := &manager.ClientStreamMessage_BackendInfo{
BackendInfo: &manager.BackendInfo{
info := &manager.ClientStreamMessage_AttestationPolicy{
AttestationPolicy: &manager.AttestationPolicy{
Info: res,
Id: mes.BackendInfoReq.Id,
Id: mes.AttestationPolicyReq.Id,
},
}
client.sendMessage(&manager.ClientStreamMessage{Message: info})
}

func (client ManagerClient) handleSVMInfoReq(ctx context.Context) {
func (client ManagerClient) handleSVMInfoReq(ctx context.Context, mes *manager.ServerStreamMessage_SvmInfoReq) {
ovmfVersion, cpuNum, cpuType, eosVersion := client.svc.ReturnSVMInfo(ctx)
info := &manager.ClientStreamMessage_SvmInfo{
SvmInfo: &manager.SVMInfo{
Expand All @@ -159,6 +159,7 @@ func (client ManagerClient) handleSVMInfoReq(ctx context.Context) {
CpuType: cpuType,
KernelCmd: qemu.KernelCommandLine,
EosVersion: eosVersion,
Id: mes.SvmInfoReq.Id,
},
}
client.sendMessage(&manager.ClientStreamMessage{Message: info})
Expand Down
Loading

0 comments on commit 760c9bb

Please sign in to comment.