2023-05-25 12:43:44 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2023-06-07 10:16:32 -04:00
|
|
|
package attestationconfigapi
|
2023-05-25 12:43:44 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2023-06-12 10:04:54 -04:00
|
|
|
"fmt"
|
2023-05-25 12:43:44 -04:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
2023-06-09 06:48:12 -04:00
|
|
|
"time"
|
2023-05-25 12:43:44 -04:00
|
|
|
|
2023-11-14 04:03:01 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
|
2023-09-25 05:53:02 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2023-05-25 12:43:44 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2023-11-14 04:03:01 -05:00
|
|
|
func TestFetchLatestSEVSNPVersion(t *testing.T) {
|
2023-06-12 10:04:54 -04:00
|
|
|
latestStr := "2023-06-11-14-09.json"
|
|
|
|
olderStr := "2019-01-01-01-01.json"
|
2023-06-01 07:55:46 -04:00
|
|
|
testcases := map[string]struct {
|
2023-06-09 06:48:12 -04:00
|
|
|
fetcherVersions []string
|
|
|
|
timeAtTest time.Time
|
|
|
|
wantErr bool
|
2023-11-14 04:03:01 -05:00
|
|
|
attestation variant.Variant
|
|
|
|
expectedVersion func() SEVSNPVersionAPI
|
|
|
|
olderVersion func() SEVSNPVersionAPI
|
|
|
|
latestVersion func() SEVSNPVersionAPI
|
2023-06-01 07:55:46 -04:00
|
|
|
}{
|
2023-11-14 04:03:01 -05:00
|
|
|
"get latest version azure": {
|
2023-06-12 10:04:54 -04:00
|
|
|
fetcherVersions: []string{latestStr, olderStr},
|
2023-11-14 04:03:01 -05:00
|
|
|
attestation: variant.AzureSEVSNP{},
|
|
|
|
expectedVersion: func() SEVSNPVersionAPI { tmp := latestVersion; tmp.Variant = variant.AzureSEVSNP{}; return tmp },
|
|
|
|
olderVersion: func() SEVSNPVersionAPI { tmp := olderVersion; tmp.Variant = variant.AzureSEVSNP{}; return tmp },
|
|
|
|
latestVersion: func() SEVSNPVersionAPI { tmp := latestVersion; tmp.Variant = variant.AzureSEVSNP{}; return tmp },
|
|
|
|
},
|
|
|
|
"get latest version aws": {
|
|
|
|
fetcherVersions: []string{latestStr, olderStr},
|
|
|
|
attestation: variant.AWSSEVSNP{},
|
|
|
|
expectedVersion: func() SEVSNPVersionAPI { tmp := latestVersion; tmp.Variant = variant.AWSSEVSNP{}; return tmp },
|
|
|
|
olderVersion: func() SEVSNPVersionAPI { tmp := olderVersion; tmp.Variant = variant.AWSSEVSNP{}; return tmp },
|
|
|
|
latestVersion: func() SEVSNPVersionAPI { tmp := latestVersion; tmp.Variant = variant.AWSSEVSNP{}; return tmp },
|
2023-06-09 06:48:12 -04:00
|
|
|
},
|
2023-06-01 07:55:46 -04:00
|
|
|
}
|
|
|
|
for name, tc := range testcases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
client := &http.Client{
|
|
|
|
Transport: &fakeConfigAPIHandler{
|
2023-11-14 04:03:01 -05:00
|
|
|
attestation: tc.attestation,
|
2023-06-12 10:04:54 -04:00
|
|
|
versions: tc.fetcherVersions,
|
2023-11-14 04:03:01 -05:00
|
|
|
latestDate: latestStr,
|
|
|
|
latestVersion: tc.latestVersion(),
|
|
|
|
olderDate: olderStr,
|
|
|
|
olderVersion: tc.olderVersion(),
|
2023-06-01 07:55:46 -04:00
|
|
|
},
|
|
|
|
}
|
2023-09-25 05:53:02 -04:00
|
|
|
fetcher := newFetcherWithClientAndVerifier(client, dummyVerifier{}, constants.CDNRepositoryURL)
|
2023-11-14 04:03:01 -05:00
|
|
|
res, err := fetcher.FetchSEVSNPVersionLatest(context.Background(), tc.attestation)
|
2023-06-01 07:55:46 -04:00
|
|
|
assert := assert.New(t)
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
2023-11-14 04:03:01 -05:00
|
|
|
assert.Equal(tc.expectedVersion(), res)
|
2023-06-01 07:55:46 -04:00
|
|
|
}
|
|
|
|
})
|
2023-05-25 12:43:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-14 04:03:01 -05:00
|
|
|
var latestVersion = SEVSNPVersionAPI{
|
|
|
|
SEVSNPVersion: SEVSNPVersion{
|
2023-06-09 06:48:12 -04:00
|
|
|
Microcode: 93,
|
|
|
|
TEE: 0,
|
|
|
|
SNP: 6,
|
|
|
|
Bootloader: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-11-14 04:03:01 -05:00
|
|
|
var olderVersion = SEVSNPVersionAPI{
|
|
|
|
SEVSNPVersion: SEVSNPVersion{
|
2023-06-09 06:48:12 -04:00
|
|
|
Microcode: 1,
|
|
|
|
TEE: 0,
|
|
|
|
SNP: 1,
|
|
|
|
Bootloader: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-06-01 07:55:46 -04:00
|
|
|
type fakeConfigAPIHandler struct {
|
2023-11-14 04:03:01 -05:00
|
|
|
attestation variant.Variant
|
2023-06-12 10:04:54 -04:00
|
|
|
versions []string
|
2023-11-14 04:03:01 -05:00
|
|
|
latestDate string
|
|
|
|
latestVersion SEVSNPVersionAPI
|
|
|
|
olderDate string
|
|
|
|
olderVersion SEVSNPVersionAPI
|
2023-06-01 07:55:46 -04:00
|
|
|
}
|
2023-05-25 12:43:44 -04:00
|
|
|
|
|
|
|
// RoundTrip resolves the request and returns a dummy response.
|
|
|
|
func (f *fakeConfigAPIHandler) RoundTrip(req *http.Request) (*http.Response, error) {
|
2023-11-14 04:03:01 -05:00
|
|
|
if req.URL.Path == fmt.Sprintf("/constellation/v1/attestation/%s/list", f.attestation.String()) {
|
2023-05-25 12:43:44 -04:00
|
|
|
res := &http.Response{}
|
2023-06-09 06:48:12 -04:00
|
|
|
bt, err := json.Marshal(f.versions)
|
2023-05-25 12:43:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.Body = io.NopCloser(bytes.NewReader(bt))
|
|
|
|
res.Header = http.Header{}
|
|
|
|
res.Header.Set("Content-Type", "application/json")
|
|
|
|
res.StatusCode = http.StatusOK
|
|
|
|
return res, nil
|
2023-11-14 04:03:01 -05:00
|
|
|
} else if req.URL.Path == fmt.Sprintf("/constellation/v1/attestation/%s/%s", f.attestation.String(), f.latestDate) {
|
2023-05-25 12:43:44 -04:00
|
|
|
res := &http.Response{}
|
2023-11-14 04:03:01 -05:00
|
|
|
bt, err := json.Marshal(f.latestVersion)
|
2023-06-09 06:48:12 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.Body = io.NopCloser(bytes.NewReader(bt))
|
|
|
|
res.StatusCode = http.StatusOK
|
|
|
|
return res, nil
|
|
|
|
|
2023-11-14 04:03:01 -05:00
|
|
|
} else if req.URL.Path == fmt.Sprintf("/constellation/v1/attestation/%s/%s", f.attestation.String(), f.olderDate) {
|
2023-06-09 06:48:12 -04:00
|
|
|
res := &http.Response{}
|
2023-11-14 04:03:01 -05:00
|
|
|
bt, err := json.Marshal(f.olderVersion)
|
2023-05-25 12:43:44 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res.Body = io.NopCloser(bytes.NewReader(bt))
|
|
|
|
res.StatusCode = http.StatusOK
|
|
|
|
return res, nil
|
2023-11-14 04:03:01 -05:00
|
|
|
} else if req.URL.Path == fmt.Sprintf("/constellation/v1/attestation/%s/%s.sig", f.attestation.String(), f.latestDate) {
|
2023-06-01 07:55:46 -04:00
|
|
|
res := &http.Response{}
|
2023-08-25 09:10:24 -04:00
|
|
|
res.Body = io.NopCloser(bytes.NewReader([]byte("null")))
|
2023-06-09 06:48:12 -04:00
|
|
|
res.StatusCode = http.StatusOK
|
|
|
|
return res, nil
|
|
|
|
|
2023-11-14 04:03:01 -05:00
|
|
|
} else if req.URL.Path == fmt.Sprintf("/constellation/v1/attestation/%s/%s.sig", f.attestation.String(), f.olderDate) {
|
2023-06-09 06:48:12 -04:00
|
|
|
res := &http.Response{}
|
2023-08-25 09:10:24 -04:00
|
|
|
res.Body = io.NopCloser(bytes.NewReader([]byte("null")))
|
2023-06-01 07:55:46 -04:00
|
|
|
res.StatusCode = http.StatusOK
|
|
|
|
return res, nil
|
|
|
|
|
2023-05-25 12:43:44 -04:00
|
|
|
}
|
|
|
|
return nil, errors.New("no endpoint found")
|
|
|
|
}
|
2023-06-09 06:48:12 -04:00
|
|
|
|
|
|
|
type dummyVerifier struct{}
|
|
|
|
|
2023-08-01 10:48:13 -04:00
|
|
|
func (s dummyVerifier) VerifySignature(_, _ []byte) error {
|
2023-06-09 06:48:12 -04:00
|
|
|
return nil
|
|
|
|
}
|