constellation/cli/internal/cloudcmd/validators_test.go

158 lines
4.5 KiB
Go
Raw Normal View History

/*
Copyright (c) Edgeless Systems GmbH
SPDX-License-Identifier: AGPL-3.0-only
*/
2022-04-19 11:02:02 -04:00
package cloudcmd
import (
2022-04-27 05:17:41 -04:00
"crypto/sha256"
"encoding/base64"
2022-04-19 11:02:02 -04:00
"testing"
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
2022-09-21 07:47:57 -04:00
"github.com/edgelesssys/constellation/v2/internal/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2022-04-19 11:02:02 -04:00
)
func TestValidatorUpdateInitPCRs(t *testing.T) {
zero := measurements.WithAllBytes(0x00, measurements.WarnOnly)
one := measurements.WithAllBytes(0x11, measurements.WarnOnly)
one64 := base64.StdEncoding.EncodeToString(one.Expected[:])
oneHash := sha256.Sum256(one.Expected[:])
pcrZeroUpdatedOne := sha256.Sum256(append(zero.Expected[:], oneHash[:]...))
newTestPCRs := func() measurements.M {
return measurements.M{
0: measurements.WithAllBytes(0x00, measurements.WarnOnly),
1: measurements.WithAllBytes(0x00, measurements.WarnOnly),
2: measurements.WithAllBytes(0x00, measurements.WarnOnly),
3: measurements.WithAllBytes(0x00, measurements.WarnOnly),
4: measurements.WithAllBytes(0x00, measurements.WarnOnly),
5: measurements.WithAllBytes(0x00, measurements.WarnOnly),
6: measurements.WithAllBytes(0x00, measurements.WarnOnly),
7: measurements.WithAllBytes(0x00, measurements.WarnOnly),
8: measurements.WithAllBytes(0x00, measurements.WarnOnly),
9: measurements.WithAllBytes(0x00, measurements.WarnOnly),
10: measurements.WithAllBytes(0x00, measurements.WarnOnly),
11: measurements.WithAllBytes(0x00, measurements.WarnOnly),
12: measurements.WithAllBytes(0x00, measurements.WarnOnly),
13: measurements.WithAllBytes(0x00, measurements.WarnOnly),
14: measurements.WithAllBytes(0x00, measurements.WarnOnly),
15: measurements.WithAllBytes(0x00, measurements.WarnOnly),
16: measurements.WithAllBytes(0x00, measurements.WarnOnly),
17: measurements.WithAllBytes(0x11, measurements.WarnOnly),
18: measurements.WithAllBytes(0x11, measurements.WarnOnly),
19: measurements.WithAllBytes(0x11, measurements.WarnOnly),
20: measurements.WithAllBytes(0x11, measurements.WarnOnly),
21: measurements.WithAllBytes(0x11, measurements.WarnOnly),
22: measurements.WithAllBytes(0x11, measurements.WarnOnly),
23: measurements.WithAllBytes(0x00, measurements.WarnOnly),
2022-04-27 05:17:41 -04:00
}
}
testCases := map[string]struct {
config config.AttestationCfg
2022-04-27 05:17:41 -04:00
ownerID string
clusterID string
wantErr bool
}{
"gcp update owner ID": {
config: &config.GCPSEVES{
Measurements: newTestPCRs(),
},
ownerID: one64,
2022-04-27 05:17:41 -04:00
},
"gcp update cluster ID": {
config: &config.GCPSEVES{
Measurements: newTestPCRs(),
},
2022-04-27 05:17:41 -04:00
clusterID: one64,
},
"gcp update both": {
config: &config.GCPSEVES{
Measurements: newTestPCRs(),
},
2022-04-27 05:17:41 -04:00
ownerID: one64,
clusterID: one64,
},
"azure update owner ID": {
config: &config.AzureSEVSNP{
Measurements: newTestPCRs(),
},
ownerID: one64,
2022-04-27 05:17:41 -04:00
},
"azure update cluster ID": {
config: &config.AzureSEVSNP{
Measurements: newTestPCRs(),
},
2022-04-27 05:17:41 -04:00
clusterID: one64,
},
"azure update both": {
config: &config.AzureSEVSNP{
Measurements: newTestPCRs(),
},
2022-04-27 05:17:41 -04:00
ownerID: one64,
clusterID: one64,
},
"owner ID and cluster ID empty": {
config: &config.AzureSEVSNP{
Measurements: newTestPCRs(),
},
2022-04-27 05:17:41 -04:00
},
"invalid encoding": {
config: &config.GCPSEVES{
Measurements: newTestPCRs(),
},
ownerID: "invalid",
wantErr: true,
2022-04-27 05:17:41 -04:00
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
err := UpdateInitPCRs(tc.config, tc.ownerID, tc.clusterID)
2022-04-27 05:17:41 -04:00
if tc.wantErr {
assert.Error(err)
return
}
require.NoError(t, err)
m := tc.config.GetMeasurements()
for i := 0; i < len(m); i++ {
2022-04-27 05:17:41 -04:00
switch {
case i == int(measurements.PCRIndexClusterID) && tc.clusterID == "":
2022-04-27 05:17:41 -04:00
// should be deleted
_, ok := m[uint32(i)]
2022-04-27 05:17:41 -04:00
assert.False(ok)
case i == int(measurements.PCRIndexClusterID):
pcr, ok := m[uint32(i)]
2022-04-27 05:17:41 -04:00
assert.True(ok)
assert.Equal(pcrZeroUpdatedOne[:], pcr.Expected)
2022-04-27 05:17:41 -04:00
case i == int(measurements.PCRIndexOwnerID) && tc.ownerID == "":
2022-04-27 05:17:41 -04:00
// should be deleted
_, ok := m[uint32(i)]
2022-04-27 05:17:41 -04:00
assert.False(ok)
case i == int(measurements.PCRIndexOwnerID):
pcr, ok := m[uint32(i)]
2022-04-27 05:17:41 -04:00
assert.True(ok)
assert.Equal(pcrZeroUpdatedOne[:], pcr.Expected)
2022-04-27 05:17:41 -04:00
default:
if i >= 17 && i <= 22 {
assert.Equal(one, m[uint32(i)])
} else {
assert.Equal(zero, m[uint32(i)])
}
2022-04-27 05:17:41 -04:00
}
}
})
}
}