Skip to content

Commit 33744a1

Browse files
authored
COCOS-390 - Add IGVM measurement on manager (#404)
* resolved issue 390 * updated readme.md for issue 390 * resolved issue 390 * updated readme.md for issue 390 * implemented suggested changes * refactored code so it passes the linter test * change the Run fn so it prints the meassurement t a buffer * refactored code so it passes the linter test * fixed the test Run_-_Failure_Execution * changed recipe so it builds igvmmeasure binary when building manager
1 parent 4bb732e commit 33744a1

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ all: $(SERVICES)
3030

3131
$(SERVICES):
3232
$(call compile_service,$@)
33-
@if [ "$@" = "cli" ]; then $(MAKE) build-igvm; fi
33+
@if [ "$@" = "cli" ] || [ "$@" = "manager" ]; then $(MAKE) build-igvm; fi
3434

3535
$(ATTESTATION_POLICY):
3636
$(MAKE) -C ./scripts/attestation_policy

manager/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The service is configured using the environment variables from the following tab
1111
| COCOS_JAEGER_URL | The URL for the Jaeger tracing endpoint. | http://localhost:4318 |
1212
| COCOS_JAEGER_TRACE_RATIO | The ratio of traces to sample. | 1.0 |
1313
| MANAGER_INSTANCE_ID | The instance ID for the manager service. | |
14-
| MANAGER_ATTESTATION_POLICY_BINARY | The file path for the attestation policy binary. | ../../build |
14+
| MANAGER_ATTESTATION_POLICY_BINARY | The file path for the attestation policy and igvmmeassure binaries. | ../../build |
1515
| MANAGER_GRPC_CLIENT_CERT | The file path for the client certificate. | |
1616
| MANAGER_GRPC_CLIENT_KEY | The file path for the client private key. | |
1717
| MANAGER_GRPC_SERVER_CA_CERTS | The file path for the server CA certificate(s). | |

manager/attestation_policy.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
package manager
88

99
import (
10+
"bufio"
11+
"bytes"
1012
"context"
1113
"encoding/base64"
1214
"fmt"
@@ -16,6 +18,7 @@ import (
1618

1719
"github.com/google/go-sev-guest/proto/check"
1820
"github.com/ultravioletrs/cocos/manager/qemu"
21+
"github.com/ultravioletrs/cocos/pkg/attestation/igvmmeasure"
1922
"github.com/virtee/sev-snp-measure-go/cpuid"
2023
"github.com/virtee/sev-snp-measure-go/guest"
2124
"github.com/virtee/sev-snp-measure-go/vmmtypes"
@@ -67,11 +70,27 @@ func (ms *managerService) FetchAttestationPolicy(_ context.Context, computationI
6770
return nil, err
6871
}
6972
case vmi.Config.EnableSEVSNP:
70-
measurement, err = guest.CalcLaunchDigest(guest.SEV_SNP, vmi.Config.SMPCount, uint64(cpuid.CpuSigs[vmi.Config.CPU]), vmi.Config.OVMFCodeConfig.File, vmi.Config.KernelFile, vmi.Config.RootFsFile, strconv.Quote(qemu.KernelCommandLine), defGuestFeatures, "", vmmtypes.QEMU, false, "", 0)
73+
igvmMeasurementBinaryPath := fmt.Sprintf("%s/igvmmeasure", ms.attestationPolicyBinaryPath)
74+
75+
var stdoutBuffer bytes.Buffer
76+
var stderrBuffer bytes.Buffer
77+
78+
stdout := bufio.NewWriter(&stdoutBuffer)
79+
stderr := bufio.NewWriter(&stderrBuffer)
80+
81+
igvmMeasurement, err := igvmmeasure.NewIgvmMeasurement(igvmMeasurementBinaryPath, stderr, stdout)
7182
if err != nil {
7283
return nil, err
7384
}
85+
86+
err = igvmMeasurement.Run(ms.qemuCfg.IGVMConfig.File)
87+
if err != nil {
88+
return nil, err
89+
}
90+
91+
measurement = stdoutBuffer.Bytes()
7492
}
93+
7594
if measurement != nil {
7695
attestationPolicy.Policy.Measurement = measurement
7796
}

pkg/attestation/igvmmeasure/igvmmeasure.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package igvmmeasure
44

55
import (
6+
"bytes"
67
"fmt"
78
"io"
89
"os/exec"
@@ -43,17 +44,24 @@ func (m *IgvmMeasurement) Run(pathToFile string) error {
4344
args = append(args, "measure")
4445
args = append(args, "-b")
4546

46-
out, err := m.execCommand(binary, args...).CombinedOutput()
47-
if err != nil {
48-
fmt.Println("Error:", err)
47+
outBuf := &bytes.Buffer{}
48+
cmd := m.execCommand(binary, args...)
49+
cmd.Stderr = m.stderr
50+
cmd.Stdout = outBuf
51+
52+
if err := cmd.Run(); err != nil {
53+
return err
4954
}
50-
outputString := string(out)
55+
outputString := outBuf.String()
5156

5257
lines := strings.Split(strings.TrimSpace(outputString), "\n")
5358

5459
if len(lines) == 1 {
5560
outputString = strings.ToLower(outputString)
56-
fmt.Print(outputString)
61+
_, err := m.stdout.Write([]byte(outputString))
62+
if err != nil {
63+
return err
64+
}
5765
} else {
5866
return fmt.Errorf("error: %s", outputString)
5967
}

pkg/attestation/igvmmeasure/igvmmeasure_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func TestIgvmMeasurement(t *testing.T) {
5151
return igvm
5252
},
5353
expectErr: true,
54-
expectedErr: "error: some error occurred\nextra line",
54+
expectedErr: "exit status 1",
5555
},
5656
}
5757

0 commit comments

Comments
 (0)