2022-10-27 05:04:23 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-06-09 09:41:02 -04:00
|
|
|
package nitrotpm
|
2022-10-27 05:04:23 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/ec2"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/vtpm"
|
2023-03-21 07:46:49 -04:00
|
|
|
"github.com/google/go-tpm-tools/proto/attest"
|
2022-10-27 05:04:23 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGeTrustedKey(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2023-03-21 07:46:49 -04:00
|
|
|
akPub []byte
|
|
|
|
info []byte
|
2022-10-27 05:04:23 -04:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"nul byte docs": {
|
2023-03-21 07:46:49 -04:00
|
|
|
akPub: []byte{0x00, 0x00, 0x00, 0x00},
|
|
|
|
info: []byte{0x00, 0x00, 0x00, 0x00},
|
2022-10-27 05:04:23 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"nil": {
|
2023-03-21 07:46:49 -04:00
|
|
|
akPub: nil,
|
|
|
|
info: nil,
|
2022-10-27 05:04:23 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2023-03-21 07:46:49 -04:00
|
|
|
out, err := getTrustedKey(
|
|
|
|
context.Background(),
|
|
|
|
vtpm.AttestationDocument{
|
|
|
|
Attestation: &attest.Attestation{
|
|
|
|
AkPub: tc.akPub,
|
|
|
|
},
|
|
|
|
InstanceInfo: tc.info,
|
|
|
|
},
|
|
|
|
nil,
|
|
|
|
)
|
2022-10-27 05:04:23 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Nil(out)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTpmEnabled(t *testing.T) {
|
|
|
|
idDocNoTPM := imds.InstanceIdentityDocument{
|
|
|
|
ImageID: "ami-tpm-disabled",
|
|
|
|
}
|
|
|
|
userDataNoTPM, _ := json.Marshal(idDocNoTPM)
|
|
|
|
attDocNoTPM := vtpm.AttestationDocument{
|
2022-11-02 10:19:13 -04:00
|
|
|
InstanceInfo: userDataNoTPM,
|
2022-10-27 05:04:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
idDocTPM := imds.InstanceIdentityDocument{
|
|
|
|
ImageID: "ami-tpm-enabled",
|
|
|
|
}
|
|
|
|
userDataTPM, _ := json.Marshal(idDocTPM)
|
|
|
|
attDocTPM := vtpm.AttestationDocument{
|
2022-11-02 10:19:13 -04:00
|
|
|
InstanceInfo: userDataTPM,
|
2022-10-27 05:04:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
attDoc vtpm.AttestationDocument
|
|
|
|
awsAPI awsMetadataAPI
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"ami with tpm": {
|
|
|
|
attDoc: attDocNoTPM,
|
|
|
|
awsAPI: &stubDescribeAPI{describeImagesTPMSupport: "v2.0"},
|
|
|
|
},
|
|
|
|
"ami without tpm": {
|
|
|
|
attDoc: attDocTPM,
|
|
|
|
awsAPI: &stubDescribeAPI{describeImagesTPMSupport: "v1.0"},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"ami undefined": {
|
|
|
|
attDoc: vtpm.AttestationDocument{},
|
|
|
|
awsAPI: &stubDescribeAPI{describeImagesErr: errors.New("failed")},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid json instanceIdentityDocument": {
|
|
|
|
attDoc: vtpm.AttestationDocument{
|
|
|
|
UserData: []byte("{invalid}"),
|
|
|
|
},
|
|
|
|
awsAPI: &stubDescribeAPI{describeImagesErr: errors.New("failed")},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
v := Validator{
|
2022-11-02 10:19:13 -04:00
|
|
|
getDescribeClient: func(context.Context, string) (awsMetadataAPI, error) {
|
2022-10-27 05:04:23 -04:00
|
|
|
return tc.awsAPI, nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-03-06 03:15:52 -05:00
|
|
|
err := v.tpmEnabled(tc.attDoc, nil)
|
2022-10-27 05:04:23 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.Nil(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubDescribeAPI struct {
|
|
|
|
describeImagesErr error
|
|
|
|
describeImagesTPMSupport string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *stubDescribeAPI) DescribeImages(
|
2023-03-20 06:03:36 -04:00
|
|
|
_ context.Context, _ *ec2.DescribeImagesInput, _ ...func(*ec2.Options),
|
2022-10-27 05:04:23 -04:00
|
|
|
) (*ec2.DescribeImagesOutput, error) {
|
|
|
|
output := &ec2.DescribeImagesOutput{
|
|
|
|
Images: []types.Image{
|
|
|
|
{TpmSupport: types.TpmSupportValues(a.describeImagesTPMSupport)},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return output, a.describeImagesErr
|
|
|
|
}
|