2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-08-29 10:49:44 -04:00
|
|
|
package cloudcmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2022-11-15 09:40:49 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
2022-08-29 10:49:44 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUpdateMeasurements(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
testCases := map[string]struct {
|
2022-11-24 10:39:33 -05:00
|
|
|
updater *stubClientInterface
|
2022-11-15 09:40:49 -05:00
|
|
|
newMeasurements measurements.M
|
2022-08-29 10:49:44 -04:00
|
|
|
wantUpdate bool
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
2022-11-24 10:39:33 -05:00
|
|
|
updater: &stubClientInterface{
|
2022-08-29 10:49:44 -04:00
|
|
|
oldMeasurements: &corev1.ConfigMap{
|
|
|
|
Data: map[string]string{
|
2022-11-24 04:57:58 -05:00
|
|
|
constants.MeasurementsFilename: `{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":false}}`,
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-15 09:40:49 -05:00
|
|
|
newMeasurements: measurements.M{
|
2022-11-24 04:57:58 -05:00
|
|
|
0: measurements.WithAllBytes(0xBB, false),
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
wantUpdate: true,
|
|
|
|
},
|
|
|
|
"measurements are the same": {
|
2022-11-24 10:39:33 -05:00
|
|
|
updater: &stubClientInterface{
|
2022-08-29 10:49:44 -04:00
|
|
|
oldMeasurements: &corev1.ConfigMap{
|
|
|
|
Data: map[string]string{
|
2022-11-24 04:57:58 -05:00
|
|
|
constants.MeasurementsFilename: `{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":false}}`,
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-15 09:40:49 -05:00
|
|
|
newMeasurements: measurements.M{
|
2022-11-24 04:57:58 -05:00
|
|
|
0: measurements.WithAllBytes(0xAA, false),
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
},
|
2022-11-24 04:57:58 -05:00
|
|
|
"trying to set warnOnly to true results in error": {
|
2022-11-24 10:39:33 -05:00
|
|
|
updater: &stubClientInterface{
|
2022-11-24 04:57:58 -05:00
|
|
|
oldMeasurements: &corev1.ConfigMap{
|
|
|
|
Data: map[string]string{
|
|
|
|
constants.MeasurementsFilename: `{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":false}}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
newMeasurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0xAA, true),
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"setting warnOnly to false is allowed": {
|
2022-11-24 10:39:33 -05:00
|
|
|
updater: &stubClientInterface{
|
2022-11-24 04:57:58 -05:00
|
|
|
oldMeasurements: &corev1.ConfigMap{
|
|
|
|
Data: map[string]string{
|
|
|
|
constants.MeasurementsFilename: `{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":true}}`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
newMeasurements: measurements.M{
|
|
|
|
0: measurements.WithAllBytes(0xAA, false),
|
|
|
|
},
|
|
|
|
wantUpdate: true,
|
|
|
|
},
|
2022-08-29 10:49:44 -04:00
|
|
|
"getCurrent error": {
|
2022-11-24 10:39:33 -05:00
|
|
|
updater: &stubClientInterface{getErr: someErr},
|
2022-08-29 10:49:44 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"update error": {
|
2022-11-24 10:39:33 -05:00
|
|
|
updater: &stubClientInterface{
|
2022-08-29 10:49:44 -04:00
|
|
|
oldMeasurements: &corev1.ConfigMap{
|
|
|
|
Data: map[string]string{
|
2022-11-24 04:57:58 -05:00
|
|
|
constants.MeasurementsFilename: `{"0":{"expected":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","warnOnly":false}}`,
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
updateErr: someErr,
|
|
|
|
},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
upgrader := &Upgrader{
|
2022-11-24 10:39:33 -05:00
|
|
|
stableInterface: tc.updater,
|
|
|
|
outWriter: &bytes.Buffer{},
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err := upgrader.updateMeasurements(context.Background(), tc.newMeasurements)
|
|
|
|
if tc.wantErr {
|
2022-11-24 04:57:58 -05:00
|
|
|
assert.Error(err)
|
2022-08-29 10:49:44 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
if tc.wantUpdate {
|
|
|
|
newMeasurementsJSON, err := json.Marshal(tc.newMeasurements)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.JSONEq(string(newMeasurementsJSON), tc.updater.updatedMeasurements.Data[constants.MeasurementsFilename])
|
|
|
|
} else {
|
|
|
|
assert.Nil(tc.updater.updatedMeasurements)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:39:33 -05:00
|
|
|
type stubClientInterface struct {
|
2022-08-29 10:49:44 -04:00
|
|
|
oldMeasurements *corev1.ConfigMap
|
|
|
|
updatedMeasurements *corev1.ConfigMap
|
2022-11-24 10:39:33 -05:00
|
|
|
k8sVersion string
|
2022-08-29 10:49:44 -04:00
|
|
|
getErr error
|
|
|
|
updateErr error
|
2022-11-24 10:39:33 -05:00
|
|
|
k8sVersionErr error
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2022-11-24 10:39:33 -05:00
|
|
|
func (u *stubClientInterface) getCurrent(context.Context, string) (*corev1.ConfigMap, error) {
|
2022-08-29 10:49:44 -04:00
|
|
|
return u.oldMeasurements, u.getErr
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:39:33 -05:00
|
|
|
func (u *stubClientInterface) update(_ context.Context, updatedMeasurements *corev1.ConfigMap) (*corev1.ConfigMap, error) {
|
2022-08-29 10:49:44 -04:00
|
|
|
u.updatedMeasurements = updatedMeasurements
|
|
|
|
return nil, u.updateErr
|
|
|
|
}
|
|
|
|
|
2022-11-24 10:39:33 -05:00
|
|
|
func (u *stubClientInterface) kubernetesVersion() (string, error) {
|
|
|
|
return u.k8sVersion, u.k8sVersionErr
|
|
|
|
}
|
|
|
|
|
2022-08-29 10:49:44 -04:00
|
|
|
func TestUpdateImage(t *testing.T) {
|
|
|
|
someErr := errors.New("error")
|
|
|
|
testCases := map[string]struct {
|
2022-11-29 05:39:07 -05:00
|
|
|
updater *stubImageUpdater
|
|
|
|
newImageReference string
|
|
|
|
newImageVersion string
|
|
|
|
wantUpdate bool
|
|
|
|
wantErr bool
|
2022-08-29 10:49:44 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
updater: &stubImageUpdater{
|
|
|
|
setImage: &unstructured.Unstructured{
|
2022-10-25 09:51:23 -04:00
|
|
|
Object: map[string]any{
|
|
|
|
"spec": map[string]any{
|
2022-11-29 05:39:07 -05:00
|
|
|
"image": "old-image-ref",
|
|
|
|
"imageVersion": "old-image-ver",
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-29 05:39:07 -05:00
|
|
|
newImageReference: "new-image-ref",
|
|
|
|
newImageVersion: "new-image-ver",
|
|
|
|
wantUpdate: true,
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
"image is the same": {
|
|
|
|
updater: &stubImageUpdater{
|
|
|
|
setImage: &unstructured.Unstructured{
|
2022-10-25 09:51:23 -04:00
|
|
|
Object: map[string]any{
|
|
|
|
"spec": map[string]any{
|
2022-11-29 05:39:07 -05:00
|
|
|
"image": "old-image-ref",
|
|
|
|
"imageVersion": "old-image-ver",
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-29 05:39:07 -05:00
|
|
|
newImageReference: "old-image-ref",
|
|
|
|
newImageVersion: "old-image-ver",
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
"getCurrent error": {
|
|
|
|
updater: &stubImageUpdater{getErr: someErr},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"update error": {
|
|
|
|
updater: &stubImageUpdater{
|
|
|
|
setImage: &unstructured.Unstructured{
|
2022-10-25 09:51:23 -04:00
|
|
|
Object: map[string]any{
|
|
|
|
"spec": map[string]any{
|
2022-11-29 05:39:07 -05:00
|
|
|
"image": "old-image-ref",
|
|
|
|
"imageVersion": "old-image-ver",
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
updateErr: someErr,
|
|
|
|
},
|
2022-11-29 05:39:07 -05:00
|
|
|
newImageReference: "new-image-ref",
|
|
|
|
newImageVersion: "new-image-ver",
|
|
|
|
wantErr: true,
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
"no spec": {
|
|
|
|
updater: &stubImageUpdater{
|
|
|
|
setImage: &unstructured.Unstructured{
|
2022-10-25 09:51:23 -04:00
|
|
|
Object: map[string]any{},
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
},
|
2022-11-29 05:39:07 -05:00
|
|
|
newImageReference: "new-image-ref",
|
|
|
|
newImageVersion: "new-image-ver",
|
|
|
|
wantErr: true,
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
"not a map": {
|
|
|
|
updater: &stubImageUpdater{
|
|
|
|
setImage: &unstructured.Unstructured{
|
2022-10-25 09:51:23 -04:00
|
|
|
Object: map[string]any{
|
2022-08-29 10:49:44 -04:00
|
|
|
"spec": "not a map",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-29 05:39:07 -05:00
|
|
|
newImageReference: "new-image-ref",
|
|
|
|
newImageVersion: "new-image-ver",
|
|
|
|
wantErr: true,
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
"no spec.image": {
|
|
|
|
updater: &stubImageUpdater{
|
|
|
|
setImage: &unstructured.Unstructured{
|
2022-10-25 09:51:23 -04:00
|
|
|
Object: map[string]any{
|
|
|
|
"spec": map[string]any{},
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-29 05:39:07 -05:00
|
|
|
newImageReference: "new-image-ref",
|
|
|
|
newImageVersion: "new-image-ver",
|
|
|
|
wantErr: true,
|
2022-08-29 10:49:44 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
upgrader := &Upgrader{
|
2022-11-24 10:39:33 -05:00
|
|
|
dynamicInterface: tc.updater,
|
|
|
|
outWriter: &bytes.Buffer{},
|
2022-08-29 10:49:44 -04:00
|
|
|
}
|
|
|
|
|
2022-11-29 05:39:07 -05:00
|
|
|
err := upgrader.updateImage(context.Background(), tc.newImageReference, tc.newImageVersion)
|
2022-08-29 10:49:44 -04:00
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(err)
|
|
|
|
if tc.wantUpdate {
|
2022-11-29 05:39:07 -05:00
|
|
|
assert.Equal(tc.newImageReference, tc.updater.updatedImage.Object["spec"].(map[string]any)["image"])
|
|
|
|
assert.Equal(tc.newImageVersion, tc.updater.updatedImage.Object["spec"].(map[string]any)["imageVersion"])
|
2022-08-29 10:49:44 -04:00
|
|
|
} else {
|
|
|
|
assert.Nil(tc.updater.updatedImage)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubImageUpdater struct {
|
|
|
|
setImage *unstructured.Unstructured
|
|
|
|
updatedImage *unstructured.Unstructured
|
|
|
|
getErr error
|
|
|
|
updateErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *stubImageUpdater) getCurrent(ctx context.Context, name string) (*unstructured.Unstructured, error) {
|
|
|
|
return u.setImage, u.getErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *stubImageUpdater) update(_ context.Context, updatedImage *unstructured.Unstructured) (*unstructured.Unstructured, error) {
|
|
|
|
u.updatedImage = updatedImage
|
|
|
|
return nil, u.updateErr
|
|
|
|
}
|