2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
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"
|
|
|
|
|
2022-06-01 09:08:42 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/atls"
|
|
|
|
"github.com/edgelesssys/constellation/internal/attestation/azure"
|
|
|
|
"github.com/edgelesssys/constellation/internal/attestation/gcp"
|
|
|
|
"github.com/edgelesssys/constellation/internal/attestation/qemu"
|
|
|
|
"github.com/edgelesssys/constellation/internal/attestation/vtpm"
|
2022-06-07 05:08:44 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
2022-04-19 11:02:02 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/config"
|
2022-08-12 09:59:45 -04:00
|
|
|
"github.com/spf13/cobra"
|
2022-04-19 11:02:02 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
func TestNewValidator(t *testing.T) {
|
2022-04-27 05:17:41 -04:00
|
|
|
zero := []byte("00000000000000000000000000000000")
|
|
|
|
one := []byte("11111111111111111111111111111111")
|
|
|
|
testPCRs := map[uint32][]byte{
|
|
|
|
0: zero,
|
|
|
|
1: one,
|
|
|
|
2: zero,
|
|
|
|
3: one,
|
|
|
|
4: zero,
|
|
|
|
5: zero,
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-08-29 10:41:09 -04:00
|
|
|
provider cloudprovider.Provider
|
|
|
|
config *config.Config
|
|
|
|
pcrs map[uint32][]byte
|
|
|
|
enforceIdKeyDigest bool
|
|
|
|
idkeydigest string
|
|
|
|
wantErr bool
|
2022-04-27 05:17:41 -04:00
|
|
|
}{
|
|
|
|
"gcp": {
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
pcrs: testPCRs,
|
|
|
|
},
|
|
|
|
"azure": {
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
pcrs: testPCRs,
|
|
|
|
},
|
2022-05-02 04:54:54 -04:00
|
|
|
"qemu": {
|
|
|
|
provider: cloudprovider.QEMU,
|
|
|
|
pcrs: testPCRs,
|
|
|
|
},
|
2022-04-27 05:17:41 -04:00
|
|
|
"no pcrs provided": {
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
pcrs: map[uint32][]byte{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid pcr length": {
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
pcrs: map[uint32][]byte{0: []byte("0000000000000000000000000000000")},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"unknown provider": {
|
|
|
|
provider: cloudprovider.Unknown,
|
|
|
|
pcrs: testPCRs,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-08-29 10:41:09 -04:00
|
|
|
"set idkeydigest": {
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
pcrs: testPCRs,
|
|
|
|
idkeydigest: "414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141",
|
|
|
|
enforceIdKeyDigest: true,
|
|
|
|
},
|
|
|
|
"invalid idkeydigest": {
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
pcrs: testPCRs,
|
|
|
|
idkeydigest: "41414141414141414141414141414141414141414141414141414141414141414141414141414141414141414141414",
|
|
|
|
enforceIdKeyDigest: true,
|
|
|
|
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)
|
|
|
|
|
2022-05-16 12:54:25 -04:00
|
|
|
conf := &config.Config{Provider: config.ProviderConfig{}}
|
2022-04-27 05:17:41 -04:00
|
|
|
if tc.provider == cloudprovider.GCP {
|
2022-05-12 04:15:00 -04:00
|
|
|
measurements := config.Measurements(tc.pcrs)
|
2022-05-16 12:54:25 -04:00
|
|
|
conf.Provider.GCP = &config.GCPConfig{Measurements: measurements}
|
2022-04-27 05:17:41 -04:00
|
|
|
}
|
|
|
|
if tc.provider == cloudprovider.Azure {
|
2022-05-12 04:15:00 -04:00
|
|
|
measurements := config.Measurements(tc.pcrs)
|
2022-08-29 10:41:09 -04:00
|
|
|
conf.Provider.Azure = &config.AzureConfig{Measurements: measurements, EnforceIdKeyDigest: &tc.enforceIdKeyDigest, IdKeyDigest: tc.idkeydigest}
|
2022-04-27 05:17:41 -04:00
|
|
|
}
|
2022-05-02 04:54:54 -04:00
|
|
|
if tc.provider == cloudprovider.QEMU {
|
2022-05-12 04:15:00 -04:00
|
|
|
measurements := config.Measurements(tc.pcrs)
|
2022-05-16 12:54:25 -04:00
|
|
|
conf.Provider.QEMU = &config.QEMUConfig{Measurements: measurements}
|
2022-05-02 04:54:54 -04:00
|
|
|
}
|
2022-04-27 05:17:41 -04:00
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
validators, err := NewValidator(tc.provider, conf)
|
2022-04-27 05:17:41 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.pcrs, validators.pcrs)
|
|
|
|
assert.Equal(tc.provider, validators.provider)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
func TestValidatorV(t *testing.T) {
|
2022-04-27 05:17:41 -04:00
|
|
|
zero := []byte("00000000000000000000000000000000")
|
2022-08-29 10:41:09 -04:00
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
newTestPCRs := func() map[uint32][]byte {
|
|
|
|
return map[uint32][]byte{
|
|
|
|
0: zero,
|
|
|
|
1: zero,
|
|
|
|
2: zero,
|
|
|
|
3: zero,
|
|
|
|
4: zero,
|
|
|
|
5: zero,
|
|
|
|
6: zero,
|
|
|
|
7: zero,
|
|
|
|
8: zero,
|
|
|
|
9: zero,
|
|
|
|
10: zero,
|
|
|
|
11: zero,
|
|
|
|
12: zero,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
provider cloudprovider.Provider
|
|
|
|
pcrs map[uint32][]byte
|
2022-08-09 08:04:40 -04:00
|
|
|
wantVs atls.Validator
|
2022-04-27 05:17:41 -04:00
|
|
|
}{
|
|
|
|
"gcp": {
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
pcrs: newTestPCRs(),
|
2022-08-29 10:41:09 -04:00
|
|
|
wantVs: gcp.NewValidator(newTestPCRs(), nil, nil),
|
2022-04-27 05:17:41 -04:00
|
|
|
},
|
|
|
|
"azure": {
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
pcrs: newTestPCRs(),
|
2022-08-29 10:41:09 -04:00
|
|
|
wantVs: azure.NewValidator(newTestPCRs(), nil, nil, false, nil),
|
2022-04-19 11:02:02 -04:00
|
|
|
},
|
2022-05-02 04:54:54 -04:00
|
|
|
"qemu": {
|
|
|
|
provider: cloudprovider.QEMU,
|
|
|
|
pcrs: newTestPCRs(),
|
2022-08-29 10:41:09 -04:00
|
|
|
wantVs: qemu.NewValidator(newTestPCRs(), nil, nil),
|
2022-05-02 04:54:54 -04:00
|
|
|
},
|
2022-04-19 11:02:02 -04:00
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
validators := &Validator{provider: tc.provider, pcrs: tc.pcrs}
|
2022-04-27 05:17:41 -04:00
|
|
|
|
2022-08-12 09:59:45 -04:00
|
|
|
resultValidator := validators.V(&cobra.Command{})
|
2022-04-27 05:17:41 -04:00
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
assert.Equal(tc.wantVs.OID(), resultValidator.OID())
|
2022-04-27 05:17:41 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
func TestValidatorUpdateInitPCRs(t *testing.T) {
|
2022-04-27 05:17:41 -04:00
|
|
|
zero := []byte("00000000000000000000000000000000")
|
|
|
|
one := []byte("11111111111111111111111111111111")
|
|
|
|
one64 := base64.StdEncoding.EncodeToString(one)
|
|
|
|
oneHash := sha256.Sum256(one)
|
|
|
|
pcrZeroUpdatedOne := sha256.Sum256(append(zero, oneHash[:]...))
|
|
|
|
newTestPCRs := func() map[uint32][]byte {
|
|
|
|
return map[uint32][]byte{
|
|
|
|
0: zero,
|
|
|
|
1: zero,
|
|
|
|
2: zero,
|
|
|
|
3: zero,
|
|
|
|
4: zero,
|
|
|
|
5: zero,
|
|
|
|
6: zero,
|
|
|
|
7: zero,
|
|
|
|
8: zero,
|
|
|
|
9: zero,
|
|
|
|
10: zero,
|
|
|
|
11: zero,
|
|
|
|
12: zero,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
provider cloudprovider.Provider
|
|
|
|
pcrs map[uint32][]byte
|
|
|
|
ownerID string
|
|
|
|
clusterID string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"gcp update owner ID": {
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
pcrs: newTestPCRs(),
|
|
|
|
ownerID: one64,
|
|
|
|
},
|
|
|
|
"gcp update cluster ID": {
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
pcrs: newTestPCRs(),
|
|
|
|
clusterID: one64,
|
|
|
|
},
|
|
|
|
"gcp update both": {
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
pcrs: newTestPCRs(),
|
|
|
|
ownerID: one64,
|
|
|
|
clusterID: one64,
|
|
|
|
},
|
|
|
|
"azure update owner ID": {
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
pcrs: newTestPCRs(),
|
|
|
|
ownerID: one64,
|
|
|
|
},
|
|
|
|
"azure update cluster ID": {
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
pcrs: newTestPCRs(),
|
|
|
|
clusterID: one64,
|
|
|
|
},
|
|
|
|
"azure update both": {
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
pcrs: newTestPCRs(),
|
|
|
|
ownerID: one64,
|
|
|
|
clusterID: one64,
|
|
|
|
},
|
|
|
|
"owner ID and cluster ID empty": {
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
pcrs: newTestPCRs(),
|
|
|
|
},
|
|
|
|
"invalid encoding": {
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
pcrs: newTestPCRs(),
|
|
|
|
ownerID: "invalid",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
validators := &Validator{provider: tc.provider, pcrs: tc.pcrs}
|
2022-04-27 05:17:41 -04:00
|
|
|
|
|
|
|
err := validators.UpdateInitPCRs(tc.ownerID, tc.clusterID)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert.NoError(err)
|
|
|
|
for i := 0; i < len(tc.pcrs); i++ {
|
|
|
|
switch {
|
|
|
|
case i == int(vtpm.PCRIndexClusterID) && tc.clusterID == "":
|
|
|
|
// should be deleted
|
|
|
|
_, ok := validators.pcrs[uint32(i)]
|
|
|
|
assert.False(ok)
|
|
|
|
|
|
|
|
case i == int(vtpm.PCRIndexClusterID):
|
|
|
|
pcr, ok := validators.pcrs[uint32(i)]
|
|
|
|
assert.True(ok)
|
|
|
|
assert.Equal(pcrZeroUpdatedOne[:], pcr)
|
|
|
|
|
|
|
|
case i == int(vtpm.PCRIndexOwnerID) && tc.ownerID == "":
|
|
|
|
// should be deleted
|
|
|
|
_, ok := validators.pcrs[uint32(i)]
|
|
|
|
assert.False(ok)
|
|
|
|
|
|
|
|
case i == int(vtpm.PCRIndexOwnerID):
|
|
|
|
pcr, ok := validators.pcrs[uint32(i)]
|
|
|
|
assert.True(ok)
|
|
|
|
assert.Equal(pcrZeroUpdatedOne[:], pcr)
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert.Equal(zero, validators.pcrs[uint32(i)])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUpdatePCR(t *testing.T) {
|
|
|
|
emptyMap := map[uint32][]byte{}
|
|
|
|
defaultMap := map[uint32][]byte{
|
|
|
|
0: []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"),
|
|
|
|
1: []byte("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"),
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
pcrMap map[uint32][]byte
|
|
|
|
pcrIndex uint32
|
|
|
|
encoded string
|
|
|
|
wantEntries int
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"empty input, empty map": {
|
|
|
|
pcrMap: emptyMap,
|
|
|
|
pcrIndex: 10,
|
|
|
|
encoded: "",
|
|
|
|
wantEntries: 0,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
"empty input, default map": {
|
|
|
|
pcrMap: defaultMap,
|
|
|
|
pcrIndex: 10,
|
|
|
|
encoded: "",
|
|
|
|
wantEntries: len(defaultMap),
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
"correct input, empty map": {
|
|
|
|
pcrMap: emptyMap,
|
|
|
|
pcrIndex: 10,
|
|
|
|
encoded: base64.StdEncoding.EncodeToString([]byte("Constellation")),
|
|
|
|
wantEntries: 1,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
"correct input, default map": {
|
|
|
|
pcrMap: defaultMap,
|
|
|
|
pcrIndex: 10,
|
|
|
|
encoded: base64.StdEncoding.EncodeToString([]byte("Constellation")),
|
|
|
|
wantEntries: len(defaultMap) + 1,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
"unencoded input, empty map": {
|
|
|
|
pcrMap: emptyMap,
|
|
|
|
pcrIndex: 10,
|
|
|
|
encoded: "Constellation",
|
|
|
|
wantEntries: 0,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"unencoded input, default map": {
|
|
|
|
pcrMap: defaultMap,
|
|
|
|
pcrIndex: 10,
|
|
|
|
encoded: "Constellation",
|
|
|
|
wantEntries: len(defaultMap),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"empty input at occupied index": {
|
|
|
|
pcrMap: defaultMap,
|
|
|
|
pcrIndex: 0,
|
|
|
|
encoded: "",
|
|
|
|
wantEntries: len(defaultMap) - 1,
|
|
|
|
wantErr: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
pcrs := make(map[uint32][]byte)
|
|
|
|
for k, v := range tc.pcrMap {
|
|
|
|
pcrs[k] = v
|
|
|
|
}
|
|
|
|
|
2022-08-09 08:04:40 -04:00
|
|
|
validators := &Validator{
|
2022-04-27 05:17:41 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
pcrs: pcrs,
|
|
|
|
}
|
|
|
|
err := validators.updatePCR(tc.pcrIndex, tc.encoded)
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
assert.Len(pcrs, tc.wantEntries)
|
|
|
|
for _, v := range pcrs {
|
|
|
|
assert.Len(v, 32)
|
2022-04-19 11:02:02 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|